Ejemplo n.º 1
0
def test_Eval__RestictionCapableEval__eval_1():
    """It does not add names from the mapping to the
    global scope which are already there."""
    ob = RestrictionCapableEval("a + b + c")
    ob.globals['c'] = 8
    result = ob.eval(dict(a=1, b=2, c=4))
    assert result == 11
Ejemplo n.º 2
0
def test_Eval__RestictionCapableEval__prepUnrestrictedCode_1():
    """It does nothing when unrestricted code is already set by init."""
    ob = RestrictionCapableEval("a")
    assert ob.used == ('a',)
    ob.expr = "b"
    ob.prepUnrestrictedCode()
    assert ob.used == ('a',)
Ejemplo n.º 3
0
def test_Eval__RestictionCapableEval__prepUnrestrictedCode_1():
    """It does nothing when unrestricted code is already set by init."""
    ob = RestrictionCapableEval("a")
    assert ob.used == ('a', )
    ob.expr = "b"
    ob.prepUnrestrictedCode()
    assert ob.used == ('a', )
Ejemplo n.º 4
0
def test_Eval__RestictionCapableEval__eval_1():
    """It does not add names from the mapping to the
    global scope which are already there."""
    ob = RestrictionCapableEval("a + b + c")
    ob.globals['c'] = 8
    result = ob.eval(dict(a=1, b=2, c=4))
    assert result == 11
 def checkUnrestrictedEval(self):
     expr = RestrictionCapableEval("{'a':[m.pop()]}['a'] + [m[0]]")
     v = [12, 34]
     expect = v[:]
     expect.reverse()
     res = expr.eval({'m': v})
     self.assertEqual(res, expect)
     v = [12, 34]
     res = expr(m=v)
     self.assertEqual(res, expect)
Ejemplo n.º 6
0
def test_Eval__RestictionCapableEval__prepUnrestrictedCode_2():
    """It does not re-set 'used' if it is already set by an earlier call."""
    ob = RestrictionCapableEval("a")
    assert ob.used == ('a',)
    ob.used = ('b',)
    # This is needed to force re-compilation
    ob.ucode = None
    ob.prepUnrestrictedCode()
    # If it was called again, used would be ('a',) again.
    assert ob.used == ('b',)
 def checkUnrestrictedEval(self):
     expr = RestrictionCapableEval("{'a':[m.pop()]}['a'] + [m[0]]")
     v = [12, 34]
     expect = v[:]
     expect.reverse()
     res = expr.eval({'m':v})
     self.assertEqual(res, expect)
     v = [12, 34]
     res = expr(m=v)
     self.assertEqual(res, expect)
Ejemplo n.º 8
0
def test_init():
    ob = RestrictionCapableEval(exp)

    assert ob.expr == "{'a':[m.pop()]}['a']         + [m[0]]"
    assert ob.used == ('m', )
    assert ob.ucode is not None
    assert ob.rcode is None
Ejemplo n.º 9
0
 def index_object(self, documentId, o):
     try:
         if RestrictionCapableEval(self.expr).eval({'o': o}):
             self.ids.insert(documentId)
         else:
             try:
                 self.ids.remove(documentId)
             except KeyError:
                 pass
     except ConflictError:
         raise
     except:
         LOG.warn('eval() failed Object: %s, expr: %s' %\
                  (o.getId(),self.expr), exc_info=sys.exc_info())
Ejemplo n.º 10
0
def test_Eval__RestictionCapableEval__prepUnrestrictedCode_2():
    """It does not re-set 'used' if it is already set by an earlier call."""
    ob = RestrictionCapableEval("a")
    assert ob.used == ('a', )
    ob.used = ('b', )
    # This is needed to force re-compilation
    ob.ucode = None
    ob.prepUnrestrictedCode()
    # If it was called again, used would be ('a',) again.
    assert ob.used == ('b', )
Ejemplo n.º 11
0
def test_Eval__RestrictionCapableEval_1():
    """It raises SyntaxError when there are errors
    (by using forbidden stuff) in the code."""
    ob = RestrictionCapableEval("_a")
    with pytest.raises(SyntaxError):
        ob.prepRestrictedCode()
Ejemplo n.º 12
0
def test_Eval__RestrictionCapableEval__2():
    """It stores used names."""
    ob = RestrictionCapableEval("[x for x in (1, 2, 3)]")
    assert ob.used == ('x', )
Ejemplo n.º 13
0
def test_Eval__RestrictionCapableEval_1():
    """It raises SyntaxError when there are errors
    (by using forbidden stuff) in the code."""
    ob = RestrictionCapableEval("_a")
    with pytest.raises(SyntaxError):
        ob.prepRestrictedCode()
Ejemplo n.º 14
0
def test_eval():
    ob = RestrictionCapableEval(exp)
    ret = ob.eval({'m': [1, 2]})
    assert ret == [2, 1]
Ejemplo n.º 15
0
def test_call():
    ob = RestrictionCapableEval(exp)
    ret = ob(m=[1, 2])
    assert ret == [2, 1]
Ejemplo n.º 16
0
def test_eval():
    ob = RestrictionCapableEval(exp)
    ret = ob.eval({'m': [1, 2]})
    assert ret == [2, 1]
Ejemplo n.º 17
0
def test_Eval__RestictionCapableEval__eval__2():
    """It allows to use list comprehensions."""
    ob = RestrictionCapableEval("[item for item in (1, 2)]")
    result = ob.eval({})
    assert result == [1, 2]
Ejemplo n.º 18
0
def test_Eval__RestictionCapableEval__eval__2():
    """It allows to use list comprehensions."""
    ob = RestrictionCapableEval("[item for item in (1, 2)]")
    result = ob.eval({})
    assert result == [1, 2]
Ejemplo n.º 19
0
def test_init_with_syntax_error():
    with pytest.raises(SyntaxError):
        RestrictionCapableEval("if:")
Ejemplo n.º 20
0
def test_prepRestrictedCode():
    ob = RestrictionCapableEval(exp)
    ob.prepRestrictedCode()
    assert ob.used == ('m', )
    assert ob.rcode is not None
Ejemplo n.º 21
0
def test_prepRestrictedCode():
    ob = RestrictionCapableEval(exp)
    ob.prepRestrictedCode()
    assert ob.used == ('m', )
    assert ob.rcode is not None