Skip to content

Commit 18d99ae

Browse files
Some changes
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.
1 parent 7d2d2bf commit 18d99ae

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

Doc/library/importlib.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ ABC hierarchy::
256256
:func:`importlib.util.spec_from_loader` may be useful for implementing
257257
concrete ``MetaPathFinders``.
258258

259+
*Fullname* must be normalized in NFKC to match the normalization
260+
done by the Python parser.
261+
259262
.. versionadded:: 3.4
260263

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

296+
*Fullname* must be normalized in NFKC to match the normalization
297+
done by the Python parser.
298+
293299
.. versionadded:: 3.4
294300

295301
.. method:: invalidate_caches()

Lib/importlib/_bootstrap.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ def _object_name(obj):
2626
except AttributeError:
2727
return type(obj).__qualname__
2828

29+
def _normalize(name):
30+
""" Normalize 'name' in NKFC form """
31+
global _unicodedata_normalize
32+
if _unicodedata_normalize is None:
33+
from unicodedata import _normalize
34+
return _unicodedata_normalize('NFKC', name)
35+
2936
# Bootstrap-related code ######################################################
3037

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

46+
# Import done as needed by _normalize
47+
_unicodedata_normalize = None
3948

4049
def _wrap(new, old):
4150
"""Simple substitute for functools.update_wrapper."""
@@ -1381,7 +1390,15 @@ def _gcd_import(name, package=None, level=0):
13811390
the loader did not.
13821391
13831392
"""
1393+
global _unicodedata
13841394
_sanity_check(name, package, level)
1395+
1396+
if not name.isascii():
1397+
name = _normalize(name)
1398+
1399+
if package is not None and not package.isascii():
1400+
package = _normalize(package)
1401+
13851402
if level > 0:
13861403
name = _resolve_name(name, package, level)
13871404
return _find_and_load(name, _gcd_import)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
It is now possible to import modules from the filesystem regardless of how
2+
the name is normalized in the filesystem. This in particular affects
3+
importing modules with a name that contains accented latin characters.

0 commit comments

Comments
 (0)