I noticed that without source code encoding declaration, the Python 2 interpreter assumes the source code is encoded in ASCII with scripts and standard input:
$ python test.py # where test.py holds the line: print u'é'
File "test.py", line 1
SyntaxError: Non-ASCII character '\xc3' in file test.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
$ echo "print u'é'" | python
File "/dev/fd/63", line 1
SyntaxError: Non-ASCII character '\xc3' in file /dev/fd/63 on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
and it is encoded in ISO-8859-1 with the -m
module and -c
command flags:
$ python -m test # where test.py holds the line: print u'é'
é
$ python -c "print u'é'"
é
Where is it documented?
Contrast this to Python 3 which always assumes the source code is encoded in UTF-8 and thus prints é
in the four cases.
Note. – I tested this on CPython 2.7.14 on both macOS 10.13 and Ubuntu Linux 17.10 with the console encoding set to UTF-8.