Path: csiph.com!usenet.pasdenom.info!goblin1!goblin.stu.neva.ru!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'resulting': 0.04; 'argument': 0.05; '(python': 0.07; 'revision': 0.07; 'file)': 0.09; 'to:addr:pobox.com': 0.09; 'to:addr:skip': 0.09; 'variable,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'suggest': 0.14; '2.7': 0.14; 'creates': 0.14; '%d,': 0.16; "%s'": 0.16; '23,': 0.16; '8-bit': 0.16; '[*]': 0.16; 'accepts': 0.16; 'bit.': 0.16; 'excerpt': 0.16; 'expected,': 0.16; 'expects': 0.16; 'lang': 0.16; 'stringio': 0.16; 'subject:between': 0.16; 'typeerror:': 0.16; 'unicode?': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'result.': 0.19; 'thu,': 0.19; 'input': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; '2.x': 0.24; 'skip': 0.24; 'string,': 0.24; 'switched': 0.24; 'unicode': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'fixed': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'url:mailman': 0.30; '"",': 0.31; '13,': 0.31; '>>>>': 0.31; 'helpful.': 0.31; 'python2.7': 0.31; 'file': 0.32; 'url:python': 0.33; '(i.e.': 0.33; '(most': 0.33; 'older': 0.33; "i'd": 0.34; 'something': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'skip:" 50': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'branch': 0.38; 'pm,': 0.38; 'little': 0.38; 'recent': 0.39; 'pdf': 0.39; 'system.': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'url:mail': 0.40; 'how': 0.40; 'skip:t 30': 0.61; 'from:charset:utf-8': 0.61; 'back': 0.62; 'more': 0.64; '30,': 0.65; 'url:pdf': 0.68; 'default': 0.69; '12.1': 0.84; '2.7.': 0.84; 'subject:skip:S 10': 0.84; '2013,': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=7PQpaZiCnZV77JyRpei7T7duyCqOARIrWZR4FvOUvnU=; b=Bvjw+soELY0EyS+qaqW7rhMeR1BPNUKBAGA+0cIdRxBpvScmez4Ibq9XMQCWDeeu19 8/xWAzK5GANercrToZdBqpBRGJjyrBnZKq9bKbSsikRktq4tN/t8pvDFeP/dqJwjiDZh AAMjIrsP2MASKCw6kI6vBpYJe7a1TwfD4HBjSjrjuymXBkz3bvdi75pRBkZNfH/+szv8 oCwhVkNKz/xwxxr9UyRQjhIPm4noS2mwZidoBj7EbI20UVHleaR/x9ULU/vry3sto9gG 7KasL4KicERPNS2b+vZzk60uYNdk7zsx/UkGzgwdQs5zNspxWf2HwFVwnNMpNDGpMyyE v25w== MIME-Version: 1.0 X-Received: by 10.50.141.230 with SMTP id rr6mr409401igb.89.1369955345756; Thu, 30 May 2013 16:09:05 -0700 (PDT) In-Reply-To: References: Date: Fri, 31 May 2013 02:09:05 +0300 Subject: Re: Surprising difference between StringIO.StringIO and io.StringIO From: =?UTF-8?B?R8O2a3R1xJ8gS2F5YWFscA==?= To: Skip Montanaro Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: Python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369955745 news.xs4all.nl 15926 [2001:888:2000:d::a6]:48248 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46554 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/py= thon/python2python3.pdf Greetings, G=C3=B6ktu=C4=9F. On Thu, May 30, 2013 at 11:46 PM, Skip Montanaro 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 =3D StringIO.StringIO() >>>> traceback.print_stack(file=3Ds1) >>>> print s1.getvalue() > File "", line 1, in > >>>> >>>> import io >>>> s2 =3D io.StringIO() >>>> traceback.print_stack(file=3Ds2) > Traceback (most recent call last): > File "", line 1, in > 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