Skip to content

Commit

Permalink
Fix test: Warnings are only emitted once unless warn_explicit is used.
Browse files Browse the repository at this point in the history
This way the second (now deleted test) failed on PyPy.
  • Loading branch information
Michael Howitz committed Oct 13, 2017
1 parent 4109d32 commit 77c324a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/RestrictedPython/compile.py
Expand Up @@ -27,7 +27,8 @@ def _compile_restricted_mode(
policy=RestrictingNodeTransformer):

if not IS_CPYTHON:
warnings.warn(NOT_CPYTHON_WARNING, RuntimeWarning)
warnings.warn_explicit(
NOT_CPYTHON_WARNING, RuntimeWarning, 'RestrictedPython', 0)

byte_code = None
collected_errors = []
Expand Down
24 changes: 7 additions & 17 deletions tests/test_compile.py
Expand Up @@ -174,6 +174,9 @@ def test_compile_restricted():
with pytest.warns(SyntaxWarning) as record:
result = compile_restricted(PRINT_EXAMPLE, '<string>', 'exec')
assert isinstance(result, types.CodeType)
# Non-CPython versions have a RuntimeWarning, too.
if len(record) > 2: # pragma: no cover
record.pop()
assert len(record) == 2
assert record[0].message.args[0] == \
'Line 3: Print statement is deprecated ' \
Expand All @@ -196,9 +199,11 @@ def test_compile_restricted_eval():
compile_restricted(EVAL_EXAMPLE, '<string>', 'exec')


def test_compile_CPython_warning_mocked(recwarn, mocker):
def test_compile___compile_restricted_mode__1(recwarn, mocker):
"""It warns when using another Python implementation than CPython."""
mocker.patch('RestrictedPython.compile.IS_CPYTHON', new=False)
if platform.python_implementation() == 'CPython':
# Using CPython we have to fake the check:
mocker.patch('RestrictedPython.compile.IS_CPYTHON', new=False)
compile_restricted('42')
assert len(recwarn) == 1
w = recwarn.pop()
Expand All @@ -207,18 +212,3 @@ def test_compile_CPython_warning_mocked(recwarn, mocker):
'RestrictedPython is only supported on CPython: use on other Python '
'implementations may create security issues.'
)


@pytest.mark.skipif(
platform.python_implementation() == 'CPython',
reason='Warning only present if not CPython.')
def test_compile_CPython_warning(recwarn, mocker):
"""It warns when using another Python implementation than CPython."""
assert platform.python_implementation() != 'CPython'
with pytest.warns(RuntimeWarning) as record:
compile_restricted('42')
assert len(record) == 1
assert str(record[0].message) == str(
'RestrictedPython is only supported on CPython: use on other Python '
'implementations may create security issues.'
)

0 comments on commit 77c324a

Please sign in to comment.