##// 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 exec_lines = self.master_config.Global.exec_lines
569 exec_lines = self.master_config.Global.exec_lines
570 for line in exec_lines:
570 for line in exec_lines:
571 try:
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 self.shell.runlines(line)
574 self.shell.runlines(line)
574 except:
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 self.shell.showtraceback()
578 self.shell.showtraceback()
577 except:
579 except:
578 self.log.warn("Unknown error in handling Global.exec_lines:")
580 self.log.warn("Unknown error in handling Global.exec_lines:")
@@ -582,14 +584,21 b' class IPythonApp(Application):'
582 full_filename = filefind(fname, [u'.', self.ipython_dir])
584 full_filename = filefind(fname, [u'.', self.ipython_dir])
583 if os.path.isfile(full_filename):
585 if os.path.isfile(full_filename):
584 if full_filename.endswith(u'.py'):
586 if full_filename.endswith(u'.py'):
585 self.log.info("Running file in user namespace: %s" % full_filename)
587 self.log.info("Running file in user namespace: %s" %
586 self.shell.safe_execfile(full_filename, self.shell.user_ns)
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 elif full_filename.endswith('.ipy'):
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 self.shell.safe_execfile_ipy(full_filename)
598 self.shell.safe_execfile_ipy(full_filename)
590 else:
599 else:
591 self.log.warn("File does not have a .py or .ipy extension: <%s>" % full_filename)
600 self.log.warn("File does not have a .py or .ipy extension: <%s>"
592
601 % full_filename)
593 def _run_exec_files(self):
602 def _run_exec_files(self):
594 try:
603 try:
595 if hasattr(self.master_config.Global, 'exec_files'):
604 if hasattr(self.master_config.Global, 'exec_files'):
@@ -605,10 +614,12 b' class IPythonApp(Application):'
605 if hasattr(self.master_config.Global, 'code_to_run'):
614 if hasattr(self.master_config.Global, 'code_to_run'):
606 line = self.master_config.Global.code_to_run
615 line = self.master_config.Global.code_to_run
607 try:
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 self.shell.runlines(line)
619 self.shell.runlines(line)
610 except:
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 self.shell.showtraceback()
623 self.shell.showtraceback()
613 return
624 return
614 # Like Python itself, ignore the second if the first of these is present
625 # Like Python itself, ignore the second if the first of these is present
@@ -620,7 +631,8 b' class IPythonApp(Application):'
620 try:
631 try:
621 self._exec_file(fname)
632 self._exec_file(fname)
622 except:
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 self.shell.showtraceback()
636 self.shell.showtraceback()
625
637
626 def start_app(self):
638 def start_app(self):
General Comments 0
You need to be logged in to leave comments. Login now