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
5 changes: 4 additions & 1 deletion docs/CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Changes
- Warn when using another Python implementation than CPython as it is not safe to use RestrictedPython with other versions than CPyton.
See https://bitbucket.org/pypy/pypy/issues/2653 for PyPy.

- Allow to use list comprehensions in the default implementation of
``RestrictionCapableEval.eval()``.

4.0b2 (2017-09-15)
------------------

Expand Down Expand Up @@ -62,7 +65,7 @@ Changes

- Mostly complete rewrite based on Python AST module.
[loechel (Alexander Loechel), icemac (Michael Howitz), stephan-hof (Stephan Hofmockel), tlotze (Thomas Lotze)]

- Support Python versions 3.4 up to 3.6.

- switch to pytest
Expand Down
8 changes: 7 additions & 1 deletion src/RestrictedPython/Eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def default_guarded_getitem(ob, index):
return ob[index]


def default_guarded_getiter(ob):
# No restrictions.
return ob


class RestrictionCapableEval(object):
"""A base class for restricted code."""

Expand Down Expand Up @@ -99,7 +104,8 @@ def eval(self, mapping):

global_scope = {
'_getattr_': default_guarded_getattr,
'_getitem_': default_guarded_getitem
'_getitem_': default_guarded_getitem,
'_getiter_': default_guarded_getiter,
}

global_scope.update(self.globals)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ def test_Eval__RestictionCapableEval__eval_1():
ob.globals['c'] = 8
result = ob.eval(dict(a=1, b=2, c=4))
assert result == 11


def test_Eval__RestictionCapableEval__eval__2():
"""It allows to use list comprehensions."""
ob = RestrictionCapableEval("[item for item in (1, 2)]")
result = ob.eval({})
assert result == [1, 2]