Skip to content

Commit

Permalink
Allow list comprehensions in RestrictionCapableEval.eval(). (#97)
Browse files Browse the repository at this point in the history
Fixes #95.
  • Loading branch information
Michael Howitz committed Jan 26, 2018
1 parent a63f5c3 commit 4a16de1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
5 changes: 4 additions & 1 deletion docs/CHANGES.rst
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
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
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]

0 comments on commit 4a16de1

Please sign in to comment.