Show More
@@ -47,7 +47,6 b' from IPython.core.displayhook import DisplayHook' | |||||
47 | from IPython.core.displaypub import DisplayPublisher |
|
47 | from IPython.core.displaypub import DisplayPublisher | |
48 | from IPython.core.error import UsageError |
|
48 | from IPython.core.error import UsageError | |
49 | from IPython.core.extensions import ExtensionManager |
|
49 | from IPython.core.extensions import ExtensionManager | |
50 | from IPython.core.fakemodule import FakeModule, init_fakemod_dict |
|
|||
51 | from IPython.core.formatters import DisplayFormatter |
|
50 | from IPython.core.formatters import DisplayFormatter | |
52 | from IPython.core.history import HistoryManager |
|
51 | from IPython.core.history import HistoryManager | |
53 | from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2 |
|
52 | from IPython.core.inputsplitter import IPythonInputSplitter, ESC_MAGIC, ESC_MAGIC2 | |
@@ -826,15 +825,18 b' class InteractiveShell(SingletonConfigurable):' | |||||
826 | # Things related to the "main" module |
|
825 | # Things related to the "main" module | |
827 | #------------------------------------------------------------------------- |
|
826 | #------------------------------------------------------------------------- | |
828 |
|
827 | |||
829 | def new_main_mod(self, filename): |
|
828 | def new_main_mod(self, filename, modname): | |
830 | """Return a new 'main' module object for user code execution. |
|
829 | """Return a new 'main' module object for user code execution. | |
831 |
|
830 | |||
832 | ``filename`` should be the path of the script which will be run in the |
|
831 | ``filename`` should be the path of the script which will be run in the | |
833 | module. Requests with the same filename will get the same module, with |
|
832 | module. Requests with the same filename will get the same module, with | |
834 | its namespace cleared. |
|
833 | its namespace cleared. | |
835 |
|
834 | |||
|
835 | ``modname`` should be the module name - normally either '__main__' or | |||
|
836 | the basename of the file without the extension. | |||
|
837 | ||||
836 | When scripts are executed via %run, we must keep a reference to their |
|
838 | When scripts are executed via %run, we must keep a reference to their | |
837 |
__main__ module |
|
839 | __main__ module around so that Python doesn't | |
838 | clear it, rendering references to module globals useless. |
|
840 | clear it, rendering references to module globals useless. | |
839 |
|
841 | |||
840 | This method keeps said reference in a private dict, keyed by the |
|
842 | This method keeps said reference in a private dict, keyed by the | |
@@ -847,9 +849,16 b' class InteractiveShell(SingletonConfigurable):' | |||||
847 | try: |
|
849 | try: | |
848 | main_mod = self._main_mod_cache[filename] |
|
850 | main_mod = self._main_mod_cache[filename] | |
849 | except KeyError: |
|
851 | except KeyError: | |
850 |
main_mod = self._main_mod_cache[filename] = |
|
852 | main_mod = self._main_mod_cache[filename] = types.ModuleType(modname, | |
|
853 | doc="Module created for script run in IPython") | |||
851 | else: |
|
854 | else: | |
852 | init_fakemod_dict(main_mod) |
|
855 | main_mod.__dict__.clear() | |
|
856 | main_mod.__name__ = modname | |||
|
857 | ||||
|
858 | main_mod.__file__ = filename | |||
|
859 | # It seems pydoc (and perhaps others) needs any module instance to | |||
|
860 | # implement a __nonzero__ method | |||
|
861 | main_mod.__nonzero__ = lambda : True | |||
853 |
|
862 | |||
854 | return main_mod |
|
863 | return main_mod | |
855 |
|
864 | |||
@@ -863,7 +872,7 b' class InteractiveShell(SingletonConfigurable):' | |||||
863 |
|
872 | |||
864 | In [15]: import IPython |
|
873 | In [15]: import IPython | |
865 |
|
874 | |||
866 | In [16]: m = _ip.new_main_mod(IPython.__file__) |
|
875 | In [16]: m = _ip.new_main_mod(IPython.__file__, 'IPython') | |
867 |
|
876 | |||
868 | In [17]: len(_ip._main_mod_cache) > 0 |
|
877 | In [17]: len(_ip._main_mod_cache) > 0 | |
869 | Out[17]: True |
|
878 | Out[17]: True |
@@ -550,6 +550,11 b' python-profiler package from non-free.""")' | |||||
550 | __name__save = self.shell.user_ns['__name__'] |
|
550 | __name__save = self.shell.user_ns['__name__'] | |
551 | prog_ns['__name__'] = '__main__' |
|
551 | prog_ns['__name__'] = '__main__' | |
552 | main_mod = self.shell.user_module |
|
552 | main_mod = self.shell.user_module | |
|
553 | ||||
|
554 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must | |||
|
555 | # set the __file__ global in the script's namespace | |||
|
556 | # TK: Is this necessary in interactive mode? | |||
|
557 | prog_ns['__file__'] = filename | |||
553 | else: |
|
558 | else: | |
554 | # Run in a fresh, empty namespace |
|
559 | # Run in a fresh, empty namespace | |
555 | if 'n' in opts: |
|
560 | if 'n' in opts: | |
@@ -560,13 +565,8 b' python-profiler package from non-free.""")' | |||||
560 | # The shell MUST hold a reference to prog_ns so after %run |
|
565 | # The shell MUST hold a reference to prog_ns so after %run | |
561 | # exits, the python deletion mechanism doesn't zero it out |
|
566 | # exits, the python deletion mechanism doesn't zero it out | |
562 | # (leaving dangling references). See interactiveshell for details |
|
567 | # (leaving dangling references). See interactiveshell for details | |
563 | main_mod = self.shell.new_main_mod(filename) |
|
568 | main_mod = self.shell.new_main_mod(filename, name) | |
564 | prog_ns = main_mod.__dict__ |
|
569 | prog_ns = main_mod.__dict__ | |
565 | prog_ns['__name__'] = name |
|
|||
566 |
|
||||
567 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must |
|
|||
568 | # set the __file__ global in the script's namespace |
|
|||
569 | prog_ns['__file__'] = filename |
|
|||
570 |
|
570 | |||
571 | # pickle fix. See interactiveshell for an explanation. But we need to |
|
571 | # pickle fix. See interactiveshell for an explanation. But we need to | |
572 | # make sure that, if we overwrite __main__, we replace it at the end |
|
572 | # make sure that, if we overwrite __main__, we replace it at the end |
General Comments 0
You need to be logged in to leave comments.
Login now