Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 1 | import copyreg |
| 2 | import unittest |
| 3 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 5 | from test.pickletester import ExtensionSaver |
| 6 | |
| 7 | class C: |
| 8 | pass |
| 9 | |
| 10 | |
| 11 | class WithoutSlots(object): |
| 12 | pass |
| 13 | |
| 14 | class WithWeakref(object): |
| 15 | __slots__ = ('__weakref__',) |
| 16 | |
| 17 | class WithPrivate(object): |
| 18 | __slots__ = ('__spam',) |
| 19 | |
| 20 | class WithSingleString(object): |
| 21 | __slots__ = 'spam' |
| 22 | |
| 23 | class WithInherited(WithSingleString): |
| 24 | __slots__ = ('eggs',) |
| 25 | |
| 26 | |
| 27 | class 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 |
| 43 | self.assertEquals(True, copy.copy(True)) |
| 44 | |
| 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 Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 54 | self.assertTrue(copyreg._extension_registry[mod, func] == code) |
| 55 | self.assertTrue(copyreg._inverted_registry[code] == (mod, func)) |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 56 | # Shouldn't be in the cache. |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 57 | self.assertNotIn(code, copyreg._extension_cache) |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 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 | 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 Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 84 | self.assertNotIn((mod, func), copyreg._extension_registry) |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 85 | # 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): |
| 104 | self.assertEquals(copyreg._slotnames(WithoutSlots), []) |
| 105 | self.assertEquals(copyreg._slotnames(WithWeakref), []) |
| 106 | expected = ['_WithPrivate__spam'] |
| 107 | self.assertEquals(copyreg._slotnames(WithPrivate), expected) |
| 108 | self.assertEquals(copyreg._slotnames(WithSingleString), ['spam']) |
| 109 | expected = ['eggs', 'spam'] |
| 110 | expected.sort() |
| 111 | result = copyreg._slotnames(WithInherited) |
| 112 | result.sort() |
| 113 | self.assertEquals(result, expected) |
| 114 | |
| 115 | |
| 116 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 117 | support.run_unittest(CopyRegTestCase) |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 118 | |
| 119 | |
| 120 | if __name__ == "__main__": |
| 121 | test_main() |