blob: abe0748f851b137fd2ee748e537a0b53186a2956 [file] [log] [blame]
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00001import copyreg
2import unittest
3
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00005from test.pickletester import ExtensionSaver
6
7class C:
8 pass
9
10
11class WithoutSlots(object):
12 pass
13
14class WithWeakref(object):
15 __slots__ = ('__weakref__',)
16
17class WithPrivate(object):
18 __slots__ = ('__spam',)
19
20class WithSingleString(object):
21 __slots__ = 'spam'
22
23class WithInherited(WithSingleString):
24 __slots__ = ('eggs',)
25
26
27class CopyRegTestCase(unittest.TestCase):
28
29 def test_class(self):
30 self.assertRaises(TypeError, copyreg.pickle,
31 C, None, None)
32
33 def test_noncallable_reduce(self):
34 self.assertRaises(TypeError, copyreg.pickle,
35 type(1), "not a callable")
36
37 def test_noncallable_constructor(self):
38 self.assertRaises(TypeError, copyreg.pickle,
39 type(1), int, "not a callable")
40
41 def test_bool(self):
42 import copy
Ezio Melottib3aedd42010-11-20 19:04:17 +000043 self.assertEqual(True, copy.copy(True))
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000044
45 def test_extension_registry(self):
46 mod, func, code = 'junk1 ', ' junk2', 0xabcd
47 e = ExtensionSaver(code)
48 try:
49 # Shouldn't be in registry now.
50 self.assertRaises(ValueError, copyreg.remove_extension,
51 mod, func, code)
52 copyreg.add_extension(mod, func, code)
53 # Should be in the registry.
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000054 self.assertTrue(copyreg._extension_registry[mod, func] == code)
55 self.assertTrue(copyreg._inverted_registry[code] == (mod, func))
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000056 # Shouldn't be in the cache.
Benjamin Peterson577473f2010-01-19 00:09:57 +000057 self.assertNotIn(code, copyreg._extension_cache)
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000058 # Redundant registration should be OK.
59 copyreg.add_extension(mod, func, code) # shouldn't blow up
60 # Conflicting code.
61 self.assertRaises(ValueError, copyreg.add_extension,
62 mod, func, code + 1)
63 self.assertRaises(ValueError, copyreg.remove_extension,
64 mod, func, code + 1)
65 # Conflicting module name.
66 self.assertRaises(ValueError, copyreg.add_extension,
67 mod[1:], func, code )
68 self.assertRaises(ValueError, copyreg.remove_extension,
69 mod[1:], func, code )
70 # Conflicting function name.
71 self.assertRaises(ValueError, copyreg.add_extension,
72 mod, func[1:], code)
73 self.assertRaises(ValueError, copyreg.remove_extension,
74 mod, func[1:], code)
75 # Can't remove one that isn't registered at all.
76 if code + 1 not in copyreg._inverted_registry:
77 self.assertRaises(ValueError, copyreg.remove_extension,
78 mod[1:], func[1:], code + 1)
79
80 finally:
81 e.restore()
82
83 # Shouldn't be there anymore.
Benjamin Peterson577473f2010-01-19 00:09:57 +000084 self.assertNotIn((mod, func), copyreg._extension_registry)
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000085 # The code *may* be in copyreg._extension_registry, though, if
86 # we happened to pick on a registered code. So don't check for
87 # that.
88
89 # Check valid codes at the limits.
90 for code in 1, 0x7fffffff:
91 e = ExtensionSaver(code)
92 try:
93 copyreg.add_extension(mod, func, code)
94 copyreg.remove_extension(mod, func, code)
95 finally:
96 e.restore()
97
98 # Ensure invalid codes blow up.
99 for code in -1, 0, 0x80000000:
100 self.assertRaises(ValueError, copyreg.add_extension,
101 mod, func, code)
102
103 def test_slotnames(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000104 self.assertEqual(copyreg._slotnames(WithoutSlots), [])
105 self.assertEqual(copyreg._slotnames(WithWeakref), [])
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +0000106 expected = ['_WithPrivate__spam']
Ezio Melottib3aedd42010-11-20 19:04:17 +0000107 self.assertEqual(copyreg._slotnames(WithPrivate), expected)
108 self.assertEqual(copyreg._slotnames(WithSingleString), ['spam'])
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +0000109 expected = ['eggs', 'spam']
110 expected.sort()
111 result = copyreg._slotnames(WithInherited)
112 result.sort()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000113 self.assertEqual(result, expected)
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +0000114
115
116def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000117 support.run_unittest(CopyRegTestCase)
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +0000118
119
120if __name__ == "__main__":
121 test_main()