ยปCore Development>Code coverage>Lib/test/test_copyreg.py

Python code coverage for Lib/test/test_copyreg.py

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