##// END OF EJS Templates
MyTempImporter: implement modern interface
Nikita Kniazev -
Show More
@@ -1332,24 +1332,23 b' def test_run_module_from_import_hook():'
1332 fullpath = os.path.join(tmpdir, 'my_tmp.py')
1332 fullpath = os.path.join(tmpdir, 'my_tmp.py')
1333 Path(fullpath).write_text(TEST_MODULE)
1333 Path(fullpath).write_text(TEST_MODULE)
1334
1334
1335 class MyTempImporter(object):
1335 import importlib.abc
1336 def __init__(self):
1336 import importlib.util
1337 pass
1337
1338
1338 class MyTempImporter(importlib.abc.MetaPathFinder, importlib.abc.SourceLoader):
1339 def find_module(self, fullname, path=None):
1339 def find_spec(self, fullname, path, target=None):
1340 if 'my_tmp' in fullname:
1340 if fullname == "my_tmp":
1341 return self
1341 return importlib.util.spec_from_loader(fullname, self)
1342 return None
1342
1343
1343 def get_filename(self, fullname):
1344 def load_module(self, name):
1344 if fullname != "my_tmp":
1345 import imp
1345 raise ImportError(f"unexpected module name '{fullname}'")
1346 return imp.load_source('my_tmp', fullpath)
1346 return fullpath
1347
1347
1348 def get_code(self, fullname):
1348 def get_data(self, path):
1349 return compile(Path(fullpath).read_text(), "foo", "exec")
1349 if not Path(path).samefile(fullpath):
1350
1350 raise OSError(f"expected path '{fullpath}', got '{path}'")
1351 def is_package(self, __):
1351 return Path(fullpath).read_text()
1352 return False
1353
1352
1354 sys.meta_path.insert(0, MyTempImporter())
1353 sys.meta_path.insert(0, MyTempImporter())
1355
1354
General Comments 0
You need to be logged in to leave comments. Login now