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

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

class C:
    pass


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

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

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

class WithSingleString(object):
    __slots__ = 'spam'

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


27 28 29
class CopyRegTestCase(unittest.TestCase):

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

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

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

41 42 43 44
    def test_bool(self):
        import copy
        self.assertEquals(True, copy.copy(True))

45 46 47 48 49
    def test_extension_registry(self):
        mod, func, code = 'junk1 ', ' junk2', 0xabcd
        e = ExtensionSaver(code)
        try:
            # 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.
54 55
            self.assert_(copyreg._extension_registry[mod, func] == code)
            self.assert_(copyreg._inverted_registry[code] == (mod, func))
56
            # Shouldn't be in the cache.
57
            self.assert_(code not in copyreg._extension_cache)
58
            # 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 65
                              mod, func, code + 1)
            # Conflicting module name.
66
            self.assertRaises(ValueError, copyreg.add_extension,
67
                              mod[1:], func, code )
68
            self.assertRaises(ValueError, copyreg.remove_extension,
69 70
                              mod[1:], func, code )
            # Conflicting function name.
71
            self.assertRaises(ValueError, copyreg.add_extension,
72
                              mod, func[1:], code)
73
            self.assertRaises(ValueError, copyreg.remove_extension,
74 75
                              mod, func[1:], code)
            # Can't remove one that isn't registered at all.
76 77
            if code + 1 not in copyreg._inverted_registry:
                self.assertRaises(ValueError, copyreg.remove_extension,
78 79 80 81 82 83
                                  mod[1:], func[1:], code + 1)

        finally:
            e.restore()

        # Shouldn't be there anymore.
84 85
        self.assert_((mod, func) not in copyreg._extension_registry)
        # The code *may* be in copyreg._extension_registry, though, if
86 87 88 89 90 91 92
        # 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:
93 94
                copyreg.add_extension(mod, func, code)
                copyreg.remove_extension(mod, func, code)
95 96 97 98
            finally:
                e.restore()

        # 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):
104 105
        self.assertEquals(copyreg._slotnames(WithoutSlots), [])
        self.assertEquals(copyreg._slotnames(WithWeakref), [])
106
        expected = ['_WithPrivate__spam']
107 108
        self.assertEquals(copyreg._slotnames(WithPrivate), expected)
        self.assertEquals(copyreg._slotnames(WithSingleString), ['spam'])
109 110
        expected = ['eggs', 'spam']
        expected.sort()
111
        result = copyreg._slotnames(WithInherited)
112 113 114 115
        result.sort()
        self.assertEquals(result, expected)


116
def test_main():
117
    support.run_unittest(CopyRegTestCase)
118 119 120 121


if __name__ == "__main__":
    test_main()