[Python-bugs-list] [Bug #116787] StringIO does not check constructor argument type

noreply@sourceforge.net noreply@sourceforge.net
Fri, 13 Oct 2000 07:28:04 -0700


Bug #116787, was updated on 2000-Oct-13 06:33
Here is a current snapshot of the bug.

Project: Python
Category: Library
Status: Closed
Resolution: Invalid
Bug Group: Not a Bug
Priority: 5
Summary: StringIO does not check constructor argument type

Details: Python 2.0c1 (#7, Oct  9 2000, 18:44:43) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.6 -- press F1 for help
>>> import StringIO
>>> file = open("C:/Python20/readme.txt")
>>> fstr = StringIO.StringIO(file.readlines())

# The above line differs from the behavior of cStringIO, which correctly
# reports: "TypeError: expected string, list found"
# Doom is inevitable after this...

>>> len(fstr.getvalue())
838
>>> fstr.readline()
Traceback (innermost last):
  File "<pyshell#4>", line 1, in ?
    fstr.readline()
  File "c:\python20\lib\StringIO.py", line 85, in readline
    i = string.find(self.buf, '\n', self.pos)
  File "c:\python20\lib\string.py", line 172, in find
    return s.find(*args)
AttributeError: find
>>> 

Follow-Ups:

Date: 2000-Oct-13 07:07
By: fdrake

Comment:
I'm not convinced this is a bug.  Is there a reason StringIO should not accept any sequence object that returns characters for each position?  Unicode and UserString objects should be acceptable. This seems particularly difficult to test for up front.  I think the allowance of only 8-bit strings should be documented as a limitation of the currrent implementation of cStringIO.

Guido will need to make a pronouncement here.  Adding a test for the input type being a string or Unicode object would be easy enough.  Guido, feel free to assign back to me to handle this based on your decision.
-------------------------------------------------------

Date: 2000-Oct-13 07:23
By: gvanrossum

Comment:
Eh, Fred, he didn't complain about Unicode string. The argument was totally bogus (a list of strings).

But I agree that it's not a bug -- we can't possibly put type checks in all library routines. The argument has a bad type and (eventually) you get an exception. That's all we promise.

Incidentally, StringIO *does* seem to support Unicode strings "just right" (unlike cStringIO, which attempts to coerce Unicode args to ASCII first).
-------------------------------------------------------

Date: 2000-Oct-13 07:28
By: holdenweb

Comment:
I guess I was mostly looking at the inconsistency between cStringIO and StringIO.
It seems reasonable that a C module should check more rigorously, now you point
it out, since the results are more likely to be fatal under those circumstances.

Thanks for looking.  Delete this bug as necessary.

-------------------------------------------------------

For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=116787&group_id=5470