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

context manager methods (__enter__ and __exit__) raise a SyntaxError #230

Open
cloudgerman opened this issue Aug 19, 2022 · 1 comment
Open

Comments

@cloudgerman
Copy link

cloudgerman commented Aug 19, 2022

BUG/PROBLEM REPORT (OR OTHER COMMON ISSUE)

If this ticket is accepted as a bug or feature request, I volunteer to work on it (once contributor agreement is approved).

What I did:

I tried to create a context manager in restricted python, here is the code:

from RestrictedPython import compile_restricted
source_code = """
class example:
    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, exc_traceback):
        pass
"""
compile_restricted(source_code, '<inline>', 'exec')

What I expect to happen:

I expected to have a context manager that I could use in a with statement

What actually happened:

I get the following error:

SyntaxError: ('Line 3: "__enter__" is an invalid variable name because it starts with "_"', 'Line 6: "__exit__" is an invalid variable name because it starts with "_"')

These methods (__enter__ and __exit__) are missing from transformer.ALLOWED_FUNC_NAMES

What version of Python and Zope/Addons I am using:

Python 3.8.2
RestrictedPython 5.2
macOS Monterey 12.5.1

@icemac
Copy link
Member

icemac commented Aug 24, 2022

Currently you are not able to define a context manager inside restricted Python because the needed method names start with _.
But I see the value in being able to do so.

A pull request is welcome adding __enter__ and __exit__ to ALLOWED_FUNC_NAMES including a test showing that context managers can be defined and used afterwards in restricted Python.

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

No branches or pull requests

2 participants