You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
The text was updated successfully, but these errors were encountered:
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
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:
Is there anything that can be done to allow the global namespace to be updated automatically? In (unrestricted) Python, function
example()
would have access toc
, but in restricted python one is required to use global. In another example using metaclass, I tried to updateglobals()[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.
The text was updated successfully, but these errors were encountered: