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.

Title: PYTHONIOENCODING=undefined doesn't work in Python 3
Type: behavior Stage:
Components: Interpreter Core, IO, Unicode Versions: Python 3.7, Python 3.6
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, ezio.melotti, lemburg, martin.panter, ncoghlan, serhiy.storchaka, vstinner
Priority: normal Keywords:

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)'
$ PYTHONIOENCODING=undefined python -c 'print("abc")'
$ PYTHONIOENCODING=undefined python -c 'print(u"abc")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/encodings/", 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.
Date User Action Args
2022-04-11 14:58:56adminsetgithub: 76637
2018-01-03 11:01:14serhiy.storchakasetmessages: + msg309408
2018-01-03 10:25:11vstinnersetmessages: + msg309406
2018-01-03 10:00:45serhiy.storchakasetmessages: + msg309404
2018-01-02 19:30:14martin.pantersetnosy: + martin.panter
messages: + msg309390
2017-12-30 16:26:14serhiy.storchakacreate