##// END OF EJS Templates
Ensure that __file__ is defined when loading scripts....
Fernando Perez -
Show More
@@ -569,10 +569,12 b' class IPythonApp(Application):'
569 569 exec_lines = self.master_config.Global.exec_lines
570 570 for line in exec_lines:
571 571 try:
572 self.log.info("Running code in user namespace: %s" % line)
572 self.log.info("Running code in user namespace: %s" %
573 line)
573 574 self.shell.runlines(line)
574 575 except:
575 self.log.warn("Error in executing line in user namespace: %s" % line)
576 self.log.warn("Error in executing line in user "
577 "namespace: %s" % line)
576 578 self.shell.showtraceback()
577 579 except:
578 580 self.log.warn("Unknown error in handling Global.exec_lines:")
@@ -582,14 +584,21 b' class IPythonApp(Application):'
582 584 full_filename = filefind(fname, [u'.', self.ipython_dir])
583 585 if os.path.isfile(full_filename):
584 586 if full_filename.endswith(u'.py'):
585 self.log.info("Running file in user namespace: %s" % full_filename)
586 self.shell.safe_execfile(full_filename, self.shell.user_ns)
587 self.log.info("Running file in user namespace: %s" %
588 full_filename)
589 # Ensure that __file__ is always defined to match Python behavior
590 self.shell.user_ns['__file__'] = fname
591 try:
592 self.shell.safe_execfile(full_filename, self.shell.user_ns)
593 finally:
594 del self.shell.user_ns['__file__']
587 595 elif full_filename.endswith('.ipy'):
588 self.log.info("Running file in user namespace: %s" % full_filename)
596 self.log.info("Running file in user namespace: %s" %
597 full_filename)
589 598 self.shell.safe_execfile_ipy(full_filename)
590 599 else:
591 self.log.warn("File does not have a .py or .ipy extension: <%s>" % full_filename)
592
600 self.log.warn("File does not have a .py or .ipy extension: <%s>"
601 % full_filename)
593 602 def _run_exec_files(self):
594 603 try:
595 604 if hasattr(self.master_config.Global, 'exec_files'):
@@ -605,10 +614,12 b' class IPythonApp(Application):'
605 614 if hasattr(self.master_config.Global, 'code_to_run'):
606 615 line = self.master_config.Global.code_to_run
607 616 try:
608 self.log.info("Running code given at command line (-c): %s" % line)
617 self.log.info("Running code given at command line (-c): %s" %
618 line)
609 619 self.shell.runlines(line)
610 620 except:
611 self.log.warn("Error in executing line in user namespace: %s" % line)
621 self.log.warn("Error in executing line in user namespace: %s" %
622 line)
612 623 self.shell.showtraceback()
613 624 return
614 625 # Like Python itself, ignore the second if the first of these is present
@@ -620,7 +631,8 b' class IPythonApp(Application):'
620 631 try:
621 632 self._exec_file(fname)
622 633 except:
623 self.log.warn("Error in executing file in user namespace: %s" % fname)
634 self.log.warn("Error in executing file in user namespace: %s" %
635 fname)
624 636 self.shell.showtraceback()
625 637
626 638 def start_app(self):
General Comments 0
You need to be logged in to leave comments. Login now