[Python-checkins] cpython (merge 3.2 -> default): merge 3.2 (#15846)

benjamin.peterson python-checkins at python.org
Sun Sep 2 20:25:27 CEST 2012


http://hg.python.org/cpython/rev/d61424122af5
changeset:   78836:d61424122af5
parent:      78833:3b2597c1fe35
parent:      78835:0db75a55145a
user:        Benjamin Peterson <benjamin at python.org>
date:        Sun Sep 02 14:24:44 2012 -0400
summary:
  merge 3.2 (#15846)

files:
  Lib/test/test_ast.py |  6 ++++++
  Misc/NEWS            |  3 +++
  Python/ast.c         |  8 ++++++++
  3 files changed, 17 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -407,6 +407,12 @@
         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),
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@
 
 - Issue #15839: Convert SystemErrors in super() to RuntimeErrors.
 
+- Issue #15846: Fix SystemError which happened when using ast.parse in an
+  exception handler on code with syntax errors.
+
 - Issue #15801: Make sure mappings passed to '%' formatting are actually
   subscriptable.
 
diff --git a/Python/ast.c b/Python/ast.c
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -588,7 +588,15 @@
     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;
 }

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list