File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ),
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments