python / python/cpython

Cannot import an extension module if its path is not encodable in UTF-8

Open
#155,247 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Importing an extension module fails with UnicodeEncodeError if its path contains characters unencodable in UTF-8, e.g. surrogate escapes of a file name undecodable in the filesystem encoding.

$ .../nd\udcffwt/python -c "import _json"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
UnicodeEncodeError: 'utf-8' codec can't encode character '\udcff' in position 45: surrogates not allowed

As a result, CPython cannot be run in a directory whose name contains undecodable bytes, and cannot even be built there -- make fails in checksharedmods. On 3.13 and 3.14 every import of a shared extension module from such a path fails.

The cause is the key of the extensions cache: hashtable_key_from_2_strings() in Python/import.c encodes the module file name with _PyUnicode_AsUTF8NoNUL(). It was added in gh-105699.

The raw content of the strings (UCS1, UCS2 or UCS4) can be used for the key instead of the UTF-8 encoding.

Linked PRs
  • gh-155249

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Python/import.c at hashtable_key_from_2_strings(), where the issue identifies UTF-8 encoding of the extension module filename as the cause. Review linked PR gh-155249 and the checksharedmods failure described in the report. Done should include successful extension-module imports and builds from paths containing undecodable filename bytes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.