]> granicus.if.org Git - python/commitdiff
Issue #18038: SyntaxError raised during compilation sources with illegal
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 9 Jun 2013 13:51:52 +0000 (16:51 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 9 Jun 2013 13:51:52 +0000 (16:51 +0300)
encoding now always contains an encoding name.

Lib/test/test_pep263.py
Misc/NEWS
Parser/tokenizer.c

index 598d980b2a67e289dfefb23de2265ee229c13b0e..1290bc70de66c9b6342fdfb8e7e989b6c3ccac21 100644 (file)
@@ -55,6 +55,24 @@ class PEP263Test(unittest.TestCase):
         # two bytes in common with the UTF-8 BOM
         self.assertRaises(SyntaxError, eval, b'\xef\xbb\x20')
 
+    def test_error_message(self):
+        compile(b'# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec')
+        compile(b'\xef\xbb\xbf\n', 'dummy', 'exec')
+        compile(b'\xef\xbb\xbf# -*- coding: utf-8 -*-\n', 'dummy', 'exec')
+        with self.assertRaisesRegexp(SyntaxError, 'fake'):
+            compile(b'# -*- coding: fake -*-\n', 'dummy', 'exec')
+        with self.assertRaisesRegexp(SyntaxError, 'iso-8859-15'):
+            compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
+                    'dummy', 'exec')
+        with self.assertRaisesRegexp(SyntaxError, 'BOM'):
+            compile(b'\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
+                    'dummy', 'exec')
+        with self.assertRaisesRegexp(SyntaxError, 'fake'):
+            compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
+        with self.assertRaisesRegexp(SyntaxError, 'BOM'):
+            compile(b'\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
+
+
 def test_main():
     support.run_unittest(PEP263Test)
 
index 5707f041c2e1152cbdbab0592597c8398cc77f8c..dae9ff74106ddcc3c36f1ebdcdc083c6b809866b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.3.3 release candidate 1?
 Core and Builtins
 -----------------
 
+- Issue #18038: SyntaxError raised during compilation sources with illegal
+  encoding now always contains an encoding name.
+
 - Issue #17644: Fix a crash in str.format when curly braces are used in square
   brackets.
 
index 93a4a5ccb479010c8eaeaba22ebab3374c8db4e7..7fe384b5e8dff88daaabee21708c35baae8dce1c 100644 (file)
@@ -291,20 +291,20 @@ check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
                     tok->encoding = cs;
                     tok->decoding_state = STATE_NORMAL;
                 }
-                else
+                else {
+                    PyErr_Format(PyExc_SyntaxError,
+                                 "encoding problem: %s", cs);
                     PyMem_FREE(cs);
+                }
             }
         } 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;
 }