This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients docs@python, eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Date 2017-05-21.00:53:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1495328004.58.0.171347369672.issue30410@psf.upfronthosting.co.za>
In-reply-to
Content
I discussed character devices mostly because of the NUL device. It could be surprising that Python dies on an encoding error when output is redirected to NUL:

    C:\>chcp 1252
    Active code page: 1252

    C:\>python -c "print('\u20ac')" > nul
    C:\>chcp 437
    Active code page: 437

    C:\>python -c "print('\u20ac')" > nul
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Program Files\Python36\lib\encodings\cp437.py", line 19, in encode
        return codecs.charmap_encode(input,self.errors,encoding_map)[0]
    UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in position 0:
    character maps to <undefined>

Unix has a similar problem:

    $ LANG=C python3 -c 'print("\u20ac")' > /dev/null
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    UnicodeEncodeError: 'ascii' codec can't encode character '\u20ac' in position 0:
    ordinal not in range(128)

Except /dev/null isn't a TTY. Also, it's rare nowadays for the locale encoding in Unix systems to be something other than UTF-8.

It would be useful if we special-cased NUL like we do for the Windows console, but just to make it use the backslashreplace error handler. Unfortunately I don't know how to do that without calling NtQueryObject, for which ObjectNameInformation (1) can't be used because it's undocumented [1]. GetFinalPathNameByHandle also can't be used because it requires file-system devices. As a crude workaround, we could lump together all non-console character devices (i.e. isatty() but not a console). That will affect serial devices, too, but I can't think of a good reason someone would redirect stdout or stderr to a COM port.

[1]: https://msdn.microsoft.com/en-us/library/ff550964
History
Date User Action Args
2017-05-21 00:53:24eryksunsetrecipients: + eryksun, paul.moore, tim.golden, docs@python, zach.ware, steve.dower
2017-05-21 00:53:24eryksunsetmessageid: <1495328004.58.0.171347369672.issue30410@psf.upfronthosting.co.za>
2017-05-21 00:53:24eryksunlinkissue30410 messages
2017-05-21 00:53:22eryksuncreate