summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_copyreg.py
diff options
context:
space:
mode:
authorAlexandre Vassalotti <alexandre@peadrop.com>2008-05-11 08:55:36 (GMT)
committerAlexandre Vassalotti <alexandre@peadrop.com>2008-05-11 08:55:36 (GMT)
commitf7fa63dd55e638d300a8d4a148dcedb402fdb750 (patch)
tree53cc22a0517f95753d4772c11f1c247a7c30fa8b /Lib/test/test_copyreg.py
parent7760008c38b6a88dfdc675d97668763a7c1cacd2 (diff)
downloadcpython-f7fa63dd55e638d300a8d4a148dcedb402fdb750.zip
cpython-f7fa63dd55e638d300a8d4a148dcedb402fdb750.tar.gz
cpython-f7fa63dd55e638d300a8d4a148dcedb402fdb750.tar.bz2
Rename copy_reg module to copyreg.
Updated documentation. Merged revisions 63042 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r63042 | alexandre.vassalotti | 2008-05-11 04:25:28 -0400 (Sun, 11 May 2008) | 5 lines Added module stub for copy_reg renaming in 3.0. Renamed copy_reg to copyreg in the standard library, to avoid spurious warnings and ease later merging to py3k branch. Public documentation remains intact. ........
Diffstat (limited to 'Lib/test/test_copyreg.py')
-rw-r--r--Lib/test/test_copyreg.py121
1 files changed, 121 insertions, 0 deletions
diff --git a/Lib/test/test_copyreg.py b/Lib/test/test_copyreg.py
new file mode 100644
index 0000000..82f6c94
--- /dev/null
+++ b/Lib/test/test_copyreg.py
@@ -0,0 +1,121 @@
+import copyreg
+import unittest
+
+from test import test_support
+from test.pickletester import ExtensionSaver
+
+class C:
+ pass
+
+
+class WithoutSlots(object):
+ pass
+
+class WithWeakref(object):
+ __slots__ = ('__weakref__',)
+
+class WithPrivate(object):
+ __slots__ = ('__spam',)
+
+class WithSingleString(object):
+ __slots__ = 'spam'
+
+class WithInherited(WithSingleString):
+ __slots__ = ('eggs',)
+
+
+class CopyRegTestCase(unittest.TestCase):
+
+ def test_class(self):
+ self.assertRaises(TypeError, copyreg.pickle,
+ C, None, None)
+
+ def test_noncallable_reduce(self):
+ self.assertRaises(TypeError, copyreg.pickle,
+ type(1), "not a callable")
+
+ def test_noncallable_constructor(self):
+ self.assertRaises(TypeError, copyreg.pickle,
+ type(1), int, "not a callable")
+
+ def test_bool(self):
+ import copy
+ self.assertEquals(True, copy.copy(True))
+
+ def test_extension_registry(self):
+ mod, func, code = 'junk1 ', ' junk2', 0xabcd
+ e = ExtensionSaver(code)
+ try:
+ # Shouldn't be in registry now.
+ self.assertRaises(ValueError, copyreg.remove_extension,
+ mod, func, code)
+ copyreg.add_extension(mod, func, code)
+ # Should be in the registry.
+ self.assert_(copyreg._extension_registry[mod, func] == code)
+ self.assert_(copyreg._inverted_registry[code] == (mod, func))
+ # Shouldn't be in the cache.
+ self.assert_(code not in copyreg._extension_cache)
+ # Redundant registration should be OK.
+ copyreg.add_extension(mod, func, code) # shouldn't blow up
+ # Conflicting code.
+ self.assertRaises(ValueError, copyreg.add_extension,
+ mod, func, code + 1)
+ self.assertRaises(ValueError, copyreg.remove_extension,
+ mod, func, code + 1)
+ # Conflicting module name.
+ self.assertRaises(ValueError, copyreg.add_extension,
+ mod[1:], func, code )
+ self.assertRaises(ValueError, copyreg.remove_extension,
+ mod[1:], func, code )
+ # Conflicting function name.
+ self.assertRaises(ValueError, copyreg.add_extension,
+ mod, func[1:], code)
+ self.assertRaises(ValueError, copyreg.remove_extension,
+ mod, func[1:], code)
+ # Can't remove one that isn't registered at all.
+ if code + 1 not in copyreg._inverted_registry:
+ self.assertRaises(ValueError, copyreg.remove_extension,
+ mod[1:], func[1:], code + 1)
+
+ finally:
+ e.restore()
+
+ # Shouldn't be there anymore.
+ self.assert_((mod, func) not in copyreg._extension_registry)
+ # The code *may* be in copyreg._extension_registry, though, if
+ # 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:
+ copyreg.add_extension(mod, func, code)
+ copyreg.remove_extension(mod, func, code)
+ finally:
+ e.restore()
+
+ # Ensure invalid codes blow up.
+ for code in -1, 0, 0x80000000:
+ self.assertRaises(ValueError, copyreg.add_extension,
+ mod, func, code)
+
+ def test_slotnames(self):
+ self.assertEquals(copyreg._slotnames(WithoutSlots), [])
+ self.assertEquals(copyreg._slotnames(WithWeakref), [])
+ expected = ['_WithPrivate__spam']
+ self.assertEquals(copyreg._slotnames(WithPrivate), expected)
+ self.assertEquals(copyreg._slotnames(WithSingleString), ['spam'])
+ expected = ['eggs', 'spam']
+ expected.sort()
+ result = copyreg._slotnames(WithInherited)
+ result.sort()
+ self.assertEquals(result, expected)
+
+
+def test_main():
+ test_support.run_unittest(CopyRegTestCase)
+
+
+if __name__ == "__main__":
+ test_main()