Skip to content
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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changes
8.2 (unreleased)
----------------

- Remove documentation that appears to promote unsupported direct guards usage.

- Move package metadata from setup.py to pyproject.toml.

- Drop support for Python 3.9.
Expand Down
68 changes: 0 additions & 68 deletions docs/usage/policy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,71 +167,3 @@ unsafe operations, such as opening files:
Traceback (most recent call last):
...
NameError: name 'open' is not defined

Guards
......

Here's an example of a write guard that never lets restricted code
modify (assign, delete an attribute or item) except dictionaries and
lists:

.. code-block:: pycon

>>> from RestrictedPython.Guards import full_write_guard
>>> _write_ = full_write_guard
>>> _getattr_ = getattr

>>> class BikeShed(object):
... colour = 'green'
...
>>> shed = BikeShed()

Normally accessing attributes works as expected, because we're using
the standard ``getattr`` function for the ``_getattr_`` guard:

.. code-block:: pycon

>>> src = '''
... print(shed.colour)
... result = printed
... '''
>>> code = compile_restricted(src, '<string>', 'exec')
>>> exec(code)

>>> result
'green\n'

However, changing an attribute doesn't work:

.. code-block:: pycon

>>> src = '''
... shed.colour = 'red'
... '''
>>> code = compile_restricted(src, '<string>', 'exec')
>>> exec(code)
Traceback (most recent call last):
...
TypeError: attribute-less object (assign or del)

As said, this particular write guard (``full_write_guard``) will allow
restricted code to modify lists and dictionaries:

.. code-block:: pycon

>>> fibonacci = [1, 1, 2, 3, 4]
>>> transl = dict(one=1, two=2, tres=3)
>>> src = '''
... # correct mistake in list
... fibonacci[-1] = 5
... # one item doesn't belong
... del transl['tres']
... '''
>>> code = compile_restricted(src, '<string>', 'exec')
>>> exec(code)

>>> fibonacci
[1, 1, 2, 3, 5]

>>> sorted(transl.keys())
['one', 'two']
Loading