Martin von Löwis avatar Martin von Löwis committed 4d80607

Change SystemError into SyntaxError, when a Unicode string
containing an encoding declaration is compile()d. Fixes

Comments (0)

Files changed (3)


         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():
 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"
 		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);
 		PyErr_SetString(PyExc_SystemError, "lost syntax error");
-	if (base == NULL) {
+	if (base == NULL && sc.c_symtable != NULL) {
 		sc.c_symtable = NULL;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.