diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-09-16 20:57:00 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-09-16 20:57:00 (GMT) |
commit | 935349406aeb9d43fecea447f0309ce63ed3a406 (patch) | |
tree | 0c0cbc4c991840c4cb1e0cf835069fe571be7c03 /Lib/tokenize.py | |
parent | 3c41154331ed281514943a1d2c61fca0d89dc63c (diff) | |
parent | dafea851901fc1de278ad79727d3b44f46ba5a31 (diff) | |
download | cpython-935349406aeb9d43fecea447f0309ce63ed3a406.zip cpython-935349406aeb9d43fecea447f0309ce63ed3a406.tar.gz cpython-935349406aeb9d43fecea447f0309ce63ed3a406.tar.bz2 |
Issue #18873: The tokenize module, IDLE, 2to3, and the findnocoding.py script
now detect Python source code encoding only in comment lines.
Diffstat (limited to 'Lib/tokenize.py')
-rw-r--r-- | Lib/tokenize.py | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/Lib/tokenize.py b/Lib/tokenize.py index 2fbde0f..91f8e47 100644 --- a/Lib/tokenize.py +++ b/Lib/tokenize.py @@ -31,7 +31,7 @@ from token import * from codecs import lookup, BOM_UTF8 import collections from io import TextIOWrapper -cookie_re = re.compile("coding[:=]\s*([-\w.]+)") +cookie_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII) import token __all__ = token.__all__ + ["COMMENT", "tokenize", "detect_encoding", @@ -372,10 +372,10 @@ def detect_encoding(readline): msg = '{} for {!r}'.format(msg, filename) raise SyntaxError(msg) - matches = cookie_re.findall(line_string) - if not matches: + match = cookie_re.match(line_string) + if not match: return None - encoding = _get_normal_name(matches[0]) + encoding = _get_normal_name(match.group(1)) try: codec = lookup(encoding) except LookupError: |