Example #1
0
    def test_operators_on_suspicious_types(self):
        class Spam(numbers.Number):
            def __add__(inner_self, other):
                self.fail("doing attribute lookup might have side effects")

        with self.assertRaises(ValueError):
            simple_eval("a + 1", {"a": Spam()})
Example #2
0
    def test_operators_on_suspicious_types(self):
        class Spam(numbers.Number):
            def __add__(inner_self, other):
                self.fail("doing attribute lookup might have side effects")

        with self.assertRaises(ValueError):
            simple_eval('a + 1', {'a': Spam()})
Example #3
0
    def test_lookup_on_suspicious_types(self):
        class FakeDict(object):
            pass

        with self.assertRaises(ValueError):
            simple_eval('a[1]', {'a': FakeDict()})

        class TrickyDict(dict):
            def __getitem__(self, index):
                self.fail("doing key lookup isn't safe")

        with self.assertRaises(ValueError):
            simple_eval('a[1]', {'a': TrickyDict()})

        class SchrodingersDict(dict):
            def __getattribute__(inner_self, attr):
                self.fail("doing attribute lookup might have side effects")

        with self.assertRaises(ValueError):
            simple_eval('a[1]', {'a': SchrodingersDict()})

        class SchrodingersCatsDict(dict):
            def __getattr__(inner_self, attr):
                self.fail("doing attribute lookup might have side effects")

        with self.assertRaises(ValueError):
            simple_eval('a[1]', {'a': SchrodingersDict()})
Example #4
0
    def test_lookup_on_suspicious_types(self):
        class FakeDict(object):
            pass

        with self.assertRaises(ValueError):
            simple_eval("a[1]", {"a": FakeDict()})

        class TrickyDict(dict):
            def __getitem__(self, index):
                self.fail("doing key lookup isn't safe")

        with self.assertRaises(ValueError):
            simple_eval("a[1]", {"a": TrickyDict()})

        class SchrodingersDict(dict):
            def __getattribute__(inner_self, attr):
                self.fail("doing attribute lookup might have side effects")

        with self.assertRaises(ValueError):
            simple_eval("a[1]", {"a": SchrodingersDict()})

        class SchrodingersCatsDict(dict):
            def __getattr__(inner_self, attr):
                self.fail("doing attribute lookup might have side effects")

        with self.assertRaises(ValueError):
            simple_eval("a[1]", {"a": SchrodingersDict()})
Example #5
0
 def test_operators_on_numbers(self):
     self.assertEqual(simple_eval('-2'), -2)
     self.assertEqual(simple_eval('1 + 1'), 2)
     self.assertEqual(simple_eval('a - 2', {'a': 1}), -1)
     with self.assertRaises(ValueError):
         simple_eval('2 * 3')
     with self.assertRaises(ValueError):
         simple_eval('2 ** 3')
Example #6
0
 def test_operators_on_numbers(self):
     self.assertEqual(simple_eval("-2"), -2)
     self.assertEqual(simple_eval("1 + 1"), 2)
     self.assertEqual(simple_eval("a - 2", {"a": 1}), -1)
     with self.assertRaises(ValueError):
         simple_eval("2 * 3")
     with self.assertRaises(ValueError):
         simple_eval("2 ** 3")
Example #7
0
 def test_operators_on_numbers(self):
     self.assertEqual(simple_eval("-2"), -2)
     self.assertEqual(simple_eval("1 + 1"), 2)
     self.assertEqual(simple_eval("a - 2", {"a": 1}), -1)
     with self.assertRaises(ValueError):
         simple_eval("2 * 3")
     with self.assertRaises(ValueError):
         simple_eval("2 ** 3")
Example #8
0
 def test_operators_on_numbers(self):
     self.assertEqual(simple_eval('-2'), -2)
     self.assertEqual(simple_eval('1 + 1'), 2)
     self.assertEqual(simple_eval('a - 2', {'a': 1}), -1)
     with self.assertRaises(ValueError):
         simple_eval('2 * 3')
     with self.assertRaises(ValueError):
         simple_eval('2 ** 3')
Example #9
0
 def test_function_calls_raise(self):
     with self.assertRaises(ValueError):
         simple_eval('1()')
Example #10
0
    def test_attribute_access(self):
        class Foo(object):
            abc = 1

        self.assertEqual(simple_eval('foo.abc', {'foo': Foo()}), 1)
Example #11
0
 def test_name_lookup(self):
     """Names can be lookup up in a namespace"""
     self.assertEqual(simple_eval("a", {"a": 1}), 1)
     self.assertEqual(simple_eval("map"), map)
     self.assertEqual(simple_eval("a[b]", {"a": {"c": 1}, "b": "c"}), 1)
Example #12
0
 def assertMatchesStdlib(self, expr):
     self.assertEqual(ast.literal_eval(expr), simple_eval(expr))
Example #13
0
 def assertMatchesStdlib(self, expr):
     self.assertEqual(ast.literal_eval(expr), simple_eval(expr))
Example #14
0
    def test_attribute_access(self):
        class Foo:
            abc = 1

        self.assertEqual(simple_eval("foo.abc", {"foo": Foo()}), 1)
Example #15
0
 def test_name_lookup_indexing(self):
     """Names can be looked up in a namespace"""
     self.assertEqual(simple_eval("a[b]", {"a": {"c": 1}, "b": "c"}), 1)
Example #16
0
 def test_name_lookup(self):
     """Names can be looked up in a namespace"""
     self.assertEqual(simple_eval("a", {"a": 1}), 1)
     self.assertEqual(simple_eval("map"), map)
Example #17
0
 def test_indexing(self):
     """Literals can be indexed into"""
     self.assertEqual(simple_eval("[1,2][0]"), 1)
Example #18
0
 def test_nonexistant_names_raise(self):
     with self.assertRaises(EvaluationError):
         simple_eval('a')
Example #19
0
    def test_attribute_access(self):
        class Foo(object):
            abc = 1

        self.assertEqual(simple_eval('foo.abc', {'foo': Foo()}), 1)
Example #20
0
 def test_indexing(self):
     """Literals can be indexed into"""
     self.assertEqual(simple_eval('[1,2][0]'), 1)
     self.assertEqual(simple_eval('a', {'a': 1}), 1)
Example #21
0
 def test_indexing(self):
     """Literals can be indexed into"""
     self.assertEqual(simple_eval("[1,2][0]"), 1)
     self.assertEqual(simple_eval("a", {"a": 1}), 1)
Example #22
0
 def test_name_lookup(self):
     """Names can be lookup up in a namespace"""
     self.assertEqual(simple_eval('a', {'a': 1}), 1)
     self.assertEqual(simple_eval('map'), map)
     self.assertEqual(simple_eval('a[b]', {'a': {'c': 1}, 'b': 'c'}), 1)
Example #23
0
 def test_allow_name_lookup(self):
     """Names can be lookup up in a namespace"""
     self.assertEqual(simple_eval("a", {"a": 1}), 1)
Example #24
0
 def test_allow_name_lookup(self):
     """Names can be lookup up in a namespace"""
     self.assertEqual(simple_eval('a', {'a': 1}), 1)
Example #25
0
 def test_name_lookup(self):
     """Names can be lookup up in a namespace"""
     self.assertEqual(simple_eval('a', {'a': 1}), 1)
     self.assertEqual(simple_eval('map'), map)
     self.assertEqual(simple_eval('a[b]', {'a': {'c': 1}, 'b': 'c'}), 1)
Example #26
0
 def test_nonexistant_names_raise(self):
     with self.assertRaises(EvaluationError):
         simple_eval("a")
Example #27
0
 def test_function_calls_raise(self):
     with self.assertRaises(ValueError):
         simple_eval("1()")
Example #28
0
 def test_indexing(self):
     """Literals can be indexed into"""
     self.assertEqual(simple_eval('[1,2][0]'), 1)
     self.assertEqual(simple_eval('a', {'a': 1}), 1)
Example #29
0
    def test_attribute_access(self):
        class Foo(object):
            abc = 1

        self.assertEqual(simple_eval("foo.abc", {"foo": Foo()}), 1)
Example #30
0
 def test_name_lookup(self):
     """Names can be lookup up in a namespace"""
     self.assertEqual(simple_eval("a", {"a": 1}), 1)
     self.assertEqual(simple_eval("map"), map)
     self.assertEqual(simple_eval("a[b]", {"a": {"c": 1}, "b": "c"}), 1)