1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport pickle
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport cPickle
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport StringIO
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport cStringIO
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport pickletools
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport copy_reg
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test.test_support import TestFailed, verbose, have_unicode, TESTFN
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry:
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    from test.test_support import _2G, _1M, precisionbigmemtest
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept ImportError:
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # this import might fail when run on older Python versions by test_xpickle
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    _2G = _1M = 0
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def precisionbigmemtest(*args, **kwargs):
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return lambda self: None
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Tests that try a number of pickle protocols should have a
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#     for proto in protocols:
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# kind of outer loop.
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepassert pickle.HIGHEST_PROTOCOL == cPickle.HIGHEST_PROTOCOL == 2
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepprotocols = range(pickle.HIGHEST_PROTOCOL + 1)
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Copy of test.test_support.run_with_locale. This is needed to support Python
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 2.4, which didn't include it. This is all to support test_xpickle, which
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# bounces pickled objects through older Python versions to test backwards
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# compatibility.
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef run_with_locale(catstr, *locales):
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def decorator(func):
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def inner(*args, **kwds):
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                import locale
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                category = getattr(locale, catstr)
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                orig_locale = locale.setlocale(category)
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except AttributeError:
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # if the test author gives us an invalid category string
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                raise
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except:
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # cannot retrieve original locale, so do nothing
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                locale = orig_locale = None
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for loc in locales:
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    try:
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        locale.setlocale(category, loc)
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        break
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    except:
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        pass
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # now run the function, resetting the locale on exceptions
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return func(*args, **kwds)
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            finally:
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if locale and orig_locale:
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    locale.setlocale(category, orig_locale)
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        inner.func_name = func.func_name
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        inner.__doc__ = func.__doc__
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return inner
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return decorator
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Return True if opcode code appears in the pickle, else False.
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef opcode_in_pickle(code, pickle):
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for op, dummy, dummy in pickletools.genops(pickle):
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if op.code == code:
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return True
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return False
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Return the number of times opcode code appears in pickle.
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef count_opcode(code, pickle):
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    n = 0
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    for op, dummy, dummy in pickletools.genops(pickle):
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if op.code == code:
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            n += 1
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return n
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# We can't very well test the extension registry without putting known stuff
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# in it, but we have to be careful to restore its original state.  Code
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# should do this:
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#     e = ExtensionSaver(extension_code)
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#     try:
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#         fiddle w/ the extension registry's stuff for extension_code
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#     finally:
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#         e.restore()
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ExtensionSaver:
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Remember current registration for code (if any), and remove it (if
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # there is one).
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, code):
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.code = code
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if code in copy_reg._inverted_registry:
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.pair = copy_reg._inverted_registry[code]
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            copy_reg.remove_extension(self.pair[0], self.pair[1], code)
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.pair = None
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Restore previous registration for code.
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def restore(self):
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        code = self.code
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        curpair = copy_reg._inverted_registry.get(code)
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if curpair is not None:
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            copy_reg.remove_extension(curpair[0], curpair[1], code)
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pair = self.pair
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if pair is not None:
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            copy_reg.add_extension(pair[0], pair[1], code)
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass C:
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __cmp__(self, other):
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return cmp(self.__dict__, other.__dict__)
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport __main__
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep__main__.C = C
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepC.__module__ = "__main__"
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass myint(int):
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, x):
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.str = str(x)
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass initarg(C):
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, a, b):
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.a = a
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.b = b
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __getinitargs__(self):
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self.a, self.b
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass metaclass(type):
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    pass
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass use_metaclass(object):
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __metaclass__ = metaclass
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass pickling_metaclass(type):
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __eq__(self, other):
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return (type(self) == type(other) and
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.reduce_args == other.reduce_args)
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __reduce__(self):
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return (create_dynamic_class, self.reduce_args)
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __hash__ = None
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef create_dynamic_class(name, bases):
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    result = pickling_metaclass(name, bases, dict())
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    result.reduce_args = (name, bases)
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return result
148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# DATA0 .. DATA2 are the pickles we expect under the various protocols, for
150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# the object returned by create_data().
151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# break into multiple strings to avoid confusing font-lock-mode
153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepDATA0 = """(lp1
154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepI0
155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaL1L
156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaF2
157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepac__builtin__
158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcomplex
159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp2
160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep""" + \
161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""(F3
162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepF0
163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoeptRp3
164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaI1
165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaI-1
166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaI255
167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaI-255
168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaI-256
169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaI65535
170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaI-65535
171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaI-65536
172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaI2147483647
173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaI-2147483647
174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaI-2147483648
175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa""" + \
176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""(S'abc'
177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp4
178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepg4
179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep""" + \
180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""(i__main__
181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepC
182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp5
183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep""" + \
184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""(dp6
185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepS'foo'
186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp7
187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepI1
188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepsS'bar'
189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp8
190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepI2
191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepsbg5
192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptp9
193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepag9
194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepaI5
195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa.
196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Disassembly of DATA0.
199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepDATA0_DIS = """\
200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    0: (    MARK
201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    1: l        LIST       (MARK at 0)
202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    2: p    PUT        1
203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    5: I    INT        0
204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    8: a    APPEND
205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    9: L    LONG       1L
206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   13: a    APPEND
207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   14: F    FLOAT      2.0
208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   17: a    APPEND
209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   18: c    GLOBAL     '__builtin__ complex'
210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   39: p    PUT        2
211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   42: (    MARK
212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   43: F        FLOAT      3.0
213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   46: F        FLOAT      0.0
214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   49: t        TUPLE      (MARK at 42)
215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   50: R    REDUCE
216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   51: p    PUT        3
217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   54: a    APPEND
218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   55: I    INT        1
219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   58: a    APPEND
220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   59: I    INT        -1
221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   63: a    APPEND
222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   64: I    INT        255
223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   69: a    APPEND
224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   70: I    INT        -255
225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   76: a    APPEND
226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   77: I    INT        -256
227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   83: a    APPEND
228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   84: I    INT        65535
229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   91: a    APPEND
230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   92: I    INT        -65535
231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  100: a    APPEND
232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  101: I    INT        -65536
233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  109: a    APPEND
234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  110: I    INT        2147483647
235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  122: a    APPEND
236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  123: I    INT        -2147483647
237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  136: a    APPEND
238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  137: I    INT        -2147483648
239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  150: a    APPEND
240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  151: (    MARK
241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  152: S        STRING     'abc'
242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  159: p        PUT        4
243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  162: g        GET        4
244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  165: (        MARK
245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  166: i            INST       '__main__ C' (MARK at 165)
246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  178: p        PUT        5
247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  181: (        MARK
248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  182: d            DICT       (MARK at 181)
249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  183: p        PUT        6
250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  186: S        STRING     'foo'
251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  193: p        PUT        7
252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  196: I        INT        1
253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  199: s        SETITEM
254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  200: S        STRING     'bar'
255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  207: p        PUT        8
256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  210: I        INT        2
257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  213: s        SETITEM
258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  214: b        BUILD
259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  215: g        GET        5
260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  218: t        TUPLE      (MARK at 151)
261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  219: p    PUT        9
262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  222: a    APPEND
263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  223: g    GET        9
264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  226: a    APPEND
265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  227: I    INT        5
266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  230: a    APPEND
267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  231: .    STOP
268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoephighest protocol among opcodes = 0
269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepDATA1 = (']q\x01(K\x00L1L\nG@\x00\x00\x00\x00\x00\x00\x00'
272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         'c__builtin__\ncomplex\nq\x02(G@\x08\x00\x00\x00\x00\x00'
273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         '\x00G\x00\x00\x00\x00\x00\x00\x00\x00tRq\x03K\x01J\xff\xff'
274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         '\xff\xffK\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xff'
275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         'J\x01\x00\xff\xffJ\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00'
276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         '\x00\x80J\x00\x00\x00\x80(U\x03abcq\x04h\x04(c__main__\n'
277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         'C\nq\x05oq\x06}q\x07(U\x03fooq\x08K\x01U\x03barq\tK\x02ubh'
278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         '\x06tq\nh\nK\x05e.'
279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        )
280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Disassembly of DATA1.
282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepDATA1_DIS = """\
283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    0: ]    EMPTY_LIST
284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    1: q    BINPUT     1
285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    3: (    MARK
286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    4: K        BININT1    0
287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    6: L        LONG       1L
288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   10: G        BINFLOAT   2.0
289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   19: c        GLOBAL     '__builtin__ complex'
290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   40: q        BINPUT     2
291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   42: (        MARK
292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   43: G            BINFLOAT   3.0
293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   52: G            BINFLOAT   0.0
294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   61: t            TUPLE      (MARK at 42)
295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   62: R        REDUCE
296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   63: q        BINPUT     3
297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   65: K        BININT1    1
298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   67: J        BININT     -1
299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   72: K        BININT1    255
300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   74: J        BININT     -255
301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   79: J        BININT     -256
302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   84: M        BININT2    65535
303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   87: J        BININT     -65535
304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   92: J        BININT     -65536
305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   97: J        BININT     2147483647
306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  102: J        BININT     -2147483647
307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  107: J        BININT     -2147483648
308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  112: (        MARK
309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  113: U            SHORT_BINSTRING 'abc'
310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  118: q            BINPUT     4
311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  120: h            BINGET     4
312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  122: (            MARK
313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  123: c                GLOBAL     '__main__ C'
314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  135: q                BINPUT     5
315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  137: o                OBJ        (MARK at 122)
316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  138: q            BINPUT     6
317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  140: }            EMPTY_DICT
318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  141: q            BINPUT     7
319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  143: (            MARK
320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  144: U                SHORT_BINSTRING 'foo'
321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  149: q                BINPUT     8
322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  151: K                BININT1    1
323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  153: U                SHORT_BINSTRING 'bar'
324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  158: q                BINPUT     9
325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  160: K                BININT1    2
326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  162: u                SETITEMS   (MARK at 143)
327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  163: b            BUILD
328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  164: h            BINGET     6
329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  166: t            TUPLE      (MARK at 112)
330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  167: q        BINPUT     10
331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  169: h        BINGET     10
332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  171: K        BININT1    5
333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  173: e        APPENDS    (MARK at 3)
334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  174: .    STOP
335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoephighest protocol among opcodes = 1
336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepDATA2 = ('\x80\x02]q\x01(K\x00\x8a\x01\x01G@\x00\x00\x00\x00\x00\x00\x00'
339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         'c__builtin__\ncomplex\nq\x02G@\x08\x00\x00\x00\x00\x00\x00G\x00'
340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         '\x00\x00\x00\x00\x00\x00\x00\x86Rq\x03K\x01J\xff\xff\xff\xffK'
341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         '\xffJ\x01\xff\xff\xffJ\x00\xff\xff\xffM\xff\xffJ\x01\x00\xff\xff'
342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         'J\x00\x00\xff\xffJ\xff\xff\xff\x7fJ\x01\x00\x00\x80J\x00\x00\x00'
343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         '\x80(U\x03abcq\x04h\x04(c__main__\nC\nq\x05oq\x06}q\x07(U\x03foo'
344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep         'q\x08K\x01U\x03barq\tK\x02ubh\x06tq\nh\nK\x05e.')
345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Disassembly of DATA2.
347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepDATA2_DIS = """\
348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    0: \x80 PROTO      2
349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    2: ]    EMPTY_LIST
350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    3: q    BINPUT     1
351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    5: (    MARK
352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    6: K        BININT1    0
353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    8: \x8a     LONG1      1L
354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   11: G        BINFLOAT   2.0
355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   20: c        GLOBAL     '__builtin__ complex'
356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   41: q        BINPUT     2
357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   43: G        BINFLOAT   3.0
358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   52: G        BINFLOAT   0.0
359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   61: \x86     TUPLE2
360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   62: R        REDUCE
361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   63: q        BINPUT     3
362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   65: K        BININT1    1
363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   67: J        BININT     -1
364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   72: K        BININT1    255
365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   74: J        BININT     -255
366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   79: J        BININT     -256
367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   84: M        BININT2    65535
368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   87: J        BININT     -65535
369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   92: J        BININT     -65536
370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep   97: J        BININT     2147483647
371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  102: J        BININT     -2147483647
372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  107: J        BININT     -2147483648
373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  112: (        MARK
374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  113: U            SHORT_BINSTRING 'abc'
375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  118: q            BINPUT     4
376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  120: h            BINGET     4
377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  122: (            MARK
378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  123: c                GLOBAL     '__main__ C'
379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  135: q                BINPUT     5
380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  137: o                OBJ        (MARK at 122)
381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  138: q            BINPUT     6
382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  140: }            EMPTY_DICT
383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  141: q            BINPUT     7
384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  143: (            MARK
385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  144: U                SHORT_BINSTRING 'foo'
386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  149: q                BINPUT     8
387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  151: K                BININT1    1
388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  153: U                SHORT_BINSTRING 'bar'
389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  158: q                BINPUT     9
390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  160: K                BININT1    2
391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  162: u                SETITEMS   (MARK at 143)
392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  163: b            BUILD
393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  164: h            BINGET     6
394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  166: t            TUPLE      (MARK at 112)
395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  167: q        BINPUT     10
396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  169: h        BINGET     10
397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  171: K        BININT1    5
398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  173: e        APPENDS    (MARK at 5)
399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep  174: .    STOP
400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoephighest protocol among opcodes = 2
401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef create_data():
404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    c = C()
405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    c.foo = 1
406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    c.bar = 2
407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    x = [0, 1L, 2.0, 3.0+0j]
408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Append some integer test cases at cPickle.c's internal size
409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # cutoffs.
410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    uint1max = 0xff
411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    uint2max = 0xffff
412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    int4max = 0x7fffffff
413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    x.extend([1, -1,
414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep              uint1max, -uint1max, -uint1max-1,
415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep              uint2max, -uint2max, -uint2max-1,
416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep               int4max,  -int4max,  -int4max-1])
417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    y = ('abc', 'abc', c, c)
418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    x.append(y)
419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    x.append(y)
420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    x.append(5)
421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    return x
422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass AbstractPickleTests(unittest.TestCase):
424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Subclass must define self.dumps, self.loads, self.error.
425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    _testdata = create_data()
427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setUp(self):
429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_misc(self):
432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # test various datatypes not tested by testdata
433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = myint(4)
435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x, y)
438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = (1, ())
440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x, y)
443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = initarg(1, x)
445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x, y)
448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # XXX test __reduce__ protocol?
450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_roundtrip_equality(self):
452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        expected = self._testdata
453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(expected, proto)
455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            got = self.loads(s)
456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(expected, got)
457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_load_from_canned_string(self):
459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        expected = self._testdata
460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for canned in DATA0, DATA1, DATA2:
461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            got = self.loads(canned)
462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(expected, got)
463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # There are gratuitous differences between pickles produced by
465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # pickle and cPickle, largely because cPickle starts PUT indices at
466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # 1 and pickle starts them at 0.  See XXX comment in cPickle's put2() --
467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # there's a comment with an exclamation point there whose meaning
468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # is a mystery.  cPickle also suppresses PUT for objects with a refcount
469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # of 1.
470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def dont_test_disassembly(self):
471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        from pickletools import dis
472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto, expected in (0, DATA0_DIS), (1, DATA1_DIS):
474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(self._testdata, proto)
475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            filelike = cStringIO.StringIO()
476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            dis(s, out=filelike)
477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            got = filelike.getvalue()
478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(expected, got)
479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_recursive_list(self):
481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l = []
482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l.append(l)
483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(l, proto)
485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = self.loads(s)
486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(len(x), 1)
487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(x is x[0])
488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_recursive_tuple(self):
490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = ([],)
491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t[0].append(t)
492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(t, proto)
494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = self.loads(s)
495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(len(x), 1)
496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(len(x[0]), 1)
497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(x is x[0][0])
498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_recursive_dict(self):
500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = {}
501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d[1] = d
502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(d, proto)
504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = self.loads(s)
505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x.keys(), [1])
506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(x[1] is x)
507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_recursive_inst(self):
509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        i = C()
510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        i.attr = i
511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(i, proto)
513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = self.loads(s)
514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(dir(x), dir(i))
515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertIs(x.attr, x)
516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_recursive_multi(self):
518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l = []
519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = {1:l}
520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        i = C()
521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        i.attr = d
522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        l.append(i)
523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(l, proto)
525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = self.loads(s)
526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(len(x), 1)
527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(dir(x[0]), dir(i))
528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x[0].attr.keys(), [1])
529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(x[0].attr[1] is x)
530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_garyp(self):
532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(self.error, self.loads, 'garyp')
533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_insecure_strings(self):
535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        insecure = ["abc", "2 + 2", # not quoted
536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    #"'abc' + 'def'", # not a single quoted string
537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    "'abc", # quote is not closed
538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    "'abc\"", # open quote and close quote don't match
539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    "'abc'   ?", # junk after close quote
540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    "'\\'", # trailing backslash
541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    "'",    # issue #17710
542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    "' ",   # issue #17710
543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # some tests of the quoting rules
544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    #"'abc\"\''",
545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    #"'\\\\a\'\'\'\\\'\\\\\''",
546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ]
547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for s in insecure:
548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            buf = "S" + s + "\012p0\012."
549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertRaises(ValueError, self.loads, buf)
550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    if have_unicode:
552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def test_unicode(self):
553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            endcases = [u'', u'<\\u>', u'<\\\u1234>', u'<\n>',
554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        u'<\\>', u'<\\\U00012345>']
555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for proto in protocols:
556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for u in endcases:
557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    p = self.dumps(u, proto)
558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    u2 = self.loads(p)
559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.assertEqual(u2, u)
560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def test_unicode_high_plane(self):
562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            t = u'\U00012345'
563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for proto in protocols:
564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                p = self.dumps(t, proto)
565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                t2 = self.loads(p)
566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(t2, t)
567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_ints(self):
569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import sys
570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            n = sys.maxint
572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            while n:
573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for expected in (-n, n):
574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    s = self.dumps(expected, proto)
575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    n2 = self.loads(s)
576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.assertEqual(expected, n2)
577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                n = n >> 1
578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_maxint64(self):
580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        maxint64 = (1L << 63) - 1
581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        data = 'I' + str(maxint64) + '\n.'
582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        got = self.loads(data)
583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(got, maxint64)
584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Try too with a bogus literal.
586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        data = 'I' + str(maxint64) + 'JUNK\n.'
587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(ValueError, self.loads, data)
588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_long(self):
590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # 256 bytes is where LONG4 begins.
592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for nbits in 1, 8, 8*254, 8*255, 8*256, 8*257:
593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                nbase = 1L << nbits
594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for npos in nbase-1, nbase, nbase+1:
595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    for n in npos, -npos:
596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        pickle = self.dumps(n, proto)
597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        got = self.loads(pickle)
598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        self.assertEqual(n, got)
599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Try a monster.  This is quadratic-time in protos 0 & 1, so don't
600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # bother with those.
601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        nbase = long("deadbeeffeedface", 16)
602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        nbase += nbase << 1000000
603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for n in nbase, -nbase:
604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            p = self.dumps(n, 2)
605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            got = self.loads(p)
606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(n, got)
607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_float(self):
609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_values = [0.0, 4.94e-324, 1e-310, 7e-308, 6.626e-34, 0.1, 0.5,
610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                       3.14, 263.44582062374053, 6.022e23, 1e30]
611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        test_values = test_values + [-x for x in test_values]
612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for value in test_values:
614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pickle = self.dumps(value, proto)
615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                got = self.loads(pickle)
616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(value, got)
617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    @run_with_locale('LC_ALL', 'de_DE', 'fr_FR')
619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_float_format(self):
620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # make sure that floats are formatted locale independent
621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.dumps(1.2)[0:3], 'F1.')
622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_reduce(self):
624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_getinitargs(self):
627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pass
628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_metaclass(self):
630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = use_metaclass()
631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(a, proto)
633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            b = self.loads(s)
634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(a.__class__, b.__class__)
635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_dynamic_class(self):
637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = create_dynamic_class("my_dynamic_class", (object,))
638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        copy_reg.pickle(pickling_metaclass, pickling_metaclass.__reduce__)
639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(a, proto)
641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            b = self.loads(s)
642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(a, b)
643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_structseq(self):
645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import time
646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import os
647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = time.localtime()
649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(t, proto)
651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            u = self.loads(s)
652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(t, u)
653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if hasattr(os, "stat"):
654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                t = os.stat(os.curdir)
655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                s = self.dumps(t, proto)
656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                u = self.loads(s)
657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(t, u)
658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if hasattr(os, "statvfs"):
659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                t = os.statvfs(os.curdir)
660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                s = self.dumps(t, proto)
661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                u = self.loads(s)
662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(t, u)
663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Tests for protocol 2
665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_proto(self):
667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        build_none = pickle.NONE + pickle.STOP
668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            expected = build_none
670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if proto >= 2:
671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                expected = pickle.PROTO + chr(proto) + expected
672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            p = self.dumps(None, proto)
673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(p, expected)
674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        oob = protocols[-1] + 1     # a future protocol
676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        badpickle = pickle.PROTO + chr(oob) + build_none
677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.loads(badpickle)
679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except ValueError, detail:
680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(str(detail).startswith(
681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                            "unsupported pickle protocol"))
682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("expected bad protocol number to raise ValueError")
684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_long1(self):
686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = 12345678910111213141516178920L
687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x, y)
691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(opcode_in_pickle(pickle.LONG1, s), proto >= 2)
692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_long4(self):
694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = 12345678910111213141516178920L << (256*8)
695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x, y)
699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(opcode_in_pickle(pickle.LONG4, s), proto >= 2)
700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_short_tuples(self):
702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Map (proto, len(tuple)) to expected opcode.
703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        expected_opcode = {(0, 0): pickle.TUPLE,
704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (0, 1): pickle.TUPLE,
705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (0, 2): pickle.TUPLE,
706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (0, 3): pickle.TUPLE,
707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (0, 4): pickle.TUPLE,
708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (1, 0): pickle.EMPTY_TUPLE,
710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (1, 1): pickle.TUPLE,
711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (1, 2): pickle.TUPLE,
712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (1, 3): pickle.TUPLE,
713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (1, 4): pickle.TUPLE,
714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (2, 0): pickle.EMPTY_TUPLE,
716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (2, 1): pickle.TUPLE1,
717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (2, 2): pickle.TUPLE2,
718edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (2, 3): pickle.TUPLE3,
719edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (2, 4): pickle.TUPLE,
720edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                          }
721edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        a = ()
722edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        b = (1,)
723edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        c = (1, 2)
724edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = (1, 2, 3)
725edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        e = (1, 2, 3, 4)
726edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
727edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for x in a, b, c, d, e:
728edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                s = self.dumps(x, proto)
729edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                y = self.loads(s)
730edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(x, y, (proto, x, s, y))
731edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                expected = expected_opcode[proto, len(x)]
732edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(opcode_in_pickle(expected, s), True)
733edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
734edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_singletons(self):
735edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Map (proto, singleton) to expected opcode.
736edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        expected_opcode = {(0, None): pickle.NONE,
737edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (1, None): pickle.NONE,
738edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (2, None): pickle.NONE,
739edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
740edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (0, True): pickle.INT,
741edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (1, True): pickle.INT,
742edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (2, True): pickle.NEWTRUE,
743edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
744edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (0, False): pickle.INT,
745edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (1, False): pickle.INT,
746edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                           (2, False): pickle.NEWFALSE,
747edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                          }
748edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
749edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for x in None, False, True:
750edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                s = self.dumps(x, proto)
751edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                y = self.loads(s)
752edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertTrue(x is y, (proto, x, s, y))
753edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                expected = expected_opcode[proto, x]
754edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(opcode_in_pickle(expected, s), True)
755edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
756edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_newobj_tuple(self):
757edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = MyTuple([1, 2, 3])
758edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.foo = 42
759edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.bar = "hello"
760edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
761edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
762edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
763edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(tuple(x), tuple(y))
764edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x.__dict__, y.__dict__)
765edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
766edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_newobj_list(self):
767edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = MyList([1, 2, 3])
768edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.foo = 42
769edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.bar = "hello"
770edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
771edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
772edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
773edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(list(x), list(y))
774edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x.__dict__, y.__dict__)
775edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
776edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_newobj_generic(self):
777edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
778edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for C in myclasses:
779edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                B = C.__base__
780edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                x = C(C.sample)
781edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                x.foo = 42
782edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                s = self.dumps(x, proto)
783edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                y = self.loads(s)
784edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                detail = (proto, C, B, x, y, type(y))
785edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(B(x), B(y), detail)
786edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(x.__dict__, y.__dict__, detail)
787edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
788edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Register a type with copy_reg, with extension code extcode.  Pickle
789edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # an object of that type.  Check that the resulting pickle uses opcode
790edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # (EXT[124]) under proto 2, and not in proto 1.
791edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
792edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def produce_global_ext(self, extcode, opcode):
793edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        e = ExtensionSaver(extcode)
794edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
795edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            copy_reg.add_extension(__name__, "MyList", extcode)
796edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = MyList([1, 2, 3])
797edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.foo = 42
798edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.bar = "hello"
799edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
800edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Dump using protocol 1 for comparison.
801edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s1 = self.dumps(x, 1)
802edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertIn(__name__, s1)
803edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertIn("MyList", s1)
804edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(opcode_in_pickle(opcode, s1), False)
805edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
806edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s1)
807edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(list(x), list(y))
808edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x.__dict__, y.__dict__)
809edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
810edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Dump using protocol 2 for test.
811edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s2 = self.dumps(x, 2)
812edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertNotIn(__name__, s2)
813edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertNotIn("MyList", s2)
814edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(opcode_in_pickle(opcode, s2), True)
815edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
816edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s2)
817edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(list(x), list(y))
818edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x.__dict__, y.__dict__)
819edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
820edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        finally:
821edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            e.restore()
822edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
823edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_global_ext1(self):
824edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.produce_global_ext(0x00000001, pickle.EXT1)  # smallest EXT1 code
825edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.produce_global_ext(0x000000ff, pickle.EXT1)  # largest EXT1 code
826edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
827edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_global_ext2(self):
828edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.produce_global_ext(0x00000100, pickle.EXT2)  # smallest EXT2 code
829edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.produce_global_ext(0x0000ffff, pickle.EXT2)  # largest EXT2 code
830edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.produce_global_ext(0x0000abcd, pickle.EXT2)  # check endianness
831edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
832edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_global_ext4(self):
833edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.produce_global_ext(0x00010000, pickle.EXT4)  # smallest EXT4 code
834edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.produce_global_ext(0x7fffffff, pickle.EXT4)  # largest EXT4 code
835edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.produce_global_ext(0x12abcdef, pickle.EXT4)  # check endianness
836edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
837edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_list_chunking(self):
838edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        n = 10  # too small to chunk
839edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = range(n)
840edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
841edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
842edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
843edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x, y)
844edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            num_appends = count_opcode(pickle.APPENDS, s)
845edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(num_appends, proto > 0)
846edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
847edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        n = 2500  # expect at least two chunks when proto > 0
848edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = range(n)
849edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
850edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
851edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
852edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x, y)
853edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            num_appends = count_opcode(pickle.APPENDS, s)
854edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if proto == 0:
855edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(num_appends, 0)
856edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
857edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertTrue(num_appends >= 2)
858edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
859edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_dict_chunking(self):
860edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        n = 10  # too small to chunk
861edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = dict.fromkeys(range(n))
862edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
863edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
864edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
865edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x, y)
866edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            num_setitems = count_opcode(pickle.SETITEMS, s)
867edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(num_setitems, proto > 0)
868edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
869edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        n = 2500  # expect at least two chunks when proto > 0
870edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = dict.fromkeys(range(n))
871edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
872edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
873edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
874edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x, y)
875edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            num_setitems = count_opcode(pickle.SETITEMS, s)
876edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if proto == 0:
877edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(num_setitems, 0)
878edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
879edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertTrue(num_setitems >= 2)
880edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
881edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_simple_newobj(self):
882edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = object.__new__(SimpleNewObj)  # avoid __init__
883edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.abc = 666
884edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
885edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
886edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, s), proto >= 2)
887edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)   # will raise TypeError if __init__ called
888edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y.abc, 666)
889edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x.__dict__, y.__dict__)
890edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
891edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_newobj_list_slots(self):
892edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = SlotList([1, 2, 3])
893edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.foo = 42
894edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x.bar = "hello"
895edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = self.dumps(x, 2)
896edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y = self.loads(s)
897edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(list(x), list(y))
898edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.__dict__, y.__dict__)
899edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.foo, y.foo)
900edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x.bar, y.bar)
901edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
902edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_reduce_overrides_default_reduce_ex(self):
903edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
904edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = REX_one()
905edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x._reduce_called, 0)
906edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
907edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x._reduce_called, 1)
908edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
909edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y._reduce_called, 0)
910edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
911edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_reduce_ex_called(self):
912edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
913edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = REX_two()
914edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x._proto, None)
915edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
916edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x._proto, proto)
917edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
918edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y._proto, None)
919edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
920edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_reduce_ex_overrides_reduce(self):
921edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
922edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = REX_three()
923edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x._proto, None)
924edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
925edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x._proto, proto)
926edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
927edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y._proto, None)
928edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
929edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_reduce_ex_calls_base(self):
930edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
931edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = REX_four()
932edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x._proto, None)
933edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
934edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x._proto, proto)
935edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
936edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y._proto, proto)
937edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
938edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_reduce_calls_base(self):
939edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
940edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = REX_five()
941edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x._reduce_called, 0)
942edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
943edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x._reduce_called, 1)
944edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
945edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(y._reduce_called, 1)
946edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
947edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_reduce_bad_iterator(self):
948edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Issue4176: crash when 4th and 5th items of __reduce__()
949edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # are not iterators
950edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class C(object):
951edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __reduce__(self):
952edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # 4th item is not an iterator
953edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return list, (), None, [], None
954edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class D(object):
955edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __reduce__(self):
956edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # 5th item is not an iterator
957edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return dict, (), None, None, []
958edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
959edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Protocol 0 is less strict and also accept iterables.
960edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
961edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
962edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.dumps(C(), proto)
963edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except (AttributeError, pickle.PickleError, cPickle.PickleError):
964edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
965edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            try:
966edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.dumps(D(), proto)
967edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            except (AttributeError, pickle.PickleError, cPickle.PickleError):
968edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                pass
969edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
970edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_many_puts_and_gets(self):
971edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test that internal data structures correctly deal with lots of
972edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # puts/gets.
973edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        keys = ("aaa" + str(i) for i in xrange(100))
974edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        large_dict = dict((k, [4, 5, 6]) for k in keys)
975edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        obj = [dict(large_dict), dict(large_dict), dict(large_dict)]
976edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
977edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
978edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            dumped = self.dumps(obj, proto)
979edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            loaded = self.loads(dumped)
980edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(loaded, obj,
981edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                             "Failed protocol %d: %r != %r"
982edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                             % (proto, obj, loaded))
983edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
984edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_attribute_name_interning(self):
985edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test that attribute names of pickled objects are interned when
986edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # unpickling.
987edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for proto in protocols:
988edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = C()
989edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.foo = 42
990edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x.bar = "hello"
991edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            s = self.dumps(x, proto)
992edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = self.loads(s)
993edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x_keys = sorted(x.__dict__)
994edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y_keys = sorted(y.__dict__)
995edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for x_key, y_key in zip(x_keys, y_keys):
996edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertIs(x_key, y_key)
997edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
998edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
999edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Test classes for reduce_ex
1000edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1001edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass REX_one(object):
1002edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    _reduce_called = 0
1003edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __reduce__(self):
1004edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._reduce_called = 1
1005edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return REX_one, ()
1006edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # No __reduce_ex__ here, but inheriting it from object
1007edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1008edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass REX_two(object):
1009edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    _proto = None
1010edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __reduce_ex__(self, proto):
1011edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._proto = proto
1012edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return REX_two, ()
1013edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # No __reduce__ here, but inheriting it from object
1014edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1015edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass REX_three(object):
1016edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    _proto = None
1017edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __reduce_ex__(self, proto):
1018edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._proto = proto
1019edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return REX_two, ()
1020edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __reduce__(self):
1021edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raise TestFailed, "This __reduce__ shouldn't be called"
1022edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1023edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass REX_four(object):
1024edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    _proto = None
1025edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __reduce_ex__(self, proto):
1026edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._proto = proto
1027edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return object.__reduce_ex__(self, proto)
1028edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Calling base class method should succeed
1029edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1030edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass REX_five(object):
1031edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    _reduce_called = 0
1032edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __reduce__(self):
1033edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._reduce_called = 1
1034edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return object.__reduce__(self)
1035edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # This one used to fail with infinite recursion
1036edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1037edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Test classes for newobj
1038edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1039edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass MyInt(int):
1040edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    sample = 1
1041edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1042edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass MyLong(long):
1043edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    sample = 1L
1044edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1045edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass MyFloat(float):
1046edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    sample = 1.0
1047edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1048edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass MyComplex(complex):
1049edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    sample = 1.0 + 0.0j
1050edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1051edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass MyStr(str):
1052edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    sample = "hello"
1053edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1054edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass MyUnicode(unicode):
1055edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    sample = u"hello \u1234"
1056edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1057edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass MyTuple(tuple):
1058edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    sample = (1, 2, 3)
1059edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1060edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass MyList(list):
1061edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    sample = [1, 2, 3]
1062edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1063edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass MyDict(dict):
1064edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    sample = {"a": 1, "b": 2}
1065edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1066edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepmyclasses = [MyInt, MyLong, MyFloat,
1067edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             MyComplex,
1068edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             MyStr, MyUnicode,
1069edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             MyTuple, MyList, MyDict]
1070edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1071edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1072edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass SlotList(MyList):
1073edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    __slots__ = ["foo"]
1074edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1075edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass SimpleNewObj(object):
1076edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, a, b, c):
1077edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # raise an error, to make sure this isn't called
1078edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raise TypeError("SimpleNewObj.__init__() didn't expect to get called")
1079edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1080edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass AbstractPickleModuleTests(unittest.TestCase):
1081edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1082edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_dump_closed_file(self):
1083edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import os
1084edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = open(TESTFN, "w")
1085edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1086edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            f.close()
1087edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertRaises(ValueError, self.module.dump, 123, f)
1088edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        finally:
1089edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            os.remove(TESTFN)
1090edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1091edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_load_closed_file(self):
1092edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import os
1093edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = open(TESTFN, "w")
1094edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1095edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            f.close()
1096edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertRaises(ValueError, self.module.dump, 123, f)
1097edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        finally:
1098edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            os.remove(TESTFN)
1099edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_load_from_and_dump_to_file(self):
1101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        stream = cStringIO.StringIO()
1102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        data = [123, {}, 124]
1103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.module.dump(data, stream)
1104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        stream.seek(0)
1105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        unpickled = self.module.load(stream)
1106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(unpickled, data)
1107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_highest_protocol(self):
1109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Of course this needs to be changed when HIGHEST_PROTOCOL changes.
1110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.module.HIGHEST_PROTOCOL, 2)
1111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_callapi(self):
1113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = cStringIO.StringIO()
1114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # With and without keyword arguments
1115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.module.dump(123, f, -1)
1116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.module.dump(123, file=f, protocol=-1)
1117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.module.dumps(123, -1)
1118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.module.dumps(123, protocol=-1)
1119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.module.Pickler(f, -1)
1120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.module.Pickler(f, protocol=-1)
1121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_incomplete_input(self):
1123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = StringIO.StringIO("X''.")
1124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(EOFError, self.module.load, s)
1125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_restricted(self):
1127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # issue7128: cPickle failed in restricted mode
1128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        builtins = {self.module.__name__: self.module,
1129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    '__import__': __import__}
1130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d = {}
1131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        teststr = "def f(): {0}.dumps(0)".format(self.module.__name__)
1132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        exec teststr in {'__builtins__': builtins}, d
1133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        d['f']()
1134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_bad_input(self):
1136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test issue4298
1137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = '\x58\0\0\0\x54'
1138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(EOFError, self.module.loads, s)
1139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test issue7455
1140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        s = '0'
1141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # XXX Why doesn't pickle raise UnpicklingError?
1142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises((IndexError, cPickle.UnpicklingError),
1143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                          self.module.loads, s)
1144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass AbstractPersistentPicklerTests(unittest.TestCase):
1146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # This class defines persistent_id() and persistent_load()
1148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # functions that should be used by the pickler.  All even integers
1149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # are pickled using persistent ids.
1150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def persistent_id(self, object):
1152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if isinstance(object, int) and object % 2 == 0:
1153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.id_count += 1
1154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return str(object)
1155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
1156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return None
1157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def persistent_load(self, oid):
1159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.load_count += 1
1160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        object = int(oid)
1161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        assert object % 2 == 0
1162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return object
1163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_persistence(self):
1165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.id_count = 0
1166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.load_count = 0
1167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        L = range(10)
1168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.loads(self.dumps(L)), L)
1169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.id_count, 5)
1170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.load_count, 5)
1171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_bin_persistence(self):
1173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.id_count = 0
1174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.load_count = 0
1175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        L = range(10)
1176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.loads(self.dumps(L, 1)), L)
1177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.id_count, 5)
1178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(self.load_count, 5)
1179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass AbstractPicklerUnpicklerObjectTests(unittest.TestCase):
1181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    pickler_class = None
1183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    unpickler_class = None
1184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def setUp(self):
1186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        assert self.pickler_class
1187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        assert self.unpickler_class
1188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_clear_pickler_memo(self):
1190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # To test whether clear_memo() has any effect, we pickle an object,
1191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # then pickle it again without clearing the memo; the two serialized
1192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # forms should be different. If we clear_memo() and then pickle the
1193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # object again, the third serialized form should be identical to the
1194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # first one we obtained.
1195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        data = ["abcdefg", "abcdefg", 44]
1196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = cStringIO.StringIO()
1197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickler = self.pickler_class(f)
1198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickler.dump(data)
1200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        first_pickled = f.getvalue()
1201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Reset StringIO object.
1203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f.seek(0)
1204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f.truncate()
1205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickler.dump(data)
1207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        second_pickled = f.getvalue()
1208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Reset the Pickler and StringIO objects.
1210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickler.clear_memo()
1211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f.seek(0)
1212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f.truncate()
1213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickler.dump(data)
1215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        third_pickled = f.getvalue()
1216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotEqual(first_pickled, second_pickled)
1218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(first_pickled, third_pickled)
1219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_priming_pickler_memo(self):
1221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Verify that we can set the Pickler's memo attribute.
1222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        data = ["abcdefg", "abcdefg", 44]
1223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = cStringIO.StringIO()
1224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickler = self.pickler_class(f)
1225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickler.dump(data)
1227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        first_pickled = f.getvalue()
1228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = cStringIO.StringIO()
1230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        primed = self.pickler_class(f)
1231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        primed.memo = pickler.memo
1232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        primed.dump(data)
1234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        primed_pickled = f.getvalue()
1235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertNotEqual(first_pickled, primed_pickled)
1237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_priming_unpickler_memo(self):
1239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Verify that we can set the Unpickler's memo attribute.
1240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        data = ["abcdefg", "abcdefg", 44]
1241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = cStringIO.StringIO()
1242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickler = self.pickler_class(f)
1243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickler.dump(data)
1245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        first_pickled = f.getvalue()
1246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = cStringIO.StringIO()
1248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        primed = self.pickler_class(f)
1249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        primed.memo = pickler.memo
1250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        primed.dump(data)
1252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        primed_pickled = f.getvalue()
1253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        unpickler = self.unpickler_class(cStringIO.StringIO(first_pickled))
1255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        unpickled_data1 = unpickler.load()
1256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(unpickled_data1, data)
1258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        primed = self.unpickler_class(cStringIO.StringIO(primed_pickled))
1260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        primed.memo = unpickler.memo
1261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        unpickled_data2 = primed.load()
1262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        primed.memo.clear()
1264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(unpickled_data2, data)
1266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(unpickled_data2 is unpickled_data1)
1267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_reusing_unpickler_objects(self):
1269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        data1 = ["abcdefg", "abcdefg", 44]
1270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = cStringIO.StringIO()
1271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickler = self.pickler_class(f)
1272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickler.dump(data1)
1273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickled1 = f.getvalue()
1274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        data2 = ["abcdefg", 44, 44]
1276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = cStringIO.StringIO()
1277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickler = self.pickler_class(f)
1278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickler.dump(data2)
1279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pickled2 = f.getvalue()
1280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f = cStringIO.StringIO()
1282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f.write(pickled1)
1283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f.seek(0)
1284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        unpickler = self.unpickler_class(f)
1285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(unpickler.load(), data1)
1286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f.seek(0)
1288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f.truncate()
1289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f.write(pickled2)
1290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        f.seek(0)
1291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(unpickler.load(), data2)
1292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass BigmemPickleTests(unittest.TestCase):
1294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Memory requirements: 1 byte per character for input strings, 1 byte
1296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # for pickled data, 1 byte for unpickled strings, 1 byte for internal
1297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # buffer and 1 byte of free space for resizing of internal buffer.
1298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    @precisionbigmemtest(size=_2G + 100*_1M, memuse=5)
1300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_huge_strlist(self, size):
1301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        chunksize = 2**20
1302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        data = []
1303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        while size > chunksize:
1304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            data.append('x' * chunksize)
1305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            size -= chunksize
1306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            chunksize += 1
1307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        data.append('y' * size)
1308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
1309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
1310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for proto in protocols:
1311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                try:
1312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    pickled = self.dumps(data, proto)
1313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    res = self.loads(pickled)
1314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.assertEqual(res, data)
1315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                finally:
1316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    res = None
1317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    pickled = None
1318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        finally:
1319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            data = None
1320