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: StringIO.write() takes any argument and converts it to a string
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7, Python 2.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: pitrou, skip.montanaro, tarek
Priority: normal Keywords:

Created on 2010-04-12 23:56 by tarek, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg102991 - (view) Author: Tarek Ziadé (tarek) * (Python committer) Date: 2010-04-12 23:56
Looks like the python version of StringIO can write tuples, but not the C version. I am not sure this is intended:

>>> from cStringIO import StringIO as cStringIO
>>> from StringIO import StringIO as StringIO
>>> string = StringIO()
>>> string.write(('my', 'tuple'))
>>> cstring = cStringIO()
>>> cstring.write(('my', 'tuple'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write() argument 1 must be string or read-only character buffer, not tuple
msg102993 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-04-13 00:05
Probably not. I guess an implicit str() is done on the argument given to write():

>>> s = StringIO()
>>> s.write((1,2))
>>> s.write(3)
>>> s.getvalue()
'(1, 2)3'
msg102994 - (view) Author: Tarek Ziadé (tarek) * (Python committer) Date: 2010-04-13 00:10
Yes, I am not sure what would be the best fix though. We cannot raise an error on StringIO now because this will break lots of code out there. 

In the meantime, I think it's cleaner to avoid doing implicit str() conversions, so I think cStringIO has a better approach.

I would be in favor of a status quo for 2.x
msg103005 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2010-04-13 03:01
It's clear you can't change it for 2.6 or 2.7, almost certainly not 3.1.
Maybe you could change it for 3.2.
msg103006 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2010-04-13 03:04
Whoops.  No stringio.py in 3.x.  This should be closed as won't fix since it's not a problem in py3k and can't be changed in 2.x.
History
Date User Action Args
2022-04-11 14:56:59adminsetgithub: 52629
2010-04-13 12:09:14r.david.murraysetstatus: open -> closed
resolution: wont fix
stage: resolved
2010-04-13 03:04:50skip.montanarosetmessages: + msg103006
2010-04-13 03:01:05skip.montanarosetnosy: + skip.montanaro
messages: + msg103005
2010-04-13 00:10:18tareksetmessages: + msg102994
2010-04-13 00:07:49pitrousettitle: cStringIO and StringIO don't behave the same way -> StringIO.write() takes any argument and converts it to a string
2010-04-13 00:05:13pitrousetpriority: normal
nosy: + pitrou
messages: + msg102993

2010-04-12 23:56:59tareksettitle: cStringIO and StringIO doesn't behave the same way -> cStringIO and StringIO don't behave the same way
2010-04-12 23:56:51tarekcreate