TurboGears / tg2devtools

TurboGears 2.x DevTools repository
http://www.turbogears.org/
16 stars 20 forks source link

Start IPython manually instead of embedding (fixes variable scoping issues with gearbox tgshell IPython sessions) #22

Closed Jackevansevo closed 1 year ago

Jackevansevo commented 2 years ago

Prevents a category of scoping issues which happen as a result of embedding IPython https://github.com/ipython/ipython/issues/12199

The following example would previously break in gearbox tgshell when running with embedded IPython.

This issue stems from the ipython library. The behaviour is reproducible outside of tgshell by embedding ipython in a regular REPL session.

>>> import IPython
>>> IPython.embed()
Python 3.9.9 (main, Mar  4 2022, 20:56:50)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: x = 10

In [2]: def foo(): return x

In [3]: foo()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 foo()

Input In [2], in foo()
----> 1 def foo(): return x

NameError: name 'x' is not defined

In [4]:

In other frameworks

The flask package flask-shell-ipython gets around this limitation by calling IPython. start_ipython instead of attempting to embed the interpreter. Take a look and their implementation

Django does the same thing when you run ./manage.py shell see here

This PR attempts to add this behaviour to gearbox tgshell

I've naively assumed that the gearbox tgshell command doesn't depend the ipython session being embedded.