1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
from RestrictedPython import compile_restricted_exec
import_errmsg = (
'Line 1: "%s" is an invalid variable name because it starts with "_"')
def test_RestrictingNodeTransformer__visit_Import__1():
"""It allows importing a module."""
result = compile_restricted_exec('import a')
assert result.errors == ()
assert result.code is not None
def test_RestrictingNodeTransformer__visit_Import__2():
"""It denies importing a module starting with `_`."""
result = compile_restricted_exec('import _a')
assert result.errors == (import_errmsg % '_a',)
def test_RestrictingNodeTransformer__visit_Import__3():
"""It denies importing a module starting with `_` as something."""
result = compile_restricted_exec('import _a as m')
assert result.errors == (import_errmsg % '_a',)
def test_RestrictingNodeTransformer__visit_Import__4():
"""It denies importing a module as something starting with `_`."""
result = compile_restricted_exec('import a as _m')
assert result.errors == (import_errmsg % '_m',)
def test_RestrictingNodeTransformer__visit_Import__5():
"""It allows importing from a module."""
result = compile_restricted_exec('from a import m')
assert result.errors == ()
assert result.code is not None
def test_RestrictingNodeTransformer__visit_Import_6():
"""It allows importing from a module starting with `_`."""
result = compile_restricted_exec('from _a import m')
assert result.errors == ()
assert result.code is not None
def test_RestrictingNodeTransformer__visit_Import__7():
"""It denies importing from a module as something starting with `_`."""
result = compile_restricted_exec('from a import m as _n')
assert result.errors == (import_errmsg % '_n',)
def test_RestrictingNodeTransformer__visit_Import__8():
"""It denies as-importing something starting with `_` from a module."""
result = compile_restricted_exec('from a import _m as n')
assert result.errors == (import_errmsg % '_m',)
def test_RestrictingNodeTransformer__visit_Import__9():
"""It denies relative from importing as something starting with `_`."""
result = compile_restricted_exec('from .x import y as _leading_underscore')
assert result.errors == (import_errmsg % '_leading_underscore',)
def test_RestrictingNodeTransformer__visit_Import_star__1():
"""Importing `*` is a SyntaxError in Python itself."""
result = compile_restricted_exec('import *')
assert result.errors == (
"Line 1: SyntaxError: invalid syntax at statement: 'import *'",)
assert result.code is None
def test_RestrictingNodeTransformer__visit_Import_star__2():
"""It denies importing `*` from a module."""
result = compile_restricted_exec('from a import *')
assert result.errors == ('Line 1: "*" imports are not allowed.',)
assert result.code is None
|