io.StringIO only accepts Unicode input (i.e. u"multibyte string"), while StringIO.StringIO accepts either 8 bit input or unicode input. As you can see in the following excerpt from your traceback, the 'print_list' function creates an 8-bit string, which is then (probably) passed to 'file.write' as the str variable, which actually expects a unicode string, resulting in a TypeError:
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) Python 2.x strings default to 8 bit. This is fixed (!) in Python 3. I'd suggest switching to Python 3 if there is no reason to use 2.x. I found this [*] little PDF back when I switched from 2 to 3, which was helpful. [*] http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf Greetings, Göktuğ. On Thu, May 30, 2013 at 11:46 PM, Skip Montanaro <s...@pobox.com> wrote: > 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 > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list