PyXR

c:\python24\lib \ test \ test_copy_reg.py



0001 import copy_reg
0002 import unittest
0003 
0004 from test import test_support
0005 from test.pickletester import ExtensionSaver
0006 
0007 class C:
0008     pass
0009 
0010 
0011 class CopyRegTestCase(unittest.TestCase):
0012 
0013     def test_class(self):
0014         self.assertRaises(TypeError, copy_reg.pickle,
0015                           C, None, None)
0016 
0017     def test_noncallable_reduce(self):
0018         self.assertRaises(TypeError, copy_reg.pickle,
0019                           type(1), "not a callable")
0020 
0021     def test_noncallable_constructor(self):
0022         self.assertRaises(TypeError, copy_reg.pickle,
0023                           type(1), int, "not a callable")
0024 
0025     def test_bool(self):
0026         import copy
0027         self.assertEquals(True, copy.copy(True))
0028 
0029     def test_extension_registry(self):
0030         mod, func, code = 'junk1 ', ' junk2', 0xabcd
0031         e = ExtensionSaver(code)
0032         try:
0033             # Shouldn't be in registry now.
0034             self.assertRaises(ValueError, copy_reg.remove_extension,
0035                               mod, func, code)
0036             copy_reg.add_extension(mod, func, code)
0037             # Should be in the registry.
0038             self.assert_(copy_reg._extension_registry[mod, func] == code)
0039             self.assert_(copy_reg._inverted_registry[code] == (mod, func))
0040             # Shouldn't be in the cache.
0041             self.assert_(code not in copy_reg._extension_cache)
0042             # Redundant registration should be OK.
0043             copy_reg.add_extension(mod, func, code)  # shouldn't blow up
0044             # Conflicting code.
0045             self.assertRaises(ValueError, copy_reg.add_extension,
0046                               mod, func, code + 1)
0047             self.assertRaises(ValueError, copy_reg.remove_extension,
0048                               mod, func, code + 1)
0049             # Conflicting module name.
0050             self.assertRaises(ValueError, copy_reg.add_extension,
0051                               mod[1:], func, code )
0052             self.assertRaises(ValueError, copy_reg.remove_extension,
0053                               mod[1:], func, code )
0054             # Conflicting function name.
0055             self.assertRaises(ValueError, copy_reg.add_extension,
0056                               mod, func[1:], code)
0057             self.assertRaises(ValueError, copy_reg.remove_extension,
0058                               mod, func[1:], code)
0059             # Can't remove one that isn't registered at all.
0060             if code + 1 not in copy_reg._inverted_registry:
0061                 self.assertRaises(ValueError, copy_reg.remove_extension,
0062                                   mod[1:], func[1:], code + 1)
0063 
0064         finally:
0065             e.restore()
0066 
0067         # Shouldn't be there anymore.
0068         self.assert_((mod, func) not in copy_reg._extension_registry)
0069         # The code *may* be in copy_reg._extension_registry, though, if
0070         # we happened to pick on a registered code.  So don't check for
0071         # that.
0072 
0073         # Check valid codes at the limits.
0074         for code in 1, 0x7fffffff:
0075             e = ExtensionSaver(code)
0076             try:
0077                 copy_reg.add_extension(mod, func, code)
0078                 copy_reg.remove_extension(mod, func, code)
0079             finally:
0080                 e.restore()
0081 
0082         # Ensure invalid codes blow up.
0083         for code in -1, 0, 0x80000000L:
0084             self.assertRaises(ValueError, copy_reg.add_extension,
0085                               mod, func, code)
0086 
0087 def test_main():
0088     test_support.run_unittest(CopyRegTestCase)
0089 
0090 
0091 if __name__ == "__main__":
0092     test_main()
0093 

Generated by PyXR 0.9.4
SourceForge.net Logo