Skip to content

Commit

Permalink
Port with name test + fix other name tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed Feb 3, 2017
1 parent 65a27e6 commit 753f726
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
4 changes: 0 additions & 4 deletions src/RestrictedPython/tests/security_in_syntax26.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
# Each function in this module is compiled using compile_restricted().


def with_as_bad_name():
with x as _leading_underscore:
pass


def relative_import_as_bad_name():
from .x import y as _leading_underscore
Expand Down
3 changes: 1 addition & 2 deletions src/RestrictedPython/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,8 +1259,7 @@ def visit_ExceptHandler(self, node):
return node

def visit_With(self, node):
"""Protects tuple unpacking on with statements. """

"""Protect tuple unpacking on with statements. """
node = self.node_contents_visit(node)

if IS_PY2:
Expand Down
24 changes: 20 additions & 4 deletions tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def bad_name():

@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Name__1(compile):
"""It is an error if a variable name starts with `_`."""
"""It is an error if a variable name starts with `__`."""
result = compile(BAD_NAME_STARTING_WITH_UNDERSCORE)
assert result.errors == (
'Line 2: "__" is an invalid variable name because it starts with "_"',)
Expand All @@ -84,7 +84,7 @@ def overrideGuardWithName():

@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Name__2(compile):
"""It is an error if a variable name ends with `__roles__`."""
"""It is an error if a variable name starts with `_`."""
result = compile(BAD_NAME_OVERRIDE_GUARD_WITH_NAME)
assert result.errors == (
'Line 2: "_getattr" is an invalid variable name because '
Expand All @@ -100,7 +100,7 @@ def _getattr(o):

@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Name__3(compile):
"""It is an error if a variable name ends with `__roles__`."""
"""It is an error if a function name starts with `_`."""
result = compile(BAD_NAME_OVERRIDE_OVERRIDE_GUARD_WITH_FUNCTION)
assert result.errors == (
'Line 2: "_getattr" is an invalid variable name because it '
Expand All @@ -116,13 +116,29 @@ class _getattr:

@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Name__4(compile):
"""It is an error if a variable name ends with `__roles__`."""
"""It is an error if a class name starts with `_`."""
result = compile(BAD_NAME_OVERRIDE_GUARD_WITH_CLASS)
assert result.errors == (
'Line 2: "_getattr" is an invalid variable name because it '
'starts with "_"',)


BAD_NAME_IN_WITH = """\
def with_as_bad_name():
with x as _leading_underscore:
pass
"""


@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Name__4_5(compile):
"""It is an error if a variable in with starts with `_`."""
result = compile(BAD_NAME_IN_WITH)
assert result.errors == (
'Line 2: "_leading_underscore" is an invalid variable name because '
'it starts with "_"',)


BAD_NAME_ENDING_WITH___ROLES__ = """\
def bad_name():
myvar__roles__ = 12
Expand Down

0 comments on commit 753f726

Please sign in to comment.