classification
Title: io.StringIO allows any parameter
Type: behavior Stage:
Components: Extension Modules Versions: Python 3.0
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, christian.heimes, georg.brandl, zanella
Priority: high Keywords:

Created on 2008-02-01 11:18 by georg.brandl, last changed 2008-02-26 06:57 by georg.brandl. This issue is now closed.

Messages (6)
msg61957 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-02-01 11:18
>>> x = io.StringIO(1)
>>> x.read()
'1'
msg62944 - (view) Author: Rafael Zanella (zanella) Date: 2008-02-24 21:57
I believe you're referring to StringIO, if so, it changes the parameter
received to a string:

"""
class StringIO:
  def __init__(self, buf = ''):
        # Force self.buf to be a string or unicode
        if not isinstance(buf, basestring):
            buf = str(buf)
"""
msg62946 - (view) Author: Rafael Zanella (zanella) Date: 2008-02-24 22:02
oops, stupid me, this a 3.0 issue..., well seems the str() conversion is
done as well on the 3.0 io module:

"""
class StringIO(TextIOWrapper):
   def __init__(self, initial_value="", encoding="utf-8",
                 errors="strict", newline="\n"):
        super(StringIO, self).__init__(BytesIO(),
                                       encoding=encoding,
                                       errors=errors,
                                       newline=newline)
        if initial_value:
            if not isinstance(initial_value, str):
                initial_value = str(initial_value)
"""
msg62949 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-02-24 22:32
In Python 2.x StringIO.StringIO calls str() on its arguments:

>>> StringIO.StringIO(1).read()
'1'
>>> StringIO.StringIO(object).read()
"<type 'object'>"
>>> str(object)
"<type 'object'>"

io.StringIO has the same behavior:
>>> io.StringIO(1).read()
'1'
>>> io.StringIO(object).read()
"<type 'object'>"

Georg, what's the bug here?
msg63016 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-02-25 23:33
According to the docs: "When a StringIO object is created, it can be
initialized to an existing string by passing the string to the constructor."
So, it seems that that the docs are being too strict, or the code is
being too open. The conversion to str in the constructor is explicit, so
I'd favor leaving it be.
msg63033 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-02-26 06:57
Okay, then I see this as a intended feature.
History
Date User Action Args
2008-02-26 06:57:59georg.brandlsetstatus: open -> closed
resolution: wont fix
messages: + msg63033
2008-02-25 23:33:22benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg63016
2008-02-24 22:32:36christian.heimessetnosy: + christian.heimes
messages: + msg62949
2008-02-24 22:02:29zanellasetmessages: + msg62946
2008-02-24 21:57:03zanellasetnosy: + zanella
messages: + msg62944
2008-02-01 11:18:37georg.brandlcreate