Skip to content

Commit 8d239bf

Browse files
authored
[3.9] bpo-45703: Invalidate _NamespacePath cache on importlib.invalidate_cache (GH-29384) (GH-30922) (GH-31076)
Consider the following directory structure: . └── PATH1 └── namespace └── sub1 └── __init__.py And both PATH1 and PATH2 in sys path: $ PYTHONPATH=PATH1:PATH2 python3.11 >>> import namespace >>> import namespace.sub1 >>> namespace.__path__ _NamespacePath(['.../PATH1/namespace']) >>> ... While this interpreter still runs, PATH2/namespace/sub2 is created: . ├── PATH1 │ └── namespace │ └── sub1 │ └── __init__.py └── PATH2 └── namespace └── sub2 └── __init__.py The newly created module cannot be imported: >>> ... >>> namespace.__path__ _NamespacePath(['.../PATH1/namespace']) >>> import namespace.sub2 Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'namespace.sub2' Calling importlib.invalidate_caches() now newly allows to import it: >>> import importlib >>> importlib.invalidate_caches() >>> namespace.__path__ _NamespacePath(['.../PATH1/namespace']) >>> import namespace.sub2 >>> namespace.__path__ _NamespacePath(['.../PATH1/namespace', '.../PATH2/namespace']) This was not previously possible. Co-Authored-By: Miro Hrončok <[email protected]> Automerge-Triggered-By: GH:encukou
1 parent 0371e5d commit 8d239bf

File tree

4 files changed

+989
-933
lines changed

4 files changed

+989
-933
lines changed

Lib/importlib/_bootstrap_external.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1209,10 +1209,15 @@ class _NamespacePath:
12091209
using path_finder. For top-level modules, the parent module's path
12101210
is sys.path."""
12111211

1212+
# When invalidate_caches() is called, this epoch is incremented
1213+
# https://bugs.python.org/issue45703
1214+
_epoch = 0
1215+
12121216
def __init__(self, name, path, path_finder):
12131217
self._name = name
12141218
self._path = path
12151219
self._last_parent_path = tuple(self._get_parent_path())
1220+
self._last_epoch = self._epoch
12161221
self._path_finder = path_finder
12171222

12181223
def _find_parent_path_names(self):
@@ -1232,14 +1237,15 @@ def _get_parent_path(self):
12321237
def _recalculate(self):
12331238
# If the parent's path has changed, recalculate _path
12341239
parent_path = tuple(self._get_parent_path()) # Make a copy
1235-
if parent_path != self._last_parent_path:
1240+
if parent_path != self._last_parent_path or self._epoch != self._last_epoch:
12361241
spec = self._path_finder(self._name, parent_path)
12371242
# Note that no changes are made if a loader is returned, but we
12381243
# do remember the new parent path
12391244
if spec is not None and spec.loader is None:
12401245
if spec.submodule_search_locations:
12411246
self._path = spec.submodule_search_locations
12421247
self._last_parent_path = parent_path # Save the copy
1248+
self._last_epoch = self._epoch
12431249
return self._path
12441250

12451251
def __iter__(self):
@@ -1320,6 +1326,9 @@ def invalidate_caches(cls):
13201326
del sys.path_importer_cache[name]
13211327
elif hasattr(finder, 'invalidate_caches'):
13221328
finder.invalidate_caches()
1329+
# Also invalidate the caches of _NamespacePaths
1330+
# https://bugs.python.org/issue45703
1331+
_NamespacePath._epoch += 1
13231332

13241333
@classmethod
13251334
def _path_hooks(cls, path):

Lib/test/test_importlib/test_namespace_pkgs.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import importlib
33
import os
44
import sys
5+
import tempfile
56
import unittest
67

78
from test.test_importlib import util
@@ -124,6 +125,40 @@ def test_imports(self):
124125
self.assertEqual(foo.two.attr, 'portion2 foo two')
125126

126127

128+
class SeparatedNamespacePackagesCreatedWhileRunning(NamespacePackageTest):
129+
paths = ['portion1']
130+
131+
def test_invalidate_caches(self):
132+
with tempfile.TemporaryDirectory() as temp_dir:
133+
# we manipulate sys.path before anything is imported to avoid
134+
# accidental cache invalidation when changing it
135+
sys.path.append(temp_dir)
136+
137+
import foo.one
138+
self.assertEqual(foo.one.attr, 'portion1 foo one')
139+
140+
# the module does not exist, so it cannot be imported
141+
with self.assertRaises(ImportError):
142+
import foo.just_created
143+
144+
# util.create_modules() manipulates sys.path
145+
# so we must create the modules manually instead
146+
namespace_path = os.path.join(temp_dir, 'foo')
147+
os.mkdir(namespace_path)
148+
module_path = os.path.join(namespace_path, 'just_created.py')
149+
with open(module_path, 'w', encoding='utf-8') as file:
150+
file.write('attr = "just_created foo"')
151+
152+
# the module is not known, so it cannot be imported yet
153+
with self.assertRaises(ImportError):
154+
import foo.just_created
155+
156+
# but after explicit cache invalidation, it is importable
157+
importlib.invalidate_caches()
158+
import foo.just_created
159+
self.assertEqual(foo.just_created.attr, 'just_created foo')
160+
161+
127162
class SeparatedOverlappingNamespacePackages(NamespacePackageTest):
128163
paths = ['portion1', 'both_portions']
129164

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
When a namespace package is imported before another module from the same
2+
namespace is created/installed in a different :data:`sys.path` location
3+
while the program is running, calling the
4+
:func:`importlib.invalidate_caches` function will now also guarantee the new
5+
module is noticed.

0 commit comments

Comments
 (0)