##// END OF EJS Templates
support using '%run -m' for modules importable via an import hook
Nathan Goldbaum -
Show More
@@ -692,6 +692,11 b' class ExecutionMagics(Magics):'
692 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
692 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
693 error(msg)
693 error(msg)
694 return
694 return
695 except TypeError:
696 if fpath in sys.meta_path:
697 filename = ""
698 else:
699 raise
695
700
696 if filename.lower().endswith(('.ipy', '.ipynb')):
701 if filename.lower().endswith(('.ipy', '.ipynb')):
697 with preserve_keys(self.shell.user_ns, '__file__'):
702 with preserve_keys(self.shell.user_ns, '__file__'):
@@ -1198,3 +1198,49 b' def test_timeit_arguments():'
1198 # 3.7 optimize no-op statement like above out, and complain there is
1198 # 3.7 optimize no-op statement like above out, and complain there is
1199 # nothing in the for loop.
1199 # nothing in the for loop.
1200 _ip.magic("timeit -n1 -r1 a=('#')")
1200 _ip.magic("timeit -n1 -r1 a=('#')")
1201
1202
1203 TEST_MODULE = """
1204 print('Loaded my_tmp')
1205 if __name__ == "__main__":
1206 print('I just ran a script')
1207 """
1208
1209
1210 def test_run_module_from_import_hook():
1211 "Test that a module can be loaded via an import hook"
1212 with TemporaryDirectory() as tmpdir:
1213 fullpath = os.path.join(tmpdir, 'my_tmp.py')
1214 with open(fullpath, 'w') as f:
1215 f.write(TEST_MODULE)
1216
1217 class MyTempImporter(object):
1218 def __init__(self):
1219 pass
1220
1221 def find_module(self, fullname, path=None):
1222 if 'my_tmp' in fullname:
1223 return self
1224 return None
1225
1226 def load_module(self, name):
1227 import imp
1228 return imp.load_source('my_tmp', fullpath)
1229
1230 def get_code(self, fullname):
1231 with open(fullpath, 'r') as f:
1232 return compile(f.read(), 'foo', 'exec')
1233
1234 def is_package(self, __):
1235 return False
1236
1237 sys.meta_path.insert(0, MyTempImporter())
1238
1239 with capture_output() as captured:
1240 _ip.magic("run -m my_tmp")
1241 _ip.run_cell("import my_tmp")
1242
1243 output = "Loaded my_tmp\nI just ran a script\nLoaded my_tmp\n"
1244 nt.assert_equal(output, captured.stdout)
1245
1246 sys.meta_path.pop(0)
@@ -20,6 +20,7 b' Utility functions for finding modules on sys.path.'
20 # Stdlib imports
20 # Stdlib imports
21 import importlib
21 import importlib
22 import os
22 import os
23 import sys
23
24
24 # Third-party imports
25 # Third-party imports
25
26
@@ -61,6 +62,8 b' def find_mod(module_name):'
61 loader = importlib.util.find_spec(module_name)
62 loader = importlib.util.find_spec(module_name)
62 module_path = loader.origin
63 module_path = loader.origin
63 if module_path is None:
64 if module_path is None:
65 if loader.loader in sys.meta_path:
66 return loader.loader
64 return None
67 return None
65 else:
68 else:
66 split_path = module_path.split(".")
69 split_path = module_path.split(".")
General Comments 0
You need to be logged in to leave comments. Login now