Skip to content

Debug abort compiling fstring concatenation with broken imports #151238

@stestagg

Description

@stestagg

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:

  1. The \] is an invalid escape sequence, so decode_unicode_with_escapes raises a warning.
  2. 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).
  3. 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
  4. _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
  5. IF parsing gets high enough up the stack (I didn't find the frame) then someone spots the exception and raises it as normal.
  6. 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

Metadata

Metadata

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-parsertype-crashA hard crash of the interpreter, possibly with a core dump
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions