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


Groups > comp.lang.python > #97438

Python 2 ‘print’, coercing arguments to Unicode

Path csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.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.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'subject:Python': 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; 'typeerror:': 0.09; 'python': 0.10; 'argument': 0.15; 'expected,': 0.16; 'literals': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'statements;': 0.16; 'subject:Unicode': 0.16; 'suite.': 0.16; 'string': 0.17; 'bytes': 0.18; 'module,': 0.18; '>>>': 0.20; 'all,': 0.20; '"",': 0.22; 'arguments': 0.22; 'sep': 0.22; 'code,': 0.23; 'replacing': 0.23; 'import': 0.24; '(most': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'header:X -Complaints-To:1': 0.26; 'behaviour': 0.29; 'convince': 0.29; 'str': 0.29; 'objects': 0.29; "i'm": 0.30; 'option': 0.31; 'etc.)': 0.32; 'instances': 0.33; 'traceback': 0.33; 'throughout': 0.34; 'file': 0.34; 'so,': 0.35; 'unicode': 0.35; 'but': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'being': 0.37; 'received:org': 0.37; 'test': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'skip:u 10': 0.61; 'avoid': 0.61; 'more': 0.63; 'great': 0.63; 'binding': 0.66; 'choose': 0.68; 'skip:\xe2 10': 0.70; 'products': 0.70; 'subjectcharset:utf-8': 0.71; '8bit%:43': 0.72; 'yourself,': 0.72; '8bit%:46': 0.76; '5.2.1': 0.84; '_o__)': 0.84; 'received:125': 0.84; 'streams': 0.84; '8bit%:33': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Python 2 ‘print’, coercing arguments to Unicode
Date Tue, 06 Oct 2015 20:51:51 +1100
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:Dd57cw3CcL6c8z1ucPzqWbr+GLY=
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.416.1444125125.28679.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1444125125 news.xs4all.nl 23743 [2001:888:2000:d::a6]:54395
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:97438

Show key headers only | View raw


Howdy all,

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’.

Binding the ‘str’ name to the Unicode type doesn't help::

    >>> str = unicode
    >>> print(None, file=io.StringIO())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unicode argument expected, got 'str'

The reason I need to do this is that I'm replacing the standard streams
(‘sys.stderr’, etc.) with ‘io.StringIO’ instances in a test suite. That
works great for everything but ‘print’.

Since this is a test suite for existing code, I don't have the option to
change all the existing statements; I need them to work as-is.

How can I convince ‘print’, everywhere throughout a module, that it
should coerce its arguments using ‘unicode’?

-- 
 \      “Not using Microsoft products is like being a non-smoker 40 or |
  `\     50 years ago: You can choose not to smoke, yourself, but it's |
_o__)               hard to avoid second-hand smoke.” —Michael Tiemann |
Ben Finney

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


Thread

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

csiph-web