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

Skip to content

Commit c7f447c

Browse files
committed
merge 3.2 (#15846)
2 parents a3b84fb + 2e2c903 commit c7f447c

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
@@ -407,6 +407,12 @@ def test_parse(self):
407407
b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
408408
self.assertEqual(ast.dump(a), ast.dump(b))
409409

410+
def test_parse_in_error(self):
411+
try:
412+
1/0
413+
except Exception:
414+
self.assertRaises(SyntaxError, ast.parse, r"'\U'")
415+
410416
def test_dump(self):
411417
node = ast.parse('spam(eggs, "and cheese")')
412418
self.assertEqual(ast.dump(node),

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Core and Builtins
1212

1313
- Issue #15839: Convert SystemErrors in super() to RuntimeErrors.
1414

15+
- Issue #15846: Fix SystemError which happened when using ast.parse in an
16+
exception handler on code with syntax errors.
17+
1518
- Issue #15801: Make sure mappings passed to '%' formatting are actually
1619
subscriptable.
1720

Python/ast.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,15 @@ ast_error(const node *n, const char *errstr)
588588
PyObject *u = Py_BuildValue("zii", errstr, LINENO(n), n->n_col_offset);
589589
if (!u)
590590
return 0;
591+
/*
592+
* Prevent the error from being chained. PyErr_SetObject will normalize the
593+
* exception in order to chain it. ast_error_finish, however, requires the
594+
* error not to be normalized.
595+
*/
596+
PyObject *save = PyThreadState_GET()->exc_value;
597+
PyThreadState_GET()->exc_value = NULL;
591598
PyErr_SetObject(PyExc_SyntaxError, u);
599+
PyThreadState_GET()->exc_value = save;
592600
Py_DECREF(u);
593601
return 0;
594602
}

0 commit comments

Comments
 (0)