Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gf shell is not passing python globals into user defined functions #435

Closed
meeas opened this issue Aug 6, 2024 · 7 comments · Fixed by #437
Closed

gf shell is not passing python globals into user defined functions #435

meeas opened this issue Aug 6, 2024 · 7 comments · Fixed by #437
Labels

Comments

@meeas
Copy link

meeas commented Aug 6, 2024

This appears only to be an issue in 2024.0.0 and 2024.0.1.

For instance, open gf shell and paste this code:

import time
def read(x):
    time.sleep(x)
    print("Done")
read(2)

This fails with a NameError: name 'time' is not defined error.

@mossmann mossmann added the bug label Aug 7, 2024
@mossmann
Copy link
Member

mossmann commented Aug 7, 2024

This appears to have been introduced in #414, though I haven't tried to thoroughly test the previous commit because the bug fixed by #414 complicates that testing.

@mossmann
Copy link
Member

mossmann commented Aug 7, 2024

I was able to test 4d56571 (prior to #414) successfully by downgrading ipython:

pip install "ipython<=8.10"

This bug was definitely introduced in #414.

@mossmann
Copy link
Member

mossmann commented Aug 7, 2024

The origin of this bug is really here: ipython/ipython#13966
Our current problem is with the workaround that was accepted for this ipython bug.

@mossmann
Copy link
Member

mossmann commented Aug 7, 2024

As a workaround, I recommend using ipython directly instead of gf shell:

# ipython
Python 3.11.4 (main, Dec  7 2023, 15:43:41) [GCC 12.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.26.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from greatfet import GreatFET

In [2]: gf=GreatFET()

@mossmann
Copy link
Member

mossmann commented Aug 7, 2024

Another (horrible) workaround that can be done inside gf shell:

In [14]: import time

In [15]: def test():
    ...:     print(time.time())
    ...: 

In [16]: test()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[16], line 1
----> 1 test()

Cell In[15], line 2, in test()
      1 def test():
----> 2     print(time.time())

NameError: name 'time' is not defined

In [17]: globals().update(locals())

In [18]: test()
1722996533.3737051

The truly horrible thing is that you have to call globals().update(locals()) after every time you update globals.

@mossmann
Copy link
Member

mossmann commented Aug 7, 2024

It seems that this is an ancient ipython bug: ipython/ipython#62

We're running into it because ipython/ipython#13966 was not fixed.

@mossmann
Copy link
Member

mossmann commented Aug 7, 2024

I think our solution may be to use start_ipython(). This test script works:

#!/usr/bin/env python3

import IPython
from traitlets.config import Config

c = Config()
c.InteractiveShellApp.exec_lines = [
    'from greatfet import GreatFET',
    'gf=GreatFET()',
]
IPython.start_ipython(config=c)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants