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

Global scope isn't updated when new function (or class) is defined #187

Closed
ssche opened this issue Dec 18, 2019 · 3 comments
Closed

Global scope isn't updated when new function (or class) is defined #187

ssche opened this issue Dec 18, 2019 · 3 comments

Comments

@ssche
Copy link

ssche commented Dec 18, 2019

Is there anything that can be done to allow the global namespace to be updated automatically? In (unrestricted) Python, function example() would have access to c, but in restricted python one is required to use global. In another example using metaclass, I tried to update globals()[name] = ob to allow classes to be available, but this didn't work (so I assume the same problem would apply for new functions).

Any idea would be appreciated.

from RestrictedPython import compile_restricted
from RestrictedPython import safe_globals

source_code = """

def c():
    return 1

def example():
    # required:
    # global c
    return c()

example()
"""

loc = {}
byte_code = compile_restricted(source_code, '<inline>', 'exec')
exec(byte_code, safe_globals, loc)

print (loc)
@icemac icemac added the bug label Feb 14, 2020
@icemac icemac added this to To do in Zope 5.0 via automation Feb 14, 2020
@icemac icemac added this to To do in Zope 4 bugfix via automation Feb 14, 2020
@kevinyang372
Copy link

Is there any update on this issue? I am also getting stuck on using restrictedpython to compile recursive functions as they are not added to the globals

@d-maurer
Copy link
Contributor

d-maurer commented May 29, 2020

Is there any update on this issue? I am also getting stuck on using restrictedpython to compile recursive functions as they are not added to the globals

Unlike @icemac, I do not think this is a bug. In my view, you get what you called for.
You call for the execution of your source (defining c and example) with globals safe_globals and locals loc. The definitions are put into loc which is not the __globals__ of function example; as a consequence, example cannot access c.

If you want to use loc as "globals", you could achieve this via:

loc = safe_globals.copy()
exec(byte_code, loc)

@kevinyang372
Copy link

@d-maurer Thank you! That does answer my question.

Zope 5.0 automation moved this from To do to Done May 30, 2020
Zope 4 bugfix automation moved this from To do to Done May 30, 2020
@icemac icemac removed the bug label Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Zope 4 bugfix
  
Done
Zope 5.0
  
Done
Development

No branches or pull requests

4 participants