Skip to content

Commit

Permalink
Remove code which was necessary to distinguish between the implementa…
Browse files Browse the repository at this point in the history
…tions.
  • Loading branch information
Michael Howitz committed Sep 15, 2017
1 parent 4e89925 commit 1ee4bce
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 92 deletions.
7 changes: 3 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ def _exec(source, glb=None):
glb = {}
exec(code, glb)
return glb
# The next line can be dropped after the old implementation was dropped.
_exec.compile_func = compile_func
return _exec


Expand Down Expand Up @@ -54,8 +52,9 @@ def _function(source, glb=None):
return _function


# Define the arguments for @pytest.mark.parametrize to be able to test both the
# old and the new implementation to be equal:
# Define the arguments for @pytest.mark.parametrize. This was used to be able
# to test both the old and the new implementation are equal. It can be
# refactored into fixtures.
# Compile in `exec` mode.
c_exec = ('c_exec', [RestrictedPython.compile.compile_restricted_exec])
# Compile and execute in `exec` mode.
Expand Down
51 changes: 16 additions & 35 deletions tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from tests import e_eval

import pytest
import RestrictedPython.compile
import types


Expand Down Expand Up @@ -47,15 +46,13 @@ def test_compile__compile_restricted_exec__1(c_exec):
@pytest.mark.parametrize(*c_exec)
def test_compile__compile_restricted_exec__2(c_exec):
"""It compiles without restrictions if there is no policy."""
if c_exec is RestrictedPython.compile.compile_restricted_exec:
# The old version does not support a custom policy
result = c_exec('_a = 42', policy=None)
assert result.errors == ()
assert result.warnings == []
assert result.used_names == {}
glob = {}
exec(result.code, glob)
assert glob['_a'] == 42
result = c_exec('_a = 42', policy=None)
assert result.errors == ()
assert result.warnings == []
assert result.used_names == {}
glob = {}
exec(result.code, glob)
assert glob['_a'] == 42


@pytest.mark.parametrize(*c_exec)
Expand All @@ -68,11 +65,7 @@ def test_compile__compile_restricted_exec__3(c_exec):
errors = (
'Line 1: "_a" is an invalid variable name because it starts with "_"',
'Line 2: "_b" is an invalid variable name because it starts with "_"')
if c_exec is RestrictedPython.compile.compile_restricted_exec:
assert result.errors == errors
else:
# The old version did only return the first error message.
assert result.errors == (errors[0],)
assert result.errors == errors
assert result.warnings == []
assert result.used_names == {}
assert result.code is None
Expand All @@ -85,12 +78,8 @@ def test_compile__compile_restricted_exec__4(c_exec):
assert result.code is None
assert result.warnings == []
assert result.used_names == {}
if c_exec is RestrictedPython.compile.compile_restricted_exec:
assert result.errors == (
'Line 1: SyntaxError: invalid syntax in on statement: asdf|',)
else:
# The old version had a less nice error message:
assert result.errors == ('invalid syntax (<string>, line 1)',)
assert result.errors == (
'Line 1: SyntaxError: invalid syntax in on statement: asdf|',)


@pytest.mark.parametrize(*c_exec)
Expand Down Expand Up @@ -139,11 +128,8 @@ def test_compile__compile_restricted_eval__1(c_eval):
Function definitions are not allowed in Expressions.
"""
result = c_eval(FUNCTION_DEF)
if c_eval is RestrictedPython.compile.compile_restricted_eval:
assert result.errors == (
'Line 1: SyntaxError: invalid syntax in on statement: def a():',)
else:
assert result.errors == ('invalid syntax (<string>, line 1)',)
assert result.errors == (
'Line 1: SyntaxError: invalid syntax in on statement: def a():',)


@pytest.mark.parametrize(*e_eval)
Expand All @@ -164,15 +150,10 @@ def test_compile__compile_restricted_eval__used_names(c_eval):
def test_compile__compile_restricted_csingle(c_single):
"""It compiles code as an Interactive."""
result = c_single('4 * 6')
if c_single is RestrictedPython.compile.compile_restricted_single:
# New implementation disallows single mode
assert result.code is None
assert result.errors == (
'Line None: Interactive statements are not allowed.',
)
else: # RestrictedPython.RCompile.compile_restricted_single
assert result.code is not None
assert result.errors == ()
assert result.code is None
assert result.errors == (
'Line None: Interactive statements are not allowed.',
)


PRINT_EXAMPLE = """
Expand Down
1 change: 0 additions & 1 deletion tests/test_print_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import RestrictedPython


# The old 'RCompile' has no clue about the print function.
compiler = RestrictedPython.compile.compile_restricted_exec


Expand Down
5 changes: 1 addition & 4 deletions tests/transformer/test_classdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from tests import e_exec

import pytest
import RestrictedPython


GOOD_CLASS = '''
Expand Down Expand Up @@ -108,6 +107,4 @@ def test_RestrictingNodeTransformer__visit_ClassDef__5(e_exec):
comb = restricted_globals['comb']
assert comb.class_att == 2342
assert comb.base_att == 42
if e_exec.compile_func is RestrictedPython.compile.compile_restricted_exec:
# Class decorators are only supported by the new implementation.
assert comb.wrap_att == 23
assert comb.wrap_att == 23
7 changes: 1 addition & 6 deletions tests/transformer/test_eval_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from tests import c_exec

import pytest
import RestrictedPython


EXEC_STATEMENT = """\
Expand Down Expand Up @@ -46,8 +45,4 @@ def no_eval():
def test_RestrictingNodeTransformer__visit_Eval__1(c_exec):
"""It is an error if the code call the `eval` function."""
result = c_exec(EVAL_FUNCTION)
if c_exec is RestrictedPython.compile.compile_restricted_exec:
assert result.errors == ("Line 2: Eval calls are not allowed.",)
else:
# `eval()` is allowed in the old implementation. :-(
assert result.errors == ()
assert result.errors == ("Line 2: Eval calls are not allowed.",)
19 changes: 6 additions & 13 deletions tests/transformer/test_functiondef.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from tests import e_exec

import pytest
import RestrictedPython


functiondef_err_msg = 'Line 1: "_bad" is an invalid variable ' \
Expand Down Expand Up @@ -71,13 +70,11 @@ def test_RestrictingNodeTransformer__visit_FunctionDef__5(
def test_RestrictingNodeTransformer__visit_FunctionDef__6(
c_exec):
"""It prevents function arguments starting with `_` in tuples."""
# The old `compile` breaks with tuples in function arguments:
if c_exec is RestrictedPython.compile.compile_restricted_exec:
result = c_exec("def foo(a, (c, (_bad, c))): pass")
# RestrictedPython.compile.compile_restricted_exec on Python 2 renders
# the error message twice. This is necessary as otherwise *_bad and
# **_bad would be allowed.
assert functiondef_err_msg in result.errors
result = c_exec("def foo(a, (c, (_bad, c))): pass")
# RestrictedPython.compile.compile_restricted_exec on Python 2 renders
# the error message twice. This is necessary as otherwise *_bad and
# **_bad would be allowed.
assert functiondef_err_msg in result.errors


@pytest.mark.skipif(
Expand Down Expand Up @@ -122,11 +119,7 @@ def test_RestrictingNodeTransformer__visit_FunctionDef__8(
_getiter_.assert_called_once_with(val)
_getiter_.reset_mock()

try:
e_exec(NESTED_SEQ_UNPACK, glb)
except AttributeError:
# The old RCompile did not support nested.
return
e_exec(NESTED_SEQ_UNPACK, glb)

val = (1, 2, (3, (4, 5)))
ret = glb['nested'](val)
Expand Down
11 changes: 4 additions & 7 deletions tests/transformer/test_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from tests import e_exec

import pytest
import RestrictedPython
import types


Expand Down Expand Up @@ -163,9 +162,7 @@ def test_RestrictingNodeTransformer__guard_iter__2(e_exec, mocker):
_getiter_.assert_has_calls(call_ref)
_getiter_.reset_mock()

# The old code did not run with unpack sequence inside generators
if compile == RestrictedPython.compile.compile_restricted_exec:
ret = list(glb['generator'](it))
assert ret == [3, 7, 11]
_getiter_.assert_has_calls(call_ref)
_getiter_.reset_mock()
ret = list(glb['generator'](it))
assert ret == [3, 7, 11]
_getiter_.assert_has_calls(call_ref)
_getiter_.reset_mock()
34 changes: 12 additions & 22 deletions tests/transformer/test_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from tests import e_exec

import pytest
import RestrictedPython


lambda_err_msg = 'Line 1: "_bad" is an invalid variable ' \
Expand Down Expand Up @@ -52,13 +51,11 @@ def test_RestrictingNodeTransformer__visit_Lambda__4(c_exec):
@pytest.mark.parametrize(*c_exec)
def test_RestrictingNodeTransformer__visit_Lambda__5(c_exec):
"""It prevents arguments starting with `_` in tuple unpacking."""
# The old `compile` breaks with tuples in arguments:
if c_exec is RestrictedPython.compile.compile_restricted_exec:
result = c_exec("lambda (a, _bad): None")
# RestrictedPython.compile.compile_restricted_exec on Python 2 renders
# the error message twice. This is necessary as otherwise *_bad and
# **_bad would be allowed.
assert lambda_err_msg in result.errors
result = c_exec("lambda (a, _bad): None")
# RestrictedPython.compile.compile_restricted_exec on Python 2 renders
# the error message twice. This is necessary as otherwise *_bad and
# **_bad would be allowed.
assert lambda_err_msg in result.errors


@pytest.mark.skipif(
Expand All @@ -67,13 +64,11 @@ def test_RestrictingNodeTransformer__visit_Lambda__5(c_exec):
@pytest.mark.parametrize(*c_exec)
def test_RestrictingNodeTransformer__visit_Lambda__6(c_exec):
"""It prevents arguments starting with `_` in nested tuple unpacking."""
# The old `compile` breaks with tuples in arguments:
if c_exec is RestrictedPython.compile.compile_restricted_exec:
result = c_exec("lambda (a, (c, (_bad, c))): None")
# RestrictedPython.compile.compile_restricted_exec on Python 2 renders
# the error message twice. This is necessary as otherwise *_bad and
# **_bad would be allowed.
assert lambda_err_msg in result.errors
result = c_exec("lambda (a, (c, (_bad, c))): None")
# RestrictedPython.compile.compile_restricted_exec on Python 2 renders
# the error message twice. This is necessary as otherwise *_bad and
# **_bad would be allowed.
assert lambda_err_msg in result.errors


@pytest.mark.skipif(
Expand All @@ -96,8 +91,7 @@ def check_getattr_in_lambda(arg=lambda _bad=(lambda ob, name: name): _bad2):
def test_RestrictingNodeTransformer__visit_Lambda__8(c_exec):
"""It prevents arguments starting with `_` in weird lambdas."""
result = c_exec(BAD_ARG_IN_LAMBDA)
# RestrictedPython.compile.compile_restricted_exec finds both invalid
# names, while the old implementation seems to abort after the first.
# On Python 2 the first error message is contained twice:
assert lambda_err_msg in result.errors


Expand All @@ -116,11 +110,7 @@ def test_RestrictingNodeTransformer__visit_Lambda__9(
}

src = "m = lambda (a, (b, c)), *ag, **kw: a+b+c+sum(ag)+sum(kw.values())"
try:
e_exec(src, glb)
except AttributeError:
# Old implementation does not support tuple unpacking
return
e_exec(src, glb)

ret = glb['m']((1, (2, 3)), 4, 5, 6, g=7, e=8)
assert ret == 36
Expand Down

0 comments on commit 1ee4bce

Please sign in to comment.