Skip to content

gh-155247: Use raw string content for the extensions cache key - #155249

Open
serhiy-storchaka wants to merge 3 commits into
python:mainfrom
serhiy-storchaka:gh-155247-extensions-cache-key
Open

gh-155247: Use raw string content for the extensions cache key#155249
serhiy-storchaka wants to merge 3 commits into
python:mainfrom
serhiy-storchaka:gh-155247-extensions-cache-key

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

Importing an extension module failed with UnicodeEncodeError if its path contained characters unencodable in UTF-8, e.g. surrogate escapes of a file name undecodable in the filesystem encoding. As a result, Python could not be built or run in a directory with such name.

The key of the extensions cache was built with _PyUnicode_AsUTF8NoNUL(). The raw content of the strings (UCS1, UCS2 or UCS4) is now used instead.

The rejection of embedded null characters, which was a side effect of the UTF-8 encoding, is now explicit in the extension module loader.

The key of the extensions cache was built with _PyUnicode_AsUTF8NoNUL(),
so importing an extension module failed with UnicodeEncodeError if its
path contained characters unencodable in UTF-8, e.g. surrogate escapes of
an undecodable file name.  The raw content of the strings is now used.

The rejection of embedded null characters, which was a side effect of the
UTF-8 encoding, is now explicit in the extension module loader.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@serhiy-storchaka serhiy-storchaka added interpreter-core (Objects, Python, Grammar, and Parser dirs) needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes type-bug An unexpected behavior, bug, or error labels Aug 5, 2026
serhiy-storchaka and others added 2 commits August 5, 2026 19:19
A loaded extension module cannot be removed on Windows, so the temporary
directory is now removed with ignore_errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The key is hashed and compared as raw bytes, so the uninitialized padding
of the header made lookups miss at random.

Import the extension module in a subprocess: it stays loaded, and on
Windows its file cannot be removed, so the temporary directory leaked.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review interpreter-core (Objects, Python, Grammar, and Parser dirs) needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant