2008-05-11 05:55:36 -03:00
|
|
|
import copyreg
|
2001-05-22 17:38:44 -03:00
|
|
|
import unittest
|
|
|
|
|
2003-02-04 17:47:44 -04:00
|
|
|
from test.pickletester import ExtensionSaver
|
2000-10-11 19:17:35 -03:00
|
|
|
|
|
|
|
class C:
|
|
|
|
pass
|
|
|
|
|
2022-05-08 11:10:11 -03:00
|
|
|
def pickle_C(c):
|
|
|
|
return C, ()
|
|
|
|
|
2000-10-11 19:17:35 -03:00
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
class WithoutSlots(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class WithWeakref(object):
|
|
|
|
__slots__ = ('__weakref__',)
|
|
|
|
|
|
|
|
class WithPrivate(object):
|
|
|
|
__slots__ = ('__spam',)
|
|
|
|
|
2017-08-04 05:45:00 -03:00
|
|
|
class _WithLeadingUnderscoreAndPrivate(object):
|
|
|
|
__slots__ = ('__spam',)
|
|
|
|
|
|
|
|
class ___(object):
|
|
|
|
__slots__ = ('__spam',)
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
class WithSingleString(object):
|
|
|
|
__slots__ = 'spam'
|
|
|
|
|
|
|
|
class WithInherited(WithSingleString):
|
|
|
|
__slots__ = ('eggs',)
|
|
|
|
|
|
|
|
|
2001-05-22 17:38:44 -03:00
|
|
|
class CopyRegTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_class(self):
|
2022-05-08 11:10:11 -03:00
|
|
|
copyreg.pickle(C, pickle_C)
|
2001-05-22 17:38:44 -03:00
|
|
|
|
|
|
|
def test_noncallable_reduce(self):
|
2008-05-11 05:55:36 -03:00
|
|
|
self.assertRaises(TypeError, copyreg.pickle,
|
2022-05-08 11:10:11 -03:00
|
|
|
C, "not a callable")
|
2001-05-22 17:38:44 -03:00
|
|
|
|
|
|
|
def test_noncallable_constructor(self):
|
2008-05-11 05:55:36 -03:00
|
|
|
self.assertRaises(TypeError, copyreg.pickle,
|
2022-05-08 11:10:11 -03:00
|
|
|
C, pickle_C, "not a callable")
|
2001-05-22 17:38:44 -03:00
|
|
|
|
2003-01-26 07:32:44 -04:00
|
|
|
def test_bool(self):
|
|
|
|
import copy
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(True, copy.copy(True))
|
2003-01-26 07:32:44 -04:00
|
|
|
|
2003-02-04 13:49:36 -04:00
|
|
|
def test_extension_registry(self):
|
|
|
|
mod, func, code = 'junk1 ', ' junk2', 0xabcd
|
|
|
|
e = ExtensionSaver(code)
|
|
|
|
try:
|
|
|
|
# Shouldn't be in registry now.
|
2008-05-11 05:55:36 -03:00
|
|
|
self.assertRaises(ValueError, copyreg.remove_extension,
|
2003-02-04 13:49:36 -04:00
|
|
|
mod, func, code)
|
2008-05-11 05:55:36 -03:00
|
|
|
copyreg.add_extension(mod, func, code)
|
2003-02-04 13:49:36 -04:00
|
|
|
# Should be in the registry.
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(copyreg._extension_registry[mod, func] == code)
|
|
|
|
self.assertTrue(copyreg._inverted_registry[code] == (mod, func))
|
2003-02-04 13:49:36 -04:00
|
|
|
# Shouldn't be in the cache.
|
2010-01-18 20:09:57 -04:00
|
|
|
self.assertNotIn(code, copyreg._extension_cache)
|
2003-02-04 13:49:36 -04:00
|
|
|
# Redundant registration should be OK.
|
2008-05-11 05:55:36 -03:00
|
|
|
copyreg.add_extension(mod, func, code) # shouldn't blow up
|
2003-02-04 13:49:36 -04:00
|
|
|
# Conflicting code.
|
2008-05-11 05:55:36 -03:00
|
|
|
self.assertRaises(ValueError, copyreg.add_extension,
|
2003-02-04 13:49:36 -04:00
|
|
|
mod, func, code + 1)
|
2008-05-11 05:55:36 -03:00
|
|
|
self.assertRaises(ValueError, copyreg.remove_extension,
|
2003-02-04 13:49:36 -04:00
|
|
|
mod, func, code + 1)
|
|
|
|
# Conflicting module name.
|
2008-05-11 05:55:36 -03:00
|
|
|
self.assertRaises(ValueError, copyreg.add_extension,
|
2003-02-04 13:49:36 -04:00
|
|
|
mod[1:], func, code )
|
2008-05-11 05:55:36 -03:00
|
|
|
self.assertRaises(ValueError, copyreg.remove_extension,
|
2003-02-04 13:49:36 -04:00
|
|
|
mod[1:], func, code )
|
|
|
|
# Conflicting function name.
|
2008-05-11 05:55:36 -03:00
|
|
|
self.assertRaises(ValueError, copyreg.add_extension,
|
2003-02-04 13:49:36 -04:00
|
|
|
mod, func[1:], code)
|
2008-05-11 05:55:36 -03:00
|
|
|
self.assertRaises(ValueError, copyreg.remove_extension,
|
2003-02-04 13:49:36 -04:00
|
|
|
mod, func[1:], code)
|
|
|
|
# Can't remove one that isn't registered at all.
|
2008-05-11 05:55:36 -03:00
|
|
|
if code + 1 not in copyreg._inverted_registry:
|
|
|
|
self.assertRaises(ValueError, copyreg.remove_extension,
|
2003-02-04 13:49:36 -04:00
|
|
|
mod[1:], func[1:], code + 1)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
e.restore()
|
|
|
|
|
|
|
|
# Shouldn't be there anymore.
|
2010-01-18 20:09:57 -04:00
|
|
|
self.assertNotIn((mod, func), copyreg._extension_registry)
|
2008-05-11 05:55:36 -03:00
|
|
|
# The code *may* be in copyreg._extension_registry, though, if
|
2003-02-04 13:49:36 -04:00
|
|
|
# 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:
|
2008-05-11 05:55:36 -03:00
|
|
|
copyreg.add_extension(mod, func, code)
|
|
|
|
copyreg.remove_extension(mod, func, code)
|
2003-02-04 13:49:36 -04:00
|
|
|
finally:
|
|
|
|
e.restore()
|
|
|
|
|
|
|
|
# Ensure invalid codes blow up.
|
2007-01-15 12:59:06 -04:00
|
|
|
for code in -1, 0, 0x80000000:
|
2008-05-11 05:55:36 -03:00
|
|
|
self.assertRaises(ValueError, copyreg.add_extension,
|
2003-02-04 13:49:36 -04:00
|
|
|
mod, func, code)
|
2001-05-22 17:38:44 -03:00
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
def test_slotnames(self):
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(copyreg._slotnames(WithoutSlots), [])
|
|
|
|
self.assertEqual(copyreg._slotnames(WithWeakref), [])
|
2006-04-21 07:40:58 -03:00
|
|
|
expected = ['_WithPrivate__spam']
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(copyreg._slotnames(WithPrivate), expected)
|
2017-08-04 05:45:00 -03:00
|
|
|
expected = ['_WithLeadingUnderscoreAndPrivate__spam']
|
|
|
|
self.assertEqual(copyreg._slotnames(_WithLeadingUnderscoreAndPrivate),
|
|
|
|
expected)
|
|
|
|
self.assertEqual(copyreg._slotnames(___), ['__spam'])
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(copyreg._slotnames(WithSingleString), ['spam'])
|
2006-04-21 07:40:58 -03:00
|
|
|
expected = ['eggs', 'spam']
|
|
|
|
expected.sort()
|
2008-05-11 05:55:36 -03:00
|
|
|
result = copyreg._slotnames(WithInherited)
|
2006-04-21 07:40:58 -03:00
|
|
|
result.sort()
|
2010-11-20 15:04:17 -04:00
|
|
|
self.assertEqual(result, expected)
|
2006-04-21 07:40:58 -03:00
|
|
|
|
|
|
|
|
2001-09-20 18:33:42 -03:00
|
|
|
if __name__ == "__main__":
|
2015-04-13 17:00:43 -03:00
|
|
|
unittest.main()
|