from test
import test_support
from test
.pickletester
import ExtensionSaver
class CopyRegTestCase(unittest
.TestCase
):
self
.assertRaises(TypeError, copy_reg
.pickle
,
def test_noncallable_reduce(self
):
self
.assertRaises(TypeError, copy_reg
.pickle
,
type(1), "not a callable")
def test_noncallable_constructor(self
):
self
.assertRaises(TypeError, copy_reg
.pickle
,
type(1), int, "not a callable")
self
.assertEquals(True, copy
.copy(True))
def test_extension_registry(self
):
mod
, func
, code
= 'junk1 ', ' junk2', 0xabcd
# Shouldn't be in registry now.
self
.assertRaises(ValueError, copy_reg
.remove_extension
,
copy_reg
.add_extension(mod
, func
, code
)
# Should be in the registry.
self
.assert_(copy_reg
._extension
_registry
[mod
, func
] == code
)
self
.assert_(copy_reg
._inverted
_registry
[code
] == (mod
, func
))
# Shouldn't be in the cache.
self
.assert_(code
not in copy_reg
._extension
_cache
)
# Redundant registration should be OK.
copy_reg
.add_extension(mod
, func
, code
) # shouldn't blow up
self
.assertRaises(ValueError, copy_reg
.add_extension
,
self
.assertRaises(ValueError, copy_reg
.remove_extension
,
# Conflicting module name.
self
.assertRaises(ValueError, copy_reg
.add_extension
,
self
.assertRaises(ValueError, copy_reg
.remove_extension
,
# Conflicting function name.
self
.assertRaises(ValueError, copy_reg
.add_extension
,
self
.assertRaises(ValueError, copy_reg
.remove_extension
,
# Can't remove one that isn't registered at all.
if code
+ 1 not in copy_reg
._inverted
_registry
:
self
.assertRaises(ValueError, copy_reg
.remove_extension
,
mod
[1:], func
[1:], code
+ 1)
# Shouldn't be there anymore.
self
.assert_((mod
, func
) not in copy_reg
._extension
_registry
)
# The code *may* be in copy_reg._extension_registry, though, if
# we happened to pick on a registered code. So don't check for
# Check valid codes at the limits.
for code
in 1, 0x7fffffff:
copy_reg
.add_extension(mod
, func
, code
)
copy_reg
.remove_extension(mod
, func
, code
)
# Ensure invalid codes blow up.
for code
in -1, 0, 0x80000000L
:
self
.assertRaises(ValueError, copy_reg
.add_extension
,
test_support
.run_unittest(CopyRegTestCase
)
if __name__
== "__main__":