Skip to content

Commit

Permalink
make tests pass, by adapting functionality from old implementation by…
Browse files Browse the repository at this point in the history
… allowing operations / statements in new implementation
  • Loading branch information
loechel committed Apr 6, 2017
1 parent d0b1685 commit 531a40d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 55 deletions.
19 changes: 10 additions & 9 deletions src/RestrictedPython/transformer.py
Expand Up @@ -652,7 +652,7 @@ def visit_Invert(self, node):
"""
"""
self.not_allowed(node)
return self.node_contents_visit(node)

def visit_BinOp(self, node):
"""Binary Operations should be allowed."""
Expand Down Expand Up @@ -686,49 +686,49 @@ def visit_FloorDiv(self, node):
"""
"""
self.not_allowed(node)
return self.node_contents_visit(node)

def visit_Mod(self, node):
"""
"""
self.not_allowed(node)
return self.node_contents_visit(node)

def visit_Pow(self, node):
"""
"""
self.not_allowed(node)
return self.node_contents_visit(node)

def visit_LShift(self, node):
"""
"""
self.not_allowed(node)
return self.node_contents_visit(node)

def visit_RShift(self, node):
"""
"""
self.not_allowed(node)
return self.node_contents_visit(node)

def visit_BitOr(self, node):
"""
"""
self.not_allowed(node)
return self.node_contents_visit(node)

def visit_BitXor(self, node):
"""
"""
self.not_allowed(node)
return self.node_contents_visit(node)

def visit_BitAnd(self, node):
"""
"""
self.not_allowed(node)
return self.node_contents_visit(node)

def visit_MatMult(self, node):
"""Matrix Multiplication should not be allowed.
Expand Down Expand Up @@ -1379,6 +1379,7 @@ def visit_Global(self, node):
"""
"""
return self.node_contents_visit(node)
self.not_allowed(node)

def visit_Nonlocal(self, node):
Expand Down
38 changes: 28 additions & 10 deletions tests/transformer/test_global_local.py
@@ -1,37 +1,55 @@
from RestrictedPython._compat import IS_PY3
from tests import c_exec

import pytest


GLOBAL_EXAMPLE = """
a = 1
global a
a = 1
"""


@pytest.mark.parametrize(*c_exec)
def test_Global(c_exec):
result = c_exec(GLOBAL_EXAMPLE)
assert result.code is None
assert result.errors == (
'Line 3: Global statements are not allowed.',
)
assert result.code is not None
assert result.errors == ()
assert result.warnings == []
assert result.used_names == {}


# Example from:
# https://www.smallsurething.com/a-quick-guide-to-nonlocal-in-python-3/
NONLOCAL_EXAMPLE = """
a = 1
nonlocal a
def outside():
msg = "Outside!"
def inside():
nonlocal msg
msg = "Inside!"
print(msg)
inside()
print(msg)
outside()
"""


@pytest.mark.skipif(
not IS_PY3,
reason="Nonlocal Statement was introducted on Python 3.0 but never in Python 2") # NOQA: E501
@pytest.mark.parametrize(*c_exec)
def test_Nonlocal(c_exec):
result = c_exec(NONLOCAL_EXAMPLE)
assert result.code is None
assert result.errors == (
'Line 3: Nonlocal statements are not allowed.',
'Line 5: Nonlocal statements are not allowed.',
)
assert result.warnings == []
assert result.used_names == {}
assert result.warnings == [
"Line 4: Prints, but never reads 'printed' variable.",
"Line 2: Prints, but never reads 'printed' variable."
]
assert result.used_names == {
'msg': True,
'inside': True,
'outside': True
}
64 changes: 28 additions & 36 deletions tests/transformer/test_operators.py
Expand Up @@ -57,36 +57,39 @@ def test_Div(c_eval):

@pytest.mark.parametrize(*c_eval)
def test_Mod(c_eval):
result = c_eval('10 % 2')
assert result.code is None
assert result.errors == (
'Line None: Mod statements are not allowed.',
)
result = c_eval('10 % 3')
assert result.code is not None
assert result.errors == ()
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result == 1


@pytest.mark.parametrize(*c_eval)
def test_Pow(c_eval):
result = c_eval('2 ** 8')
assert result.code is None
assert result.errors == (
'Line None: Pow statements are not allowed.',
)
assert result.code is not None
assert result.errors == ()
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result == 256


@pytest.mark.parametrize(*c_eval)
def test_FloorDiv(c_eval):
result = c_eval('8 // 2')
assert result.code is None
assert result.errors == (
'Line None: FloorDiv statements are not allowed.',
)
assert result.code is not None
assert result.errors == ()
assert result.warnings == []
assert result.used_names == {}

eval_result = eval(result.code)
assert eval_result == 4


@pytest.mark.skipif(
not IS_PY35_OR_GREATER,
Expand Down Expand Up @@ -221,64 +224,53 @@ def test_Not(c_eval):
@pytest.mark.parametrize(*c_eval)
def test_BitAnd(c_eval):
result = c_eval('60 & 13')
assert result.code is None
assert result.errors == (
'Line None: BitAnd statements are not allowed.',)
assert result.code is not None
assert result.errors == ()
assert result.warnings == []
assert result.used_names == {}


@pytest.mark.parametrize(*c_eval)
def test_BitOr(c_eval):
result = c_eval('60 | 13')
assert result.code is None
assert result.errors == (
'Line None: BitOr statements are not allowed.',
)
assert result.code is not None
assert result.errors == ()
assert result.warnings == []
assert result.used_names == {}


@pytest.mark.parametrize(*c_eval)
def test_BitXor(c_eval):
result = c_eval('60 ^ 13')
assert result.code is None
assert result.errors == (
'Line None: BitXor statements are not allowed.',
)
assert result.code is not None
assert result.errors == ()
assert result.warnings == []
assert result.used_names == {}


@pytest.mark.parametrize(*c_eval)
def test_Invert(c_eval):
result = c_eval('~60')
assert result.code is None
assert result.errors == (
'Line None: Invert statements are not allowed.',
)
assert result.code is not None
assert result.errors == ()
assert result.warnings == []
assert result.used_names == {}


@pytest.mark.parametrize(*c_eval)
def test_LShift(c_eval):
result = c_eval('60 << 2')
assert result.code is None
assert result.errors == (
'Line None: LShift statements are not allowed.',
)
assert result.code is not None
assert result.errors == ()
assert result.warnings == []
assert result.used_names == {}


@pytest.mark.parametrize(*c_eval)
def test_RShift(c_eval):
result = c_eval('60 >> 2')
assert result.code is None
assert result.errors == (
'Line None: RShift statements are not allowed.',
)
assert result.code is not None
assert result.errors == ()
assert result.warnings == []
assert result.used_names == {}

Expand Down

0 comments on commit 531a40d

Please sign in to comment.