Skip to content

Likely fix for issue #10 #11

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

anthonykozar
Copy link

No description provided.

… in local scopes.

Problem

In both Pyonic 2 & 3, functions that are defined and variables that are assigned 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.

Solution

The Python 3.11 documentation says this about the built-in exec() function: "If only globals is provided, it must be a dictionary (and not a subclass of dictionary), which will be used for both the global and the local variables ... Remember that at the module level, globals and locals are the same dictionary. If exec gets two separate objects as globals and locals, the code will be executed as if it were embedded in a class definition." <https://docs.python.org/3/library/functions.html#exec>

So by passing two different objects as globals and locals to exec(), the Pyonic code was incorrectly asking Python to run it as part of a class definition.  globals() and locals() return the same object in the top-level module.  Therefore, only one dictionary should be passed to exec().
@anthonykozar
Copy link
Author

Problem

In both Pyonic 2 & 3, functions that are defined and variables that are assigned 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.

Solution

The Python 3.11 documentation says this about the built-in exec() function: "If only globals is provided, it must be a dictionary (and not a subclass of dictionary), which will be used for both the global and the local variables ... Remember that at the module level, globals and locals are the same dictionary. If exec gets two separate objects as globals and locals, the code will be executed as if it were embedded in a class definition." https://docs.python.org/3/library/functions.html#exec

So by passing two different objects as globals and locals to exec(), the Pyonic code was incorrectly asking Python to run it as part of a class definition. globals() and locals() return the same object in the top-level module. Therefore, only one dictionary should be passed to exec().

@inclement
Copy link
Owner

Thanks for the PR and I appreciate the effort you've put into diagnosing this, but unfortunately I'm not really maintaining Pyonic Interpreter any more and I don't currently even have the build chain set up. If anyone can confirm the change works I'm happy to merge, but until then I'll leave this PR open.

@anthonykozar
Copy link
Author

Thanks very much for your response!

I'm hoping to get a new computer soon because it is getting harder to set up working build environments for many projects on my 15-year-old Mac. When I do, I will try building the Pyonic interpreter myself and let you know how my patch works.

BTW, do you know of any better ways to use Python interactively on Android? I have a commandline Python installed with Termux but the Pyonic GUI is vastly better for the kind of interactive experimentation that I do. I recently discovered QPython but it appears that it's console mode isn't any better than the commandline. (I have reservations about installing it too). Do you know of any newer apps comparable to Pyonic (that are similarly FOSS and free of any "anti-features")?

Thanks!

@inclement
Copy link
Owner

I'm afraid I'm very out of date on the state of Android interpreters, I'd have hoped there would be good ones but I made Pyonic as an experiment partly because I thought it should clearly be possible to make a much better gui than was otherwise available at the time.

@anthonykozar
Copy link
Author

Thanks.

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

Successfully merging this pull request may close these issues.

2 participants