Skip to content

debug abort importing module with lone surrogate when -X importtime=2 is set #156126

Description

@stestagg

Crash report

What happened?

imp-name.py:

import sys, types

name = 'mod\ud800'
sys.modules[name] = types.ModuleType(name)

a = __import__(name)
print(a)

Normal:

$> ./python.exe imp-name.py
<module 'mod\ud800'>

With importtime=2:

$> ./python.exe -X importtime=2 imp-name.py
import time: self [us] | cumulative | imported package
...
import time:     16878 |      20452 | _colorize
UnicodeEncodeError: 'utf-8' codec can't encode character '\ud800' in position 3: surrogates not allowed

The above exception was the direct cause of the following exception:

SystemError: <built-in function __import__> returned a result with an exception set

Current thread 0x0000000201fb75c0 (most recent call first):
  File "~/src/tp/cpython/imp-name.py", line 6 in <module>
fish: Job 1, './python.exe -X importtime=2 im…' terminated by signal SIGABRT (Abort)
$ [SIGABRT]>

The code in import.c:

cpython/Python/import.c

Lines 325 to 328 in 228b1bf

#define import_level FIND_AND_LOAD(interp).import_level
fprintf(stderr, "import time: cached | cached | %*s\n",
import_level*2, PyUnicode_AsUTF8(name));
#undef import_level

Passes PyUnicode_AsUTF8(name) directly to fprintf without checking if the conversion/view was successful or not. This causes either a segfaul, or an abort depending on the libc.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Output from running 'python -VV' on the command line:

Python 3.16.0a0 (heads/main:e8158d1a02d, Aug 20 2026, 15:09:24) [Clang 21.0.0 (clang-2100.3.27.1)]

Linked PRs

Metadata

Metadata

Assignees

Labels

3.14bugs and security fixes3.15pre-release feature fixes, bugs and security fixes3.16new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions