@@ -85,17 +85,6 @@ def get_frozen_object(name):
85
85
is_frozen_package = get_frozen_object
86
86
87
87
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
-
99
88
@__builtin__
100
89
def cache_all_file_modules ():
101
90
"""
@@ -111,25 +100,43 @@ def cache_all_file_modules():
111
100
freeze_module (v , k )
112
101
113
102
114
- class CachedLoader :
103
+ @__builtin__
104
+ def _patch_package_paths (paths ):
115
105
import sys
106
+ return _sub_package_paths (paths , sys .graal_python_stdlib_home , "!stdlib!" )
116
107
117
- @staticmethod
118
- def create_module (spec ):
119
- pass
120
108
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 )
126
133
127
134
128
135
class CachedImportFinder :
129
136
@staticmethod
130
137
def find_spec (fullname , path , target = None ):
131
138
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 ) )
133
140
if path is not None :
134
141
if len (path ) > 0 :
135
142
submodule_search_locations = path
@@ -138,5 +145,21 @@ def find_spec(fullname, path, target=None):
138
145
submodule_search_locations = None
139
146
is_package = False
140
147
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
141
150
spec .submodule_search_locations = submodule_search_locations
142
151
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