##// 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 import imp, os
94 import imp, os
95
95
96 def find_module(name, path=None):
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 """
100 if name is None:
101 return None
102 try:
99 file, filename, _ = imp.find_module(name, path)
103 file, filename, _ = imp.find_module(name, path)
104 except ImportError:
105 return None
100 if file is None:
106 if file is None:
101 return filename
107 return filename
102 else:
108 else:
103 file.close()
109 file.close()
110 if os.path.splitext(filename)[1] in [".py", "pyc"]:
104 return filename
111 return filename
112 else:
113 return None
105
114
106 def get_init(dirname):
115 def get_init(dirname):
107 """Get __init__ file path for module with directory dirname
116 """Get __init__ file path for module with directory dirname
108 """
117 """
109 fbase = os.path.join(dirname, "__init__")
118 fbase = os.path.join(dirname, "__init__")
110 for ext in [".py", ".pyw", ".pyc", ".pyo"]:
119 for ext in [".py", ".pyw"]:
111 fname = fbase + ext
120 fname = fbase + ext
112 if os.path.isfile(fname):
121 if os.path.isfile(fname):
113 return fname
122 return fname
@@ -117,13 +126,10 b' def find_mod(name):'
117 """Find module *name* on sys.path
126 """Find module *name* on sys.path
118 """
127 """
119 parts = name.split(".")
128 parts = name.split(".")
120 if len(parts) == 1:
121 basepath = find_module(parts[0])
122 else:
123 basepath = find_module(parts[0])
129 basepath = find_module(parts[0])
124 for submodname in parts[1:]:
130 for submodname in parts[1:]:
125 basepath = find_module(submodname, [basepath])
131 basepath = find_module(submodname, [basepath])
126 if os.path.isdir(basepath):
132 if basepath and os.path.isdir(basepath):
127 basepath = get_init(basepath)
133 basepath = get_init(basepath)
128 return basepath
134 return basepath
129
135
@@ -1596,7 +1602,10 b' Currently the magic system has the following functions:\\n"""'
1596 just as if the commands were written on IPython prompt.
1602 just as if the commands were written on IPython prompt.
1597
1603
1598 -m: specify module name to load instead of script path. Similar to
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 %run -m example
1610 %run -m example
1602
1611
@@ -1607,10 +1616,13 b' Currently the magic system has the following functions:\\n"""'
1607 # get arguments and set sys.argv for program to be run.
1616 # get arguments and set sys.argv for program to be run.
1608 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:em:',
1617 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:em:',
1609 mode='list',list_all=1)
1618 mode='list',list_all=1)
1610 if opts.has_key("m"):
1619 if "m" in opts:
1611 modulename = opts.get("m")[0]
1620 modulename = opts["m"][0]
1612 arg_lst = [find_mod(modulename)]
1621 modpath = find_mod(modulename)
1613
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 try:
1626 try:
1615 filename = file_finder(arg_lst[0])
1627 filename = file_finder(arg_lst[0])
1616 except IndexError:
1628 except IndexError:
General Comments 0
You need to be logged in to leave comments. Login now