Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | import copy_reg |
2 | import unittest | |
3 | ||
4 | from test import test_support | |
5 | from test.pickletester import ExtensionSaver | |
6 | ||
7 | class C: | |
8 | pass | |
9 | ||
10 | ||
11 | class CopyRegTestCase(unittest.TestCase): | |
12 | ||
13 | def test_class(self): | |
14 | self.assertRaises(TypeError, copy_reg.pickle, | |
15 | C, None, None) | |
16 | ||
17 | def test_noncallable_reduce(self): | |
18 | self.assertRaises(TypeError, copy_reg.pickle, | |
19 | type(1), "not a callable") | |
20 | ||
21 | def test_noncallable_constructor(self): | |
22 | self.assertRaises(TypeError, copy_reg.pickle, | |
23 | type(1), int, "not a callable") | |
24 | ||
25 | def test_bool(self): | |
26 | import copy | |
27 | self.assertEquals(True, copy.copy(True)) | |
28 | ||
29 | def test_extension_registry(self): | |
30 | mod, func, code = 'junk1 ', ' junk2', 0xabcd | |
31 | e = ExtensionSaver(code) | |
32 | try: | |
33 | # Shouldn't be in registry now. | |
34 | self.assertRaises(ValueError, copy_reg.remove_extension, | |
35 | mod, func, code) | |
36 | copy_reg.add_extension(mod, func, code) | |
37 | # Should be in the registry. | |
38 | self.assert_(copy_reg._extension_registry[mod, func] == code) | |
39 | self.assert_(copy_reg._inverted_registry[code] == (mod, func)) | |
40 | # Shouldn't be in the cache. | |
41 | self.assert_(code not in copy_reg._extension_cache) | |
42 | # Redundant registration should be OK. | |
43 | copy_reg.add_extension(mod, func, code) # shouldn't blow up | |
44 | # Conflicting code. | |
45 | self.assertRaises(ValueError, copy_reg.add_extension, | |
46 | mod, func, code + 1) | |
47 | self.assertRaises(ValueError, copy_reg.remove_extension, | |
48 | mod, func, code + 1) | |
49 | # Conflicting module name. | |
50 | self.assertRaises(ValueError, copy_reg.add_extension, | |
51 | mod[1:], func, code ) | |
52 | self.assertRaises(ValueError, copy_reg.remove_extension, | |
53 | mod[1:], func, code ) | |
54 | # Conflicting function name. | |
55 | self.assertRaises(ValueError, copy_reg.add_extension, | |
56 | mod, func[1:], code) | |
57 | self.assertRaises(ValueError, copy_reg.remove_extension, | |
58 | mod, func[1:], code) | |
59 | # Can't remove one that isn't registered at all. | |
60 | if code + 1 not in copy_reg._inverted_registry: | |
61 | self.assertRaises(ValueError, copy_reg.remove_extension, | |
62 | mod[1:], func[1:], code + 1) | |
63 | ||
64 | finally: | |
65 | e.restore() | |
66 | ||
67 | # Shouldn't be there anymore. | |
68 | self.assert_((mod, func) not in copy_reg._extension_registry) | |
69 | # The code *may* be in copy_reg._extension_registry, though, if | |
70 | # we happened to pick on a registered code. So don't check for | |
71 | # that. | |
72 | ||
73 | # Check valid codes at the limits. | |
74 | for code in 1, 0x7fffffff: | |
75 | e = ExtensionSaver(code) | |
76 | try: | |
77 | copy_reg.add_extension(mod, func, code) | |
78 | copy_reg.remove_extension(mod, func, code) | |
79 | finally: | |
80 | e.restore() | |
81 | ||
82 | # Ensure invalid codes blow up. | |
83 | for code in -1, 0, 0x80000000L: | |
84 | self.assertRaises(ValueError, copy_reg.add_extension, | |
85 | mod, func, code) | |
86 | ||
87 | def test_main(): | |
88 | test_support.run_unittest(CopyRegTestCase) | |
89 | ||
90 | ||
91 | if __name__ == "__main__": | |
92 | test_main() |