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/idlelib | |
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/idlelib')
-rw-r--r-- | Lib/idlelib/IOBinding.py | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/Lib/idlelib/IOBinding.py b/Lib/idlelib/IOBinding.py index 203b009..7589ab8 100644 --- a/Lib/idlelib/IOBinding.py +++ b/Lib/idlelib/IOBinding.py @@ -63,7 +63,7 @@ locale_encoding = locale_encoding.lower() encoding = locale_encoding ### KBK 07Sep07 This is used all over IDLE, check! ### 'encoding' is used below in encode(), check! -coding_re = re.compile("coding[:=]\s*([-\w_.]+)") +coding_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII) def coding_spec(data): """Return the encoding declaration according to PEP 263. @@ -84,14 +84,16 @@ def coding_spec(data): lines = data # consider only the first two lines if '\n' in lines: - lst = lines.split('\n')[:2] + lst = lines.split('\n', 2)[:2] elif '\r' in lines: - lst = lines.split('\r')[:2] + lst = lines.split('\r', 2)[:2] + else: + lst = [lines] + for line in lst: + match = coding_re.match(line) + if match is not None: + break else: - lst = list(lines) - str = '\n'.join(lst) - match = coding_re.search(str) - if not match: return None name = match.group(1) try: |