Beispiel #1
0
def test_slice(e_eval):
    low = 1
    high = 4
    stride = 3

    rglb = {'_getitem_': getitem}  # restricted globals

    assert e_eval('[1, 2, 3, 4, 5]', rglb) == [1, 2, 3, 4, 5]
    assert e_eval('[1, 2, 3, 4, 5][:]', rglb) == [1, 2, 3, 4, 5]
    assert e_eval('[1, 2, 3, 4, 5][%d:]' % low, rglb) == [2, 3, 4, 5]
    assert e_eval('[1, 2, 3, 4, 5][:%d]' % high, rglb) == [1, 2, 3, 4]
    assert e_eval('[1, 2, 3, 4, 5][%d:%d]' % (low, high), rglb) == [2, 3, 4]
    assert e_eval('[1, 2, 3, 4, 5][::%d]' % stride, rglb) == [1, 4]
    assert e_eval('[1, 2, 3, 4, 5][%d::%d]' % (low, stride), rglb) == [2, 5]
    assert e_eval('[1, 2, 3, 4, 5][:%d:%d]' % (high, stride), rglb) == [1, 4]
    assert e_eval('[1, 2, 3, 4, 5][%d:%d:%d]' % (low, high, stride),
                  rglb) == [2]  # NOQA: E501
Beispiel #2
0
def test_Sub(e_eval):
    assert e_eval('5 - 3') == 2
def test_Guards__safe_builtins__1(e_eval):
    """It contains `slice()`."""
    restricted_globals = dict(__builtins__=safe_builtins)
    assert e_eval('slice(1)', restricted_globals) == slice(1)
def test_UAdd(e_eval):
    assert e_eval('+a', {'a': 42}) == 42
def test_RestrictingNodeTransformer__visit_NotIn_Dict(e_eval):
    """It allows `not in` expressions for dicts."""
    assert e_eval('2 not in {1: 1, 2: 2, 3: 3}') is False
def test_RestrictingNodeTransformer__visit_In_Set(e_eval):
    """It allows `in` expressions for sets."""
    assert e_eval('2 in {1, 1, 2, 3}') is True
def test_RestrictingNodeTransformer__visit_Eq__1(e_eval):
    """It allows == expressions."""
    assert e_eval('1 == int("1")') is True
def test_RestrictingNodeTransformer__visit_GtE__1(e_eval):
    """It allows >= expressions."""
    assert e_eval('1 >= 3') is False
def test_BitXor(e_eval):
    assert e_eval('5 ^ 3') == 6
def test_BitOr(e_eval):
    assert e_eval('5 | 3') == 7
Beispiel #11
0
def test_FloorDiv(e_eval):
    assert e_eval('7 // 2') == 3
Beispiel #12
0
def test_Pow(e_eval):
    assert e_eval('2 ** 8') == 256
Beispiel #13
0
def test_Mod(e_eval):
    assert e_eval('10 % 3') == 1
Beispiel #14
0
def test_Div(e_eval):
    assert e_eval('10 / 2') == 5
Beispiel #15
0
def test_Mult(e_eval):
    assert e_eval('2 * 2') == 4
def test_RestrictingNodeTransformer__visit_NotEq__1(e_eval):
    """It allows != expressions."""
    assert e_eval('1 != int("1")') is False
def test_RestrictingNodeTransformer__visit_LtE__1(e_eval):
    """It allows < expressions."""
    assert e_eval('1 <= 3') is True
def test_Invert(e_eval):
    assert e_eval('~17') == -18
def test_RestrictingNodeTransformer__visit_IsNot__1(e_eval):
    """It allows `is not` expressions."""
    assert e_eval('2 is not None') is True
def test_LShift(e_eval):
    assert e_eval('8 << 2') == 32
def test_RestrictingNodeTransformer__visit_NotIn_List(e_eval):
    """It allows `not in` expressions for lists."""
    assert e_eval('2 not in [1, 2, 3]') is False
def test_RShift(e_eval):
    assert e_eval('8 >> 1') == 4
def test_RestrictingNodeTransformer__visit_NotIn_Set(e_eval):
    """It allows `not in` expressions for sets."""
    assert e_eval('2 not in {1, 2, 3}') is False
def test_BitAnd(e_eval):
    assert e_eval('5 & 3') == 1
def test_USub(e_eval):
    assert e_eval('-a', {'a': 2411}) == -2411
Beispiel #26
0
def test_NotIs(e_eval):
    assert e_eval('1 is not True') is True
Beispiel #27
0
def test_Guards__safe_builtins__1(e_eval):
    """It contains `slice()`."""
    assert e_eval('slice(1)', safe_globals) == slice(1)
Beispiel #28
0
def test_Is(e_eval):
    assert e_eval('True is True') is True
def test_compile__compile_restricted_eval__2(e_eval):
    """It compiles code as an Expression."""
    assert e_eval('4 * 6') == 24
Beispiel #30
0
def test_Add(e_eval):
    assert e_eval('1 + 1') == 2