summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2006-03-22 13:55:50 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2006-03-22 13:55:50 (GMT)
commit73c8c2c3dd0ff10a31ee4c07273d7f1f3343561a (patch)
treef478549d94e803fa2a824f68971b25bebca5a205
parent88fd50038b2ddbcbb162a87387a78e6f208bc849 (diff)
downloadcpython-73c8c2c3dd0ff10a31ee4c07273d7f1f3343561a.zip
cpython-73c8c2c3dd0ff10a31ee4c07273d7f1f3343561a.tar.gz
cpython-73c8c2c3dd0ff10a31ee4c07273d7f1f3343561a.tar.bz2
Change SystemError into SyntaxError, when a Unicode string
containing an encoding declaration is compile()d. Fixes #1115379.
-rw-r--r--Lib/test/test_compile.py4
-rw-r--r--Misc/NEWS3
-rw-r--r--Python/compile.c8
3 files changed, 14 insertions, 1 deletions
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;
}