changeset: 100623:1c44cea2ea8f branch: 3.5 parent: 100620:0d7a07b7c89b user: Serhiy Storchaka date: Sun Mar 20 23:36:29 2016 +0200 files: Lib/idlelib/IOBinding.py Lib/lib2to3/pgen2/tokenize.py Lib/test/test_importlib/source/test_source_encoding.py Lib/test/test_source_encoding.py Lib/tokenize.py Misc/NEWS Parser/tokenizer.c Tools/scripts/findnocoding.py description: Issue #26581: Use the first coding cookie on a line, not the last one. diff -r 0d7a07b7c89b -r 1c44cea2ea8f Lib/idlelib/IOBinding.py --- a/Lib/idlelib/IOBinding.py Sun Mar 20 22:29:40 2016 +0200 +++ b/Lib/idlelib/IOBinding.py Sun Mar 20 23:36:29 2016 +0200 @@ -62,7 +62,7 @@ encoding = locale_encoding ### KBK 07Sep07 This is used all over IDLE, check! ### 'encoding' is used below in encode(), check! -coding_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII) +coding_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII) blank_re = re.compile(r'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII) def coding_spec(data): diff -r 0d7a07b7c89b -r 1c44cea2ea8f Lib/lib2to3/pgen2/tokenize.py --- a/Lib/lib2to3/pgen2/tokenize.py Sun Mar 20 22:29:40 2016 +0200 +++ b/Lib/lib2to3/pgen2/tokenize.py Sun Mar 20 23:36:29 2016 +0200 @@ -236,7 +236,7 @@ startline = False toks_append(tokval) -cookie_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII) +cookie_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII) blank_re = re.compile(br'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII) def _get_normal_name(orig_enc): diff -r 0d7a07b7c89b -r 1c44cea2ea8f Lib/test/test_importlib/source/test_source_encoding.py --- a/Lib/test/test_importlib/source/test_source_encoding.py Sun Mar 20 22:29:40 2016 +0200 +++ b/Lib/test/test_importlib/source/test_source_encoding.py Sun Mar 20 23:36:29 2016 +0200 @@ -14,7 +14,7 @@ import warnings -CODING_RE = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII) +CODING_RE = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII) class EncodingTest: diff -r 0d7a07b7c89b -r 1c44cea2ea8f Lib/test/test_source_encoding.py --- a/Lib/test/test_source_encoding.py Sun Mar 20 22:29:40 2016 +0200 +++ b/Lib/test/test_source_encoding.py Sun Mar 20 23:36:29 2016 +0200 @@ -178,7 +178,7 @@ def test_double_coding_same_line(self): src = (b'#coding:iso8859-15 coding:latin1\n' b'print(ascii("\xc3\xa4"))\n') - self.check_script_output(src, br"'\xc3\xa4'") + self.check_script_output(src, br"'\xc3\u20ac'") def test_first_non_utf8_coding_line(self): src = (b'#coding:iso-8859-15 \xa4\n' diff -r 0d7a07b7c89b -r 1c44cea2ea8f Lib/tokenize.py --- a/Lib/tokenize.py Sun Mar 20 22:29:40 2016 +0200 +++ b/Lib/tokenize.py Sun Mar 20 23:36:29 2016 +0200 @@ -33,7 +33,7 @@ import sys from token import * -cookie_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII) +cookie_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII) blank_re = re.compile(br'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII) import token diff -r 0d7a07b7c89b -r 1c44cea2ea8f Misc/NEWS --- a/Misc/NEWS Sun Mar 20 22:29:40 2016 +0200 +++ b/Misc/NEWS Sun Mar 20 23:36:29 2016 +0200 @@ -10,6 +10,9 @@ Core and Builtins ----------------- +- Issue #26581: If coding cookie is specified multiple times on a line in + Python source code file, only the first one is taken to account. + - Issue #26464: Fix str.translate() when string is ASCII and first replacements removes character, but next replacement uses a non-ASCII character or a string longer than 1 character. Regression introduced in Python 3.5.0. diff -r 0d7a07b7c89b -r 1c44cea2ea8f Parser/tokenizer.c --- a/Parser/tokenizer.c Sun Mar 20 22:29:40 2016 +0200 +++ b/Parser/tokenizer.c Sun Mar 20 23:36:29 2016 +0200 @@ -275,6 +275,7 @@ return 0; } *spec = r; + break; } } } diff -r 0d7a07b7c89b -r 1c44cea2ea8f Tools/scripts/findnocoding.py --- a/Tools/scripts/findnocoding.py Sun Mar 20 22:29:40 2016 +0200 +++ b/Tools/scripts/findnocoding.py Sun Mar 20 23:36:29 2016 +0200 @@ -32,7 +32,7 @@ "no sophisticated Python source file search will be done.", file=sys.stderr) -decl_re = re.compile(rb'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)') +decl_re = re.compile(rb'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)') blank_re = re.compile(rb'^[ \t\f]*(?:[#\r\n]|$)') def get_declaration(line):