Skip to content

Commit

Permalink
fix invalid source code input and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel committed Feb 3, 2017
1 parent 6023850 commit 0e845d2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
18 changes: 10 additions & 8 deletions src/RestrictedPython/compile.py
@@ -1,7 +1,9 @@
from collections import namedtuple
from RestrictedPython._compat import IS_PY2
from RestrictedPython.transformer import RestrictingNodeTransformer

import ast
import warnings


CompileResult = namedtuple(
Expand All @@ -25,13 +27,14 @@ def _compile_restricted_mode(
# Unrestricted Source Checks
byte_code = compile(source, filename, mode=mode, flags=flags,
dont_inherit=dont_inherit)
# TODO: Should be an elif check if policy is subclass of
# RestrictionNodeTransformer any other object passed in as policy might
# throw an error or is a NodeVisitor subclass that could be initialized
# with three parameters.
# elif issubclass(policy, RestrictingNodeTransformer):
else:
elif issubclass(policy, RestrictingNodeTransformer):
c_ast = None
allowed_source_types = [str]
if IS_PY2:
allowed_source_types.append(unicode)
if not issubclass(type(source), tuple(allowed_source_types)):
raise TypeError('Not allowed source type: '
'"{0.__class__.__name__}".'.format(source))
try:
c_ast = ast.parse(source, filename, mode)
except (TypeError, ValueError) as e:
Expand Down Expand Up @@ -141,7 +144,6 @@ def compile_restricted(
policy ... `ast.NodeTransformer` class defining the restrictions.
"""
byte_code, errors, warnings, used_names = None, None, None, None
if mode in ['exec', 'eval', 'single', 'function']:
result = _compile_restricted_mode(
source,
Expand All @@ -157,6 +159,6 @@ def compile_restricted(
warning,
SyntaxWarning
)
if errors:
if result.errors:
raise SyntaxError(result.errors)
return result.code
6 changes: 4 additions & 2 deletions src/RestrictedPython/transformer.py
Expand Up @@ -487,12 +487,14 @@ def generic_visit(self, node):
"""
import warnings
warnings.warn(
'{o.__class__.__name__} statement is not known to RestrictedPython'.format(node),
'{o.__class__.__name__}'
' statement is not known to RestrictedPython'.format(node),
SyntaxWarning
)
self.warn(
node,
'{o.__class__.__name__} statement is not known to RestrictedPython'.format(node)
'{o.__class__.__name__}'
' statement is not known to RestrictedPython'.format(node)
)
self.not_allowed(node)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_compile.py
@@ -1,11 +1,17 @@
from . import compile
from RestrictedPython import compile_restricted
from RestrictedPython import CompileResult
from RestrictedPython._compat import IS_PY2

import pytest
import RestrictedPython.compile


def test_compile__compile_restricted_invalid_code_input():
with pytest.raises(TypeError):
compile_restricted(object(), '<string>', 'eval')


@pytest.mark.parametrize(*compile)
def test_compile__compile_restricted_exec__1(compile):
"""It returns a CompileResult on success."""
Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Expand Up @@ -15,12 +15,14 @@ skip_missing_interpreters = False

[testenv]
usedevelop = True
extras =
develop
test
commands =
py.test --cov=src --cov-report=xml {posargs}
setenv =
COVERAGE_FILE=.coverage.{envname}
deps =
.[test,develop]
pytest
pytest-cov
pytest-remove-stale-bytecode
Expand Down

0 comments on commit 0e845d2

Please sign in to comment.