Skip to content

Commit

Permalink
resolved conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel committed Sep 22, 2016
2 parents 4a65143 + 4a573e1 commit 0b8181b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/RestrictedPython/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import sys

AST_WHITELIST = [
ast.Assign,
ast.Attribute, # see visit_Attribute for restrictions
ast.Call, # see visit_Call for restrictions
ast.Expr,
ast.FunctionDef,
ast.List,
ast.Load,
ast.Module,
ast.Name,
ast.Name, # see visit_Name for restrictions
ast.Num,
ast.Store,
ast.Str,
]

Expand Down Expand Up @@ -280,16 +283,29 @@ def visit_Or(self, node):



def visit_Attribute(self, node):
if node.attr.startswith('_'):
self.error(
node, 'Attribute names starting with "_" are not allowed.')
else:
return self.generic_visit(node)

def visit_Call(self, node):
if node.func.id == 'exec':
self.error(node, 'Exec statements are not allowed.')
else:
return self.generic_visit(node)

<<<<<<< HEAD
def visit_Print(self, node):
if node.dest is not None:
self.error(
node,
'print statements with destination / chevron are not allowed.')
=======
def visit_Name(self, node):
if node.id.startswith('__'):
self.error(node, 'Names starting with "__" are not allowed.')
>>>>>>> 4a573e1af29eaa0d51bb6eaea560043fafde52ee
else:
return self.generic_visit(node)
28 changes: 28 additions & 0 deletions tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,31 @@ def test_transformer__RestrictingNodeTransformer__generic_visit__103():
compile_restricted(EXEC_STATEMENT, '<undefined>', 'exec')
assert ("Missing parentheses in call to 'exec' (<undefined>, line 2)" ==
str(err.value))


BAD_NAME = """\
def bad_name():
__ = 12
"""


def test_transformer__RestrictingNodeTransformer__generic_visit__104():
"""It raises a SyntaxError if a bad name is used."""
with pytest.raises(SyntaxError) as err:
compile_restricted(BAD_NAME, '<undefined>', 'exec')
assert ('Line 2: Names starting with "__" are not allowed.' ==
str(err.value))


BAD_ATTR = """\
def bad_attr():
some_ob._some_attr = 15
"""


def test_transformer__RestrictingNodeTransformer__generic_visit__105():
"""It raises a SyntaxError if a bad attribute name is used."""
with pytest.raises(SyntaxError) as err:
compile_restricted(BAD_ATTR, '<undefined>', 'exec')
assert ('Line 2: Attribute names starting with "_" are not allowed.' ==
str(err.value))

0 comments on commit 0b8181b

Please sign in to comment.