blob: 487b5336253eadc10dfd1602afbddcbb4187dc66 [file] [log] [blame]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001#include "Python.h"
2#include "structmember.h"
3
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004PyDoc_STRVAR(pickle_module_doc,
5"Optimized C implementation for the Python pickle module.");
6
Larry Hastings61272b72014-01-07 12:41:53 -08007/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08008module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -08009class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
10class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
11class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
12class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080013[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030014/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000016/* Bump this when new opcodes are added to the pickle protocol. */
17enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010018 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000019 DEFAULT_PROTOCOL = 3
20};
21
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000022/* Pickle opcodes. These must be kept updated with pickle.py.
23 Extensive docs are in pickletools.py. */
24enum opcode {
25 MARK = '(',
26 STOP = '.',
27 POP = '0',
28 POP_MARK = '1',
29 DUP = '2',
30 FLOAT = 'F',
31 INT = 'I',
32 BININT = 'J',
33 BININT1 = 'K',
34 LONG = 'L',
35 BININT2 = 'M',
36 NONE = 'N',
37 PERSID = 'P',
38 BINPERSID = 'Q',
39 REDUCE = 'R',
40 STRING = 'S',
41 BINSTRING = 'T',
42 SHORT_BINSTRING = 'U',
43 UNICODE = 'V',
44 BINUNICODE = 'X',
45 APPEND = 'a',
46 BUILD = 'b',
47 GLOBAL = 'c',
48 DICT = 'd',
49 EMPTY_DICT = '}',
50 APPENDS = 'e',
51 GET = 'g',
52 BINGET = 'h',
53 INST = 'i',
54 LONG_BINGET = 'j',
55 LIST = 'l',
56 EMPTY_LIST = ']',
57 OBJ = 'o',
58 PUT = 'p',
59 BINPUT = 'q',
60 LONG_BINPUT = 'r',
61 SETITEM = 's',
62 TUPLE = 't',
63 EMPTY_TUPLE = ')',
64 SETITEMS = 'u',
65 BINFLOAT = 'G',
66
67 /* Protocol 2. */
68 PROTO = '\x80',
69 NEWOBJ = '\x81',
70 EXT1 = '\x82',
71 EXT2 = '\x83',
72 EXT4 = '\x84',
73 TUPLE1 = '\x85',
74 TUPLE2 = '\x86',
75 TUPLE3 = '\x87',
76 NEWTRUE = '\x88',
77 NEWFALSE = '\x89',
78 LONG1 = '\x8a',
79 LONG4 = '\x8b',
80
81 /* Protocol 3 (Python 3.x) */
82 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010083 SHORT_BINBYTES = 'C',
84
85 /* Protocol 4 */
86 SHORT_BINUNICODE = '\x8c',
87 BINUNICODE8 = '\x8d',
88 BINBYTES8 = '\x8e',
89 EMPTY_SET = '\x8f',
90 ADDITEMS = '\x90',
91 FROZENSET = '\x91',
92 NEWOBJ_EX = '\x92',
93 STACK_GLOBAL = '\x93',
94 MEMOIZE = '\x94',
95 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000096};
97
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000098enum {
99 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
100 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
101 break if this gets out of synch with pickle.py, but it's unclear that would
102 help anything either. */
103 BATCHSIZE = 1000,
104
105 /* Nesting limit until Pickler, when running in "fast mode", starts
106 checking for self-referential data-structures. */
107 FAST_NESTING_LIMIT = 50,
108
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000109 /* Initial size of the write buffer of Pickler. */
110 WRITE_BUF_SIZE = 4096,
111
Antoine Pitrou04248a82010-10-12 20:51:21 +0000112 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100113 PREFETCH = 8192 * 16,
114
115 FRAME_SIZE_TARGET = 64 * 1024,
116
117 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000118};
119
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800120/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000121
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800122/* State of the pickle module, per PEP 3121. */
123typedef struct {
124 /* Exception classes for pickle. */
125 PyObject *PickleError;
126 PyObject *PicklingError;
127 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129 /* copyreg.dispatch_table, {type_object: pickling_function} */
130 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000131
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800132 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000133
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800134 /* copyreg._extension_registry, {(module_name, function_name): code} */
135 PyObject *extension_registry;
136 /* copyreg._extension_cache, {code: object} */
137 PyObject *extension_cache;
138 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
139 PyObject *inverted_registry;
140
141 /* Import mappings for compatibility with Python 2.x */
142
143 /* _compat_pickle.NAME_MAPPING,
144 {(oldmodule, oldname): (newmodule, newname)} */
145 PyObject *name_mapping_2to3;
146 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
147 PyObject *import_mapping_2to3;
148 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
149 PyObject *name_mapping_3to2;
150 PyObject *import_mapping_3to2;
151
152 /* codecs.encode, used for saving bytes in older protocols */
153 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300154 /* builtins.getattr, used for saving nested names with protocol < 4 */
155 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300156 /* functools.partial, used for implementing __newobj_ex__ with protocols
157 2 and 3 */
158 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800159} PickleState;
160
161/* Forward declaration of the _pickle module definition. */
162static struct PyModuleDef _picklemodule;
163
164/* Given a module object, get its per-module state. */
165static PickleState *
166_Pickle_GetState(PyObject *module)
167{
168 return (PickleState *)PyModule_GetState(module);
169}
170
171/* Find the module instance imported in the currently running sub-interpreter
172 and get its state. */
173static PickleState *
174_Pickle_GetGlobalState(void)
175{
176 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
177}
178
179/* Clear the given pickle module state. */
180static void
181_Pickle_ClearState(PickleState *st)
182{
183 Py_CLEAR(st->PickleError);
184 Py_CLEAR(st->PicklingError);
185 Py_CLEAR(st->UnpicklingError);
186 Py_CLEAR(st->dispatch_table);
187 Py_CLEAR(st->extension_registry);
188 Py_CLEAR(st->extension_cache);
189 Py_CLEAR(st->inverted_registry);
190 Py_CLEAR(st->name_mapping_2to3);
191 Py_CLEAR(st->import_mapping_2to3);
192 Py_CLEAR(st->name_mapping_3to2);
193 Py_CLEAR(st->import_mapping_3to2);
194 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300195 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100196 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800197}
198
199/* Initialize the given pickle module state. */
200static int
201_Pickle_InitState(PickleState *st)
202{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300203 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800204 PyObject *copyreg = NULL;
205 PyObject *compat_pickle = NULL;
206 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300207 PyObject *functools = NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800208
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300209 builtins = PyEval_GetBuiltins();
210 if (builtins == NULL)
211 goto error;
212 st->getattr = PyDict_GetItemString(builtins, "getattr");
213 if (st->getattr == NULL)
214 goto error;
215 Py_INCREF(st->getattr);
216
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800217 copyreg = PyImport_ImportModule("copyreg");
218 if (!copyreg)
219 goto error;
220 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
221 if (!st->dispatch_table)
222 goto error;
223 if (!PyDict_CheckExact(st->dispatch_table)) {
224 PyErr_Format(PyExc_RuntimeError,
225 "copyreg.dispatch_table should be a dict, not %.200s",
226 Py_TYPE(st->dispatch_table)->tp_name);
227 goto error;
228 }
229 st->extension_registry = \
230 PyObject_GetAttrString(copyreg, "_extension_registry");
231 if (!st->extension_registry)
232 goto error;
233 if (!PyDict_CheckExact(st->extension_registry)) {
234 PyErr_Format(PyExc_RuntimeError,
235 "copyreg._extension_registry should be a dict, "
236 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
237 goto error;
238 }
239 st->inverted_registry = \
240 PyObject_GetAttrString(copyreg, "_inverted_registry");
241 if (!st->inverted_registry)
242 goto error;
243 if (!PyDict_CheckExact(st->inverted_registry)) {
244 PyErr_Format(PyExc_RuntimeError,
245 "copyreg._inverted_registry should be a dict, "
246 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
247 goto error;
248 }
249 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
250 if (!st->extension_cache)
251 goto error;
252 if (!PyDict_CheckExact(st->extension_cache)) {
253 PyErr_Format(PyExc_RuntimeError,
254 "copyreg._extension_cache should be a dict, "
255 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
256 goto error;
257 }
258 Py_CLEAR(copyreg);
259
260 /* Load the 2.x -> 3.x stdlib module mapping tables */
261 compat_pickle = PyImport_ImportModule("_compat_pickle");
262 if (!compat_pickle)
263 goto error;
264 st->name_mapping_2to3 = \
265 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
266 if (!st->name_mapping_2to3)
267 goto error;
268 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
269 PyErr_Format(PyExc_RuntimeError,
270 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
271 Py_TYPE(st->name_mapping_2to3)->tp_name);
272 goto error;
273 }
274 st->import_mapping_2to3 = \
275 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
276 if (!st->import_mapping_2to3)
277 goto error;
278 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
279 PyErr_Format(PyExc_RuntimeError,
280 "_compat_pickle.IMPORT_MAPPING should be a dict, "
281 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
282 goto error;
283 }
284 /* ... and the 3.x -> 2.x mapping tables */
285 st->name_mapping_3to2 = \
286 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
287 if (!st->name_mapping_3to2)
288 goto error;
289 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
290 PyErr_Format(PyExc_RuntimeError,
291 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
292 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
293 goto error;
294 }
295 st->import_mapping_3to2 = \
296 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
297 if (!st->import_mapping_3to2)
298 goto error;
299 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
300 PyErr_Format(PyExc_RuntimeError,
301 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
302 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
303 goto error;
304 }
305 Py_CLEAR(compat_pickle);
306
307 codecs = PyImport_ImportModule("codecs");
308 if (codecs == NULL)
309 goto error;
310 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
311 if (st->codecs_encode == NULL) {
312 goto error;
313 }
314 if (!PyCallable_Check(st->codecs_encode)) {
315 PyErr_Format(PyExc_RuntimeError,
316 "codecs.encode should be a callable, not %.200s",
317 Py_TYPE(st->codecs_encode)->tp_name);
318 goto error;
319 }
320 Py_CLEAR(codecs);
321
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300322 functools = PyImport_ImportModule("functools");
323 if (!functools)
324 goto error;
325 st->partial = PyObject_GetAttrString(functools, "partial");
326 if (!st->partial)
327 goto error;
328 Py_CLEAR(functools);
329
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800330 return 0;
331
332 error:
333 Py_CLEAR(copyreg);
334 Py_CLEAR(compat_pickle);
335 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300336 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800337 _Pickle_ClearState(st);
338 return -1;
339}
340
341/* Helper for calling a function with a single argument quickly.
342
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800343 This function steals the reference of the given argument. */
344static PyObject *
345_Pickle_FastCall(PyObject *func, PyObject *obj)
346{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800347 PyObject *result;
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800348 PyObject *arg_tuple = PyTuple_New(1);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800349
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800350 /* Note: this function used to reuse the argument tuple. This used to give
351 a slight performance boost with older pickle implementations where many
352 unbuffered reads occurred (thus needing many function calls).
353
354 However, this optimization was removed because it was too complicated
355 to get right. It abused the C API for tuples to mutate them which led
356 to subtle reference counting and concurrency bugs. Furthermore, the
357 introduction of protocol 4 and the prefetching optimization via peek()
358 significantly reduced the number of function calls we do. Thus, the
359 benefits became marginal at best. */
360
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800361 if (arg_tuple == NULL) {
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800362 Py_DECREF(obj);
363 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800364 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800365 PyTuple_SET_ITEM(arg_tuple, 0, obj);
366 result = PyObject_Call(func, arg_tuple, NULL);
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800367 Py_CLEAR(arg_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800368 return result;
369}
370
371/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000372
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000373/* Internal data type used as the unpickling stack. */
374typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000375 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000376 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200377 int mark_set; /* is MARK set? */
378 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000379 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000380} Pdata;
381
382static void
383Pdata_dealloc(Pdata *self)
384{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200385 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000386 while (--i >= 0) {
387 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000388 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000389 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000390 PyObject_Del(self);
391}
392
393static PyTypeObject Pdata_Type = {
394 PyVarObject_HEAD_INIT(NULL, 0)
395 "_pickle.Pdata", /*tp_name*/
396 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200397 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000398 (destructor)Pdata_dealloc, /*tp_dealloc*/
399};
400
401static PyObject *
402Pdata_New(void)
403{
404 Pdata *self;
405
406 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
407 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000408 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200409 self->mark_set = 0;
410 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000411 self->allocated = 8;
412 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000413 if (self->data)
414 return (PyObject *)self;
415 Py_DECREF(self);
416 return PyErr_NoMemory();
417}
418
419
420/* Retain only the initial clearto items. If clearto >= the current
421 * number of items, this is a (non-erroneous) NOP.
422 */
423static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200424Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000425{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200426 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000427
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200428 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000429 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000430 return 0;
431
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000432 while (--i >= clearto) {
433 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000434 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000435 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000436 return 0;
437}
438
439static int
440Pdata_grow(Pdata *self)
441{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000442 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200443 size_t allocated = (size_t)self->allocated;
444 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000445
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000446 new_allocated = (allocated >> 3) + 6;
447 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200448 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000449 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000450 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500451 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000452 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000453 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000454
455 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200456 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000457 return 0;
458
459 nomemory:
460 PyErr_NoMemory();
461 return -1;
462}
463
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200464static int
465Pdata_stack_underflow(Pdata *self)
466{
467 PickleState *st = _Pickle_GetGlobalState();
468 PyErr_SetString(st->UnpicklingError,
469 self->mark_set ?
470 "unexpected MARK found" :
471 "unpickling stack underflow");
472 return -1;
473}
474
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000475/* D is a Pdata*. Pop the topmost element and store it into V, which
476 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
477 * is raised and V is set to NULL.
478 */
479static PyObject *
480Pdata_pop(Pdata *self)
481{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200482 if (Py_SIZE(self) <= self->fence) {
483 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000484 return NULL;
485 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000486 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000487}
488#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
489
490static int
491Pdata_push(Pdata *self, PyObject *obj)
492{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000493 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000494 return -1;
495 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000496 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000497 return 0;
498}
499
500/* Push an object on stack, transferring its ownership to the stack. */
501#define PDATA_PUSH(D, O, ER) do { \
502 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
503
504/* Push an object on stack, adding a new reference to the object. */
505#define PDATA_APPEND(D, O, ER) do { \
506 Py_INCREF((O)); \
507 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
508
509static PyObject *
510Pdata_poptuple(Pdata *self, Py_ssize_t start)
511{
512 PyObject *tuple;
513 Py_ssize_t len, i, j;
514
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200515 if (start < self->fence) {
516 Pdata_stack_underflow(self);
517 return NULL;
518 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000519 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000520 tuple = PyTuple_New(len);
521 if (tuple == NULL)
522 return NULL;
523 for (i = start, j = 0; j < len; i++, j++)
524 PyTuple_SET_ITEM(tuple, j, self->data[i]);
525
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000526 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000527 return tuple;
528}
529
530static PyObject *
531Pdata_poplist(Pdata *self, Py_ssize_t start)
532{
533 PyObject *list;
534 Py_ssize_t len, i, j;
535
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000536 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000537 list = PyList_New(len);
538 if (list == NULL)
539 return NULL;
540 for (i = start, j = 0; j < len; i++, j++)
541 PyList_SET_ITEM(list, j, self->data[i]);
542
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000543 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000544 return list;
545}
546
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000547typedef struct {
548 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200549 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000550} PyMemoEntry;
551
552typedef struct {
553 Py_ssize_t mt_mask;
554 Py_ssize_t mt_used;
555 Py_ssize_t mt_allocated;
556 PyMemoEntry *mt_table;
557} PyMemoTable;
558
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000559typedef struct PicklerObject {
560 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000561 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000562 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000563 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000564 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100565 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000566
567 PyObject *write; /* write() method of the output stream. */
568 PyObject *output_buffer; /* Write into a local bytearray buffer before
569 flushing to the stream. */
570 Py_ssize_t output_len; /* Length of output_buffer. */
571 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000572 int proto; /* Pickle protocol number, >= 0 */
573 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100574 int framing; /* True when framing is enabled, proto >= 4 */
575 Py_ssize_t frame_start; /* Position in output_buffer where the
576 where the current frame begins. -1 if there
577 is no frame currently open. */
578
579 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000580 int fast; /* Enable fast mode if set to a true value.
581 The fast mode disable the usage of memo,
582 therefore speeding the pickling process by
583 not generating superfluous PUT opcodes. It
584 should not be used if with self-referential
585 objects. */
586 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000587 int fix_imports; /* Indicate whether Pickler should fix
588 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000589 PyObject *fast_memo;
590} PicklerObject;
591
592typedef struct UnpicklerObject {
593 PyObject_HEAD
594 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000595
596 /* The unpickler memo is just an array of PyObject *s. Using a dict
597 is unnecessary, since the keys are contiguous ints. */
598 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100599 Py_ssize_t memo_size; /* Capacity of the memo array */
600 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000601
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000602 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000603
604 Py_buffer buffer;
605 char *input_buffer;
606 char *input_line;
607 Py_ssize_t input_len;
608 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000609 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100610
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000611 PyObject *read; /* read() method of the input stream. */
612 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000613 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000614
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000615 char *encoding; /* Name of the encoding to be used for
616 decoding strings pickled using Python
617 2.x. The default value is "ASCII" */
618 char *errors; /* Name of errors handling scheme to used when
619 decoding strings. The default value is
620 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500621 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000622 objects. */
623 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
624 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000625 int proto; /* Protocol of the pickle loaded. */
626 int fix_imports; /* Indicate whether Unpickler should fix
627 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000628} UnpicklerObject;
629
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200630typedef struct {
631 PyObject_HEAD
632 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
633} PicklerMemoProxyObject;
634
635typedef struct {
636 PyObject_HEAD
637 UnpicklerObject *unpickler;
638} UnpicklerMemoProxyObject;
639
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000640/* Forward declarations */
641static int save(PicklerObject *, PyObject *, int);
642static int save_reduce(PicklerObject *, PyObject *, PyObject *);
643static PyTypeObject Pickler_Type;
644static PyTypeObject Unpickler_Type;
645
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200646#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000647
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000648/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300649 A custom hashtable mapping void* to Python ints. This is used by the pickler
650 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000651 a bunch of unnecessary object creation. This makes a huge performance
652 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000653
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000654#define MT_MINSIZE 8
655#define PERTURB_SHIFT 5
656
657
658static PyMemoTable *
659PyMemoTable_New(void)
660{
661 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
662 if (memo == NULL) {
663 PyErr_NoMemory();
664 return NULL;
665 }
666
667 memo->mt_used = 0;
668 memo->mt_allocated = MT_MINSIZE;
669 memo->mt_mask = MT_MINSIZE - 1;
670 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
671 if (memo->mt_table == NULL) {
672 PyMem_FREE(memo);
673 PyErr_NoMemory();
674 return NULL;
675 }
676 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
677
678 return memo;
679}
680
681static PyMemoTable *
682PyMemoTable_Copy(PyMemoTable *self)
683{
684 Py_ssize_t i;
685 PyMemoTable *new = PyMemoTable_New();
686 if (new == NULL)
687 return NULL;
688
689 new->mt_used = self->mt_used;
690 new->mt_allocated = self->mt_allocated;
691 new->mt_mask = self->mt_mask;
692 /* The table we get from _New() is probably smaller than we wanted.
693 Free it and allocate one that's the right size. */
694 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500695 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000696 if (new->mt_table == NULL) {
697 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200698 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000699 return NULL;
700 }
701 for (i = 0; i < self->mt_allocated; i++) {
702 Py_XINCREF(self->mt_table[i].me_key);
703 }
704 memcpy(new->mt_table, self->mt_table,
705 sizeof(PyMemoEntry) * self->mt_allocated);
706
707 return new;
708}
709
710static Py_ssize_t
711PyMemoTable_Size(PyMemoTable *self)
712{
713 return self->mt_used;
714}
715
716static int
717PyMemoTable_Clear(PyMemoTable *self)
718{
719 Py_ssize_t i = self->mt_allocated;
720
721 while (--i >= 0) {
722 Py_XDECREF(self->mt_table[i].me_key);
723 }
724 self->mt_used = 0;
725 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
726 return 0;
727}
728
729static void
730PyMemoTable_Del(PyMemoTable *self)
731{
732 if (self == NULL)
733 return;
734 PyMemoTable_Clear(self);
735
736 PyMem_FREE(self->mt_table);
737 PyMem_FREE(self);
738}
739
740/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
741 can be considerably simpler than dictobject.c's lookdict(). */
742static PyMemoEntry *
743_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
744{
745 size_t i;
746 size_t perturb;
747 size_t mask = (size_t)self->mt_mask;
748 PyMemoEntry *table = self->mt_table;
749 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000750 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000751
752 i = hash & mask;
753 entry = &table[i];
754 if (entry->me_key == NULL || entry->me_key == key)
755 return entry;
756
757 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
758 i = (i << 2) + i + perturb + 1;
759 entry = &table[i & mask];
760 if (entry->me_key == NULL || entry->me_key == key)
761 return entry;
762 }
763 assert(0); /* Never reached */
764 return NULL;
765}
766
767/* Returns -1 on failure, 0 on success. */
768static int
769_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
770{
771 PyMemoEntry *oldtable = NULL;
772 PyMemoEntry *oldentry, *newentry;
773 Py_ssize_t new_size = MT_MINSIZE;
774 Py_ssize_t to_process;
775
776 assert(min_size > 0);
777
778 /* Find the smallest valid table size >= min_size. */
779 while (new_size < min_size && new_size > 0)
780 new_size <<= 1;
781 if (new_size <= 0) {
782 PyErr_NoMemory();
783 return -1;
784 }
785 /* new_size needs to be a power of two. */
786 assert((new_size & (new_size - 1)) == 0);
787
788 /* Allocate new table. */
789 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500790 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000791 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200792 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000793 PyErr_NoMemory();
794 return -1;
795 }
796 self->mt_allocated = new_size;
797 self->mt_mask = new_size - 1;
798 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
799
800 /* Copy entries from the old table. */
801 to_process = self->mt_used;
802 for (oldentry = oldtable; to_process > 0; oldentry++) {
803 if (oldentry->me_key != NULL) {
804 to_process--;
805 /* newentry is a pointer to a chunk of the new
806 mt_table, so we're setting the key:value pair
807 in-place. */
808 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
809 newentry->me_key = oldentry->me_key;
810 newentry->me_value = oldentry->me_value;
811 }
812 }
813
814 /* Deallocate the old table. */
815 PyMem_FREE(oldtable);
816 return 0;
817}
818
819/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200820static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000821PyMemoTable_Get(PyMemoTable *self, PyObject *key)
822{
823 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
824 if (entry->me_key == NULL)
825 return NULL;
826 return &entry->me_value;
827}
828
829/* Returns -1 on failure, 0 on success. */
830static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200831PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000832{
833 PyMemoEntry *entry;
834
835 assert(key != NULL);
836
837 entry = _PyMemoTable_Lookup(self, key);
838 if (entry->me_key != NULL) {
839 entry->me_value = value;
840 return 0;
841 }
842 Py_INCREF(key);
843 entry->me_key = key;
844 entry->me_value = value;
845 self->mt_used++;
846
847 /* If we added a key, we can safely resize. Otherwise just return!
848 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
849 *
850 * Quadrupling the size improves average table sparseness
851 * (reducing collisions) at the cost of some memory. It also halves
852 * the number of expensive resize operations in a growing memo table.
853 *
854 * Very large memo tables (over 50K items) use doubling instead.
855 * This may help applications with severe memory constraints.
856 */
857 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
858 return 0;
859 return _PyMemoTable_ResizeTable(self,
860 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
861}
862
863#undef MT_MINSIZE
864#undef PERTURB_SHIFT
865
866/*************************************************************************/
867
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000868
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000869static int
870_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000871{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000872 Py_CLEAR(self->output_buffer);
873 self->output_buffer =
874 PyBytes_FromStringAndSize(NULL, self->max_output_len);
875 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000876 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000877 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100878 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000879 return 0;
880}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000881
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100882static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100883_write_size64(char *out, size_t value)
884{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200885 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800886
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200887 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800888
889 for (i = 0; i < sizeof(size_t); i++) {
890 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
891 }
892 for (i = sizeof(size_t); i < 8; i++) {
893 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800894 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100895}
896
897static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100898_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
899{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100900 qdata[0] = FRAME;
901 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100902}
903
904static int
905_Pickler_CommitFrame(PicklerObject *self)
906{
907 size_t frame_len;
908 char *qdata;
909
910 if (!self->framing || self->frame_start == -1)
911 return 0;
912 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
913 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
914 _Pickler_WriteFrameHeader(self, qdata, frame_len);
915 self->frame_start = -1;
916 return 0;
917}
918
919static int
920_Pickler_OpcodeBoundary(PicklerObject *self)
921{
922 Py_ssize_t frame_len;
923
924 if (!self->framing || self->frame_start == -1)
925 return 0;
926 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
927 if (frame_len >= FRAME_SIZE_TARGET)
928 return _Pickler_CommitFrame(self);
929 else
930 return 0;
931}
932
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000933static PyObject *
934_Pickler_GetString(PicklerObject *self)
935{
936 PyObject *output_buffer = self->output_buffer;
937
938 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100939
940 if (_Pickler_CommitFrame(self))
941 return NULL;
942
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000943 self->output_buffer = NULL;
944 /* Resize down to exact size */
945 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
946 return NULL;
947 return output_buffer;
948}
949
950static int
951_Pickler_FlushToFile(PicklerObject *self)
952{
953 PyObject *output, *result;
954
955 assert(self->write != NULL);
956
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100957 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000958 output = _Pickler_GetString(self);
959 if (output == NULL)
960 return -1;
961
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800962 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000963 Py_XDECREF(result);
964 return (result == NULL) ? -1 : 0;
965}
966
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200967static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100968_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000969{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100970 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000971 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100972 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000973
974 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100975 need_new_frame = (self->framing && self->frame_start == -1);
976
977 if (need_new_frame)
978 n = data_len + FRAME_HEADER_SIZE;
979 else
980 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000981
982 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100983 if (required > self->max_output_len) {
984 /* Make place in buffer for the pickle chunk */
985 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
986 PyErr_NoMemory();
987 return -1;
988 }
989 self->max_output_len = (self->output_len + n) / 2 * 3;
990 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
991 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000992 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000993 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100994 if (need_new_frame) {
995 /* Setup new frame */
996 Py_ssize_t frame_start = self->output_len;
997 self->frame_start = frame_start;
998 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
999 /* Write an invalid value, for debugging */
1000 buffer[frame_start + i] = 0xFE;
1001 }
1002 self->output_len += FRAME_HEADER_SIZE;
1003 }
1004 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001005 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001006 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001007 buffer[self->output_len + i] = s[i];
1008 }
1009 }
1010 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001011 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001012 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001013 self->output_len += data_len;
1014 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001015}
1016
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001017static PicklerObject *
1018_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001019{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001020 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001021
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001022 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1023 if (self == NULL)
1024 return NULL;
1025
1026 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001027 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001028 self->write = NULL;
1029 self->proto = 0;
1030 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001031 self->framing = 0;
1032 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001033 self->fast = 0;
1034 self->fast_nesting = 0;
1035 self->fix_imports = 0;
1036 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001037 self->max_output_len = WRITE_BUF_SIZE;
1038 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001039
1040 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001041 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1042 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001043
1044 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001045 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001046 return NULL;
1047 }
1048 return self;
1049}
1050
1051static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001052_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001053{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001054 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001055
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001056 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001057 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001058 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001059 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001060 proto = PyLong_AsLong(protocol);
1061 if (proto < 0) {
1062 if (proto == -1 && PyErr_Occurred())
1063 return -1;
1064 proto = HIGHEST_PROTOCOL;
1065 }
1066 else if (proto > HIGHEST_PROTOCOL) {
1067 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1068 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001069 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001070 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001071 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001072 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001073 self->bin = proto > 0;
1074 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001075 return 0;
1076}
1077
1078/* Returns -1 (with an exception set) on failure, 0 on success. This may
1079 be called once on a freshly created Pickler. */
1080static int
1081_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1082{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001083 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001084 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001085 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001086 if (self->write == NULL) {
1087 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1088 PyErr_SetString(PyExc_TypeError,
1089 "file must have a 'write' attribute");
1090 return -1;
1091 }
1092
1093 return 0;
1094}
1095
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001096/* Returns the size of the input on success, -1 on failure. This takes its
1097 own reference to `input`. */
1098static Py_ssize_t
1099_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1100{
1101 if (self->buffer.buf != NULL)
1102 PyBuffer_Release(&self->buffer);
1103 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1104 return -1;
1105 self->input_buffer = self->buffer.buf;
1106 self->input_len = self->buffer.len;
1107 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001108 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001109 return self->input_len;
1110}
1111
Antoine Pitrou04248a82010-10-12 20:51:21 +00001112static int
1113_Unpickler_SkipConsumed(UnpicklerObject *self)
1114{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001115 Py_ssize_t consumed;
1116 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001117
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001118 consumed = self->next_read_idx - self->prefetched_idx;
1119 if (consumed <= 0)
1120 return 0;
1121
1122 assert(self->peek); /* otherwise we did something wrong */
1123 /* This makes an useless copy... */
1124 r = PyObject_CallFunction(self->read, "n", consumed);
1125 if (r == NULL)
1126 return -1;
1127 Py_DECREF(r);
1128
1129 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001130 return 0;
1131}
1132
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001133static const Py_ssize_t READ_WHOLE_LINE = -1;
1134
1135/* If reading from a file, we need to only pull the bytes we need, since there
1136 may be multiple pickle objects arranged contiguously in the same input
1137 buffer.
1138
1139 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1140 bytes from the input stream/buffer.
1141
1142 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1143 failure; on success, returns the number of bytes read from the file.
1144
1145 On success, self->input_len will be 0; this is intentional so that when
1146 unpickling from a file, the "we've run out of data" code paths will trigger,
1147 causing the Unpickler to go back to the file for more data. Use the returned
1148 size to tell you how much data you can process. */
1149static Py_ssize_t
1150_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1151{
1152 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001153 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001154
1155 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001156
Antoine Pitrou04248a82010-10-12 20:51:21 +00001157 if (_Unpickler_SkipConsumed(self) < 0)
1158 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001159
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001160 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001161 PyObject *empty_tuple = PyTuple_New(0);
1162 data = PyObject_Call(self->readline, empty_tuple, NULL);
1163 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001164 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001165 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001166 PyObject *len;
1167 /* Prefetch some data without advancing the file pointer, if possible */
1168 if (self->peek && n < PREFETCH) {
1169 len = PyLong_FromSsize_t(PREFETCH);
1170 if (len == NULL)
1171 return -1;
1172 data = _Pickle_FastCall(self->peek, len);
1173 if (data == NULL) {
1174 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1175 return -1;
1176 /* peek() is probably not supported by the given file object */
1177 PyErr_Clear();
1178 Py_CLEAR(self->peek);
1179 }
1180 else {
1181 read_size = _Unpickler_SetStringInput(self, data);
1182 Py_DECREF(data);
1183 self->prefetched_idx = 0;
1184 if (n <= read_size)
1185 return n;
1186 }
1187 }
1188 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001189 if (len == NULL)
1190 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001191 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001192 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001193 if (data == NULL)
1194 return -1;
1195
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001196 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001197 Py_DECREF(data);
1198 return read_size;
1199}
1200
1201/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1202
1203 This should be used for all data reads, rather than accessing the unpickler's
1204 input buffer directly. This method deals correctly with reading from input
1205 streams, which the input buffer doesn't deal with.
1206
1207 Note that when reading from a file-like object, self->next_read_idx won't
1208 be updated (it should remain at 0 for the entire unpickling process). You
1209 should use this function's return value to know how many bytes you can
1210 consume.
1211
1212 Returns -1 (with an exception set) on failure. On success, return the
1213 number of chars read. */
1214static Py_ssize_t
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001215_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001216{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001217 Py_ssize_t num_read;
1218
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001219 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001220 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1221 PickleState *st = _Pickle_GetGlobalState();
1222 PyErr_SetString(st->UnpicklingError,
1223 "read would overflow (invalid bytecode)");
1224 return -1;
1225 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001226 if (self->next_read_idx + n <= self->input_len) {
1227 *s = self->input_buffer + self->next_read_idx;
1228 self->next_read_idx += n;
1229 return n;
1230 }
1231 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001232 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001233 return -1;
1234 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001235 num_read = _Unpickler_ReadFromFile(self, n);
1236 if (num_read < 0)
1237 return -1;
1238 if (num_read < n) {
1239 PyErr_Format(PyExc_EOFError, "Ran out of input");
1240 return -1;
1241 }
1242 *s = self->input_buffer;
1243 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001244 return n;
1245}
1246
1247static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001248_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1249 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001250{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001251 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001252 if (input_line == NULL) {
1253 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001254 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001255 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001256
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001257 memcpy(input_line, line, len);
1258 input_line[len] = '\0';
1259 self->input_line = input_line;
1260 *result = self->input_line;
1261 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001262}
1263
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001264/* Read a line from the input stream/buffer. If we run off the end of the input
1265 before hitting \n, return the data we found.
1266
1267 Returns the number of chars read, or -1 on failure. */
1268static Py_ssize_t
1269_Unpickler_Readline(UnpicklerObject *self, char **result)
1270{
1271 Py_ssize_t i, num_read;
1272
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001273 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001274 if (self->input_buffer[i] == '\n') {
1275 char *line_start = self->input_buffer + self->next_read_idx;
1276 num_read = i - self->next_read_idx + 1;
1277 self->next_read_idx = i + 1;
1278 return _Unpickler_CopyLine(self, line_start, num_read, result);
1279 }
1280 }
1281 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001282 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1283 if (num_read < 0)
1284 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001285 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001286 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001287 }
Victor Stinner121aab42011-09-29 23:40:53 +02001288
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001289 /* If we get here, we've run off the end of the input string. Return the
1290 remaining string and let the caller figure it out. */
1291 *result = self->input_buffer + self->next_read_idx;
1292 num_read = i - self->next_read_idx;
1293 self->next_read_idx = i;
1294 return num_read;
1295}
1296
1297/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1298 will be modified in place. */
1299static int
1300_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1301{
1302 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001303
1304 assert(new_size > self->memo_size);
1305
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001306 PyMem_RESIZE(self->memo, PyObject *, new_size);
1307 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001308 PyErr_NoMemory();
1309 return -1;
1310 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001311 for (i = self->memo_size; i < new_size; i++)
1312 self->memo[i] = NULL;
1313 self->memo_size = new_size;
1314 return 0;
1315}
1316
1317/* Returns NULL if idx is out of bounds. */
1318static PyObject *
1319_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1320{
1321 if (idx < 0 || idx >= self->memo_size)
1322 return NULL;
1323
1324 return self->memo[idx];
1325}
1326
1327/* Returns -1 (with an exception set) on failure, 0 on success.
1328 This takes its own reference to `value`. */
1329static int
1330_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1331{
1332 PyObject *old_item;
1333
1334 if (idx >= self->memo_size) {
1335 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1336 return -1;
1337 assert(idx < self->memo_size);
1338 }
1339 Py_INCREF(value);
1340 old_item = self->memo[idx];
1341 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001342 if (old_item != NULL) {
1343 Py_DECREF(old_item);
1344 }
1345 else {
1346 self->memo_len++;
1347 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001348 return 0;
1349}
1350
1351static PyObject **
1352_Unpickler_NewMemo(Py_ssize_t new_size)
1353{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001354 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001355 if (memo == NULL) {
1356 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001357 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001358 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001359 memset(memo, 0, new_size * sizeof(PyObject *));
1360 return memo;
1361}
1362
1363/* Free the unpickler's memo, taking care to decref any items left in it. */
1364static void
1365_Unpickler_MemoCleanup(UnpicklerObject *self)
1366{
1367 Py_ssize_t i;
1368 PyObject **memo = self->memo;
1369
1370 if (self->memo == NULL)
1371 return;
1372 self->memo = NULL;
1373 i = self->memo_size;
1374 while (--i >= 0) {
1375 Py_XDECREF(memo[i]);
1376 }
1377 PyMem_FREE(memo);
1378}
1379
1380static UnpicklerObject *
1381_Unpickler_New(void)
1382{
1383 UnpicklerObject *self;
1384
1385 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1386 if (self == NULL)
1387 return NULL;
1388
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001389 self->pers_func = NULL;
1390 self->input_buffer = NULL;
1391 self->input_line = NULL;
1392 self->input_len = 0;
1393 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001394 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001395 self->read = NULL;
1396 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001397 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001398 self->encoding = NULL;
1399 self->errors = NULL;
1400 self->marks = NULL;
1401 self->num_marks = 0;
1402 self->marks_size = 0;
1403 self->proto = 0;
1404 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001405 memset(&self->buffer, 0, sizeof(Py_buffer));
1406 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001407 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001408 self->memo = _Unpickler_NewMemo(self->memo_size);
1409 self->stack = (Pdata *)Pdata_New();
1410
1411 if (self->memo == NULL || self->stack == NULL) {
1412 Py_DECREF(self);
1413 return NULL;
1414 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001415
1416 return self;
1417}
1418
1419/* Returns -1 (with an exception set) on failure, 0 on success. This may
1420 be called once on a freshly created Pickler. */
1421static int
1422_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1423{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001424 _Py_IDENTIFIER(peek);
1425 _Py_IDENTIFIER(read);
1426 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001427
1428 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001429 if (self->peek == NULL) {
1430 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1431 PyErr_Clear();
1432 else
1433 return -1;
1434 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001435 self->read = _PyObject_GetAttrId(file, &PyId_read);
1436 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001437 if (self->readline == NULL || self->read == NULL) {
1438 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1439 PyErr_SetString(PyExc_TypeError,
1440 "file must have 'read' and 'readline' attributes");
1441 Py_CLEAR(self->read);
1442 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001443 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001444 return -1;
1445 }
1446 return 0;
1447}
1448
1449/* Returns -1 (with an exception set) on failure, 0 on success. This may
1450 be called once on a freshly created Pickler. */
1451static int
1452_Unpickler_SetInputEncoding(UnpicklerObject *self,
1453 const char *encoding,
1454 const char *errors)
1455{
1456 if (encoding == NULL)
1457 encoding = "ASCII";
1458 if (errors == NULL)
1459 errors = "strict";
1460
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001461 self->encoding = _PyMem_Strdup(encoding);
1462 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001463 if (self->encoding == NULL || self->errors == NULL) {
1464 PyErr_NoMemory();
1465 return -1;
1466 }
1467 return 0;
1468}
1469
1470/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001471static int
1472memo_get(PicklerObject *self, PyObject *key)
1473{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001474 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001475 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001476 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001477
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001478 value = PyMemoTable_Get(self->memo, key);
1479 if (value == NULL) {
1480 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001481 return -1;
1482 }
1483
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001484 if (!self->bin) {
1485 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001486 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1487 "%" PY_FORMAT_SIZE_T "d\n", *value);
1488 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001489 }
1490 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001491 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001492 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001493 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001494 len = 2;
1495 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001496 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001497 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001498 pdata[1] = (unsigned char)(*value & 0xff);
1499 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1500 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1501 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001502 len = 5;
1503 }
1504 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001505 PickleState *st = _Pickle_GetGlobalState();
1506 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001507 "memo id too large for LONG_BINGET");
1508 return -1;
1509 }
1510 }
1511
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001512 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001513 return -1;
1514
1515 return 0;
1516}
1517
1518/* Store an object in the memo, assign it a new unique ID based on the number
1519 of objects currently stored in the memo and generate a PUT opcode. */
1520static int
1521memo_put(PicklerObject *self, PyObject *obj)
1522{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001523 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001524 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001525 Py_ssize_t idx;
1526
1527 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001528
1529 if (self->fast)
1530 return 0;
1531
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001532 idx = PyMemoTable_Size(self->memo);
1533 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1534 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001535
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001536 if (self->proto >= 4) {
1537 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1538 return -1;
1539 return 0;
1540 }
1541 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001542 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001543 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001544 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001545 len = strlen(pdata);
1546 }
1547 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001548 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001549 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001550 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001551 len = 2;
1552 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001553 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001554 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001555 pdata[1] = (unsigned char)(idx & 0xff);
1556 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1557 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1558 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001559 len = 5;
1560 }
1561 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001562 PickleState *st = _Pickle_GetGlobalState();
1563 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001564 "memo id too large for LONG_BINPUT");
1565 return -1;
1566 }
1567 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001568 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001569 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001570
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001571 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001572}
1573
1574static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001575get_dotted_path(PyObject *obj, PyObject *name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001576 _Py_static_string(PyId_dot, ".");
1577 _Py_static_string(PyId_locals, "<locals>");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001578 PyObject *dotted_path;
1579 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001580
1581 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001582 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001583 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001584 n = PyList_GET_SIZE(dotted_path);
1585 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001586 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001587 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001588 PyObject *result = PyUnicode_RichCompare(
1589 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1590 int is_equal = (result == Py_True);
1591 assert(PyBool_Check(result));
1592 Py_DECREF(result);
1593 if (is_equal) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001594 if (obj == NULL)
1595 PyErr_Format(PyExc_AttributeError,
1596 "Can't pickle local object %R", name);
1597 else
1598 PyErr_Format(PyExc_AttributeError,
1599 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001600 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001601 return NULL;
1602 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001603 }
1604 return dotted_path;
1605}
1606
1607static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001608get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001609{
1610 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001611 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001612
1613 assert(PyList_CheckExact(names));
1614 Py_INCREF(obj);
1615 n = PyList_GET_SIZE(names);
1616 for (i = 0; i < n; i++) {
1617 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001618 Py_XDECREF(parent);
1619 parent = obj;
1620 obj = PyObject_GetAttr(parent, name);
1621 if (obj == NULL) {
1622 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001623 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001624 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001625 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001626 if (pparent != NULL)
1627 *pparent = parent;
1628 else
1629 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001630 return obj;
1631}
1632
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001633static void
1634reformat_attribute_error(PyObject *obj, PyObject *name)
1635{
1636 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1637 PyErr_Clear();
1638 PyErr_Format(PyExc_AttributeError,
1639 "Can't get attribute %R on %R", name, obj);
1640 }
1641}
1642
1643
1644static PyObject *
1645getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1646{
1647 PyObject *dotted_path, *attr;
1648
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001649 if (allow_qualname) {
1650 dotted_path = get_dotted_path(obj, name);
1651 if (dotted_path == NULL)
1652 return NULL;
1653 attr = get_deep_attribute(obj, dotted_path, NULL);
1654 Py_DECREF(dotted_path);
1655 }
1656 else
1657 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001658 if (attr == NULL)
1659 reformat_attribute_error(obj, name);
1660 return attr;
1661}
1662
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001663static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001664whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001665{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001666 PyObject *module_name;
1667 PyObject *modules_dict;
1668 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001669 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001670 _Py_IDENTIFIER(__module__);
1671 _Py_IDENTIFIER(modules);
1672 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001673
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001674 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1675
1676 if (module_name == NULL) {
1677 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001678 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001679 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001680 }
1681 else {
1682 /* In some rare cases (e.g., bound methods of extension types),
1683 __module__ can be None. If it is so, then search sys.modules for
1684 the module of global. */
1685 if (module_name != Py_None)
1686 return module_name;
1687 Py_CLEAR(module_name);
1688 }
1689 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001690
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001691 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001692 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001693 if (modules_dict == NULL) {
1694 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001695 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001696 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001697
1698 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001699 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1700 PyObject *candidate;
1701 if (PyUnicode_Check(module_name) &&
1702 !PyUnicode_CompareWithASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001703 continue;
1704 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001705 continue;
1706
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001707 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001708 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001709 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001710 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001711 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001712 continue;
1713 }
1714
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001715 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001716 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001717 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001718 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001719 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001720 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001721 }
1722
1723 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001724 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001725 Py_INCREF(module_name);
1726 return module_name;
1727}
1728
1729/* fast_save_enter() and fast_save_leave() are guards against recursive
1730 objects when Pickler is used with the "fast mode" (i.e., with object
1731 memoization disabled). If the nesting of a list or dict object exceed
1732 FAST_NESTING_LIMIT, these guards will start keeping an internal
1733 reference to the seen list or dict objects and check whether these objects
1734 are recursive. These are not strictly necessary, since save() has a
1735 hard-coded recursion limit, but they give a nicer error message than the
1736 typical RuntimeError. */
1737static int
1738fast_save_enter(PicklerObject *self, PyObject *obj)
1739{
1740 /* if fast_nesting < 0, we're doing an error exit. */
1741 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1742 PyObject *key = NULL;
1743 if (self->fast_memo == NULL) {
1744 self->fast_memo = PyDict_New();
1745 if (self->fast_memo == NULL) {
1746 self->fast_nesting = -1;
1747 return 0;
1748 }
1749 }
1750 key = PyLong_FromVoidPtr(obj);
1751 if (key == NULL)
1752 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001753 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001754 Py_DECREF(key);
1755 PyErr_Format(PyExc_ValueError,
1756 "fast mode: can't pickle cyclic objects "
1757 "including object type %.200s at %p",
1758 obj->ob_type->tp_name, obj);
1759 self->fast_nesting = -1;
1760 return 0;
1761 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001762 if (PyErr_Occurred()) {
1763 return 0;
1764 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001765 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1766 Py_DECREF(key);
1767 self->fast_nesting = -1;
1768 return 0;
1769 }
1770 Py_DECREF(key);
1771 }
1772 return 1;
1773}
1774
1775static int
1776fast_save_leave(PicklerObject *self, PyObject *obj)
1777{
1778 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1779 PyObject *key = PyLong_FromVoidPtr(obj);
1780 if (key == NULL)
1781 return 0;
1782 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1783 Py_DECREF(key);
1784 return 0;
1785 }
1786 Py_DECREF(key);
1787 }
1788 return 1;
1789}
1790
1791static int
1792save_none(PicklerObject *self, PyObject *obj)
1793{
1794 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001795 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001796 return -1;
1797
1798 return 0;
1799}
1800
1801static int
1802save_bool(PicklerObject *self, PyObject *obj)
1803{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001804 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001805 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001806 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001807 return -1;
1808 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001809 else {
1810 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1811 * so that unpicklers written before bools were introduced unpickle them
1812 * as ints, but unpicklers after can recognize that bools were intended.
1813 * Note that protocol 2 added direct ways to pickle bools.
1814 */
1815 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1816 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1817 return -1;
1818 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001819 return 0;
1820}
1821
1822static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001823save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001824{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001825 PyObject *repr = NULL;
1826 Py_ssize_t size;
1827 long val;
1828 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001829
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001830 const char long_op = LONG;
1831
1832 val= PyLong_AsLong(obj);
1833 if (val == -1 && PyErr_Occurred()) {
1834 /* out of range for int pickling */
1835 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001836 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001837 else if (self->bin &&
1838 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001839 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001840 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001841
1842 Note: we can't use -0x80000000L in the above condition because some
1843 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1844 before applying the unary minus when sizeof(long) <= 4. The
1845 resulting value stays unsigned which is commonly not what we want,
1846 so MSVC happily warns us about it. However, that result would have
1847 been fine because we guard for sizeof(long) <= 4 which turns the
1848 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001849 char pdata[32];
1850 Py_ssize_t len = 0;
1851
1852 pdata[1] = (unsigned char)(val & 0xff);
1853 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1854 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1855 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001856
1857 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1858 if (pdata[2] == 0) {
1859 pdata[0] = BININT1;
1860 len = 2;
1861 }
1862 else {
1863 pdata[0] = BININT2;
1864 len = 3;
1865 }
1866 }
1867 else {
1868 pdata[0] = BININT;
1869 len = 5;
1870 }
1871
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001872 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001873 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001874
1875 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001876 }
1877
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001878 if (self->proto >= 2) {
1879 /* Linear-time pickling. */
1880 size_t nbits;
1881 size_t nbytes;
1882 unsigned char *pdata;
1883 char header[5];
1884 int i;
1885 int sign = _PyLong_Sign(obj);
1886
1887 if (sign == 0) {
1888 header[0] = LONG1;
1889 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001890 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001891 goto error;
1892 return 0;
1893 }
1894 nbits = _PyLong_NumBits(obj);
1895 if (nbits == (size_t)-1 && PyErr_Occurred())
1896 goto error;
1897 /* How many bytes do we need? There are nbits >> 3 full
1898 * bytes of data, and nbits & 7 leftover bits. If there
1899 * are any leftover bits, then we clearly need another
1900 * byte. Wnat's not so obvious is that we *probably*
1901 * need another byte even if there aren't any leftovers:
1902 * the most-significant bit of the most-significant byte
1903 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001904 * opposite of the one we need. The exception is ints
1905 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001906 * its own 256's-complement, so has the right sign bit
1907 * even without the extra byte. That's a pain to check
1908 * for in advance, though, so we always grab an extra
1909 * byte at the start, and cut it back later if possible.
1910 */
1911 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001912 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001913 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001914 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001915 goto error;
1916 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001917 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001918 if (repr == NULL)
1919 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001920 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001921 i = _PyLong_AsByteArray((PyLongObject *)obj,
1922 pdata, nbytes,
1923 1 /* little endian */ , 1 /* signed */ );
1924 if (i < 0)
1925 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001926 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001927 * needed. This is so iff the MSB is all redundant sign
1928 * bits.
1929 */
1930 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001931 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001932 pdata[nbytes - 1] == 0xff &&
1933 (pdata[nbytes - 2] & 0x80) != 0) {
1934 nbytes--;
1935 }
1936
1937 if (nbytes < 256) {
1938 header[0] = LONG1;
1939 header[1] = (unsigned char)nbytes;
1940 size = 2;
1941 }
1942 else {
1943 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001944 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001945 for (i = 1; i < 5; i++) {
1946 header[i] = (unsigned char)(size & 0xff);
1947 size >>= 8;
1948 }
1949 size = 5;
1950 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001951 if (_Pickler_Write(self, header, size) < 0 ||
1952 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001953 goto error;
1954 }
1955 else {
1956 char *string;
1957
Mark Dickinson8dd05142009-01-20 20:43:58 +00001958 /* proto < 2: write the repr and newline. This is quadratic-time (in
1959 the number of digits), in both directions. We add a trailing 'L'
1960 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001961
1962 repr = PyObject_Repr(obj);
1963 if (repr == NULL)
1964 goto error;
1965
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001966 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001967 if (string == NULL)
1968 goto error;
1969
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001970 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1971 _Pickler_Write(self, string, size) < 0 ||
1972 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001973 goto error;
1974 }
1975
1976 if (0) {
1977 error:
1978 status = -1;
1979 }
1980 Py_XDECREF(repr);
1981
1982 return status;
1983}
1984
1985static int
1986save_float(PicklerObject *self, PyObject *obj)
1987{
1988 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1989
1990 if (self->bin) {
1991 char pdata[9];
1992 pdata[0] = BINFLOAT;
1993 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1994 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001995 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001996 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001997 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001998 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001999 int result = -1;
2000 char *buf = NULL;
2001 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002002
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002003 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002004 goto done;
2005
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002006 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002007 if (!buf) {
2008 PyErr_NoMemory();
2009 goto done;
2010 }
2011
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002012 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002013 goto done;
2014
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002015 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002016 goto done;
2017
2018 result = 0;
2019done:
2020 PyMem_Free(buf);
2021 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002022 }
2023
2024 return 0;
2025}
2026
2027static int
2028save_bytes(PicklerObject *self, PyObject *obj)
2029{
2030 if (self->proto < 3) {
2031 /* Older pickle protocols do not have an opcode for pickling bytes
2032 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002033 the __reduce__ method) to permit bytes object unpickling.
2034
2035 Here we use a hack to be compatible with Python 2. Since in Python
2036 2 'bytes' is just an alias for 'str' (which has different
2037 parameters than the actual bytes object), we use codecs.encode
2038 to create the appropriate 'str' object when unpickled using
2039 Python 2 *and* the appropriate 'bytes' object when unpickled
2040 using Python 3. Again this is a hack and we don't need to do this
2041 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002042 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002043 int status;
2044
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002045 if (PyBytes_GET_SIZE(obj) == 0) {
2046 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2047 }
2048 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002049 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002050 PyObject *unicode_str =
2051 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2052 PyBytes_GET_SIZE(obj),
2053 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002054 _Py_IDENTIFIER(latin1);
2055
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002056 if (unicode_str == NULL)
2057 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002058 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002059 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002060 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002061 Py_DECREF(unicode_str);
2062 }
2063
2064 if (reduce_value == NULL)
2065 return -1;
2066
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002067 /* save_reduce() will memoize the object automatically. */
2068 status = save_reduce(self, reduce_value, obj);
2069 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002070 return status;
2071 }
2072 else {
2073 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002074 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002075 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002076
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002077 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002078 if (size < 0)
2079 return -1;
2080
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002081 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002082 header[0] = SHORT_BINBYTES;
2083 header[1] = (unsigned char)size;
2084 len = 2;
2085 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002086 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002087 header[0] = BINBYTES;
2088 header[1] = (unsigned char)(size & 0xff);
2089 header[2] = (unsigned char)((size >> 8) & 0xff);
2090 header[3] = (unsigned char)((size >> 16) & 0xff);
2091 header[4] = (unsigned char)((size >> 24) & 0xff);
2092 len = 5;
2093 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002094 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002095 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002096 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002097 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002098 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002099 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002100 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002101 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002102 return -1; /* string too large */
2103 }
2104
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002105 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002106 return -1;
2107
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002108 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002109 return -1;
2110
2111 if (memo_put(self, obj) < 0)
2112 return -1;
2113
2114 return 0;
2115 }
2116}
2117
2118/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2119 backslash and newline characters to \uXXXX escapes. */
2120static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002121raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002122{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002123 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002124 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002125 void *data;
2126 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002127 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002128
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002129 if (PyUnicode_READY(obj))
2130 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002131
Victor Stinner358af132015-10-12 22:36:57 +02002132 _PyBytesWriter_Init(&writer);
2133
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002134 size = PyUnicode_GET_LENGTH(obj);
2135 data = PyUnicode_DATA(obj);
2136 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002137
Victor Stinner358af132015-10-12 22:36:57 +02002138 p = _PyBytesWriter_Alloc(&writer, size);
2139 if (p == NULL)
2140 goto error;
2141 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002142
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002143 for (i=0; i < size; i++) {
2144 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002145 /* Map 32-bit characters to '\Uxxxxxxxx' */
2146 if (ch >= 0x10000) {
Victor Stinner358af132015-10-12 22:36:57 +02002147 /* -1: substract 1 preallocated byte */
2148 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2149 if (p == NULL)
2150 goto error;
2151
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002152 *p++ = '\\';
2153 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002154 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2155 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2156 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2157 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2158 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2159 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2160 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2161 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002162 }
Victor Stinner358af132015-10-12 22:36:57 +02002163 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002164 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Victor Stinner358af132015-10-12 22:36:57 +02002165 /* -1: substract 1 preallocated byte */
2166 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2167 if (p == NULL)
2168 goto error;
2169
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002170 *p++ = '\\';
2171 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002172 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2173 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2174 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2175 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002176 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002177 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002178 else
2179 *p++ = (char) ch;
2180 }
Victor Stinner358af132015-10-12 22:36:57 +02002181
2182 return _PyBytesWriter_Finish(&writer, p);
2183
2184error:
2185 _PyBytesWriter_Dealloc(&writer);
2186 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002187}
2188
2189static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002190write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2191{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002192 char header[9];
2193 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002194
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002195 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002196 if (size <= 0xff && self->proto >= 4) {
2197 header[0] = SHORT_BINUNICODE;
2198 header[1] = (unsigned char)(size & 0xff);
2199 len = 2;
2200 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002201 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002202 header[0] = BINUNICODE;
2203 header[1] = (unsigned char)(size & 0xff);
2204 header[2] = (unsigned char)((size >> 8) & 0xff);
2205 header[3] = (unsigned char)((size >> 16) & 0xff);
2206 header[4] = (unsigned char)((size >> 24) & 0xff);
2207 len = 5;
2208 }
2209 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002210 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002211 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002212 len = 9;
2213 }
2214 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002215 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002216 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002217 return -1;
2218 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002219
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002220 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002221 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002222 if (_Pickler_Write(self, data, size) < 0)
2223 return -1;
2224
2225 return 0;
2226}
2227
2228static int
2229write_unicode_binary(PicklerObject *self, PyObject *obj)
2230{
2231 PyObject *encoded = NULL;
2232 Py_ssize_t size;
2233 char *data;
2234 int r;
2235
2236 if (PyUnicode_READY(obj))
2237 return -1;
2238
2239 data = PyUnicode_AsUTF8AndSize(obj, &size);
2240 if (data != NULL)
2241 return write_utf8(self, data, size);
2242
2243 /* Issue #8383: for strings with lone surrogates, fallback on the
2244 "surrogatepass" error handler. */
2245 PyErr_Clear();
2246 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2247 if (encoded == NULL)
2248 return -1;
2249
2250 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2251 PyBytes_GET_SIZE(encoded));
2252 Py_DECREF(encoded);
2253 return r;
2254}
2255
2256static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002257save_unicode(PicklerObject *self, PyObject *obj)
2258{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002259 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002260 if (write_unicode_binary(self, obj) < 0)
2261 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002262 }
2263 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002264 PyObject *encoded;
2265 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002266 const char unicode_op = UNICODE;
2267
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002268 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002269 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002270 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002271
Antoine Pitrou299978d2013-04-07 17:38:11 +02002272 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2273 Py_DECREF(encoded);
2274 return -1;
2275 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002276
2277 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002278 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2279 Py_DECREF(encoded);
2280 return -1;
2281 }
2282 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002283
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002284 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002285 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002286 }
2287 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002288 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002289
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002290 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002291}
2292
2293/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2294static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002295store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002296{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002297 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002298
2299 assert(PyTuple_Size(t) == len);
2300
2301 for (i = 0; i < len; i++) {
2302 PyObject *element = PyTuple_GET_ITEM(t, i);
2303
2304 if (element == NULL)
2305 return -1;
2306 if (save(self, element, 0) < 0)
2307 return -1;
2308 }
2309
2310 return 0;
2311}
2312
2313/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2314 * used across protocols to minimize the space needed to pickle them.
2315 * Tuples are also the only builtin immutable type that can be recursive
2316 * (a tuple can be reached from itself), and that requires some subtle
2317 * magic so that it works in all cases. IOW, this is a long routine.
2318 */
2319static int
2320save_tuple(PicklerObject *self, PyObject *obj)
2321{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002322 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002323
2324 const char mark_op = MARK;
2325 const char tuple_op = TUPLE;
2326 const char pop_op = POP;
2327 const char pop_mark_op = POP_MARK;
2328 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2329
2330 if ((len = PyTuple_Size(obj)) < 0)
2331 return -1;
2332
2333 if (len == 0) {
2334 char pdata[2];
2335
2336 if (self->proto) {
2337 pdata[0] = EMPTY_TUPLE;
2338 len = 1;
2339 }
2340 else {
2341 pdata[0] = MARK;
2342 pdata[1] = TUPLE;
2343 len = 2;
2344 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002345 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002346 return -1;
2347 return 0;
2348 }
2349
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002350 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002351 * saving the tuple elements, the tuple must be recursive, in
2352 * which case we'll pop everything we put on the stack, and fetch
2353 * its value from the memo.
2354 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002355 if (len <= 3 && self->proto >= 2) {
2356 /* Use TUPLE{1,2,3} opcodes. */
2357 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002358 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002359
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002360 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002361 /* pop the len elements */
2362 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002363 if (_Pickler_Write(self, &pop_op, 1) < 0)
2364 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002365 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002366 if (memo_get(self, obj) < 0)
2367 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002368
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002369 return 0;
2370 }
2371 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002372 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2373 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002374 }
2375 goto memoize;
2376 }
2377
2378 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2379 * Generate MARK e1 e2 ... TUPLE
2380 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002381 if (_Pickler_Write(self, &mark_op, 1) < 0)
2382 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002383
2384 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002385 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002386
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002387 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002388 /* pop the stack stuff we pushed */
2389 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002390 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2391 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002392 }
2393 else {
2394 /* Note that we pop one more than len, to remove
2395 * the MARK too.
2396 */
2397 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002398 if (_Pickler_Write(self, &pop_op, 1) < 0)
2399 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002400 }
2401 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002402 if (memo_get(self, obj) < 0)
2403 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002404
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002405 return 0;
2406 }
2407 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002408 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2409 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002410 }
2411
2412 memoize:
2413 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002414 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002415
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002416 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002417}
2418
2419/* iter is an iterator giving items, and we batch up chunks of
2420 * MARK item item ... item APPENDS
2421 * opcode sequences. Calling code should have arranged to first create an
2422 * empty list, or list-like object, for the APPENDS to operate on.
2423 * Returns 0 on success, <0 on error.
2424 */
2425static int
2426batch_list(PicklerObject *self, PyObject *iter)
2427{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002428 PyObject *obj = NULL;
2429 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002430 int i, n;
2431
2432 const char mark_op = MARK;
2433 const char append_op = APPEND;
2434 const char appends_op = APPENDS;
2435
2436 assert(iter != NULL);
2437
2438 /* XXX: I think this function could be made faster by avoiding the
2439 iterator interface and fetching objects directly from list using
2440 PyList_GET_ITEM.
2441 */
2442
2443 if (self->proto == 0) {
2444 /* APPENDS isn't available; do one at a time. */
2445 for (;;) {
2446 obj = PyIter_Next(iter);
2447 if (obj == NULL) {
2448 if (PyErr_Occurred())
2449 return -1;
2450 break;
2451 }
2452 i = save(self, obj, 0);
2453 Py_DECREF(obj);
2454 if (i < 0)
2455 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002456 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002457 return -1;
2458 }
2459 return 0;
2460 }
2461
2462 /* proto > 0: write in batches of BATCHSIZE. */
2463 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002464 /* Get first item */
2465 firstitem = PyIter_Next(iter);
2466 if (firstitem == NULL) {
2467 if (PyErr_Occurred())
2468 goto error;
2469
2470 /* nothing more to add */
2471 break;
2472 }
2473
2474 /* Try to get a second item */
2475 obj = PyIter_Next(iter);
2476 if (obj == NULL) {
2477 if (PyErr_Occurred())
2478 goto error;
2479
2480 /* Only one item to write */
2481 if (save(self, firstitem, 0) < 0)
2482 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002483 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002484 goto error;
2485 Py_CLEAR(firstitem);
2486 break;
2487 }
2488
2489 /* More than one item to write */
2490
2491 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002492 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002493 goto error;
2494
2495 if (save(self, firstitem, 0) < 0)
2496 goto error;
2497 Py_CLEAR(firstitem);
2498 n = 1;
2499
2500 /* Fetch and save up to BATCHSIZE items */
2501 while (obj) {
2502 if (save(self, obj, 0) < 0)
2503 goto error;
2504 Py_CLEAR(obj);
2505 n += 1;
2506
2507 if (n == BATCHSIZE)
2508 break;
2509
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002510 obj = PyIter_Next(iter);
2511 if (obj == NULL) {
2512 if (PyErr_Occurred())
2513 goto error;
2514 break;
2515 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002516 }
2517
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002518 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002519 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002520
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002521 } while (n == BATCHSIZE);
2522 return 0;
2523
2524 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002525 Py_XDECREF(firstitem);
2526 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002527 return -1;
2528}
2529
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002530/* This is a variant of batch_list() above, specialized for lists (with no
2531 * support for list subclasses). Like batch_list(), we batch up chunks of
2532 * MARK item item ... item APPENDS
2533 * opcode sequences. Calling code should have arranged to first create an
2534 * empty list, or list-like object, for the APPENDS to operate on.
2535 * Returns 0 on success, -1 on error.
2536 *
2537 * This version is considerably faster than batch_list(), if less general.
2538 *
2539 * Note that this only works for protocols > 0.
2540 */
2541static int
2542batch_list_exact(PicklerObject *self, PyObject *obj)
2543{
2544 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002545 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002546
2547 const char append_op = APPEND;
2548 const char appends_op = APPENDS;
2549 const char mark_op = MARK;
2550
2551 assert(obj != NULL);
2552 assert(self->proto > 0);
2553 assert(PyList_CheckExact(obj));
2554
2555 if (PyList_GET_SIZE(obj) == 1) {
2556 item = PyList_GET_ITEM(obj, 0);
2557 if (save(self, item, 0) < 0)
2558 return -1;
2559 if (_Pickler_Write(self, &append_op, 1) < 0)
2560 return -1;
2561 return 0;
2562 }
2563
2564 /* Write in batches of BATCHSIZE. */
2565 total = 0;
2566 do {
2567 this_batch = 0;
2568 if (_Pickler_Write(self, &mark_op, 1) < 0)
2569 return -1;
2570 while (total < PyList_GET_SIZE(obj)) {
2571 item = PyList_GET_ITEM(obj, total);
2572 if (save(self, item, 0) < 0)
2573 return -1;
2574 total++;
2575 if (++this_batch == BATCHSIZE)
2576 break;
2577 }
2578 if (_Pickler_Write(self, &appends_op, 1) < 0)
2579 return -1;
2580
2581 } while (total < PyList_GET_SIZE(obj));
2582
2583 return 0;
2584}
2585
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002586static int
2587save_list(PicklerObject *self, PyObject *obj)
2588{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002589 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002590 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002591 int status = 0;
2592
2593 if (self->fast && !fast_save_enter(self, obj))
2594 goto error;
2595
2596 /* Create an empty list. */
2597 if (self->bin) {
2598 header[0] = EMPTY_LIST;
2599 len = 1;
2600 }
2601 else {
2602 header[0] = MARK;
2603 header[1] = LIST;
2604 len = 2;
2605 }
2606
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002607 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002608 goto error;
2609
2610 /* Get list length, and bow out early if empty. */
2611 if ((len = PyList_Size(obj)) < 0)
2612 goto error;
2613
2614 if (memo_put(self, obj) < 0)
2615 goto error;
2616
2617 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002618 /* Materialize the list elements. */
2619 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002620 if (Py_EnterRecursiveCall(" while pickling an object"))
2621 goto error;
2622 status = batch_list_exact(self, obj);
2623 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002624 } else {
2625 PyObject *iter = PyObject_GetIter(obj);
2626 if (iter == NULL)
2627 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002628
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002629 if (Py_EnterRecursiveCall(" while pickling an object")) {
2630 Py_DECREF(iter);
2631 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002632 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002633 status = batch_list(self, iter);
2634 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002635 Py_DECREF(iter);
2636 }
2637 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002638 if (0) {
2639 error:
2640 status = -1;
2641 }
2642
2643 if (self->fast && !fast_save_leave(self, obj))
2644 status = -1;
2645
2646 return status;
2647}
2648
2649/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2650 * MARK key value ... key value SETITEMS
2651 * opcode sequences. Calling code should have arranged to first create an
2652 * empty dict, or dict-like object, for the SETITEMS to operate on.
2653 * Returns 0 on success, <0 on error.
2654 *
2655 * This is very much like batch_list(). The difference between saving
2656 * elements directly, and picking apart two-tuples, is so long-winded at
2657 * the C level, though, that attempts to combine these routines were too
2658 * ugly to bear.
2659 */
2660static int
2661batch_dict(PicklerObject *self, PyObject *iter)
2662{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002663 PyObject *obj = NULL;
2664 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002665 int i, n;
2666
2667 const char mark_op = MARK;
2668 const char setitem_op = SETITEM;
2669 const char setitems_op = SETITEMS;
2670
2671 assert(iter != NULL);
2672
2673 if (self->proto == 0) {
2674 /* SETITEMS isn't available; do one at a time. */
2675 for (;;) {
2676 obj = PyIter_Next(iter);
2677 if (obj == NULL) {
2678 if (PyErr_Occurred())
2679 return -1;
2680 break;
2681 }
2682 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2683 PyErr_SetString(PyExc_TypeError, "dict items "
2684 "iterator must return 2-tuples");
2685 return -1;
2686 }
2687 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2688 if (i >= 0)
2689 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2690 Py_DECREF(obj);
2691 if (i < 0)
2692 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002693 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002694 return -1;
2695 }
2696 return 0;
2697 }
2698
2699 /* proto > 0: write in batches of BATCHSIZE. */
2700 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002701 /* Get first item */
2702 firstitem = PyIter_Next(iter);
2703 if (firstitem == NULL) {
2704 if (PyErr_Occurred())
2705 goto error;
2706
2707 /* nothing more to add */
2708 break;
2709 }
2710 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2711 PyErr_SetString(PyExc_TypeError, "dict items "
2712 "iterator must return 2-tuples");
2713 goto error;
2714 }
2715
2716 /* Try to get a second item */
2717 obj = PyIter_Next(iter);
2718 if (obj == NULL) {
2719 if (PyErr_Occurred())
2720 goto error;
2721
2722 /* Only one item to write */
2723 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2724 goto error;
2725 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2726 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002727 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002728 goto error;
2729 Py_CLEAR(firstitem);
2730 break;
2731 }
2732
2733 /* More than one item to write */
2734
2735 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002736 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002737 goto error;
2738
2739 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2740 goto error;
2741 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2742 goto error;
2743 Py_CLEAR(firstitem);
2744 n = 1;
2745
2746 /* Fetch and save up to BATCHSIZE items */
2747 while (obj) {
2748 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2749 PyErr_SetString(PyExc_TypeError, "dict items "
2750 "iterator must return 2-tuples");
2751 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002752 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002753 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2754 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2755 goto error;
2756 Py_CLEAR(obj);
2757 n += 1;
2758
2759 if (n == BATCHSIZE)
2760 break;
2761
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002762 obj = PyIter_Next(iter);
2763 if (obj == NULL) {
2764 if (PyErr_Occurred())
2765 goto error;
2766 break;
2767 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002768 }
2769
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002770 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002771 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002772
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002773 } while (n == BATCHSIZE);
2774 return 0;
2775
2776 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002777 Py_XDECREF(firstitem);
2778 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002779 return -1;
2780}
2781
Collin Winter5c9b02d2009-05-25 05:43:30 +00002782/* This is a variant of batch_dict() above that specializes for dicts, with no
2783 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2784 * MARK key value ... key value SETITEMS
2785 * opcode sequences. Calling code should have arranged to first create an
2786 * empty dict, or dict-like object, for the SETITEMS to operate on.
2787 * Returns 0 on success, -1 on error.
2788 *
2789 * Note that this currently doesn't work for protocol 0.
2790 */
2791static int
2792batch_dict_exact(PicklerObject *self, PyObject *obj)
2793{
2794 PyObject *key = NULL, *value = NULL;
2795 int i;
2796 Py_ssize_t dict_size, ppos = 0;
2797
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002798 const char mark_op = MARK;
2799 const char setitem_op = SETITEM;
2800 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002801
2802 assert(obj != NULL);
2803 assert(self->proto > 0);
2804
2805 dict_size = PyDict_Size(obj);
2806
2807 /* Special-case len(d) == 1 to save space. */
2808 if (dict_size == 1) {
2809 PyDict_Next(obj, &ppos, &key, &value);
2810 if (save(self, key, 0) < 0)
2811 return -1;
2812 if (save(self, value, 0) < 0)
2813 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002814 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002815 return -1;
2816 return 0;
2817 }
2818
2819 /* Write in batches of BATCHSIZE. */
2820 do {
2821 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002822 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002823 return -1;
2824 while (PyDict_Next(obj, &ppos, &key, &value)) {
2825 if (save(self, key, 0) < 0)
2826 return -1;
2827 if (save(self, value, 0) < 0)
2828 return -1;
2829 if (++i == BATCHSIZE)
2830 break;
2831 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002832 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002833 return -1;
2834 if (PyDict_Size(obj) != dict_size) {
2835 PyErr_Format(
2836 PyExc_RuntimeError,
2837 "dictionary changed size during iteration");
2838 return -1;
2839 }
2840
2841 } while (i == BATCHSIZE);
2842 return 0;
2843}
2844
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002845static int
2846save_dict(PicklerObject *self, PyObject *obj)
2847{
2848 PyObject *items, *iter;
2849 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002850 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002851 int status = 0;
2852
2853 if (self->fast && !fast_save_enter(self, obj))
2854 goto error;
2855
2856 /* Create an empty dict. */
2857 if (self->bin) {
2858 header[0] = EMPTY_DICT;
2859 len = 1;
2860 }
2861 else {
2862 header[0] = MARK;
2863 header[1] = DICT;
2864 len = 2;
2865 }
2866
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002867 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002868 goto error;
2869
2870 /* Get dict size, and bow out early if empty. */
2871 if ((len = PyDict_Size(obj)) < 0)
2872 goto error;
2873
2874 if (memo_put(self, obj) < 0)
2875 goto error;
2876
2877 if (len != 0) {
2878 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002879 if (PyDict_CheckExact(obj) && self->proto > 0) {
2880 /* We can take certain shortcuts if we know this is a dict and
2881 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002882 if (Py_EnterRecursiveCall(" while pickling an object"))
2883 goto error;
2884 status = batch_dict_exact(self, obj);
2885 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002886 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002887 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002888
2889 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002890 if (items == NULL)
2891 goto error;
2892 iter = PyObject_GetIter(items);
2893 Py_DECREF(items);
2894 if (iter == NULL)
2895 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002896 if (Py_EnterRecursiveCall(" while pickling an object")) {
2897 Py_DECREF(iter);
2898 goto error;
2899 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002900 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002901 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002902 Py_DECREF(iter);
2903 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002904 }
2905
2906 if (0) {
2907 error:
2908 status = -1;
2909 }
2910
2911 if (self->fast && !fast_save_leave(self, obj))
2912 status = -1;
2913
2914 return status;
2915}
2916
2917static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002918save_set(PicklerObject *self, PyObject *obj)
2919{
2920 PyObject *item;
2921 int i;
2922 Py_ssize_t set_size, ppos = 0;
2923 Py_hash_t hash;
2924
2925 const char empty_set_op = EMPTY_SET;
2926 const char mark_op = MARK;
2927 const char additems_op = ADDITEMS;
2928
2929 if (self->proto < 4) {
2930 PyObject *items;
2931 PyObject *reduce_value;
2932 int status;
2933
2934 items = PySequence_List(obj);
2935 if (items == NULL) {
2936 return -1;
2937 }
2938 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2939 Py_DECREF(items);
2940 if (reduce_value == NULL) {
2941 return -1;
2942 }
2943 /* save_reduce() will memoize the object automatically. */
2944 status = save_reduce(self, reduce_value, obj);
2945 Py_DECREF(reduce_value);
2946 return status;
2947 }
2948
2949 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2950 return -1;
2951
2952 if (memo_put(self, obj) < 0)
2953 return -1;
2954
2955 set_size = PySet_GET_SIZE(obj);
2956 if (set_size == 0)
2957 return 0; /* nothing to do */
2958
2959 /* Write in batches of BATCHSIZE. */
2960 do {
2961 i = 0;
2962 if (_Pickler_Write(self, &mark_op, 1) < 0)
2963 return -1;
2964 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2965 if (save(self, item, 0) < 0)
2966 return -1;
2967 if (++i == BATCHSIZE)
2968 break;
2969 }
2970 if (_Pickler_Write(self, &additems_op, 1) < 0)
2971 return -1;
2972 if (PySet_GET_SIZE(obj) != set_size) {
2973 PyErr_Format(
2974 PyExc_RuntimeError,
2975 "set changed size during iteration");
2976 return -1;
2977 }
2978 } while (i == BATCHSIZE);
2979
2980 return 0;
2981}
2982
2983static int
2984save_frozenset(PicklerObject *self, PyObject *obj)
2985{
2986 PyObject *iter;
2987
2988 const char mark_op = MARK;
2989 const char frozenset_op = FROZENSET;
2990
2991 if (self->fast && !fast_save_enter(self, obj))
2992 return -1;
2993
2994 if (self->proto < 4) {
2995 PyObject *items;
2996 PyObject *reduce_value;
2997 int status;
2998
2999 items = PySequence_List(obj);
3000 if (items == NULL) {
3001 return -1;
3002 }
3003 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3004 items);
3005 Py_DECREF(items);
3006 if (reduce_value == NULL) {
3007 return -1;
3008 }
3009 /* save_reduce() will memoize the object automatically. */
3010 status = save_reduce(self, reduce_value, obj);
3011 Py_DECREF(reduce_value);
3012 return status;
3013 }
3014
3015 if (_Pickler_Write(self, &mark_op, 1) < 0)
3016 return -1;
3017
3018 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003019 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003020 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003021 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003022 for (;;) {
3023 PyObject *item;
3024
3025 item = PyIter_Next(iter);
3026 if (item == NULL) {
3027 if (PyErr_Occurred()) {
3028 Py_DECREF(iter);
3029 return -1;
3030 }
3031 break;
3032 }
3033 if (save(self, item, 0) < 0) {
3034 Py_DECREF(item);
3035 Py_DECREF(iter);
3036 return -1;
3037 }
3038 Py_DECREF(item);
3039 }
3040 Py_DECREF(iter);
3041
3042 /* If the object is already in the memo, this means it is
3043 recursive. In this case, throw away everything we put on the
3044 stack, and fetch the object back from the memo. */
3045 if (PyMemoTable_Get(self->memo, obj)) {
3046 const char pop_mark_op = POP_MARK;
3047
3048 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3049 return -1;
3050 if (memo_get(self, obj) < 0)
3051 return -1;
3052 return 0;
3053 }
3054
3055 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3056 return -1;
3057 if (memo_put(self, obj) < 0)
3058 return -1;
3059
3060 return 0;
3061}
3062
3063static int
3064fix_imports(PyObject **module_name, PyObject **global_name)
3065{
3066 PyObject *key;
3067 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003068 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003069
3070 key = PyTuple_Pack(2, *module_name, *global_name);
3071 if (key == NULL)
3072 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003073 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003074 Py_DECREF(key);
3075 if (item) {
3076 PyObject *fixed_module_name;
3077 PyObject *fixed_global_name;
3078
3079 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3080 PyErr_Format(PyExc_RuntimeError,
3081 "_compat_pickle.REVERSE_NAME_MAPPING values "
3082 "should be 2-tuples, not %.200s",
3083 Py_TYPE(item)->tp_name);
3084 return -1;
3085 }
3086 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3087 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3088 if (!PyUnicode_Check(fixed_module_name) ||
3089 !PyUnicode_Check(fixed_global_name)) {
3090 PyErr_Format(PyExc_RuntimeError,
3091 "_compat_pickle.REVERSE_NAME_MAPPING values "
3092 "should be pairs of str, not (%.200s, %.200s)",
3093 Py_TYPE(fixed_module_name)->tp_name,
3094 Py_TYPE(fixed_global_name)->tp_name);
3095 return -1;
3096 }
3097
3098 Py_CLEAR(*module_name);
3099 Py_CLEAR(*global_name);
3100 Py_INCREF(fixed_module_name);
3101 Py_INCREF(fixed_global_name);
3102 *module_name = fixed_module_name;
3103 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003104 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003105 }
3106 else if (PyErr_Occurred()) {
3107 return -1;
3108 }
3109
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003110 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003111 if (item) {
3112 if (!PyUnicode_Check(item)) {
3113 PyErr_Format(PyExc_RuntimeError,
3114 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3115 "should be strings, not %.200s",
3116 Py_TYPE(item)->tp_name);
3117 return -1;
3118 }
3119 Py_CLEAR(*module_name);
3120 Py_INCREF(item);
3121 *module_name = item;
3122 }
3123 else if (PyErr_Occurred()) {
3124 return -1;
3125 }
3126
3127 return 0;
3128}
3129
3130static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003131save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3132{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003133 PyObject *global_name = NULL;
3134 PyObject *module_name = NULL;
3135 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003136 PyObject *parent = NULL;
3137 PyObject *dotted_path = NULL;
3138 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003139 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003140 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003141 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003142 _Py_IDENTIFIER(__name__);
3143 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003144
3145 const char global_op = GLOBAL;
3146
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003147 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003148 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003149 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003150 }
3151 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003152 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3153 if (global_name == NULL) {
3154 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3155 goto error;
3156 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003157 }
3158 if (global_name == NULL) {
3159 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3160 if (global_name == NULL)
3161 goto error;
3162 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003163 }
3164
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003165 dotted_path = get_dotted_path(module, global_name);
3166 if (dotted_path == NULL)
3167 goto error;
3168 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003169 if (module_name == NULL)
3170 goto error;
3171
3172 /* XXX: Change to use the import C API directly with level=0 to disallow
3173 relative imports.
3174
3175 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3176 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3177 custom import functions (IMHO, this would be a nice security
3178 feature). The import C API would need to be extended to support the
3179 extra parameters of __import__ to fix that. */
3180 module = PyImport_Import(module_name);
3181 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003182 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003183 "Can't pickle %R: import of module %R failed",
3184 obj, module_name);
3185 goto error;
3186 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003187 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3188 Py_INCREF(lastname);
3189 cls = get_deep_attribute(module, dotted_path, &parent);
3190 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003191 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003192 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003193 "Can't pickle %R: attribute lookup %S on %S failed",
3194 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003195 goto error;
3196 }
3197 if (cls != obj) {
3198 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003199 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003200 "Can't pickle %R: it's not the same object as %S.%S",
3201 obj, module_name, global_name);
3202 goto error;
3203 }
3204 Py_DECREF(cls);
3205
3206 if (self->proto >= 2) {
3207 /* See whether this is in the extension registry, and if
3208 * so generate an EXT opcode.
3209 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003210 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003211 PyObject *code_obj; /* extension code as Python object */
3212 long code; /* extension code as C value */
3213 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003214 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003215
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003216 extension_key = PyTuple_Pack(2, module_name, global_name);
3217 if (extension_key == NULL) {
3218 goto error;
3219 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003220 code_obj = PyDict_GetItemWithError(st->extension_registry,
3221 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003222 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003223 /* The object is not registered in the extension registry.
3224 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003225 if (code_obj == NULL) {
3226 if (PyErr_Occurred()) {
3227 goto error;
3228 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003229 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003230 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003231
3232 /* XXX: pickle.py doesn't check neither the type, nor the range
3233 of the value returned by the extension_registry. It should for
3234 consistency. */
3235
3236 /* Verify code_obj has the right type and value. */
3237 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003238 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003239 "Can't pickle %R: extension code %R isn't an integer",
3240 obj, code_obj);
3241 goto error;
3242 }
3243 code = PyLong_AS_LONG(code_obj);
3244 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003245 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003246 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3247 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003248 goto error;
3249 }
3250
3251 /* Generate an EXT opcode. */
3252 if (code <= 0xff) {
3253 pdata[0] = EXT1;
3254 pdata[1] = (unsigned char)code;
3255 n = 2;
3256 }
3257 else if (code <= 0xffff) {
3258 pdata[0] = EXT2;
3259 pdata[1] = (unsigned char)(code & 0xff);
3260 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3261 n = 3;
3262 }
3263 else {
3264 pdata[0] = EXT4;
3265 pdata[1] = (unsigned char)(code & 0xff);
3266 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3267 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3268 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3269 n = 5;
3270 }
3271
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003272 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003273 goto error;
3274 }
3275 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003276 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003277 if (parent == module) {
3278 Py_INCREF(lastname);
3279 Py_DECREF(global_name);
3280 global_name = lastname;
3281 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003282 if (self->proto >= 4) {
3283 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003284
Christian Heimese8b1ba12013-11-23 21:13:39 +01003285 if (save(self, module_name, 0) < 0)
3286 goto error;
3287 if (save(self, global_name, 0) < 0)
3288 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003289
3290 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3291 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003292 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003293 else if (parent != module) {
3294 PickleState *st = _Pickle_GetGlobalState();
3295 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3296 st->getattr, parent, lastname);
3297 status = save_reduce(self, reduce_value, NULL);
3298 Py_DECREF(reduce_value);
3299 if (status < 0)
3300 goto error;
3301 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003302 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003303 /* Generate a normal global opcode if we are using a pickle
3304 protocol < 4, or if the object is not registered in the
3305 extension registry. */
3306 PyObject *encoded;
3307 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003308
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003309 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003310 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003311
3312 /* For protocol < 3 and if the user didn't request against doing
3313 so, we convert module names to the old 2.x module names. */
3314 if (self->proto < 3 && self->fix_imports) {
3315 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003316 goto error;
3317 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003318 }
3319
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003320 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3321 both the module name and the global name using UTF-8. We do so
3322 only when we are using the pickle protocol newer than version
3323 3. This is to ensure compatibility with older Unpickler running
3324 on Python 2.x. */
3325 if (self->proto == 3) {
3326 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003327 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003328 else {
3329 unicode_encoder = PyUnicode_AsASCIIString;
3330 }
3331 encoded = unicode_encoder(module_name);
3332 if (encoded == NULL) {
3333 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003334 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003335 "can't pickle module identifier '%S' using "
3336 "pickle protocol %i",
3337 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003338 goto error;
3339 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003340 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3341 PyBytes_GET_SIZE(encoded)) < 0) {
3342 Py_DECREF(encoded);
3343 goto error;
3344 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003345 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003346 if(_Pickler_Write(self, "\n", 1) < 0)
3347 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003348
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003349 /* Save the name of the module. */
3350 encoded = unicode_encoder(global_name);
3351 if (encoded == NULL) {
3352 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003353 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003354 "can't pickle global identifier '%S' using "
3355 "pickle protocol %i",
3356 global_name, self->proto);
3357 goto error;
3358 }
3359 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3360 PyBytes_GET_SIZE(encoded)) < 0) {
3361 Py_DECREF(encoded);
3362 goto error;
3363 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003364 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003365 if (_Pickler_Write(self, "\n", 1) < 0)
3366 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003367 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003368 /* Memoize the object. */
3369 if (memo_put(self, obj) < 0)
3370 goto error;
3371 }
3372
3373 if (0) {
3374 error:
3375 status = -1;
3376 }
3377 Py_XDECREF(module_name);
3378 Py_XDECREF(global_name);
3379 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003380 Py_XDECREF(parent);
3381 Py_XDECREF(dotted_path);
3382 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003383
3384 return status;
3385}
3386
3387static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003388save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3389{
3390 PyObject *reduce_value;
3391 int status;
3392
3393 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3394 if (reduce_value == NULL) {
3395 return -1;
3396 }
3397 status = save_reduce(self, reduce_value, obj);
3398 Py_DECREF(reduce_value);
3399 return status;
3400}
3401
3402static int
3403save_type(PicklerObject *self, PyObject *obj)
3404{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003405 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003406 return save_singleton_type(self, obj, Py_None);
3407 }
3408 else if (obj == (PyObject *)&PyEllipsis_Type) {
3409 return save_singleton_type(self, obj, Py_Ellipsis);
3410 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003411 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003412 return save_singleton_type(self, obj, Py_NotImplemented);
3413 }
3414 return save_global(self, obj, NULL);
3415}
3416
3417static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003418save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3419{
3420 PyObject *pid = NULL;
3421 int status = 0;
3422
3423 const char persid_op = PERSID;
3424 const char binpersid_op = BINPERSID;
3425
3426 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003427 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003428 if (pid == NULL)
3429 return -1;
3430
3431 if (pid != Py_None) {
3432 if (self->bin) {
3433 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003434 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003435 goto error;
3436 }
3437 else {
3438 PyObject *pid_str = NULL;
3439 char *pid_ascii_bytes;
3440 Py_ssize_t size;
3441
3442 pid_str = PyObject_Str(pid);
3443 if (pid_str == NULL)
3444 goto error;
3445
3446 /* XXX: Should it check whether the persistent id only contains
3447 ASCII characters? And what if the pid contains embedded
3448 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003449 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003450 Py_DECREF(pid_str);
3451 if (pid_ascii_bytes == NULL)
3452 goto error;
3453
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003454 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3455 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3456 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003457 goto error;
3458 }
3459 status = 1;
3460 }
3461
3462 if (0) {
3463 error:
3464 status = -1;
3465 }
3466 Py_XDECREF(pid);
3467
3468 return status;
3469}
3470
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003471static PyObject *
3472get_class(PyObject *obj)
3473{
3474 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003475 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003476
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003477 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003478 if (cls == NULL) {
3479 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3480 PyErr_Clear();
3481 cls = (PyObject *) Py_TYPE(obj);
3482 Py_INCREF(cls);
3483 }
3484 }
3485 return cls;
3486}
3487
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003488/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3489 * appropriate __reduce__ method for obj.
3490 */
3491static int
3492save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3493{
3494 PyObject *callable;
3495 PyObject *argtup;
3496 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003497 PyObject *listitems = Py_None;
3498 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003499 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003500 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003501 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003502
3503 const char reduce_op = REDUCE;
3504 const char build_op = BUILD;
3505 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003506 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003507
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003508 size = PyTuple_Size(args);
3509 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003510 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003511 "__reduce__ must contain 2 through 5 elements");
3512 return -1;
3513 }
3514
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003515 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3516 &callable, &argtup, &state, &listitems, &dictitems))
3517 return -1;
3518
3519 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003520 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003521 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003522 return -1;
3523 }
3524 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003525 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003526 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003527 return -1;
3528 }
3529
3530 if (state == Py_None)
3531 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003532
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003533 if (listitems == Py_None)
3534 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003535 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003536 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003537 "returned by __reduce__ must be an iterator, not %s",
3538 Py_TYPE(listitems)->tp_name);
3539 return -1;
3540 }
3541
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003542 if (dictitems == Py_None)
3543 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003544 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003545 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003546 "returned by __reduce__ must be an iterator, not %s",
3547 Py_TYPE(dictitems)->tp_name);
3548 return -1;
3549 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003550
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003551 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003552 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003553 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003554
Victor Stinner804e05e2013-11-14 01:26:17 +01003555 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003556 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003557 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003558 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003559 }
3560 PyErr_Clear();
3561 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003562 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003563 _Py_IDENTIFIER(__newobj_ex__);
3564 use_newobj_ex = PyUnicode_Compare(
3565 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003566 if (!use_newobj_ex) {
3567 _Py_IDENTIFIER(__newobj__);
3568 use_newobj = PyUnicode_Compare(
3569 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
3570 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003571 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003572 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003573 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003574
3575 if (use_newobj_ex) {
3576 PyObject *cls;
3577 PyObject *args;
3578 PyObject *kwargs;
3579
3580 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003581 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003582 "length of the NEWOBJ_EX argument tuple must be "
3583 "exactly 3, not %zd", Py_SIZE(argtup));
3584 return -1;
3585 }
3586
3587 cls = PyTuple_GET_ITEM(argtup, 0);
3588 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003589 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003590 "first item from NEWOBJ_EX argument tuple must "
3591 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3592 return -1;
3593 }
3594 args = PyTuple_GET_ITEM(argtup, 1);
3595 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003596 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003597 "second item from NEWOBJ_EX argument tuple must "
3598 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3599 return -1;
3600 }
3601 kwargs = PyTuple_GET_ITEM(argtup, 2);
3602 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003603 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003604 "third item from NEWOBJ_EX argument tuple must "
3605 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3606 return -1;
3607 }
3608
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003609 if (self->proto >= 4) {
3610 if (save(self, cls, 0) < 0 ||
3611 save(self, args, 0) < 0 ||
3612 save(self, kwargs, 0) < 0 ||
3613 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3614 return -1;
3615 }
3616 }
3617 else {
3618 PyObject *newargs;
3619 PyObject *cls_new;
3620 Py_ssize_t i;
3621 _Py_IDENTIFIER(__new__);
3622
3623 newargs = PyTuple_New(Py_SIZE(args) + 2);
3624 if (newargs == NULL)
3625 return -1;
3626
3627 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3628 if (cls_new == NULL) {
3629 Py_DECREF(newargs);
3630 return -1;
3631 }
3632 PyTuple_SET_ITEM(newargs, 0, cls_new);
3633 Py_INCREF(cls);
3634 PyTuple_SET_ITEM(newargs, 1, cls);
3635 for (i = 0; i < Py_SIZE(args); i++) {
3636 PyObject *item = PyTuple_GET_ITEM(args, i);
3637 Py_INCREF(item);
3638 PyTuple_SET_ITEM(newargs, i + 2, item);
3639 }
3640
3641 callable = PyObject_Call(st->partial, newargs, kwargs);
3642 Py_DECREF(newargs);
3643 if (callable == NULL)
3644 return -1;
3645
3646 newargs = PyTuple_New(0);
3647 if (newargs == NULL) {
3648 Py_DECREF(callable);
3649 return -1;
3650 }
3651
3652 if (save(self, callable, 0) < 0 ||
3653 save(self, newargs, 0) < 0 ||
3654 _Pickler_Write(self, &reduce_op, 1) < 0) {
3655 Py_DECREF(newargs);
3656 Py_DECREF(callable);
3657 return -1;
3658 }
3659 Py_DECREF(newargs);
3660 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003661 }
3662 }
3663 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003664 PyObject *cls;
3665 PyObject *newargtup;
3666 PyObject *obj_class;
3667 int p;
3668
3669 /* Sanity checks. */
3670 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003671 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003672 return -1;
3673 }
3674
3675 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003676 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003677 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003678 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003679 return -1;
3680 }
3681
3682 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003683 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003684 p = obj_class != cls; /* true iff a problem */
3685 Py_DECREF(obj_class);
3686 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003687 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003688 "__newobj__ args has the wrong class");
3689 return -1;
3690 }
3691 }
3692 /* XXX: These calls save() are prone to infinite recursion. Imagine
3693 what happen if the value returned by the __reduce__() method of
3694 some extension type contains another object of the same type. Ouch!
3695
3696 Here is a quick example, that I ran into, to illustrate what I
3697 mean:
3698
3699 >>> import pickle, copyreg
3700 >>> copyreg.dispatch_table.pop(complex)
3701 >>> pickle.dumps(1+2j)
3702 Traceback (most recent call last):
3703 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003704 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003705
3706 Removing the complex class from copyreg.dispatch_table made the
3707 __reduce_ex__() method emit another complex object:
3708
3709 >>> (1+1j).__reduce_ex__(2)
3710 (<function __newobj__ at 0xb7b71c3c>,
3711 (<class 'complex'>, (1+1j)), None, None, None)
3712
3713 Thus when save() was called on newargstup (the 2nd item) recursion
3714 ensued. Of course, the bug was in the complex class which had a
3715 broken __getnewargs__() that emitted another complex object. But,
3716 the point, here, is it is quite easy to end up with a broken reduce
3717 function. */
3718
3719 /* Save the class and its __new__ arguments. */
3720 if (save(self, cls, 0) < 0)
3721 return -1;
3722
3723 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3724 if (newargtup == NULL)
3725 return -1;
3726
3727 p = save(self, newargtup, 0);
3728 Py_DECREF(newargtup);
3729 if (p < 0)
3730 return -1;
3731
3732 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003733 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003734 return -1;
3735 }
3736 else { /* Not using NEWOBJ. */
3737 if (save(self, callable, 0) < 0 ||
3738 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003739 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003740 return -1;
3741 }
3742
3743 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3744 the caller do not want to memoize the object. Not particularly useful,
3745 but that is to mimic the behavior save_reduce() in pickle.py when
3746 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003747 if (obj != NULL) {
3748 /* If the object is already in the memo, this means it is
3749 recursive. In this case, throw away everything we put on the
3750 stack, and fetch the object back from the memo. */
3751 if (PyMemoTable_Get(self->memo, obj)) {
3752 const char pop_op = POP;
3753
3754 if (_Pickler_Write(self, &pop_op, 1) < 0)
3755 return -1;
3756 if (memo_get(self, obj) < 0)
3757 return -1;
3758
3759 return 0;
3760 }
3761 else if (memo_put(self, obj) < 0)
3762 return -1;
3763 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003764
3765 if (listitems && batch_list(self, listitems) < 0)
3766 return -1;
3767
3768 if (dictitems && batch_dict(self, dictitems) < 0)
3769 return -1;
3770
3771 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003772 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003773 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003774 return -1;
3775 }
3776
3777 return 0;
3778}
3779
3780static int
3781save(PicklerObject *self, PyObject *obj, int pers_save)
3782{
3783 PyTypeObject *type;
3784 PyObject *reduce_func = NULL;
3785 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003786 int status = 0;
3787
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003788 if (_Pickler_OpcodeBoundary(self) < 0)
3789 return -1;
3790
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003791 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003792 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003793
3794 /* The extra pers_save argument is necessary to avoid calling save_pers()
3795 on its returned object. */
3796 if (!pers_save && self->pers_func) {
3797 /* save_pers() returns:
3798 -1 to signal an error;
3799 0 if it did nothing successfully;
3800 1 if a persistent id was saved.
3801 */
3802 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3803 goto done;
3804 }
3805
3806 type = Py_TYPE(obj);
3807
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003808 /* The old cPickle had an optimization that used switch-case statement
3809 dispatching on the first letter of the type name. This has was removed
3810 since benchmarks shown that this optimization was actually slowing
3811 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003812
3813 /* Atom types; these aren't memoized, so don't check the memo. */
3814
3815 if (obj == Py_None) {
3816 status = save_none(self, obj);
3817 goto done;
3818 }
3819 else if (obj == Py_False || obj == Py_True) {
3820 status = save_bool(self, obj);
3821 goto done;
3822 }
3823 else if (type == &PyLong_Type) {
3824 status = save_long(self, obj);
3825 goto done;
3826 }
3827 else if (type == &PyFloat_Type) {
3828 status = save_float(self, obj);
3829 goto done;
3830 }
3831
3832 /* Check the memo to see if it has the object. If so, generate
3833 a GET (or BINGET) opcode, instead of pickling the object
3834 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003835 if (PyMemoTable_Get(self->memo, obj)) {
3836 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003837 goto error;
3838 goto done;
3839 }
3840
3841 if (type == &PyBytes_Type) {
3842 status = save_bytes(self, obj);
3843 goto done;
3844 }
3845 else if (type == &PyUnicode_Type) {
3846 status = save_unicode(self, obj);
3847 goto done;
3848 }
3849 else if (type == &PyDict_Type) {
3850 status = save_dict(self, obj);
3851 goto done;
3852 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003853 else if (type == &PySet_Type) {
3854 status = save_set(self, obj);
3855 goto done;
3856 }
3857 else if (type == &PyFrozenSet_Type) {
3858 status = save_frozenset(self, obj);
3859 goto done;
3860 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003861 else if (type == &PyList_Type) {
3862 status = save_list(self, obj);
3863 goto done;
3864 }
3865 else if (type == &PyTuple_Type) {
3866 status = save_tuple(self, obj);
3867 goto done;
3868 }
3869 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003870 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003871 goto done;
3872 }
3873 else if (type == &PyFunction_Type) {
3874 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003875 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003876 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003877
3878 /* XXX: This part needs some unit tests. */
3879
3880 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003881 * self.dispatch_table, copyreg.dispatch_table, the object's
3882 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003883 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003884 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003885 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003886 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3887 (PyObject *)type);
3888 if (reduce_func == NULL) {
3889 if (PyErr_Occurred()) {
3890 goto error;
3891 }
3892 } else {
3893 /* PyDict_GetItemWithError() returns a borrowed reference.
3894 Increase the reference count to be consistent with
3895 PyObject_GetItem and _PyObject_GetAttrId used below. */
3896 Py_INCREF(reduce_func);
3897 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003898 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003899 reduce_func = PyObject_GetItem(self->dispatch_table,
3900 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003901 if (reduce_func == NULL) {
3902 if (PyErr_ExceptionMatches(PyExc_KeyError))
3903 PyErr_Clear();
3904 else
3905 goto error;
3906 }
3907 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003908 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003909 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003910 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003911 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003912 else if (PyType_IsSubtype(type, &PyType_Type)) {
3913 status = save_global(self, obj, NULL);
3914 goto done;
3915 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003916 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003917 _Py_IDENTIFIER(__reduce__);
3918 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003919
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003920
3921 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3922 automatically defined as __reduce__. While this is convenient, this
3923 make it impossible to know which method was actually called. Of
3924 course, this is not a big deal. But still, it would be nice to let
3925 the user know which method was called when something go
3926 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3927 don't actually have to check for a __reduce__ method. */
3928
3929 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003930 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003931 if (reduce_func != NULL) {
3932 PyObject *proto;
3933 proto = PyLong_FromLong(self->proto);
3934 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003935 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003936 }
3937 }
3938 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003939 PickleState *st = _Pickle_GetGlobalState();
3940
3941 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003942 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003943 }
3944 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003945 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003946 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003947 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003948 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003949 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003950 PyObject *empty_tuple = PyTuple_New(0);
3951 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003952 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003953 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003954 }
3955 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003956 PyErr_Format(st->PicklingError,
3957 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003958 type->tp_name, obj);
3959 goto error;
3960 }
3961 }
3962 }
3963
3964 if (reduce_value == NULL)
3965 goto error;
3966
3967 if (PyUnicode_Check(reduce_value)) {
3968 status = save_global(self, obj, reduce_value);
3969 goto done;
3970 }
3971
3972 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003973 PickleState *st = _Pickle_GetGlobalState();
3974 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003975 "__reduce__ must return a string or tuple");
3976 goto error;
3977 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003978
3979 status = save_reduce(self, reduce_value, obj);
3980
3981 if (0) {
3982 error:
3983 status = -1;
3984 }
3985 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003986
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003987 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003988 Py_XDECREF(reduce_func);
3989 Py_XDECREF(reduce_value);
3990
3991 return status;
3992}
3993
3994static int
3995dump(PicklerObject *self, PyObject *obj)
3996{
3997 const char stop_op = STOP;
3998
3999 if (self->proto >= 2) {
4000 char header[2];
4001
4002 header[0] = PROTO;
4003 assert(self->proto >= 0 && self->proto < 256);
4004 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004005 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004006 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004007 if (self->proto >= 4)
4008 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004009 }
4010
4011 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004012 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004013 return -1;
4014
4015 return 0;
4016}
4017
Larry Hastings61272b72014-01-07 12:41:53 -08004018/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004019
4020_pickle.Pickler.clear_memo
4021
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004022Clears the pickler's "memo".
4023
4024The memo is the data structure that remembers which objects the
4025pickler has already seen, so that shared or recursive objects are
4026pickled by reference and not by value. This method is useful when
4027re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004028[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004029
Larry Hastings3cceb382014-01-04 11:09:09 -08004030static PyObject *
4031_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004032/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004033{
4034 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004035 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004036
4037 Py_RETURN_NONE;
4038}
4039
Larry Hastings61272b72014-01-07 12:41:53 -08004040/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004041
4042_pickle.Pickler.dump
4043
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004044 obj: object
4045 /
4046
4047Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004048[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004049
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004050static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004051_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004052/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004053{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004054 /* Check whether the Pickler was initialized correctly (issue3664).
4055 Developers often forget to call __init__() in their subclasses, which
4056 would trigger a segfault without this check. */
4057 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004058 PickleState *st = _Pickle_GetGlobalState();
4059 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004060 "Pickler.__init__() was not called by %s.__init__()",
4061 Py_TYPE(self)->tp_name);
4062 return NULL;
4063 }
4064
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004065 if (_Pickler_ClearBuffer(self) < 0)
4066 return NULL;
4067
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004068 if (dump(self, obj) < 0)
4069 return NULL;
4070
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004071 if (_Pickler_FlushToFile(self) < 0)
4072 return NULL;
4073
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004074 Py_RETURN_NONE;
4075}
4076
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004077/*[clinic input]
4078
4079_pickle.Pickler.__sizeof__ -> Py_ssize_t
4080
4081Returns size in memory, in bytes.
4082[clinic start generated code]*/
4083
4084static Py_ssize_t
4085_pickle_Pickler___sizeof___impl(PicklerObject *self)
4086/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4087{
4088 Py_ssize_t res, s;
4089
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004090 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004091 if (self->memo != NULL) {
4092 res += sizeof(PyMemoTable);
4093 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4094 }
4095 if (self->output_buffer != NULL) {
4096 s = _PySys_GetSizeOf(self->output_buffer);
4097 if (s == -1)
4098 return -1;
4099 res += s;
4100 }
4101 return res;
4102}
4103
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004104static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004105 _PICKLE_PICKLER_DUMP_METHODDEF
4106 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004107 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004108 {NULL, NULL} /* sentinel */
4109};
4110
4111static void
4112Pickler_dealloc(PicklerObject *self)
4113{
4114 PyObject_GC_UnTrack(self);
4115
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004116 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004117 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004118 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004119 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004120 Py_XDECREF(self->fast_memo);
4121
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004122 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004123
4124 Py_TYPE(self)->tp_free((PyObject *)self);
4125}
4126
4127static int
4128Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4129{
4130 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004131 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004132 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004133 Py_VISIT(self->fast_memo);
4134 return 0;
4135}
4136
4137static int
4138Pickler_clear(PicklerObject *self)
4139{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004140 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004141 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004142 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004143 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004144 Py_CLEAR(self->fast_memo);
4145
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004146 if (self->memo != NULL) {
4147 PyMemoTable *memo = self->memo;
4148 self->memo = NULL;
4149 PyMemoTable_Del(memo);
4150 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004151 return 0;
4152}
4153
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004154
Larry Hastings61272b72014-01-07 12:41:53 -08004155/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004156
4157_pickle.Pickler.__init__
4158
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004159 file: object
4160 protocol: object = NULL
4161 fix_imports: bool = True
4162
4163This takes a binary file for writing a pickle data stream.
4164
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004165The optional *protocol* argument tells the pickler to use the given
4166protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4167protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004168
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004169Specifying a negative protocol version selects the highest protocol
4170version supported. The higher the protocol used, the more recent the
4171version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004172
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004173The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004174bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004175writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004176this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004177
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004178If *fix_imports* is True and protocol is less than 3, pickle will try
4179to map the new Python 3 names to the old module names used in Python
41802, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004181[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004182
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004183static int
Larry Hastings89964c42015-04-14 18:07:59 -04004184_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4185 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004186/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004187{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004188 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004189 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004190
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004191 /* In case of multiple __init__() calls, clear previous content. */
4192 if (self->write != NULL)
4193 (void)Pickler_clear(self);
4194
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004195 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004196 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004197
4198 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004199 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004200
4201 /* memo and output_buffer may have already been created in _Pickler_New */
4202 if (self->memo == NULL) {
4203 self->memo = PyMemoTable_New();
4204 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004205 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004206 }
4207 self->output_len = 0;
4208 if (self->output_buffer == NULL) {
4209 self->max_output_len = WRITE_BUF_SIZE;
4210 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4211 self->max_output_len);
4212 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004213 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004214 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004215
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004216 self->fast = 0;
4217 self->fast_nesting = 0;
4218 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004219 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004220 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4221 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4222 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004223 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004224 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004225 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004226 self->dispatch_table = NULL;
4227 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4228 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4229 &PyId_dispatch_table);
4230 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004231 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004232 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004233
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004234 return 0;
4235}
4236
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004237
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004238/* Define a proxy object for the Pickler's internal memo object. This is to
4239 * avoid breaking code like:
4240 * pickler.memo.clear()
4241 * and
4242 * pickler.memo = saved_memo
4243 * Is this a good idea? Not really, but we don't want to break code that uses
4244 * it. Note that we don't implement the entire mapping API here. This is
4245 * intentional, as these should be treated as black-box implementation details.
4246 */
4247
Larry Hastings61272b72014-01-07 12:41:53 -08004248/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004249_pickle.PicklerMemoProxy.clear
4250
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004251Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004252[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004253
Larry Hastings3cceb382014-01-04 11:09:09 -08004254static PyObject *
4255_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004256/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004257{
4258 if (self->pickler->memo)
4259 PyMemoTable_Clear(self->pickler->memo);
4260 Py_RETURN_NONE;
4261}
4262
Larry Hastings61272b72014-01-07 12:41:53 -08004263/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004264_pickle.PicklerMemoProxy.copy
4265
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004266Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004267[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004268
Larry Hastings3cceb382014-01-04 11:09:09 -08004269static PyObject *
4270_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004271/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004272{
4273 Py_ssize_t i;
4274 PyMemoTable *memo;
4275 PyObject *new_memo = PyDict_New();
4276 if (new_memo == NULL)
4277 return NULL;
4278
4279 memo = self->pickler->memo;
4280 for (i = 0; i < memo->mt_allocated; ++i) {
4281 PyMemoEntry entry = memo->mt_table[i];
4282 if (entry.me_key != NULL) {
4283 int status;
4284 PyObject *key, *value;
4285
4286 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004287 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004288
4289 if (key == NULL || value == NULL) {
4290 Py_XDECREF(key);
4291 Py_XDECREF(value);
4292 goto error;
4293 }
4294 status = PyDict_SetItem(new_memo, key, value);
4295 Py_DECREF(key);
4296 Py_DECREF(value);
4297 if (status < 0)
4298 goto error;
4299 }
4300 }
4301 return new_memo;
4302
4303 error:
4304 Py_XDECREF(new_memo);
4305 return NULL;
4306}
4307
Larry Hastings61272b72014-01-07 12:41:53 -08004308/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004309_pickle.PicklerMemoProxy.__reduce__
4310
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004311Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004312[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004313
Larry Hastings3cceb382014-01-04 11:09:09 -08004314static PyObject *
4315_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004316/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004317{
4318 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004319 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004320 if (contents == NULL)
4321 return NULL;
4322
4323 reduce_value = PyTuple_New(2);
4324 if (reduce_value == NULL) {
4325 Py_DECREF(contents);
4326 return NULL;
4327 }
4328 dict_args = PyTuple_New(1);
4329 if (dict_args == NULL) {
4330 Py_DECREF(contents);
4331 Py_DECREF(reduce_value);
4332 return NULL;
4333 }
4334 PyTuple_SET_ITEM(dict_args, 0, contents);
4335 Py_INCREF((PyObject *)&PyDict_Type);
4336 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4337 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4338 return reduce_value;
4339}
4340
4341static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004342 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4343 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4344 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004345 {NULL, NULL} /* sentinel */
4346};
4347
4348static void
4349PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4350{
4351 PyObject_GC_UnTrack(self);
4352 Py_XDECREF(self->pickler);
4353 PyObject_GC_Del((PyObject *)self);
4354}
4355
4356static int
4357PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4358 visitproc visit, void *arg)
4359{
4360 Py_VISIT(self->pickler);
4361 return 0;
4362}
4363
4364static int
4365PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4366{
4367 Py_CLEAR(self->pickler);
4368 return 0;
4369}
4370
4371static PyTypeObject PicklerMemoProxyType = {
4372 PyVarObject_HEAD_INIT(NULL, 0)
4373 "_pickle.PicklerMemoProxy", /*tp_name*/
4374 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4375 0,
4376 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4377 0, /* tp_print */
4378 0, /* tp_getattr */
4379 0, /* tp_setattr */
4380 0, /* tp_compare */
4381 0, /* tp_repr */
4382 0, /* tp_as_number */
4383 0, /* tp_as_sequence */
4384 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004385 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004386 0, /* tp_call */
4387 0, /* tp_str */
4388 PyObject_GenericGetAttr, /* tp_getattro */
4389 PyObject_GenericSetAttr, /* tp_setattro */
4390 0, /* tp_as_buffer */
4391 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4392 0, /* tp_doc */
4393 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4394 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4395 0, /* tp_richcompare */
4396 0, /* tp_weaklistoffset */
4397 0, /* tp_iter */
4398 0, /* tp_iternext */
4399 picklerproxy_methods, /* tp_methods */
4400};
4401
4402static PyObject *
4403PicklerMemoProxy_New(PicklerObject *pickler)
4404{
4405 PicklerMemoProxyObject *self;
4406
4407 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4408 if (self == NULL)
4409 return NULL;
4410 Py_INCREF(pickler);
4411 self->pickler = pickler;
4412 PyObject_GC_Track(self);
4413 return (PyObject *)self;
4414}
4415
4416/*****************************************************************************/
4417
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004418static PyObject *
4419Pickler_get_memo(PicklerObject *self)
4420{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004421 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004422}
4423
4424static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004425Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004426{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004427 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004428
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004429 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004430 PyErr_SetString(PyExc_TypeError,
4431 "attribute deletion is not supported");
4432 return -1;
4433 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004434
4435 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4436 PicklerObject *pickler =
4437 ((PicklerMemoProxyObject *)obj)->pickler;
4438
4439 new_memo = PyMemoTable_Copy(pickler->memo);
4440 if (new_memo == NULL)
4441 return -1;
4442 }
4443 else if (PyDict_Check(obj)) {
4444 Py_ssize_t i = 0;
4445 PyObject *key, *value;
4446
4447 new_memo = PyMemoTable_New();
4448 if (new_memo == NULL)
4449 return -1;
4450
4451 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004452 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004453 PyObject *memo_obj;
4454
4455 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4456 PyErr_SetString(PyExc_TypeError,
4457 "'memo' values must be 2-item tuples");
4458 goto error;
4459 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004460 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004461 if (memo_id == -1 && PyErr_Occurred())
4462 goto error;
4463 memo_obj = PyTuple_GET_ITEM(value, 1);
4464 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4465 goto error;
4466 }
4467 }
4468 else {
4469 PyErr_Format(PyExc_TypeError,
4470 "'memo' attribute must be an PicklerMemoProxy object"
4471 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004472 return -1;
4473 }
4474
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004475 PyMemoTable_Del(self->memo);
4476 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004477
4478 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004479
4480 error:
4481 if (new_memo)
4482 PyMemoTable_Del(new_memo);
4483 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004484}
4485
4486static PyObject *
4487Pickler_get_persid(PicklerObject *self)
4488{
4489 if (self->pers_func == NULL)
4490 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4491 else
4492 Py_INCREF(self->pers_func);
4493 return self->pers_func;
4494}
4495
4496static int
4497Pickler_set_persid(PicklerObject *self, PyObject *value)
4498{
4499 PyObject *tmp;
4500
4501 if (value == NULL) {
4502 PyErr_SetString(PyExc_TypeError,
4503 "attribute deletion is not supported");
4504 return -1;
4505 }
4506 if (!PyCallable_Check(value)) {
4507 PyErr_SetString(PyExc_TypeError,
4508 "persistent_id must be a callable taking one argument");
4509 return -1;
4510 }
4511
4512 tmp = self->pers_func;
4513 Py_INCREF(value);
4514 self->pers_func = value;
4515 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4516
4517 return 0;
4518}
4519
4520static PyMemberDef Pickler_members[] = {
4521 {"bin", T_INT, offsetof(PicklerObject, bin)},
4522 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004523 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004524 {NULL}
4525};
4526
4527static PyGetSetDef Pickler_getsets[] = {
4528 {"memo", (getter)Pickler_get_memo,
4529 (setter)Pickler_set_memo},
4530 {"persistent_id", (getter)Pickler_get_persid,
4531 (setter)Pickler_set_persid},
4532 {NULL}
4533};
4534
4535static PyTypeObject Pickler_Type = {
4536 PyVarObject_HEAD_INIT(NULL, 0)
4537 "_pickle.Pickler" , /*tp_name*/
4538 sizeof(PicklerObject), /*tp_basicsize*/
4539 0, /*tp_itemsize*/
4540 (destructor)Pickler_dealloc, /*tp_dealloc*/
4541 0, /*tp_print*/
4542 0, /*tp_getattr*/
4543 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004544 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004545 0, /*tp_repr*/
4546 0, /*tp_as_number*/
4547 0, /*tp_as_sequence*/
4548 0, /*tp_as_mapping*/
4549 0, /*tp_hash*/
4550 0, /*tp_call*/
4551 0, /*tp_str*/
4552 0, /*tp_getattro*/
4553 0, /*tp_setattro*/
4554 0, /*tp_as_buffer*/
4555 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004556 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004557 (traverseproc)Pickler_traverse, /*tp_traverse*/
4558 (inquiry)Pickler_clear, /*tp_clear*/
4559 0, /*tp_richcompare*/
4560 0, /*tp_weaklistoffset*/
4561 0, /*tp_iter*/
4562 0, /*tp_iternext*/
4563 Pickler_methods, /*tp_methods*/
4564 Pickler_members, /*tp_members*/
4565 Pickler_getsets, /*tp_getset*/
4566 0, /*tp_base*/
4567 0, /*tp_dict*/
4568 0, /*tp_descr_get*/
4569 0, /*tp_descr_set*/
4570 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004571 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004572 PyType_GenericAlloc, /*tp_alloc*/
4573 PyType_GenericNew, /*tp_new*/
4574 PyObject_GC_Del, /*tp_free*/
4575 0, /*tp_is_gc*/
4576};
4577
Victor Stinner121aab42011-09-29 23:40:53 +02004578/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004579
4580 XXX: It would be nice to able to avoid Python function call overhead, by
4581 using directly the C version of find_class(), when find_class() is not
4582 overridden by a subclass. Although, this could become rather hackish. A
4583 simpler optimization would be to call the C function when self is not a
4584 subclass instance. */
4585static PyObject *
4586find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4587{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004588 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004589
4590 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4591 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004592}
4593
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004594static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004595marker(UnpicklerObject *self)
4596{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004597 Py_ssize_t mark;
4598
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004599 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004600 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004601 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004602 return -1;
4603 }
4604
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004605 mark = self->marks[--self->num_marks];
4606 self->stack->mark_set = self->num_marks != 0;
4607 self->stack->fence = self->num_marks ?
4608 self->marks[self->num_marks - 1] : 0;
4609 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004610}
4611
4612static int
4613load_none(UnpicklerObject *self)
4614{
4615 PDATA_APPEND(self->stack, Py_None, -1);
4616 return 0;
4617}
4618
4619static int
4620bad_readline(void)
4621{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004622 PickleState *st = _Pickle_GetGlobalState();
4623 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004624 return -1;
4625}
4626
4627static int
4628load_int(UnpicklerObject *self)
4629{
4630 PyObject *value;
4631 char *endptr, *s;
4632 Py_ssize_t len;
4633 long x;
4634
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004635 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004636 return -1;
4637 if (len < 2)
4638 return bad_readline();
4639
4640 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004641 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004642 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004643 x = strtol(s, &endptr, 0);
4644
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004645 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004646 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004647 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004648 errno = 0;
4649 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004650 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004651 if (value == NULL) {
4652 PyErr_SetString(PyExc_ValueError,
4653 "could not convert string to int");
4654 return -1;
4655 }
4656 }
4657 else {
4658 if (len == 3 && (x == 0 || x == 1)) {
4659 if ((value = PyBool_FromLong(x)) == NULL)
4660 return -1;
4661 }
4662 else {
4663 if ((value = PyLong_FromLong(x)) == NULL)
4664 return -1;
4665 }
4666 }
4667
4668 PDATA_PUSH(self->stack, value, -1);
4669 return 0;
4670}
4671
4672static int
4673load_bool(UnpicklerObject *self, PyObject *boolean)
4674{
4675 assert(boolean == Py_True || boolean == Py_False);
4676 PDATA_APPEND(self->stack, boolean, -1);
4677 return 0;
4678}
4679
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004680/* s contains x bytes of an unsigned little-endian integer. Return its value
4681 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4682 */
4683static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004684calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004685{
4686 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004687 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004688 size_t x = 0;
4689
Serhiy Storchakae0606192015-09-29 22:10:07 +03004690 if (nbytes > (int)sizeof(size_t)) {
4691 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4692 * have 64-bit size that can't be represented on 32-bit platform.
4693 */
4694 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4695 if (s[i])
4696 return -1;
4697 }
4698 nbytes = (int)sizeof(size_t);
4699 }
4700 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004701 x |= (size_t) s[i] << (8 * i);
4702 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004703
4704 if (x > PY_SSIZE_T_MAX)
4705 return -1;
4706 else
4707 return (Py_ssize_t) x;
4708}
4709
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004710/* s contains x bytes of a little-endian integer. Return its value as a
4711 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4712 * int, but when x is 4 it's a signed one. This is an historical source
4713 * of x-platform bugs.
4714 */
4715static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004716calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004717{
4718 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004719 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004720 long x = 0;
4721
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004722 for (i = 0; i < nbytes; i++) {
4723 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004724 }
4725
4726 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4727 * is signed, so on a box with longs bigger than 4 bytes we need
4728 * to extend a BININT's sign bit to the full width.
4729 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004730 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004731 x |= -(x & (1L << 31));
4732 }
4733
4734 return x;
4735}
4736
4737static int
4738load_binintx(UnpicklerObject *self, char *s, int size)
4739{
4740 PyObject *value;
4741 long x;
4742
4743 x = calc_binint(s, size);
4744
4745 if ((value = PyLong_FromLong(x)) == NULL)
4746 return -1;
4747
4748 PDATA_PUSH(self->stack, value, -1);
4749 return 0;
4750}
4751
4752static int
4753load_binint(UnpicklerObject *self)
4754{
4755 char *s;
4756
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004757 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004758 return -1;
4759
4760 return load_binintx(self, s, 4);
4761}
4762
4763static int
4764load_binint1(UnpicklerObject *self)
4765{
4766 char *s;
4767
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004768 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004769 return -1;
4770
4771 return load_binintx(self, s, 1);
4772}
4773
4774static int
4775load_binint2(UnpicklerObject *self)
4776{
4777 char *s;
4778
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004779 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004780 return -1;
4781
4782 return load_binintx(self, s, 2);
4783}
4784
4785static int
4786load_long(UnpicklerObject *self)
4787{
4788 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004789 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004790 Py_ssize_t len;
4791
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004792 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004793 return -1;
4794 if (len < 2)
4795 return bad_readline();
4796
Mark Dickinson8dd05142009-01-20 20:43:58 +00004797 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4798 the 'L' before calling PyLong_FromString. In order to maintain
4799 compatibility with Python 3.0.0, we don't actually *require*
4800 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004801 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004802 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004803 /* XXX: Should the base argument explicitly set to 10? */
4804 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004805 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004806 return -1;
4807
4808 PDATA_PUSH(self->stack, value, -1);
4809 return 0;
4810}
4811
4812/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4813 * data following.
4814 */
4815static int
4816load_counted_long(UnpicklerObject *self, int size)
4817{
4818 PyObject *value;
4819 char *nbytes;
4820 char *pdata;
4821
4822 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004823 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004824 return -1;
4825
4826 size = calc_binint(nbytes, size);
4827 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004828 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004829 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004830 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004831 "LONG pickle has negative byte count");
4832 return -1;
4833 }
4834
4835 if (size == 0)
4836 value = PyLong_FromLong(0L);
4837 else {
4838 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004839 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004840 return -1;
4841 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4842 1 /* little endian */ , 1 /* signed */ );
4843 }
4844 if (value == NULL)
4845 return -1;
4846 PDATA_PUSH(self->stack, value, -1);
4847 return 0;
4848}
4849
4850static int
4851load_float(UnpicklerObject *self)
4852{
4853 PyObject *value;
4854 char *endptr, *s;
4855 Py_ssize_t len;
4856 double d;
4857
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004858 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004859 return -1;
4860 if (len < 2)
4861 return bad_readline();
4862
4863 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004864 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4865 if (d == -1.0 && PyErr_Occurred())
4866 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004867 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004868 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4869 return -1;
4870 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004871 value = PyFloat_FromDouble(d);
4872 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004873 return -1;
4874
4875 PDATA_PUSH(self->stack, value, -1);
4876 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004877}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004878
4879static int
4880load_binfloat(UnpicklerObject *self)
4881{
4882 PyObject *value;
4883 double x;
4884 char *s;
4885
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004886 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004887 return -1;
4888
4889 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4890 if (x == -1.0 && PyErr_Occurred())
4891 return -1;
4892
4893 if ((value = PyFloat_FromDouble(x)) == NULL)
4894 return -1;
4895
4896 PDATA_PUSH(self->stack, value, -1);
4897 return 0;
4898}
4899
4900static int
4901load_string(UnpicklerObject *self)
4902{
4903 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004904 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004905 Py_ssize_t len;
4906 char *s, *p;
4907
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004908 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004909 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004910 /* Strip the newline */
4911 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004912 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004913 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004914 p = s + 1;
4915 len -= 2;
4916 }
4917 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004918 PickleState *st = _Pickle_GetGlobalState();
4919 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004920 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004921 return -1;
4922 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004923 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004924
4925 /* Use the PyBytes API to decode the string, since that is what is used
4926 to encode, and then coerce the result to Unicode. */
4927 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004928 if (bytes == NULL)
4929 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004930
4931 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4932 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4933 if (strcmp(self->encoding, "bytes") == 0) {
4934 obj = bytes;
4935 }
4936 else {
4937 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4938 Py_DECREF(bytes);
4939 if (obj == NULL) {
4940 return -1;
4941 }
4942 }
4943
4944 PDATA_PUSH(self->stack, obj, -1);
4945 return 0;
4946}
4947
4948static int
4949load_counted_binstring(UnpicklerObject *self, int nbytes)
4950{
4951 PyObject *obj;
4952 Py_ssize_t size;
4953 char *s;
4954
4955 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004956 return -1;
4957
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004958 size = calc_binsize(s, nbytes);
4959 if (size < 0) {
4960 PickleState *st = _Pickle_GetGlobalState();
4961 PyErr_Format(st->UnpicklingError,
4962 "BINSTRING exceeds system's maximum size of %zd bytes",
4963 PY_SSIZE_T_MAX);
4964 return -1;
4965 }
4966
4967 if (_Unpickler_Read(self, &s, size) < 0)
4968 return -1;
4969
4970 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4971 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4972 if (strcmp(self->encoding, "bytes") == 0) {
4973 obj = PyBytes_FromStringAndSize(s, size);
4974 }
4975 else {
4976 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4977 }
4978 if (obj == NULL) {
4979 return -1;
4980 }
4981
4982 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004983 return 0;
4984}
4985
4986static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004987load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004988{
4989 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004990 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004991 char *s;
4992
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004993 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004994 return -1;
4995
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004996 size = calc_binsize(s, nbytes);
4997 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004998 PyErr_Format(PyExc_OverflowError,
4999 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005000 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005001 return -1;
5002 }
5003
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005004 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005005 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005006
5007 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005008 if (bytes == NULL)
5009 return -1;
5010
5011 PDATA_PUSH(self->stack, bytes, -1);
5012 return 0;
5013}
5014
5015static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005016load_unicode(UnpicklerObject *self)
5017{
5018 PyObject *str;
5019 Py_ssize_t len;
5020 char *s;
5021
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005022 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005023 return -1;
5024 if (len < 1)
5025 return bad_readline();
5026
5027 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5028 if (str == NULL)
5029 return -1;
5030
5031 PDATA_PUSH(self->stack, str, -1);
5032 return 0;
5033}
5034
5035static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005036load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005037{
5038 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005039 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005040 char *s;
5041
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005042 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005043 return -1;
5044
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005045 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005046 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005047 PyErr_Format(PyExc_OverflowError,
5048 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005049 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005050 return -1;
5051 }
5052
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005053 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005054 return -1;
5055
Victor Stinner485fb562010-04-13 11:07:24 +00005056 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005057 if (str == NULL)
5058 return -1;
5059
5060 PDATA_PUSH(self->stack, str, -1);
5061 return 0;
5062}
5063
5064static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005065load_counted_tuple(UnpicklerObject *self, int len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005066{
5067 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005068
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005069 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005070 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005071
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005072 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005073 if (tuple == NULL)
5074 return -1;
5075 PDATA_PUSH(self->stack, tuple, -1);
5076 return 0;
5077}
5078
5079static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005080load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005081{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005082 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005083
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005084 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005085 return -1;
5086
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005087 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005088}
5089
5090static int
5091load_empty_list(UnpicklerObject *self)
5092{
5093 PyObject *list;
5094
5095 if ((list = PyList_New(0)) == NULL)
5096 return -1;
5097 PDATA_PUSH(self->stack, list, -1);
5098 return 0;
5099}
5100
5101static int
5102load_empty_dict(UnpicklerObject *self)
5103{
5104 PyObject *dict;
5105
5106 if ((dict = PyDict_New()) == NULL)
5107 return -1;
5108 PDATA_PUSH(self->stack, dict, -1);
5109 return 0;
5110}
5111
5112static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005113load_empty_set(UnpicklerObject *self)
5114{
5115 PyObject *set;
5116
5117 if ((set = PySet_New(NULL)) == NULL)
5118 return -1;
5119 PDATA_PUSH(self->stack, set, -1);
5120 return 0;
5121}
5122
5123static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005124load_list(UnpicklerObject *self)
5125{
5126 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005127 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005128
5129 if ((i = marker(self)) < 0)
5130 return -1;
5131
5132 list = Pdata_poplist(self->stack, i);
5133 if (list == NULL)
5134 return -1;
5135 PDATA_PUSH(self->stack, list, -1);
5136 return 0;
5137}
5138
5139static int
5140load_dict(UnpicklerObject *self)
5141{
5142 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005143 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005144
5145 if ((i = marker(self)) < 0)
5146 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005147 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005148
5149 if ((dict = PyDict_New()) == NULL)
5150 return -1;
5151
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005152 if ((j - i) % 2 != 0) {
5153 PickleState *st = _Pickle_GetGlobalState();
5154 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005155 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005156 return -1;
5157 }
5158
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005159 for (k = i + 1; k < j; k += 2) {
5160 key = self->stack->data[k - 1];
5161 value = self->stack->data[k];
5162 if (PyDict_SetItem(dict, key, value) < 0) {
5163 Py_DECREF(dict);
5164 return -1;
5165 }
5166 }
5167 Pdata_clear(self->stack, i);
5168 PDATA_PUSH(self->stack, dict, -1);
5169 return 0;
5170}
5171
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005172static int
5173load_frozenset(UnpicklerObject *self)
5174{
5175 PyObject *items;
5176 PyObject *frozenset;
5177 Py_ssize_t i;
5178
5179 if ((i = marker(self)) < 0)
5180 return -1;
5181
5182 items = Pdata_poptuple(self->stack, i);
5183 if (items == NULL)
5184 return -1;
5185
5186 frozenset = PyFrozenSet_New(items);
5187 Py_DECREF(items);
5188 if (frozenset == NULL)
5189 return -1;
5190
5191 PDATA_PUSH(self->stack, frozenset, -1);
5192 return 0;
5193}
5194
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005195static PyObject *
5196instantiate(PyObject *cls, PyObject *args)
5197{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005198 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005199 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005200 /* Caller must assure args are a tuple. Normally, args come from
5201 Pdata_poptuple which packs objects from the top of the stack
5202 into a newly created tuple. */
5203 assert(PyTuple_Check(args));
5204 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005205 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005206 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005207 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005208 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005209 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005210
5211 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005212 }
5213 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005214}
5215
5216static int
5217load_obj(UnpicklerObject *self)
5218{
5219 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005220 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005221
5222 if ((i = marker(self)) < 0)
5223 return -1;
5224
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005225 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005226 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005227
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005228 args = Pdata_poptuple(self->stack, i + 1);
5229 if (args == NULL)
5230 return -1;
5231
5232 PDATA_POP(self->stack, cls);
5233 if (cls) {
5234 obj = instantiate(cls, args);
5235 Py_DECREF(cls);
5236 }
5237 Py_DECREF(args);
5238 if (obj == NULL)
5239 return -1;
5240
5241 PDATA_PUSH(self->stack, obj, -1);
5242 return 0;
5243}
5244
5245static int
5246load_inst(UnpicklerObject *self)
5247{
5248 PyObject *cls = NULL;
5249 PyObject *args = NULL;
5250 PyObject *obj = NULL;
5251 PyObject *module_name;
5252 PyObject *class_name;
5253 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005254 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005255 char *s;
5256
5257 if ((i = marker(self)) < 0)
5258 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005259 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005260 return -1;
5261 if (len < 2)
5262 return bad_readline();
5263
5264 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5265 identifiers are permitted in Python 3.0, since the INST opcode is only
5266 supported by older protocols on Python 2.x. */
5267 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5268 if (module_name == NULL)
5269 return -1;
5270
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005271 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005272 if (len < 2) {
5273 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005274 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005275 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005276 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005277 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005278 cls = find_class(self, module_name, class_name);
5279 Py_DECREF(class_name);
5280 }
5281 }
5282 Py_DECREF(module_name);
5283
5284 if (cls == NULL)
5285 return -1;
5286
5287 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5288 obj = instantiate(cls, args);
5289 Py_DECREF(args);
5290 }
5291 Py_DECREF(cls);
5292
5293 if (obj == NULL)
5294 return -1;
5295
5296 PDATA_PUSH(self->stack, obj, -1);
5297 return 0;
5298}
5299
5300static int
5301load_newobj(UnpicklerObject *self)
5302{
5303 PyObject *args = NULL;
5304 PyObject *clsraw = NULL;
5305 PyTypeObject *cls; /* clsraw cast to its true type */
5306 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005307 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005308
5309 /* Stack is ... cls argtuple, and we want to call
5310 * cls.__new__(cls, *argtuple).
5311 */
5312 PDATA_POP(self->stack, args);
5313 if (args == NULL)
5314 goto error;
5315 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005316 PyErr_SetString(st->UnpicklingError,
5317 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005318 goto error;
5319 }
5320
5321 PDATA_POP(self->stack, clsraw);
5322 cls = (PyTypeObject *)clsraw;
5323 if (cls == NULL)
5324 goto error;
5325 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005326 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005327 "isn't a type object");
5328 goto error;
5329 }
5330 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005331 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005332 "has NULL tp_new");
5333 goto error;
5334 }
5335
5336 /* Call __new__. */
5337 obj = cls->tp_new(cls, args, NULL);
5338 if (obj == NULL)
5339 goto error;
5340
5341 Py_DECREF(args);
5342 Py_DECREF(clsraw);
5343 PDATA_PUSH(self->stack, obj, -1);
5344 return 0;
5345
5346 error:
5347 Py_XDECREF(args);
5348 Py_XDECREF(clsraw);
5349 return -1;
5350}
5351
5352static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005353load_newobj_ex(UnpicklerObject *self)
5354{
5355 PyObject *cls, *args, *kwargs;
5356 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005357 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005358
5359 PDATA_POP(self->stack, kwargs);
5360 if (kwargs == NULL) {
5361 return -1;
5362 }
5363 PDATA_POP(self->stack, args);
5364 if (args == NULL) {
5365 Py_DECREF(kwargs);
5366 return -1;
5367 }
5368 PDATA_POP(self->stack, cls);
5369 if (cls == NULL) {
5370 Py_DECREF(kwargs);
5371 Py_DECREF(args);
5372 return -1;
5373 }
Larry Hastings61272b72014-01-07 12:41:53 -08005374
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005375 if (!PyType_Check(cls)) {
5376 Py_DECREF(kwargs);
5377 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005378 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005379 "NEWOBJ_EX class argument must be a type, not %.200s",
5380 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005381 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005382 return -1;
5383 }
5384
5385 if (((PyTypeObject *)cls)->tp_new == NULL) {
5386 Py_DECREF(kwargs);
5387 Py_DECREF(args);
5388 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005389 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005390 "NEWOBJ_EX class argument doesn't have __new__");
5391 return -1;
5392 }
5393 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5394 Py_DECREF(kwargs);
5395 Py_DECREF(args);
5396 Py_DECREF(cls);
5397 if (obj == NULL) {
5398 return -1;
5399 }
5400 PDATA_PUSH(self->stack, obj, -1);
5401 return 0;
5402}
5403
5404static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005405load_global(UnpicklerObject *self)
5406{
5407 PyObject *global = NULL;
5408 PyObject *module_name;
5409 PyObject *global_name;
5410 Py_ssize_t len;
5411 char *s;
5412
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005413 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005414 return -1;
5415 if (len < 2)
5416 return bad_readline();
5417 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5418 if (!module_name)
5419 return -1;
5420
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005421 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005422 if (len < 2) {
5423 Py_DECREF(module_name);
5424 return bad_readline();
5425 }
5426 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5427 if (global_name) {
5428 global = find_class(self, module_name, global_name);
5429 Py_DECREF(global_name);
5430 }
5431 }
5432 Py_DECREF(module_name);
5433
5434 if (global == NULL)
5435 return -1;
5436 PDATA_PUSH(self->stack, global, -1);
5437 return 0;
5438}
5439
5440static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005441load_stack_global(UnpicklerObject *self)
5442{
5443 PyObject *global;
5444 PyObject *module_name;
5445 PyObject *global_name;
5446
5447 PDATA_POP(self->stack, global_name);
5448 PDATA_POP(self->stack, module_name);
5449 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5450 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005451 PickleState *st = _Pickle_GetGlobalState();
5452 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005453 Py_XDECREF(global_name);
5454 Py_XDECREF(module_name);
5455 return -1;
5456 }
5457 global = find_class(self, module_name, global_name);
5458 Py_DECREF(global_name);
5459 Py_DECREF(module_name);
5460 if (global == NULL)
5461 return -1;
5462 PDATA_PUSH(self->stack, global, -1);
5463 return 0;
5464}
5465
5466static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005467load_persid(UnpicklerObject *self)
5468{
5469 PyObject *pid;
5470 Py_ssize_t len;
5471 char *s;
5472
5473 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005474 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005475 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005476 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005477 return bad_readline();
5478
5479 pid = PyBytes_FromStringAndSize(s, len - 1);
5480 if (pid == NULL)
5481 return -1;
5482
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005483 /* This does not leak since _Pickle_FastCall() steals the reference
5484 to pid first. */
5485 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005486 if (pid == NULL)
5487 return -1;
5488
5489 PDATA_PUSH(self->stack, pid, -1);
5490 return 0;
5491 }
5492 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005493 PickleState *st = _Pickle_GetGlobalState();
5494 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005495 "A load persistent id instruction was encountered,\n"
5496 "but no persistent_load function was specified.");
5497 return -1;
5498 }
5499}
5500
5501static int
5502load_binpersid(UnpicklerObject *self)
5503{
5504 PyObject *pid;
5505
5506 if (self->pers_func) {
5507 PDATA_POP(self->stack, pid);
5508 if (pid == NULL)
5509 return -1;
5510
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005511 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005512 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005513 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005514 if (pid == NULL)
5515 return -1;
5516
5517 PDATA_PUSH(self->stack, pid, -1);
5518 return 0;
5519 }
5520 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005521 PickleState *st = _Pickle_GetGlobalState();
5522 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005523 "A load persistent id instruction was encountered,\n"
5524 "but no persistent_load function was specified.");
5525 return -1;
5526 }
5527}
5528
5529static int
5530load_pop(UnpicklerObject *self)
5531{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005532 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005533
5534 /* Note that we split the (pickle.py) stack into two stacks,
5535 * an object stack and a mark stack. We have to be clever and
5536 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005537 * mark stack first, and only signalling a stack underflow if
5538 * the object stack is empty and the mark stack doesn't match
5539 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005540 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005541 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005542 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005543 self->stack->mark_set = self->num_marks != 0;
5544 self->stack->fence = self->num_marks ?
5545 self->marks[self->num_marks - 1] : 0;
5546 } else if (len <= self->stack->fence)
5547 return Pdata_stack_underflow(self->stack);
5548 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005549 len--;
5550 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005551 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005552 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005553 return 0;
5554}
5555
5556static int
5557load_pop_mark(UnpicklerObject *self)
5558{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005559 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005560
5561 if ((i = marker(self)) < 0)
5562 return -1;
5563
5564 Pdata_clear(self->stack, i);
5565
5566 return 0;
5567}
5568
5569static int
5570load_dup(UnpicklerObject *self)
5571{
5572 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005573 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005574
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005575 if (len <= self->stack->fence)
5576 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005577 last = self->stack->data[len - 1];
5578 PDATA_APPEND(self->stack, last, -1);
5579 return 0;
5580}
5581
5582static int
5583load_get(UnpicklerObject *self)
5584{
5585 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005586 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005587 Py_ssize_t len;
5588 char *s;
5589
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005590 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005591 return -1;
5592 if (len < 2)
5593 return bad_readline();
5594
5595 key = PyLong_FromString(s, NULL, 10);
5596 if (key == NULL)
5597 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005598 idx = PyLong_AsSsize_t(key);
5599 if (idx == -1 && PyErr_Occurred()) {
5600 Py_DECREF(key);
5601 return -1;
5602 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005603
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005604 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005605 if (value == NULL) {
5606 if (!PyErr_Occurred())
5607 PyErr_SetObject(PyExc_KeyError, key);
5608 Py_DECREF(key);
5609 return -1;
5610 }
5611 Py_DECREF(key);
5612
5613 PDATA_APPEND(self->stack, value, -1);
5614 return 0;
5615}
5616
5617static int
5618load_binget(UnpicklerObject *self)
5619{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005620 PyObject *value;
5621 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005622 char *s;
5623
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005624 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005625 return -1;
5626
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005627 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005628
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005629 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005631 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005632 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005633 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005634 Py_DECREF(key);
5635 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005636 return -1;
5637 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005638
5639 PDATA_APPEND(self->stack, value, -1);
5640 return 0;
5641}
5642
5643static int
5644load_long_binget(UnpicklerObject *self)
5645{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005646 PyObject *value;
5647 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005648 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005649
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005650 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005651 return -1;
5652
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005653 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005654
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005655 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005656 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005657 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005658 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005659 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005660 Py_DECREF(key);
5661 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005662 return -1;
5663 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005664
5665 PDATA_APPEND(self->stack, value, -1);
5666 return 0;
5667}
5668
5669/* Push an object from the extension registry (EXT[124]). nbytes is
5670 * the number of bytes following the opcode, holding the index (code) value.
5671 */
5672static int
5673load_extension(UnpicklerObject *self, int nbytes)
5674{
5675 char *codebytes; /* the nbytes bytes after the opcode */
5676 long code; /* calc_binint returns long */
5677 PyObject *py_code; /* code as a Python int */
5678 PyObject *obj; /* the object to push */
5679 PyObject *pair; /* (module_name, class_name) */
5680 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005681 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005682
5683 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005684 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005685 return -1;
5686 code = calc_binint(codebytes, nbytes);
5687 if (code <= 0) { /* note that 0 is forbidden */
5688 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005689 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690 return -1;
5691 }
5692
5693 /* Look for the code in the cache. */
5694 py_code = PyLong_FromLong(code);
5695 if (py_code == NULL)
5696 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005697 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005698 if (obj != NULL) {
5699 /* Bingo. */
5700 Py_DECREF(py_code);
5701 PDATA_APPEND(self->stack, obj, -1);
5702 return 0;
5703 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005704 if (PyErr_Occurred()) {
5705 Py_DECREF(py_code);
5706 return -1;
5707 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005708
5709 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005710 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005711 if (pair == NULL) {
5712 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005713 if (!PyErr_Occurred()) {
5714 PyErr_Format(PyExc_ValueError, "unregistered extension "
5715 "code %ld", code);
5716 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005717 return -1;
5718 }
5719 /* Since the extension registry is manipulable via Python code,
5720 * confirm that pair is really a 2-tuple of strings.
5721 */
5722 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5723 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5724 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5725 Py_DECREF(py_code);
5726 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5727 "isn't a 2-tuple of strings", code);
5728 return -1;
5729 }
5730 /* Load the object. */
5731 obj = find_class(self, module_name, class_name);
5732 if (obj == NULL) {
5733 Py_DECREF(py_code);
5734 return -1;
5735 }
5736 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005737 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005738 Py_DECREF(py_code);
5739 if (code < 0) {
5740 Py_DECREF(obj);
5741 return -1;
5742 }
5743 PDATA_PUSH(self->stack, obj, -1);
5744 return 0;
5745}
5746
5747static int
5748load_put(UnpicklerObject *self)
5749{
5750 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005751 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005752 Py_ssize_t len;
5753 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005754
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005755 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005756 return -1;
5757 if (len < 2)
5758 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005759 if (Py_SIZE(self->stack) <= self->stack->fence)
5760 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005761 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005762
5763 key = PyLong_FromString(s, NULL, 10);
5764 if (key == NULL)
5765 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005766 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005767 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005768 if (idx < 0) {
5769 if (!PyErr_Occurred())
5770 PyErr_SetString(PyExc_ValueError,
5771 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005772 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005773 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005774
5775 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005776}
5777
5778static int
5779load_binput(UnpicklerObject *self)
5780{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005781 PyObject *value;
5782 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005783 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005784
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005785 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005786 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005787
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005788 if (Py_SIZE(self->stack) <= self->stack->fence)
5789 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005790 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005791
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005792 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005793
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005794 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005795}
5796
5797static int
5798load_long_binput(UnpicklerObject *self)
5799{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005800 PyObject *value;
5801 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005802 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005803
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005804 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005805 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005806
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005807 if (Py_SIZE(self->stack) <= self->stack->fence)
5808 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005809 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005810
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005811 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005812 if (idx < 0) {
5813 PyErr_SetString(PyExc_ValueError,
5814 "negative LONG_BINPUT argument");
5815 return -1;
5816 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005817
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005818 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005819}
5820
5821static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005822load_memoize(UnpicklerObject *self)
5823{
5824 PyObject *value;
5825
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005826 if (Py_SIZE(self->stack) <= self->stack->fence)
5827 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005828 value = self->stack->data[Py_SIZE(self->stack) - 1];
5829
5830 return _Unpickler_MemoPut(self, self->memo_len, value);
5831}
5832
5833static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005834do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005835{
5836 PyObject *value;
5837 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005838 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005839
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005840 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005841 if (x > len || x <= self->stack->fence)
5842 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005843 if (len == x) /* nothing to do */
5844 return 0;
5845
5846 list = self->stack->data[x - 1];
5847
5848 if (PyList_Check(list)) {
5849 PyObject *slice;
5850 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005851 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005852
5853 slice = Pdata_poplist(self->stack, x);
5854 if (!slice)
5855 return -1;
5856 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005857 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005858 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005859 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005860 }
5861 else {
5862 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005863 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005864
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005865 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005866 if (append_func == NULL)
5867 return -1;
5868 for (i = x; i < len; i++) {
5869 PyObject *result;
5870
5871 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005872 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005873 if (result == NULL) {
5874 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005875 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005876 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005877 return -1;
5878 }
5879 Py_DECREF(result);
5880 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005881 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005882 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005883 }
5884
5885 return 0;
5886}
5887
5888static int
5889load_append(UnpicklerObject *self)
5890{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005891 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
5892 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005893 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005894}
5895
5896static int
5897load_appends(UnpicklerObject *self)
5898{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005899 Py_ssize_t i = marker(self);
5900 if (i < 0)
5901 return -1;
5902 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005903}
5904
5905static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005906do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005907{
5908 PyObject *value, *key;
5909 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005910 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005911 int status = 0;
5912
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005913 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005914 if (x > len || x <= self->stack->fence)
5915 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005916 if (len == x) /* nothing to do */
5917 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005918 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005919 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005920 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005921 PyErr_SetString(st->UnpicklingError,
5922 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005923 return -1;
5924 }
5925
5926 /* Here, dict does not actually need to be a PyDict; it could be anything
5927 that supports the __setitem__ attribute. */
5928 dict = self->stack->data[x - 1];
5929
5930 for (i = x + 1; i < len; i += 2) {
5931 key = self->stack->data[i - 1];
5932 value = self->stack->data[i];
5933 if (PyObject_SetItem(dict, key, value) < 0) {
5934 status = -1;
5935 break;
5936 }
5937 }
5938
5939 Pdata_clear(self->stack, x);
5940 return status;
5941}
5942
5943static int
5944load_setitem(UnpicklerObject *self)
5945{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005946 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005947}
5948
5949static int
5950load_setitems(UnpicklerObject *self)
5951{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005952 Py_ssize_t i = marker(self);
5953 if (i < 0)
5954 return -1;
5955 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005956}
5957
5958static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005959load_additems(UnpicklerObject *self)
5960{
5961 PyObject *set;
5962 Py_ssize_t mark, len, i;
5963
5964 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005965 if (mark < 0)
5966 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005967 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005968 if (mark > len || mark <= self->stack->fence)
5969 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005970 if (len == mark) /* nothing to do */
5971 return 0;
5972
5973 set = self->stack->data[mark - 1];
5974
5975 if (PySet_Check(set)) {
5976 PyObject *items;
5977 int status;
5978
5979 items = Pdata_poptuple(self->stack, mark);
5980 if (items == NULL)
5981 return -1;
5982
5983 status = _PySet_Update(set, items);
5984 Py_DECREF(items);
5985 return status;
5986 }
5987 else {
5988 PyObject *add_func;
5989 _Py_IDENTIFIER(add);
5990
5991 add_func = _PyObject_GetAttrId(set, &PyId_add);
5992 if (add_func == NULL)
5993 return -1;
5994 for (i = mark; i < len; i++) {
5995 PyObject *result;
5996 PyObject *item;
5997
5998 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005999 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006000 if (result == NULL) {
6001 Pdata_clear(self->stack, i + 1);
6002 Py_SIZE(self->stack) = mark;
6003 return -1;
6004 }
6005 Py_DECREF(result);
6006 }
6007 Py_SIZE(self->stack) = mark;
6008 }
6009
6010 return 0;
6011}
6012
6013static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006014load_build(UnpicklerObject *self)
6015{
6016 PyObject *state, *inst, *slotstate;
6017 PyObject *setstate;
6018 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006019 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006020
6021 /* Stack is ... instance, state. We want to leave instance at
6022 * the stack top, possibly mutated via instance.__setstate__(state).
6023 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006024 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6025 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006026
6027 PDATA_POP(self->stack, state);
6028 if (state == NULL)
6029 return -1;
6030
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006031 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006032
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006033 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006034 if (setstate == NULL) {
6035 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6036 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006037 else {
6038 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006039 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006040 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006041 }
6042 else {
6043 PyObject *result;
6044
6045 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006046 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006047 Py_DECREF(setstate);
6048 if (result == NULL)
6049 return -1;
6050 Py_DECREF(result);
6051 return 0;
6052 }
6053
6054 /* A default __setstate__. First see whether state embeds a
6055 * slot state dict too (a proto 2 addition).
6056 */
6057 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
6058 PyObject *tmp = state;
6059
6060 state = PyTuple_GET_ITEM(tmp, 0);
6061 slotstate = PyTuple_GET_ITEM(tmp, 1);
6062 Py_INCREF(state);
6063 Py_INCREF(slotstate);
6064 Py_DECREF(tmp);
6065 }
6066 else
6067 slotstate = NULL;
6068
6069 /* Set inst.__dict__ from the state dict (if any). */
6070 if (state != Py_None) {
6071 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006072 PyObject *d_key, *d_value;
6073 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006074 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006075
6076 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006077 PickleState *st = _Pickle_GetGlobalState();
6078 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006079 goto error;
6080 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006081 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006082 if (dict == NULL)
6083 goto error;
6084
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006085 i = 0;
6086 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6087 /* normally the keys for instance attributes are
6088 interned. we should try to do that here. */
6089 Py_INCREF(d_key);
6090 if (PyUnicode_CheckExact(d_key))
6091 PyUnicode_InternInPlace(&d_key);
6092 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6093 Py_DECREF(d_key);
6094 goto error;
6095 }
6096 Py_DECREF(d_key);
6097 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006098 Py_DECREF(dict);
6099 }
6100
6101 /* Also set instance attributes from the slotstate dict (if any). */
6102 if (slotstate != NULL) {
6103 PyObject *d_key, *d_value;
6104 Py_ssize_t i;
6105
6106 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006107 PickleState *st = _Pickle_GetGlobalState();
6108 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006109 "slot state is not a dictionary");
6110 goto error;
6111 }
6112 i = 0;
6113 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6114 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6115 goto error;
6116 }
6117 }
6118
6119 if (0) {
6120 error:
6121 status = -1;
6122 }
6123
6124 Py_DECREF(state);
6125 Py_XDECREF(slotstate);
6126 return status;
6127}
6128
6129static int
6130load_mark(UnpicklerObject *self)
6131{
6132
6133 /* Note that we split the (pickle.py) stack into two stacks, an
6134 * object stack and a mark stack. Here we push a mark onto the
6135 * mark stack.
6136 */
6137
6138 if ((self->num_marks + 1) >= self->marks_size) {
6139 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006140
6141 /* Use the size_t type to check for overflow. */
6142 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006143 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006144 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006145 PyErr_NoMemory();
6146 return -1;
6147 }
6148
6149 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006150 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006151 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006152 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6153 if (self->marks == NULL) {
6154 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006155 PyErr_NoMemory();
6156 return -1;
6157 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006158 self->marks_size = (Py_ssize_t)alloc;
6159 }
6160
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006161 self->stack->mark_set = 1;
6162 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006163
6164 return 0;
6165}
6166
6167static int
6168load_reduce(UnpicklerObject *self)
6169{
6170 PyObject *callable = NULL;
6171 PyObject *argtup = NULL;
6172 PyObject *obj = NULL;
6173
6174 PDATA_POP(self->stack, argtup);
6175 if (argtup == NULL)
6176 return -1;
6177 PDATA_POP(self->stack, callable);
6178 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006179 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006180 Py_DECREF(callable);
6181 }
6182 Py_DECREF(argtup);
6183
6184 if (obj == NULL)
6185 return -1;
6186
6187 PDATA_PUSH(self->stack, obj, -1);
6188 return 0;
6189}
6190
6191/* Just raises an error if we don't know the protocol specified. PROTO
6192 * is the first opcode for protocols >= 2.
6193 */
6194static int
6195load_proto(UnpicklerObject *self)
6196{
6197 char *s;
6198 int i;
6199
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006200 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006201 return -1;
6202
6203 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006204 if (i <= HIGHEST_PROTOCOL) {
6205 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006206 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006207 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006208
6209 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6210 return -1;
6211}
6212
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006213static int
6214load_frame(UnpicklerObject *self)
6215{
6216 char *s;
6217 Py_ssize_t frame_len;
6218
6219 if (_Unpickler_Read(self, &s, 8) < 0)
6220 return -1;
6221
6222 frame_len = calc_binsize(s, 8);
6223 if (frame_len < 0) {
6224 PyErr_Format(PyExc_OverflowError,
6225 "FRAME length exceeds system's maximum of %zd bytes",
6226 PY_SSIZE_T_MAX);
6227 return -1;
6228 }
6229
6230 if (_Unpickler_Read(self, &s, frame_len) < 0)
6231 return -1;
6232
6233 /* Rewind to start of frame */
6234 self->next_read_idx -= frame_len;
6235 return 0;
6236}
6237
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006238static PyObject *
6239load(UnpicklerObject *self)
6240{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006241 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006242 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006243
6244 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006245 self->stack->mark_set = 0;
6246 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006247 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006248 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006249 Pdata_clear(self->stack, 0);
6250
6251 /* Convenient macros for the dispatch while-switch loop just below. */
6252#define OP(opcode, load_func) \
6253 case opcode: if (load_func(self) < 0) break; continue;
6254
6255#define OP_ARG(opcode, load_func, arg) \
6256 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6257
6258 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006259 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006260 break;
6261
6262 switch ((enum opcode)s[0]) {
6263 OP(NONE, load_none)
6264 OP(BININT, load_binint)
6265 OP(BININT1, load_binint1)
6266 OP(BININT2, load_binint2)
6267 OP(INT, load_int)
6268 OP(LONG, load_long)
6269 OP_ARG(LONG1, load_counted_long, 1)
6270 OP_ARG(LONG4, load_counted_long, 4)
6271 OP(FLOAT, load_float)
6272 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006273 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6274 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6275 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6276 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6277 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006278 OP(STRING, load_string)
6279 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006280 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6281 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6282 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006283 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6284 OP_ARG(TUPLE1, load_counted_tuple, 1)
6285 OP_ARG(TUPLE2, load_counted_tuple, 2)
6286 OP_ARG(TUPLE3, load_counted_tuple, 3)
6287 OP(TUPLE, load_tuple)
6288 OP(EMPTY_LIST, load_empty_list)
6289 OP(LIST, load_list)
6290 OP(EMPTY_DICT, load_empty_dict)
6291 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006292 OP(EMPTY_SET, load_empty_set)
6293 OP(ADDITEMS, load_additems)
6294 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006295 OP(OBJ, load_obj)
6296 OP(INST, load_inst)
6297 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006298 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006299 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006300 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006301 OP(APPEND, load_append)
6302 OP(APPENDS, load_appends)
6303 OP(BUILD, load_build)
6304 OP(DUP, load_dup)
6305 OP(BINGET, load_binget)
6306 OP(LONG_BINGET, load_long_binget)
6307 OP(GET, load_get)
6308 OP(MARK, load_mark)
6309 OP(BINPUT, load_binput)
6310 OP(LONG_BINPUT, load_long_binput)
6311 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006312 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006313 OP(POP, load_pop)
6314 OP(POP_MARK, load_pop_mark)
6315 OP(SETITEM, load_setitem)
6316 OP(SETITEMS, load_setitems)
6317 OP(PERSID, load_persid)
6318 OP(BINPERSID, load_binpersid)
6319 OP(REDUCE, load_reduce)
6320 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006321 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006322 OP_ARG(EXT1, load_extension, 1)
6323 OP_ARG(EXT2, load_extension, 2)
6324 OP_ARG(EXT4, load_extension, 4)
6325 OP_ARG(NEWTRUE, load_bool, Py_True)
6326 OP_ARG(NEWFALSE, load_bool, Py_False)
6327
6328 case STOP:
6329 break;
6330
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006331 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006332 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006333 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006334 }
6335 else {
6336 PickleState *st = _Pickle_GetGlobalState();
6337 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006338 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006339 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006340 return NULL;
6341 }
6342
6343 break; /* and we are done! */
6344 }
6345
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006346 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006347 return NULL;
6348 }
6349
Victor Stinner2ae57e32013-10-31 13:39:23 +01006350 if (_Unpickler_SkipConsumed(self) < 0)
6351 return NULL;
6352
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006353 PDATA_POP(self->stack, value);
6354 return value;
6355}
6356
Larry Hastings61272b72014-01-07 12:41:53 -08006357/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006358
6359_pickle.Unpickler.load
6360
6361Load a pickle.
6362
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006363Read a pickled object representation from the open file object given
6364in the constructor, and return the reconstituted object hierarchy
6365specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006366[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006367
Larry Hastings3cceb382014-01-04 11:09:09 -08006368static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006369_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006370/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006371{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006372 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006373
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006374 /* Check whether the Unpickler was initialized correctly. This prevents
6375 segfaulting if a subclass overridden __init__ with a function that does
6376 not call Unpickler.__init__(). Here, we simply ensure that self->read
6377 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006378 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006379 PickleState *st = _Pickle_GetGlobalState();
6380 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006381 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006382 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006383 return NULL;
6384 }
6385
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006386 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006387}
6388
6389/* The name of find_class() is misleading. In newer pickle protocols, this
6390 function is used for loading any global (i.e., functions), not just
6391 classes. The name is kept only for backward compatibility. */
6392
Larry Hastings61272b72014-01-07 12:41:53 -08006393/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006394
6395_pickle.Unpickler.find_class
6396
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006397 module_name: object
6398 global_name: object
6399 /
6400
6401Return an object from a specified module.
6402
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006403If necessary, the module will be imported. Subclasses may override
6404this method (e.g. to restrict unpickling of arbitrary classes and
6405functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006406
6407This method is called whenever a class or a function object is
6408needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006409[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006410
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006411static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006412_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6413 PyObject *module_name,
6414 PyObject *global_name)
6415/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006416{
6417 PyObject *global;
6418 PyObject *modules_dict;
6419 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006420 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006421
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006422 /* Try to map the old names used in Python 2.x to the new ones used in
6423 Python 3.x. We do this only with old pickle protocols and when the
6424 user has not disabled the feature. */
6425 if (self->proto < 3 && self->fix_imports) {
6426 PyObject *key;
6427 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006428 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006429
6430 /* Check if the global (i.e., a function or a class) was renamed
6431 or moved to another module. */
6432 key = PyTuple_Pack(2, module_name, global_name);
6433 if (key == NULL)
6434 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006435 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006436 Py_DECREF(key);
6437 if (item) {
6438 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6439 PyErr_Format(PyExc_RuntimeError,
6440 "_compat_pickle.NAME_MAPPING values should be "
6441 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6442 return NULL;
6443 }
6444 module_name = PyTuple_GET_ITEM(item, 0);
6445 global_name = PyTuple_GET_ITEM(item, 1);
6446 if (!PyUnicode_Check(module_name) ||
6447 !PyUnicode_Check(global_name)) {
6448 PyErr_Format(PyExc_RuntimeError,
6449 "_compat_pickle.NAME_MAPPING values should be "
6450 "pairs of str, not (%.200s, %.200s)",
6451 Py_TYPE(module_name)->tp_name,
6452 Py_TYPE(global_name)->tp_name);
6453 return NULL;
6454 }
6455 }
6456 else if (PyErr_Occurred()) {
6457 return NULL;
6458 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006459 else {
6460 /* Check if the module was renamed. */
6461 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6462 if (item) {
6463 if (!PyUnicode_Check(item)) {
6464 PyErr_Format(PyExc_RuntimeError,
6465 "_compat_pickle.IMPORT_MAPPING values should be "
6466 "strings, not %.200s", Py_TYPE(item)->tp_name);
6467 return NULL;
6468 }
6469 module_name = item;
6470 }
6471 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006472 return NULL;
6473 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006474 }
6475 }
6476
Victor Stinnerbb520202013-11-06 22:40:41 +01006477 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006478 if (modules_dict == NULL) {
6479 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006480 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006481 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006482
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006483 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006484 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006485 if (PyErr_Occurred())
6486 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006487 module = PyImport_Import(module_name);
6488 if (module == NULL)
6489 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006490 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006491 Py_DECREF(module);
6492 }
Victor Stinner121aab42011-09-29 23:40:53 +02006493 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006494 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006495 }
6496 return global;
6497}
6498
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006499/*[clinic input]
6500
6501_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6502
6503Returns size in memory, in bytes.
6504[clinic start generated code]*/
6505
6506static Py_ssize_t
6507_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6508/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6509{
6510 Py_ssize_t res;
6511
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006512 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006513 if (self->memo != NULL)
6514 res += self->memo_size * sizeof(PyObject *);
6515 if (self->marks != NULL)
6516 res += self->marks_size * sizeof(Py_ssize_t);
6517 if (self->input_line != NULL)
6518 res += strlen(self->input_line) + 1;
6519 if (self->encoding != NULL)
6520 res += strlen(self->encoding) + 1;
6521 if (self->errors != NULL)
6522 res += strlen(self->errors) + 1;
6523 return res;
6524}
6525
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006526static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006527 _PICKLE_UNPICKLER_LOAD_METHODDEF
6528 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006529 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006530 {NULL, NULL} /* sentinel */
6531};
6532
6533static void
6534Unpickler_dealloc(UnpicklerObject *self)
6535{
6536 PyObject_GC_UnTrack((PyObject *)self);
6537 Py_XDECREF(self->readline);
6538 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006539 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006540 Py_XDECREF(self->stack);
6541 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006542 if (self->buffer.buf != NULL) {
6543 PyBuffer_Release(&self->buffer);
6544 self->buffer.buf = NULL;
6545 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006546
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006547 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006548 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006549 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006550 PyMem_Free(self->encoding);
6551 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006552
6553 Py_TYPE(self)->tp_free((PyObject *)self);
6554}
6555
6556static int
6557Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6558{
6559 Py_VISIT(self->readline);
6560 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006561 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006562 Py_VISIT(self->stack);
6563 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006564 return 0;
6565}
6566
6567static int
6568Unpickler_clear(UnpicklerObject *self)
6569{
6570 Py_CLEAR(self->readline);
6571 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006572 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006573 Py_CLEAR(self->stack);
6574 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006575 if (self->buffer.buf != NULL) {
6576 PyBuffer_Release(&self->buffer);
6577 self->buffer.buf = NULL;
6578 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006579
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006580 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006581 PyMem_Free(self->marks);
6582 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006583 PyMem_Free(self->input_line);
6584 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006585 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006586 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006587 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006588 self->errors = NULL;
6589
6590 return 0;
6591}
6592
Larry Hastings61272b72014-01-07 12:41:53 -08006593/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006594
6595_pickle.Unpickler.__init__
6596
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006597 file: object
6598 *
6599 fix_imports: bool = True
6600 encoding: str = 'ASCII'
6601 errors: str = 'strict'
6602
6603This takes a binary file for reading a pickle data stream.
6604
6605The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006606protocol argument is needed. Bytes past the pickled object's
6607representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006608
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006609The argument *file* must have two methods, a read() method that takes
6610an integer argument, and a readline() method that requires no
6611arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006612binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006613other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006614
6615Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6616which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006617generated by Python 2. If *fix_imports* is True, pickle will try to
6618map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006619*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006620instances pickled by Python 2; these default to 'ASCII' and 'strict',
6621respectively. The *encoding* can be 'bytes' to read these 8-bit
6622string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006623[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006624
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006625static int
Larry Hastings89964c42015-04-14 18:07:59 -04006626_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6627 int fix_imports, const char *encoding,
6628 const char *errors)
Martin Panter2eb819f2015-11-02 04:04:57 +00006629/*[clinic end generated code: output=e2c8ce748edc57b0 input=04ece661aa884837]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006630{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006631 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006632
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006633 /* In case of multiple __init__() calls, clear previous content. */
6634 if (self->read != NULL)
6635 (void)Unpickler_clear(self);
6636
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006637 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006638 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006639
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006640 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006641 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006642
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006643 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006644 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006645 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006646
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006647 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006648 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6649 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006650 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006651 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006652 }
6653 else {
6654 self->pers_func = NULL;
6655 }
6656
6657 self->stack = (Pdata *)Pdata_New();
6658 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006659 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006660
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006661 self->memo_size = 32;
6662 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006663 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006664 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006665
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006666 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006667
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006668 return 0;
6669}
6670
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006671
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006672/* Define a proxy object for the Unpickler's internal memo object. This is to
6673 * avoid breaking code like:
6674 * unpickler.memo.clear()
6675 * and
6676 * unpickler.memo = saved_memo
6677 * Is this a good idea? Not really, but we don't want to break code that uses
6678 * it. Note that we don't implement the entire mapping API here. This is
6679 * intentional, as these should be treated as black-box implementation details.
6680 *
6681 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006682 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006683 */
6684
Larry Hastings61272b72014-01-07 12:41:53 -08006685/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006686_pickle.UnpicklerMemoProxy.clear
6687
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006688Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006689[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006690
Larry Hastings3cceb382014-01-04 11:09:09 -08006691static PyObject *
6692_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006693/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006694{
6695 _Unpickler_MemoCleanup(self->unpickler);
6696 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6697 if (self->unpickler->memo == NULL)
6698 return NULL;
6699 Py_RETURN_NONE;
6700}
6701
Larry Hastings61272b72014-01-07 12:41:53 -08006702/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006703_pickle.UnpicklerMemoProxy.copy
6704
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006705Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006706[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006707
Larry Hastings3cceb382014-01-04 11:09:09 -08006708static PyObject *
6709_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006710/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006711{
6712 Py_ssize_t i;
6713 PyObject *new_memo = PyDict_New();
6714 if (new_memo == NULL)
6715 return NULL;
6716
6717 for (i = 0; i < self->unpickler->memo_size; i++) {
6718 int status;
6719 PyObject *key, *value;
6720
6721 value = self->unpickler->memo[i];
6722 if (value == NULL)
6723 continue;
6724
6725 key = PyLong_FromSsize_t(i);
6726 if (key == NULL)
6727 goto error;
6728 status = PyDict_SetItem(new_memo, key, value);
6729 Py_DECREF(key);
6730 if (status < 0)
6731 goto error;
6732 }
6733 return new_memo;
6734
6735error:
6736 Py_DECREF(new_memo);
6737 return NULL;
6738}
6739
Larry Hastings61272b72014-01-07 12:41:53 -08006740/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006741_pickle.UnpicklerMemoProxy.__reduce__
6742
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006743Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006744[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006745
Larry Hastings3cceb382014-01-04 11:09:09 -08006746static PyObject *
6747_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006748/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006749{
6750 PyObject *reduce_value;
6751 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006752 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006753 if (contents == NULL)
6754 return NULL;
6755
6756 reduce_value = PyTuple_New(2);
6757 if (reduce_value == NULL) {
6758 Py_DECREF(contents);
6759 return NULL;
6760 }
6761 constructor_args = PyTuple_New(1);
6762 if (constructor_args == NULL) {
6763 Py_DECREF(contents);
6764 Py_DECREF(reduce_value);
6765 return NULL;
6766 }
6767 PyTuple_SET_ITEM(constructor_args, 0, contents);
6768 Py_INCREF((PyObject *)&PyDict_Type);
6769 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6770 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6771 return reduce_value;
6772}
6773
6774static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006775 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6776 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6777 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006778 {NULL, NULL} /* sentinel */
6779};
6780
6781static void
6782UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6783{
6784 PyObject_GC_UnTrack(self);
6785 Py_XDECREF(self->unpickler);
6786 PyObject_GC_Del((PyObject *)self);
6787}
6788
6789static int
6790UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6791 visitproc visit, void *arg)
6792{
6793 Py_VISIT(self->unpickler);
6794 return 0;
6795}
6796
6797static int
6798UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6799{
6800 Py_CLEAR(self->unpickler);
6801 return 0;
6802}
6803
6804static PyTypeObject UnpicklerMemoProxyType = {
6805 PyVarObject_HEAD_INIT(NULL, 0)
6806 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6807 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6808 0,
6809 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6810 0, /* tp_print */
6811 0, /* tp_getattr */
6812 0, /* tp_setattr */
6813 0, /* tp_compare */
6814 0, /* tp_repr */
6815 0, /* tp_as_number */
6816 0, /* tp_as_sequence */
6817 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006818 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006819 0, /* tp_call */
6820 0, /* tp_str */
6821 PyObject_GenericGetAttr, /* tp_getattro */
6822 PyObject_GenericSetAttr, /* tp_setattro */
6823 0, /* tp_as_buffer */
6824 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6825 0, /* tp_doc */
6826 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6827 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6828 0, /* tp_richcompare */
6829 0, /* tp_weaklistoffset */
6830 0, /* tp_iter */
6831 0, /* tp_iternext */
6832 unpicklerproxy_methods, /* tp_methods */
6833};
6834
6835static PyObject *
6836UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6837{
6838 UnpicklerMemoProxyObject *self;
6839
6840 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6841 &UnpicklerMemoProxyType);
6842 if (self == NULL)
6843 return NULL;
6844 Py_INCREF(unpickler);
6845 self->unpickler = unpickler;
6846 PyObject_GC_Track(self);
6847 return (PyObject *)self;
6848}
6849
6850/*****************************************************************************/
6851
6852
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006853static PyObject *
6854Unpickler_get_memo(UnpicklerObject *self)
6855{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006856 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006857}
6858
6859static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006860Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006861{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006862 PyObject **new_memo;
6863 Py_ssize_t new_memo_size = 0;
6864 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006865
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006866 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006867 PyErr_SetString(PyExc_TypeError,
6868 "attribute deletion is not supported");
6869 return -1;
6870 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006871
6872 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6873 UnpicklerObject *unpickler =
6874 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6875
6876 new_memo_size = unpickler->memo_size;
6877 new_memo = _Unpickler_NewMemo(new_memo_size);
6878 if (new_memo == NULL)
6879 return -1;
6880
6881 for (i = 0; i < new_memo_size; i++) {
6882 Py_XINCREF(unpickler->memo[i]);
6883 new_memo[i] = unpickler->memo[i];
6884 }
6885 }
6886 else if (PyDict_Check(obj)) {
6887 Py_ssize_t i = 0;
6888 PyObject *key, *value;
6889
6890 new_memo_size = PyDict_Size(obj);
6891 new_memo = _Unpickler_NewMemo(new_memo_size);
6892 if (new_memo == NULL)
6893 return -1;
6894
6895 while (PyDict_Next(obj, &i, &key, &value)) {
6896 Py_ssize_t idx;
6897 if (!PyLong_Check(key)) {
6898 PyErr_SetString(PyExc_TypeError,
6899 "memo key must be integers");
6900 goto error;
6901 }
6902 idx = PyLong_AsSsize_t(key);
6903 if (idx == -1 && PyErr_Occurred())
6904 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006905 if (idx < 0) {
6906 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006907 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006908 goto error;
6909 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006910 if (_Unpickler_MemoPut(self, idx, value) < 0)
6911 goto error;
6912 }
6913 }
6914 else {
6915 PyErr_Format(PyExc_TypeError,
6916 "'memo' attribute must be an UnpicklerMemoProxy object"
6917 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006918 return -1;
6919 }
6920
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006921 _Unpickler_MemoCleanup(self);
6922 self->memo_size = new_memo_size;
6923 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006924
6925 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006926
6927 error:
6928 if (new_memo_size) {
6929 i = new_memo_size;
6930 while (--i >= 0) {
6931 Py_XDECREF(new_memo[i]);
6932 }
6933 PyMem_FREE(new_memo);
6934 }
6935 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006936}
6937
6938static PyObject *
6939Unpickler_get_persload(UnpicklerObject *self)
6940{
6941 if (self->pers_func == NULL)
6942 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6943 else
6944 Py_INCREF(self->pers_func);
6945 return self->pers_func;
6946}
6947
6948static int
6949Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6950{
6951 PyObject *tmp;
6952
6953 if (value == NULL) {
6954 PyErr_SetString(PyExc_TypeError,
6955 "attribute deletion is not supported");
6956 return -1;
6957 }
6958 if (!PyCallable_Check(value)) {
6959 PyErr_SetString(PyExc_TypeError,
6960 "persistent_load must be a callable taking "
6961 "one argument");
6962 return -1;
6963 }
6964
6965 tmp = self->pers_func;
6966 Py_INCREF(value);
6967 self->pers_func = value;
6968 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6969
6970 return 0;
6971}
6972
6973static PyGetSetDef Unpickler_getsets[] = {
6974 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6975 {"persistent_load", (getter)Unpickler_get_persload,
6976 (setter)Unpickler_set_persload},
6977 {NULL}
6978};
6979
6980static PyTypeObject Unpickler_Type = {
6981 PyVarObject_HEAD_INIT(NULL, 0)
6982 "_pickle.Unpickler", /*tp_name*/
6983 sizeof(UnpicklerObject), /*tp_basicsize*/
6984 0, /*tp_itemsize*/
6985 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6986 0, /*tp_print*/
6987 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006988 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006989 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006990 0, /*tp_repr*/
6991 0, /*tp_as_number*/
6992 0, /*tp_as_sequence*/
6993 0, /*tp_as_mapping*/
6994 0, /*tp_hash*/
6995 0, /*tp_call*/
6996 0, /*tp_str*/
6997 0, /*tp_getattro*/
6998 0, /*tp_setattro*/
6999 0, /*tp_as_buffer*/
7000 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007001 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007002 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7003 (inquiry)Unpickler_clear, /*tp_clear*/
7004 0, /*tp_richcompare*/
7005 0, /*tp_weaklistoffset*/
7006 0, /*tp_iter*/
7007 0, /*tp_iternext*/
7008 Unpickler_methods, /*tp_methods*/
7009 0, /*tp_members*/
7010 Unpickler_getsets, /*tp_getset*/
7011 0, /*tp_base*/
7012 0, /*tp_dict*/
7013 0, /*tp_descr_get*/
7014 0, /*tp_descr_set*/
7015 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007016 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007017 PyType_GenericAlloc, /*tp_alloc*/
7018 PyType_GenericNew, /*tp_new*/
7019 PyObject_GC_Del, /*tp_free*/
7020 0, /*tp_is_gc*/
7021};
7022
Larry Hastings61272b72014-01-07 12:41:53 -08007023/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007024
7025_pickle.dump
7026
7027 obj: object
7028 file: object
7029 protocol: object = NULL
7030 *
7031 fix_imports: bool = True
7032
7033Write a pickled representation of obj to the open file object file.
7034
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007035This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7036be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007037
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007038The optional *protocol* argument tells the pickler to use the given
7039protocol supported protocols are 0, 1, 2, 3 and 4. The default
7040protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007041
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007042Specifying a negative protocol version selects the highest protocol
7043version supported. The higher the protocol used, the more recent the
7044version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007045
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007046The *file* argument must have a write() method that accepts a single
7047bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007048writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007049this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007050
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007051If *fix_imports* is True and protocol is less than 3, pickle will try
7052to map the new Python 3 names to the old module names used in Python
70532, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007054[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007055
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007056static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007057_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file,
7058 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00007059/*[clinic end generated code: output=0de7dff89c406816 input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007060{
7061 PicklerObject *pickler = _Pickler_New();
7062
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007063 if (pickler == NULL)
7064 return NULL;
7065
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007066 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007067 goto error;
7068
7069 if (_Pickler_SetOutputStream(pickler, file) < 0)
7070 goto error;
7071
7072 if (dump(pickler, obj) < 0)
7073 goto error;
7074
7075 if (_Pickler_FlushToFile(pickler) < 0)
7076 goto error;
7077
7078 Py_DECREF(pickler);
7079 Py_RETURN_NONE;
7080
7081 error:
7082 Py_XDECREF(pickler);
7083 return NULL;
7084}
7085
Larry Hastings61272b72014-01-07 12:41:53 -08007086/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007087
7088_pickle.dumps
7089
7090 obj: object
7091 protocol: object = NULL
7092 *
7093 fix_imports: bool = True
7094
7095Return the pickled representation of the object as a bytes object.
7096
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007097The optional *protocol* argument tells the pickler to use the given
7098protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7099protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007100
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007101Specifying a negative protocol version selects the highest protocol
7102version supported. The higher the protocol used, the more recent the
7103version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007104
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007105If *fix_imports* is True and *protocol* is less than 3, pickle will
7106try to map the new Python 3 names to the old module names used in
7107Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007108[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007109
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007110static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007111_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol,
7112 int fix_imports)
7113/*[clinic end generated code: output=daa380db56fe07b9 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007114{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007115 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007116 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007117
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007118 if (pickler == NULL)
7119 return NULL;
7120
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007121 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007122 goto error;
7123
7124 if (dump(pickler, obj) < 0)
7125 goto error;
7126
7127 result = _Pickler_GetString(pickler);
7128 Py_DECREF(pickler);
7129 return result;
7130
7131 error:
7132 Py_XDECREF(pickler);
7133 return NULL;
7134}
7135
Larry Hastings61272b72014-01-07 12:41:53 -08007136/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007137
7138_pickle.load
7139
7140 file: object
7141 *
7142 fix_imports: bool = True
7143 encoding: str = 'ASCII'
7144 errors: str = 'strict'
7145
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007146Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007147
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007148This is equivalent to ``Unpickler(file).load()``, but may be more
7149efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007150
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007151The protocol version of the pickle is detected automatically, so no
7152protocol argument is needed. Bytes past the pickled object's
7153representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007154
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007155The argument *file* must have two methods, a read() method that takes
7156an integer argument, and a readline() method that requires no
7157arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007158binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007159other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007160
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007161Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7162which are used to control compatiblity support for pickle stream
7163generated by Python 2. If *fix_imports* is True, pickle will try to
7164map the old Python 2 names to the new names used in Python 3. The
7165*encoding* and *errors* tell pickle how to decode 8-bit string
7166instances pickled by Python 2; these default to 'ASCII' and 'strict',
7167respectively. The *encoding* can be 'bytes' to read these 8-bit
7168string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007169[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007170
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007171static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007172_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports,
7173 const char *encoding, const char *errors)
Martin Panter2eb819f2015-11-02 04:04:57 +00007174/*[clinic end generated code: output=798f1c57cb2b4eb1 input=2df7c7a1e6742204]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007175{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007176 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007177 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007178
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007179 if (unpickler == NULL)
7180 return NULL;
7181
7182 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7183 goto error;
7184
7185 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7186 goto error;
7187
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007188 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007189
7190 result = load(unpickler);
7191 Py_DECREF(unpickler);
7192 return result;
7193
7194 error:
7195 Py_XDECREF(unpickler);
7196 return NULL;
7197}
7198
Larry Hastings61272b72014-01-07 12:41:53 -08007199/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007200
7201_pickle.loads
7202
7203 data: object
7204 *
7205 fix_imports: bool = True
7206 encoding: str = 'ASCII'
7207 errors: str = 'strict'
7208
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007209Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007210
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007211The protocol version of the pickle is detected automatically, so no
7212protocol argument is needed. Bytes past the pickled object's
7213representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007214
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007215Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7216which are used to control compatiblity support for pickle stream
7217generated by Python 2. If *fix_imports* is True, pickle will try to
7218map the old Python 2 names to the new names used in Python 3. The
7219*encoding* and *errors* tell pickle how to decode 8-bit string
7220instances pickled by Python 2; these default to 'ASCII' and 'strict',
7221respectively. The *encoding* can be 'bytes' to read these 8-bit
7222string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007223[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007224
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007225static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007226_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports,
7227 const char *encoding, const char *errors)
7228/*[clinic end generated code: output=61e9cdb01e36a736 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007229{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007230 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007231 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007232
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007233 if (unpickler == NULL)
7234 return NULL;
7235
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007236 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007237 goto error;
7238
7239 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7240 goto error;
7241
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007242 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007243
7244 result = load(unpickler);
7245 Py_DECREF(unpickler);
7246 return result;
7247
7248 error:
7249 Py_XDECREF(unpickler);
7250 return NULL;
7251}
7252
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007253static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007254 _PICKLE_DUMP_METHODDEF
7255 _PICKLE_DUMPS_METHODDEF
7256 _PICKLE_LOAD_METHODDEF
7257 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007258 {NULL, NULL} /* sentinel */
7259};
7260
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007261static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007262pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007263{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007264 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007265 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007266}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007267
Stefan Krahf483b0f2013-12-14 13:43:10 +01007268static void
7269pickle_free(PyObject *m)
7270{
7271 _Pickle_ClearState(_Pickle_GetState(m));
7272}
7273
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007274static int
7275pickle_traverse(PyObject *m, visitproc visit, void *arg)
7276{
7277 PickleState *st = _Pickle_GetState(m);
7278 Py_VISIT(st->PickleError);
7279 Py_VISIT(st->PicklingError);
7280 Py_VISIT(st->UnpicklingError);
7281 Py_VISIT(st->dispatch_table);
7282 Py_VISIT(st->extension_registry);
7283 Py_VISIT(st->extension_cache);
7284 Py_VISIT(st->inverted_registry);
7285 Py_VISIT(st->name_mapping_2to3);
7286 Py_VISIT(st->import_mapping_2to3);
7287 Py_VISIT(st->name_mapping_3to2);
7288 Py_VISIT(st->import_mapping_3to2);
7289 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007290 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007291 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007292}
7293
7294static struct PyModuleDef _picklemodule = {
7295 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007296 "_pickle", /* m_name */
7297 pickle_module_doc, /* m_doc */
7298 sizeof(PickleState), /* m_size */
7299 pickle_methods, /* m_methods */
7300 NULL, /* m_reload */
7301 pickle_traverse, /* m_traverse */
7302 pickle_clear, /* m_clear */
7303 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007304};
7305
7306PyMODINIT_FUNC
7307PyInit__pickle(void)
7308{
7309 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007310 PickleState *st;
7311
7312 m = PyState_FindModule(&_picklemodule);
7313 if (m) {
7314 Py_INCREF(m);
7315 return m;
7316 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007317
7318 if (PyType_Ready(&Unpickler_Type) < 0)
7319 return NULL;
7320 if (PyType_Ready(&Pickler_Type) < 0)
7321 return NULL;
7322 if (PyType_Ready(&Pdata_Type) < 0)
7323 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007324 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7325 return NULL;
7326 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7327 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007328
7329 /* Create the module and add the functions. */
7330 m = PyModule_Create(&_picklemodule);
7331 if (m == NULL)
7332 return NULL;
7333
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007334 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007335 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7336 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007337 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007338 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7339 return NULL;
7340
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007341 st = _Pickle_GetState(m);
7342
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007343 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007344 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7345 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007346 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007347 st->PicklingError = \
7348 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7349 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007350 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007351 st->UnpicklingError = \
7352 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7353 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007354 return NULL;
7355
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007356 Py_INCREF(st->PickleError);
7357 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007358 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007359 Py_INCREF(st->PicklingError);
7360 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007361 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007362 Py_INCREF(st->UnpicklingError);
7363 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007364 return NULL;
7365
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007366 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007367 return NULL;
7368
7369 return m;
7370}