Skip to content

Commit 35b6195

Browse files
committed
substitute the __path__ attribute so they are updated per context
1 parent b5261ee commit 35b6195

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_imports.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,15 @@ def test_recursive_import_from():
134134
if sys.version_info.minor >= 6:
135135
import package.recpkg
136136
assert package.recpkg.context is package.recpkg.reduction.context
137+
138+
139+
def test_imp_cached_imports():
140+
import _imp
141+
142+
finder = _imp.CachedImportFinder
143+
144+
spec = finder.find_spec("encodings", None)
145+
assert spec.submodule_search_locations
146+
147+
spec = finder.find_spec("encodings.utf_8", None)
148+
assert not spec.submodule_search_locations

graalpython/lib-graalpython/_imp.py

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,6 @@ def get_frozen_object(name):
8585
is_frozen_package = get_frozen_object
8686

8787

88-
@__builtin__
89-
def freeze_module(mod, key=None):
90-
"""
91-
Freeze a module under the optional key in the language cache so that it can
92-
be shared across multiple contexts.
93-
"""
94-
path = getattr(mod, "__path__", None)
95-
name = key or mod.__name__
96-
graal_python_cache_module_code(key, mod.__file__, path)
97-
98-
9988
@__builtin__
10089
def cache_all_file_modules():
10190
"""
@@ -111,25 +100,43 @@ def cache_all_file_modules():
111100
freeze_module(v, k)
112101

113102

114-
class CachedLoader:
103+
@__builtin__
104+
def _patch_package_paths(paths):
115105
import sys
106+
return _sub_package_paths(paths, sys.graal_python_stdlib_home, "!stdlib!")
116107

117-
@staticmethod
118-
def create_module(spec):
119-
pass
120108

121-
@staticmethod
122-
def exec_module(module):
123-
modulename = module.__name__
124-
exec(graal_python_get_cached_code(modulename), module.__dict__)
125-
CachedLoader.sys.modules[modulename] = module
109+
@__builtin__
110+
def _unpatch_package_paths(paths):
111+
import sys
112+
return _sub_package_paths(paths, "!stdlib!", sys.graal_python_stdlib_home)
113+
114+
115+
@__builtin__
116+
def _sub_package_paths(paths, fro, to):
117+
if paths is not None:
118+
return [p.replace(fro, to) for p in paths]
119+
120+
121+
@__builtin__
122+
def freeze_module(mod, key=None):
123+
"""
124+
Freeze a module under the optional key in the language cache so that it can
125+
be shared across multiple contexts. If the module is a package in the
126+
standard library path, it's __path__ is substituted to not leak the standard
127+
library path to other contexts.
128+
"""
129+
import sys
130+
path = _patch_package_paths(getattr(mod, "__path__", None))
131+
name = key or mod.__name__
132+
graal_python_cache_module_code(key, mod.__file__, path)
126133

127134

128135
class CachedImportFinder:
129136
@staticmethod
130137
def find_spec(fullname, path, target=None):
131138
from _frozen_importlib import ModuleSpec
132-
path = graal_python_get_cached_code_path(fullname)
139+
path = _unpatch_package_paths(graal_python_get_cached_code_path(fullname))
133140
if path is not None:
134141
if len(path) > 0:
135142
submodule_search_locations = path
@@ -138,5 +145,21 @@ def find_spec(fullname, path, target=None):
138145
submodule_search_locations = None
139146
is_package = False
140147
spec = ModuleSpec(fullname, CachedLoader, is_package=is_package)
148+
# we're not setting origin, so the module won't have a __file__
149+
# attribute and will show up as built-in
141150
spec.submodule_search_locations = submodule_search_locations
142151
return spec
152+
153+
154+
class CachedLoader:
155+
import sys
156+
157+
@staticmethod
158+
def create_module(spec):
159+
pass
160+
161+
@staticmethod
162+
def exec_module(module):
163+
modulename = module.__name__
164+
exec(graal_python_get_cached_code(modulename), module.__dict__)
165+
CachedLoader.sys.modules[modulename] = module

0 commit comments

Comments
 (0)