You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
detect_encoding should fail with SyntaxError on invalid encoding #59195
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2012-07-07.10:29:50.995>created_at=<Date2012-06-03.10:29:02.107>labels= ['type-bug', 'library', 'expert-unicode']
title='detect_encoding should fail with SyntaxError on invalid encoding'updated_at=<Date2018-04-23.01:09:05.686>user='https://github.com/florentx'
I've hit this issue while playing with tokenize for the PEP-8.py module.
The tokenize detect_encoding() should report SyntaxError when the encoding is improperly declared.
However it raises a LookupError in some cases.
$ ./python -m tokenize Lib/test/bad_coding2.py
unexpected error: unknown encoding: utf8-sig
Traceback (most recent call last):
File "./Lib/runpy.py", line 162, in _run_module_as_main"__main__", fname, loader, pkg_name)
File "./Lib/runpy.py", line 75, in _run_codeexec(code, run_globals)
File "./Lib/tokenize.py", line 686, in <module>
main()
File "./Lib/tokenize.py", line 656, in main
tokens =list(tokenize(f.readline))
File "./Lib/tokenize.py", line 489, in _tokenize
line = line.decode(encoding)
LookupError: unknown encoding: utf8-sig
If a source file uses both the UTF-8 BOM mark signature and a
magic encoding comment, the only allowed encoding for the comment
is 'utf-8'. Any other encoding will cause an error.
Activity
florentx commentedon Jun 3, 2012
I've hit this issue while playing with tokenize for the PEP-8.py module.
The tokenize detect_encoding() should report SyntaxError when the encoding is improperly declared.
However it raises a LookupError in some cases.
florentx commentedon Jun 3, 2012
This patch seems to fix the issue.
vstinner commentedon Jun 4, 2012
The patch is correct according to the PEP-263:
The fix should also be applied to 3.2.
(Note: Python 3.1 doesn't accept bugfixes anymore.)
florentx commentedon Jun 6, 2012
It should raise a SyntaxError, if coding is 'utf8'.
I don't agree with the last patch proposed.
If the import report a SyntaxError, 'tokenize' should do the same.
$ ./python Lib/test/bad_coding2.py File "Lib/test/bad_coding2.py", line 1 SyntaxError: encoding problem: utf-8and it complies strictly with PEP-263.
vstinner commentedon Jun 6, 2012
Oops, I didn't want to attach my patch to the issue. Mine is wrong, whereas yours is the right fix :-)
python-dev commentedon Jul 7, 2012
New changeset 5020afc0b7c9 by Florent Xicluna in branch '3.2':
Issue bpo-14990: tokenize: correctly fail with SyntaxError on invalid encoding declaration.
http://hg.python.org/cpython/rev/5020afc0b7c9
florentx commentedon Jul 7, 2012
Thanks. Fixed in trunk too, changeset b4322ad1fec4