Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e2c903

Browse files
committed
prevert ast errors from being normalized before ast_error_finish is called (closes #15846)
1 parent 1c371b2 commit 2e2c903

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

Lib/test/test_ast.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ def test_parse(self):
386386
b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
387387
self.assertEqual(ast.dump(a), ast.dump(b))
388388

389+
def test_parse_in_error(self):
390+
try:
391+
1/0
392+
except Exception:
393+
self.assertRaises(SyntaxError, ast.parse, r"'\U'")
394+
389395
def test_dump(self):
390396
node = ast.parse('spam(eggs, "and cheese")')
391397
self.assertEqual(ast.dump(node),

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #15846: Fix SystemError which happened when using ast.parse in an
14+
exception handler on code with syntax errors.
15+
1316
- Issue #15761: Fix crash when PYTHONEXECUTABLE is set on Mac OS X.
1417

1518
- Issue #15801: Make sure mappings passed to '%' formatting are actually

Python/ast.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,15 @@ ast_error(const node *n, const char *errstr)
9292
PyObject *u = Py_BuildValue("zii", errstr, LINENO(n), n->n_col_offset);
9393
if (!u)
9494
return 0;
95+
/*
96+
* Prevent the error from being chained. PyErr_SetObject will normalize the
97+
* exception in order to chain it. ast_error_finish, however, requires the
98+
* error not to be normalized.
99+
*/
100+
PyObject *save = PyThreadState_GET()->exc_value;
101+
PyThreadState_GET()->exc_value = NULL;
95102
PyErr_SetObject(PyExc_SyntaxError, u);
103+
PyThreadState_GET()->exc_value = save;
96104
Py_DECREF(u);
97105
return 0;
98106
}

0 commit comments

Comments
 (0)