From 73c8c2c3dd0ff10a31ee4c07273d7f1f3343561a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Wed, 22 Mar 2006 13:55:50 +0000 Subject: Change SystemError into SyntaxError, when a Unicode string containing an encoding declaration is compile()d. Fixes #1115379. --- Lib/test/test_compile.py | 4 ++++ Misc/NEWS | 3 +++ Python/compile.c | 8 +++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index c567fa4..010b38a 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -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) diff --git a/Misc/NEWS b/Misc/NEWS index 6abb881..b94b66c 100644 --- 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" diff --git a/Python/compile.c b/Python/compile.c index a5bd09e..4a7ec8c 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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; } -- cgit v0.12