annotate Lib/test/pickletester.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents a6d70861bf6d
children 680959a3ae2e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
1 import unittest
27591
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
2 import pickle
27864
b4eb34372bdb Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
Tim Peters <tim.peters@gmail.com>
parents: 27825
diff changeset
3 import cPickle
55430
868b4e639b5a Merged revisions 74189 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54344
diff changeset
4 import StringIO
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
5 import cStringIO
27645
b66acbe29575 Proper testing of proto 2 in part requires checking that the new opcodes
Tim Peters <tim.peters@gmail.com>
parents: 27615
diff changeset
6 import pickletools
27686
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
7 import copy_reg
27591
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
8
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
9 from test.test_support import TestFailed, have_unicode, TESTFN
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
10
27585
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
11 # Tests that try a number of pickle protocols should have a
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
12 # for proto in protocols:
27864
b4eb34372bdb Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
Tim Peters <tim.peters@gmail.com>
parents: 27825
diff changeset
13 # kind of outer loop.
b4eb34372bdb Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
Tim Peters <tim.peters@gmail.com>
parents: 27825
diff changeset
14 assert pickle.HIGHEST_PROTOCOL == cPickle.HIGHEST_PROTOCOL == 2
b4eb34372bdb Added a HIGHEST_PROTOCOL module attribute to pickle and cPickle.
Tim Peters <tim.peters@gmail.com>
parents: 27825
diff changeset
15 protocols = range(pickle.HIGHEST_PROTOCOL + 1)
27585
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
16
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
17 # Copy of test.test_support.run_with_locale. This is needed to support Python
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
18 # 2.4, which didn't include it. This is all to support test_xpickle, which
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
19 # bounces pickled objects through older Python versions to test backwards
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
20 # compatibility.
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
21 def run_with_locale(catstr, *locales):
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
22 def decorator(func):
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
23 def inner(*args, **kwds):
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
24 try:
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
25 import locale
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
26 category = getattr(locale, catstr)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
27 orig_locale = locale.setlocale(category)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
28 except AttributeError:
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
29 # if the test author gives us an invalid category string
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
30 raise
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
31 except:
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
32 # cannot retrieve original locale, so do nothing
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
33 locale = orig_locale = None
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
34 else:
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
35 for loc in locales:
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
36 try:
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
37 locale.setlocale(category, loc)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
38 break
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
39 except:
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
40 pass
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
41
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
42 # now run the function, resetting the locale on exceptions
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
43 try:
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
44 return func(*args, **kwds)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
45 finally:
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
46 if locale and orig_locale:
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
47 locale.setlocale(category, orig_locale)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
48 inner.func_name = func.func_name
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
49 inner.__doc__ = func.__doc__
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
50 return inner
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
51 return decorator
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
52
27657
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
53
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
54 # Return True if opcode code appears in the pickle, else False.
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
55 def opcode_in_pickle(code, pickle):
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
56 for op, dummy, dummy in pickletools.genops(pickle):
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
57 if op.code == code:
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
58 return True
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
59 return False
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
60
27813
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
61 # Return the number of times opcode code appears in pickle.
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
62 def count_opcode(code, pickle):
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
63 n = 0
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
64 for op, dummy, dummy in pickletools.genops(pickle):
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
65 if op.code == code:
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
66 n += 1
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
67 return n
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
68
27686
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
69 # We can't very well test the extension registry without putting known stuff
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
70 # in it, but we have to be careful to restore its original state. Code
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
71 # should do this:
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
72 #
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
73 # e = ExtensionSaver(extension_code)
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
74 # try:
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
75 # fiddle w/ the extension registry's stuff for extension_code
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
76 # finally:
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
77 # e.restore()
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
78
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
79 class ExtensionSaver:
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
80 # Remember current registration for code (if any), and remove it (if
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
81 # there is one).
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
82 def __init__(self, code):
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
83 self.code = code
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
84 if code in copy_reg._inverted_registry:
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
85 self.pair = copy_reg._inverted_registry[code]
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
86 copy_reg.remove_extension(self.pair[0], self.pair[1], code)
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
87 else:
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
88 self.pair = None
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
89
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
90 # Restore previous registration for code.
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
91 def restore(self):
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
92 code = self.code
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
93 curpair = copy_reg._inverted_registry.get(code)
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
94 if curpair is not None:
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
95 copy_reg.remove_extension(curpair[0], curpair[1], code)
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
96 pair = self.pair
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
97 if pair is not None:
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
98 copy_reg.add_extension(pair[0], pair[1], code)
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
99
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
100 class C:
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
101 def __cmp__(self, other):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
102 return cmp(self.__dict__, other.__dict__)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
103
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
104 import __main__
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
105 __main__.C = C
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
106 C.__module__ = "__main__"
17365
11e8bb4d845d Test full range of native ints. This exposes two more binary pickle
Tim Peters <tim.peters@gmail.com>
parents: 17358
diff changeset
107
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
108 class myint(int):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
109 def __init__(self, x):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
110 self.str = str(x)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
111
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
112 class initarg(C):
21481
003a985cab95 The test using class initarg failed, because it was lacking a
Guido van Rossum <guido@python.org>
parents: 20547
diff changeset
113
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
114 def __init__(self, a, b):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
115 self.a = a
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
116 self.b = b
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
117
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
118 def __getinitargs__(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
119 return self.a, self.b
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
120
21488
bb168de50a03 Add test for pickling new-style class with custom metaclass.
Guido van Rossum <guido@python.org>
parents: 21481
diff changeset
121 class metaclass(type):
bb168de50a03 Add test for pickling new-style class with custom metaclass.
Guido van Rossum <guido@python.org>
parents: 21481
diff changeset
122 pass
bb168de50a03 Add test for pickling new-style class with custom metaclass.
Guido van Rossum <guido@python.org>
parents: 21481
diff changeset
123
bb168de50a03 Add test for pickling new-style class with custom metaclass.
Guido van Rossum <guido@python.org>
parents: 21481
diff changeset
124 class use_metaclass(object):
bb168de50a03 Add test for pickling new-style class with custom metaclass.
Guido van Rossum <guido@python.org>
parents: 21481
diff changeset
125 __metaclass__ = metaclass
bb168de50a03 Add test for pickling new-style class with custom metaclass.
Guido van Rossum <guido@python.org>
parents: 21481
diff changeset
126
72641
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
127 class pickling_metaclass(type):
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
128 def __eq__(self, other):
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
129 return (type(self) == type(other) and
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
130 self.reduce_args == other.reduce_args)
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
131
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
132 def __reduce__(self):
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
133 return (create_dynamic_class, self.reduce_args)
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
134
73406
a6d70861bf6d Silence a couple of warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72641
diff changeset
135 __hash__ = None
a6d70861bf6d Silence a couple of warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 72641
diff changeset
136
72641
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
137 def create_dynamic_class(name, bases):
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
138 result = pickling_metaclass(name, bases, dict())
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
139 result.reduce_args = (name, bases)
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
140 return result
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
141
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
142 # DATA0 .. DATA2 are the pickles we expect under the various protocols, for
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
143 # the object returned by create_data().
27585
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
144
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
145 # break into multiple strings to avoid confusing font-lock-mode
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
146 DATA0 = """(lp1
16133
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
147 I0
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
148 aL1L
17358
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
149 aF2
16133
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
150 ac__builtin__
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
151 complex
17358
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
152 p2
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
153 """ + \
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
154 """(F3
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
155 F0
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
156 tRp3
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
157 aI1
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
158 aI-1
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
159 aI255
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
160 aI-255
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
161 aI-256
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
162 aI65535
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
163 aI-65535
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
164 aI-65536
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
165 aI2147483647
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
166 aI-2147483647
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
167 aI-2147483648
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
168 a""" + \
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
169 """(S'abc'
16133
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
170 p4
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
171 g4
17358
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
172 """ + \
17538
162fa6b1366f Change the test data to ask for class C from module __main__ rather
Guido van Rossum <guido@python.org>
parents: 17365
diff changeset
173 """(i__main__
16133
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
174 C
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
175 p5
17358
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
176 """ + \
16133
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
177 """(dp6
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
178 S'foo'
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
179 p7
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
180 I1
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
181 sS'bar'
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
182 p8
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
183 I2
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
184 sbg5
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
185 tp9
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
186 ag9
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
187 aI5
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
188 a.
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
189 """
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
190
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
191 # Disassembly of DATA0.
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
192 DATA0_DIS = """\
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
193 0: ( MARK
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
194 1: l LIST (MARK at 0)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
195 2: p PUT 1
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
196 5: I INT 0
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
197 8: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
198 9: L LONG 1L
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
199 13: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
200 14: F FLOAT 2.0
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
201 17: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
202 18: c GLOBAL '__builtin__ complex'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
203 39: p PUT 2
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
204 42: ( MARK
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
205 43: F FLOAT 3.0
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
206 46: F FLOAT 0.0
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
207 49: t TUPLE (MARK at 42)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
208 50: R REDUCE
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
209 51: p PUT 3
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
210 54: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
211 55: I INT 1
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
212 58: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
213 59: I INT -1
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
214 63: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
215 64: I INT 255
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
216 69: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
217 70: I INT -255
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
218 76: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
219 77: I INT -256
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
220 83: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
221 84: I INT 65535
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
222 91: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
223 92: I INT -65535
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
224 100: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
225 101: I INT -65536
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
226 109: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
227 110: I INT 2147483647
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
228 122: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
229 123: I INT -2147483647
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
230 136: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
231 137: I INT -2147483648
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
232 150: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
233 151: ( MARK
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
234 152: S STRING 'abc'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
235 159: p PUT 4
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
236 162: g GET 4
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
237 165: ( MARK
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
238 166: i INST '__main__ C' (MARK at 165)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
239 178: p PUT 5
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
240 181: ( MARK
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
241 182: d DICT (MARK at 181)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
242 183: p PUT 6
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
243 186: S STRING 'foo'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
244 193: p PUT 7
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
245 196: I INT 1
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
246 199: s SETITEM
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
247 200: S STRING 'bar'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
248 207: p PUT 8
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
249 210: I INT 2
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
250 213: s SETITEM
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
251 214: b BUILD
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
252 215: g GET 5
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
253 218: t TUPLE (MARK at 151)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
254 219: p PUT 9
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
255 222: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
256 223: g GET 9
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
257 226: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
258 227: I INT 5
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
259 230: a APPEND
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
260 231: . STOP
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
261 highest protocol among opcodes = 0
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
262 """
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
263
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
264 DATA1 = (']q\x01(K\x00L1L\nG@\x00\x00\x00\x00\x00\x00\x00'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
265 'c__builtin__\ncomplex\nq\x02(G@\x08\x00\x00\x00\x00\x00'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
266 '\x00G\x00\x00\x00\x00\x00\x00\x00\x00tRq\x03K\x01J\xff\xff'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
267 '\xff\xffK\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xff'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
268 'J\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
269 '\x00\x80J\x00\x00\x00\x80(U\x03abcq\x04h\x04(c__main__\n'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
270 'C\nq\x05oq\x06}q\x07(U\x03fooq\x08K\x01U\x03barq\tK\x02ubh'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
271 '\x06tq\nh\nK\x05e.'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
272 )
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
273
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
274 # Disassembly of DATA1.
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
275 DATA1_DIS = """\
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
276 0: ] EMPTY_LIST
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
277 1: q BINPUT 1
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
278 3: ( MARK
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
279 4: K BININT1 0
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
280 6: L LONG 1L
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
281 10: G BINFLOAT 2.0
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
282 19: c GLOBAL '__builtin__ complex'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
283 40: q BINPUT 2
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
284 42: ( MARK
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
285 43: G BINFLOAT 3.0
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
286 52: G BINFLOAT 0.0
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
287 61: t TUPLE (MARK at 42)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
288 62: R REDUCE
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
289 63: q BINPUT 3
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
290 65: K BININT1 1
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
291 67: J BININT -1
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
292 72: K BININT1 255
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
293 74: J BININT -255
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
294 79: J BININT -256
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
295 84: M BININT2 65535
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
296 87: J BININT -65535
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
297 92: J BININT -65536
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
298 97: J BININT 2147483647
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
299 102: J BININT -2147483647
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
300 107: J BININT -2147483648
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
301 112: ( MARK
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
302 113: U SHORT_BINSTRING 'abc'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
303 118: q BINPUT 4
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
304 120: h BINGET 4
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
305 122: ( MARK
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
306 123: c GLOBAL '__main__ C'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
307 135: q BINPUT 5
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
308 137: o OBJ (MARK at 122)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
309 138: q BINPUT 6
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
310 140: } EMPTY_DICT
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
311 141: q BINPUT 7
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
312 143: ( MARK
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
313 144: U SHORT_BINSTRING 'foo'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
314 149: q BINPUT 8
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
315 151: K BININT1 1
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
316 153: U SHORT_BINSTRING 'bar'
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
317 158: q BINPUT 9
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
318 160: K BININT1 2
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
319 162: u SETITEMS (MARK at 143)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
320 163: b BUILD
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
321 164: h BINGET 6
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
322 166: t TUPLE (MARK at 112)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
323 167: q BINPUT 10
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
324 169: h BINGET 10
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
325 171: K BININT1 5
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
326 173: e APPENDS (MARK at 3)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
327 174: . STOP
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
328 highest protocol among opcodes = 1
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
329 """
20547
a12ccdbce8da Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20444
diff changeset
330
28132
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
331 DATA2 = ('\x80\x02]q\x01(K\x00\x8a\x01\x01G@\x00\x00\x00\x00\x00\x00\x00'
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
332 'c__builtin__\ncomplex\nq\x02G@\x08\x00\x00\x00\x00\x00\x00G\x00'
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
333 '\x00\x00\x00\x00\x00\x00\x00\x86Rq\x03K\x01J\xff\xff\xff\xffK'
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
334 '\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xffJ\x01\x00\xff\xff'
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
335 'J\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00\x00\x80J\x00\x00\x00'
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
336 '\x80(U\x03abcq\x04h\x04(c__main__\nC\nq\x05oq\x06}q\x07(U\x03foo'
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
337 'q\x08K\x01U\x03barq\tK\x02ubh\x06tq\nh\nK\x05e.')
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
338
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
339 # Disassembly of DATA2.
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
340 DATA2_DIS = """\
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
341 0: \x80 PROTO 2
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
342 2: ] EMPTY_LIST
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
343 3: q BINPUT 1
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
344 5: ( MARK
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
345 6: K BININT1 0
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
346 8: \x8a LONG1 1L
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
347 11: G BINFLOAT 2.0
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
348 20: c GLOBAL '__builtin__ complex'
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
349 41: q BINPUT 2
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
350 43: G BINFLOAT 3.0
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
351 52: G BINFLOAT 0.0
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
352 61: \x86 TUPLE2
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
353 62: R REDUCE
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
354 63: q BINPUT 3
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
355 65: K BININT1 1
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
356 67: J BININT -1
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
357 72: K BININT1 255
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
358 74: J BININT -255
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
359 79: J BININT -256
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
360 84: M BININT2 65535
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
361 87: J BININT -65535
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
362 92: J BININT -65536
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
363 97: J BININT 2147483647
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
364 102: J BININT -2147483647
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
365 107: J BININT -2147483648
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
366 112: ( MARK
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
367 113: U SHORT_BINSTRING 'abc'
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
368 118: q BINPUT 4
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
369 120: h BINGET 4
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
370 122: ( MARK
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
371 123: c GLOBAL '__main__ C'
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
372 135: q BINPUT 5
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
373 137: o OBJ (MARK at 122)
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
374 138: q BINPUT 6
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
375 140: } EMPTY_DICT
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
376 141: q BINPUT 7
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
377 143: ( MARK
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
378 144: U SHORT_BINSTRING 'foo'
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
379 149: q BINPUT 8
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
380 151: K BININT1 1
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
381 153: U SHORT_BINSTRING 'bar'
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
382 158: q BINPUT 9
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
383 160: K BININT1 2
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
384 162: u SETITEMS (MARK at 143)
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
385 163: b BUILD
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
386 164: h BINGET 6
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
387 166: t TUPLE (MARK at 112)
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
388 167: q BINPUT 10
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
389 169: h BINGET 10
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
390 171: K BININT1 5
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
391 173: e APPENDS (MARK at 5)
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
392 174: . STOP
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
393 highest protocol among opcodes = 2
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
394 """
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
395
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
396 def create_data():
16133
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
397 c = C()
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
398 c.foo = 1
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
399 c.bar = 2
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
400 x = [0, 1L, 2.0, 3.0+0j]
17358
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
401 # Append some integer test cases at cPickle.c's internal size
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
402 # cutoffs.
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
403 uint1max = 0xff
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
404 uint2max = 0xffff
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
405 int4max = 0x7fffffff
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
406 x.extend([1, -1,
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
407 uint1max, -uint1max, -uint1max-1,
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
408 uint2max, -uint2max, -uint2max-1,
3aaedbac95b3 Pickles have a number of storage formats for various sizes and kinds of
Tim Peters <tim.peters@gmail.com>
parents: 17356
diff changeset
409 int4max, -int4max, -int4max-1])
16133
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
410 y = ('abc', 'abc', c, c)
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
411 x.append(y)
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
412 x.append(y)
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
413 x.append(5)
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
414 return x
17356
28cf0a7943cf No functional change -- just added whitespace in places so I could follow
Tim Peters <tim.peters@gmail.com>
parents: 16134
diff changeset
415
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
416 class AbstractPickleTests(unittest.TestCase):
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
417 # Subclass must define self.dumps, self.loads, self.error.
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
418
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
419 _testdata = create_data()
17356
28cf0a7943cf No functional change -- just added whitespace in places so I could follow
Tim Peters <tim.peters@gmail.com>
parents: 16134
diff changeset
420
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
421 def setUp(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
422 pass
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
423
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
424 def test_misc(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
425 # test various datatypes not tested by testdata
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
426 for proto in protocols:
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
427 x = myint(4)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
428 s = self.dumps(x, proto)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
429 y = self.loads(s)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
430 self.assertEqual(x, y)
17356
28cf0a7943cf No functional change -- just added whitespace in places so I could follow
Tim Peters <tim.peters@gmail.com>
parents: 16134
diff changeset
431
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
432 x = (1, ())
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
433 s = self.dumps(x, proto)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
434 y = self.loads(s)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
435 self.assertEqual(x, y)
17356
28cf0a7943cf No functional change -- just added whitespace in places so I could follow
Tim Peters <tim.peters@gmail.com>
parents: 16134
diff changeset
436
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
437 x = initarg(1, x)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
438 s = self.dumps(x, proto)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
439 y = self.loads(s)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
440 self.assertEqual(x, y)
17356
28cf0a7943cf No functional change -- just added whitespace in places so I could follow
Tim Peters <tim.peters@gmail.com>
parents: 16134
diff changeset
441
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
442 # XXX test __reduce__ protocol?
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
443
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
444 def test_roundtrip_equality(self):
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
445 expected = self._testdata
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
446 for proto in protocols:
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
447 s = self.dumps(expected, proto)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
448 got = self.loads(s)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
449 self.assertEqual(expected, got)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
450
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
451 def test_load_from_canned_string(self):
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
452 expected = self._testdata
28132
97b97e57190c test_load_from_canned_string(): Created a DATA2 string to test a canned
Tim Peters <tim.peters@gmail.com>
parents: 27951
diff changeset
453 for canned in DATA0, DATA1, DATA2:
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
454 got = self.loads(canned)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
455 self.assertEqual(expected, got)
17356
28cf0a7943cf No functional change -- just added whitespace in places so I could follow
Tim Peters <tim.peters@gmail.com>
parents: 16134
diff changeset
456
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
457 # There are gratuitous differences between pickles produced by
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
458 # pickle and cPickle, largely because cPickle starts PUT indices at
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
459 # 1 and pickle starts them at 0. See XXX comment in cPickle's put2() --
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
460 # there's a comment with an exclamation point there whose meaning
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
461 # is a mystery. cPickle also suppresses PUT for objects with a refcount
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
462 # of 1.
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
463 def dont_test_disassembly(self):
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
464 from pickletools import dis
17356
28cf0a7943cf No functional change -- just added whitespace in places so I could follow
Tim Peters <tim.peters@gmail.com>
parents: 16134
diff changeset
465
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
466 for proto, expected in (0, DATA0_DIS), (1, DATA1_DIS):
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
467 s = self.dumps(self._testdata, proto)
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
468 filelike = cStringIO.StringIO()
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
469 dis(s, out=filelike)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
470 got = filelike.getvalue()
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
471 self.assertEqual(expected, got)
16133
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
472
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
473 def test_recursive_list(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
474 l = []
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
475 l.append(l)
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
476 for proto in protocols:
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
477 s = self.dumps(l, proto)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
478 x = self.loads(s)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28135
diff changeset
479 self.assertEqual(len(x), 1)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54343
diff changeset
480 self.assertTrue(x is x[0])
17356
28cf0a7943cf No functional change -- just added whitespace in places so I could follow
Tim Peters <tim.peters@gmail.com>
parents: 16134
diff changeset
481
54344
d3daad0e7f6f Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
482 def test_recursive_tuple(self):
d3daad0e7f6f Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
483 t = ([],)
d3daad0e7f6f Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
484 t[0].append(t)
d3daad0e7f6f Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
485 for proto in protocols:
d3daad0e7f6f Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
486 s = self.dumps(t, proto)
d3daad0e7f6f Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
487 x = self.loads(s)
d3daad0e7f6f Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
488 self.assertEqual(len(x), 1)
d3daad0e7f6f Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
489 self.assertEqual(len(x[0]), 1)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54343
diff changeset
490 self.assertTrue(x is x[0][0])
54344
d3daad0e7f6f Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
491
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
492 def test_recursive_dict(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
493 d = {}
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
494 d[1] = d
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
495 for proto in protocols:
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
496 s = self.dumps(d, proto)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
497 x = self.loads(s)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28135
diff changeset
498 self.assertEqual(x.keys(), [1])
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54343
diff changeset
499 self.assertTrue(x[1] is x)
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
500
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
501 def test_recursive_inst(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
502 i = C()
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
503 i.attr = i
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
504 for proto in protocols:
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
505 s = self.dumps(i, 2)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
506 x = self.loads(s)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28135
diff changeset
507 self.assertEqual(dir(x), dir(i))
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54343
diff changeset
508 self.assertTrue(x.attr is x)
16133
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
509
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
510 def test_recursive_multi(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
511 l = []
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
512 d = {1:l}
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
513 i = C()
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
514 i.attr = d
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
515 l.append(i)
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
516 for proto in protocols:
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
517 s = self.dumps(l, proto)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
518 x = self.loads(s)
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28135
diff changeset
519 self.assertEqual(len(x), 1)
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28135
diff changeset
520 self.assertEqual(dir(x[0]), dir(i))
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 28135
diff changeset
521 self.assertEqual(x[0].attr.keys(), [1])
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54343
diff changeset
522 self.assertTrue(x[0].attr[1] is x)
16133
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
523
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
524 def test_garyp(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
525 self.assertRaises(self.error, self.loads, 'garyp')
16133
d48018b33e35 Reorganize pickle/cPickle testing so the tests pass regardless of the order
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
526
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
527 def test_insecure_strings(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
528 insecure = ["abc", "2 + 2", # not quoted
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24710
diff changeset
529 #"'abc' + 'def'", # not a single quoted string
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
530 "'abc", # quote is not closed
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
531 "'abc\"", # open quote and close quote don't match
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
532 "'abc' ?", # junk after close quote
24809
c75cdc471b22 Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents: 24808
diff changeset
533 "'\\'", # trailing backslash
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
534 # some tests of the quoting rules
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24710
diff changeset
535 #"'abc\"\''",
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24710
diff changeset
536 #"'\\\\a\'\'\'\\\'\\\\\''",
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
537 ]
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
538 for s in insecure:
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
539 buf = "S" + s + "\012p0\012."
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
540 self.assertRaises(ValueError, self.loads, buf)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
541
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 17538
diff changeset
542 if have_unicode:
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
543 def test_unicode(self):
50305
e673632e1885 Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50290
diff changeset
544 endcases = [u'', u'<\\u>', u'<\\\u1234>', u'<\n>',
e673632e1885 Merged revisions 67898,67904-67907,67912,67918,67920-67921,67923-67924,67927,67930,67932,67943 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50290
diff changeset
545 u'<\\>', u'<\\\U00012345>']
27585
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
546 for proto in protocols:
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
547 for u in endcases:
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
548 p = self.dumps(u, proto)
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
549 u2 = self.loads(p)
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
550 self.assertEqual(u2, u)
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
551
50290
d27024755593 Merged revisions 67934-67935 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 49609
diff changeset
552 def test_unicode_high_plane(self):
d27024755593 Merged revisions 67934-67935 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 49609
diff changeset
553 t = u'\U00012345'
d27024755593 Merged revisions 67934-67935 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 49609
diff changeset
554 for proto in protocols:
d27024755593 Merged revisions 67934-67935 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 49609
diff changeset
555 p = self.dumps(t, proto)
d27024755593 Merged revisions 67934-67935 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 49609
diff changeset
556 t2 = self.loads(p)
d27024755593 Merged revisions 67934-67935 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 49609
diff changeset
557 self.assertEqual(t2, t)
d27024755593 Merged revisions 67934-67935 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 49609
diff changeset
558
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
559 def test_ints(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
560 import sys
27585
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
561 for proto in protocols:
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
562 n = sys.maxint
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
563 while n:
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
564 for expected in (-n, n):
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
565 s = self.dumps(expected, proto)
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
566 n2 = self.loads(s)
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
567 self.assertEqual(expected, n2)
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
568 n = n >> 1
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
569
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
570 def test_maxint64(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
571 maxint64 = (1L << 63) - 1
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
572 data = 'I' + str(maxint64) + '\n.'
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
573 got = self.loads(data)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
574 self.assertEqual(got, maxint64)
17365
11e8bb4d845d Test full range of native ints. This exposes two more binary pickle
Tim Peters <tim.peters@gmail.com>
parents: 17358
diff changeset
575
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
576 # Try too with a bogus literal.
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
577 data = 'I' + str(maxint64) + 'JUNK\n.'
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
578 self.assertRaises(ValueError, self.loads, data)
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
579
27585
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
580 def test_long(self):
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
581 for proto in protocols:
27587
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
582 # 256 bytes is where LONG4 begins.
27585
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
583 for nbits in 1, 8, 8*254, 8*255, 8*256, 8*257:
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
584 nbase = 1L << nbits
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
585 for npos in nbase-1, nbase, nbase+1:
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
586 for n in npos, -npos:
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
587 pickle = self.dumps(n, proto)
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
588 got = self.loads(pickle)
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
589 self.assertEqual(n, got)
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
590 # Try a monster. This is quadratic-time in protos 0 & 1, so don't
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
591 # bother with those.
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
592 nbase = long("deadbeeffeedface", 16)
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
593 nbase += nbase << 1000000
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
594 for n in nbase, -nbase:
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
595 p = self.dumps(n, 2)
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
596 got = self.loads(p)
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
597 self.assertEqual(n, got)
a05814184e8c cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents: 27573
diff changeset
598
51020
0139648526d9 Merged revisions 68903,68906 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50305
diff changeset
599 def test_float(self):
0139648526d9 Merged revisions 68903,68906 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50305
diff changeset
600 test_values = [0.0, 4.94e-324, 1e-310, 7e-308, 6.626e-34, 0.1, 0.5,
0139648526d9 Merged revisions 68903,68906 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50305
diff changeset
601 3.14, 263.44582062374053, 6.022e23, 1e30]
0139648526d9 Merged revisions 68903,68906 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50305
diff changeset
602 test_values = test_values + [-x for x in test_values]
0139648526d9 Merged revisions 68903,68906 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50305
diff changeset
603 for proto in protocols:
0139648526d9 Merged revisions 68903,68906 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50305
diff changeset
604 for value in test_values:
0139648526d9 Merged revisions 68903,68906 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50305
diff changeset
605 pickle = self.dumps(value, proto)
0139648526d9 Merged revisions 68903,68906 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50305
diff changeset
606 got = self.loads(pickle)
0139648526d9 Merged revisions 68903,68906 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50305
diff changeset
607 self.assertEqual(value, got)
0139648526d9 Merged revisions 68903,68906 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50305
diff changeset
608
38055
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 32829
diff changeset
609 @run_with_locale('LC_ALL', 'de_DE', 'fr_FR')
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 32829
diff changeset
610 def test_float_format(self):
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 32829
diff changeset
611 # make sure that floats are formatted locale independent
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 32829
diff changeset
612 self.assertEqual(self.dumps(1.2)[0:3], 'F1.')
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 32829
diff changeset
613
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
614 def test_reduce(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
615 pass
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
616
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
617 def test_getinitargs(self):
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
618 pass
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
619
21488
bb168de50a03 Add test for pickling new-style class with custom metaclass.
Guido van Rossum <guido@python.org>
parents: 21481
diff changeset
620 def test_metaclass(self):
bb168de50a03 Add test for pickling new-style class with custom metaclass.
Guido van Rossum <guido@python.org>
parents: 21481
diff changeset
621 a = use_metaclass()
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
622 for proto in protocols:
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
623 s = self.dumps(a, proto)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
624 b = self.loads(s)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
625 self.assertEqual(a.__class__, b.__class__)
21488
bb168de50a03 Add test for pickling new-style class with custom metaclass.
Guido van Rossum <guido@python.org>
parents: 21481
diff changeset
626
72641
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
627 def test_dynamic_class(self):
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
628 a = create_dynamic_class("my_dynamic_class", (object,))
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
629 copy_reg.pickle(pickling_metaclass, pickling_metaclass.__reduce__)
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
630 for proto in protocols:
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
631 s = self.dumps(a, proto)
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
632 b = self.loads(s)
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
633 self.assertEqual(a, b)
64053bd79590 Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 68256
diff changeset
634
22138
ed2a17a376d0 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21488
diff changeset
635 def test_structseq(self):
ed2a17a376d0 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21488
diff changeset
636 import time
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
637 import os
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
638
22138
ed2a17a376d0 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21488
diff changeset
639 t = time.localtime()
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
640 for proto in protocols:
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
641 s = self.dumps(t, proto)
22165
22c66587c452 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22138
diff changeset
642 u = self.loads(s)
22c66587c452 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 22138
diff changeset
643 self.assertEqual(t, u)
27596
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
644 if hasattr(os, "stat"):
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
645 t = os.stat(os.curdir)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
646 s = self.dumps(t, proto)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
647 u = self.loads(s)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
648 self.assertEqual(t, u)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
649 if hasattr(os, "statvfs"):
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
650 t = os.statvfs(os.curdir)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
651 s = self.dumps(t, proto)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
652 u = self.loads(s)
72228a25daaa Beefed up the tests by putting in more "for proto in protocols:" outer
Tim Peters <tim.peters@gmail.com>
parents: 27591
diff changeset
653 self.assertEqual(t, u)
22138
ed2a17a376d0 backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 21488
diff changeset
654
27456
115ee40518a8 First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents: 26202
diff changeset
655 # Tests for protocol 2
115ee40518a8 First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents: 26202
diff changeset
656
27591
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
657 def test_proto(self):
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
658 build_none = pickle.NONE + pickle.STOP
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
659 for proto in protocols:
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
660 expected = build_none
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
661 if proto >= 2:
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
662 expected = pickle.PROTO + chr(proto) + expected
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
663 p = self.dumps(None, proto)
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
664 self.assertEqual(p, expected)
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
665
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
666 oob = protocols[-1] + 1 # a future protocol
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
667 badpickle = pickle.PROTO + chr(oob) + build_none
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
668 try:
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
669 self.loads(badpickle)
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
670 except ValueError, detail:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54343
diff changeset
671 self.assertTrue(str(detail).startswith(
27591
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
672 "unsupported pickle protocol"))
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
673 else:
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
674 self.fail("expected bad protocol number to raise ValueError")
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
675
27456
115ee40518a8 First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents: 26202
diff changeset
676 def test_long1(self):
115ee40518a8 First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents: 26202
diff changeset
677 x = 12345678910111213141516178920L
27655
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
678 for proto in protocols:
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
679 s = self.dumps(x, proto)
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
680 y = self.loads(s)
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
681 self.assertEqual(x, y)
27657
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
682 self.assertEqual(opcode_in_pickle(pickle.LONG1, s), proto >= 2)
27456
115ee40518a8 First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents: 26202
diff changeset
683
115ee40518a8 First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents: 26202
diff changeset
684 def test_long4(self):
115ee40518a8 First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents: 26202
diff changeset
685 x = 12345678910111213141516178920L << (256*8)
27655
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
686 for proto in protocols:
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
687 s = self.dumps(x, proto)
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
688 y = self.loads(s)
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
689 self.assertEqual(x, y)
27657
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
690 self.assertEqual(opcode_in_pickle(pickle.LONG4, s), proto >= 2)
27456
115ee40518a8 First baby steps towards implementing protocol 2: PROTO, LONG1 and LONG4.
Guido van Rossum <guido@python.org>
parents: 26202
diff changeset
691
27459
ea480406cc65 More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents: 27456
diff changeset
692 def test_short_tuples(self):
27615
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
693 # Map (proto, len(tuple)) to expected opcode.
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
694 expected_opcode = {(0, 0): pickle.TUPLE,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
695 (0, 1): pickle.TUPLE,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
696 (0, 2): pickle.TUPLE,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
697 (0, 3): pickle.TUPLE,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
698 (0, 4): pickle.TUPLE,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
699
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
700 (1, 0): pickle.EMPTY_TUPLE,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
701 (1, 1): pickle.TUPLE,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
702 (1, 2): pickle.TUPLE,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
703 (1, 3): pickle.TUPLE,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
704 (1, 4): pickle.TUPLE,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
705
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
706 (2, 0): pickle.EMPTY_TUPLE,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
707 (2, 1): pickle.TUPLE1,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
708 (2, 2): pickle.TUPLE2,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
709 (2, 3): pickle.TUPLE3,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
710 (2, 4): pickle.TUPLE,
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
711 }
27459
ea480406cc65 More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents: 27456
diff changeset
712 a = ()
27460
0554b7c5f6d7 Shouldn't test short tuples with all items equal -- one potential bug
Guido van Rossum <guido@python.org>
parents: 27459
diff changeset
713 b = (1,)
0554b7c5f6d7 Shouldn't test short tuples with all items equal -- one potential bug
Guido van Rossum <guido@python.org>
parents: 27459
diff changeset
714 c = (1, 2)
0554b7c5f6d7 Shouldn't test short tuples with all items equal -- one potential bug
Guido van Rossum <guido@python.org>
parents: 27459
diff changeset
715 d = (1, 2, 3)
0554b7c5f6d7 Shouldn't test short tuples with all items equal -- one potential bug
Guido van Rossum <guido@python.org>
parents: 27459
diff changeset
716 e = (1, 2, 3, 4)
27591
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
717 for proto in protocols:
27459
ea480406cc65 More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents: 27456
diff changeset
718 for x in a, b, c, d, e:
ea480406cc65 More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents: 27456
diff changeset
719 s = self.dumps(x, proto)
ea480406cc65 More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents: 27456
diff changeset
720 y = self.loads(s)
ea480406cc65 More protocol 2: TUPLE1, TUPLE2, TUPLE3.
Guido van Rossum <guido@python.org>
parents: 27456
diff changeset
721 self.assertEqual(x, y, (proto, x, s, y))
27615
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
722 expected = expected_opcode[proto, len(x)]
27657
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
723 self.assertEqual(opcode_in_pickle(expected, s), True)
27615
45ad77a43d87 cPickle support for TUPLE[123]. Incidentally plugged several undetected
Tim Peters <tim.peters@gmail.com>
parents: 27599
diff changeset
724
27461
883af28b9a0d OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents: 27460
diff changeset
725 def test_singletons(self):
27655
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
726 # Map (proto, singleton) to expected opcode.
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
727 expected_opcode = {(0, None): pickle.NONE,
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
728 (1, None): pickle.NONE,
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
729 (2, None): pickle.NONE,
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
730
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
731 (0, True): pickle.INT,
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
732 (1, True): pickle.INT,
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
733 (2, True): pickle.NEWTRUE,
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
734
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
735 (0, False): pickle.INT,
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
736 (1, False): pickle.INT,
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
737 (2, False): pickle.NEWFALSE,
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
738 }
27591
d6b65e3855a2 Add cPickle support for PROTO. Duplicated PROTO/LONG1/LONG4 code in
Tim Peters <tim.peters@gmail.com>
parents: 27587
diff changeset
739 for proto in protocols:
27461
883af28b9a0d OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents: 27460
diff changeset
740 for x in None, False, True:
883af28b9a0d OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents: 27460
diff changeset
741 s = self.dumps(x, proto)
883af28b9a0d OK, this is really the last one tonight!
Guido van Rossum <guido@python.org>
parents: 27460
diff changeset
742 y = self.loads(s)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54343
diff changeset
743 self.assertTrue(x is y, (proto, x, s, y))
27655
e11e976fee78 Do a better job of testing that opcodes aren't generated under protocols
Tim Peters <tim.peters@gmail.com>
parents: 27645
diff changeset
744 expected = expected_opcode[proto, x]
27657
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
745 self.assertEqual(opcode_in_pickle(expected, s), True)
27599
d158d10ef4ae Implemented proto 2 NEWTRUE and NEWFALSE in cPickle.
Tim Peters <tim.peters@gmail.com>
parents: 27596
diff changeset
746
27482
7c8dfd3f4ae9 Some experimental support for generating NEWOBJ with proto=2, and
Guido van Rossum <guido@python.org>
parents: 27461
diff changeset
747 def test_newobj_tuple(self):
27486
e9578747f43d The default __reduce__ on the base object type obscured any
Guido van Rossum <guido@python.org>
parents: 27482
diff changeset
748 x = MyTuple([1, 2, 3])
e9578747f43d The default __reduce__ on the base object type obscured any
Guido van Rossum <guido@python.org>
parents: 27482
diff changeset
749 x.foo = 42
e9578747f43d The default __reduce__ on the base object type obscured any
Guido van Rossum <guido@python.org>
parents: 27482
diff changeset
750 x.bar = "hello"
27659
0bbbcacc05fb test_newobj_tuple(), test_newobj_list(): These tests should work under
Tim Peters <tim.peters@gmail.com>
parents: 27658
diff changeset
751 for proto in protocols:
0bbbcacc05fb test_newobj_tuple(), test_newobj_list(): These tests should work under
Tim Peters <tim.peters@gmail.com>
parents: 27658
diff changeset
752 s = self.dumps(x, proto)
0bbbcacc05fb test_newobj_tuple(), test_newobj_list(): These tests should work under
Tim Peters <tim.peters@gmail.com>
parents: 27658
diff changeset
753 y = self.loads(s)
0bbbcacc05fb test_newobj_tuple(), test_newobj_list(): These tests should work under
Tim Peters <tim.peters@gmail.com>
parents: 27658
diff changeset
754 self.assertEqual(tuple(x), tuple(y))
0bbbcacc05fb test_newobj_tuple(), test_newobj_list(): These tests should work under
Tim Peters <tim.peters@gmail.com>
parents: 27658
diff changeset
755 self.assertEqual(x.__dict__, y.__dict__)
27482
7c8dfd3f4ae9 Some experimental support for generating NEWOBJ with proto=2, and
Guido van Rossum <guido@python.org>
parents: 27461
diff changeset
756
7c8dfd3f4ae9 Some experimental support for generating NEWOBJ with proto=2, and
Guido van Rossum <guido@python.org>
parents: 27461
diff changeset
757 def test_newobj_list(self):
27486
e9578747f43d The default __reduce__ on the base object type obscured any
Guido van Rossum <guido@python.org>
parents: 27482
diff changeset
758 x = MyList([1, 2, 3])
e9578747f43d The default __reduce__ on the base object type obscured any
Guido van Rossum <guido@python.org>
parents: 27482
diff changeset
759 x.foo = 42
e9578747f43d The default __reduce__ on the base object type obscured any
Guido van Rossum <guido@python.org>
parents: 27482
diff changeset
760 x.bar = "hello"
27659
0bbbcacc05fb test_newobj_tuple(), test_newobj_list(): These tests should work under
Tim Peters <tim.peters@gmail.com>
parents: 27658
diff changeset
761 for proto in protocols:
0bbbcacc05fb test_newobj_tuple(), test_newobj_list(): These tests should work under
Tim Peters <tim.peters@gmail.com>
parents: 27658
diff changeset
762 s = self.dumps(x, proto)
0bbbcacc05fb test_newobj_tuple(), test_newobj_list(): These tests should work under
Tim Peters <tim.peters@gmail.com>
parents: 27658
diff changeset
763 y = self.loads(s)
0bbbcacc05fb test_newobj_tuple(), test_newobj_list(): These tests should work under
Tim Peters <tim.peters@gmail.com>
parents: 27658
diff changeset
764 self.assertEqual(list(x), list(y))
0bbbcacc05fb test_newobj_tuple(), test_newobj_list(): These tests should work under
Tim Peters <tim.peters@gmail.com>
parents: 27658
diff changeset
765 self.assertEqual(x.__dict__, y.__dict__)
27482
7c8dfd3f4ae9 Some experimental support for generating NEWOBJ with proto=2, and
Guido van Rossum <guido@python.org>
parents: 27461
diff changeset
766
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
767 def test_newobj_generic(self):
27658
b595c4735fcc test_newobj_generic(): Use the global protocols vector instead of a
Tim Peters <tim.peters@gmail.com>
parents: 27657
diff changeset
768 for proto in protocols:
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
769 for C in myclasses:
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
770 B = C.__base__
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
771 x = C(C.sample)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
772 x.foo = 42
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
773 s = self.dumps(x, proto)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
774 y = self.loads(s)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
775 detail = (proto, C, B, x, y, type(y))
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
776 self.assertEqual(B(x), B(y), detail)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
777 self.assertEqual(x.__dict__, y.__dict__, detail)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
778
27657
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
779 # Register a type with copy_reg, with extension code extcode. Pickle
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
780 # an object of that type. Check that the resulting pickle uses opcode
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
781 # (EXT[124]) under proto 2, and not in proto 1.
27686
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
782
27657
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
783 def produce_global_ext(self, extcode, opcode):
27686
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
784 e = ExtensionSaver(extcode)
27510
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
785 try:
27686
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
786 copy_reg.add_extension(__name__, "MyList", extcode)
27510
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
787 x = MyList([1, 2, 3])
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
788 x.foo = 42
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
789 x.bar = "hello"
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
790
27657
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
791 # Dump using protocol 1 for comparison.
27510
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
792 s1 = self.dumps(x, 1)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58183
diff changeset
793 self.assertIn(__name__, s1)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58183
diff changeset
794 self.assertIn("MyList", s1)
27686
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
795 self.assertEqual(opcode_in_pickle(opcode, s1), False)
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
796
27510
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
797 y = self.loads(s1)
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
798 self.assertEqual(list(x), list(y))
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
799 self.assertEqual(x.__dict__, y.__dict__)
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
800
27657
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
801 # Dump using protocol 2 for test.
27510
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
802 s2 = self.dumps(x, 2)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58183
diff changeset
803 self.assertNotIn(__name__, s2)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58183
diff changeset
804 self.assertNotIn("MyList", s2)
27686
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
805 self.assertEqual(opcode_in_pickle(opcode, s2), True)
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
806
27510
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
807 y = self.loads(s2)
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
808 self.assertEqual(list(x), list(y))
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
809 self.assertEqual(x.__dict__, y.__dict__)
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
810
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
811 finally:
27686
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
812 e.restore()
27657
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
813
67315026d6d3 Added a bit to the EXT[124] tests, and refactored them to squash code
Tim Peters <tim.peters@gmail.com>
parents: 27655
diff changeset
814 def test_global_ext1(self):
27686
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
815 self.produce_global_ext(0x00000001, pickle.EXT1) # smallest EXT1 code
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
816 self.produce_global_ext(0x000000ff, pickle.EXT1) # largest EXT1 code
27510
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
817
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
818 def test_global_ext2(self):
27686
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
819 self.produce_global_ext(0x00000100, pickle.EXT2) # smallest EXT2 code
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
820 self.produce_global_ext(0x0000ffff, pickle.EXT2) # largest EXT2 code
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
821 self.produce_global_ext(0x0000abcd, pickle.EXT2) # check endianness
27510
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
822
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
823 def test_global_ext4(self):
27686
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
824 self.produce_global_ext(0x00010000, pickle.EXT4) # smallest EXT4 code
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
825 self.produce_global_ext(0x7fffffff, pickle.EXT4) # largest EXT4 code
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
826 self.produce_global_ext(0x12abcdef, pickle.EXT4) # check endianness
3599f6593fe9 cPickle: exempt two_tuple from GC -- it's a speed hack, and doesn't
Tim Peters <tim.peters@gmail.com>
parents: 27684
diff changeset
827
27813
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
828 def test_list_chunking(self):
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
829 n = 10 # too small to chunk
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
830 x = range(n)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
831 for proto in protocols:
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
832 s = self.dumps(x, proto)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
833 y = self.loads(s)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
834 self.assertEqual(x, y)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
835 num_appends = count_opcode(pickle.APPENDS, s)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
836 self.assertEqual(num_appends, proto > 0)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
837
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
838 n = 2500 # expect at least two chunks when proto > 0
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
839 x = range(n)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
840 for proto in protocols:
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
841 s = self.dumps(x, proto)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
842 y = self.loads(s)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
843 self.assertEqual(x, y)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
844 num_appends = count_opcode(pickle.APPENDS, s)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
845 if proto == 0:
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
846 self.assertEqual(num_appends, 0)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
847 else:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54343
diff changeset
848 self.assertTrue(num_appends >= 2)
27813
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
849
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
850 def test_dict_chunking(self):
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
851 n = 10 # too small to chunk
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
852 x = dict.fromkeys(range(n))
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
853 for proto in protocols:
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
854 s = self.dumps(x, proto)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
855 y = self.loads(s)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
856 self.assertEqual(x, y)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
857 num_setitems = count_opcode(pickle.SETITEMS, s)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
858 self.assertEqual(num_setitems, proto > 0)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
859
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
860 n = 2500 # expect at least two chunks when proto > 0
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
861 x = dict.fromkeys(range(n))
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
862 for proto in protocols:
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
863 s = self.dumps(x, proto)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
864 y = self.loads(s)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
865 self.assertEqual(x, y)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
866 num_setitems = count_opcode(pickle.SETITEMS, s)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
867 if proto == 0:
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
868 self.assertEqual(num_setitems, 0)
f6c95f9138ad Added tests to ensure that list and dict "chunking" are actually
Tim Peters <tim.peters@gmail.com>
parents: 27686
diff changeset
869 else:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54343
diff changeset
870 self.assertTrue(num_setitems >= 2)
27510
20c581c23bce Test all three EXT opcodes, and move these tests into
Guido van Rossum <guido@python.org>
parents: 27500
diff changeset
871
27872
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
872 def test_simple_newobj(self):
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
873 x = object.__new__(SimpleNewObj) # avoid __init__
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
874 x.abc = 666
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
875 for proto in protocols:
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
876 s = self.dumps(x, proto)
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
877 self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s), proto >= 2)
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
878 y = self.loads(s) # will raise TypeError if __init__ called
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
879 self.assertEqual(y.abc, 666)
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
880 self.assertEqual(x.__dict__, y.__dict__)
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
881
27825
011f734258db Implemented batching for dicts in cPickle. This is after two failed
Tim Peters <tim.peters@gmail.com>
parents: 27822
diff changeset
882 def test_newobj_list_slots(self):
011f734258db Implemented batching for dicts in cPickle. This is after two failed
Tim Peters <tim.peters@gmail.com>
parents: 27822
diff changeset
883 x = SlotList([1, 2, 3])
011f734258db Implemented batching for dicts in cPickle. This is after two failed
Tim Peters <tim.peters@gmail.com>
parents: 27822
diff changeset
884 x.foo = 42
011f734258db Implemented batching for dicts in cPickle. This is after two failed
Tim Peters <tim.peters@gmail.com>
parents: 27822
diff changeset
885 x.bar = "hello"
011f734258db Implemented batching for dicts in cPickle. This is after two failed
Tim Peters <tim.peters@gmail.com>
parents: 27822
diff changeset
886 s = self.dumps(x, 2)
011f734258db Implemented batching for dicts in cPickle. This is after two failed
Tim Peters <tim.peters@gmail.com>
parents: 27822
diff changeset
887 y = self.loads(s)
011f734258db Implemented batching for dicts in cPickle. This is after two failed
Tim Peters <tim.peters@gmail.com>
parents: 27822
diff changeset
888 self.assertEqual(list(x), list(y))
011f734258db Implemented batching for dicts in cPickle. This is after two failed
Tim Peters <tim.peters@gmail.com>
parents: 27822
diff changeset
889 self.assertEqual(x.__dict__, y.__dict__)
011f734258db Implemented batching for dicts in cPickle. This is after two failed
Tim Peters <tim.peters@gmail.com>
parents: 27822
diff changeset
890 self.assertEqual(x.foo, y.foo)
011f734258db Implemented batching for dicts in cPickle. This is after two failed
Tim Peters <tim.peters@gmail.com>
parents: 27822
diff changeset
891 self.assertEqual(x.bar, y.bar)
011f734258db Implemented batching for dicts in cPickle. This is after two failed
Tim Peters <tim.peters@gmail.com>
parents: 27822
diff changeset
892
27951
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
893 def test_reduce_overrides_default_reduce_ex(self):
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
894 for proto in protocols:
27951
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
895 x = REX_one()
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
896 self.assertEqual(x._reduce_called, 0)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
897 s = self.dumps(x, proto)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
898 self.assertEqual(x._reduce_called, 1)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
899 y = self.loads(s)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
900 self.assertEqual(y._reduce_called, 0)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
901
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
902 def test_reduce_ex_called(self):
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
903 for proto in protocols:
27951
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
904 x = REX_two()
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
905 self.assertEqual(x._proto, None)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
906 s = self.dumps(x, proto)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
907 self.assertEqual(x._proto, proto)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
908 y = self.loads(s)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
909 self.assertEqual(y._proto, None)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
910
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
911 def test_reduce_ex_overrides_reduce(self):
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
912 for proto in protocols:
27951
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
913 x = REX_three()
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
914 self.assertEqual(x._proto, None)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
915 s = self.dumps(x, proto)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
916 self.assertEqual(x._proto, proto)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
917 y = self.loads(s)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
918 self.assertEqual(y._proto, None)
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
919
41201
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
920 def test_reduce_ex_calls_base(self):
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
921 for proto in protocols:
41201
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
922 x = REX_four()
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
923 self.assertEqual(x._proto, None)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
924 s = self.dumps(x, proto)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
925 self.assertEqual(x._proto, proto)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
926 y = self.loads(s)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
927 self.assertEqual(y._proto, proto)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
928
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
929 def test_reduce_calls_base(self):
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
930 for proto in protocols:
41201
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
931 x = REX_five()
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
932 self.assertEqual(x._reduce_called, 0)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
933 s = self.dumps(x, proto)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
934 self.assertEqual(x._reduce_called, 1)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
935 y = self.loads(s)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
936 self.assertEqual(y._reduce_called, 1)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
937
49612
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
938 def test_reduce_bad_iterator(self):
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
939 # Issue4176: crash when 4th and 5th items of __reduce__()
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
940 # are not iterators
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
941 class C(object):
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
942 def __reduce__(self):
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
943 # 4th item is not an iterator
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
944 return list, (), None, [], None
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
945 class D(object):
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
946 def __reduce__(self):
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
947 # 5th item is not an iterator
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
948 return dict, (), None, None, []
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
949
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
950 # Protocol 0 is less strict and also accept iterables.
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
951 for proto in protocols:
49612
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
952 try:
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
953 self.dumps(C(), proto)
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
954 except (AttributeError, pickle.PickleError, cPickle.PickleError):
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
955 pass
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
956 try:
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
957 self.dumps(D(), proto)
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
958 except (AttributeError, pickle.PickleError, cPickle.PickleError):
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
959 pass
e8c0a5a646ca Issue #4176: Pickle would crash the interpreter when a __reduce__ function
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41201
diff changeset
960
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
961 def test_many_puts_and_gets(self):
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
962 # Test that internal data structures correctly deal with lots of
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
963 # puts/gets.
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
964 keys = ("aaa" + str(i) for i in xrange(100))
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
965 large_dict = dict((k, [4, 5, 6]) for k in keys)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
966 obj = [dict(large_dict), dict(large_dict), dict(large_dict)]
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
967
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
968 for proto in protocols:
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
969 dumped = self.dumps(obj, proto)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
970 loaded = self.loads(dumped)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
971 self.assertEqual(loaded, obj,
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
972 "Failed protocol %d: %r != %r"
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
973 % (proto, obj, loaded))
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
974
53693
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
975 def test_attribute_name_interning(self):
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
976 # Test that attribute names of pickled objects are interned when
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
977 # unpickling.
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
978 for proto in protocols:
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
979 x = C()
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
980 x.foo = 42
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
981 x.bar = "hello"
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
982 s = self.dumps(x, proto)
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
983 y = self.loads(s)
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
984 x_keys = sorted(x.__dict__)
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
985 y_keys = sorted(y.__dict__)
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
986 for x_key, y_key in zip(x_keys, y_keys):
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
987 self.assertIs(x_key, y_key)
e54141d63e01 Isue #5084: unpickling now interns the attribute names of pickled objects,
Antoine Pitrou <solipsis@pitrou.net>
parents: 53000
diff changeset
988
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
989
27951
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
990 # Test classes for reduce_ex
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
991
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
992 class REX_one(object):
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
993 _reduce_called = 0
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
994 def __reduce__(self):
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
995 self._reduce_called = 1
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
996 return REX_one, ()
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
997 # No __reduce_ex__ here, but inheriting it from object
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
998
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
999 class REX_two(object):
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1000 _proto = None
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1001 def __reduce_ex__(self, proto):
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1002 self._proto = proto
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1003 return REX_two, ()
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1004 # No __reduce__ here, but inheriting it from object
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1005
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1006 class REX_three(object):
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1007 _proto = None
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1008 def __reduce_ex__(self, proto):
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1009 self._proto = proto
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1010 return REX_two, ()
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1011 def __reduce__(self):
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1012 raise TestFailed, "This __reduce__ shouldn't be called"
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1013
41201
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1014 class REX_four(object):
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1015 _proto = None
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1016 def __reduce_ex__(self, proto):
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1017 self._proto = proto
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1018 return object.__reduce_ex__(self, proto)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1019 # Calling base class method should succeed
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1020
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1021 class REX_five(object):
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1022 _reduce_called = 0
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1023 def __reduce__(self):
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1024 self._reduce_called = 1
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1025 return object.__reduce__(self)
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1026 # This one used to fail with infinite recursion
4217f3850f0b Patch #1462488: prevent a segfault in object_reduce_ex() by splitting
Žiga Seilnacht <ziga.seilnacht@gmail.com>
parents: 38055
diff changeset
1027
27951
b540e608bb8a Three test cases for __reduce_ex__. This fails for cPickle, until Tim
Guido van Rossum <guido@python.org>
parents: 27901
diff changeset
1028 # Test classes for newobj
27901
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 27899
diff changeset
1029
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1030 class MyInt(int):
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1031 sample = 1
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1032
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1033 class MyLong(long):
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1034 sample = 1L
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1035
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1036 class MyFloat(float):
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1037 sample = 1.0
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1038
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1039 class MyComplex(complex):
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1040 sample = 1.0 + 0.0j
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1041
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1042 class MyStr(str):
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1043 sample = "hello"
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1044
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1045 class MyUnicode(unicode):
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1046 sample = u"hello \u1234"
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1047
27482
7c8dfd3f4ae9 Some experimental support for generating NEWOBJ with proto=2, and
Guido van Rossum <guido@python.org>
parents: 27461
diff changeset
1048 class MyTuple(tuple):
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1049 sample = (1, 2, 3)
27482
7c8dfd3f4ae9 Some experimental support for generating NEWOBJ with proto=2, and
Guido van Rossum <guido@python.org>
parents: 27461
diff changeset
1050
7c8dfd3f4ae9 Some experimental support for generating NEWOBJ with proto=2, and
Guido van Rossum <guido@python.org>
parents: 27461
diff changeset
1051 class MyList(list):
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1052 sample = [1, 2, 3]
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1053
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1054 class MyDict(dict):
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1055 sample = {"a": 1, "b": 2}
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1056
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1057 myclasses = [MyInt, MyLong, MyFloat,
28135
b53455a51b78 MyComplex now works.
Guido van Rossum <guido@python.org>
parents: 28132
diff changeset
1058 MyComplex,
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1059 MyStr, MyUnicode,
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1060 MyTuple, MyList, MyDict]
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27510
diff changeset
1061
27482
7c8dfd3f4ae9 Some experimental support for generating NEWOBJ with proto=2, and
Guido van Rossum <guido@python.org>
parents: 27461
diff changeset
1062
27494
1f16abfaf63e Add a test for a list subclass with a __dict__ as well as slots.
Guido van Rossum <guido@python.org>
parents: 27486
diff changeset
1063 class SlotList(MyList):
1f16abfaf63e Add a test for a list subclass with a __dict__ as well as slots.
Guido van Rossum <guido@python.org>
parents: 27486
diff changeset
1064 __slots__ = ["foo"]
1f16abfaf63e Add a test for a list subclass with a __dict__ as well as slots.
Guido van Rossum <guido@python.org>
parents: 27486
diff changeset
1065
27872
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
1066 class SimpleNewObj(object):
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
1067 def __init__(self, a, b, c):
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
1068 # raise an error, to make sure this isn't called
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
1069 raise TypeError("SimpleNewObj.__init__() didn't expect to get called")
11e6123de6fe Added a simple NEWOBJ test. This is in the pickle-only part of the
Tim Peters <tim.peters@gmail.com>
parents: 27864
diff changeset
1070
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
1071 class AbstractPickleModuleTests(unittest.TestCase):
19544
d711e82ece23 pickle.py, load_int(): Match cPickle's just-repaired ability to unpickle
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
1072
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
1073 def test_dump_closed_file(self):
24710
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1074 import os
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1075 f = open(TESTFN, "w")
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1076 try:
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1077 f.close()
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1078 self.assertRaises(ValueError, self.module.dump, 123, f)
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1079 finally:
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1080 os.remove(TESTFN)
20444
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
1081
bc905a11c148 Covert pickle tests to use unittest.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19544
diff changeset
1082 def test_load_closed_file(self):
24710
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1083 import os
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1084 f = open(TESTFN, "w")
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1085 try:
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1086 f.close()
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1087 self.assertRaises(ValueError, self.module.dump, 123, f)
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1088 finally:
11c701038805 Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Guido van Rossum <guido@python.org>
parents: 24442
diff changeset
1089 os.remove(TESTFN)
26202
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1090
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1091 def test_load_from_and_dump_to_file(self):
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1092 stream = cStringIO.StringIO()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1093 data = [123, {}, 124]
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1094 self.module.dump(data, stream)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1095 stream.seek(0)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1096 unpickled = self.module.load(stream)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1097 self.assertEqual(unpickled, data)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1098
27874
6af780e0862d We didn't have any tests making pickles with one of {pickle, cPickle},
Tim Peters <tim.peters@gmail.com>
parents: 27872
diff changeset
1099 def test_highest_protocol(self):
6af780e0862d We didn't have any tests making pickles with one of {pickle, cPickle},
Tim Peters <tim.peters@gmail.com>
parents: 27872
diff changeset
1100 # Of course this needs to be changed when HIGHEST_PROTOCOL changes.
6af780e0862d We didn't have any tests making pickles with one of {pickle, cPickle},
Tim Peters <tim.peters@gmail.com>
parents: 27872
diff changeset
1101 self.assertEqual(self.module.HIGHEST_PROTOCOL, 2)
6af780e0862d We didn't have any tests making pickles with one of {pickle, cPickle},
Tim Peters <tim.peters@gmail.com>
parents: 27872
diff changeset
1102
32829
cbd4ae74890d Patch #995766: Keyword argument support in cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 30801
diff changeset
1103 def test_callapi(self):
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1104 f = cStringIO.StringIO()
32829
cbd4ae74890d Patch #995766: Keyword argument support in cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 30801
diff changeset
1105 # With and without keyword arguments
cbd4ae74890d Patch #995766: Keyword argument support in cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 30801
diff changeset
1106 self.module.dump(123, f, -1)
cbd4ae74890d Patch #995766: Keyword argument support in cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 30801
diff changeset
1107 self.module.dump(123, file=f, protocol=-1)
cbd4ae74890d Patch #995766: Keyword argument support in cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 30801
diff changeset
1108 self.module.dumps(123, -1)
cbd4ae74890d Patch #995766: Keyword argument support in cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 30801
diff changeset
1109 self.module.dumps(123, protocol=-1)
cbd4ae74890d Patch #995766: Keyword argument support in cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 30801
diff changeset
1110 self.module.Pickler(f, -1)
cbd4ae74890d Patch #995766: Keyword argument support in cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 30801
diff changeset
1111 self.module.Pickler(f, protocol=-1)
27874
6af780e0862d We didn't have any tests making pickles with one of {pickle, cPickle},
Tim Peters <tim.peters@gmail.com>
parents: 27872
diff changeset
1112
55430
868b4e639b5a Merged revisions 74189 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54344
diff changeset
1113 def test_incomplete_input(self):
868b4e639b5a Merged revisions 74189 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54344
diff changeset
1114 s = StringIO.StringIO("X''.")
868b4e639b5a Merged revisions 74189 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54344
diff changeset
1115 self.assertRaises(EOFError, self.module.load, s)
868b4e639b5a Merged revisions 74189 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54344
diff changeset
1116
57439
b51fda754f5a Merged revisions 76499 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55430
diff changeset
1117 def test_restricted(self):
b51fda754f5a Merged revisions 76499 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55430
diff changeset
1118 # issue7128: cPickle failed in restricted mode
b51fda754f5a Merged revisions 76499 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55430
diff changeset
1119 builtins = {self.module.__name__: self.module,
b51fda754f5a Merged revisions 76499 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55430
diff changeset
1120 '__import__': __import__}
b51fda754f5a Merged revisions 76499 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55430
diff changeset
1121 d = {}
b51fda754f5a Merged revisions 76499 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55430
diff changeset
1122 teststr = "def f(): {0}.dumps(0)".format(self.module.__name__)
b51fda754f5a Merged revisions 76499 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55430
diff changeset
1123 exec teststr in {'__builtins__': builtins}, d
b51fda754f5a Merged revisions 76499 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55430
diff changeset
1124 d['f']()
b51fda754f5a Merged revisions 76499 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55430
diff changeset
1125
58188
afe6dde80abb Merged revisions 77352-77354 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57439
diff changeset
1126 def test_bad_input(self):
afe6dde80abb Merged revisions 77352-77354 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57439
diff changeset
1127 # Test issue4298
afe6dde80abb Merged revisions 77352-77354 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57439
diff changeset
1128 s = '\x58\0\0\0\x54'
afe6dde80abb Merged revisions 77352-77354 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57439
diff changeset
1129 self.assertRaises(EOFError, self.module.loads, s)
afe6dde80abb Merged revisions 77352-77354 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57439
diff changeset
1130 # Test issue7455
afe6dde80abb Merged revisions 77352-77354 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57439
diff changeset
1131 s = '0'
afe6dde80abb Merged revisions 77352-77354 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57439
diff changeset
1132 # XXX Why doesn't pickle raise UnpicklingError?
afe6dde80abb Merged revisions 77352-77354 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57439
diff changeset
1133 self.assertRaises((IndexError, cPickle.UnpicklingError),
afe6dde80abb Merged revisions 77352-77354 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57439
diff changeset
1134 self.module.loads, s)
57439
b51fda754f5a Merged revisions 76499 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55430
diff changeset
1135
26202
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1136 class AbstractPersistentPicklerTests(unittest.TestCase):
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1137
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1138 # This class defines persistent_id() and persistent_load()
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1139 # functions that should be used by the pickler. All even integers
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1140 # are pickled using persistent ids.
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1141
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1142 def persistent_id(self, object):
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1143 if isinstance(object, int) and object % 2 == 0:
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1144 self.id_count += 1
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1145 return str(object)
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1146 else:
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1147 return None
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1148
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1149 def persistent_load(self, oid):
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1150 self.load_count += 1
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1151 object = int(oid)
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1152 assert object % 2 == 0
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1153 return object
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1154
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1155 def test_persistence(self):
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1156 self.id_count = 0
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1157 self.load_count = 0
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1158 L = range(10)
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1159 self.assertEqual(self.loads(self.dumps(L)), L)
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1160 self.assertEqual(self.id_count, 5)
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1161 self.assertEqual(self.load_count, 5)
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1162
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1163 def test_bin_persistence(self):
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1164 self.id_count = 0
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1165 self.load_count = 0
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1166 L = range(10)
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1167 self.assertEqual(self.loads(self.dumps(L, 1)), L)
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1168 self.assertEqual(self.id_count, 5)
1cd8106bcd20 Add some simple tests of the persistence hooks.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24809
diff changeset
1169 self.assertEqual(self.load_count, 5)
53000
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1170
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1171 class AbstractPicklerUnpicklerObjectTests(unittest.TestCase):
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1172
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1173 pickler_class = None
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1174 unpickler_class = None
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1175
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1176 def setUp(self):
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1177 assert self.pickler_class
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1178 assert self.unpickler_class
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1179
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1180 def test_clear_pickler_memo(self):
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1181 # To test whether clear_memo() has any effect, we pickle an object,
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1182 # then pickle it again without clearing the memo; the two serialized
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1183 # forms should be different. If we clear_memo() and then pickle the
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1184 # object again, the third serialized form should be identical to the
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1185 # first one we obtained.
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1186 data = ["abcdefg", "abcdefg", 44]
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1187 f = cStringIO.StringIO()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1188 pickler = self.pickler_class(f)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1189
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1190 pickler.dump(data)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1191 first_pickled = f.getvalue()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1192
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1193 # Reset StringIO object.
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1194 f.seek(0)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1195 f.truncate()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1196
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1197 pickler.dump(data)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1198 second_pickled = f.getvalue()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1199
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1200 # Reset the Pickler and StringIO objects.
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1201 pickler.clear_memo()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1202 f.seek(0)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1203 f.truncate()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1204
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1205 pickler.dump(data)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1206 third_pickled = f.getvalue()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1207
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1208 self.assertNotEqual(first_pickled, second_pickled)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1209 self.assertEqual(first_pickled, third_pickled)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1210
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1211 def test_priming_pickler_memo(self):
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1212 # Verify that we can set the Pickler's memo attribute.
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1213 data = ["abcdefg", "abcdefg", 44]
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1214 f = cStringIO.StringIO()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1215 pickler = self.pickler_class(f)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1216
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1217 pickler.dump(data)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1218 first_pickled = f.getvalue()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1219
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1220 f = cStringIO.StringIO()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1221 primed = self.pickler_class(f)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1222 primed.memo = pickler.memo
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1223
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1224 primed.dump(data)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1225 primed_pickled = f.getvalue()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1226
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1227 self.assertNotEqual(first_pickled, primed_pickled)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1228
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1229 def test_priming_unpickler_memo(self):
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1230 # Verify that we can set the Unpickler's memo attribute.
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1231 data = ["abcdefg", "abcdefg", 44]
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1232 f = cStringIO.StringIO()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1233 pickler = self.pickler_class(f)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1234
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1235 pickler.dump(data)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1236 first_pickled = f.getvalue()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1237
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1238 f = cStringIO.StringIO()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1239 primed = self.pickler_class(f)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1240 primed.memo = pickler.memo
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1241
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1242 primed.dump(data)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1243 primed_pickled = f.getvalue()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1244
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1245 unpickler = self.unpickler_class(cStringIO.StringIO(first_pickled))
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1246 unpickled_data1 = unpickler.load()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1247
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1248 self.assertEqual(unpickled_data1, data)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1249
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1250 primed = self.unpickler_class(cStringIO.StringIO(primed_pickled))
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1251 primed.memo = unpickler.memo
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1252 unpickled_data2 = primed.load()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1253
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1254 primed.memo.clear()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1255
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1256 self.assertEqual(unpickled_data2, data)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1257 self.assertTrue(unpickled_data2 is unpickled_data1)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1258
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1259 def test_reusing_unpickler_objects(self):
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1260 data1 = ["abcdefg", "abcdefg", 44]
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1261 f = cStringIO.StringIO()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1262 pickler = self.pickler_class(f)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1263 pickler.dump(data1)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1264 pickled1 = f.getvalue()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1265
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1266 data2 = ["abcdefg", 44, 44]
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1267 f = cStringIO.StringIO()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1268 pickler = self.pickler_class(f)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1269 pickler.dump(data2)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1270 pickled2 = f.getvalue()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1271
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1272 f = cStringIO.StringIO()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1273 f.write(pickled1)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1274 f.seek(0)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1275 unpickler = self.unpickler_class(f)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1276 self.assertEqual(unpickler.load(), data1)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1277
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1278 f.seek(0)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1279 f.truncate()
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1280 f.write(pickled2)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1281 f.seek(0)
9a7317ce6db9 Issue 5665: add more pickling tests.
Collin Winter <collinw@gmail.com>
parents: 51020
diff changeset
1282 self.assertEqual(unpickler.load(), data2)