File: test_magic.py

package info (click to toggle)
rpyc 6.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,324 kB
  • sloc: python: 6,442; makefile: 122
file content (74 lines) | stat: -rw-r--r-- 1,910 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import sys
import rpyc
import unittest

is_py3 = sys.version_info >= (3,)


class Meta(type):

    def __hash__(self):
        return 4321


Base = Meta('Base', (object,), {})


class Foo(Base):
    def __hash__(self):
        return 1234


class Bar(Foo):
    pass


class Mux(Foo):
    def __eq__(self, other):
        return True


class TestContextManagers(unittest.TestCase):
    def setUp(self):
        self.conn = rpyc.classic.connect_thread()

    def tearDown(self):
        self.conn.close()

    def test_hash_class(self):
        hesh = self.conn.builtins.hash
        mod = self.conn.modules.tests.test_magic
        self.assertEqual(hash(mod.Base), 4321)
        self.assertEqual(hash(mod.Foo), 4321)
        self.assertEqual(hash(mod.Bar), 4321)
        self.assertEqual(hash(mod.Base().__class__), 4321)
        self.assertEqual(hash(mod.Foo().__class__), 4321)
        self.assertEqual(hash(mod.Bar().__class__), 4321)

        basecl_ = mod.Foo().__class__.__mro__[1]
        object_ = mod.Foo().__class__.__mro__[2]
        self.assertEqual(hash(basecl_), hesh(basecl_))
        self.assertEqual(hash(object_), hesh(object_))
        self.assertEqual(hash(object_), hesh(self.conn.builtins.object))

    def test_hash_obj(self):
        hesh = self.conn.builtins.hash
        mod = self.conn.modules.tests.test_magic
        obj = mod.Base()

        self.assertNotEqual(hash(obj), 1234)
        self.assertNotEqual(hash(obj), 4321)
        self.assertEqual(hash(obj), hesh(obj))

        self.assertEqual(hash(mod.Foo()), 1234)
        self.assertEqual(hash(mod.Bar()), 1234)
        if is_py3:
            # py3 implicitly adds '__hash__=None' during class construction
            # if '__eq__ is defined:
            self.assertRaises(TypeError, lambda: hash(mod.Mux()))
        else:
            self.assertEqual(hash(mod.Mux()), 1234)


if __name__ == "__main__":
    unittest.main()