Ejemplo n.º 1
0
def test_slice():
    rglb = {'_getitem_': getitem}  # restricted globals

    assert restricted_eval('[1, 2, 3, 4, 5]', rglb) == [1, 2, 3, 4, 5]
    assert restricted_eval('[1, 2, 3, 4, 5][:]', rglb) == [1, 2, 3, 4, 5]
    assert restricted_eval('[1, 2, 3, 4, 5][1:]', rglb) == [2, 3, 4, 5]
    assert restricted_eval('[1, 2, 3, 4, 5][:4]', rglb) == [1, 2, 3, 4]
    assert restricted_eval('[1, 2, 3, 4, 5][1:4]', rglb) == [2, 3, 4]
    assert restricted_eval('[1, 2, 3, 4, 5][::3]', rglb) == [1, 4]
    assert restricted_eval('[1, 2, 3, 4, 5][1::3]', rglb) == [2, 5]
    assert restricted_eval('[1, 2, 3, 4, 5][:4:3]', rglb) == [1, 4]
    assert restricted_eval('[1, 2, 3, 4, 5][1:4:3]', rglb) == [2]
Ejemplo n.º 2
0
def test_Sub():
    assert restricted_eval('5 - 3') == 2
def test_FloorDiv():
    assert restricted_eval('7 // 2') == 3
def test_Mod():
    assert restricted_eval('10 % 3') == 1
def test_Mult():
    assert restricted_eval('2 * 2') == 4
def test_Or():
    assert restricted_eval('False or True') is True
Ejemplo n.º 7
0
def test_compile__compile_restricted_eval__2():
    """It compiles code as an Expression."""
    assert restricted_eval('4 * 6') == 24
Ejemplo n.º 8
0
def test_Add():
    assert restricted_eval('1 + 1') == 2
Ejemplo n.º 9
0
def test_RestrictingNodeTransformer__visit_NotEq__1():
    """It allows != expressions."""
    assert restricted_eval('1 != int("1")') is False
Ejemplo n.º 10
0
def test_RestrictingNodeTransformer__visit_NotIn_Dict():
    """It allows `not in` expressions for dicts."""
    assert restricted_eval('2 not in {1: 1, 2: 2, 3: 3}') is False
Ejemplo n.º 11
0
def test_RestrictingNodeTransformer__visit_NotIn_Set():
    """It allows `not in` expressions for sets."""
    assert restricted_eval('2 not in {1, 2, 3}') is False
Ejemplo n.º 12
0
def test_RestrictingNodeTransformer__visit_In_Set():
    """It allows `in` expressions for sets."""
    assert restricted_eval('2 in {1, 1, 2, 3}') is True
Ejemplo n.º 13
0
def test_RestrictingNodeTransformer__visit_NotIn_List():
    """It allows `not in` expressions for lists."""
    assert restricted_eval('2 not in [1, 2, 3]') is False
Ejemplo n.º 14
0
def test_RestrictingNodeTransformer__visit_IsNot__1():
    """It allows `is not` expressions."""
    assert restricted_eval('2 is not None') is True
Ejemplo n.º 15
0
def test_RestrictingNodeTransformer__visit_Eq__1():
    """It allows == expressions."""
    assert restricted_eval('1 == int("1")') is True
Ejemplo n.º 16
0
def test_Div():
    assert restricted_eval('10 / 2') == 5
Ejemplo n.º 17
0
def test_Pow():
    assert restricted_eval('2 ** 8') == 256
def test_Bytes():
    """It allows to use bytes literals."""
    assert restricted_eval('b"code"') == b"code"
Ejemplo n.º 19
0
def test_USub():
    assert restricted_eval('-a', {'a': 2411}) == -2411
def test_Set():
    """It allows to use set literals."""
    assert restricted_eval('{1, 2, 3}') == set([1, 2, 3])
def test_NotIs():
    assert restricted_eval('1 is not True') is True
def test_Ellipsis():
    """It allows using the `...` statement."""
    assert restricted_eval('...') == Ellipsis
Ejemplo n.º 23
0
def test_RestrictingNodeTransformer__visit_GtE__1():
    """It allows >= expressions."""
    assert restricted_eval('1 >= 3') is False
def test_Num():
    """It allows to use number literals."""
    assert restricted_eval('42') == 42
def test_Div():
    assert restricted_eval('10 / 2') == 5
def test_In():
    assert restricted_eval('1 in [1, 2, 3]') is True
def test_Pow():
    assert restricted_eval('2 ** 8') == 256
def test_NotIn():
    assert restricted_eval('4 not in [1, 2, 3]') is True
def test_Add():
    assert restricted_eval('1 + 1') == 2
Ejemplo n.º 30
0
def test_Bytes():
    """It allows to use bytes literals."""
    assert restricted_eval('b"code"') == b"code"
Ejemplo n.º 31
0
def test_Mult():
    assert restricted_eval('2 * 2') == 4
Ejemplo n.º 32
0
def test_Set():
    """It allows to use set literals."""
    assert restricted_eval('{1, 2, 3}') == set([1, 2, 3])
Ejemplo n.º 33
0
def test_Mod():
    assert restricted_eval('10 % 3') == 1
Ejemplo n.º 34
0
def test_Ellipsis():
    """It allows using the `...` statement."""
    assert restricted_eval('...') == Ellipsis
Ejemplo n.º 35
0
def test_FloorDiv():
    assert restricted_eval('7 // 2') == 3
Ejemplo n.º 36
0
def test_Num():
    """It allows to use number literals."""
    assert restricted_eval('42') == 42
Ejemplo n.º 37
0
def test_UAdd():
    assert restricted_eval('+a', {'a': 42}) == 42
Ejemplo n.º 38
0
def test_Guards_bytes():
    """It contains bytes"""
    assert restricted_eval('bytes(1)') == bytes(1)
Ejemplo n.º 39
0
def test_Guards__safe_builtins__1():
    """It contains `slice()`."""
    assert restricted_eval('slice(1)') == slice(1)
Ejemplo n.º 40
0
def test_Guards_sorted():
    """It contains sorted"""
    assert restricted_eval('sorted([5, 2, 8, 1])') == sorted([5, 2, 8, 1])
def test_Is():
    assert restricted_eval('True is True') is True
Ejemplo n.º 42
0
def test_Guards__safe_builtins__1():
    """It contains `slice()`."""
    assert restricted_eval('slice(1)') == slice(1)
def test_Not():
    assert restricted_eval('not False') is True
def test_Sub():
    assert restricted_eval('5 - 3') == 2
def test_And():
    assert restricted_eval('True and True') is True
Ejemplo n.º 46
0
def test_RestrictingNodeTransformer__visit_LtE__1():
    """It allows < expressions."""
    assert restricted_eval('1 <= 3') is True