]> granicus.if.org Git - python/commitdiff
prevert ast errors from being normalized before ast_error_finish is called (closes...
authorBenjamin Peterson <benjamin@python.org>
Sun, 2 Sep 2012 18:23:15 +0000 (14:23 -0400)
committerBenjamin Peterson <benjamin@python.org>
Sun, 2 Sep 2012 18:23:15 +0000 (14:23 -0400)
Lib/test/test_ast.py
Misc/NEWS
Python/ast.c

index e22d4de88ba763dc396ef3b693868625a83c14c0..2887092a7d7a4fb5c80aa231f4f762aa641ea3ca 100644 (file)
@@ -386,6 +386,12 @@ class ASTHelpers_Test(unittest.TestCase):
         b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
         self.assertEqual(ast.dump(a), ast.dump(b))
 
+    def test_parse_in_error(self):
+        try:
+            1/0
+        except Exception:
+            self.assertRaises(SyntaxError, ast.parse, r"'\U'")
+
     def test_dump(self):
         node = ast.parse('spam(eggs, "and cheese")')
         self.assertEqual(ast.dump(node),
index 4bb9698ac1d1964dbb9717ccf412a17694b5f549..544bf67afb26521aa8ea4ca9fcf74764d163e036 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
 Core and Builtins
 -----------------
 
+- Issue #15846: Fix SystemError which happened when using ast.parse in an
+  exception handler on code with syntax errors.
+
 - Issue #15761: Fix crash when PYTHONEXECUTABLE is set on Mac OS X.
 
 - Issue #15801: Make sure mappings passed to '%' formatting are actually
index 6faf5b21a6ebba9c964ff631ca15da761f07533e..3d0e3844b729ec7c8d190e8f9a330c870cccb392 100644 (file)
@@ -92,7 +92,15 @@ ast_error(const node *n, const char *errstr)
     PyObject *u = Py_BuildValue("zii", errstr, LINENO(n), n->n_col_offset);
     if (!u)
         return 0;
+    /*
+     * Prevent the error from being chained. PyErr_SetObject will normalize the
+     * exception in order to chain it. ast_error_finish, however, requires the
+     * error not to be normalized.
+     */
+    PyObject *save = PyThreadState_GET()->exc_value;
+    PyThreadState_GET()->exc_value = NULL;
     PyErr_SetObject(PyExc_SyntaxError, u);
+    PyThreadState_GET()->exc_value = save;
     Py_DECREF(u);
     return 0;
 }