Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97439

Re: Python 2 ‘print’, coercing arguments to Unicode

Path csiph.com!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; '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:plane.gmane.org': 0.16; 'subject:Unicode': 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: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; 'robert': 0.35; 'unicode': 0.35; 'should': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'still': 0.40; 'skip:u 10': 0.61; 'more': 0.63; 'inherit': 0.66; 'here': 0.66; 'skip:\xe2 10': 0.70; 'subjectcharset:utf-8': 0.71; '8bit%:43': 0.72; '8bit%:46': 0.76; '5.2.1': 0.84; '_o__)': 0.84; 'received:125': 0.84; '8bit%:18': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Python 2 ‘print’, coercing arguments to Unicode
Date Tue, 06 Oct 2015 21:45:03 +1100
References <851td81g08.fsf@benfinney.id.au>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock sha1:X5+ugEHEmwoRcYCtFHN8GSn7MXQ=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.417.1444128313.28679.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1444128313 news.xs4all.nl 23758 [2001:888:2000:d::a6]:40895
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:97439

Show key headers only | View raw


Ben Finney <ben+python@benfinney.id.au> 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 "<stdin>", line 1, in <module>
>     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 "<stdin>", line 1, in <module>
    TypeError: unicode argument expected, got 'str'
    >>> print None
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    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’?

-- 
 \          “Pity the meek, for they shall inherit the earth.” —Donald |
  `\                                              Robert Perry Marquis |
_o__)                                                                  |
Ben Finney

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python 2 ‘print’, coercing arguments to Unicode Ben Finney <ben+python@benfinney.id.au> - 2015-10-06 21:45 +1100

csiph-web