Skip to content

Commit

Permalink
Some changes
Browse files Browse the repository at this point in the history
1. Add a NEWS entry

2. __import__ and find_module will now normalize their
   arguments as needed

3. Update the importlib documentation to note that the
   name passed to finders must be normalized.
  • Loading branch information
ronaldoussoren committed Dec 29, 2023
1 parent 7d2d2bf commit 18d99ae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Doc/library/importlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ ABC hierarchy::
:func:`importlib.util.spec_from_loader` may be useful for implementing
concrete ``MetaPathFinders``.

*Fullname* must be normalized in NFKC to match the normalization
done by the Python parser.

.. versionadded:: 3.4

.. method:: invalidate_caches()
Expand Down Expand Up @@ -290,6 +293,9 @@ ABC hierarchy::
guess about what spec to return. :func:`importlib.util.spec_from_loader`
may be useful for implementing concrete ``PathEntryFinders``.

*Fullname* must be normalized in NFKC to match the normalization
done by the Python parser.

.. versionadded:: 3.4

.. method:: invalidate_caches()
Expand Down
17 changes: 17 additions & 0 deletions Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ def _object_name(obj):
except AttributeError:
return type(obj).__qualname__

def _normalize(name):
""" Normalize 'name' in NKFC form """
global _unicodedata_normalize
if _unicodedata_normalize is None:
from unicodedata import _normalize
return _unicodedata_normalize('NFKC', name)

# Bootstrap-related code ######################################################

# Modules injected manually by _setup()
Expand All @@ -36,6 +43,8 @@ def _object_name(obj):
# Import done by _install_external_importers()
_bootstrap_external = None

# Import done as needed by _normalize
_unicodedata_normalize = None

def _wrap(new, old):
"""Simple substitute for functools.update_wrapper."""
Expand Down Expand Up @@ -1381,7 +1390,15 @@ def _gcd_import(name, package=None, level=0):
the loader did not.
"""
global _unicodedata
_sanity_check(name, package, level)

if not name.isascii():
name = _normalize(name)

if package is not None and not package.isascii():
package = _normalize(package)

if level > 0:
name = _resolve_name(name, package, level)
return _find_and_load(name, _gcd_import)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
It is now possible to import modules from the filesystem regardless of how
the name is normalized in the filesystem. This in particular affects
importing modules with a name that contains accented latin characters.

0 comments on commit 18d99ae

Please sign in to comment.