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

Skip to content

Commit 3af14aa

Browse files
Issue #18038: SyntaxError raised during compilation sources with illegal
encoding now always contains an encoding name.
1 parent c1f5839 commit 3af14aa

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

Lib/test/test_pep263.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ def test_issue7820(self):
5555
# two bytes in common with the UTF-8 BOM
5656
self.assertRaises(SyntaxError, eval, b'\xef\xbb\x20')
5757

58+
def test_error_message(self):
59+
compile(b'# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec')
60+
compile(b'\xef\xbb\xbf\n', 'dummy', 'exec')
61+
compile(b'\xef\xbb\xbf# -*- coding: utf-8 -*-\n', 'dummy', 'exec')
62+
with self.assertRaisesRegexp(SyntaxError, 'fake'):
63+
compile(b'# -*- coding: fake -*-\n', 'dummy', 'exec')
64+
with self.assertRaisesRegexp(SyntaxError, 'iso-8859-15'):
65+
compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
66+
'dummy', 'exec')
67+
with self.assertRaisesRegexp(SyntaxError, 'BOM'):
68+
compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
69+
'dummy', 'exec')
70+
with self.assertRaisesRegexp(SyntaxError, 'fake'):
71+
compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
72+
with self.assertRaisesRegexp(SyntaxError, 'BOM'):
73+
compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
74+
75+
5876
def test_main():
5977
support.run_unittest(PEP263Test)
6078

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.3.3 release candidate 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #18038: SyntaxError raised during compilation sources with illegal
16+
encoding now always contains an encoding name.
17+
1518
- Issue #17644: Fix a crash in str.format when curly braces are used in square
1619
brackets.
1720

Parser/tokenizer.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,20 @@ check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
291291
tok->encoding = cs;
292292
tok->decoding_state = STATE_NORMAL;
293293
}
294-
else
294+
else {
295+
PyErr_Format(PyExc_SyntaxError,
296+
"encoding problem: %s", cs);
295297
PyMem_FREE(cs);
298+
}
296299
}
297300
} else { /* then, compare cs with BOM */
298301
r = (strcmp(tok->encoding, cs) == 0);
302+
if (!r)
303+
PyErr_Format(PyExc_SyntaxError,
304+
"encoding problem: %s with BOM", cs);
299305
PyMem_FREE(cs);
300306
}
301307
}
302-
if (!r) {
303-
cs = tok->encoding;
304-
if (!cs)
305-
cs = "with BOM";
306-
PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
307-
}
308308
return r;
309309
}
310310

0 commit comments

Comments
 (0)