Skip to content

Commit

Permalink
Use and document safe_globals.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed Oct 26, 2018
1 parent 6c33409 commit 514456a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.rst
Expand Up @@ -47,14 +47,14 @@ This example directly executed in Python could harm your system.
.. code-block:: pycon
>>> from RestrictedPython import compile_restricted
>>> from RestrictedPython import safe_builtins
>>> from RestrictedPython import safe_globals
>>>
>>> source_code = """
... import os
...
... os.listdir('/')
... """
>>> byte_code = compile_restricted(source_code, '<inline>', 'exec')
>>> exec(byte_code, {'__builtins__': safe_builtins}, {})
>>> exec(byte_code, safe_globals, {})
Traceback (most recent call last):
ImportError: __import__ not found
1 change: 1 addition & 0 deletions docs/upgrade_dependencies/index.rst
Expand Up @@ -28,6 +28,7 @@ RestrictedPython did move some of the imports to the base namespace, so you shou

* predefined built-ins:

* ``from RestrictedPython import safe_globals``
* ``from RestrictedPython import safe_builtins``
* ``from RestrictedPython import limited_builtins``
* ``from RestrictedPython import utility_builtins``
Expand Down
1 change: 1 addition & 0 deletions docs/usage/api.rst
Expand Up @@ -130,6 +130,7 @@ RestrictedPython has tree major scopes:

2. restricted builtins

* ``safe_globals``
* ``safe_builtins``
* ``limited_builtins``
* ``utility_builtins``
Expand Down
5 changes: 4 additions & 1 deletion docs/usage/basic_usage.rst
Expand Up @@ -86,12 +86,15 @@ So you normally end up using:
filename='<inline code>',
mode='exec'
)
exec(byte_code, safe_builtins, None)
exec(byte_code, {'__builtins__': safe_builtins}, None)
except SyntaxError as e:
pass

One common advanced usage would be to define an own restricted builtin dictionary.

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

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

Expand Down

0 comments on commit 514456a

Please sign in to comment.