##// END OF EJS Templates
Don't override passed user_ns in embed()
Thomas Kluyver -
Show More
@@ -881,6 +881,8 b' class InteractiveShell(SingletonConfigurable, Magic):'
881 #-------------------------------------------------------------------------
881 #-------------------------------------------------------------------------
882 # Things related to IPython's various namespaces
882 # Things related to IPython's various namespaces
883 #-------------------------------------------------------------------------
883 #-------------------------------------------------------------------------
884 default_user_ns = True
885 default_user_module = True
884
886
885 def init_create_namespaces(self, user_module=None, user_ns=None):
887 def init_create_namespaces(self, user_module=None, user_ns=None):
886 # Create the namespace where the user will operate. user_ns is
888 # Create the namespace where the user will operate. user_ns is
@@ -919,6 +921,10 b' class InteractiveShell(SingletonConfigurable, Magic):'
919 # These routines return a properly built module and dict as needed by
921 # These routines return a properly built module and dict as needed by
920 # the rest of the code, and can also be used by extension writers to
922 # the rest of the code, and can also be used by extension writers to
921 # generate properly initialized namespaces.
923 # generate properly initialized namespaces.
924 if user_ns is not None:
925 self.default_user_ns = False
926 if user_module is not None:
927 self.default_user_module = False
922 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
928 self.user_module, self.user_ns = self.prepare_user_module(user_module, user_ns)
923
929
924 # A record of hidden variables we have added to the user namespace, so
930 # A record of hidden variables we have added to the user namespace, so
@@ -175,9 +175,9 b' class InteractiveShellEmbed(TerminalInteractiveShell):'
175 if local_ns is None or module is None:
175 if local_ns is None or module is None:
176 call_frame = sys._getframe(stack_depth).f_back
176 call_frame = sys._getframe(stack_depth).f_back
177
177
178 if local_ns is None:
178 if local_ns is None and not self.default_user_ns:
179 local_ns = call_frame.f_locals
179 local_ns = call_frame.f_locals
180 if module is None:
180 if module is None and not self.default_user_module:
181 global_ns = call_frame.f_globals
181 global_ns = call_frame.f_globals
182 module = sys.modules[global_ns['__name__']]
182 module = sys.modules[global_ns['__name__']]
183
183
@@ -189,21 +189,26 b' class InteractiveShellEmbed(TerminalInteractiveShell):'
189 # Update namespaces and fire up interpreter
189 # Update namespaces and fire up interpreter
190
190
191 # The global one is easy, we can just throw it in
191 # The global one is easy, we can just throw it in
192 self.user_module = module
192 if module is not None:
193 self.user_module = module
193
194
194 # But the user/local one is tricky: ipython needs it to store internal
195 # But the user/local one is tricky: ipython needs it to store internal
195 # data, but we also need the locals. We'll throw our hidden variables
196 # data, but we also need the locals. We'll throw our hidden variables
196 # like _ih and get_ipython() into the local namespace, but delete them
197 # like _ih and get_ipython() into the local namespace, but delete them
197 # later.
198 # later.
198 self.user_ns = local_ns
199 if local_ns is not None:
199 self.init_user_ns()
200 self.user_ns = local_ns
201 self.init_user_ns()
200
202
201 # Patch for global embedding to make sure that things don't overwrite
203 # Patch for global embedding to make sure that things don't overwrite
202 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
204 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
203 # FIXME. Test this a bit more carefully (the if.. is new)
205 # FIXME. Test this a bit more carefully (the if.. is new)
204 # N.B. This can't now ever be called. Not sure what it was for.
206 # N.B. This can't now ever be called. Not sure what it was for.
205 if local_ns is None and module is None:
207 # And now, since it wasn't called in the previous version, I'm
206 self.user_global_ns.update(__main__.__dict__)
208 # commenting out these lines so they can't be called with my new changes
209 # --TK, 2011-12-10
210 #if local_ns is None and module is None:
211 # self.user_global_ns.update(__main__.__dict__)
207
212
208 # make sure the tab-completer has the correct frame information, so it
213 # make sure the tab-completer has the correct frame information, so it
209 # actually completes using the frame's locals/globals
214 # actually completes using the frame's locals/globals
@@ -213,8 +218,9 b' class InteractiveShellEmbed(TerminalInteractiveShell):'
213 self.interact(display_banner=display_banner)
218 self.interact(display_banner=display_banner)
214
219
215 # now, purge out the local namespace of IPython's hidden variables.
220 # now, purge out the local namespace of IPython's hidden variables.
216 for name in self.user_ns_hidden:
221 if local_ns is not None:
217 local_ns.pop(name, None)
222 for name in self.user_ns_hidden:
223 local_ns.pop(name, None)
218
224
219 # Restore original namespace so shell can shut down when we exit.
225 # Restore original namespace so shell can shut down when we exit.
220 self.user_module = orig_user_module
226 self.user_module = orig_user_module
General Comments 0
You need to be logged in to leave comments. Login now