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 should implement __str__()
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: cinamod, rhettinger
Priority: normal Keywords:

Created on 2006-04-25 20:11 by cinamod, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg28344 - (view) Author: Dom Lachowicz (cinamod) Date: 2006-04-25 20:11
I was a bit surprised when I discovered that StringIO
didn't have a __str__() method, equivalent to
getvalue(). Calling str(stringio_object) gives the
following:

>>> import StringIO
>>> s = StringIO.StringIO("hello world")
>>> print "%s" % (str(s))
<StringIO.StringIO instance at 0x401ef08c>

I had some (perhaps dodgy code) code that did:

if isinstance(data, types.FileType):
  return data.read()
else:
  return str(data)

Since StringIO doesn't subclass any file type and
doesn't implement a __str__() method, I was getting
seemingly bogus results. This was trivially worked
around by adding another "isinstance(data,
StringIO.StringIO)" case, but it was surprising
nonetheless. Thanks.
msg28345 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2006-04-25 22:58
Logged In: YES 
user_id=80475

This isn't a bug.  The module is correctly emulating the API
for file objects.

I recommend replacing your isintance() test with a try/except:

try:
.... return data.read()
except AttributeError:
.... return str(data)

msg28346 - (view) Author: Dom Lachowicz (cinamod) Date: 2006-04-26 02:53
Logged In: YES 
user_id=69417

The StringIO API diverges from the file API already. It adds
API (getvalue()) doesn't implement the encoding, mode, name,
newlines, or softspace attributes, and implements isatty()
in violation of the section 2.3.9 docs.

In some contexts, it may look like a file, but it isn't a
file. And I don't see how implementing __str__() changes
that. It's equivalent to saying that Java's StringBuffer
class shouldn't override toString()...
msg28347 - (view) Author: Dom Lachowicz (cinamod) Date: 2006-04-26 02:54
Logged In: YES 
user_id=69417

The StringIO API diverges from the file API already. It adds
API (getvalue()) doesn't implement the encoding, mode, name,
newlines, or softspace attributes, and implements isatty()
in violation of the section 2.3.9 docs.

In some contexts, it may look like a file, but it isn't a
file. And I don't see how implementing __str__() changes
that. It's equivalent to saying that Java's StringBuffer
class shouldn't override toString()...
msg28348 - (view) Author: Dom Lachowicz (cinamod) Date: 2006-04-26 15:09
Logged In: YES 
user_id=69417

Since section 2.3.9 of the docs doesn't mention anything
about an overriden meaning of FILE.__str__(), its meaning in
that API is undefined. "Correctly emulating" undefined API
is impossible.
History
Date User Action Args
2022-04-11 14:56:17adminsetgithub: 43277
2006-04-25 20:11:40cinamodcreate