ipython/ipython — Issue #954: IPython embed doesn't respect namespaces
Author: certik
Created: 2011-10-31 03:09:35+00:00
State: open

In Qsnake, where we embed IPython manually, the local namespace from Qsnake is exposed to IPython, which is a bug. Here is a simple script to reproduce the problem:

```

ondrej@eagle:~$ qsnake --shell
Type CTRL-D to exit the Qsnake shell.
Qsnake: ondrej@eagle:~$ python
Python 2.6.4 (r264:75706, May 7 2011, 22:48:31)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import IPython
>>> c = IPython.config.loader.Config()
>>> c.InteractiveShell.confirm_exit = False
>>> banner = "test"
>>> namespace = {}
>>> IPython.frontend.terminal.embed.InteractiveShellEmbed(config=c,
... user_ns=namespace, banner1=banner).mainloop()
test
In [1]: c
Out[1]: {'InteractiveShell': {'confirm_exit': False}}

In [2]: namespace
Out[2]:
{'IPython': ,
'In': ['', u'c', u'namespace'],
'Out': {1: {'InteractiveShell': {'confirm_exit': False}}},
'_': {'InteractiveShell': {'confirm_exit': False}},
'_1': {'InteractiveShell': {'confirm_exit': False}},
'__': '',
'___': '',
'__builtin__': ,
'__builtins__': ,
'__doc__': None,
'__name__': '__main__',
'__package__': None,
'_dh': [u'/home/ondrej'],
'_i': u'c',
'_i1': u'c',
'_i2': u'namespace',
'_ih': ['', u'c', u'namespace'],
'_ii': u'',
'_iii': u'',
'_oh': {1: {'InteractiveShell': {'confirm_exit': False}}},
'_sh': ,
'banner': 'test',
'c': {'InteractiveShell': {'confirm_exit': False}},
'exit': ,
'get_ipython': >,
'help': Type help() for interactive help, or help(object) for help about object.,
'namespace': {...},
'quit': }

In [3]: banner
Out[3]: 'test'
```

As you can see, the `c`, `banner` and `namespace` variables are declared in Python above, but somehow they get propagated to IPython. What is worse, if you do:

```
In [4]: from sympy import var

In [5]: var("c")
Out[5]: c

In [6]: c
Out[6]: {'InteractiveShell': {'confirm_exit': False}}
```

So the `c` variable can't be used in sympy (that's how I discovered this problem).