diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-09-16 20:51:56 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-09-16 20:51:56 (GMT) |
commit | dafea851901fc1de278ad79727d3b44f46ba5a31 (patch) | |
tree | 5f8d95de4856502e61c78168e7918776b161e9b4 /Lib/idlelib/IOBinding.py | |
parent | 975fce37883899a55bbcdaa6300c5c6ffe9d3db2 (diff) | |
download | cpython-dafea851901fc1de278ad79727d3b44f46ba5a31.zip cpython-dafea851901fc1de278ad79727d3b44f46ba5a31.tar.gz cpython-dafea851901fc1de278ad79727d3b44f46ba5a31.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/IOBinding.py')
-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 4558ae6..cba8048 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: |