This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: cStringIO().write TypeError
Type: Stage:
Components: Library (Lib) Versions: Python 2.2
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: lemburg Nosy List: brett.cannon, kgk, lemburg, loewis
Priority: normal Keywords:

Created on 2002-11-09 02:30 by kgk, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Messages (6)
msg13149 - (view) Author: kris kvilekval (kgk) Date: 2002-11-09 02:30

There is an incompatibilty between StringIO and 
cStringIO, as demonstrated below..  Passing an
integer to StringIO.write is OK while
it not OK to pass one to cStringIO.

$ python
Python 2.2.2 (#4, Oct 15 2002, 04:21:28) 
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> import StringIO
>>> sfile = StringIO.StringIO()
>>> sfile.write (100)
>>> print sfile.getvalue()
100
>>> import cStringIO
>>> cfile = cStringIO.StringIO()
>>> cfile.write (100)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: write() argument 1 must be string or
read-only buffer, not int
>>> cfile.write (str(100))
msg13150 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-11-09 18:04
Logged In: YES 
user_id=21627

This is by design. The file protocol makes no guarantee that
you can write objects other than strings into a file, unless
the object explicitly documents the support for other objects.

cStringIO differs in many ways from StringIO, this is but
one way.
msg13151 - (view) Author: kris kvilekval (kgk) Date: 2002-11-09 21:09
Logged In: YES 
user_id=569286

This seems like an unnecessary incompatibility,
however I guess it is there in the interest of speed?
msg13152 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-11-09 21:35
Logged In: YES 
user_id=21627

It's a bug that StringIO accepts it. file.write does not
support numbers, either. Unfortunately, this bug cannot be
fixed, for backwards compatibility. The conversion of any
argument to a string was originally introduced for Python
2.2, to support arbitrary buffer objects. That it also
accepts numbers now was an unexpected side effect.

Assigning to Marc-Andre, as he introduced that feature
originally.
msg13153 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2003-05-22 02:20
Logged In: YES 
user_id=357491

If this bug cannot be fixed then shouldn't this bug report be closed?
msg13154 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2003-05-22 06:49
Logged In: YES 
user_id=21627

Indeed.
History
Date User Action Args
2022-04-10 16:05:51adminsetgithub: 37443
2002-11-09 02:30:57kgkcreate