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

Peter Otten __peter__ at web.de
Wed Aug 3 11:09:50 EDT 2016


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")
ähnlich üblich mö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





More information about the Python-list mailing list