Surprising difference between StringIO.StringIO and io.StringIO
Skip Montanaro
skip at pobox.com
Thu May 30 16:46:41 EDT 2013
Consider this quick session (Python 2.7 using the tip of the 2.7
branch in Mercurial):
% python2.7
Python 2.7.5+ (2.7:93eb15779050, May 30 2013, 15:27:39)
[GCC 4.4.6 [TWW]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import traceback
>>>
>>> import StringIO
>>> s1 = StringIO.StringIO()
>>> traceback.print_stack(file=s1)
>>> print s1.getvalue()
File "<stdin>", line 1, in <module>
>>>
>>> import io
>>> s2 = io.StringIO()
>>> traceback.print_stack(file=s2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/skipm/x86_64-linux3.1/lib/python2.7/traceback.py", line
269, in print_stack
print_list(extract_stack(f, limit), file)
File "/home/skipm/x86_64-linux3.1/lib/python2.7/traceback.py", line
23, in print_list
' File "%s", line %d, in %s' % (filename,lineno,name))
File "/home/skipm/x86_64-linux3.1/lib/python2.7/traceback.py", line
13, in _print
file.write(str+terminator)
TypeError: unicode argument expected, got 'str'
>>> print s2.getvalue()
What is it about io.StringIO that it doesn't like strings and requires
Unicode? This is on an OpenSUSE 12.1 system. I have tried with LANG
set to the default ("en_US.UTF-8") and to "C". I also tried on a
Solaris system with an older micro revision of Python 2.7. Same
result.
Am I missing something about how io.StringIO works? I thought it was
a more-or-less drop-in replacement for StringIO.StringIO.
Thx,
Skip
More information about the Python-list
mailing list