Show More
@@ -2448,6 +2448,9 b' class InteractiveShell(SingletonConfigurable):' | |||||
2448 | dname = os.path.dirname(fname) |
|
2448 | dname = os.path.dirname(fname) | |
2449 |
|
2449 | |||
2450 | with prepended_to_syspath(dname): |
|
2450 | with prepended_to_syspath(dname): | |
|
2451 | # Ensure that __file__ is always defined to match Python behavior | |||
|
2452 | save_fname = self.user_ns.get('__file__',None) | |||
|
2453 | self.user_ns['__file__'] = fname | |||
2451 | try: |
|
2454 | try: | |
2452 | py3compat.execfile(fname,*where) |
|
2455 | py3compat.execfile(fname,*where) | |
2453 | except SystemExit as status: |
|
2456 | except SystemExit as status: | |
@@ -2468,6 +2471,8 b' class InteractiveShell(SingletonConfigurable):' | |||||
2468 | if kw['raise_exceptions']: |
|
2471 | if kw['raise_exceptions']: | |
2469 | raise |
|
2472 | raise | |
2470 | self.showtraceback() |
|
2473 | self.showtraceback() | |
|
2474 | finally: | |||
|
2475 | self.user_ns['__file__'] = save_fname | |||
2471 |
|
2476 | |||
2472 | def safe_execfile_ipy(self, fname): |
|
2477 | def safe_execfile_ipy(self, fname): | |
2473 | """Like safe_execfile, but for .ipy files with IPython syntax. |
|
2478 | """Like safe_execfile, but for .ipy files with IPython syntax. | |
@@ -2494,6 +2499,9 b' class InteractiveShell(SingletonConfigurable):' | |||||
2494 | dname = os.path.dirname(fname) |
|
2499 | dname = os.path.dirname(fname) | |
2495 |
|
2500 | |||
2496 | with prepended_to_syspath(dname): |
|
2501 | with prepended_to_syspath(dname): | |
|
2502 | # Ensure that __file__ is always defined to match Python behavior | |||
|
2503 | save_fname = self.user_ns.get('__file__',None) | |||
|
2504 | self.user_ns['__file__'] = fname | |||
2497 | try: |
|
2505 | try: | |
2498 | with open(fname) as thefile: |
|
2506 | with open(fname) as thefile: | |
2499 | # self.run_cell currently captures all exceptions |
|
2507 | # self.run_cell currently captures all exceptions | |
@@ -2504,6 +2512,8 b' class InteractiveShell(SingletonConfigurable):' | |||||
2504 | except: |
|
2512 | except: | |
2505 | self.showtraceback() |
|
2513 | self.showtraceback() | |
2506 | warn('Unknown failure executing file: <%s>' % fname) |
|
2514 | warn('Unknown failure executing file: <%s>' % fname) | |
|
2515 | finally: | |||
|
2516 | self.user_ns['__file__'] = save_fname | |||
2507 |
|
2517 | |||
2508 | def safe_run_module(self, mod_name, where): |
|
2518 | def safe_run_module(self, mod_name, where): | |
2509 | """A safe version of runpy.run_module(). |
|
2519 | """A safe version of runpy.run_module(). |
@@ -277,20 +277,12 b' class InteractiveShellApp(Configurable):' | |||||
277 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] |
|
277 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] | |
278 | try: |
|
278 | try: | |
279 | if os.path.isfile(full_filename): |
|
279 | if os.path.isfile(full_filename): | |
|
280 | self.log.info("Running file in user namespace: %s" % full_filename) | |||
280 | if full_filename.endswith('.ipy'): |
|
281 | if full_filename.endswith('.ipy'): | |
281 | self.log.info("Running file in user namespace: %s" % |
|
|||
282 | full_filename) |
|
|||
283 | self.shell.safe_execfile_ipy(full_filename) |
|
282 | self.shell.safe_execfile_ipy(full_filename) | |
284 | else: |
|
283 | else: | |
285 | # default to python, even without extension |
|
284 | # default to python, even without extension | |
286 | self.log.info("Running file in user namespace: %s" % |
|
285 | self.shell.safe_execfile(full_filename, self.shell.user_ns) | |
287 | full_filename) |
|
|||
288 | # Ensure that __file__ is always defined to match Python behavior |
|
|||
289 | self.shell.user_ns['__file__'] = fname |
|
|||
290 | try: |
|
|||
291 | self.shell.safe_execfile(full_filename, self.shell.user_ns) |
|
|||
292 | finally: |
|
|||
293 | del self.shell.user_ns['__file__'] |
|
|||
294 | finally: |
|
286 | finally: | |
295 | sys.argv = save_argv |
|
287 | sys.argv = save_argv | |
296 |
|
288 |
General Comments 0
You need to be logged in to leave comments.
Login now