Skip to content

Commit 0e27339

Browse files
author
Guido van Rossum
committed
When two coding cookies are present, use the first. Fixes #1281.
1 parent f119547 commit 0e27339

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

mypy/test/data/parse-errors.test

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,34 @@ file:2: error: Unknown encoding 'uft-8'
453453
[out]
454454
file:1: error: Unknown encoding 'Uft.8'
455455

456-
[case testInvalidEncoding2]
456+
[case testInvalidEncoding3]
457457
#!/usr/bin python
458458
# vim: set fileencoding=uft8 :
459459
[out]
460460
file:2: error: Unknown encoding 'uft8'
461461

462+
[case testDoubleEncoding]
463+
# coding: uft8
464+
# coding: utf8
465+
# The first coding cookie should be used and fail.
466+
[out]
467+
file:1: error: Unknown encoding 'uft8'
468+
469+
[case testDoubleEncoding2]
470+
# Again the first cookie should be used and fail.
471+
# coding: uft8
472+
# coding: utf8
473+
[out]
474+
file:2: error: Unknown encoding 'uft8'
475+
476+
[case testDoubleEncoding3]
477+
# If the third line were interpreted as a coding cookie, we'd get a
478+
# different error message.
479+
# coding: ascii
480+
á = 1
481+
[out]
482+
file:4: error: Unrecognized character
483+
462484
[case testLongLiteralInPython3]
463485
2L
464486
0x2L

mypy/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
T = TypeVar('T')
99

10-
ENCODING_RE = re.compile(br'(\s*#.*(\r\n?|\n))?\s*#.*coding[:=]\s*([-\w.]+)')
10+
ENCODING_RE = re.compile(br'(\s*#.*(\r\n?|\n)){0,1}?\s*#.*coding[:=]\s*([-\w.]+)')
1111

1212
default_python2_interpreter = ['python2', 'python', '/usr/bin/python']
1313

0 commit comments

Comments
 (0)