Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #112294
| Path | csiph.com!news.fcku.it!peer02.fr7!news.highwinds-media.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Peter Otten <__peter__@web.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: print() function with encoding= and errors= parameters? |
| Date | Wed, 03 Aug 2016 17:09:50 +0200 |
| Organization | None |
| Lines | 50 |
| 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> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| X-Trace | news.uni-berlin.de s5qzcCK0BwiKOu7DCCYKQAt9d+9aiMEF5eLcE1pz5dYA== |
| 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.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python3': 0.05; 'sys,': 0.07; 'valueerror:': 0.07; 'library?': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:[ 30': 0.09; 'stdout': 0.09; 'stringio': 0.09; 'sys.stdout': 0.09; 'underlying': 0.09; 'valueerror': 0.09; 'bug': 0.10; 'python': 0.10; 'output': 0.13; 'wed,': 0.15; 'encoding': 0.15; 'classes)': 0.16; 'malcolm': 0.16; 'm\xc3\xb6glich': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:parameters': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'bytes': 0.18; 'duplicate': 0.18; '>>>': 0.20; 'aug': 0.20; '"",': 0.22; 'libraries': 0.22; 'sends': 0.22; '(you': 0.23; 'errors': 0.23; 'import': 0.24; '(most': 0.24; 'long,': 0.24; 'feature': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'raw': 0.27; 'function': 0.28; 'looks': 0.29; 'way?': 0.29; 'environment': 0.29; "i'm": 0.30; 'print': 0.30; 'option': 0.31; 'class': 0.33; 'raised': 0.33; 'traceback': 0.33; "skip:' 20": 0.34; 'file': 0.34; 'could': 0.35; 'configured': 0.35; "isn't": 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'there': 0.36; 'closing': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:org': 0.37; "won't": 0.38; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'your': 0.60; 'close': 0.61; 'concerns': 0.66; 'behavior?': 0.84; 'detached': 0.84; "it'd": 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| 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> |
| X-Mailman-Original-Message-ID | <nnt1fv$9en$1@blaine.gmane.org> |
| X-Mailman-Original-References | <1470227371.397715.684773553.140807A6@webmail.messagingengine.com> <1470233112.2667238.684861393.67022A98@webmail.messagingengine.com> |
| X-Received-Bytes | 5024 |
| X-Received-Body-CRC | 4220685810 |
| Xref | csiph.com comp.lang.python:112294 |
Show key headers only | 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")
ä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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: print() function with encoding= and errors= parameters? Peter Otten <__peter__@web.de> - 2016-08-03 17:09 +0200
csiph-web