Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #74989
| From | "Frank Millman" <frank@chagford.com> |
|---|---|
| Subject | Re: Unicode, stdout, and stderr |
| Date | 2014-07-22 11:29 +0200 |
| References | <lqkvn0$ptp$1@ger.gmane.org> <lql3am$2q7$1@ger.gmane.org> <lql4jh$iup$1@ger.gmane.org> <lql9oi$hlt$1@ger.gmane.org> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.12177.1406021381.18130.python-list@python.org> (permalink) |
"Peter Otten" <__peter__@web.de> wrote in message
news:lql9oi$hlt$1@ger.gmane.org...
> Frank Millman wrote:
>
[...]
>
>> Out of interest, does the same thing happen when writing to sys.stderr?
>
> If you are asking about the fallback mechanism, that is specific to
> sys.displayhook in the interactive interpreter.
>
> But stdout and stderr do handle errors differently:
>
>>>> import sys
>>>> sys.stdout.errors
> 'strict'
>>>> sys.stderr.errors
> 'backslashreplace'
>
> So a codepoint written to stdout that cannot be encoded with
> stdout.encoding
> raises an error while a codepoint written to stderr that cannot be encoded
> with stderr.encoding is escaped.
>
> Another way to make stdout more forgiving:
>
>>>> import sys
>>>> print("\u2119")
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/usr/local/lib/python3.4/encodings/cp437.py", line 19, in encode
> return codecs.charmap_encode(input,self.errors,encoding_map)[0]
> UnicodeEncodeError: 'charmap' codec can't encode character '\u2119' in
> position 0: character maps to <undefined>
>>>> sys.stdout = open(1, mode="w", errors="xmlcharrefreplace",
> encoding=sys.stdout.encoding, closefd=False)
>>>> print("\u2119")
> ℙ
>
That's a lot of very useful information.
Thanks very much
Frank
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Unicode, stdout, and stderr "Frank Millman" <frank@chagford.com> - 2014-07-22 11:29 +0200
csiph-web