Skip to content

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

Open

Description

@serhiy-storchaka

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

Activity

  1. added
    type-bug
    interpreter-core
    on Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-coretype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions