From 65a27e6382574796308b4201796fd0d011030161 Mon Sep 17 00:00:00 2001 From: Michael Howitz Date: Fri, 3 Feb 2017 12:19:43 +0100 Subject: [PATCH] Port AugAssign tests. --- .../tests/security_in_syntax.py | 24 ------------ tests/__init__.py | 13 ++++--- tests/test_transformer.py | 38 +++++++++++++++---- 3 files changed, 38 insertions(+), 37 deletions(-) delete mode 100644 src/RestrictedPython/tests/security_in_syntax.py diff --git a/src/RestrictedPython/tests/security_in_syntax.py b/src/RestrictedPython/tests/security_in_syntax.py deleted file mode 100644 index 08bb41a..0000000 --- a/src/RestrictedPython/tests/security_in_syntax.py +++ /dev/null @@ -1,24 +0,0 @@ -# These are all supposed to raise a SyntaxError when using -# compile_restricted() but not when using compile(). -# Each function in this module is compiled using compile_restricted(). - - -def keyword_arg_with_bad_name(): - def f(okname=1, __badname=2): - pass - - -def no_augmeneted_assignment_to_sub(): - a[b] += c - - -def no_augmeneted_assignment_to_attr(): - a.b += c - - -def no_augmeneted_assignment_to_slice(): - a[x:y] += c - - -def no_augmeneted_assignment_to_slice2(): - a[x:y:z] += c diff --git a/tests/__init__.py b/tests/__init__.py index 51a994b..9e787b4 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -5,12 +5,13 @@ def _execute(compile_func): """Factory to create an execute function.""" - def _execute(source): - code, errors = compile_func(source)[:2] - assert errors == (), errors - assert code is not None - glb = {} - exec(code, glb) + def _execute(source, glb=None): + result = compile_func(source) + assert result.errors == (), result.errors + assert result.code is not None + if glb is None: + glb = {} + exec(result.code, glb) return glb return _execute diff --git a/tests/test_transformer.py b/tests/test_transformer.py index 334fa42..9bd314d 100644 --- a/tests/test_transformer.py +++ b/tests/test_transformer.py @@ -639,9 +639,10 @@ def test_transformer__RestrictingNodeTransformer__visit_Subscript_2( _write_.reset_mock() -@pytest.mark.parametrize(*compile) -def test_transformer__RestrictingNodeTransformer__visit_AugAssign( - compile, mocker): +@pytest.mark.parametrize(*execute) +def test_transformer__RestrictingNodeTransformer__visit_AugAssign__1( + execute, mocker): + """It allows augmented assign for variables.""" _inplacevar_ = mocker.stub() _inplacevar_.side_effect = lambda op, val, expr: val + expr @@ -652,24 +653,47 @@ def test_transformer__RestrictingNodeTransformer__visit_AugAssign( 'z': 0 } - result = compile("a += x + z") - assert result.errors == () - exec(result.code, glb) - + execute("a += x + z", glb) assert glb['a'] == 2 _inplacevar_.assert_called_once_with('+=', 1, 1) _inplacevar_.reset_mock() + +@pytest.mark.parametrize(*compile) +def test_transformer__RestrictingNodeTransformer__visit_AugAssign__2(compile): + """It forbids augmented assign of attributes.""" result = compile("a.a += 1") assert result.errors == ( 'Line 1: Augmented assignment of attributes is not allowed.',) + +@pytest.mark.parametrize(*compile) +def test_transformer__RestrictingNodeTransformer__visit_AugAssign__3(compile): + """It forbids augmented assign of subscripts.""" result = compile("a[a] += 1") assert result.errors == ( 'Line 1: Augmented assignment of object items and slices is not ' 'allowed.',) +@pytest.mark.parametrize(*compile) +def test_transformer__RestrictingNodeTransformer__visit_AugAssign__4(compile): + """It forbids augmented assign of slices.""" + result = compile("a[x:y] += 1") + assert result.errors == ( + 'Line 1: Augmented assignment of object items and slices is not ' + 'allowed.',) + + +@pytest.mark.parametrize(*compile) +def test_transformer__RestrictingNodeTransformer__visit_AugAssign__5(compile): + """It forbids augmented assign of slices with steps.""" + result = compile("a[x:y:z] += 1") + assert result.errors == ( + 'Line 1: Augmented assignment of object items and slices is not ' + 'allowed.',) + + # def f(a, b, c): pass # f(*two_element_sequence, **dict_with_key_c) #