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


Groups > comp.lang.python > #112294

Re: print() function with encoding= and errors= parameters?

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: print() function with encoding= and errors= parameters?
Date 2016-08-03 17:09 +0200
Organization None
Message-ID <mailman.147.1470237003.6033.python-list@python.org> (permalink)
References <1470227371.397715.684773553.140807A6@webmail.messagingengine.com> <1470233112.2667238.684861393.67022A98@webmail.messagingengine.com> <nnt1fv$9en$1@blaine.gmane.org>

Show all headers | View raw


Random832 wrote:

> On Wed, Aug 3, 2016, at 08:29, Malcolm Greene wrote:
>> Looking for a way to use the Python 3 print() function with encoding and
>> errors parameters.

>> Are there any concerns with closing and re-opening sys.stdout so
>> sys.stdout has a specific encoding and errors behavior? Would this break
>> other standard libraries that depend on sys.stdout being configured a
>> specific way?

Can you give an example of what you have in mind? One that would not be 
considered a bug in said library?

> You could duplicate stdout [open(os.dup(sys.stdout.fileno()), ...)]
> 
> You could make an IO class which sends bytes output to sys.stdout.buffer
> but won't close it when closed (you know, it'd be nice to be able to
> have "not owned underlying stream" as an option of the standard IO
> classes)
> 
> If your output isn't going to be long, you could print everything to a
> StringIO and then write to sys.stdout.buffer at the end.

I'm unsure about this myself -- wouldn't it be better to detach the 
underlying raw stream? Like

>>> import sys, io
>>> print("ähnlich üblich möglich")
ähnlich üblich möglich
>>> sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding="ascii", 
errors="xmlcharrefreplace")
>>> print("ähnlich üblich möglich")
&#228;hnlich &#252;blich m&#246;glich

The ValueError raised if you try to write to the original stdout

>>> print("ähnlich üblich möglich", file=sys.__stdout__)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: underlying buffer has been detached

looks like a feature to me.

PS: An alternative would be to set the environment variable:

$ PYTHONIOENCODING=ascii:backslashreplace python3 -c 'print("Smørrebrød")'
Sm\xf8rrebr\xf8d

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


Thread

Re: print() function with encoding= and errors= parameters? Peter Otten <__peter__@web.de> - 2016-08-03 17:09 +0200

csiph-web