|
24 | 24 | import textwrap |
25 | 25 | import warnings |
26 | 26 | from ctypes import c_char_p, c_void_p |
| 27 | +from importlib.abc import Loader, MetaPathFinder |
| 28 | +from importlib.machinery import ModuleSpec |
27 | 29 | from logging import getLogger # see `.logger` |
28 | 30 | from types import ModuleType # this is python 3.3 specific |
29 | 31 |
|
@@ -189,7 +191,7 @@ def __try_getattr(self, name): |
189 | 191 | if self._julia.isamodule(jl_fullname): |
190 | 192 | realname = self._julia.fullname(self._julia.eval(jl_fullname)) |
191 | 193 | if self._julia.isdefined(realname): |
192 | | - return self.__loader__.load_module("julia." + realname) |
| 194 | + return self.__loader__.create_module(_find_spec_from_fullname("julia." + realname)) |
193 | 195 | # Otherwise, it may be, e.g., "Main.anonymous", created by |
194 | 196 | # Module(). |
195 | 197 |
|
@@ -220,27 +222,31 @@ def __setattr__(self, name, value): |
220 | 222 |
|
221 | 223 |
|
222 | 224 | # add custom import behavior for the julia "module" |
223 | | -class JuliaImporter(object): |
| 225 | +class JuliaImporter(MetaPathFinder): |
224 | 226 |
|
225 | | - # find_module was deprecated in v3.4 |
226 | | - def find_module(self, fullname, path=None): |
227 | | - if fullname.startswith("julia."): |
228 | | - filename = fullname.split(".", 2)[1] |
229 | | - filepath = os.path.join(os.path.dirname(__file__), filename) |
230 | | - if os.path.isfile(filepath + ".py") or os.path.isdir(filepath): |
231 | | - return |
232 | | - return JuliaModuleLoader() |
| 227 | + def find_spec(self, fullname, path=None, target=None): |
| 228 | + return _find_spec_from_fullname(fullname) |
233 | 229 |
|
234 | 230 |
|
235 | | -class JuliaModuleLoader(object): |
| 231 | +def _find_spec_from_fullname(fullname): |
| 232 | + if fullname.startswith("julia."): |
| 233 | + filename = fullname.split(".", 2)[1] |
| 234 | + filepath = os.path.join(os.path.dirname(__file__), filename) |
| 235 | + if os.path.isfile(filepath + ".py") or os.path.isdir(filepath): |
| 236 | + return |
| 237 | + return ModuleSpec(fullname, JuliaModuleLoader(), origin=filepath) |
236 | 238 |
|
| 239 | +class JuliaModuleLoader(Loader): |
237 | 240 | @property |
238 | 241 | def julia(self): |
239 | 242 | self.__class__.julia = julia = Julia() |
240 | 243 | return julia |
241 | 244 |
|
242 | | - # load module was deprecated in v3.4 |
243 | | - def load_module(self, fullname): |
| 245 | + def exec_module(self, module): |
| 246 | + pass |
| 247 | + |
| 248 | + def create_module(self, spec): |
| 249 | + fullname = spec.name |
244 | 250 | juliapath = remove_prefix(fullname, "julia.") |
245 | 251 | if juliapath == 'Main': |
246 | 252 | return sys.modules.setdefault(fullname, |
|
0 commit comments