1"""
2Test generic manipulation of objects.
3"""
4
5
6import unittest
7from numba.core.compiler import compile_isolated, Flags
8from numba.core import types
9from numba.tests.support import TestCase
10
11
12enable_pyobj_flags = Flags()
13enable_pyobj_flags.set("enable_pyobject")
14
15force_pyobj_flags = Flags()
16force_pyobj_flags.set("force_pyobject")
17
18no_pyobj_flags = Flags()
19
20
21class C(object):
22    pass
23
24
25def setattr_usecase(o, v):
26    o.x = v
27
28def delattr_usecase(o):
29    del o.x
30
31
32class TestAttributes(TestCase):
33
34    def test_setattr(self, flags=enable_pyobj_flags):
35        pyfunc = setattr_usecase
36        cr = compile_isolated(pyfunc, (types.pyobject, types.int32), flags=flags)
37        cfunc = cr.entry_point
38        c = C()
39        cfunc(c, 123)
40        self.assertEqual(c.x, 123)
41
42    def test_setattr_attribute_error(self, flags=enable_pyobj_flags):
43        pyfunc = setattr_usecase
44        cr = compile_isolated(pyfunc, (types.pyobject, types.int32), flags=flags)
45        cfunc = cr.entry_point
46        # Can't set undeclared slot
47        with self.assertRaises(AttributeError):
48            cfunc(object(), 123)
49
50    def test_delattr(self, flags=enable_pyobj_flags):
51        pyfunc = delattr_usecase
52        cr = compile_isolated(pyfunc, (types.pyobject,), flags=flags)
53        cfunc = cr.entry_point
54        c = C()
55        c.x = 123
56        cfunc(c)
57        with self.assertRaises(AttributeError):
58            c.x
59
60    def test_delattr_attribute_error(self, flags=enable_pyobj_flags):
61        pyfunc = delattr_usecase
62        cr = compile_isolated(pyfunc, (types.pyobject,), flags=flags)
63        cfunc = cr.entry_point
64        # Can't delete non-existing attribute
65        with self.assertRaises(AttributeError):
66            cfunc(C())
67
68
69if __name__ == '__main__':
70    unittest.main()
71