annotate Lib/test/test_copy_reg.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents f419b2e96a98
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
1 import copy_reg
17919
ce69fb9c8d56 Convert copy_reg test to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15160
diff changeset
2 import unittest
27686
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27677
diff changeset
3
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
4 from test import test_support
27686
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27677
diff changeset
5 from test.pickletester import ExtensionSaver
15160
3c21064e8214 Test the exception-raising for error cases in copy_reg.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6
3c21064e8214 Test the exception-raising for error cases in copy_reg.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
7 class C:
3c21064e8214 Test the exception-raising for error cases in copy_reg.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
8 pass
3c21064e8214 Test the exception-raising for error cases in copy_reg.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
9
3c21064e8214 Test the exception-raising for error cases in copy_reg.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10
37341
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
11 class WithoutSlots(object):
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
12 pass
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
13
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
14 class WithWeakref(object):
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
15 __slots__ = ('__weakref__',)
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
16
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
17 class WithPrivate(object):
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
18 __slots__ = ('__spam',)
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
19
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
20 class WithSingleString(object):
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
21 __slots__ = 'spam'
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
22
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
23 class WithInherited(WithSingleString):
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
24 __slots__ = ('eggs',)
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
25
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
26
17919
ce69fb9c8d56 Convert copy_reg test to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15160
diff changeset
27 class CopyRegTestCase(unittest.TestCase):
ce69fb9c8d56 Convert copy_reg test to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15160
diff changeset
28
ce69fb9c8d56 Convert copy_reg test to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15160
diff changeset
29 def test_class(self):
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
30 self.assertRaises(TypeError, copy_reg.pickle,
17919
ce69fb9c8d56 Convert copy_reg test to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15160
diff changeset
31 C, None, None)
ce69fb9c8d56 Convert copy_reg test to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15160
diff changeset
32
ce69fb9c8d56 Convert copy_reg test to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15160
diff changeset
33 def test_noncallable_reduce(self):
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
34 self.assertRaises(TypeError, copy_reg.pickle,
17919
ce69fb9c8d56 Convert copy_reg test to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15160
diff changeset
35 type(1), "not a callable")
ce69fb9c8d56 Convert copy_reg test to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15160
diff changeset
36
ce69fb9c8d56 Convert copy_reg test to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15160
diff changeset
37 def test_noncallable_constructor(self):
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
38 self.assertRaises(TypeError, copy_reg.pickle,
17919
ce69fb9c8d56 Convert copy_reg test to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 15160
diff changeset
39 type(1), int, "not a callable")
15160
3c21064e8214 Test the exception-raising for error cases in copy_reg.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40
27397
eaf3f80700b3 Test that True can be copied.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
41 def test_bool(self):
eaf3f80700b3 Test that True can be copied.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
42 import copy
eaf3f80700b3 Test that True can be copied.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
43 self.assertEquals(True, copy.copy(True))
eaf3f80700b3 Test that True can be copied.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
44
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
45 def test_extension_registry(self):
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
46 mod, func, code = 'junk1 ', ' junk2', 0xabcd
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
47 e = ExtensionSaver(code)
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
48 try:
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
49 # Shouldn't be in registry now.
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
50 self.assertRaises(ValueError, copy_reg.remove_extension,
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
51 mod, func, code)
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
52 copy_reg.add_extension(mod, func, code)
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
53 # Should be in the registry.
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
54 self.assert_(copy_reg._extension_registry[mod, func] == code)
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
55 self.assert_(copy_reg._inverted_registry[code] == (mod, func))
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
56 # Shouldn't be in the cache.
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
57 self.assert_(code not in copy_reg._extension_cache)
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
58 # Redundant registration should be OK.
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
59 copy_reg.add_extension(mod, func, code) # shouldn't blow up
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
60 # Conflicting code.
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
61 self.assertRaises(ValueError, copy_reg.add_extension,
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
62 mod, func, code + 1)
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
63 self.assertRaises(ValueError, copy_reg.remove_extension,
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
64 mod, func, code + 1)
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
65 # Conflicting module name.
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
66 self.assertRaises(ValueError, copy_reg.add_extension,
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
67 mod[1:], func, code )
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
68 self.assertRaises(ValueError, copy_reg.remove_extension,
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
69 mod[1:], func, code )
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
70 # Conflicting function name.
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
71 self.assertRaises(ValueError, copy_reg.add_extension,
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
72 mod, func[1:], code)
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
73 self.assertRaises(ValueError, copy_reg.remove_extension,
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
74 mod, func[1:], code)
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
75 # Can't remove one that isn't registered at all.
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
76 if code + 1 not in copy_reg._inverted_registry:
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
77 self.assertRaises(ValueError, copy_reg.remove_extension,
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
78 mod[1:], func[1:], code + 1)
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
79
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
80 finally:
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
81 e.restore()
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
82
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
83 # Shouldn't be there anymore.
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
84 self.assert_((mod, func) not in copy_reg._extension_registry)
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
85 # The code *may* be in copy_reg._extension_registry, though, if
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
86 # we happened to pick on a registered code. So don't check for
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
87 # that.
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
88
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
89 # Check valid codes at the limits.
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
90 for code in 1, 0x7fffffff:
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
91 e = ExtensionSaver(code)
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
92 try:
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
93 copy_reg.add_extension(mod, func, code)
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
94 copy_reg.remove_extension(mod, func, code)
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
95 finally:
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
96 e.restore()
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
97
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
98 # Ensure invalid codes blow up.
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
99 for code in -1, 0, 0x80000000L:
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
100 self.assertRaises(ValueError, copy_reg.add_extension,
27677
5199f7ea6746 Added basic tests of copy_reg's extension registry.
Tim Peters <tim.peters@gmail.com>
parents: 27397
diff changeset
101 mod, func, code)
15160
3c21064e8214 Test the exception-raising for error cases in copy_reg.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
102
37341
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
103 def test_slotnames(self):
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
104 self.assertEquals(copy_reg._slotnames(WithoutSlots), [])
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
105 self.assertEquals(copy_reg._slotnames(WithWeakref), [])
37341
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
106 expected = ['_WithPrivate__spam']
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
107 self.assertEquals(copy_reg._slotnames(WithPrivate), expected)
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
108 self.assertEquals(copy_reg._slotnames(WithSingleString), ['spam'])
37341
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
109 expected = ['eggs', 'spam']
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
110 expected.sort()
47189
f419b2e96a98 Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents: 46865
diff changeset
111 result = copy_reg._slotnames(WithInherited)
37341
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
112 result.sort()
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
113 self.assertEquals(result, expected)
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
114
f330048d8bdf Patch #1462313, bug #1443328: the pickle modules now can handle classes
Georg Brandl <georg@python.org>
parents: 27686
diff changeset
115
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17919
diff changeset
116 def test_main():
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17919
diff changeset
117 test_support.run_unittest(CopyRegTestCase)
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17919
diff changeset
118
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17919
diff changeset
119
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17919
diff changeset
120 if __name__ == "__main__":
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17919
diff changeset
121 test_main()