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

handling of compile_restricted optional params flags and dont_inherit #114

Open
loechel opened this issue May 18, 2018 · 4 comments
Open

Comments

@loechel
Copy link
Member

loechel commented May 18, 2018

@stephan-hof did bring up the point that the current handling of flags and dont_inherit did not work as expected in #39 (comment)

Currently this inheritance is not compatible.
Let's assume the following code in module called test.py

from __future__ import print_function
from RestrictedPython import compile_restricted as compile
compile(<source>, filename="<inline>", mode="exec", ...) 

Even if dont_inhert=False the future is not recognized in the compile_restricted function.
When compile_restricted is called it uses the futures from compile.py not from test.py
See: https://github.com/python/cpython/blob/2.7/Python/ceval.c#L4159

If this inheriting should be compatible with the original compile function something like this needs to be done.

parent_flags = sys._getframe(1).f_code.co_flags
parent_flags = parent_flags & PyCF_MASK
flags = flags | parent_flags

and then call compile with flags=flags and dont_inhert=True to avoid using future flags from compile.py.

Right now the users of RestrictedPython don't need this feature of future inheriting so I would leave it out. Typically the comes from a different component anyway so inheriting the flags where restricted_compile is called could lead to surprising effects.

@loechel
Copy link
Member Author

loechel commented May 18, 2018

supersede #96

Currently _compile_restricted_mode takes flags and dont_inherit as arguments but it does not pass them to compile (code has comment signs in front of) in case there is a policy.

Either it should not have arguments or it should handle them properly.

by @icemac

@ebagdasa
Copy link

ebagdasa commented Sep 23, 2019

is it related to this code:

byte_code = compile(c_ast, filename, mode=mode # ,
# flags=flags,
# dont_inherit=dont_inherit
)

Why were these lines commented?

@loechel
Copy link
Member Author

loechel commented Sep 24, 2019

@ebagdasa as fas as I remember they are commented out, as we got problems / unpredicted results if we pass the current defaults.

We should look more into that when we have time.

@perrinjerome
Copy link
Contributor

For reference, to get __future__.print_function working, I did perrinjerome@4c132f6 . The missing piece of the puzzle seems to be that the ast should not be retrieved with ast.parse but with compile and ast.PyCF_ONLY_AST flags - at least for print_function because this is a future that makes a difference at parsing time, not compile time.

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

3 participants