Skip to content

Commit

Permalink
Refactor check_name into a separate method.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-hof committed Oct 13, 2016
1 parent 726b0f0 commit 91e714e
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/RestrictedPython/transformer.py
Expand Up @@ -293,6 +293,22 @@ def transform_slice(self, slice_):
else:
raise Exception("Unknown slice type: {0}".format(slice_))

def check_name(self, node, name):
if name is None:
return

if name.startswith('_') and name != '_':
self.error(
node,
'"{name}" is an invalid variable name because it '
'starts with "_"'.format(name=name))

elif name.endswith('__roles__'):
self.error(node, '"%s" is an invalid variable name because '
'it ends with "__roles__".' % name)

elif name == "printed":
self.error(node, '"printed" is a reserved name.')

# Special Functions for an ast.NodeTransformer

Expand Down Expand Up @@ -379,10 +395,7 @@ def visit_Name(self, node):
"""
"""
if node.id.startswith('_') and node.id != '_':
self.error(node, '"{name}" is an invalid variable name because it '
'starts with "_"'.format(name=node.id))

self.check_name(node, node.id)
return self.generic_visit(node)

def visit_Load(self, node):
Expand Down

0 comments on commit 91e714e

Please sign in to comment.