[Python-checkins] cpython (3.3): Issue #18038: SyntaxError raised during compilation sources with illegal

serhiy.storchaka python-checkins at python.org
Sun Jun 9 15:59:01 CEST 2013


http://hg.python.org/cpython/rev/15aa786b723b
changeset:   84063:15aa786b723b
branch:      3.3
parent:      84061:aafa11c1dd61
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Jun 09 16:51:52 2013 +0300
summary:
  Issue #18038: SyntaxError raised during compilation sources with illegal
encoding now always contains an encoding name.

files:
  Lib/test/test_pep263.py |  18 ++++++++++++++++++
  Misc/NEWS               |   3 +++
  Parser/tokenizer.c      |  14 +++++++-------
  3 files changed, 28 insertions(+), 7 deletions(-)


diff --git a/Lib/test/test_pep263.py b/Lib/test/test_pep263.py
--- a/Lib/test/test_pep263.py
+++ b/Lib/test/test_pep263.py
@@ -55,6 +55,24 @@
         # 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)
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@
 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.
 
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -291,20 +291,20 @@
                     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;
 }
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list