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