Issue 32456: PYTHONIOENCODING=undefined doesn't work in Python 3 (original) (raw)

Created on 2017-12-30 16:26 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin.

Messages (5)
msg309237 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-12-30 16:26
In Python 2.7 setting PYTHONIOENCODING=undefined mostly works as expected. $ PYTHONIOENCODING=undefined python -c 'print(123)' 123 $ PYTHONIOENCODING=undefined python -c 'print("abc")' abc $ PYTHONIOENCODING=undefined python -c 'print(u"abc")' Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/encodings/undefined.py", line 19, in encode raise UnicodeError("undefined encoding") UnicodeError: undefined encoding Most tests (except test_doctest and test_gdb) are passed with PYTHONIOENCODING=undefined. But in Python 3 setting PYTHONIOENCODING=undefined seems just silently terminates the interpreter (exited with code 1).
msg309390 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2018-01-02 19:30
My guess is there is no message because in Python 3, errors are encoded according to PYTHONIOENCODING. Perhaps it works as you expect if you bypass sys.excepthook: $ PYTHONIOENCODING=undefined python -c 'import sys, os; sys.excepthook = lambda *exc: os.write(2, ascii(exc).encode()); print(u"abc")' (<class 'UnicodeError'>, UnicodeError('undefined encoding',), <traceback object at 0xb7037b94>)
msg309404 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-01-03 10:00
Thank you for solving this puzzle Martin. The question is whether we need to do something for improving error reporting in this case or just close this issue with a resolution "not a bug" or "won't fix".
msg309406 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-01-03 10:25
Can't we check if the encoding exists using codecs.lookup() before using it to emit a better error message? Look at initfsencoding() in the C code which implements such check.
msg309408 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-01-03 11:01
The "undefined" encoding exists.
History
Date User Action Args
2022-04-11 14:58:56 admin set github: 76637
2018-01-03 11:01:14 serhiy.storchaka set messages: +
2018-01-03 10:25:11 vstinner set messages: +
2018-01-03 10:00:45 serhiy.storchaka set messages: +
2018-01-02 19:30:14 martin.panter set nosy: + martin.pantermessages: +
2017-12-30 16:26:14 serhiy.storchaka create