##// END OF EJS Templates
Improve error handling for %run -m
Jörgen Stenarson -
Show More
@@ -94,20 +94,29 b' def needs_local_scope(func):'
94 94 import imp, os
95 95
96 96 def find_module(name, path=None):
97 """imp.find_module variant that only return path of module
97 """imp.find_module variant that only return path of module.
98 Return None if module is missing or does not have .py or .pyw extension
98 99 """
99 file, filename, _ = imp.find_module(name, path)
100 if name is None:
101 return None
102 try:
103 file, filename, _ = imp.find_module(name, path)
104 except ImportError:
105 return None
100 106 if file is None:
101 107 return filename
102 108 else:
103 109 file.close()
104 return filename
110 if os.path.splitext(filename)[1] in [".py", "pyc"]:
111 return filename
112 else:
113 return None
105 114
106 115 def get_init(dirname):
107 116 """Get __init__ file path for module with directory dirname
108 117 """
109 118 fbase = os.path.join(dirname, "__init__")
110 for ext in [".py", ".pyw", ".pyc", ".pyo"]:
119 for ext in [".py", ".pyw"]:
111 120 fname = fbase + ext
112 121 if os.path.isfile(fname):
113 122 return fname
@@ -117,13 +126,10 b' def find_mod(name):'
117 126 """Find module *name* on sys.path
118 127 """
119 128 parts = name.split(".")
120 if len(parts) == 1:
121 basepath = find_module(parts[0])
122 else:
123 basepath = find_module(parts[0])
124 for submodname in parts[1:]:
125 basepath = find_module(submodname, [basepath])
126 if os.path.isdir(basepath):
129 basepath = find_module(parts[0])
130 for submodname in parts[1:]:
131 basepath = find_module(submodname, [basepath])
132 if basepath and os.path.isdir(basepath):
127 133 basepath = get_init(basepath)
128 134 return basepath
129 135
@@ -1596,7 +1602,10 b' Currently the magic system has the following functions:\\n"""'
1596 1602 just as if the commands were written on IPython prompt.
1597 1603
1598 1604 -m: specify module name to load instead of script path. Similar to
1599 the -m option for the python interpreter. For example:
1605 the -m option for the python interpreter. Use this option last if you
1606 want to combine with other %run options. Unlike the python interpreter
1607 only source modules are allowed no .pyc or .pyo files.
1608 For example:
1600 1609
1601 1610 %run -m example
1602 1611
@@ -1607,10 +1616,13 b' Currently the magic system has the following functions:\\n"""'
1607 1616 # get arguments and set sys.argv for program to be run.
1608 1617 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:em:',
1609 1618 mode='list',list_all=1)
1610 if opts.has_key("m"):
1611 modulename = opts.get("m")[0]
1612 arg_lst = [find_mod(modulename)]
1613
1619 if "m" in opts:
1620 modulename = opts["m"][0]
1621 modpath = find_mod(modulename)
1622 if modpath is None:
1623 warn('%r is not a valid modulename on sys.path'%modulename)
1624 return
1625 arg_lst = [modpath] + arg_lst
1614 1626 try:
1615 1627 filename = file_finder(arg_lst[0])
1616 1628 except IndexError:
General Comments 0
You need to be logged in to leave comments. Login now