diff -r 85c04fdaa404 Lib/test/test_pep263.py --- a/Lib/test/test_pep263.py Wed May 22 15:28:30 2013 +0300 +++ b/Lib/test/test_pep263.py Fri May 24 12:54:04 2013 +0300 @@ -41,6 +41,27 @@ # 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') + self.assertRaisesRegexp(SyntaxError, 'fake', compile, + '# -*- coding: fake -*-\n', + 'dummy', 'exec') + self.assertRaisesRegexp(SyntaxError, 'iso-8859-15', compile, + '\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', + 'dummy', 'exec') + self.assertRaisesRegexp(SyntaxError, 'BOM', compile, + '\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n', + 'dummy', 'exec') + self.assertRaisesRegexp(SyntaxError, 'fake', compile, + '\xef\xbb\xbf# -*- coding: fake -*-\n', + 'dummy', 'exec') + self.assertRaisesRegexp(SyntaxError, 'BOM', compile, + '\xef\xbb\xbf# -*- coding: fake -*-\n', + 'dummy', 'exec') + + def test_main(): test_support.run_unittest(PEP263Test) diff -r 85c04fdaa404 Parser/tokenizer.c --- a/Parser/tokenizer.c Wed May 22 15:28:30 2013 +0300 +++ b/Parser/tokenizer.c Fri May 24 12:54:04 2013 +0300 @@ -277,8 +277,11 @@ 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 @@ } } 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; }