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

Top-level functions and variables not visible in local scopes #10

Open
anthonykozar opened this issue Dec 6, 2022 · 2 comments
Open

Comments

@anthonykozar
Copy link

This is a problem that has been hindering my use of Pyonic interpreter for several years. I'm kind of surprised that it is not listed as a known issue or hasn't been reported yet.

Basically, in both Pyonic 2 & 3, functions that I define and variables that I assign at the top-level of the interpreter are not visible as globals within other functions (or even themselves -- no recursion). A simple example:

def foo():
  print("foo")

def bar():
  foo()
  print("bar")

bar()

The call to foo() from bar() raises NameError with the message "global name 'foo' is not defined" in Python 2.7 and "name 'foo' is not defined" in Python 3.6.

dir() shows both foo and bar but globals() does not. I've been working around the problem by passing in functions as default parameters to functions but this has some limitations. (For one thing, recursion doesn't work properly).

I was hoping to try fixing this myself but I have no experience with Kivy or Python for Android and unfortunately don't have time to try to set up those tools.

Thanks!

Anthony Kozar

@anthonykozar
Copy link
Author

Here are some screenshots with more information.
Screenshot_20221206-145946_Pyonic Python 3 interpreter
Screenshot_20221206-150121_Pyonic Python 2 interpreter

@anthonykozar
Copy link
Author

I think I have a solution to this issue now. The solution is to pass only one dictionary to exec() for both the globals and locals at the top-level. It works when I try simulating what is happening in pyonic/interpreter_subprocess/interpreter.py from a Python command line interpreter. So, something like

import copy
loc = copy.copy(locals())
glb = copy.copy(globals())
exec('def cube(x): return x**3', glb, loc)
exec('def pc(): print cube(9)', glb, loc)
exec('pc()', glb, loc)

produces the same error as the Pyonic interpreter:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
  File "<string>", line 1, in pc
NameError: global name 'cube' is not defined

while this produces the correct output.

import copy
glb = copy.copy(globals())
exec('def cube(x): return x**3', glb)
exec('def pc(): print cube(9)', glb)
exec('pc()', glb, loc)

I haven't had the time to try to rebuild the Pyonic app with this fix but I'm reasonably confident that it will work. I can run my patched version of pyonic/interpreter_subprocess/interpreter.py from a command line and it prints out

python service starting!
service got this far
got this far
got this far4

I suspect that my computer and OS may be too old to build current versions of Kivy and/or p4a, so I expect to have to do a lot of experimention to rebuild. My attempts to simply modify the APK file haven't succeeded yet either.

So, I've created a pull request with this fix and a separate commit that removes the need to import the copy module in the hope that a new official build can be produced. I realize that the project hasn't been updated in quite a while. :( More details about the solution are in the git commit message.

THANKS!

Anthony
#11

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

No branches or pull requests

1 participant