Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e431d3c

Browse files
Issue #26581: Use the first coding cookie on a line, not the last one.
1 parent 97eee1c commit e431d3c

8 files changed

Lines changed: 10 additions & 6 deletions

File tree

Lib/idlelib/IOBinding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
encoding = locale_encoding ### KBK 07Sep07 This is used all over IDLE, check!
6363
### 'encoding' is used below in encode(), check!
6464

65-
coding_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII)
65+
coding_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII)
6666
blank_re = re.compile(r'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII)
6767

6868
def coding_spec(data):

Lib/lib2to3/pgen2/tokenize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def compat(self, token, iterable):
236236
startline = False
237237
toks_append(tokval)
238238

239-
cookie_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII)
239+
cookie_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII)
240240
blank_re = re.compile(br'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII)
241241

242242
def _get_normal_name(orig_enc):

Lib/test/test_importlib/source/test_source_encoding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import warnings
1515

1616

17-
CODING_RE = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII)
17+
CODING_RE = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII)
1818

1919

2020
class EncodingTest:

Lib/test/test_source_encoding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def test_double_coding_line(self):
178178
def test_double_coding_same_line(self):
179179
src = (b'#coding:iso8859-15 coding:latin1\n'
180180
b'print(ascii("\xc3\xa4"))\n')
181-
self.check_script_output(src, br"'\xc3\xa4'")
181+
self.check_script_output(src, br"'\xc3\u20ac'")
182182

183183
def test_first_non_utf8_coding_line(self):
184184
src = (b'#coding:iso-8859-15 \xa4\n'

Lib/tokenize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import sys
3434
from token import *
3535

36-
cookie_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII)
36+
cookie_re = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII)
3737
blank_re = re.compile(br'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII)
3838

3939
import token

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: tba
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #26581: If coding cookie is specified multiple times on a line in
14+
Python source code file, only the first one is taken to account.
15+
1316
- Issue #26464: Fix str.translate() when string is ASCII and first replacements
1417
removes character, but next replacement uses a non-ASCII character or a
1518
string longer than 1 character. Regression introduced in Python 3.5.0.

Parser/tokenizer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ get_coding_spec(const char *s, char **spec, Py_ssize_t size, struct tok_state *t
275275
return 0;
276276
}
277277
*spec = r;
278+
break;
278279
}
279280
}
280281
}

Tools/scripts/findnocoding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def walk_python_files(self, paths, *args, **kwargs):
3232
"no sophisticated Python source file search will be done.", file=sys.stderr)
3333

3434

35-
decl_re = re.compile(rb'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)')
35+
decl_re = re.compile(rb'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)')
3636
blank_re = re.compile(rb'^[ \t\f]*(?:[#\r\n]|$)')
3737

3838
def get_declaration(line):

0 commit comments

Comments
 (0)