test_copyreg.py 4.04 KB
Newer Older
1
import copyreg
2 3
import unittest

4
from test.pickletester import ExtensionSaver
5 6 7 8 9

class C:
    pass


10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
class WithoutSlots(object):
    pass

class WithWeakref(object):
    __slots__ = ('__weakref__',)

class WithPrivate(object):
    __slots__ = ('__spam',)

class WithSingleString(object):
    __slots__ = 'spam'

class WithInherited(WithSingleString):
    __slots__ = ('eggs',)


26 27 28
class CopyRegTestCase(unittest.TestCase):

    def test_class(self):
29
        self.assertRaises(TypeError, copyreg.pickle,
30 31 32
                          C, None, None)

    def test_noncallable_reduce(self):
33
        self.assertRaises(TypeError, copyreg.pickle,
34 35 36
                          type(1), "not a callable")

    def test_noncallable_constructor(self):
37
        self.assertRaises(TypeError, copyreg.pickle,
38 39
                          type(1), int, "not a callable")

40 41
    def test_bool(self):
        import copy
42
        self.assertEqual(True, copy.copy(True))
43

44 45 46 47 48
    def test_extension_registry(self):
        mod, func, code = 'junk1 ', ' junk2', 0xabcd
        e = ExtensionSaver(code)
        try:
            # Shouldn't be in registry now.
49
            self.assertRaises(ValueError, copyreg.remove_extension,
50
                              mod, func, code)
51
            copyreg.add_extension(mod, func, code)
52
            # Should be in the registry.
53 54
            self.assertTrue(copyreg._extension_registry[mod, func] == code)
            self.assertTrue(copyreg._inverted_registry[code] == (mod, func))
55
            # Shouldn't be in the cache.
56
            self.assertNotIn(code, copyreg._extension_cache)
57
            # Redundant registration should be OK.
58
            copyreg.add_extension(mod, func, code)  # shouldn't blow up
59
            # Conflicting code.
60
            self.assertRaises(ValueError, copyreg.add_extension,
61
                              mod, func, code + 1)
62
            self.assertRaises(ValueError, copyreg.remove_extension,
63 64
                              mod, func, code + 1)
            # Conflicting module name.
65
            self.assertRaises(ValueError, copyreg.add_extension,
66
                              mod[1:], func, code )
67
            self.assertRaises(ValueError, copyreg.remove_extension,
68 69
                              mod[1:], func, code )
            # Conflicting function name.
70
            self.assertRaises(ValueError, copyreg.add_extension,
71
                              mod, func[1:], code)
72
            self.assertRaises(ValueError, copyreg.remove_extension,
73 74
                              mod, func[1:], code)
            # Can't remove one that isn't registered at all.
75 76
            if code + 1 not in copyreg._inverted_registry:
                self.assertRaises(ValueError, copyreg.remove_extension,
77 78 79 80 81 82
                                  mod[1:], func[1:], code + 1)

        finally:
            e.restore()

        # Shouldn't be there anymore.
83
        self.assertNotIn((mod, func), copyreg._extension_registry)
84
        # The code *may* be in copyreg._extension_registry, though, if
85 86 87 88 89 90 91
        # we happened to pick on a registered code.  So don't check for
        # that.

        # Check valid codes at the limits.
        for code in 1, 0x7fffffff:
            e = ExtensionSaver(code)
            try:
92 93
                copyreg.add_extension(mod, func, code)
                copyreg.remove_extension(mod, func, code)
94 95 96 97
            finally:
                e.restore()

        # Ensure invalid codes blow up.
98
        for code in -1, 0, 0x80000000:
99
            self.assertRaises(ValueError, copyreg.add_extension,
100
                              mod, func, code)
101

102
    def test_slotnames(self):
103 104
        self.assertEqual(copyreg._slotnames(WithoutSlots), [])
        self.assertEqual(copyreg._slotnames(WithWeakref), [])
105
        expected = ['_WithPrivate__spam']
106 107
        self.assertEqual(copyreg._slotnames(WithPrivate), expected)
        self.assertEqual(copyreg._slotnames(WithSingleString), ['spam'])
108 109
        expected = ['eggs', 'spam']
        expected.sort()
110
        result = copyreg._slotnames(WithInherited)
111
        result.sort()
112
        self.assertEqual(result, expected)
113 114


115
if __name__ == "__main__":
116
    unittest.main()