4
from test import test_support
5
from test.pickletester import ExtensionSaver
11
class WithoutSlots(object):
14
class WithWeakref(object):
15
__slots__ = ('__weakref__',)
17
class WithPrivate(object):
18
__slots__ = ('__spam',)
20
class WithSingleString(object):
23
class WithInherited(WithSingleString):
27
class CopyRegTestCase(unittest.TestCase):
30
self.assertRaises(TypeError, copy_reg.pickle,
33
def test_noncallable_reduce(self):
34
self.assertRaises(TypeError, copy_reg.pickle,
35
type(1), "not a callable")
37
def test_noncallable_constructor(self):
38
self.assertRaises(TypeError, copy_reg.pickle,
39
type(1), int, "not a callable")
43
self.assertEquals(True, copy.copy(True))
45
def test_extension_registry(self):
46
mod, func, code = 'junk1 ', ' junk2', 0xabcd
47
e = ExtensionSaver(code)
49
# Shouldn't be in registry now.
50
self.assertRaises(ValueError, copy_reg.remove_extension,
52
copy_reg.add_extension(mod, func, code)
53
# Should be in the registry.
54
self.assert_(copy_reg._extension_registry[mod, func] == code)
55
self.assert_(copy_reg._inverted_registry[code] == (mod, func))
56
# Shouldn't be in the cache.
57
self.assert_(code not in copy_reg._extension_cache)
58
# Redundant registration should be OK.
59
copy_reg.add_extension(mod, func, code) # shouldn't blow up
61
self.assertRaises(ValueError, copy_reg.add_extension,
63
self.assertRaises(ValueError, copy_reg.remove_extension,
65
# Conflicting module name.
66
self.assertRaises(ValueError, copy_reg.add_extension,
68
self.assertRaises(ValueError, copy_reg.remove_extension,
70
# Conflicting function name.
71
self.assertRaises(ValueError, copy_reg.add_extension,
73
self.assertRaises(ValueError, copy_reg.remove_extension,
75
# Can't remove one that isn't registered at all.
76
if code + 1 not in copy_reg._inverted_registry:
77
self.assertRaises(ValueError, copy_reg.remove_extension,
78
mod[1:], func[1:], code + 1)
83
# Shouldn't be there anymore.
84
self.assert_((mod, func) not in copy_reg._extension_registry)
85
# The code *may* be in copy_reg._extension_registry, though, if
86
# we happened to pick on a registered code. So don't check for
89
# Check valid codes at the limits.
90
for code in 1, 0x7fffffff:
91
e = ExtensionSaver(code)
93
copy_reg.add_extension(mod, func, code)
94
copy_reg.remove_extension(mod, func, code)
98
# Ensure invalid codes blow up.
99
for code in -1, 0, 0x80000000L:
100
self.assertRaises(ValueError, copy_reg.add_extension,
103
def test_slotnames(self):
104
self.assertEquals(copy_reg._slotnames(WithoutSlots), [])
105
self.assertEquals(copy_reg._slotnames(WithWeakref), [])
106
expected = ['_WithPrivate__spam']
107
self.assertEquals(copy_reg._slotnames(WithPrivate), expected)
108
self.assertEquals(copy_reg._slotnames(WithSingleString), ['spam'])
109
expected = ['eggs', 'spam']
111
result = copy_reg._slotnames(WithInherited)
113
self.assertEquals(result, expected)
117
test_support.run_unittest(CopyRegTestCase)
120
if __name__ == "__main__":