##// END OF EJS Templates
Merge pull request #1352 from bfroehle/run_module...
Min RK -
r6096:39adc27e merge
parent child Browse files
Show More
@@ -26,6 +26,7 b' import codeop'
26 import inspect
26 import inspect
27 import os
27 import os
28 import re
28 import re
29 import runpy
29 import sys
30 import sys
30 import tempfile
31 import tempfile
31 import types
32 import types
@@ -2356,6 +2357,28 b' class InteractiveShell(SingletonConfigurable, Magic):'
2356 self.showtraceback()
2357 self.showtraceback()
2357 warn('Unknown failure executing file: <%s>' % fname)
2358 warn('Unknown failure executing file: <%s>' % fname)
2358
2359
2360 def safe_run_module(self, mod_name, where):
2361 """A safe version of runpy.run_module().
2362
2363 This version will never throw an exception, but instead print
2364 helpful error messages to the screen.
2365
2366 Parameters
2367 ----------
2368 mod_name : string
2369 The name of the module to be executed.
2370 where : dict
2371 The globals namespace.
2372 """
2373 try:
2374 where.update(
2375 runpy.run_module(str(mod_name), run_name="__main__",
2376 alter_sys=True)
2377 )
2378 except:
2379 self.showtraceback()
2380 warn('Unknown failure executing module: <%s>' % mod_name)
2381
2359 def run_cell(self, raw_cell, store_history=False):
2382 def run_cell(self, raw_cell, store_history=False):
2360 """Run a complete IPython cell.
2383 """Run a complete IPython cell.
2361
2384
@@ -103,6 +103,7 b' shell_aliases = dict('
103 logfile='InteractiveShell.logfile',
103 logfile='InteractiveShell.logfile',
104 logappend='InteractiveShell.logappend',
104 logappend='InteractiveShell.logappend',
105 c='InteractiveShellApp.code_to_run',
105 c='InteractiveShellApp.code_to_run',
106 m='InteractiveShellApp.module_to_run',
106 ext='InteractiveShellApp.extra_extension',
107 ext='InteractiveShellApp.extra_extension',
107 )
108 )
108 shell_aliases['cache-size'] = 'InteractiveShell.cache_size'
109 shell_aliases['cache-size'] = 'InteractiveShell.cache_size'
@@ -146,6 +147,9 b' class InteractiveShellApp(Configurable):'
146 code_to_run = Unicode('', config=True,
147 code_to_run = Unicode('', config=True,
147 help="Execute the given command string."
148 help="Execute the given command string."
148 )
149 )
150 module_to_run = Unicode('', config=True,
151 help="Run the module as a script."
152 )
149 pylab_import_all = Bool(True, config=True,
153 pylab_import_all = Bool(True, config=True,
150 help="""If true, an 'import *' is done from numpy and pylab,
154 help="""If true, an 'import *' is done from numpy and pylab,
151 when using pylab"""
155 when using pylab"""
@@ -183,6 +187,7 b' class InteractiveShellApp(Configurable):'
183 self._run_exec_lines()
187 self._run_exec_lines()
184 self._run_exec_files()
188 self._run_exec_files()
185 self._run_cmd_line_code()
189 self._run_cmd_line_code()
190 self._run_module()
186
191
187 # flush output, so itwon't be attached to the first cell
192 # flush output, so itwon't be attached to the first cell
188 sys.stdout.flush()
193 sys.stdout.flush()
@@ -294,3 +299,15 b' class InteractiveShellApp(Configurable):'
294 fname)
299 fname)
295 self.shell.showtraceback()
300 self.shell.showtraceback()
296
301
302 def _run_module(self):
303 """Run module specified at the command-line."""
304 if self.module_to_run:
305 # Make sure that the module gets a proper sys.argv as if it were
306 # run using `python -m`.
307 save_argv = sys.argv
308 sys.argv = [sys.executable] + self.extra_args
309 try:
310 self.shell.safe_run_module(self.module_to_run,
311 self.shell.user_ns)
312 finally:
313 sys.argv = save_argv
@@ -265,13 +265,16 b' class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):'
265 self.interact = True
265 self.interact = True
266
266
267 def _file_to_run_changed(self, name, old, new):
267 def _file_to_run_changed(self, name, old, new):
268 if new:
269 self.something_to_run = True
268 if new and not self.force_interact:
270 if new and not self.force_interact:
269 self.interact = False
271 self.interact = False
270 _code_to_run_changed = _file_to_run_changed
272 _code_to_run_changed = _file_to_run_changed
273 _module_to_run_changed = _file_to_run_changed
271
274
272 # internal, not-configurable
275 # internal, not-configurable
273 interact=Bool(True)
276 interact=Bool(True)
274
277 something_to_run=Bool(False)
275
278
276 def parse_command_line(self, argv=None):
279 def parse_command_line(self, argv=None):
277 """override to allow old '-pylab' flag with deprecation warning"""
280 """override to allow old '-pylab' flag with deprecation warning"""
@@ -306,7 +309,7 b' class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):'
306 if not self.ignore_old_config:
309 if not self.ignore_old_config:
307 check_for_old_config(self.ipython_dir)
310 check_for_old_config(self.ipython_dir)
308 # print self.extra_args
311 # print self.extra_args
309 if self.extra_args:
312 if self.extra_args and not self.something_to_run:
310 self.file_to_run = self.extra_args[0]
313 self.file_to_run = self.extra_args[0]
311 # create the shell
314 # create the shell
312 self.init_shell()
315 self.init_shell()
General Comments 0
You need to be logged in to leave comments. Login now