Path: csiph.com!eternal-september.org!feeder.eternal-september.org!gegeweb.org!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'sys': 0.05; 'objects,': 0.07; '__future__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sys.stdout': 0.09; 'typeerror:': 0.09; 'python': 0.10; 'def': 0.13; 'argument': 0.15; 'explicitly': 0.15; 'coerced': 0.16; 'expected,': 0.16; 'literal,': 0.16; 'literals': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Unicode': 0.16; 'wrote:': 0.16; 'string': 0.17; 'bytes': 0.18; 'module,': 0.18; '>>>': 0.20; '"",': 0.22; 'arguments': 0.22; 'sep': 0.22; 'appears': 0.23; 'import': 0.24; '(most': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:_ 20': 0.26; 'function': 0.28; 'skip:u 20': 0.28; 'behaviour': 0.29; 'convince': 0.29; 'objects': 0.29; 'print': 0.30; 'another': 0.32; 'foo': 0.33; 'traceback': 0.33; 'throughout': 0.34; 'file': 0.34; 'so,': 0.35; 'replaced': 0.35; 'unicode': 0.35; 'but': 0.36; 'should': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:org': 0.37; 'anything': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.40; 'still': 0.40; 'received:de': 0.40; 'skip:u 10': 0.61; 'more': 0.63; 'statement,': 0.66; 'here': 0.66; 'skip:\xe2 10': 0.70; 'subjectcharset:utf-8': 0.71; '8bit%:43': 0.72; '8bit%:46': 0.76; '**kw)': 0.84; '**kw):': 0.84; '5.2.1': 0.84; '8bit%:18': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Python 2 =?UTF-8?B?4oCYcHJpbnTigJks?= coercing arguments to Unicode Date: Tue, 06 Oct 2015 16:30:31 +0200 Organization: None References: <851td81g08.fsf@benfinney.id.au> <85wpv0z368.fsf@benfinney.id.au> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p57bd8428.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 76 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1444141848 news.xs4all.nl 23741 [2001:888:2000:d::a6]:51637 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97445 Ben Finney wrote: > Ben Finney writes: > >> In Python 2.7, I am seeing this behaviour for ‘print’:: >> >> Python 2.7.10 (default, Sep 13 2015, 20:30:50) >> [GCC 5.2.1 20150911] on linux2 >> Type "help", "copyright", "credits" or "license" for more >> information. >> >>> from __future__ import unicode_literals >> >>> from __future__ import print_function >> >>> import io >> >>> print(None) >> None >> >>> print(None, file=io.StringIO()) >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: unicode argument expected, got 'str' >> >> So, although my string literals are now Unicode objects, apparently >> ‘print’ still coerces objects using the bytes type ‘str’. > > To eliminate ‘from __future__ import print_function’ as a possible > factor, here is another demonstration without that:: > > Python 2.7.10 (default, Sep 13 2015, 20:30:50) > [GCC 5.2.1 20150911] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> from __future__ import unicode_literals > >>> import sys > >>> import io > >>> print "foo" > foo > >>> print None > None > >>> sys.stdout = io.StringIO() > >>> print "foo" > Traceback (most recent call last): > File "", line 1, in > TypeError: unicode argument expected, got 'str' > >>> print None > Traceback (most recent call last): > File "", line 1, in > TypeError: unicode argument expected, got 'str' > > So it appears that even a string literal, which is explicitly Unicode by > the above ‘from __future__ import unicode_literals’, is still being > coerced to a bytes ‘str’ object by ‘print’. > > How can I convince ‘print’, everywhere throughout a module, that it > should coerce its arguments using ‘unicode’? I don't think this is possible with the print statement, but the print() function can be replaced with anything you like: $ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from __future__ import unicode_literals >>> from __future__ import print_function >>> import io >>> _print = print >>> def print(*args, **kw): ... return _print(*map(unicode, args), **kw) ... >>> print(None, file=io.StringIO()) >>> outstream = io.StringIO() >>> print(None, file=outstream) >>> outstream.getvalue() u'None\n' >>> print(None) None