Skip to content

Commit

Permalink
make tests python 3 passing
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel committed Sep 29, 2016
1 parent d3704c8 commit 9a587fb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 24 deletions.
42 changes: 34 additions & 8 deletions src/RestrictedPython/Guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
# AccessControl.ZopeGuards contains a large set of wrappers for builtins.
# DocumentTemplate.DT_UTil contains a few.

import sys

safe_builtins = {}

_safe_names = [
'None',
'False',
'True',
'abs',
'basestring',
'bool',
'callable',
'chr',
'cmp',
'complex',
'divmod',
'float',
Expand All @@ -37,7 +37,6 @@
'isinstance',
'issubclass',
'len',
'long',
'oct',
'ord',
'pow',
Expand All @@ -46,9 +45,6 @@
'round',
'str',
'tuple',
'unichr',
'unicode',
'xrange',
'zip'
]

Expand Down Expand Up @@ -83,7 +79,6 @@
'ReferenceError',
'RuntimeError',
'RuntimeWarning',
'StandardError',
'StopIteration',
'SyntaxError',
'SyntaxWarning',
Expand All @@ -103,6 +98,37 @@
'ZeroDivisionError',
]

version = sys.version_info
if version >= (2, 7) and version < (2, 8):
_safe_names.extend([
'basestring',
'cmp',
'long',
'unichr',
'unicode',
'xrange',
])
_safe_exceptions.extend([
'StandardError',
])

if version >= (3, 0):
_safe_names.extend([

])
_safe_exceptions.extend([

])

if version >= (3, 5):
_safe_names.extend([

])
_safe_exceptions.extend([

])


for name in _safe_names:
safe_builtins[name] = __builtins__[name]

Expand Down Expand Up @@ -214,7 +240,7 @@ def __init__(self, ob):
def _full_write_guard():
# Nested scope abuse!
# safetype and Wrapper variables are used by guard()
safetype = {dict: True, list: True}.has_key
safetype = {dict: True, list: True}.has_key if version < (3, 0) else {dict: True, list: True}.keys
Wrapper = _write_wrapper()

def guard(ob):
Expand Down
41 changes: 27 additions & 14 deletions src/RestrictedPython/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,33 @@ def compile_restricted_exec(
used_names = []
if policy is None:
# Unrestricted Source Checks
return compile(source, filename, mode='exec', flags=flags, dont_inherit=dont_inherit)
c_ast = ast.parse(source, filename, 'exec')
policy(errors, warnings, used_names).visit(c_ast)
try:
byte_code = compile(c_ast, filename, mode='exec' # ,
#flags=flags,
#dont_inherit=dont_inherit
)
except SyntaxError as v:
byte_code = None
errors.append(v)
except TypeError as v:
byte_code = None
errors.append(v)
byte_code = compile(source, filename, mode='exec', flags=flags,
dont_inherit=dont_inherit)
else:
c_ast = None
try:
c_ast = ast.parse(source, filename, 'exec')
except SyntaxError as v:
c_ast = None
errors.append('Line {lineno}: {type}: {msg} in on statement: {statement}'.format(
lineno=v.lineno,
type=v.__class__.__name__,
msg=v.msg,
statement=v.text.strip()
))
try:
if c_ast:
policy(errors, warnings, used_names).visit(c_ast)
byte_code = compile(c_ast, filename, mode='exec' # ,
#flags=flags,
#dont_inherit=dont_inherit
)
except SyntaxError as v:
byte_code = None
errors.append(v)
except TypeError as v:
byte_code = None
errors.append(v)
return byte_code, errors, warnings, used_names


Expand Down
14 changes: 13 additions & 1 deletion src/RestrictedPython/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
]

version = sys.version_info
if version >= (2, 7) and version <= (2, 8):
if version >= (2, 7) and version < (2, 8):
AST_WHITELIST.extend([
ast.Print,
])
Expand All @@ -110,6 +110,10 @@
ast.Bytes,
])

if version >= (3, 4):
AST_WHITELIST.extend([
])

if version >= (3, 5):
AST_WHITELIST.extend([
# Async und await, # No Async Elements
Expand All @@ -119,6 +123,14 @@
#ast.AsyncWith, # No Async Elements
])

if version >= (3, 6):
AST_WHITELIST.extend([
# Async und await, # No Async Elements
#ast.AsyncFunctionDef, # No Async Elements
#ast.Await, # No Async Elements
#ast.AsyncFor, # No Async Elements
#ast.AsyncWith, # No Async Elements
])

class RestrictingNodeTransformer(ast.NodeTransformer):

Expand Down
3 changes: 2 additions & 1 deletion tests/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_transformer__RestrictingNodeTransformer__generic_visit__102():
def test_transformer__RestrictingNodeTransformer__generic_visit__103():
"""It raises a SyntaxError if the code contains an `exec` statement."""
code, errors, warnings, used_names = compile_restricted_exec(EXEC_STATEMENT, '<undefined>')
assert "Missing parentheses in call to 'exec' (<undefined>, line 2)" in errors
assert "Line 2: SyntaxError: Missing parentheses in call to 'exec' in on statement: exec 'q = 1'" in errors


BAD_NAME = """\
Expand All @@ -98,6 +98,7 @@ def test_transformer__RestrictingNodeTransformer__generic_visit__104():

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

Expand Down

0 comments on commit 9a587fb

Please sign in to comment.