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

enable single mode #272

Merged
merged 6 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changes

- Allow to use the package with Python 3.13 -- Caution: No security
audit has been done so far.
- Add support for single mode statements / execution.


7.1 (2024-03-14)
Expand Down
56 changes: 56 additions & 0 deletions docs/usage/basic_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,62 @@ One common advanced usage would be to define an own restricted builtin dictionar

There is a shortcut for ``{'__builtins__': safe_builtins}`` named ``safe_globals`` which can be imported from ``RestrictedPython``.

Other Usages
------------

RestrictedPython has similar to normal Python multiple modes:

* exec
* eval
* single
* function

you can use it by:

.. testcode::

from RestrictedPython import compile_restricted

source_code = """
def do_something():
pass
"""

byte_code = compile_restricted(
source_code,
filename='<inline code>',
mode='exec'
)
exec(byte_code)
do_something()

.. testcode::

from RestrictedPython import compile_restricted

byte_code = compile_restricted(
"2 + 2",
filename='<inline code>',
mode='eval'
)
eval(byte_code)


.. testcode:: single

from RestrictedPython import compile_restricted

byte_code = compile_restricted(
"2 + 2",
filename='<inline code>',
mode='single'
)
exec(byte_code)

.. testoutput:: single

4

Necessary setup
---------------

Expand Down
4 changes: 4 additions & 0 deletions src/RestrictedPython/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,10 @@ def visit_NameConstant(self, node):
"""
return self.node_contents_visit(node)

def visit_Interactive(self, node):
"""Allow single mode without restrictions."""
return self.node_contents_visit(node)

def visit_List(self, node):
"""Allow list literals without restrictions."""
return self.node_contents_visit(node)
Expand Down
23 changes: 17 additions & 6 deletions tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,24 @@ def test_compile__compile_restricted_eval__used_names():
assert result.used_names == {'a': True, 'b': True, 'x': True, 'func': True}


def test_compile__compile_restricted_csingle():
def test_compile__compile_restricted_single__1():
"""It compiles code as an Interactive."""
result = compile_restricted_single('4 * 6')
assert result.code is None
assert result.errors == (
'Line None: Interactive statements are not allowed.',
)
result = compile_restricted_single('x = 4 * 6')

assert result.errors == ()
assert result.warnings == []
assert result.code is not None
locals = {}
exec(result.code, {}, locals)
assert locals["x"] == 24


def test_compile__compile_restricted__2():
"""It compiles code as an Interactive."""
code = compile_restricted('x = 4 * 6', filename="<string>", mode="single")
locals = {}
exec(code, {}, locals)
assert locals["x"] == 24


PRINT_EXAMPLE = """
Expand Down
Loading