##// END OF EJS Templates
Simplify caching of modules from %run-ing scripts....
Thomas Kluyver -
Show More
@@ -819,16 +819,9 b' class InteractiveShell(SingletonConfigurable):'
819 819 # Things related to the "main" module
820 820 #-------------------------------------------------------------------------
821 821
822 def new_main_mod(self,ns=None):
822 def new_main_mod(self,filename):
823 823 """Return a new 'main' module object for user code execution.
824 """
825 main_mod = self._user_main_module
826 init_fakemod_dict(main_mod,ns)
827 return main_mod
828
829 def cache_main_mod(self,ns,fname):
830 """Cache a main module's namespace.
831
824
832 825 When scripts are executed via %run, we must keep a reference to the
833 826 namespace of their __main__ module (a FakeModule instance) around so
834 827 that Python doesn't clear it, rendering objects defined therein
@@ -840,32 +833,16 b' class InteractiveShell(SingletonConfigurable):'
840 833 keep one copy of the namespace (the last one), thus preventing memory
841 834 leaks from old references while allowing the objects from the last
842 835 execution to be accessible.
843
844 Note: we can not allow the actual FakeModule instances to be deleted,
845 because of how Python tears down modules (it hard-sets all their
846 references to None without regard for reference counts). This method
847 must therefore make a *copy* of the given namespace, to allow the
848 original module's __dict__ to be cleared and reused.
849
850
851 Parameters
852 ----------
853 ns : a namespace (a dict, typically)
854
855 fname : str
856 Filename associated with the namespace.
857
858 Examples
859 --------
860
861 In [10]: import IPython
862
863 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
864
865 In [12]: IPython.__file__ in _ip._main_ns_cache
866 Out[12]: True
867 836 """
868 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
837 filename = os.path.abspath(filename)
838 try:
839 main_mod = self._main_mod_cache[filename]
840 except KeyError:
841 main_mod = self._main_mod_cache[filename] = FakeModule()
842 else:
843 init_fakemod_dict(main_mod)
844
845 return main_mod
869 846
870 847 def clear_main_mod_cache(self):
871 848 """Clear the cache of main modules.
@@ -877,17 +854,17 b' class InteractiveShell(SingletonConfigurable):'
877 854
878 855 In [15]: import IPython
879 856
880 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
857 In [16]: m = _ip.new_main_mod(IPython.__file__)
881 858
882 In [17]: len(_ip._main_ns_cache) > 0
859 In [17]: len(_ip._main_mod_cache) > 0
883 860 Out[17]: True
884 861
885 862 In [18]: _ip.clear_main_mod_cache()
886 863
887 In [19]: len(_ip._main_ns_cache) == 0
864 In [19]: len(_ip._main_mod_cache) == 0
888 865 Out[19]: True
889 866 """
890 self._main_ns_cache.clear()
867 self._main_mod_cache.clear()
891 868
892 869 #-------------------------------------------------------------------------
893 870 # Things related to debugging
@@ -1017,10 +994,7 b' class InteractiveShell(SingletonConfigurable):'
1017 994 # and clear_main_mod_cache() methods for details on use.
1018 995
1019 996 # This is the cache used for 'main' namespaces
1020 self._main_ns_cache = {}
1021 # And this is the single instance of FakeModule whose __dict__ we keep
1022 # copying and clearing for reuse on each %run
1023 self._user_main_module = FakeModule()
997 self._main_mod_cache = {}
1024 998
1025 999 # A table holding all the namespaces IPython deals with, so that
1026 1000 # introspection facilities can search easily.
@@ -1174,8 +1148,8 b' class InteractiveShell(SingletonConfigurable):'
1174 1148
1175 1149 Note that this does not include the displayhook, which also caches
1176 1150 objects from the output."""
1177 return [self.user_ns, self.user_global_ns,
1178 self._user_main_module.__dict__] + self._main_ns_cache.values()
1151 return [self.user_ns, self.user_global_ns] + \
1152 [m.__dict__ for m in self._main_mod_cache.values()]
1179 1153
1180 1154 def reset(self, new_session=True):
1181 1155 """Clear all internal namespaces, and attempt to release references to
@@ -1219,9 +1193,6 b' class InteractiveShell(SingletonConfigurable):'
1219 1193 # execution protection
1220 1194 self.clear_main_mod_cache()
1221 1195
1222 # Clear out the namespace from the last %run
1223 self.new_main_mod()
1224
1225 1196 def del_var(self, varname, by_name=False):
1226 1197 """Delete a variable from the various namespaces, so that, as
1227 1198 far as possible, we're not keeping any hidden references to it.
@@ -508,7 +508,7 b' python-profiler package from non-free.""")'
508 508 prog_ns = self.shell.user_ns
509 509 __name__save = self.shell.user_ns['__name__']
510 510 prog_ns['__name__'] = '__main__'
511 main_mod = self.shell.new_main_mod(prog_ns)
511 main_mod = self.shell.user_module
512 512 else:
513 513 # Run in a fresh, empty namespace
514 514 if 'n' in opts:
@@ -516,7 +516,10 b' python-profiler package from non-free.""")'
516 516 else:
517 517 name = '__main__'
518 518
519 main_mod = self.shell.new_main_mod()
519 # The shell MUST hold a reference to prog_ns so after %run
520 # exits, the python deletion mechanism doesn't zero it out
521 # (leaving dangling references). See interactiveshell for details
522 main_mod = self.shell.new_main_mod(filename)
520 523 prog_ns = main_mod.__dict__
521 524 prog_ns['__name__'] = name
522 525
@@ -593,10 +596,6 b' python-profiler package from non-free.""")'
593 596 if 'i' in opts:
594 597 self.shell.user_ns['__name__'] = __name__save
595 598 else:
596 # The shell MUST hold a reference to prog_ns so after %run
597 # exits, the python deletion mechanism doesn't zero it out
598 # (leaving dangling references).
599 self.shell.cache_main_mod(prog_ns, filename)
600 599 # update IPython interactive namespace
601 600
602 601 # Some forms of read errors on the file may mean the
General Comments 0
You need to be logged in to leave comments. Login now