Skip to content

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

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

serhiy-storchaka wants to merge 5 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 only security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 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.
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Nov 6, 2026
…ensions-cache-key

# Conflicts:
#	Lib/test/test_import/__init__.py
#	Python/importdl.c
@serhiy-storchaka

Copy link
Copy Markdown
Member Author

@ericsnowcurrently, could you please review this? It changes the key of the extensions cache, which you added in gh-105699, so that extension modules can be imported from a path which is not encodable in UTF-8. I have just resolved conflicts with main.

@ericsnowcurrently ericsnowcurrently left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thanks for taking care of this.

@serhiy-storchaka
serhiy-storchaka enabled auto-merge (squash) November 26, 2026 00:27
@serhiy-storchaka

Copy link
Copy Markdown
Member Author

Thank you for your review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge interpreter-core (Objects, Python, Grammar, and Parser dirs) needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes stale Stale PR or inactive for long period of time. type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants