]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Change SystemError into SyntaxError, when a Unicode string
authorMartin v. Löwis <martin@v.loewis.de>
Wed, 22 Mar 2006 13:55:50 +0000 (13:55 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Wed, 22 Mar 2006 13:55:50 +0000 (13:55 +0000)
containing an encoding declaration is compile()d. Fixes
#1115379.

Lib/test/test_compile.py
Misc/NEWS
Python/compile.c

index c567fa432abb6efafc3c6463a94c003819296063..010b38abd17df041b7f0e6e1267b0c9a865c4211 100644 (file)
@@ -261,6 +261,10 @@ if 1:
         f1, f2 = f()
         self.assertNotEqual(id(f1.func_code), id(f2.func_code))
 
+    def test_unicode_encoding(self):
+        # SF bug 1115379
+        self.assertRaises(SyntaxError, compile, u"# -*- coding: utf-8 -*-\npass\n", "tmp", "exec")
+
 def test_main():
     test_support.run_unittest(TestSpecifics)
 
index 6abb881dca91cd159a961e387f99ea95ae80b8cb..b94b66c226d2b5626f7acb0e266d5a3966a1d779 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.4.3c1?
 Core and builtins
 -----------------
 
+- Bug #1115379: Compiling a Unicode string with an encoding declaration
+  now gives a SyntaxError.
+
 - Fix missing check on whether the PendingDeprecationWarning for string
   exceptions was re-raised as an actual PendingDeprecationWarning when
   'warnings' is set to a filter action of "error"
index a5bd09eb4d9c66d5a2f83713f34cf55d19b8c26f..4a7ec8c23e817e1ee5c04c8e19acdd0f8f3284e2 100644 (file)
@@ -4952,6 +4952,12 @@ jcompile(node *n, const char *filename, struct compiling *base,
                return NULL;
        if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
                sc.c_encoding = "utf-8";
+               if (TYPE(n) == encoding_decl) {
+                       com_error(&sc, PyExc_SyntaxError, 
+                                 "encoding declaration in Unicode string");
+                       co = NULL;
+                       goto exit;
+               }
        } else if (TYPE(n) == encoding_decl) {
                sc.c_encoding = STR(n);
                n = CHILD(n, 0);
@@ -5044,7 +5050,7 @@ jcompile(node *n, const char *filename, struct compiling *base,
                PyErr_SetString(PyExc_SystemError, "lost syntax error");
        }
  exit:
-       if (base == NULL) {
+       if (base == NULL && sc.c_symtable != NULL) {
                PySymtable_Free(sc.c_symtable);
                sc.c_symtable = NULL;
        }