1import copyreg
2import unittest
3
4from test.pickletester import ExtensionSaver
5
6class C:
7    pass
8
9
10class WithoutSlots(object):
11    pass
12
13class WithWeakref(object):
14    __slots__ = ('__weakref__',)
15
16class WithPrivate(object):
17    __slots__ = ('__spam',)
18
19class WithSingleString(object):
20    __slots__ = 'spam'
21
22class WithInherited(WithSingleString):
23    __slots__ = ('eggs',)
24
25
26class CopyRegTestCase(unittest.TestCase):
27
28    def test_class(self):
29        self.assertRaises(TypeError, copyreg.pickle,
30                          C, None, None)
31
32    def test_noncallable_reduce(self):
33        self.assertRaises(TypeError, copyreg.pickle,
34                          type(1), "not a callable")
35
36    def test_noncallable_constructor(self):
37        self.assertRaises(TypeError, copyreg.pickle,
38                          type(1), int, "not a callable")
39
40    def test_bool(self):
41        import copy
42        self.assertEqual(True, copy.copy(True))
43
44    def test_extension_registry(self):
45        mod, func, code = 'junk1 ', ' junk2', 0xabcd
46        e = ExtensionSaver(code)
47        try:
48            # Shouldn't be in registry now.
49            self.assertRaises(ValueError, copyreg.remove_extension,
50                              mod, func, code)
51            copyreg.add_extension(mod, func, code)
52            # Should be in the registry.
53            self.assertTrue(copyreg._extension_registry[mod, func] == code)
54            self.assertTrue(copyreg._inverted_registry[code] == (mod, func))
55            # Shouldn't be in the cache.
56            self.assertNotIn(code, copyreg._extension_cache)
57            # Redundant registration should be OK.
58            copyreg.add_extension(mod, func, code)  # shouldn't blow up
59            # Conflicting code.
60            self.assertRaises(ValueError, copyreg.add_extension,
61                              mod, func, code + 1)
62            self.assertRaises(ValueError, copyreg.remove_extension,
63                              mod, func, code + 1)
64            # Conflicting module name.
65            self.assertRaises(ValueError, copyreg.add_extension,
66                              mod[1:], func, code )
67            self.assertRaises(ValueError, copyreg.remove_extension,
68                              mod[1:], func, code )
69            # Conflicting function name.
70            self.assertRaises(ValueError, copyreg.add_extension,
71                              mod, func[1:], code)
72            self.assertRaises(ValueError, copyreg.remove_extension,
73                              mod, func[1:], code)
74            # Can't remove one that isn't registered at all.
75            if code + 1 not in copyreg._inverted_registry:
76                self.assertRaises(ValueError, copyreg.remove_extension,
77                                  mod[1:], func[1:], code + 1)
78
79        finally:
80            e.restore()
81
82        # Shouldn't be there anymore.
83        self.assertNotIn((mod, func), copyreg._extension_registry)
84        # The code *may* be in copyreg._extension_registry, though, if
85        # we happened to pick on a registered code.  So don't check for
86        # that.
87
88        # Check valid codes at the limits.
89        for code in 1, 0x7fffffff:
90            e = ExtensionSaver(code)
91            try:
92                copyreg.add_extension(mod, func, code)
93                copyreg.remove_extension(mod, func, code)
94            finally:
95                e.restore()
96
97        # Ensure invalid codes blow up.
98        for code in -1, 0, 0x80000000:
99            self.assertRaises(ValueError, copyreg.add_extension,
100                              mod, func, code)
101
102    def test_slotnames(self):
103        self.assertEqual(copyreg._slotnames(WithoutSlots), [])
104        self.assertEqual(copyreg._slotnames(WithWeakref), [])
105        expected = ['_WithPrivate__spam']
106        self.assertEqual(copyreg._slotnames(WithPrivate), expected)
107        self.assertEqual(copyreg._slotnames(WithSingleString), ['spam'])
108        expected = ['eggs', 'spam']
109        expected.sort()
110        result = copyreg._slotnames(WithInherited)
111        result.sort()
112        self.assertEqual(result, expected)
113
114
115if __name__ == "__main__":
116    unittest.main()
117