Commit 729ad5cf authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #18038: SyntaxError raised during compilation sources with illegal

encoding now always contains an encoding name.
parent c49805e9
......@@ -41,6 +41,24 @@ class PEP263Test(unittest.TestCase):
# two bytes in common with the UTF-8 BOM
self.assertRaises(SyntaxError, eval, '\xef\xbb\x20')
def test_error_message(self):
compile('# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec')
compile('\xef\xbb\xbf\n', 'dummy', 'exec')
compile('\xef\xbb\xbf# -*- coding: utf-8 -*-\n', 'dummy', 'exec')
with self.assertRaisesRegexp(SyntaxError, 'fake'):
compile('# -*- coding: fake -*-\n', 'dummy', 'exec')
with self.assertRaisesRegexp(SyntaxError, 'iso-8859-15'):
compile('\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
'dummy', 'exec')
with self.assertRaisesRegexp(SyntaxError, 'BOM'):
compile('\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
'dummy', 'exec')
with self.assertRaisesRegexp(SyntaxError, 'fake'):
compile('\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
with self.assertRaisesRegexp(SyntaxError, 'BOM'):
compile('\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
def test_main():
test_support.run_unittest(PEP263Test)
......
......@@ -9,6 +9,9 @@ What's New in Python 2.7.6?
Core and Builtins
-----------------
- Issue #18038: SyntaxError raised during compilation sources with illegal
encoding now always contains an encoding name.
- Issue #18019: Fix crash in the repr of dictionaries containing their own
views.
......
......@@ -277,8 +277,11 @@ check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
tok->encoding = cs;
tok->decoding_state = -1;
}
else
else {
PyErr_Format(PyExc_SyntaxError,
"encoding problem: %s", cs);
PyMem_FREE(cs);
}
#else
/* Without Unicode support, we cannot
process the coding spec. Since there
......@@ -289,15 +292,12 @@ check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
}
} else { /* then, compare cs with BOM */
r = (strcmp(tok->encoding, cs) == 0);
if (!r)
PyErr_Format(PyExc_SyntaxError,
"encoding problem: %s with BOM", cs);
PyMem_FREE(cs);
}
}
if (!r) {
cs = tok->encoding;
if (!cs)
cs = "with BOM";
PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
}
return r;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment