Skip to content

Commit

Permalink
fixes test against old version
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel committed Sep 28, 2016
1 parent cb1c22a commit b004c3e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/RestrictedPython/RCompile.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def compile_restricted_function(p, body, name, filename, globalize=None):
def compile_restricted_exec(source, filename='<string>'):
"""Compiles a restricted code suite."""
gen = RModule(source, filename)
return compileAndTuplize(gen)
return _compileAndTuplize(gen)


def compile_restricted_eval(source, filename='<string>'):
Expand Down
17 changes: 9 additions & 8 deletions src/RestrictedPython/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
"""RestrictedPython package."""

# Old API --> Old Import Locations
#from RestrictedPython.RCompile import compile_restricted
#from RestrictedPython.RCompile import compile_restricted_eval
#from RestrictedPython.RCompile import compile_restricted_exec
#from RestrictedPython.RCompile import compile_restricted_function
from RestrictedPython.RCompile import compile_restricted
from RestrictedPython.RCompile import compile_restricted_eval
from RestrictedPython.RCompile import compile_restricted_exec
from RestrictedPython.RCompile import compile_restricted_function

# new API Style
from RestrictedPython.compile import compile_restricted
from RestrictedPython.compile import compile_restricted_eval
from RestrictedPython.compile import compile_restricted_exec
from RestrictedPython.compile import compile_restricted_function
#from RestrictedPython.compile import compile_restricted
#from RestrictedPython.compile import compile_restricted_exec
#from RestrictedPython.compile import compile_restricted_eval
#from RestrictedPython.compile import compile_restricted_single
#from RestrictedPython.compile import compile_restricted_function

from RestrictedPython.PrintCollector import PrintCollector

Expand Down
22 changes: 13 additions & 9 deletions src/RestrictedPython/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ def compile_restricted_exec(
used_names = []
if policy is None:
# Unrestricted Source Checks
return compile(source, filename, flags=flags, dont_inherit=dont_inherit)
return compile(source, filename, mode='exec', flags=flags, dont_inherit=dont_inherit)
c_ast = ast.parse(source, filename, 'exec')
r_ast = policy(errors, warnings, used_names).visit(c_ast)
if r_ast is None:
raise SyntaxError('AST parse fehlgeschlagen')
try:
byte_code = compile(r_ast, filename, mode='exec', flags=flags,
dont_inherit=dont_inherit)
byte_code = compile(r_ast, filename, mode='exec' # ,
#flags=flags,
#dont_inherit=dont_inherit
)
except SyntaxError as v:
byte_code = None
errors.append(v)
Expand All @@ -39,7 +43,7 @@ def compile_restricted_eval(
used_names = []
if policy is None:
# Unrestricted Source Checks
return compile(source, filename, flags=flags, dont_inherit=dont_inherit)
return compile(source, filename, mode='eval', flags=flags, dont_inherit=dont_inherit)
c_ast = ast.parse(source, filename, 'eval')
r_ast = policy(errors, warnings, used_names).visit(c_ast)
try:
Expand All @@ -63,7 +67,7 @@ def compile_restricted_single(
used_names = []
if policy is None:
# Unrestricted Source Checks
return compile(source, filename, flags=flags, dont_inherit=dont_inherit)
return compile(source, filename, mode='single', flags=flags, dont_inherit=dont_inherit)
c_ast = ast.parse(source, filename, 'single')
r_ast = policy(errors, warnings, used_names).visit(c_ast)
try:
Expand Down Expand Up @@ -126,19 +130,19 @@ def compile_restricted(
"""
byte_code, errors, warnings, used_names = None, None, None, None
if mode == 'exec':
byte_code, errors, warnings, used_names = restricted_compile_exec(
byte_code, errors, warnings, used_names = compile_restricted_exec(
source, filename=filename, flags=flags, dont_inherit=dont_inherit,
policy=policy)
elif mode == 'eval':
byte_code, errors, warnings, used_names = restricted_compile_eval(
byte_code, errors, warnings, used_names = compile_restricted_eval(
source, filename=filename, flags=flags, dont_inherit=dont_inherit,
policy=policy)
elif mode == 'single':
byte_code, errors, warnings, used_names = restricted_compile_single(
byte_code, errors, warnings, used_names = compile_restricted_single(
source, filename=filename, flags=flags, dont_inherit=dont_inherit,
policy=policy)
elif mode == 'function':
byte_code, errors, warnings, used_names = restricted_compile_function(
byte_code, errors, warnings, used_names = compile_restricted_function(
source, filename=filename, flags=flags, dont_inherit=dont_inherit,
policy=policy)
else:
Expand Down
11 changes: 11 additions & 0 deletions src/RestrictedPython/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
class RestrictingNodeTransformer(ast.NodeTransformer):

def __init__(self, errors=[], warnings=[], used_names=[]):
super(RestrictingNodeTransformer, self).__init__()
self.errors = errors
self.warnings = warnings
self.used_names = used_names
Expand All @@ -131,6 +132,16 @@ def error(self, node, info):
lineno = getattr(node, 'lineno', None)
self.errors.append('Line {lineno}: {info}'.format(lineno=lineno, info=info))

def warn(self, node, info):
"""Record a security error discovered during transformation."""
lineno = getattr(node, 'lineno', None)
self.warnings.append('Line {lineno}: {info}'.format(lineno=lineno, info=info))

def use_name(self, node, info):
"""Record a security error discovered during transformation."""
lineno = getattr(node, 'lineno', None)
self.used_names.append('Line {lineno}: {info}'.format(lineno=lineno, info=info))

# Special Functions for an ast.NodeTransformer

def visit(self, node):
Expand Down
45 changes: 22 additions & 23 deletions tests/test_transformer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from RestrictedPython import compile_restricted
from RestrictedPython import compile_restricted_exec
from RestrictedPython import compile_restricted_eval
#from RestrictedPython import compile_restricted_single
from RestrictedPython import compile_restricted_function

import pytest
import sys

Expand All @@ -11,21 +16,23 @@ def no_yield():

def test_transformer__RestrictingNodeTransformer__generic_visit__1():
"""It compiles a number successfully."""
code = compile_restricted('42', '<undefined>', 'exec')
code, errors, warnings, used_names = compile_restricted_exec('42', '<undefined>')
assert 'code' == str(code.__class__.__name__)


def test_transformer__RestrictingNodeTransformer__generic_visit__2():
"""It compiles a function call successfully."""
code = compile_restricted('max([1, 2, 3])', '<undefined>', 'exec')
code, errors, warnings, used_names = compile_restricted_exec('max([1, 2, 3])', '<undefined>')
assert 'code' == str(code.__class__.__name__)


def test_transformer__RestrictingNodeTransformer__generic_visit__100():
"""It raises a SyntaxError if the code contains a `yield`."""
with pytest.raises(SyntaxError) as err:
compile_restricted(YIELD, '<undefined>', 'exec')
assert "Line 2: Yield statements are not allowed." == str(err.value)
code, errors, warnings, used_names = compile_restricted_exec(YIELD, '<undefined>')
assert "Line 2: Yield statements are not allowed." in errors
#with pytest.raises(SyntaxError) as err:
# code, errors, warnings, used_names = compile_restricted_exec(YIELD, '<undefined>')
#assert "Line 2: Yield statements are not allowed." == str(err.value)


EXEC_FUNCTION = """\
Expand All @@ -36,9 +43,8 @@ def no_exec():

def test_transformer__RestrictingNodeTransformer__generic_visit__101():
"""It raises a SyntaxError if the code contains an `exec` function."""
with pytest.raises(SyntaxError) as err:
compile_restricted(EXEC_FUNCTION, '<undefined>', 'exec')
assert "Line 2: Exec statements are not allowed." == str(err.value)
code, errors, warnings, used_names = compile_restricted_exec(EXEC_FUNCTION, '<undefined>')
assert "Line 2: Exec statements are not allowed." in errors


EXEC_STATEMENT = """\
Expand All @@ -51,19 +57,16 @@ def no_exec():
reason="exec statement no longer exists in Python 3")
def test_transformer__RestrictingNodeTransformer__generic_visit__102():
"""It raises a SyntaxError if the code contains an `exec` statement."""
with pytest.raises(SyntaxError) as err:
compile_restricted(EXEC_STATEMENT, '<undefined>', 'exec')
assert "Line 2: Exec statements are not allowed." == str(err.value)
code, errors, warnings, used_names = compile_restricted_exec(EXEC_STATEMENT, '<undefined>')
assert "Line 2: Exec statements are not allowed." in errors


@pytest.mark.skipif(sys.version_info < (3,),
reason="exec statement no longer exists in Python 3")
def test_transformer__RestrictingNodeTransformer__generic_visit__103():
"""It raises a SyntaxError if the code contains an `exec` statement."""
with pytest.raises(SyntaxError) as err:
compile_restricted(EXEC_STATEMENT, '<undefined>', 'exec')
assert ("Missing parentheses in call to 'exec' (<undefined>, line 2)" ==
str(err.value))
code, errors, warnings, used_names = compile_restricted_exec(EXEC_STATEMENT, '<undefined>')
assert "Missing parentheses in call to 'exec' (<undefined>, line 2)" in errors


BAD_NAME = """\
Expand All @@ -74,10 +77,8 @@ def bad_name():

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: "__" is an invalid variable name because it starts with "_"' ==
str(err.value))
code, errors, warnings, used_names = compile_restricted_exec(BAD_NAME, '<undefined>')
assert 'Line 2: "__" is an invalid variable name because it starts with "_"' in errors


BAD_ATTR = """\
Expand All @@ -88,7 +89,5 @@ def bad_attr():

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: "_some_attr" is an invalid attribute name because it starts with "_".' ==
str(err.value))
code, errors, warnings, used_names = compile_restricted_exec(BAD_ATTR, '<undefined>')
assert 'Line 2: "_some_attr" is an invalid attribute name because it starts with "_".' in errors

0 comments on commit b004c3e

Please sign in to comment.