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).
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>)
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".
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.