Crash report
What happened?
import builtins
builtins.__import__ = builtins # anything that will raise if you try to call it
compile('"x"f"\]"b""', '<doesnt exist>', 'exec')
/pfm/scratch/t1-2-a.py:3: SyntaxWarning: "\]" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\]"? A raw string is also an option.
compile('"x"f"\]"b""', '<doesnt exist>', 'exec')
python3: Objects/call.c:342: PyObject *_PyObject_Call(PyThreadState *, PyObject *, PyObject *, PyObject *): Assertion `!_PyErr_Occurred(tstate)' failed.
Aborted
What's going on here:
- The
\] is an invalid escape sequence, so decode_unicode_with_escapes raises a warning.
- During the formatting/rendering of the warning, the code raises a TypeError trying to call
__import__ (here really the builtins module, but could be anything that raises an unexpected exception type).
- This error gets propagated up the stack reasonably until
_PyPegen_joined_str which unconditionally passes the result of _get_resized_exprs (here NULL) to _PyAST_JoinedStr
_PyAST_JoinedStr just constructs an ast node with a value union value of NULL and returns it up the stack as a valid node, but PyErr_Occurred() still has the Exception hanging around
- IF parsing gets high enough up the stack (I didn't find the frame) then someone spots the exception and raises it as normal.
- In this case, the parser tries to concatenate the fstring with a bytes string, which is invalid so
_PyPegen_concatenate_strings raises an exception, which tries to read the source file, triggering an error: PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); which trips the !_PyErr_Occurred(tstate) debug assert.
There are several similar ways to trigger the abort once the rogue PyErr_Occurred is floating around:
import builtins
builtins.__import__ = builtins # anything that will raise if you try to call it
compile('"x"f"\]"f"\]"', '<doesnt exist>', 'exec')
/pfm/scratch/t1-2-a.py:3: SyntaxWarning: "\]" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\]"? A raw string is also an option.
compile('"x"f"\]"f"\]"', '<doesnt exist>', 'exec')
Fatal Python error: _Py_CheckFunctionResult: a function returned a result with an exception set
Python runtime state: initialized
Exception ignored in the internal traceback machinery:
Traceback (most recent call last):
File "/pfm/scratch/t1-2-a.py", line 3, in <module>
TypeError: 'module' object is not callable
TypeError: 'module' object is not callable
The above exception was the direct cause of the following exception:
SystemError: <class 'SyntaxWarning'> returned a result with an exception set
Current thread 0x0000fffff7fef020 [python3] (most recent call first):
File "/pfm/scratch/t1-2-a.py", line 3 in <module>
Aborted
and
import builtins
builtins.__import__ = builtins # anything that will raise if you try to call it
compile('"x"f"\]"b"\xdd"', '<doesnt exist>', 'exec')
which triggers the !_PyErr_Occurred assert, but via _PyPegen_parse_string instead.
In a non-debug build, without the assert, then the parser spots the exception and raises it, BUT there's a temporary invalid AST (joined string node with null members list) node floating around, and If someone could find a way to trigger a PyErr_Clear() call in the parser, that could possible escape the parser and cause some issues.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.16.0a0 (heads/main-dirty:ff64d8d, Jun 10 2026, 07:51:16) [Clang 22.1.6 ]
Linked PRs
Crash report
What happened?
What's going on here:
\]is an invalid escape sequence, so decode_unicode_with_escapes raises a warning.__import__(here really thebuiltinsmodule, but could be anything that raises an unexpected exception type)._PyPegen_joined_strwhich unconditionally passes the result of_get_resized_exprs(here NULL) to_PyAST_JoinedStr_PyAST_JoinedStrjust constructs an ast node with a value union value of NULL and returns it up the stack as a valid node, but PyErr_Occurred() still has the Exception hanging around_PyPegen_concatenate_stringsraises an exception, which tries to read the source file, triggering an error:PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);which trips the!_PyErr_Occurred(tstate)debug assert.There are several similar ways to trigger the abort once the rogue PyErr_Occurred is floating around:
and
which triggers the !_PyErr_Occurred assert, but via _PyPegen_parse_string instead.
In a non-debug build, without the assert, then the parser spots the exception and raises it, BUT there's a temporary invalid AST (joined string node with null members list) node floating around, and If someone could find a way to trigger a PyErr_Clear() call in the parser, that could possible escape the parser and cause some issues.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.16.0a0 (heads/main-dirty:ff64d8d, Jun 10 2026, 07:51:16) [Clang 22.1.6 ]
Linked PRs
_get_resized_exprsfailure in_PyPegen_{joined,template}_str#151259