Namespaces for embedding #1140

merged 7 commits into from Dec 12, 2011

3 participants

IPython member

This should fix the embedding problem in #1136.

One additional complexity: if you specify only one of user_ns or user_module when instantiating the embedded shell, they are hooked up so that you have a single namespace (i.e. ip.user_ns is ip.user_global_ns). So, the logic at activation time to auto-detect the module and local namespace will only kick in if neither were specified at instantiation.

You should still be able to pass local_ns or module at activation to override them separately.


Thank you. Both my simple example and the scrapy shell are working well.

@minrk minrk commented on the diff Dec 11, 2011
@@ -170,9 +171,17 @@ def mainloop(self, local_ns=None, module=None, stack_depth=0,
IPython itself (via %run), but some funny things will happen (a few
globals get overwritten). In the future this will be cleaned up, as
there is no fundamental reason why it can't work perfectly."""
+ if (global_ns is not None) and (module is None):
+ class DummyMod(object):
+ """A dummy module object for embedded IPython."""
+ pass
+ warnings.warn("global_ns is deprecated, use module instead.", DeprecationWarning)
+ module = DummyMod()
+ module.__dict__ = global_ns
minrk Dec 11, 2011 IPython member

Thanks, I think this is the main thing we need to preserve.

IPython member

Terrific, thanks for taking care of it. Do we / can we have simple tests for some of the basic cases?

IPython member

I'm not quite sure how to automate tests for embedding, but I can tidy up a couple of test scripts and add them in docs/examples to run manually if that's sufficient.

IPython member

Added some manual test scripts.

IPython member

Nice. Manual tests look good, and I checked embedded shells in currently released sympy and django, both of which appear to work happily (at least they don't raise) with your changes. If you think this is good to go, go ahead and merge.

@takluyver takluyver merged commit feb3b24 into ipython:master Dec 12, 2011
IPython member

Thanks, merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment