blob: e687a1eb0e28b94e1e867146771fb8620d3d83ba [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
9class _pickle.Pickler
10class _pickle.PicklerMemoProxy
11class _pickle.Unpickler
12class _pickle.UnpicklerMemoProxy
Larry Hastings61272b72014-01-07 12:41:53 -080013[clinic start generated code]*/
14/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015
Larry Hastings61272b72014-01-07 12:41:53 -080016/*[python input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080017class PicklerObject_converter(self_converter):
18 type = "PicklerObject *"
19
20class PicklerMemoProxyObject_converter(self_converter):
21 type = "PicklerMemoProxyObject *"
22
23class UnpicklerObject_converter(self_converter):
24 type = "UnpicklerObject *"
25
26class UnpicklerMemoProxyObject_converter(self_converter):
27 type = "UnpicklerMemoProxyObject *"
Larry Hastings61272b72014-01-07 12:41:53 -080028[python start generated code]*/
29/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080030
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000031/* Bump this when new opcodes are added to the pickle protocol. */
32enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010033 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000034 DEFAULT_PROTOCOL = 3
35};
36
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000037/* Pickle opcodes. These must be kept updated with pickle.py.
38 Extensive docs are in pickletools.py. */
39enum opcode {
40 MARK = '(',
41 STOP = '.',
42 POP = '0',
43 POP_MARK = '1',
44 DUP = '2',
45 FLOAT = 'F',
46 INT = 'I',
47 BININT = 'J',
48 BININT1 = 'K',
49 LONG = 'L',
50 BININT2 = 'M',
51 NONE = 'N',
52 PERSID = 'P',
53 BINPERSID = 'Q',
54 REDUCE = 'R',
55 STRING = 'S',
56 BINSTRING = 'T',
57 SHORT_BINSTRING = 'U',
58 UNICODE = 'V',
59 BINUNICODE = 'X',
60 APPEND = 'a',
61 BUILD = 'b',
62 GLOBAL = 'c',
63 DICT = 'd',
64 EMPTY_DICT = '}',
65 APPENDS = 'e',
66 GET = 'g',
67 BINGET = 'h',
68 INST = 'i',
69 LONG_BINGET = 'j',
70 LIST = 'l',
71 EMPTY_LIST = ']',
72 OBJ = 'o',
73 PUT = 'p',
74 BINPUT = 'q',
75 LONG_BINPUT = 'r',
76 SETITEM = 's',
77 TUPLE = 't',
78 EMPTY_TUPLE = ')',
79 SETITEMS = 'u',
80 BINFLOAT = 'G',
81
82 /* Protocol 2. */
83 PROTO = '\x80',
84 NEWOBJ = '\x81',
85 EXT1 = '\x82',
86 EXT2 = '\x83',
87 EXT4 = '\x84',
88 TUPLE1 = '\x85',
89 TUPLE2 = '\x86',
90 TUPLE3 = '\x87',
91 NEWTRUE = '\x88',
92 NEWFALSE = '\x89',
93 LONG1 = '\x8a',
94 LONG4 = '\x8b',
95
96 /* Protocol 3 (Python 3.x) */
97 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010098 SHORT_BINBYTES = 'C',
99
100 /* Protocol 4 */
101 SHORT_BINUNICODE = '\x8c',
102 BINUNICODE8 = '\x8d',
103 BINBYTES8 = '\x8e',
104 EMPTY_SET = '\x8f',
105 ADDITEMS = '\x90',
106 FROZENSET = '\x91',
107 NEWOBJ_EX = '\x92',
108 STACK_GLOBAL = '\x93',
109 MEMOIZE = '\x94',
110 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000111};
112
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000113enum {
114 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
115 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
116 break if this gets out of synch with pickle.py, but it's unclear that would
117 help anything either. */
118 BATCHSIZE = 1000,
119
120 /* Nesting limit until Pickler, when running in "fast mode", starts
121 checking for self-referential data-structures. */
122 FAST_NESTING_LIMIT = 50,
123
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000124 /* Initial size of the write buffer of Pickler. */
125 WRITE_BUF_SIZE = 4096,
126
Antoine Pitrou04248a82010-10-12 20:51:21 +0000127 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100128 PREFETCH = 8192 * 16,
129
130 FRAME_SIZE_TARGET = 64 * 1024,
131
132 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000133};
134
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800135/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000136
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800137/* State of the pickle module, per PEP 3121. */
138typedef struct {
139 /* Exception classes for pickle. */
140 PyObject *PickleError;
141 PyObject *PicklingError;
142 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800143
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800144 /* copyreg.dispatch_table, {type_object: pickling_function} */
145 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000146
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800147 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000148
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800149 /* copyreg._extension_registry, {(module_name, function_name): code} */
150 PyObject *extension_registry;
151 /* copyreg._extension_cache, {code: object} */
152 PyObject *extension_cache;
153 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
154 PyObject *inverted_registry;
155
156 /* Import mappings for compatibility with Python 2.x */
157
158 /* _compat_pickle.NAME_MAPPING,
159 {(oldmodule, oldname): (newmodule, newname)} */
160 PyObject *name_mapping_2to3;
161 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
162 PyObject *import_mapping_2to3;
163 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
164 PyObject *name_mapping_3to2;
165 PyObject *import_mapping_3to2;
166
167 /* codecs.encode, used for saving bytes in older protocols */
168 PyObject *codecs_encode;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800169} PickleState;
170
171/* Forward declaration of the _pickle module definition. */
172static struct PyModuleDef _picklemodule;
173
174/* Given a module object, get its per-module state. */
175static PickleState *
176_Pickle_GetState(PyObject *module)
177{
178 return (PickleState *)PyModule_GetState(module);
179}
180
181/* Find the module instance imported in the currently running sub-interpreter
182 and get its state. */
183static PickleState *
184_Pickle_GetGlobalState(void)
185{
186 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
187}
188
189/* Clear the given pickle module state. */
190static void
191_Pickle_ClearState(PickleState *st)
192{
193 Py_CLEAR(st->PickleError);
194 Py_CLEAR(st->PicklingError);
195 Py_CLEAR(st->UnpicklingError);
196 Py_CLEAR(st->dispatch_table);
197 Py_CLEAR(st->extension_registry);
198 Py_CLEAR(st->extension_cache);
199 Py_CLEAR(st->inverted_registry);
200 Py_CLEAR(st->name_mapping_2to3);
201 Py_CLEAR(st->import_mapping_2to3);
202 Py_CLEAR(st->name_mapping_3to2);
203 Py_CLEAR(st->import_mapping_3to2);
204 Py_CLEAR(st->codecs_encode);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800205}
206
207/* Initialize the given pickle module state. */
208static int
209_Pickle_InitState(PickleState *st)
210{
211 PyObject *copyreg = NULL;
212 PyObject *compat_pickle = NULL;
213 PyObject *codecs = NULL;
214
215 copyreg = PyImport_ImportModule("copyreg");
216 if (!copyreg)
217 goto error;
218 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
219 if (!st->dispatch_table)
220 goto error;
221 if (!PyDict_CheckExact(st->dispatch_table)) {
222 PyErr_Format(PyExc_RuntimeError,
223 "copyreg.dispatch_table should be a dict, not %.200s",
224 Py_TYPE(st->dispatch_table)->tp_name);
225 goto error;
226 }
227 st->extension_registry = \
228 PyObject_GetAttrString(copyreg, "_extension_registry");
229 if (!st->extension_registry)
230 goto error;
231 if (!PyDict_CheckExact(st->extension_registry)) {
232 PyErr_Format(PyExc_RuntimeError,
233 "copyreg._extension_registry should be a dict, "
234 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
235 goto error;
236 }
237 st->inverted_registry = \
238 PyObject_GetAttrString(copyreg, "_inverted_registry");
239 if (!st->inverted_registry)
240 goto error;
241 if (!PyDict_CheckExact(st->inverted_registry)) {
242 PyErr_Format(PyExc_RuntimeError,
243 "copyreg._inverted_registry should be a dict, "
244 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
245 goto error;
246 }
247 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
248 if (!st->extension_cache)
249 goto error;
250 if (!PyDict_CheckExact(st->extension_cache)) {
251 PyErr_Format(PyExc_RuntimeError,
252 "copyreg._extension_cache should be a dict, "
253 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
254 goto error;
255 }
256 Py_CLEAR(copyreg);
257
258 /* Load the 2.x -> 3.x stdlib module mapping tables */
259 compat_pickle = PyImport_ImportModule("_compat_pickle");
260 if (!compat_pickle)
261 goto error;
262 st->name_mapping_2to3 = \
263 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
264 if (!st->name_mapping_2to3)
265 goto error;
266 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
267 PyErr_Format(PyExc_RuntimeError,
268 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
269 Py_TYPE(st->name_mapping_2to3)->tp_name);
270 goto error;
271 }
272 st->import_mapping_2to3 = \
273 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
274 if (!st->import_mapping_2to3)
275 goto error;
276 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
277 PyErr_Format(PyExc_RuntimeError,
278 "_compat_pickle.IMPORT_MAPPING should be a dict, "
279 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
280 goto error;
281 }
282 /* ... and the 3.x -> 2.x mapping tables */
283 st->name_mapping_3to2 = \
284 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
285 if (!st->name_mapping_3to2)
286 goto error;
287 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
288 PyErr_Format(PyExc_RuntimeError,
289 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
290 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
291 goto error;
292 }
293 st->import_mapping_3to2 = \
294 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
295 if (!st->import_mapping_3to2)
296 goto error;
297 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
298 PyErr_Format(PyExc_RuntimeError,
299 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
300 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
301 goto error;
302 }
303 Py_CLEAR(compat_pickle);
304
305 codecs = PyImport_ImportModule("codecs");
306 if (codecs == NULL)
307 goto error;
308 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
309 if (st->codecs_encode == NULL) {
310 goto error;
311 }
312 if (!PyCallable_Check(st->codecs_encode)) {
313 PyErr_Format(PyExc_RuntimeError,
314 "codecs.encode should be a callable, not %.200s",
315 Py_TYPE(st->codecs_encode)->tp_name);
316 goto error;
317 }
318 Py_CLEAR(codecs);
319
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800320 return 0;
321
322 error:
323 Py_CLEAR(copyreg);
324 Py_CLEAR(compat_pickle);
325 Py_CLEAR(codecs);
326 _Pickle_ClearState(st);
327 return -1;
328}
329
330/* Helper for calling a function with a single argument quickly.
331
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800332 This function steals the reference of the given argument. */
333static PyObject *
334_Pickle_FastCall(PyObject *func, PyObject *obj)
335{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800336 PyObject *result;
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800337 PyObject *arg_tuple = PyTuple_New(1);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800338
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800339 /* Note: this function used to reuse the argument tuple. This used to give
340 a slight performance boost with older pickle implementations where many
341 unbuffered reads occurred (thus needing many function calls).
342
343 However, this optimization was removed because it was too complicated
344 to get right. It abused the C API for tuples to mutate them which led
345 to subtle reference counting and concurrency bugs. Furthermore, the
346 introduction of protocol 4 and the prefetching optimization via peek()
347 significantly reduced the number of function calls we do. Thus, the
348 benefits became marginal at best. */
349
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800350 if (arg_tuple == NULL) {
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800351 Py_DECREF(obj);
352 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800353 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800354 PyTuple_SET_ITEM(arg_tuple, 0, obj);
355 result = PyObject_Call(func, arg_tuple, NULL);
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800356 Py_CLEAR(arg_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800357 return result;
358}
359
360/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000361
362static int
363stack_underflow(void)
364{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800365 PickleState *st = _Pickle_GetGlobalState();
366 PyErr_SetString(st->UnpicklingError, "unpickling stack underflow");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000367 return -1;
368}
369
370/* Internal data type used as the unpickling stack. */
371typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000372 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000373 PyObject **data;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000374 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000375} Pdata;
376
377static void
378Pdata_dealloc(Pdata *self)
379{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200380 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000381 while (--i >= 0) {
382 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000383 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000384 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000385 PyObject_Del(self);
386}
387
388static PyTypeObject Pdata_Type = {
389 PyVarObject_HEAD_INIT(NULL, 0)
390 "_pickle.Pdata", /*tp_name*/
391 sizeof(Pdata), /*tp_basicsize*/
392 0, /*tp_itemsize*/
393 (destructor)Pdata_dealloc, /*tp_dealloc*/
394};
395
396static PyObject *
397Pdata_New(void)
398{
399 Pdata *self;
400
401 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
402 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000403 Py_SIZE(self) = 0;
404 self->allocated = 8;
405 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000406 if (self->data)
407 return (PyObject *)self;
408 Py_DECREF(self);
409 return PyErr_NoMemory();
410}
411
412
413/* Retain only the initial clearto items. If clearto >= the current
414 * number of items, this is a (non-erroneous) NOP.
415 */
416static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200417Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000418{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200419 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000420
421 if (clearto < 0)
422 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000423 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000424 return 0;
425
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000426 while (--i >= clearto) {
427 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000428 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000429 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000430 return 0;
431}
432
433static int
434Pdata_grow(Pdata *self)
435{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000436 PyObject **data = self->data;
437 Py_ssize_t allocated = self->allocated;
438 Py_ssize_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000439
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000440 new_allocated = (allocated >> 3) + 6;
441 /* check for integer overflow */
442 if (new_allocated > PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000443 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000444 new_allocated += allocated;
445 if (new_allocated > (PY_SSIZE_T_MAX / sizeof(PyObject *)))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000446 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000447 data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *));
448 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000449 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000450
451 self->data = data;
452 self->allocated = new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000453 return 0;
454
455 nomemory:
456 PyErr_NoMemory();
457 return -1;
458}
459
460/* D is a Pdata*. Pop the topmost element and store it into V, which
461 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
462 * is raised and V is set to NULL.
463 */
464static PyObject *
465Pdata_pop(Pdata *self)
466{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800467 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000468 if (Py_SIZE(self) == 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800469 PyErr_SetString(st->UnpicklingError, "bad pickle data");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000470 return NULL;
471 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000472 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000473}
474#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
475
476static int
477Pdata_push(Pdata *self, PyObject *obj)
478{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000479 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000480 return -1;
481 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000482 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000483 return 0;
484}
485
486/* Push an object on stack, transferring its ownership to the stack. */
487#define PDATA_PUSH(D, O, ER) do { \
488 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
489
490/* Push an object on stack, adding a new reference to the object. */
491#define PDATA_APPEND(D, O, ER) do { \
492 Py_INCREF((O)); \
493 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
494
495static PyObject *
496Pdata_poptuple(Pdata *self, Py_ssize_t start)
497{
498 PyObject *tuple;
499 Py_ssize_t len, i, j;
500
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000501 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000502 tuple = PyTuple_New(len);
503 if (tuple == NULL)
504 return NULL;
505 for (i = start, j = 0; j < len; i++, j++)
506 PyTuple_SET_ITEM(tuple, j, self->data[i]);
507
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000508 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000509 return tuple;
510}
511
512static PyObject *
513Pdata_poplist(Pdata *self, Py_ssize_t start)
514{
515 PyObject *list;
516 Py_ssize_t len, i, j;
517
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000518 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000519 list = PyList_New(len);
520 if (list == NULL)
521 return NULL;
522 for (i = start, j = 0; j < len; i++, j++)
523 PyList_SET_ITEM(list, j, self->data[i]);
524
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000525 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000526 return list;
527}
528
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000529typedef struct {
530 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200531 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000532} PyMemoEntry;
533
534typedef struct {
535 Py_ssize_t mt_mask;
536 Py_ssize_t mt_used;
537 Py_ssize_t mt_allocated;
538 PyMemoEntry *mt_table;
539} PyMemoTable;
540
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000541typedef struct PicklerObject {
542 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000543 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000544 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000545 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000546 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100547 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000548
549 PyObject *write; /* write() method of the output stream. */
550 PyObject *output_buffer; /* Write into a local bytearray buffer before
551 flushing to the stream. */
552 Py_ssize_t output_len; /* Length of output_buffer. */
553 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000554 int proto; /* Pickle protocol number, >= 0 */
555 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100556 int framing; /* True when framing is enabled, proto >= 4 */
557 Py_ssize_t frame_start; /* Position in output_buffer where the
558 where the current frame begins. -1 if there
559 is no frame currently open. */
560
561 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000562 int fast; /* Enable fast mode if set to a true value.
563 The fast mode disable the usage of memo,
564 therefore speeding the pickling process by
565 not generating superfluous PUT opcodes. It
566 should not be used if with self-referential
567 objects. */
568 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000569 int fix_imports; /* Indicate whether Pickler should fix
570 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000571 PyObject *fast_memo;
572} PicklerObject;
573
574typedef struct UnpicklerObject {
575 PyObject_HEAD
576 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000577
578 /* The unpickler memo is just an array of PyObject *s. Using a dict
579 is unnecessary, since the keys are contiguous ints. */
580 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100581 Py_ssize_t memo_size; /* Capacity of the memo array */
582 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000583
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000584 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000585
586 Py_buffer buffer;
587 char *input_buffer;
588 char *input_line;
589 Py_ssize_t input_len;
590 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000591 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100592
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000593 PyObject *read; /* read() method of the input stream. */
594 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000595 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000596
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000597 char *encoding; /* Name of the encoding to be used for
598 decoding strings pickled using Python
599 2.x. The default value is "ASCII" */
600 char *errors; /* Name of errors handling scheme to used when
601 decoding strings. The default value is
602 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500603 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000604 objects. */
605 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
606 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000607 int proto; /* Protocol of the pickle loaded. */
608 int fix_imports; /* Indicate whether Unpickler should fix
609 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000610} UnpicklerObject;
611
612/* Forward declarations */
613static int save(PicklerObject *, PyObject *, int);
614static int save_reduce(PicklerObject *, PyObject *, PyObject *);
615static PyTypeObject Pickler_Type;
616static PyTypeObject Unpickler_Type;
617
618
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000619/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300620 A custom hashtable mapping void* to Python ints. This is used by the pickler
621 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000622 a bunch of unnecessary object creation. This makes a huge performance
623 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000624
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000625#define MT_MINSIZE 8
626#define PERTURB_SHIFT 5
627
628
629static PyMemoTable *
630PyMemoTable_New(void)
631{
632 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
633 if (memo == NULL) {
634 PyErr_NoMemory();
635 return NULL;
636 }
637
638 memo->mt_used = 0;
639 memo->mt_allocated = MT_MINSIZE;
640 memo->mt_mask = MT_MINSIZE - 1;
641 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
642 if (memo->mt_table == NULL) {
643 PyMem_FREE(memo);
644 PyErr_NoMemory();
645 return NULL;
646 }
647 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
648
649 return memo;
650}
651
652static PyMemoTable *
653PyMemoTable_Copy(PyMemoTable *self)
654{
655 Py_ssize_t i;
656 PyMemoTable *new = PyMemoTable_New();
657 if (new == NULL)
658 return NULL;
659
660 new->mt_used = self->mt_used;
661 new->mt_allocated = self->mt_allocated;
662 new->mt_mask = self->mt_mask;
663 /* The table we get from _New() is probably smaller than we wanted.
664 Free it and allocate one that's the right size. */
665 PyMem_FREE(new->mt_table);
666 new->mt_table = PyMem_MALLOC(self->mt_allocated * sizeof(PyMemoEntry));
667 if (new->mt_table == NULL) {
668 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200669 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000670 return NULL;
671 }
672 for (i = 0; i < self->mt_allocated; i++) {
673 Py_XINCREF(self->mt_table[i].me_key);
674 }
675 memcpy(new->mt_table, self->mt_table,
676 sizeof(PyMemoEntry) * self->mt_allocated);
677
678 return new;
679}
680
681static Py_ssize_t
682PyMemoTable_Size(PyMemoTable *self)
683{
684 return self->mt_used;
685}
686
687static int
688PyMemoTable_Clear(PyMemoTable *self)
689{
690 Py_ssize_t i = self->mt_allocated;
691
692 while (--i >= 0) {
693 Py_XDECREF(self->mt_table[i].me_key);
694 }
695 self->mt_used = 0;
696 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
697 return 0;
698}
699
700static void
701PyMemoTable_Del(PyMemoTable *self)
702{
703 if (self == NULL)
704 return;
705 PyMemoTable_Clear(self);
706
707 PyMem_FREE(self->mt_table);
708 PyMem_FREE(self);
709}
710
711/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
712 can be considerably simpler than dictobject.c's lookdict(). */
713static PyMemoEntry *
714_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
715{
716 size_t i;
717 size_t perturb;
718 size_t mask = (size_t)self->mt_mask;
719 PyMemoEntry *table = self->mt_table;
720 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000721 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000722
723 i = hash & mask;
724 entry = &table[i];
725 if (entry->me_key == NULL || entry->me_key == key)
726 return entry;
727
728 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
729 i = (i << 2) + i + perturb + 1;
730 entry = &table[i & mask];
731 if (entry->me_key == NULL || entry->me_key == key)
732 return entry;
733 }
734 assert(0); /* Never reached */
735 return NULL;
736}
737
738/* Returns -1 on failure, 0 on success. */
739static int
740_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
741{
742 PyMemoEntry *oldtable = NULL;
743 PyMemoEntry *oldentry, *newentry;
744 Py_ssize_t new_size = MT_MINSIZE;
745 Py_ssize_t to_process;
746
747 assert(min_size > 0);
748
749 /* Find the smallest valid table size >= min_size. */
750 while (new_size < min_size && new_size > 0)
751 new_size <<= 1;
752 if (new_size <= 0) {
753 PyErr_NoMemory();
754 return -1;
755 }
756 /* new_size needs to be a power of two. */
757 assert((new_size & (new_size - 1)) == 0);
758
759 /* Allocate new table. */
760 oldtable = self->mt_table;
761 self->mt_table = PyMem_MALLOC(new_size * sizeof(PyMemoEntry));
762 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200763 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000764 PyErr_NoMemory();
765 return -1;
766 }
767 self->mt_allocated = new_size;
768 self->mt_mask = new_size - 1;
769 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
770
771 /* Copy entries from the old table. */
772 to_process = self->mt_used;
773 for (oldentry = oldtable; to_process > 0; oldentry++) {
774 if (oldentry->me_key != NULL) {
775 to_process--;
776 /* newentry is a pointer to a chunk of the new
777 mt_table, so we're setting the key:value pair
778 in-place. */
779 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
780 newentry->me_key = oldentry->me_key;
781 newentry->me_value = oldentry->me_value;
782 }
783 }
784
785 /* Deallocate the old table. */
786 PyMem_FREE(oldtable);
787 return 0;
788}
789
790/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200791static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000792PyMemoTable_Get(PyMemoTable *self, PyObject *key)
793{
794 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
795 if (entry->me_key == NULL)
796 return NULL;
797 return &entry->me_value;
798}
799
800/* Returns -1 on failure, 0 on success. */
801static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200802PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000803{
804 PyMemoEntry *entry;
805
806 assert(key != NULL);
807
808 entry = _PyMemoTable_Lookup(self, key);
809 if (entry->me_key != NULL) {
810 entry->me_value = value;
811 return 0;
812 }
813 Py_INCREF(key);
814 entry->me_key = key;
815 entry->me_value = value;
816 self->mt_used++;
817
818 /* If we added a key, we can safely resize. Otherwise just return!
819 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
820 *
821 * Quadrupling the size improves average table sparseness
822 * (reducing collisions) at the cost of some memory. It also halves
823 * the number of expensive resize operations in a growing memo table.
824 *
825 * Very large memo tables (over 50K items) use doubling instead.
826 * This may help applications with severe memory constraints.
827 */
828 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
829 return 0;
830 return _PyMemoTable_ResizeTable(self,
831 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
832}
833
834#undef MT_MINSIZE
835#undef PERTURB_SHIFT
836
837/*************************************************************************/
838
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000839
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000840static int
841_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000842{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000843 Py_CLEAR(self->output_buffer);
844 self->output_buffer =
845 PyBytes_FromStringAndSize(NULL, self->max_output_len);
846 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000847 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000848 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100849 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000850 return 0;
851}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000852
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100853static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100854_write_size64(char *out, size_t value)
855{
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800856 int i;
857
858 assert(sizeof(size_t) <= 8);
859
860 for (i = 0; i < sizeof(size_t); i++) {
861 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
862 }
863 for (i = sizeof(size_t); i < 8; i++) {
864 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800865 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100866}
867
868static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100869_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
870{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100871 qdata[0] = FRAME;
872 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100873}
874
875static int
876_Pickler_CommitFrame(PicklerObject *self)
877{
878 size_t frame_len;
879 char *qdata;
880
881 if (!self->framing || self->frame_start == -1)
882 return 0;
883 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
884 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
885 _Pickler_WriteFrameHeader(self, qdata, frame_len);
886 self->frame_start = -1;
887 return 0;
888}
889
890static int
891_Pickler_OpcodeBoundary(PicklerObject *self)
892{
893 Py_ssize_t frame_len;
894
895 if (!self->framing || self->frame_start == -1)
896 return 0;
897 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
898 if (frame_len >= FRAME_SIZE_TARGET)
899 return _Pickler_CommitFrame(self);
900 else
901 return 0;
902}
903
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000904static PyObject *
905_Pickler_GetString(PicklerObject *self)
906{
907 PyObject *output_buffer = self->output_buffer;
908
909 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100910
911 if (_Pickler_CommitFrame(self))
912 return NULL;
913
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000914 self->output_buffer = NULL;
915 /* Resize down to exact size */
916 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
917 return NULL;
918 return output_buffer;
919}
920
921static int
922_Pickler_FlushToFile(PicklerObject *self)
923{
924 PyObject *output, *result;
925
926 assert(self->write != NULL);
927
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100928 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000929 output = _Pickler_GetString(self);
930 if (output == NULL)
931 return -1;
932
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800933 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000934 Py_XDECREF(result);
935 return (result == NULL) ? -1 : 0;
936}
937
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200938static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100939_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000940{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100941 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000942 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100943 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000944
945 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100946 need_new_frame = (self->framing && self->frame_start == -1);
947
948 if (need_new_frame)
949 n = data_len + FRAME_HEADER_SIZE;
950 else
951 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000952
953 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100954 if (required > self->max_output_len) {
955 /* Make place in buffer for the pickle chunk */
956 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
957 PyErr_NoMemory();
958 return -1;
959 }
960 self->max_output_len = (self->output_len + n) / 2 * 3;
961 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
962 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000963 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000964 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100965 if (need_new_frame) {
966 /* Setup new frame */
967 Py_ssize_t frame_start = self->output_len;
968 self->frame_start = frame_start;
969 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
970 /* Write an invalid value, for debugging */
971 buffer[frame_start + i] = 0xFE;
972 }
973 self->output_len += FRAME_HEADER_SIZE;
974 }
975 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000976 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100977 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000978 buffer[self->output_len + i] = s[i];
979 }
980 }
981 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100982 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000983 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100984 self->output_len += data_len;
985 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000986}
987
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000988static PicklerObject *
989_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000990{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000991 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000992
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000993 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
994 if (self == NULL)
995 return NULL;
996
997 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100998 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000999 self->write = NULL;
1000 self->proto = 0;
1001 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001002 self->framing = 0;
1003 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001004 self->fast = 0;
1005 self->fast_nesting = 0;
1006 self->fix_imports = 0;
1007 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001008 self->max_output_len = WRITE_BUF_SIZE;
1009 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001010
1011 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001012 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1013 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001014
1015 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001016 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001017 return NULL;
1018 }
1019 return self;
1020}
1021
1022static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001023_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001024{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001025 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001026
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001027 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001028 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001029 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001030 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001031 proto = PyLong_AsLong(protocol);
1032 if (proto < 0) {
1033 if (proto == -1 && PyErr_Occurred())
1034 return -1;
1035 proto = HIGHEST_PROTOCOL;
1036 }
1037 else if (proto > HIGHEST_PROTOCOL) {
1038 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1039 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001040 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001041 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001042 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001043 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001044 self->bin = proto > 0;
1045 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001046 return 0;
1047}
1048
1049/* Returns -1 (with an exception set) on failure, 0 on success. This may
1050 be called once on a freshly created Pickler. */
1051static int
1052_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1053{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001054 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001055 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001056 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001057 if (self->write == NULL) {
1058 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1059 PyErr_SetString(PyExc_TypeError,
1060 "file must have a 'write' attribute");
1061 return -1;
1062 }
1063
1064 return 0;
1065}
1066
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001067/* Returns the size of the input on success, -1 on failure. This takes its
1068 own reference to `input`. */
1069static Py_ssize_t
1070_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1071{
1072 if (self->buffer.buf != NULL)
1073 PyBuffer_Release(&self->buffer);
1074 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1075 return -1;
1076 self->input_buffer = self->buffer.buf;
1077 self->input_len = self->buffer.len;
1078 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001079 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001080 return self->input_len;
1081}
1082
Antoine Pitrou04248a82010-10-12 20:51:21 +00001083static int
1084_Unpickler_SkipConsumed(UnpicklerObject *self)
1085{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001086 Py_ssize_t consumed;
1087 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001088
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001089 consumed = self->next_read_idx - self->prefetched_idx;
1090 if (consumed <= 0)
1091 return 0;
1092
1093 assert(self->peek); /* otherwise we did something wrong */
1094 /* This makes an useless copy... */
1095 r = PyObject_CallFunction(self->read, "n", consumed);
1096 if (r == NULL)
1097 return -1;
1098 Py_DECREF(r);
1099
1100 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001101 return 0;
1102}
1103
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001104static const Py_ssize_t READ_WHOLE_LINE = -1;
1105
1106/* If reading from a file, we need to only pull the bytes we need, since there
1107 may be multiple pickle objects arranged contiguously in the same input
1108 buffer.
1109
1110 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1111 bytes from the input stream/buffer.
1112
1113 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1114 failure; on success, returns the number of bytes read from the file.
1115
1116 On success, self->input_len will be 0; this is intentional so that when
1117 unpickling from a file, the "we've run out of data" code paths will trigger,
1118 causing the Unpickler to go back to the file for more data. Use the returned
1119 size to tell you how much data you can process. */
1120static Py_ssize_t
1121_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1122{
1123 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001124 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001125
1126 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001127
Antoine Pitrou04248a82010-10-12 20:51:21 +00001128 if (_Unpickler_SkipConsumed(self) < 0)
1129 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001130
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001131 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001132 PyObject *empty_tuple = PyTuple_New(0);
1133 data = PyObject_Call(self->readline, empty_tuple, NULL);
1134 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001135 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001136 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001137 PyObject *len;
1138 /* Prefetch some data without advancing the file pointer, if possible */
1139 if (self->peek && n < PREFETCH) {
1140 len = PyLong_FromSsize_t(PREFETCH);
1141 if (len == NULL)
1142 return -1;
1143 data = _Pickle_FastCall(self->peek, len);
1144 if (data == NULL) {
1145 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1146 return -1;
1147 /* peek() is probably not supported by the given file object */
1148 PyErr_Clear();
1149 Py_CLEAR(self->peek);
1150 }
1151 else {
1152 read_size = _Unpickler_SetStringInput(self, data);
1153 Py_DECREF(data);
1154 self->prefetched_idx = 0;
1155 if (n <= read_size)
1156 return n;
1157 }
1158 }
1159 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001160 if (len == NULL)
1161 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001162 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001163 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001164 if (data == NULL)
1165 return -1;
1166
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001167 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001168 Py_DECREF(data);
1169 return read_size;
1170}
1171
1172/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1173
1174 This should be used for all data reads, rather than accessing the unpickler's
1175 input buffer directly. This method deals correctly with reading from input
1176 streams, which the input buffer doesn't deal with.
1177
1178 Note that when reading from a file-like object, self->next_read_idx won't
1179 be updated (it should remain at 0 for the entire unpickling process). You
1180 should use this function's return value to know how many bytes you can
1181 consume.
1182
1183 Returns -1 (with an exception set) on failure. On success, return the
1184 number of chars read. */
1185static Py_ssize_t
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001186_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001187{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001188 Py_ssize_t num_read;
1189
Antoine Pitrou04248a82010-10-12 20:51:21 +00001190 if (self->next_read_idx + n <= self->input_len) {
1191 *s = self->input_buffer + self->next_read_idx;
1192 self->next_read_idx += n;
1193 return n;
1194 }
1195 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001196 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001197 return -1;
1198 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001199 num_read = _Unpickler_ReadFromFile(self, n);
1200 if (num_read < 0)
1201 return -1;
1202 if (num_read < n) {
1203 PyErr_Format(PyExc_EOFError, "Ran out of input");
1204 return -1;
1205 }
1206 *s = self->input_buffer;
1207 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001208 return n;
1209}
1210
1211static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001212_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1213 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001214{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001215 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001216 if (input_line == NULL) {
1217 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001218 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001219 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001220
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001221 memcpy(input_line, line, len);
1222 input_line[len] = '\0';
1223 self->input_line = input_line;
1224 *result = self->input_line;
1225 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001226}
1227
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001228/* Read a line from the input stream/buffer. If we run off the end of the input
1229 before hitting \n, return the data we found.
1230
1231 Returns the number of chars read, or -1 on failure. */
1232static Py_ssize_t
1233_Unpickler_Readline(UnpicklerObject *self, char **result)
1234{
1235 Py_ssize_t i, num_read;
1236
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001237 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001238 if (self->input_buffer[i] == '\n') {
1239 char *line_start = self->input_buffer + self->next_read_idx;
1240 num_read = i - self->next_read_idx + 1;
1241 self->next_read_idx = i + 1;
1242 return _Unpickler_CopyLine(self, line_start, num_read, result);
1243 }
1244 }
1245 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001246 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1247 if (num_read < 0)
1248 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001249 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001250 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001251 }
Victor Stinner121aab42011-09-29 23:40:53 +02001252
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001253 /* If we get here, we've run off the end of the input string. Return the
1254 remaining string and let the caller figure it out. */
1255 *result = self->input_buffer + self->next_read_idx;
1256 num_read = i - self->next_read_idx;
1257 self->next_read_idx = i;
1258 return num_read;
1259}
1260
1261/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1262 will be modified in place. */
1263static int
1264_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1265{
1266 Py_ssize_t i;
1267 PyObject **memo;
1268
1269 assert(new_size > self->memo_size);
1270
1271 memo = PyMem_REALLOC(self->memo, new_size * sizeof(PyObject *));
1272 if (memo == NULL) {
1273 PyErr_NoMemory();
1274 return -1;
1275 }
1276 self->memo = memo;
1277 for (i = self->memo_size; i < new_size; i++)
1278 self->memo[i] = NULL;
1279 self->memo_size = new_size;
1280 return 0;
1281}
1282
1283/* Returns NULL if idx is out of bounds. */
1284static PyObject *
1285_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1286{
1287 if (idx < 0 || idx >= self->memo_size)
1288 return NULL;
1289
1290 return self->memo[idx];
1291}
1292
1293/* Returns -1 (with an exception set) on failure, 0 on success.
1294 This takes its own reference to `value`. */
1295static int
1296_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1297{
1298 PyObject *old_item;
1299
1300 if (idx >= self->memo_size) {
1301 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1302 return -1;
1303 assert(idx < self->memo_size);
1304 }
1305 Py_INCREF(value);
1306 old_item = self->memo[idx];
1307 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001308 if (old_item != NULL) {
1309 Py_DECREF(old_item);
1310 }
1311 else {
1312 self->memo_len++;
1313 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001314 return 0;
1315}
1316
1317static PyObject **
1318_Unpickler_NewMemo(Py_ssize_t new_size)
1319{
1320 PyObject **memo = PyMem_MALLOC(new_size * sizeof(PyObject *));
Victor Stinner42024562013-07-12 00:53:57 +02001321 if (memo == NULL) {
1322 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001323 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001324 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001325 memset(memo, 0, new_size * sizeof(PyObject *));
1326 return memo;
1327}
1328
1329/* Free the unpickler's memo, taking care to decref any items left in it. */
1330static void
1331_Unpickler_MemoCleanup(UnpicklerObject *self)
1332{
1333 Py_ssize_t i;
1334 PyObject **memo = self->memo;
1335
1336 if (self->memo == NULL)
1337 return;
1338 self->memo = NULL;
1339 i = self->memo_size;
1340 while (--i >= 0) {
1341 Py_XDECREF(memo[i]);
1342 }
1343 PyMem_FREE(memo);
1344}
1345
1346static UnpicklerObject *
1347_Unpickler_New(void)
1348{
1349 UnpicklerObject *self;
1350
1351 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1352 if (self == NULL)
1353 return NULL;
1354
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001355 self->pers_func = NULL;
1356 self->input_buffer = NULL;
1357 self->input_line = NULL;
1358 self->input_len = 0;
1359 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001360 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001361 self->read = NULL;
1362 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001363 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001364 self->encoding = NULL;
1365 self->errors = NULL;
1366 self->marks = NULL;
1367 self->num_marks = 0;
1368 self->marks_size = 0;
1369 self->proto = 0;
1370 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001371 memset(&self->buffer, 0, sizeof(Py_buffer));
1372 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001373 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001374 self->memo = _Unpickler_NewMemo(self->memo_size);
1375 self->stack = (Pdata *)Pdata_New();
1376
1377 if (self->memo == NULL || self->stack == NULL) {
1378 Py_DECREF(self);
1379 return NULL;
1380 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001381
1382 return self;
1383}
1384
1385/* Returns -1 (with an exception set) on failure, 0 on success. This may
1386 be called once on a freshly created Pickler. */
1387static int
1388_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1389{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001390 _Py_IDENTIFIER(peek);
1391 _Py_IDENTIFIER(read);
1392 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001393
1394 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001395 if (self->peek == NULL) {
1396 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1397 PyErr_Clear();
1398 else
1399 return -1;
1400 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001401 self->read = _PyObject_GetAttrId(file, &PyId_read);
1402 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001403 if (self->readline == NULL || self->read == NULL) {
1404 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1405 PyErr_SetString(PyExc_TypeError,
1406 "file must have 'read' and 'readline' attributes");
1407 Py_CLEAR(self->read);
1408 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001409 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001410 return -1;
1411 }
1412 return 0;
1413}
1414
1415/* Returns -1 (with an exception set) on failure, 0 on success. This may
1416 be called once on a freshly created Pickler. */
1417static int
1418_Unpickler_SetInputEncoding(UnpicklerObject *self,
1419 const char *encoding,
1420 const char *errors)
1421{
1422 if (encoding == NULL)
1423 encoding = "ASCII";
1424 if (errors == NULL)
1425 errors = "strict";
1426
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001427 self->encoding = _PyMem_Strdup(encoding);
1428 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001429 if (self->encoding == NULL || self->errors == NULL) {
1430 PyErr_NoMemory();
1431 return -1;
1432 }
1433 return 0;
1434}
1435
1436/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001437static int
1438memo_get(PicklerObject *self, PyObject *key)
1439{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001440 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001441 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001442 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001443
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001444 value = PyMemoTable_Get(self->memo, key);
1445 if (value == NULL) {
1446 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001447 return -1;
1448 }
1449
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001450 if (!self->bin) {
1451 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001452 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1453 "%" PY_FORMAT_SIZE_T "d\n", *value);
1454 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001455 }
1456 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001457 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001458 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001459 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001460 len = 2;
1461 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001462 else if (*value <= 0xffffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001463 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001464 pdata[1] = (unsigned char)(*value & 0xff);
1465 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1466 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1467 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001468 len = 5;
1469 }
1470 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001471 PickleState *st = _Pickle_GetGlobalState();
1472 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001473 "memo id too large for LONG_BINGET");
1474 return -1;
1475 }
1476 }
1477
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001478 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001479 return -1;
1480
1481 return 0;
1482}
1483
1484/* Store an object in the memo, assign it a new unique ID based on the number
1485 of objects currently stored in the memo and generate a PUT opcode. */
1486static int
1487memo_put(PicklerObject *self, PyObject *obj)
1488{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001489 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001490 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001491 Py_ssize_t idx;
1492
1493 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001494
1495 if (self->fast)
1496 return 0;
1497
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001498 idx = PyMemoTable_Size(self->memo);
1499 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1500 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001501
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001502 if (self->proto >= 4) {
1503 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1504 return -1;
1505 return 0;
1506 }
1507 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001508 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001509 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001510 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001511 len = strlen(pdata);
1512 }
1513 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001514 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001515 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001516 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001517 len = 2;
1518 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001519 else if (idx <= 0xffffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001520 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001521 pdata[1] = (unsigned char)(idx & 0xff);
1522 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1523 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1524 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001525 len = 5;
1526 }
1527 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001528 PickleState *st = _Pickle_GetGlobalState();
1529 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001530 "memo id too large for LONG_BINPUT");
1531 return -1;
1532 }
1533 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001534 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001535 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001536
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001537 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001538}
1539
1540static PyObject *
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001541getattribute(PyObject *obj, PyObject *name, int allow_qualname) {
1542 PyObject *dotted_path;
1543 Py_ssize_t i;
1544 _Py_static_string(PyId_dot, ".");
1545 _Py_static_string(PyId_locals, "<locals>");
1546
1547 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
1548 if (dotted_path == NULL) {
1549 return NULL;
1550 }
1551 assert(Py_SIZE(dotted_path) >= 1);
1552 if (!allow_qualname && Py_SIZE(dotted_path) > 1) {
1553 PyErr_Format(PyExc_AttributeError,
1554 "Can't get qualified attribute %R on %R;"
1555 "use protocols >= 4 to enable support",
1556 name, obj);
1557 Py_DECREF(dotted_path);
1558 return NULL;
1559 }
1560 Py_INCREF(obj);
1561 for (i = 0; i < Py_SIZE(dotted_path); i++) {
1562 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
1563 PyObject *tmp;
1564 PyObject *result = PyUnicode_RichCompare(
1565 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1566 int is_equal = (result == Py_True);
1567 assert(PyBool_Check(result));
1568 Py_DECREF(result);
1569 if (is_equal) {
1570 PyErr_Format(PyExc_AttributeError,
1571 "Can't get local attribute %R on %R", name, obj);
1572 Py_DECREF(dotted_path);
1573 Py_DECREF(obj);
1574 return NULL;
1575 }
1576 tmp = PyObject_GetAttr(obj, subpath);
1577 Py_DECREF(obj);
1578 if (tmp == NULL) {
1579 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1580 PyErr_Clear();
1581 PyErr_Format(PyExc_AttributeError,
1582 "Can't get attribute %R on %R", name, obj);
1583 }
1584 Py_DECREF(dotted_path);
1585 return NULL;
1586 }
1587 obj = tmp;
1588 }
1589 Py_DECREF(dotted_path);
1590 return obj;
1591}
1592
1593static PyObject *
1594whichmodule(PyObject *global, PyObject *global_name, int allow_qualname)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001595{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001596 PyObject *module_name;
1597 PyObject *modules_dict;
1598 PyObject *module;
1599 PyObject *obj;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001600 Py_ssize_t i, j;
1601 _Py_IDENTIFIER(__module__);
1602 _Py_IDENTIFIER(modules);
1603 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001604
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001605 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1606
1607 if (module_name == NULL) {
1608 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001609 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001610 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001611 }
1612 else {
1613 /* In some rare cases (e.g., bound methods of extension types),
1614 __module__ can be None. If it is so, then search sys.modules for
1615 the module of global. */
1616 if (module_name != Py_None)
1617 return module_name;
1618 Py_CLEAR(module_name);
1619 }
1620 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001621
Victor Stinnerbb520202013-11-06 22:40:41 +01001622 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001623 if (modules_dict == NULL) {
1624 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001625 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001626 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001627
1628 i = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001629 while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001630 PyObject *result = PyUnicode_RichCompare(
1631 module_name, _PyUnicode_FromId(&PyId___main__), Py_EQ);
1632 int is_equal = (result == Py_True);
1633 assert(PyBool_Check(result));
1634 Py_DECREF(result);
1635 if (is_equal)
1636 continue;
1637 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001638 continue;
1639
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001640 obj = getattribute(module, global_name, allow_qualname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001641 if (obj == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001642 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001643 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001644 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001645 continue;
1646 }
1647
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001648 if (obj == global) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001649 Py_DECREF(obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001650 Py_INCREF(module_name);
1651 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001652 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001653 Py_DECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001654 }
1655
1656 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001657 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001658 Py_INCREF(module_name);
1659 return module_name;
1660}
1661
1662/* fast_save_enter() and fast_save_leave() are guards against recursive
1663 objects when Pickler is used with the "fast mode" (i.e., with object
1664 memoization disabled). If the nesting of a list or dict object exceed
1665 FAST_NESTING_LIMIT, these guards will start keeping an internal
1666 reference to the seen list or dict objects and check whether these objects
1667 are recursive. These are not strictly necessary, since save() has a
1668 hard-coded recursion limit, but they give a nicer error message than the
1669 typical RuntimeError. */
1670static int
1671fast_save_enter(PicklerObject *self, PyObject *obj)
1672{
1673 /* if fast_nesting < 0, we're doing an error exit. */
1674 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1675 PyObject *key = NULL;
1676 if (self->fast_memo == NULL) {
1677 self->fast_memo = PyDict_New();
1678 if (self->fast_memo == NULL) {
1679 self->fast_nesting = -1;
1680 return 0;
1681 }
1682 }
1683 key = PyLong_FromVoidPtr(obj);
1684 if (key == NULL)
1685 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001686 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001687 Py_DECREF(key);
1688 PyErr_Format(PyExc_ValueError,
1689 "fast mode: can't pickle cyclic objects "
1690 "including object type %.200s at %p",
1691 obj->ob_type->tp_name, obj);
1692 self->fast_nesting = -1;
1693 return 0;
1694 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001695 if (PyErr_Occurred()) {
1696 return 0;
1697 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001698 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1699 Py_DECREF(key);
1700 self->fast_nesting = -1;
1701 return 0;
1702 }
1703 Py_DECREF(key);
1704 }
1705 return 1;
1706}
1707
1708static int
1709fast_save_leave(PicklerObject *self, PyObject *obj)
1710{
1711 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1712 PyObject *key = PyLong_FromVoidPtr(obj);
1713 if (key == NULL)
1714 return 0;
1715 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1716 Py_DECREF(key);
1717 return 0;
1718 }
1719 Py_DECREF(key);
1720 }
1721 return 1;
1722}
1723
1724static int
1725save_none(PicklerObject *self, PyObject *obj)
1726{
1727 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001728 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001729 return -1;
1730
1731 return 0;
1732}
1733
1734static int
1735save_bool(PicklerObject *self, PyObject *obj)
1736{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001737 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001738 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001739 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001740 return -1;
1741 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001742 else {
1743 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1744 * so that unpicklers written before bools were introduced unpickle them
1745 * as ints, but unpicklers after can recognize that bools were intended.
1746 * Note that protocol 2 added direct ways to pickle bools.
1747 */
1748 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1749 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1750 return -1;
1751 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001752 return 0;
1753}
1754
1755static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001756save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001757{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001758 PyObject *repr = NULL;
1759 Py_ssize_t size;
1760 long val;
1761 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001762
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001763 const char long_op = LONG;
1764
1765 val= PyLong_AsLong(obj);
1766 if (val == -1 && PyErr_Occurred()) {
1767 /* out of range for int pickling */
1768 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001769 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001770 else if (self->bin &&
1771 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001772 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001773 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001774
1775 Note: we can't use -0x80000000L in the above condition because some
1776 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1777 before applying the unary minus when sizeof(long) <= 4. The
1778 resulting value stays unsigned which is commonly not what we want,
1779 so MSVC happily warns us about it. However, that result would have
1780 been fine because we guard for sizeof(long) <= 4 which turns the
1781 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001782 char pdata[32];
1783 Py_ssize_t len = 0;
1784
1785 pdata[1] = (unsigned char)(val & 0xff);
1786 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1787 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1788 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001789
1790 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1791 if (pdata[2] == 0) {
1792 pdata[0] = BININT1;
1793 len = 2;
1794 }
1795 else {
1796 pdata[0] = BININT2;
1797 len = 3;
1798 }
1799 }
1800 else {
1801 pdata[0] = BININT;
1802 len = 5;
1803 }
1804
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001805 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001806 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001807
1808 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001809 }
1810
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001811 if (self->proto >= 2) {
1812 /* Linear-time pickling. */
1813 size_t nbits;
1814 size_t nbytes;
1815 unsigned char *pdata;
1816 char header[5];
1817 int i;
1818 int sign = _PyLong_Sign(obj);
1819
1820 if (sign == 0) {
1821 header[0] = LONG1;
1822 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001823 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001824 goto error;
1825 return 0;
1826 }
1827 nbits = _PyLong_NumBits(obj);
1828 if (nbits == (size_t)-1 && PyErr_Occurred())
1829 goto error;
1830 /* How many bytes do we need? There are nbits >> 3 full
1831 * bytes of data, and nbits & 7 leftover bits. If there
1832 * are any leftover bits, then we clearly need another
1833 * byte. Wnat's not so obvious is that we *probably*
1834 * need another byte even if there aren't any leftovers:
1835 * the most-significant bit of the most-significant byte
1836 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001837 * opposite of the one we need. The exception is ints
1838 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001839 * its own 256's-complement, so has the right sign bit
1840 * even without the extra byte. That's a pain to check
1841 * for in advance, though, so we always grab an extra
1842 * byte at the start, and cut it back later if possible.
1843 */
1844 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001845 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001846 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001847 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001848 goto error;
1849 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001850 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001851 if (repr == NULL)
1852 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001853 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001854 i = _PyLong_AsByteArray((PyLongObject *)obj,
1855 pdata, nbytes,
1856 1 /* little endian */ , 1 /* signed */ );
1857 if (i < 0)
1858 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001859 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001860 * needed. This is so iff the MSB is all redundant sign
1861 * bits.
1862 */
1863 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001864 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001865 pdata[nbytes - 1] == 0xff &&
1866 (pdata[nbytes - 2] & 0x80) != 0) {
1867 nbytes--;
1868 }
1869
1870 if (nbytes < 256) {
1871 header[0] = LONG1;
1872 header[1] = (unsigned char)nbytes;
1873 size = 2;
1874 }
1875 else {
1876 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001877 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001878 for (i = 1; i < 5; i++) {
1879 header[i] = (unsigned char)(size & 0xff);
1880 size >>= 8;
1881 }
1882 size = 5;
1883 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001884 if (_Pickler_Write(self, header, size) < 0 ||
1885 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001886 goto error;
1887 }
1888 else {
1889 char *string;
1890
Mark Dickinson8dd05142009-01-20 20:43:58 +00001891 /* proto < 2: write the repr and newline. This is quadratic-time (in
1892 the number of digits), in both directions. We add a trailing 'L'
1893 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001894
1895 repr = PyObject_Repr(obj);
1896 if (repr == NULL)
1897 goto error;
1898
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001899 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001900 if (string == NULL)
1901 goto error;
1902
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001903 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1904 _Pickler_Write(self, string, size) < 0 ||
1905 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001906 goto error;
1907 }
1908
1909 if (0) {
1910 error:
1911 status = -1;
1912 }
1913 Py_XDECREF(repr);
1914
1915 return status;
1916}
1917
1918static int
1919save_float(PicklerObject *self, PyObject *obj)
1920{
1921 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1922
1923 if (self->bin) {
1924 char pdata[9];
1925 pdata[0] = BINFLOAT;
1926 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1927 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001928 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001929 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001930 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001931 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001932 int result = -1;
1933 char *buf = NULL;
1934 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001935
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001936 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001937 goto done;
1938
Mark Dickinson3e09f432009-04-17 08:41:23 +00001939 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001940 if (!buf) {
1941 PyErr_NoMemory();
1942 goto done;
1943 }
1944
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001945 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001946 goto done;
1947
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001948 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001949 goto done;
1950
1951 result = 0;
1952done:
1953 PyMem_Free(buf);
1954 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001955 }
1956
1957 return 0;
1958}
1959
1960static int
1961save_bytes(PicklerObject *self, PyObject *obj)
1962{
1963 if (self->proto < 3) {
1964 /* Older pickle protocols do not have an opcode for pickling bytes
1965 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001966 the __reduce__ method) to permit bytes object unpickling.
1967
1968 Here we use a hack to be compatible with Python 2. Since in Python
1969 2 'bytes' is just an alias for 'str' (which has different
1970 parameters than the actual bytes object), we use codecs.encode
1971 to create the appropriate 'str' object when unpickled using
1972 Python 2 *and* the appropriate 'bytes' object when unpickled
1973 using Python 3. Again this is a hack and we don't need to do this
1974 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001975 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001976 int status;
1977
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001978 if (PyBytes_GET_SIZE(obj) == 0) {
1979 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
1980 }
1981 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001982 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001983 PyObject *unicode_str =
1984 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
1985 PyBytes_GET_SIZE(obj),
1986 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001987 _Py_IDENTIFIER(latin1);
1988
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001989 if (unicode_str == NULL)
1990 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001991 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001992 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001993 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001994 Py_DECREF(unicode_str);
1995 }
1996
1997 if (reduce_value == NULL)
1998 return -1;
1999
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002000 /* save_reduce() will memoize the object automatically. */
2001 status = save_reduce(self, reduce_value, obj);
2002 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002003 return status;
2004 }
2005 else {
2006 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002007 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002008 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002009
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002010 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002011 if (size < 0)
2012 return -1;
2013
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002014 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002015 header[0] = SHORT_BINBYTES;
2016 header[1] = (unsigned char)size;
2017 len = 2;
2018 }
2019 else if (size <= 0xffffffffL) {
2020 header[0] = BINBYTES;
2021 header[1] = (unsigned char)(size & 0xff);
2022 header[2] = (unsigned char)((size >> 8) & 0xff);
2023 header[3] = (unsigned char)((size >> 16) & 0xff);
2024 header[4] = (unsigned char)((size >> 24) & 0xff);
2025 len = 5;
2026 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002027 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002028 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002029 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002030 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002031 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002033 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002034 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002035 return -1; /* string too large */
2036 }
2037
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002038 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002039 return -1;
2040
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002041 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002042 return -1;
2043
2044 if (memo_put(self, obj) < 0)
2045 return -1;
2046
2047 return 0;
2048 }
2049}
2050
2051/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2052 backslash and newline characters to \uXXXX escapes. */
2053static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002054raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002055{
2056 PyObject *repr, *result;
2057 char *p;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002058 Py_ssize_t i, size, expandsize;
2059 void *data;
2060 unsigned int kind;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002061
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002062 if (PyUnicode_READY(obj))
2063 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002064
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002065 size = PyUnicode_GET_LENGTH(obj);
2066 data = PyUnicode_DATA(obj);
2067 kind = PyUnicode_KIND(obj);
2068 if (kind == PyUnicode_4BYTE_KIND)
2069 expandsize = 10;
2070 else
2071 expandsize = 6;
Victor Stinner121aab42011-09-29 23:40:53 +02002072
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002073 if (size > PY_SSIZE_T_MAX / expandsize)
2074 return PyErr_NoMemory();
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002075 repr = PyByteArray_FromStringAndSize(NULL, expandsize * size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002076 if (repr == NULL)
2077 return NULL;
2078 if (size == 0)
2079 goto done;
2080
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002081 p = PyByteArray_AS_STRING(repr);
2082 for (i=0; i < size; i++) {
2083 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002084 /* Map 32-bit characters to '\Uxxxxxxxx' */
2085 if (ch >= 0x10000) {
2086 *p++ = '\\';
2087 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002088 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2089 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2090 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2091 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2092 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2093 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2094 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2095 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002096 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002097 /* Map 16-bit characters to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002098 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002099 *p++ = '\\';
2100 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002101 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2102 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2103 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2104 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002105 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002106 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002107 else
2108 *p++ = (char) ch;
2109 }
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002110 size = p - PyByteArray_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002111
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002112done:
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002113 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002114 Py_DECREF(repr);
2115 return result;
2116}
2117
2118static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002119write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2120{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002121 char header[9];
2122 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002123
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002124 if (size <= 0xff && self->proto >= 4) {
2125 header[0] = SHORT_BINUNICODE;
2126 header[1] = (unsigned char)(size & 0xff);
2127 len = 2;
2128 }
2129 else if (size <= 0xffffffffUL) {
2130 header[0] = BINUNICODE;
2131 header[1] = (unsigned char)(size & 0xff);
2132 header[2] = (unsigned char)((size >> 8) & 0xff);
2133 header[3] = (unsigned char)((size >> 16) & 0xff);
2134 header[4] = (unsigned char)((size >> 24) & 0xff);
2135 len = 5;
2136 }
2137 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002138 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002139 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002140 len = 9;
2141 }
2142 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002143 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002144 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002145 return -1;
2146 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002147
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002148 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002149 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002150 if (_Pickler_Write(self, data, size) < 0)
2151 return -1;
2152
2153 return 0;
2154}
2155
2156static int
2157write_unicode_binary(PicklerObject *self, PyObject *obj)
2158{
2159 PyObject *encoded = NULL;
2160 Py_ssize_t size;
2161 char *data;
2162 int r;
2163
2164 if (PyUnicode_READY(obj))
2165 return -1;
2166
2167 data = PyUnicode_AsUTF8AndSize(obj, &size);
2168 if (data != NULL)
2169 return write_utf8(self, data, size);
2170
2171 /* Issue #8383: for strings with lone surrogates, fallback on the
2172 "surrogatepass" error handler. */
2173 PyErr_Clear();
2174 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2175 if (encoded == NULL)
2176 return -1;
2177
2178 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2179 PyBytes_GET_SIZE(encoded));
2180 Py_DECREF(encoded);
2181 return r;
2182}
2183
2184static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002185save_unicode(PicklerObject *self, PyObject *obj)
2186{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002187 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002188 if (write_unicode_binary(self, obj) < 0)
2189 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002190 }
2191 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002192 PyObject *encoded;
2193 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002194 const char unicode_op = UNICODE;
2195
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002196 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002197 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002198 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002199
Antoine Pitrou299978d2013-04-07 17:38:11 +02002200 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2201 Py_DECREF(encoded);
2202 return -1;
2203 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002204
2205 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002206 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2207 Py_DECREF(encoded);
2208 return -1;
2209 }
2210 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002211
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002212 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002213 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002214 }
2215 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002216 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002217
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002218 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002219}
2220
2221/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2222static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002223store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002224{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002225 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002226
2227 assert(PyTuple_Size(t) == len);
2228
2229 for (i = 0; i < len; i++) {
2230 PyObject *element = PyTuple_GET_ITEM(t, i);
2231
2232 if (element == NULL)
2233 return -1;
2234 if (save(self, element, 0) < 0)
2235 return -1;
2236 }
2237
2238 return 0;
2239}
2240
2241/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2242 * used across protocols to minimize the space needed to pickle them.
2243 * Tuples are also the only builtin immutable type that can be recursive
2244 * (a tuple can be reached from itself), and that requires some subtle
2245 * magic so that it works in all cases. IOW, this is a long routine.
2246 */
2247static int
2248save_tuple(PicklerObject *self, PyObject *obj)
2249{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002250 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002251
2252 const char mark_op = MARK;
2253 const char tuple_op = TUPLE;
2254 const char pop_op = POP;
2255 const char pop_mark_op = POP_MARK;
2256 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2257
2258 if ((len = PyTuple_Size(obj)) < 0)
2259 return -1;
2260
2261 if (len == 0) {
2262 char pdata[2];
2263
2264 if (self->proto) {
2265 pdata[0] = EMPTY_TUPLE;
2266 len = 1;
2267 }
2268 else {
2269 pdata[0] = MARK;
2270 pdata[1] = TUPLE;
2271 len = 2;
2272 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002273 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002274 return -1;
2275 return 0;
2276 }
2277
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002278 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002279 * saving the tuple elements, the tuple must be recursive, in
2280 * which case we'll pop everything we put on the stack, and fetch
2281 * its value from the memo.
2282 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002283 if (len <= 3 && self->proto >= 2) {
2284 /* Use TUPLE{1,2,3} opcodes. */
2285 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002286 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002287
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002288 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002289 /* pop the len elements */
2290 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002291 if (_Pickler_Write(self, &pop_op, 1) < 0)
2292 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002293 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002294 if (memo_get(self, obj) < 0)
2295 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002296
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002297 return 0;
2298 }
2299 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002300 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2301 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002302 }
2303 goto memoize;
2304 }
2305
2306 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2307 * Generate MARK e1 e2 ... TUPLE
2308 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002309 if (_Pickler_Write(self, &mark_op, 1) < 0)
2310 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002311
2312 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002313 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002314
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002315 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002316 /* pop the stack stuff we pushed */
2317 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002318 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2319 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002320 }
2321 else {
2322 /* Note that we pop one more than len, to remove
2323 * the MARK too.
2324 */
2325 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002326 if (_Pickler_Write(self, &pop_op, 1) < 0)
2327 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002328 }
2329 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002330 if (memo_get(self, obj) < 0)
2331 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002332
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002333 return 0;
2334 }
2335 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002336 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2337 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002338 }
2339
2340 memoize:
2341 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002342 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002343
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002344 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002345}
2346
2347/* iter is an iterator giving items, and we batch up chunks of
2348 * MARK item item ... item APPENDS
2349 * opcode sequences. Calling code should have arranged to first create an
2350 * empty list, or list-like object, for the APPENDS to operate on.
2351 * Returns 0 on success, <0 on error.
2352 */
2353static int
2354batch_list(PicklerObject *self, PyObject *iter)
2355{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002356 PyObject *obj = NULL;
2357 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002358 int i, n;
2359
2360 const char mark_op = MARK;
2361 const char append_op = APPEND;
2362 const char appends_op = APPENDS;
2363
2364 assert(iter != NULL);
2365
2366 /* XXX: I think this function could be made faster by avoiding the
2367 iterator interface and fetching objects directly from list using
2368 PyList_GET_ITEM.
2369 */
2370
2371 if (self->proto == 0) {
2372 /* APPENDS isn't available; do one at a time. */
2373 for (;;) {
2374 obj = PyIter_Next(iter);
2375 if (obj == NULL) {
2376 if (PyErr_Occurred())
2377 return -1;
2378 break;
2379 }
2380 i = save(self, obj, 0);
2381 Py_DECREF(obj);
2382 if (i < 0)
2383 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002384 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002385 return -1;
2386 }
2387 return 0;
2388 }
2389
2390 /* proto > 0: write in batches of BATCHSIZE. */
2391 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002392 /* Get first item */
2393 firstitem = PyIter_Next(iter);
2394 if (firstitem == NULL) {
2395 if (PyErr_Occurred())
2396 goto error;
2397
2398 /* nothing more to add */
2399 break;
2400 }
2401
2402 /* Try to get a second item */
2403 obj = PyIter_Next(iter);
2404 if (obj == NULL) {
2405 if (PyErr_Occurred())
2406 goto error;
2407
2408 /* Only one item to write */
2409 if (save(self, firstitem, 0) < 0)
2410 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002411 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002412 goto error;
2413 Py_CLEAR(firstitem);
2414 break;
2415 }
2416
2417 /* More than one item to write */
2418
2419 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002420 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002421 goto error;
2422
2423 if (save(self, firstitem, 0) < 0)
2424 goto error;
2425 Py_CLEAR(firstitem);
2426 n = 1;
2427
2428 /* Fetch and save up to BATCHSIZE items */
2429 while (obj) {
2430 if (save(self, obj, 0) < 0)
2431 goto error;
2432 Py_CLEAR(obj);
2433 n += 1;
2434
2435 if (n == BATCHSIZE)
2436 break;
2437
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002438 obj = PyIter_Next(iter);
2439 if (obj == NULL) {
2440 if (PyErr_Occurred())
2441 goto error;
2442 break;
2443 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002444 }
2445
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002446 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002447 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002448
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002449 } while (n == BATCHSIZE);
2450 return 0;
2451
2452 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002453 Py_XDECREF(firstitem);
2454 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002455 return -1;
2456}
2457
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002458/* This is a variant of batch_list() above, specialized for lists (with no
2459 * support for list subclasses). Like batch_list(), we batch up chunks of
2460 * MARK item item ... item APPENDS
2461 * opcode sequences. Calling code should have arranged to first create an
2462 * empty list, or list-like object, for the APPENDS to operate on.
2463 * Returns 0 on success, -1 on error.
2464 *
2465 * This version is considerably faster than batch_list(), if less general.
2466 *
2467 * Note that this only works for protocols > 0.
2468 */
2469static int
2470batch_list_exact(PicklerObject *self, PyObject *obj)
2471{
2472 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002473 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002474
2475 const char append_op = APPEND;
2476 const char appends_op = APPENDS;
2477 const char mark_op = MARK;
2478
2479 assert(obj != NULL);
2480 assert(self->proto > 0);
2481 assert(PyList_CheckExact(obj));
2482
2483 if (PyList_GET_SIZE(obj) == 1) {
2484 item = PyList_GET_ITEM(obj, 0);
2485 if (save(self, item, 0) < 0)
2486 return -1;
2487 if (_Pickler_Write(self, &append_op, 1) < 0)
2488 return -1;
2489 return 0;
2490 }
2491
2492 /* Write in batches of BATCHSIZE. */
2493 total = 0;
2494 do {
2495 this_batch = 0;
2496 if (_Pickler_Write(self, &mark_op, 1) < 0)
2497 return -1;
2498 while (total < PyList_GET_SIZE(obj)) {
2499 item = PyList_GET_ITEM(obj, total);
2500 if (save(self, item, 0) < 0)
2501 return -1;
2502 total++;
2503 if (++this_batch == BATCHSIZE)
2504 break;
2505 }
2506 if (_Pickler_Write(self, &appends_op, 1) < 0)
2507 return -1;
2508
2509 } while (total < PyList_GET_SIZE(obj));
2510
2511 return 0;
2512}
2513
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002514static int
2515save_list(PicklerObject *self, PyObject *obj)
2516{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002517 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002518 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002519 int status = 0;
2520
2521 if (self->fast && !fast_save_enter(self, obj))
2522 goto error;
2523
2524 /* Create an empty list. */
2525 if (self->bin) {
2526 header[0] = EMPTY_LIST;
2527 len = 1;
2528 }
2529 else {
2530 header[0] = MARK;
2531 header[1] = LIST;
2532 len = 2;
2533 }
2534
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002535 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002536 goto error;
2537
2538 /* Get list length, and bow out early if empty. */
2539 if ((len = PyList_Size(obj)) < 0)
2540 goto error;
2541
2542 if (memo_put(self, obj) < 0)
2543 goto error;
2544
2545 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002546 /* Materialize the list elements. */
2547 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002548 if (Py_EnterRecursiveCall(" while pickling an object"))
2549 goto error;
2550 status = batch_list_exact(self, obj);
2551 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002552 } else {
2553 PyObject *iter = PyObject_GetIter(obj);
2554 if (iter == NULL)
2555 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002556
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002557 if (Py_EnterRecursiveCall(" while pickling an object")) {
2558 Py_DECREF(iter);
2559 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002560 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002561 status = batch_list(self, iter);
2562 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002563 Py_DECREF(iter);
2564 }
2565 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002566 if (0) {
2567 error:
2568 status = -1;
2569 }
2570
2571 if (self->fast && !fast_save_leave(self, obj))
2572 status = -1;
2573
2574 return status;
2575}
2576
2577/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2578 * MARK key value ... key value SETITEMS
2579 * opcode sequences. Calling code should have arranged to first create an
2580 * empty dict, or dict-like object, for the SETITEMS to operate on.
2581 * Returns 0 on success, <0 on error.
2582 *
2583 * This is very much like batch_list(). The difference between saving
2584 * elements directly, and picking apart two-tuples, is so long-winded at
2585 * the C level, though, that attempts to combine these routines were too
2586 * ugly to bear.
2587 */
2588static int
2589batch_dict(PicklerObject *self, PyObject *iter)
2590{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002591 PyObject *obj = NULL;
2592 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002593 int i, n;
2594
2595 const char mark_op = MARK;
2596 const char setitem_op = SETITEM;
2597 const char setitems_op = SETITEMS;
2598
2599 assert(iter != NULL);
2600
2601 if (self->proto == 0) {
2602 /* SETITEMS isn't available; do one at a time. */
2603 for (;;) {
2604 obj = PyIter_Next(iter);
2605 if (obj == NULL) {
2606 if (PyErr_Occurred())
2607 return -1;
2608 break;
2609 }
2610 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2611 PyErr_SetString(PyExc_TypeError, "dict items "
2612 "iterator must return 2-tuples");
2613 return -1;
2614 }
2615 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2616 if (i >= 0)
2617 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2618 Py_DECREF(obj);
2619 if (i < 0)
2620 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002621 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002622 return -1;
2623 }
2624 return 0;
2625 }
2626
2627 /* proto > 0: write in batches of BATCHSIZE. */
2628 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002629 /* Get first item */
2630 firstitem = PyIter_Next(iter);
2631 if (firstitem == NULL) {
2632 if (PyErr_Occurred())
2633 goto error;
2634
2635 /* nothing more to add */
2636 break;
2637 }
2638 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2639 PyErr_SetString(PyExc_TypeError, "dict items "
2640 "iterator must return 2-tuples");
2641 goto error;
2642 }
2643
2644 /* Try to get a second item */
2645 obj = PyIter_Next(iter);
2646 if (obj == NULL) {
2647 if (PyErr_Occurred())
2648 goto error;
2649
2650 /* Only one item to write */
2651 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2652 goto error;
2653 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2654 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002655 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002656 goto error;
2657 Py_CLEAR(firstitem);
2658 break;
2659 }
2660
2661 /* More than one item to write */
2662
2663 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002664 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002665 goto error;
2666
2667 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2668 goto error;
2669 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2670 goto error;
2671 Py_CLEAR(firstitem);
2672 n = 1;
2673
2674 /* Fetch and save up to BATCHSIZE items */
2675 while (obj) {
2676 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2677 PyErr_SetString(PyExc_TypeError, "dict items "
2678 "iterator must return 2-tuples");
2679 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002680 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002681 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2682 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2683 goto error;
2684 Py_CLEAR(obj);
2685 n += 1;
2686
2687 if (n == BATCHSIZE)
2688 break;
2689
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002690 obj = PyIter_Next(iter);
2691 if (obj == NULL) {
2692 if (PyErr_Occurred())
2693 goto error;
2694 break;
2695 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002696 }
2697
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002698 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002699 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002700
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002701 } while (n == BATCHSIZE);
2702 return 0;
2703
2704 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002705 Py_XDECREF(firstitem);
2706 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002707 return -1;
2708}
2709
Collin Winter5c9b02d2009-05-25 05:43:30 +00002710/* This is a variant of batch_dict() above that specializes for dicts, with no
2711 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2712 * MARK key value ... key value SETITEMS
2713 * opcode sequences. Calling code should have arranged to first create an
2714 * empty dict, or dict-like object, for the SETITEMS to operate on.
2715 * Returns 0 on success, -1 on error.
2716 *
2717 * Note that this currently doesn't work for protocol 0.
2718 */
2719static int
2720batch_dict_exact(PicklerObject *self, PyObject *obj)
2721{
2722 PyObject *key = NULL, *value = NULL;
2723 int i;
2724 Py_ssize_t dict_size, ppos = 0;
2725
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002726 const char mark_op = MARK;
2727 const char setitem_op = SETITEM;
2728 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002729
2730 assert(obj != NULL);
2731 assert(self->proto > 0);
2732
2733 dict_size = PyDict_Size(obj);
2734
2735 /* Special-case len(d) == 1 to save space. */
2736 if (dict_size == 1) {
2737 PyDict_Next(obj, &ppos, &key, &value);
2738 if (save(self, key, 0) < 0)
2739 return -1;
2740 if (save(self, value, 0) < 0)
2741 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002742 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002743 return -1;
2744 return 0;
2745 }
2746
2747 /* Write in batches of BATCHSIZE. */
2748 do {
2749 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002750 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002751 return -1;
2752 while (PyDict_Next(obj, &ppos, &key, &value)) {
2753 if (save(self, key, 0) < 0)
2754 return -1;
2755 if (save(self, value, 0) < 0)
2756 return -1;
2757 if (++i == BATCHSIZE)
2758 break;
2759 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002760 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002761 return -1;
2762 if (PyDict_Size(obj) != dict_size) {
2763 PyErr_Format(
2764 PyExc_RuntimeError,
2765 "dictionary changed size during iteration");
2766 return -1;
2767 }
2768
2769 } while (i == BATCHSIZE);
2770 return 0;
2771}
2772
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002773static int
2774save_dict(PicklerObject *self, PyObject *obj)
2775{
2776 PyObject *items, *iter;
2777 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002778 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002779 int status = 0;
2780
2781 if (self->fast && !fast_save_enter(self, obj))
2782 goto error;
2783
2784 /* Create an empty dict. */
2785 if (self->bin) {
2786 header[0] = EMPTY_DICT;
2787 len = 1;
2788 }
2789 else {
2790 header[0] = MARK;
2791 header[1] = DICT;
2792 len = 2;
2793 }
2794
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002795 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002796 goto error;
2797
2798 /* Get dict size, and bow out early if empty. */
2799 if ((len = PyDict_Size(obj)) < 0)
2800 goto error;
2801
2802 if (memo_put(self, obj) < 0)
2803 goto error;
2804
2805 if (len != 0) {
2806 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002807 if (PyDict_CheckExact(obj) && self->proto > 0) {
2808 /* We can take certain shortcuts if we know this is a dict and
2809 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002810 if (Py_EnterRecursiveCall(" while pickling an object"))
2811 goto error;
2812 status = batch_dict_exact(self, obj);
2813 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002814 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002815 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002816
2817 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002818 if (items == NULL)
2819 goto error;
2820 iter = PyObject_GetIter(items);
2821 Py_DECREF(items);
2822 if (iter == NULL)
2823 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002824 if (Py_EnterRecursiveCall(" while pickling an object")) {
2825 Py_DECREF(iter);
2826 goto error;
2827 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002828 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002829 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002830 Py_DECREF(iter);
2831 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002832 }
2833
2834 if (0) {
2835 error:
2836 status = -1;
2837 }
2838
2839 if (self->fast && !fast_save_leave(self, obj))
2840 status = -1;
2841
2842 return status;
2843}
2844
2845static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002846save_set(PicklerObject *self, PyObject *obj)
2847{
2848 PyObject *item;
2849 int i;
2850 Py_ssize_t set_size, ppos = 0;
2851 Py_hash_t hash;
2852
2853 const char empty_set_op = EMPTY_SET;
2854 const char mark_op = MARK;
2855 const char additems_op = ADDITEMS;
2856
2857 if (self->proto < 4) {
2858 PyObject *items;
2859 PyObject *reduce_value;
2860 int status;
2861
2862 items = PySequence_List(obj);
2863 if (items == NULL) {
2864 return -1;
2865 }
2866 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2867 Py_DECREF(items);
2868 if (reduce_value == NULL) {
2869 return -1;
2870 }
2871 /* save_reduce() will memoize the object automatically. */
2872 status = save_reduce(self, reduce_value, obj);
2873 Py_DECREF(reduce_value);
2874 return status;
2875 }
2876
2877 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2878 return -1;
2879
2880 if (memo_put(self, obj) < 0)
2881 return -1;
2882
2883 set_size = PySet_GET_SIZE(obj);
2884 if (set_size == 0)
2885 return 0; /* nothing to do */
2886
2887 /* Write in batches of BATCHSIZE. */
2888 do {
2889 i = 0;
2890 if (_Pickler_Write(self, &mark_op, 1) < 0)
2891 return -1;
2892 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2893 if (save(self, item, 0) < 0)
2894 return -1;
2895 if (++i == BATCHSIZE)
2896 break;
2897 }
2898 if (_Pickler_Write(self, &additems_op, 1) < 0)
2899 return -1;
2900 if (PySet_GET_SIZE(obj) != set_size) {
2901 PyErr_Format(
2902 PyExc_RuntimeError,
2903 "set changed size during iteration");
2904 return -1;
2905 }
2906 } while (i == BATCHSIZE);
2907
2908 return 0;
2909}
2910
2911static int
2912save_frozenset(PicklerObject *self, PyObject *obj)
2913{
2914 PyObject *iter;
2915
2916 const char mark_op = MARK;
2917 const char frozenset_op = FROZENSET;
2918
2919 if (self->fast && !fast_save_enter(self, obj))
2920 return -1;
2921
2922 if (self->proto < 4) {
2923 PyObject *items;
2924 PyObject *reduce_value;
2925 int status;
2926
2927 items = PySequence_List(obj);
2928 if (items == NULL) {
2929 return -1;
2930 }
2931 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2932 items);
2933 Py_DECREF(items);
2934 if (reduce_value == NULL) {
2935 return -1;
2936 }
2937 /* save_reduce() will memoize the object automatically. */
2938 status = save_reduce(self, reduce_value, obj);
2939 Py_DECREF(reduce_value);
2940 return status;
2941 }
2942
2943 if (_Pickler_Write(self, &mark_op, 1) < 0)
2944 return -1;
2945
2946 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002947 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01002948 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002949 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002950 for (;;) {
2951 PyObject *item;
2952
2953 item = PyIter_Next(iter);
2954 if (item == NULL) {
2955 if (PyErr_Occurred()) {
2956 Py_DECREF(iter);
2957 return -1;
2958 }
2959 break;
2960 }
2961 if (save(self, item, 0) < 0) {
2962 Py_DECREF(item);
2963 Py_DECREF(iter);
2964 return -1;
2965 }
2966 Py_DECREF(item);
2967 }
2968 Py_DECREF(iter);
2969
2970 /* If the object is already in the memo, this means it is
2971 recursive. In this case, throw away everything we put on the
2972 stack, and fetch the object back from the memo. */
2973 if (PyMemoTable_Get(self->memo, obj)) {
2974 const char pop_mark_op = POP_MARK;
2975
2976 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2977 return -1;
2978 if (memo_get(self, obj) < 0)
2979 return -1;
2980 return 0;
2981 }
2982
2983 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
2984 return -1;
2985 if (memo_put(self, obj) < 0)
2986 return -1;
2987
2988 return 0;
2989}
2990
2991static int
2992fix_imports(PyObject **module_name, PyObject **global_name)
2993{
2994 PyObject *key;
2995 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002996 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002997
2998 key = PyTuple_Pack(2, *module_name, *global_name);
2999 if (key == NULL)
3000 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003001 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003002 Py_DECREF(key);
3003 if (item) {
3004 PyObject *fixed_module_name;
3005 PyObject *fixed_global_name;
3006
3007 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3008 PyErr_Format(PyExc_RuntimeError,
3009 "_compat_pickle.REVERSE_NAME_MAPPING values "
3010 "should be 2-tuples, not %.200s",
3011 Py_TYPE(item)->tp_name);
3012 return -1;
3013 }
3014 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3015 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3016 if (!PyUnicode_Check(fixed_module_name) ||
3017 !PyUnicode_Check(fixed_global_name)) {
3018 PyErr_Format(PyExc_RuntimeError,
3019 "_compat_pickle.REVERSE_NAME_MAPPING values "
3020 "should be pairs of str, not (%.200s, %.200s)",
3021 Py_TYPE(fixed_module_name)->tp_name,
3022 Py_TYPE(fixed_global_name)->tp_name);
3023 return -1;
3024 }
3025
3026 Py_CLEAR(*module_name);
3027 Py_CLEAR(*global_name);
3028 Py_INCREF(fixed_module_name);
3029 Py_INCREF(fixed_global_name);
3030 *module_name = fixed_module_name;
3031 *global_name = fixed_global_name;
3032 }
3033 else if (PyErr_Occurred()) {
3034 return -1;
3035 }
3036
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003037 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003038 if (item) {
3039 if (!PyUnicode_Check(item)) {
3040 PyErr_Format(PyExc_RuntimeError,
3041 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3042 "should be strings, not %.200s",
3043 Py_TYPE(item)->tp_name);
3044 return -1;
3045 }
3046 Py_CLEAR(*module_name);
3047 Py_INCREF(item);
3048 *module_name = item;
3049 }
3050 else if (PyErr_Occurred()) {
3051 return -1;
3052 }
3053
3054 return 0;
3055}
3056
3057static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003058save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3059{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003060 PyObject *global_name = NULL;
3061 PyObject *module_name = NULL;
3062 PyObject *module = NULL;
3063 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003064 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003065 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003066 _Py_IDENTIFIER(__name__);
3067 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003068
3069 const char global_op = GLOBAL;
3070
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003071 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003072 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003073 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003074 }
3075 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003076 if (self->proto >= 4) {
3077 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3078 if (global_name == NULL) {
3079 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3080 goto error;
3081 PyErr_Clear();
3082 }
3083 }
3084 if (global_name == NULL) {
3085 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3086 if (global_name == NULL)
3087 goto error;
3088 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003089 }
3090
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003091 module_name = whichmodule(obj, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003092 if (module_name == NULL)
3093 goto error;
3094
3095 /* XXX: Change to use the import C API directly with level=0 to disallow
3096 relative imports.
3097
3098 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3099 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3100 custom import functions (IMHO, this would be a nice security
3101 feature). The import C API would need to be extended to support the
3102 extra parameters of __import__ to fix that. */
3103 module = PyImport_Import(module_name);
3104 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003105 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003106 "Can't pickle %R: import of module %R failed",
3107 obj, module_name);
3108 goto error;
3109 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003110 cls = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003111 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003112 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003113 "Can't pickle %R: attribute lookup %S on %S failed",
3114 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003115 goto error;
3116 }
3117 if (cls != obj) {
3118 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003119 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003120 "Can't pickle %R: it's not the same object as %S.%S",
3121 obj, module_name, global_name);
3122 goto error;
3123 }
3124 Py_DECREF(cls);
3125
3126 if (self->proto >= 2) {
3127 /* See whether this is in the extension registry, and if
3128 * so generate an EXT opcode.
3129 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003130 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003131 PyObject *code_obj; /* extension code as Python object */
3132 long code; /* extension code as C value */
3133 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003134 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003135
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003136 extension_key = PyTuple_Pack(2, module_name, global_name);
3137 if (extension_key == NULL) {
3138 goto error;
3139 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003140 code_obj = PyDict_GetItemWithError(st->extension_registry,
3141 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003142 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003143 /* The object is not registered in the extension registry.
3144 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003145 if (code_obj == NULL) {
3146 if (PyErr_Occurred()) {
3147 goto error;
3148 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003149 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003150 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003151
3152 /* XXX: pickle.py doesn't check neither the type, nor the range
3153 of the value returned by the extension_registry. It should for
3154 consistency. */
3155
3156 /* Verify code_obj has the right type and value. */
3157 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003158 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003159 "Can't pickle %R: extension code %R isn't an integer",
3160 obj, code_obj);
3161 goto error;
3162 }
3163 code = PyLong_AS_LONG(code_obj);
3164 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003165 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003166 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3167 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003168 goto error;
3169 }
3170
3171 /* Generate an EXT opcode. */
3172 if (code <= 0xff) {
3173 pdata[0] = EXT1;
3174 pdata[1] = (unsigned char)code;
3175 n = 2;
3176 }
3177 else if (code <= 0xffff) {
3178 pdata[0] = EXT2;
3179 pdata[1] = (unsigned char)(code & 0xff);
3180 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3181 n = 3;
3182 }
3183 else {
3184 pdata[0] = EXT4;
3185 pdata[1] = (unsigned char)(code & 0xff);
3186 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3187 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3188 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3189 n = 5;
3190 }
3191
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003192 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003193 goto error;
3194 }
3195 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003196 gen_global:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003197 if (self->proto >= 4) {
3198 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003199
Christian Heimese8b1ba12013-11-23 21:13:39 +01003200 if (save(self, module_name, 0) < 0)
3201 goto error;
3202 if (save(self, global_name, 0) < 0)
3203 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003204
3205 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3206 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003207 }
3208 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003209 /* Generate a normal global opcode if we are using a pickle
3210 protocol < 4, or if the object is not registered in the
3211 extension registry. */
3212 PyObject *encoded;
3213 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003214
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003215 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003216 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003217
3218 /* For protocol < 3 and if the user didn't request against doing
3219 so, we convert module names to the old 2.x module names. */
3220 if (self->proto < 3 && self->fix_imports) {
3221 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003222 goto error;
3223 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003224 }
3225
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003226 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3227 both the module name and the global name using UTF-8. We do so
3228 only when we are using the pickle protocol newer than version
3229 3. This is to ensure compatibility with older Unpickler running
3230 on Python 2.x. */
3231 if (self->proto == 3) {
3232 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003233 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003234 else {
3235 unicode_encoder = PyUnicode_AsASCIIString;
3236 }
3237 encoded = unicode_encoder(module_name);
3238 if (encoded == NULL) {
3239 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003240 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003241 "can't pickle module identifier '%S' using "
3242 "pickle protocol %i",
3243 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003244 goto error;
3245 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003246 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3247 PyBytes_GET_SIZE(encoded)) < 0) {
3248 Py_DECREF(encoded);
3249 goto error;
3250 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003251 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003252 if(_Pickler_Write(self, "\n", 1) < 0)
3253 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003254
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003255 /* Save the name of the module. */
3256 encoded = unicode_encoder(global_name);
3257 if (encoded == NULL) {
3258 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003259 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003260 "can't pickle global identifier '%S' using "
3261 "pickle protocol %i",
3262 global_name, self->proto);
3263 goto error;
3264 }
3265 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3266 PyBytes_GET_SIZE(encoded)) < 0) {
3267 Py_DECREF(encoded);
3268 goto error;
3269 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003270 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003271 if (_Pickler_Write(self, "\n", 1) < 0)
3272 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003273 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003274 /* Memoize the object. */
3275 if (memo_put(self, obj) < 0)
3276 goto error;
3277 }
3278
3279 if (0) {
3280 error:
3281 status = -1;
3282 }
3283 Py_XDECREF(module_name);
3284 Py_XDECREF(global_name);
3285 Py_XDECREF(module);
3286
3287 return status;
3288}
3289
3290static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003291save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3292{
3293 PyObject *reduce_value;
3294 int status;
3295
3296 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3297 if (reduce_value == NULL) {
3298 return -1;
3299 }
3300 status = save_reduce(self, reduce_value, obj);
3301 Py_DECREF(reduce_value);
3302 return status;
3303}
3304
3305static int
3306save_type(PicklerObject *self, PyObject *obj)
3307{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003308 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003309 return save_singleton_type(self, obj, Py_None);
3310 }
3311 else if (obj == (PyObject *)&PyEllipsis_Type) {
3312 return save_singleton_type(self, obj, Py_Ellipsis);
3313 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003314 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003315 return save_singleton_type(self, obj, Py_NotImplemented);
3316 }
3317 return save_global(self, obj, NULL);
3318}
3319
3320static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003321save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3322{
3323 PyObject *pid = NULL;
3324 int status = 0;
3325
3326 const char persid_op = PERSID;
3327 const char binpersid_op = BINPERSID;
3328
3329 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003330 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003331 if (pid == NULL)
3332 return -1;
3333
3334 if (pid != Py_None) {
3335 if (self->bin) {
3336 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003337 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003338 goto error;
3339 }
3340 else {
3341 PyObject *pid_str = NULL;
3342 char *pid_ascii_bytes;
3343 Py_ssize_t size;
3344
3345 pid_str = PyObject_Str(pid);
3346 if (pid_str == NULL)
3347 goto error;
3348
3349 /* XXX: Should it check whether the persistent id only contains
3350 ASCII characters? And what if the pid contains embedded
3351 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003352 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003353 Py_DECREF(pid_str);
3354 if (pid_ascii_bytes == NULL)
3355 goto error;
3356
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003357 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3358 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3359 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003360 goto error;
3361 }
3362 status = 1;
3363 }
3364
3365 if (0) {
3366 error:
3367 status = -1;
3368 }
3369 Py_XDECREF(pid);
3370
3371 return status;
3372}
3373
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003374static PyObject *
3375get_class(PyObject *obj)
3376{
3377 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003378 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003379
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003380 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003381 if (cls == NULL) {
3382 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3383 PyErr_Clear();
3384 cls = (PyObject *) Py_TYPE(obj);
3385 Py_INCREF(cls);
3386 }
3387 }
3388 return cls;
3389}
3390
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003391/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3392 * appropriate __reduce__ method for obj.
3393 */
3394static int
3395save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3396{
3397 PyObject *callable;
3398 PyObject *argtup;
3399 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003400 PyObject *listitems = Py_None;
3401 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003402 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003403 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003404 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003405
3406 const char reduce_op = REDUCE;
3407 const char build_op = BUILD;
3408 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003409 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003410
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003411 size = PyTuple_Size(args);
3412 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003413 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003414 "__reduce__ must contain 2 through 5 elements");
3415 return -1;
3416 }
3417
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003418 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3419 &callable, &argtup, &state, &listitems, &dictitems))
3420 return -1;
3421
3422 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003423 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003424 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003425 return -1;
3426 }
3427 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003428 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003429 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003430 return -1;
3431 }
3432
3433 if (state == Py_None)
3434 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003435
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003436 if (listitems == Py_None)
3437 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003438 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003439 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003440 "returned by __reduce__ must be an iterator, not %s",
3441 Py_TYPE(listitems)->tp_name);
3442 return -1;
3443 }
3444
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003445 if (dictitems == Py_None)
3446 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003447 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003448 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003449 "returned by __reduce__ must be an iterator, not %s",
3450 Py_TYPE(dictitems)->tp_name);
3451 return -1;
3452 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003453
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003454 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003455 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003456 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003457
Victor Stinner804e05e2013-11-14 01:26:17 +01003458 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003459 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003460 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003461 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003462 }
3463 PyErr_Clear();
3464 }
3465 else if (self->proto >= 4) {
3466 _Py_IDENTIFIER(__newobj_ex__);
3467 use_newobj_ex = PyUnicode_Check(name) &&
3468 PyUnicode_Compare(
3469 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
3470 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003471 }
3472 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003473 _Py_IDENTIFIER(__newobj__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003474 use_newobj = PyUnicode_Check(name) &&
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003475 PyUnicode_Compare(
3476 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003477 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003478 }
3479 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003480
3481 if (use_newobj_ex) {
3482 PyObject *cls;
3483 PyObject *args;
3484 PyObject *kwargs;
3485
3486 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003487 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003488 "length of the NEWOBJ_EX argument tuple must be "
3489 "exactly 3, not %zd", Py_SIZE(argtup));
3490 return -1;
3491 }
3492
3493 cls = PyTuple_GET_ITEM(argtup, 0);
3494 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003495 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003496 "first item from NEWOBJ_EX argument tuple must "
3497 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3498 return -1;
3499 }
3500 args = PyTuple_GET_ITEM(argtup, 1);
3501 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003502 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003503 "second item from NEWOBJ_EX argument tuple must "
3504 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3505 return -1;
3506 }
3507 kwargs = PyTuple_GET_ITEM(argtup, 2);
3508 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003509 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003510 "third item from NEWOBJ_EX argument tuple must "
3511 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3512 return -1;
3513 }
3514
3515 if (save(self, cls, 0) < 0 ||
3516 save(self, args, 0) < 0 ||
3517 save(self, kwargs, 0) < 0 ||
3518 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3519 return -1;
3520 }
3521 }
3522 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003523 PyObject *cls;
3524 PyObject *newargtup;
3525 PyObject *obj_class;
3526 int p;
3527
3528 /* Sanity checks. */
3529 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003530 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003531 return -1;
3532 }
3533
3534 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003535 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003536 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003537 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003538 return -1;
3539 }
3540
3541 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003542 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003543 p = obj_class != cls; /* true iff a problem */
3544 Py_DECREF(obj_class);
3545 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003546 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003547 "__newobj__ args has the wrong class");
3548 return -1;
3549 }
3550 }
3551 /* XXX: These calls save() are prone to infinite recursion. Imagine
3552 what happen if the value returned by the __reduce__() method of
3553 some extension type contains another object of the same type. Ouch!
3554
3555 Here is a quick example, that I ran into, to illustrate what I
3556 mean:
3557
3558 >>> import pickle, copyreg
3559 >>> copyreg.dispatch_table.pop(complex)
3560 >>> pickle.dumps(1+2j)
3561 Traceback (most recent call last):
3562 ...
3563 RuntimeError: maximum recursion depth exceeded
3564
3565 Removing the complex class from copyreg.dispatch_table made the
3566 __reduce_ex__() method emit another complex object:
3567
3568 >>> (1+1j).__reduce_ex__(2)
3569 (<function __newobj__ at 0xb7b71c3c>,
3570 (<class 'complex'>, (1+1j)), None, None, None)
3571
3572 Thus when save() was called on newargstup (the 2nd item) recursion
3573 ensued. Of course, the bug was in the complex class which had a
3574 broken __getnewargs__() that emitted another complex object. But,
3575 the point, here, is it is quite easy to end up with a broken reduce
3576 function. */
3577
3578 /* Save the class and its __new__ arguments. */
3579 if (save(self, cls, 0) < 0)
3580 return -1;
3581
3582 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3583 if (newargtup == NULL)
3584 return -1;
3585
3586 p = save(self, newargtup, 0);
3587 Py_DECREF(newargtup);
3588 if (p < 0)
3589 return -1;
3590
3591 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003592 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003593 return -1;
3594 }
3595 else { /* Not using NEWOBJ. */
3596 if (save(self, callable, 0) < 0 ||
3597 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003598 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003599 return -1;
3600 }
3601
3602 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3603 the caller do not want to memoize the object. Not particularly useful,
3604 but that is to mimic the behavior save_reduce() in pickle.py when
3605 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003606 if (obj != NULL) {
3607 /* If the object is already in the memo, this means it is
3608 recursive. In this case, throw away everything we put on the
3609 stack, and fetch the object back from the memo. */
3610 if (PyMemoTable_Get(self->memo, obj)) {
3611 const char pop_op = POP;
3612
3613 if (_Pickler_Write(self, &pop_op, 1) < 0)
3614 return -1;
3615 if (memo_get(self, obj) < 0)
3616 return -1;
3617
3618 return 0;
3619 }
3620 else if (memo_put(self, obj) < 0)
3621 return -1;
3622 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003623
3624 if (listitems && batch_list(self, listitems) < 0)
3625 return -1;
3626
3627 if (dictitems && batch_dict(self, dictitems) < 0)
3628 return -1;
3629
3630 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003631 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003632 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003633 return -1;
3634 }
3635
3636 return 0;
3637}
3638
3639static int
3640save(PicklerObject *self, PyObject *obj, int pers_save)
3641{
3642 PyTypeObject *type;
3643 PyObject *reduce_func = NULL;
3644 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003645 int status = 0;
3646
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003647 if (_Pickler_OpcodeBoundary(self) < 0)
3648 return -1;
3649
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003650 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003651 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003652
3653 /* The extra pers_save argument is necessary to avoid calling save_pers()
3654 on its returned object. */
3655 if (!pers_save && self->pers_func) {
3656 /* save_pers() returns:
3657 -1 to signal an error;
3658 0 if it did nothing successfully;
3659 1 if a persistent id was saved.
3660 */
3661 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3662 goto done;
3663 }
3664
3665 type = Py_TYPE(obj);
3666
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003667 /* The old cPickle had an optimization that used switch-case statement
3668 dispatching on the first letter of the type name. This has was removed
3669 since benchmarks shown that this optimization was actually slowing
3670 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003671
3672 /* Atom types; these aren't memoized, so don't check the memo. */
3673
3674 if (obj == Py_None) {
3675 status = save_none(self, obj);
3676 goto done;
3677 }
3678 else if (obj == Py_False || obj == Py_True) {
3679 status = save_bool(self, obj);
3680 goto done;
3681 }
3682 else if (type == &PyLong_Type) {
3683 status = save_long(self, obj);
3684 goto done;
3685 }
3686 else if (type == &PyFloat_Type) {
3687 status = save_float(self, obj);
3688 goto done;
3689 }
3690
3691 /* Check the memo to see if it has the object. If so, generate
3692 a GET (or BINGET) opcode, instead of pickling the object
3693 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003694 if (PyMemoTable_Get(self->memo, obj)) {
3695 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003696 goto error;
3697 goto done;
3698 }
3699
3700 if (type == &PyBytes_Type) {
3701 status = save_bytes(self, obj);
3702 goto done;
3703 }
3704 else if (type == &PyUnicode_Type) {
3705 status = save_unicode(self, obj);
3706 goto done;
3707 }
3708 else if (type == &PyDict_Type) {
3709 status = save_dict(self, obj);
3710 goto done;
3711 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003712 else if (type == &PySet_Type) {
3713 status = save_set(self, obj);
3714 goto done;
3715 }
3716 else if (type == &PyFrozenSet_Type) {
3717 status = save_frozenset(self, obj);
3718 goto done;
3719 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003720 else if (type == &PyList_Type) {
3721 status = save_list(self, obj);
3722 goto done;
3723 }
3724 else if (type == &PyTuple_Type) {
3725 status = save_tuple(self, obj);
3726 goto done;
3727 }
3728 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003729 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003730 goto done;
3731 }
3732 else if (type == &PyFunction_Type) {
3733 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003734 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003735 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003736
3737 /* XXX: This part needs some unit tests. */
3738
3739 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003740 * self.dispatch_table, copyreg.dispatch_table, the object's
3741 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003742 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003743 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003744 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003745 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3746 (PyObject *)type);
3747 if (reduce_func == NULL) {
3748 if (PyErr_Occurred()) {
3749 goto error;
3750 }
3751 } else {
3752 /* PyDict_GetItemWithError() returns a borrowed reference.
3753 Increase the reference count to be consistent with
3754 PyObject_GetItem and _PyObject_GetAttrId used below. */
3755 Py_INCREF(reduce_func);
3756 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003757 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003758 reduce_func = PyObject_GetItem(self->dispatch_table,
3759 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003760 if (reduce_func == NULL) {
3761 if (PyErr_ExceptionMatches(PyExc_KeyError))
3762 PyErr_Clear();
3763 else
3764 goto error;
3765 }
3766 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003767 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003768 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003769 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003770 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003771 else if (PyType_IsSubtype(type, &PyType_Type)) {
3772 status = save_global(self, obj, NULL);
3773 goto done;
3774 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003775 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003776 _Py_IDENTIFIER(__reduce__);
3777 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003778
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003779
3780 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3781 automatically defined as __reduce__. While this is convenient, this
3782 make it impossible to know which method was actually called. Of
3783 course, this is not a big deal. But still, it would be nice to let
3784 the user know which method was called when something go
3785 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3786 don't actually have to check for a __reduce__ method. */
3787
3788 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003789 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003790 if (reduce_func != NULL) {
3791 PyObject *proto;
3792 proto = PyLong_FromLong(self->proto);
3793 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003794 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003795 }
3796 }
3797 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003798 PickleState *st = _Pickle_GetGlobalState();
3799
3800 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003801 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003802 }
3803 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003804 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003805 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003806 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003807 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003808 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003809 PyObject *empty_tuple = PyTuple_New(0);
3810 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003811 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003812 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003813 }
3814 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003815 PyErr_Format(st->PicklingError,
3816 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003817 type->tp_name, obj);
3818 goto error;
3819 }
3820 }
3821 }
3822
3823 if (reduce_value == NULL)
3824 goto error;
3825
3826 if (PyUnicode_Check(reduce_value)) {
3827 status = save_global(self, obj, reduce_value);
3828 goto done;
3829 }
3830
3831 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003832 PickleState *st = _Pickle_GetGlobalState();
3833 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003834 "__reduce__ must return a string or tuple");
3835 goto error;
3836 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003837
3838 status = save_reduce(self, reduce_value, obj);
3839
3840 if (0) {
3841 error:
3842 status = -1;
3843 }
3844 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003845
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003846 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003847 Py_XDECREF(reduce_func);
3848 Py_XDECREF(reduce_value);
3849
3850 return status;
3851}
3852
3853static int
3854dump(PicklerObject *self, PyObject *obj)
3855{
3856 const char stop_op = STOP;
3857
3858 if (self->proto >= 2) {
3859 char header[2];
3860
3861 header[0] = PROTO;
3862 assert(self->proto >= 0 && self->proto < 256);
3863 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003864 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003865 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003866 if (self->proto >= 4)
3867 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003868 }
3869
3870 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003871 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003872 return -1;
3873
3874 return 0;
3875}
3876
Larry Hastings61272b72014-01-07 12:41:53 -08003877/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003878
3879_pickle.Pickler.clear_memo
3880
3881 self: PicklerObject
3882
3883Clears the pickler's "memo".
3884
3885The memo is the data structure that remembers which objects the
3886pickler has already seen, so that shared or recursive objects are
3887pickled by reference and not by value. This method is useful when
3888re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08003889[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003890
3891PyDoc_STRVAR(_pickle_Pickler_clear_memo__doc__,
3892"clear_memo()\n"
3893"Clears the pickler\'s \"memo\".\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003894"\n"
3895"The memo is the data structure that remembers which objects the\n"
3896"pickler has already seen, so that shared or recursive objects are\n"
3897"pickled by reference and not by value. This method is useful when\n"
3898"re-using picklers.");
3899
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003900#define _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF \
3901 {"clear_memo", (PyCFunction)_pickle_Pickler_clear_memo, METH_NOARGS, _pickle_Pickler_clear_memo__doc__},
3902
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003903static PyObject *
Larry Hastings3cceb382014-01-04 11:09:09 -08003904_pickle_Pickler_clear_memo_impl(PicklerObject *self);
3905
3906static PyObject *
3907_pickle_Pickler_clear_memo(PyObject *self, PyObject *Py_UNUSED(ignored))
3908{
3909 PyObject *return_value = NULL;
3910
3911 return_value = _pickle_Pickler_clear_memo_impl((PicklerObject *)self);
3912
3913 return return_value;
3914}
3915
3916static PyObject *
3917_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings61272b72014-01-07 12:41:53 -08003918/*[clinic end generated code: checksum=0574593b102fffb8e781d7bb9b536ceffc525ac1]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003919{
3920 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003921 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003922
3923 Py_RETURN_NONE;
3924}
3925
Larry Hastings61272b72014-01-07 12:41:53 -08003926/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003927
3928_pickle.Pickler.dump
3929
3930 self: PicklerObject
3931 obj: object
3932 /
3933
3934Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08003935[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003936
3937PyDoc_STRVAR(_pickle_Pickler_dump__doc__,
3938"dump(obj)\n"
3939"Write a pickled representation of the given object to the open file.");
3940
3941#define _PICKLE_PICKLER_DUMP_METHODDEF \
3942 {"dump", (PyCFunction)_pickle_Pickler_dump, METH_O, _pickle_Pickler_dump__doc__},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003943
3944static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003945_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings61272b72014-01-07 12:41:53 -08003946/*[clinic end generated code: checksum=b72a69ec98737fabf66dae7c5a3210178bdbd3e6]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003947{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003948 /* Check whether the Pickler was initialized correctly (issue3664).
3949 Developers often forget to call __init__() in their subclasses, which
3950 would trigger a segfault without this check. */
3951 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003952 PickleState *st = _Pickle_GetGlobalState();
3953 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003954 "Pickler.__init__() was not called by %s.__init__()",
3955 Py_TYPE(self)->tp_name);
3956 return NULL;
3957 }
3958
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003959 if (_Pickler_ClearBuffer(self) < 0)
3960 return NULL;
3961
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003962 if (dump(self, obj) < 0)
3963 return NULL;
3964
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003965 if (_Pickler_FlushToFile(self) < 0)
3966 return NULL;
3967
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003968 Py_RETURN_NONE;
3969}
3970
3971static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003972 _PICKLE_PICKLER_DUMP_METHODDEF
3973 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003974 {NULL, NULL} /* sentinel */
3975};
3976
3977static void
3978Pickler_dealloc(PicklerObject *self)
3979{
3980 PyObject_GC_UnTrack(self);
3981
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003982 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003983 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003984 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003985 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003986 Py_XDECREF(self->fast_memo);
3987
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003988 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003989
3990 Py_TYPE(self)->tp_free((PyObject *)self);
3991}
3992
3993static int
3994Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
3995{
3996 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003997 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003998 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003999 Py_VISIT(self->fast_memo);
4000 return 0;
4001}
4002
4003static int
4004Pickler_clear(PicklerObject *self)
4005{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004006 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004007 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004008 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004009 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004010 Py_CLEAR(self->fast_memo);
4011
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004012 if (self->memo != NULL) {
4013 PyMemoTable *memo = self->memo;
4014 self->memo = NULL;
4015 PyMemoTable_Del(memo);
4016 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004017 return 0;
4018}
4019
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004020
Larry Hastings61272b72014-01-07 12:41:53 -08004021/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004022
4023_pickle.Pickler.__init__
4024
4025 self: PicklerObject
4026 file: object
4027 protocol: object = NULL
4028 fix_imports: bool = True
4029
4030This takes a binary file for writing a pickle data stream.
4031
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004032The optional *protocol* argument tells the pickler to use the given
4033protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4034protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004035
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004036Specifying a negative protocol version selects the highest protocol
4037version supported. The higher the protocol used, the more recent the
4038version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004039
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004040The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004041bytes argument. It can thus be a file object opened for binary
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004042writing, a io.BytesIO instance, or any other custom object that meets
4043this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004044
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004045If *fix_imports* is True and protocol is less than 3, pickle will try
4046to map the new Python 3 names to the old module names used in Python
40472, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004048[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004049
4050PyDoc_STRVAR(_pickle_Pickler___init____doc__,
4051"__init__(file, protocol=None, fix_imports=True)\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004052"This takes a binary file for writing a pickle data stream.\n"
4053"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004054"The optional *protocol* argument tells the pickler to use the given\n"
4055"protocol; supported protocols are 0, 1, 2, 3 and 4. The default\n"
4056"protocol is 3; a backward-incompatible protocol designed for Python 3.\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004057"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004058"Specifying a negative protocol version selects the highest protocol\n"
4059"version supported. The higher the protocol used, the more recent the\n"
4060"version of Python needed to read the pickle produced.\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004061"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004062"The *file* argument must have a write() method that accepts a single\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004063"bytes argument. It can thus be a file object opened for binary\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004064"writing, a io.BytesIO instance, or any other custom object that meets\n"
4065"this interface.\n"
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004066"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004067"If *fix_imports* is True and protocol is less than 3, pickle will try\n"
4068"to map the new Python 3 names to the old module names used in Python\n"
4069"2, so that the pickle data stream is readable with Python 2.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004070
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004071static PyObject *
4072_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports);
4073
4074static PyObject *
4075_pickle_Pickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004076{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004077 PyObject *return_value = NULL;
4078 static char *_keywords[] = {"file", "protocol", "fix_imports", NULL};
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004079 PyObject *file;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004080 PyObject *protocol = NULL;
4081 int fix_imports = 1;
4082
4083 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
4084 "O|Op:__init__", _keywords,
4085 &file, &protocol, &fix_imports))
4086 goto exit;
4087 return_value = _pickle_Pickler___init___impl((PicklerObject *)self, file, protocol, fix_imports);
4088
4089exit:
4090 return return_value;
4091}
4092
4093static PyObject *
4094_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports)
Larry Hastings8666e652014-01-12 14:12:59 -08004095/*[clinic end generated code: checksum=defa3d9e9f8b51fb257d4fdfca99db503db0e6df]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004096{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004097 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004098 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004099
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004100 /* In case of multiple __init__() calls, clear previous content. */
4101 if (self->write != NULL)
4102 (void)Pickler_clear(self);
4103
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004104 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
4105 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004106
4107 if (_Pickler_SetOutputStream(self, file) < 0)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004108 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004109
4110 /* memo and output_buffer may have already been created in _Pickler_New */
4111 if (self->memo == NULL) {
4112 self->memo = PyMemoTable_New();
4113 if (self->memo == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004114 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004115 }
4116 self->output_len = 0;
4117 if (self->output_buffer == NULL) {
4118 self->max_output_len = WRITE_BUF_SIZE;
4119 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4120 self->max_output_len);
4121 if (self->output_buffer == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004122 return NULL;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004123 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004124
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004125 self->fast = 0;
4126 self->fast_nesting = 0;
4127 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004128 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004129 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4130 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4131 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004132 if (self->pers_func == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004133 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004134 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004135 self->dispatch_table = NULL;
4136 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4137 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4138 &PyId_dispatch_table);
4139 if (self->dispatch_table == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004140 return NULL;
4141 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004142
4143 Py_RETURN_NONE;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004144}
4145
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004146/* Wrap the Clinic generated signature to slot it in tp_init. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004147static int
4148Pickler_init(PyObject *self, PyObject *args, PyObject *kwargs)
4149{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004150 PyObject *result = _pickle_Pickler___init__(self, args, kwargs);
4151 if (result == NULL) {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004152 return -1;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004153 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004154 Py_DECREF(result);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004155 return 0;
4156}
4157
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004158/* Define a proxy object for the Pickler's internal memo object. This is to
4159 * avoid breaking code like:
4160 * pickler.memo.clear()
4161 * and
4162 * pickler.memo = saved_memo
4163 * Is this a good idea? Not really, but we don't want to break code that uses
4164 * it. Note that we don't implement the entire mapping API here. This is
4165 * intentional, as these should be treated as black-box implementation details.
4166 */
4167
4168typedef struct {
4169 PyObject_HEAD
4170 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
4171} PicklerMemoProxyObject;
4172
Larry Hastings61272b72014-01-07 12:41:53 -08004173/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004174_pickle.PicklerMemoProxy.clear
4175
4176 self: PicklerMemoProxyObject
4177
4178Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004179[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004180
4181PyDoc_STRVAR(_pickle_PicklerMemoProxy_clear__doc__,
4182"clear()\n"
4183"Remove all items from memo.");
4184
4185#define _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF \
4186 {"clear", (PyCFunction)_pickle_PicklerMemoProxy_clear, METH_NOARGS, _pickle_PicklerMemoProxy_clear__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004187
4188static PyObject *
Larry Hastings3cceb382014-01-04 11:09:09 -08004189_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self);
4190
4191static PyObject *
4192_pickle_PicklerMemoProxy_clear(PyObject *self, PyObject *Py_UNUSED(ignored))
4193{
4194 PyObject *return_value = NULL;
4195
4196 return_value = _pickle_PicklerMemoProxy_clear_impl((PicklerMemoProxyObject *)self);
4197
4198 return return_value;
4199}
4200
4201static PyObject *
4202_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings61272b72014-01-07 12:41:53 -08004203/*[clinic end generated code: checksum=c6ca252530ccb3ea2f4b33507b51b183f23b24c7]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004204{
4205 if (self->pickler->memo)
4206 PyMemoTable_Clear(self->pickler->memo);
4207 Py_RETURN_NONE;
4208}
4209
Larry Hastings61272b72014-01-07 12:41:53 -08004210/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004211_pickle.PicklerMemoProxy.copy
4212
4213 self: PicklerMemoProxyObject
4214
4215Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004216[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004217
4218PyDoc_STRVAR(_pickle_PicklerMemoProxy_copy__doc__,
4219"copy()\n"
4220"Copy the memo to a new object.");
4221
4222#define _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF \
4223 {"copy", (PyCFunction)_pickle_PicklerMemoProxy_copy, METH_NOARGS, _pickle_PicklerMemoProxy_copy__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004224
4225static PyObject *
Larry Hastings3cceb382014-01-04 11:09:09 -08004226_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self);
4227
4228static PyObject *
4229_pickle_PicklerMemoProxy_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
4230{
4231 PyObject *return_value = NULL;
4232
4233 return_value = _pickle_PicklerMemoProxy_copy_impl((PicklerMemoProxyObject *)self);
4234
4235 return return_value;
4236}
4237
4238static PyObject *
4239_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings61272b72014-01-07 12:41:53 -08004240/*[clinic end generated code: checksum=808c4d5a37359ed5fb2efe81dbe5ff480719f470]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004241{
4242 Py_ssize_t i;
4243 PyMemoTable *memo;
4244 PyObject *new_memo = PyDict_New();
4245 if (new_memo == NULL)
4246 return NULL;
4247
4248 memo = self->pickler->memo;
4249 for (i = 0; i < memo->mt_allocated; ++i) {
4250 PyMemoEntry entry = memo->mt_table[i];
4251 if (entry.me_key != NULL) {
4252 int status;
4253 PyObject *key, *value;
4254
4255 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004256 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004257
4258 if (key == NULL || value == NULL) {
4259 Py_XDECREF(key);
4260 Py_XDECREF(value);
4261 goto error;
4262 }
4263 status = PyDict_SetItem(new_memo, key, value);
4264 Py_DECREF(key);
4265 Py_DECREF(value);
4266 if (status < 0)
4267 goto error;
4268 }
4269 }
4270 return new_memo;
4271
4272 error:
4273 Py_XDECREF(new_memo);
4274 return NULL;
4275}
4276
Larry Hastings61272b72014-01-07 12:41:53 -08004277/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004278_pickle.PicklerMemoProxy.__reduce__
4279
4280 self: PicklerMemoProxyObject
4281
4282Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004283[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004284
4285PyDoc_STRVAR(_pickle_PicklerMemoProxy___reduce____doc__,
4286"__reduce__()\n"
4287"Implement pickle support.");
4288
4289#define _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF \
4290 {"__reduce__", (PyCFunction)_pickle_PicklerMemoProxy___reduce__, METH_NOARGS, _pickle_PicklerMemoProxy___reduce____doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004291
4292static PyObject *
Larry Hastings3cceb382014-01-04 11:09:09 -08004293_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self);
4294
4295static PyObject *
4296_pickle_PicklerMemoProxy___reduce__(PyObject *self, PyObject *Py_UNUSED(ignored))
4297{
4298 PyObject *return_value = NULL;
4299
4300 return_value = _pickle_PicklerMemoProxy___reduce___impl((PicklerMemoProxyObject *)self);
4301
4302 return return_value;
4303}
4304
4305static PyObject *
4306_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings61272b72014-01-07 12:41:53 -08004307/*[clinic end generated code: checksum=2293152bdf53951a012d430767b608f5fb4213b5]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004308{
4309 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004310 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004311 if (contents == NULL)
4312 return NULL;
4313
4314 reduce_value = PyTuple_New(2);
4315 if (reduce_value == NULL) {
4316 Py_DECREF(contents);
4317 return NULL;
4318 }
4319 dict_args = PyTuple_New(1);
4320 if (dict_args == NULL) {
4321 Py_DECREF(contents);
4322 Py_DECREF(reduce_value);
4323 return NULL;
4324 }
4325 PyTuple_SET_ITEM(dict_args, 0, contents);
4326 Py_INCREF((PyObject *)&PyDict_Type);
4327 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4328 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4329 return reduce_value;
4330}
4331
4332static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004333 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4334 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4335 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004336 {NULL, NULL} /* sentinel */
4337};
4338
4339static void
4340PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4341{
4342 PyObject_GC_UnTrack(self);
4343 Py_XDECREF(self->pickler);
4344 PyObject_GC_Del((PyObject *)self);
4345}
4346
4347static int
4348PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4349 visitproc visit, void *arg)
4350{
4351 Py_VISIT(self->pickler);
4352 return 0;
4353}
4354
4355static int
4356PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4357{
4358 Py_CLEAR(self->pickler);
4359 return 0;
4360}
4361
4362static PyTypeObject PicklerMemoProxyType = {
4363 PyVarObject_HEAD_INIT(NULL, 0)
4364 "_pickle.PicklerMemoProxy", /*tp_name*/
4365 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4366 0,
4367 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4368 0, /* tp_print */
4369 0, /* tp_getattr */
4370 0, /* tp_setattr */
4371 0, /* tp_compare */
4372 0, /* tp_repr */
4373 0, /* tp_as_number */
4374 0, /* tp_as_sequence */
4375 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004376 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004377 0, /* tp_call */
4378 0, /* tp_str */
4379 PyObject_GenericGetAttr, /* tp_getattro */
4380 PyObject_GenericSetAttr, /* tp_setattro */
4381 0, /* tp_as_buffer */
4382 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4383 0, /* tp_doc */
4384 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4385 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4386 0, /* tp_richcompare */
4387 0, /* tp_weaklistoffset */
4388 0, /* tp_iter */
4389 0, /* tp_iternext */
4390 picklerproxy_methods, /* tp_methods */
4391};
4392
4393static PyObject *
4394PicklerMemoProxy_New(PicklerObject *pickler)
4395{
4396 PicklerMemoProxyObject *self;
4397
4398 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4399 if (self == NULL)
4400 return NULL;
4401 Py_INCREF(pickler);
4402 self->pickler = pickler;
4403 PyObject_GC_Track(self);
4404 return (PyObject *)self;
4405}
4406
4407/*****************************************************************************/
4408
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004409static PyObject *
4410Pickler_get_memo(PicklerObject *self)
4411{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004412 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004413}
4414
4415static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004416Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004417{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004418 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004419
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004420 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004421 PyErr_SetString(PyExc_TypeError,
4422 "attribute deletion is not supported");
4423 return -1;
4424 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004425
4426 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4427 PicklerObject *pickler =
4428 ((PicklerMemoProxyObject *)obj)->pickler;
4429
4430 new_memo = PyMemoTable_Copy(pickler->memo);
4431 if (new_memo == NULL)
4432 return -1;
4433 }
4434 else if (PyDict_Check(obj)) {
4435 Py_ssize_t i = 0;
4436 PyObject *key, *value;
4437
4438 new_memo = PyMemoTable_New();
4439 if (new_memo == NULL)
4440 return -1;
4441
4442 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004443 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004444 PyObject *memo_obj;
4445
4446 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4447 PyErr_SetString(PyExc_TypeError,
4448 "'memo' values must be 2-item tuples");
4449 goto error;
4450 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004451 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004452 if (memo_id == -1 && PyErr_Occurred())
4453 goto error;
4454 memo_obj = PyTuple_GET_ITEM(value, 1);
4455 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4456 goto error;
4457 }
4458 }
4459 else {
4460 PyErr_Format(PyExc_TypeError,
4461 "'memo' attribute must be an PicklerMemoProxy object"
4462 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004463 return -1;
4464 }
4465
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004466 PyMemoTable_Del(self->memo);
4467 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004468
4469 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004470
4471 error:
4472 if (new_memo)
4473 PyMemoTable_Del(new_memo);
4474 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004475}
4476
4477static PyObject *
4478Pickler_get_persid(PicklerObject *self)
4479{
4480 if (self->pers_func == NULL)
4481 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4482 else
4483 Py_INCREF(self->pers_func);
4484 return self->pers_func;
4485}
4486
4487static int
4488Pickler_set_persid(PicklerObject *self, PyObject *value)
4489{
4490 PyObject *tmp;
4491
4492 if (value == NULL) {
4493 PyErr_SetString(PyExc_TypeError,
4494 "attribute deletion is not supported");
4495 return -1;
4496 }
4497 if (!PyCallable_Check(value)) {
4498 PyErr_SetString(PyExc_TypeError,
4499 "persistent_id must be a callable taking one argument");
4500 return -1;
4501 }
4502
4503 tmp = self->pers_func;
4504 Py_INCREF(value);
4505 self->pers_func = value;
4506 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4507
4508 return 0;
4509}
4510
4511static PyMemberDef Pickler_members[] = {
4512 {"bin", T_INT, offsetof(PicklerObject, bin)},
4513 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004514 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004515 {NULL}
4516};
4517
4518static PyGetSetDef Pickler_getsets[] = {
4519 {"memo", (getter)Pickler_get_memo,
4520 (setter)Pickler_set_memo},
4521 {"persistent_id", (getter)Pickler_get_persid,
4522 (setter)Pickler_set_persid},
4523 {NULL}
4524};
4525
4526static PyTypeObject Pickler_Type = {
4527 PyVarObject_HEAD_INIT(NULL, 0)
4528 "_pickle.Pickler" , /*tp_name*/
4529 sizeof(PicklerObject), /*tp_basicsize*/
4530 0, /*tp_itemsize*/
4531 (destructor)Pickler_dealloc, /*tp_dealloc*/
4532 0, /*tp_print*/
4533 0, /*tp_getattr*/
4534 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004535 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004536 0, /*tp_repr*/
4537 0, /*tp_as_number*/
4538 0, /*tp_as_sequence*/
4539 0, /*tp_as_mapping*/
4540 0, /*tp_hash*/
4541 0, /*tp_call*/
4542 0, /*tp_str*/
4543 0, /*tp_getattro*/
4544 0, /*tp_setattro*/
4545 0, /*tp_as_buffer*/
4546 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004547 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004548 (traverseproc)Pickler_traverse, /*tp_traverse*/
4549 (inquiry)Pickler_clear, /*tp_clear*/
4550 0, /*tp_richcompare*/
4551 0, /*tp_weaklistoffset*/
4552 0, /*tp_iter*/
4553 0, /*tp_iternext*/
4554 Pickler_methods, /*tp_methods*/
4555 Pickler_members, /*tp_members*/
4556 Pickler_getsets, /*tp_getset*/
4557 0, /*tp_base*/
4558 0, /*tp_dict*/
4559 0, /*tp_descr_get*/
4560 0, /*tp_descr_set*/
4561 0, /*tp_dictoffset*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004562 Pickler_init, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004563 PyType_GenericAlloc, /*tp_alloc*/
4564 PyType_GenericNew, /*tp_new*/
4565 PyObject_GC_Del, /*tp_free*/
4566 0, /*tp_is_gc*/
4567};
4568
Victor Stinner121aab42011-09-29 23:40:53 +02004569/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004570
4571 XXX: It would be nice to able to avoid Python function call overhead, by
4572 using directly the C version of find_class(), when find_class() is not
4573 overridden by a subclass. Although, this could become rather hackish. A
4574 simpler optimization would be to call the C function when self is not a
4575 subclass instance. */
4576static PyObject *
4577find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4578{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004579 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004580
4581 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4582 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004583}
4584
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004585static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004586marker(UnpicklerObject *self)
4587{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004588 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004589 if (self->num_marks < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004590 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004591 return -1;
4592 }
4593
4594 return self->marks[--self->num_marks];
4595}
4596
4597static int
4598load_none(UnpicklerObject *self)
4599{
4600 PDATA_APPEND(self->stack, Py_None, -1);
4601 return 0;
4602}
4603
4604static int
4605bad_readline(void)
4606{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004607 PickleState *st = _Pickle_GetGlobalState();
4608 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004609 return -1;
4610}
4611
4612static int
4613load_int(UnpicklerObject *self)
4614{
4615 PyObject *value;
4616 char *endptr, *s;
4617 Py_ssize_t len;
4618 long x;
4619
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004620 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004621 return -1;
4622 if (len < 2)
4623 return bad_readline();
4624
4625 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004626 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004627 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004628 x = strtol(s, &endptr, 0);
4629
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004630 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004631 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004632 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004633 errno = 0;
4634 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004635 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004636 if (value == NULL) {
4637 PyErr_SetString(PyExc_ValueError,
4638 "could not convert string to int");
4639 return -1;
4640 }
4641 }
4642 else {
4643 if (len == 3 && (x == 0 || x == 1)) {
4644 if ((value = PyBool_FromLong(x)) == NULL)
4645 return -1;
4646 }
4647 else {
4648 if ((value = PyLong_FromLong(x)) == NULL)
4649 return -1;
4650 }
4651 }
4652
4653 PDATA_PUSH(self->stack, value, -1);
4654 return 0;
4655}
4656
4657static int
4658load_bool(UnpicklerObject *self, PyObject *boolean)
4659{
4660 assert(boolean == Py_True || boolean == Py_False);
4661 PDATA_APPEND(self->stack, boolean, -1);
4662 return 0;
4663}
4664
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004665/* s contains x bytes of an unsigned little-endian integer. Return its value
4666 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4667 */
4668static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004669calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004670{
4671 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004672 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004673 size_t x = 0;
4674
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004675 for (i = 0; i < nbytes && i < sizeof(size_t); i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004676 x |= (size_t) s[i] << (8 * i);
4677 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004678
4679 if (x > PY_SSIZE_T_MAX)
4680 return -1;
4681 else
4682 return (Py_ssize_t) x;
4683}
4684
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004685/* s contains x bytes of a little-endian integer. Return its value as a
4686 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4687 * int, but when x is 4 it's a signed one. This is an historical source
4688 * of x-platform bugs.
4689 */
4690static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004691calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004692{
4693 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004694 int i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004695 long x = 0;
4696
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004697 for (i = 0; i < nbytes; i++) {
4698 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004699 }
4700
4701 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4702 * is signed, so on a box with longs bigger than 4 bytes we need
4703 * to extend a BININT's sign bit to the full width.
4704 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004705 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004706 x |= -(x & (1L << 31));
4707 }
4708
4709 return x;
4710}
4711
4712static int
4713load_binintx(UnpicklerObject *self, char *s, int size)
4714{
4715 PyObject *value;
4716 long x;
4717
4718 x = calc_binint(s, size);
4719
4720 if ((value = PyLong_FromLong(x)) == NULL)
4721 return -1;
4722
4723 PDATA_PUSH(self->stack, value, -1);
4724 return 0;
4725}
4726
4727static int
4728load_binint(UnpicklerObject *self)
4729{
4730 char *s;
4731
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004732 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004733 return -1;
4734
4735 return load_binintx(self, s, 4);
4736}
4737
4738static int
4739load_binint1(UnpicklerObject *self)
4740{
4741 char *s;
4742
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004743 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004744 return -1;
4745
4746 return load_binintx(self, s, 1);
4747}
4748
4749static int
4750load_binint2(UnpicklerObject *self)
4751{
4752 char *s;
4753
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004754 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004755 return -1;
4756
4757 return load_binintx(self, s, 2);
4758}
4759
4760static int
4761load_long(UnpicklerObject *self)
4762{
4763 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004764 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004765 Py_ssize_t len;
4766
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004767 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004768 return -1;
4769 if (len < 2)
4770 return bad_readline();
4771
Mark Dickinson8dd05142009-01-20 20:43:58 +00004772 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4773 the 'L' before calling PyLong_FromString. In order to maintain
4774 compatibility with Python 3.0.0, we don't actually *require*
4775 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004776 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004777 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004778 /* XXX: Should the base argument explicitly set to 10? */
4779 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004780 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004781 return -1;
4782
4783 PDATA_PUSH(self->stack, value, -1);
4784 return 0;
4785}
4786
4787/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4788 * data following.
4789 */
4790static int
4791load_counted_long(UnpicklerObject *self, int size)
4792{
4793 PyObject *value;
4794 char *nbytes;
4795 char *pdata;
4796
4797 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004798 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004799 return -1;
4800
4801 size = calc_binint(nbytes, size);
4802 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004803 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004804 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004805 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004806 "LONG pickle has negative byte count");
4807 return -1;
4808 }
4809
4810 if (size == 0)
4811 value = PyLong_FromLong(0L);
4812 else {
4813 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004814 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004815 return -1;
4816 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4817 1 /* little endian */ , 1 /* signed */ );
4818 }
4819 if (value == NULL)
4820 return -1;
4821 PDATA_PUSH(self->stack, value, -1);
4822 return 0;
4823}
4824
4825static int
4826load_float(UnpicklerObject *self)
4827{
4828 PyObject *value;
4829 char *endptr, *s;
4830 Py_ssize_t len;
4831 double d;
4832
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004833 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004834 return -1;
4835 if (len < 2)
4836 return bad_readline();
4837
4838 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004839 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4840 if (d == -1.0 && PyErr_Occurred())
4841 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004842 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004843 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4844 return -1;
4845 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004846 value = PyFloat_FromDouble(d);
4847 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004848 return -1;
4849
4850 PDATA_PUSH(self->stack, value, -1);
4851 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004852}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004853
4854static int
4855load_binfloat(UnpicklerObject *self)
4856{
4857 PyObject *value;
4858 double x;
4859 char *s;
4860
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004861 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004862 return -1;
4863
4864 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4865 if (x == -1.0 && PyErr_Occurred())
4866 return -1;
4867
4868 if ((value = PyFloat_FromDouble(x)) == NULL)
4869 return -1;
4870
4871 PDATA_PUSH(self->stack, value, -1);
4872 return 0;
4873}
4874
4875static int
4876load_string(UnpicklerObject *self)
4877{
4878 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004879 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004880 Py_ssize_t len;
4881 char *s, *p;
4882
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004883 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004884 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004885 /* Strip the newline */
4886 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004887 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004888 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004889 p = s + 1;
4890 len -= 2;
4891 }
4892 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004893 PickleState *st = _Pickle_GetGlobalState();
4894 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004895 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004896 return -1;
4897 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004898 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004899
4900 /* Use the PyBytes API to decode the string, since that is what is used
4901 to encode, and then coerce the result to Unicode. */
4902 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004903 if (bytes == NULL)
4904 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004905
4906 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4907 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4908 if (strcmp(self->encoding, "bytes") == 0) {
4909 obj = bytes;
4910 }
4911 else {
4912 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4913 Py_DECREF(bytes);
4914 if (obj == NULL) {
4915 return -1;
4916 }
4917 }
4918
4919 PDATA_PUSH(self->stack, obj, -1);
4920 return 0;
4921}
4922
4923static int
4924load_counted_binstring(UnpicklerObject *self, int nbytes)
4925{
4926 PyObject *obj;
4927 Py_ssize_t size;
4928 char *s;
4929
4930 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004931 return -1;
4932
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004933 size = calc_binsize(s, nbytes);
4934 if (size < 0) {
4935 PickleState *st = _Pickle_GetGlobalState();
4936 PyErr_Format(st->UnpicklingError,
4937 "BINSTRING exceeds system's maximum size of %zd bytes",
4938 PY_SSIZE_T_MAX);
4939 return -1;
4940 }
4941
4942 if (_Unpickler_Read(self, &s, size) < 0)
4943 return -1;
4944
4945 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4946 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4947 if (strcmp(self->encoding, "bytes") == 0) {
4948 obj = PyBytes_FromStringAndSize(s, size);
4949 }
4950 else {
4951 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4952 }
4953 if (obj == NULL) {
4954 return -1;
4955 }
4956
4957 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004958 return 0;
4959}
4960
4961static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004962load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004963{
4964 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004965 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004966 char *s;
4967
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004968 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004969 return -1;
4970
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004971 size = calc_binsize(s, nbytes);
4972 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004973 PyErr_Format(PyExc_OverflowError,
4974 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004975 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004976 return -1;
4977 }
4978
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004979 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004980 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004981
4982 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004983 if (bytes == NULL)
4984 return -1;
4985
4986 PDATA_PUSH(self->stack, bytes, -1);
4987 return 0;
4988}
4989
4990static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004991load_unicode(UnpicklerObject *self)
4992{
4993 PyObject *str;
4994 Py_ssize_t len;
4995 char *s;
4996
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004997 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004998 return -1;
4999 if (len < 1)
5000 return bad_readline();
5001
5002 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5003 if (str == NULL)
5004 return -1;
5005
5006 PDATA_PUSH(self->stack, str, -1);
5007 return 0;
5008}
5009
5010static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005011load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005012{
5013 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005014 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005015 char *s;
5016
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005017 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005018 return -1;
5019
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005020 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005021 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005022 PyErr_Format(PyExc_OverflowError,
5023 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005024 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005025 return -1;
5026 }
5027
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005028 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005029 return -1;
5030
Victor Stinner485fb562010-04-13 11:07:24 +00005031 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005032 if (str == NULL)
5033 return -1;
5034
5035 PDATA_PUSH(self->stack, str, -1);
5036 return 0;
5037}
5038
5039static int
5040load_tuple(UnpicklerObject *self)
5041{
5042 PyObject *tuple;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005043 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005044
5045 if ((i = marker(self)) < 0)
5046 return -1;
5047
5048 tuple = Pdata_poptuple(self->stack, i);
5049 if (tuple == NULL)
5050 return -1;
5051 PDATA_PUSH(self->stack, tuple, -1);
5052 return 0;
5053}
5054
5055static int
5056load_counted_tuple(UnpicklerObject *self, int len)
5057{
5058 PyObject *tuple;
5059
5060 tuple = PyTuple_New(len);
5061 if (tuple == NULL)
5062 return -1;
5063
5064 while (--len >= 0) {
5065 PyObject *item;
5066
5067 PDATA_POP(self->stack, item);
5068 if (item == NULL)
5069 return -1;
5070 PyTuple_SET_ITEM(tuple, len, item);
5071 }
5072 PDATA_PUSH(self->stack, tuple, -1);
5073 return 0;
5074}
5075
5076static int
5077load_empty_list(UnpicklerObject *self)
5078{
5079 PyObject *list;
5080
5081 if ((list = PyList_New(0)) == NULL)
5082 return -1;
5083 PDATA_PUSH(self->stack, list, -1);
5084 return 0;
5085}
5086
5087static int
5088load_empty_dict(UnpicklerObject *self)
5089{
5090 PyObject *dict;
5091
5092 if ((dict = PyDict_New()) == NULL)
5093 return -1;
5094 PDATA_PUSH(self->stack, dict, -1);
5095 return 0;
5096}
5097
5098static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005099load_empty_set(UnpicklerObject *self)
5100{
5101 PyObject *set;
5102
5103 if ((set = PySet_New(NULL)) == NULL)
5104 return -1;
5105 PDATA_PUSH(self->stack, set, -1);
5106 return 0;
5107}
5108
5109static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005110load_list(UnpicklerObject *self)
5111{
5112 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005113 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005114
5115 if ((i = marker(self)) < 0)
5116 return -1;
5117
5118 list = Pdata_poplist(self->stack, i);
5119 if (list == NULL)
5120 return -1;
5121 PDATA_PUSH(self->stack, list, -1);
5122 return 0;
5123}
5124
5125static int
5126load_dict(UnpicklerObject *self)
5127{
5128 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005129 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005130
5131 if ((i = marker(self)) < 0)
5132 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005133 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005134
5135 if ((dict = PyDict_New()) == NULL)
5136 return -1;
5137
5138 for (k = i + 1; k < j; k += 2) {
5139 key = self->stack->data[k - 1];
5140 value = self->stack->data[k];
5141 if (PyDict_SetItem(dict, key, value) < 0) {
5142 Py_DECREF(dict);
5143 return -1;
5144 }
5145 }
5146 Pdata_clear(self->stack, i);
5147 PDATA_PUSH(self->stack, dict, -1);
5148 return 0;
5149}
5150
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005151static int
5152load_frozenset(UnpicklerObject *self)
5153{
5154 PyObject *items;
5155 PyObject *frozenset;
5156 Py_ssize_t i;
5157
5158 if ((i = marker(self)) < 0)
5159 return -1;
5160
5161 items = Pdata_poptuple(self->stack, i);
5162 if (items == NULL)
5163 return -1;
5164
5165 frozenset = PyFrozenSet_New(items);
5166 Py_DECREF(items);
5167 if (frozenset == NULL)
5168 return -1;
5169
5170 PDATA_PUSH(self->stack, frozenset, -1);
5171 return 0;
5172}
5173
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005174static PyObject *
5175instantiate(PyObject *cls, PyObject *args)
5176{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005177 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005178 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005179 /* Caller must assure args are a tuple. Normally, args come from
5180 Pdata_poptuple which packs objects from the top of the stack
5181 into a newly created tuple. */
5182 assert(PyTuple_Check(args));
5183 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005184 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005185 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005186 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005187 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005188 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005189
5190 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005191 }
5192 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005193}
5194
5195static int
5196load_obj(UnpicklerObject *self)
5197{
5198 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005199 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005200
5201 if ((i = marker(self)) < 0)
5202 return -1;
5203
5204 args = Pdata_poptuple(self->stack, i + 1);
5205 if (args == NULL)
5206 return -1;
5207
5208 PDATA_POP(self->stack, cls);
5209 if (cls) {
5210 obj = instantiate(cls, args);
5211 Py_DECREF(cls);
5212 }
5213 Py_DECREF(args);
5214 if (obj == NULL)
5215 return -1;
5216
5217 PDATA_PUSH(self->stack, obj, -1);
5218 return 0;
5219}
5220
5221static int
5222load_inst(UnpicklerObject *self)
5223{
5224 PyObject *cls = NULL;
5225 PyObject *args = NULL;
5226 PyObject *obj = NULL;
5227 PyObject *module_name;
5228 PyObject *class_name;
5229 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005230 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005231 char *s;
5232
5233 if ((i = marker(self)) < 0)
5234 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005235 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005236 return -1;
5237 if (len < 2)
5238 return bad_readline();
5239
5240 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5241 identifiers are permitted in Python 3.0, since the INST opcode is only
5242 supported by older protocols on Python 2.x. */
5243 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5244 if (module_name == NULL)
5245 return -1;
5246
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005247 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005248 if (len < 2)
5249 return bad_readline();
5250 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005251 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005252 cls = find_class(self, module_name, class_name);
5253 Py_DECREF(class_name);
5254 }
5255 }
5256 Py_DECREF(module_name);
5257
5258 if (cls == NULL)
5259 return -1;
5260
5261 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5262 obj = instantiate(cls, args);
5263 Py_DECREF(args);
5264 }
5265 Py_DECREF(cls);
5266
5267 if (obj == NULL)
5268 return -1;
5269
5270 PDATA_PUSH(self->stack, obj, -1);
5271 return 0;
5272}
5273
5274static int
5275load_newobj(UnpicklerObject *self)
5276{
5277 PyObject *args = NULL;
5278 PyObject *clsraw = NULL;
5279 PyTypeObject *cls; /* clsraw cast to its true type */
5280 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005281 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005282
5283 /* Stack is ... cls argtuple, and we want to call
5284 * cls.__new__(cls, *argtuple).
5285 */
5286 PDATA_POP(self->stack, args);
5287 if (args == NULL)
5288 goto error;
5289 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005290 PyErr_SetString(st->UnpicklingError,
5291 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005292 goto error;
5293 }
5294
5295 PDATA_POP(self->stack, clsraw);
5296 cls = (PyTypeObject *)clsraw;
5297 if (cls == NULL)
5298 goto error;
5299 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005300 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005301 "isn't a type object");
5302 goto error;
5303 }
5304 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005305 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005306 "has NULL tp_new");
5307 goto error;
5308 }
5309
5310 /* Call __new__. */
5311 obj = cls->tp_new(cls, args, NULL);
5312 if (obj == NULL)
5313 goto error;
5314
5315 Py_DECREF(args);
5316 Py_DECREF(clsraw);
5317 PDATA_PUSH(self->stack, obj, -1);
5318 return 0;
5319
5320 error:
5321 Py_XDECREF(args);
5322 Py_XDECREF(clsraw);
5323 return -1;
5324}
5325
5326static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005327load_newobj_ex(UnpicklerObject *self)
5328{
5329 PyObject *cls, *args, *kwargs;
5330 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005331 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005332
5333 PDATA_POP(self->stack, kwargs);
5334 if (kwargs == NULL) {
5335 return -1;
5336 }
5337 PDATA_POP(self->stack, args);
5338 if (args == NULL) {
5339 Py_DECREF(kwargs);
5340 return -1;
5341 }
5342 PDATA_POP(self->stack, cls);
5343 if (cls == NULL) {
5344 Py_DECREF(kwargs);
5345 Py_DECREF(args);
5346 return -1;
5347 }
Larry Hastings61272b72014-01-07 12:41:53 -08005348
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005349 if (!PyType_Check(cls)) {
5350 Py_DECREF(kwargs);
5351 Py_DECREF(args);
5352 Py_DECREF(cls);
Larry Hastings61272b72014-01-07 12:41:53 -08005353 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005354 "NEWOBJ_EX class argument must be a type, not %.200s",
5355 Py_TYPE(cls)->tp_name);
5356 return -1;
5357 }
5358
5359 if (((PyTypeObject *)cls)->tp_new == NULL) {
5360 Py_DECREF(kwargs);
5361 Py_DECREF(args);
5362 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005363 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005364 "NEWOBJ_EX class argument doesn't have __new__");
5365 return -1;
5366 }
5367 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5368 Py_DECREF(kwargs);
5369 Py_DECREF(args);
5370 Py_DECREF(cls);
5371 if (obj == NULL) {
5372 return -1;
5373 }
5374 PDATA_PUSH(self->stack, obj, -1);
5375 return 0;
5376}
5377
5378static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005379load_global(UnpicklerObject *self)
5380{
5381 PyObject *global = NULL;
5382 PyObject *module_name;
5383 PyObject *global_name;
5384 Py_ssize_t len;
5385 char *s;
5386
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005387 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005388 return -1;
5389 if (len < 2)
5390 return bad_readline();
5391 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5392 if (!module_name)
5393 return -1;
5394
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005395 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005396 if (len < 2) {
5397 Py_DECREF(module_name);
5398 return bad_readline();
5399 }
5400 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5401 if (global_name) {
5402 global = find_class(self, module_name, global_name);
5403 Py_DECREF(global_name);
5404 }
5405 }
5406 Py_DECREF(module_name);
5407
5408 if (global == NULL)
5409 return -1;
5410 PDATA_PUSH(self->stack, global, -1);
5411 return 0;
5412}
5413
5414static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005415load_stack_global(UnpicklerObject *self)
5416{
5417 PyObject *global;
5418 PyObject *module_name;
5419 PyObject *global_name;
5420
5421 PDATA_POP(self->stack, global_name);
5422 PDATA_POP(self->stack, module_name);
5423 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5424 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005425 PickleState *st = _Pickle_GetGlobalState();
5426 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005427 Py_XDECREF(global_name);
5428 Py_XDECREF(module_name);
5429 return -1;
5430 }
5431 global = find_class(self, module_name, global_name);
5432 Py_DECREF(global_name);
5433 Py_DECREF(module_name);
5434 if (global == NULL)
5435 return -1;
5436 PDATA_PUSH(self->stack, global, -1);
5437 return 0;
5438}
5439
5440static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005441load_persid(UnpicklerObject *self)
5442{
5443 PyObject *pid;
5444 Py_ssize_t len;
5445 char *s;
5446
5447 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005448 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005449 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005450 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005451 return bad_readline();
5452
5453 pid = PyBytes_FromStringAndSize(s, len - 1);
5454 if (pid == NULL)
5455 return -1;
5456
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005457 /* This does not leak since _Pickle_FastCall() steals the reference
5458 to pid first. */
5459 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005460 if (pid == NULL)
5461 return -1;
5462
5463 PDATA_PUSH(self->stack, pid, -1);
5464 return 0;
5465 }
5466 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005467 PickleState *st = _Pickle_GetGlobalState();
5468 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005469 "A load persistent id instruction was encountered,\n"
5470 "but no persistent_load function was specified.");
5471 return -1;
5472 }
5473}
5474
5475static int
5476load_binpersid(UnpicklerObject *self)
5477{
5478 PyObject *pid;
5479
5480 if (self->pers_func) {
5481 PDATA_POP(self->stack, pid);
5482 if (pid == NULL)
5483 return -1;
5484
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005485 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005486 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005487 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005488 if (pid == NULL)
5489 return -1;
5490
5491 PDATA_PUSH(self->stack, pid, -1);
5492 return 0;
5493 }
5494 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005495 PickleState *st = _Pickle_GetGlobalState();
5496 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005497 "A load persistent id instruction was encountered,\n"
5498 "but no persistent_load function was specified.");
5499 return -1;
5500 }
5501}
5502
5503static int
5504load_pop(UnpicklerObject *self)
5505{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005506 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005507
5508 /* Note that we split the (pickle.py) stack into two stacks,
5509 * an object stack and a mark stack. We have to be clever and
5510 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005511 * mark stack first, and only signalling a stack underflow if
5512 * the object stack is empty and the mark stack doesn't match
5513 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005514 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005515 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005516 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005517 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005518 len--;
5519 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005520 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005521 } else {
5522 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005523 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005524 return 0;
5525}
5526
5527static int
5528load_pop_mark(UnpicklerObject *self)
5529{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005530 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005531
5532 if ((i = marker(self)) < 0)
5533 return -1;
5534
5535 Pdata_clear(self->stack, i);
5536
5537 return 0;
5538}
5539
5540static int
5541load_dup(UnpicklerObject *self)
5542{
5543 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005544 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005545
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005546 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005547 return stack_underflow();
5548 last = self->stack->data[len - 1];
5549 PDATA_APPEND(self->stack, last, -1);
5550 return 0;
5551}
5552
5553static int
5554load_get(UnpicklerObject *self)
5555{
5556 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005557 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005558 Py_ssize_t len;
5559 char *s;
5560
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005561 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005562 return -1;
5563 if (len < 2)
5564 return bad_readline();
5565
5566 key = PyLong_FromString(s, NULL, 10);
5567 if (key == NULL)
5568 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005569 idx = PyLong_AsSsize_t(key);
5570 if (idx == -1 && PyErr_Occurred()) {
5571 Py_DECREF(key);
5572 return -1;
5573 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005574
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005575 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005576 if (value == NULL) {
5577 if (!PyErr_Occurred())
5578 PyErr_SetObject(PyExc_KeyError, key);
5579 Py_DECREF(key);
5580 return -1;
5581 }
5582 Py_DECREF(key);
5583
5584 PDATA_APPEND(self->stack, value, -1);
5585 return 0;
5586}
5587
5588static int
5589load_binget(UnpicklerObject *self)
5590{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005591 PyObject *value;
5592 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005593 char *s;
5594
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005595 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005596 return -1;
5597
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005598 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005599
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005600 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005601 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005602 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005603 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005604 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005605 Py_DECREF(key);
5606 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005607 return -1;
5608 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005609
5610 PDATA_APPEND(self->stack, value, -1);
5611 return 0;
5612}
5613
5614static int
5615load_long_binget(UnpicklerObject *self)
5616{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005617 PyObject *value;
5618 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005619 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005620
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005621 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005622 return -1;
5623
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005624 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005625
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005626 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005627 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005628 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005629 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005631 Py_DECREF(key);
5632 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005633 return -1;
5634 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005635
5636 PDATA_APPEND(self->stack, value, -1);
5637 return 0;
5638}
5639
5640/* Push an object from the extension registry (EXT[124]). nbytes is
5641 * the number of bytes following the opcode, holding the index (code) value.
5642 */
5643static int
5644load_extension(UnpicklerObject *self, int nbytes)
5645{
5646 char *codebytes; /* the nbytes bytes after the opcode */
5647 long code; /* calc_binint returns long */
5648 PyObject *py_code; /* code as a Python int */
5649 PyObject *obj; /* the object to push */
5650 PyObject *pair; /* (module_name, class_name) */
5651 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005652 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005653
5654 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005655 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005656 return -1;
5657 code = calc_binint(codebytes, nbytes);
5658 if (code <= 0) { /* note that 0 is forbidden */
5659 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005660 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005661 return -1;
5662 }
5663
5664 /* Look for the code in the cache. */
5665 py_code = PyLong_FromLong(code);
5666 if (py_code == NULL)
5667 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005668 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005669 if (obj != NULL) {
5670 /* Bingo. */
5671 Py_DECREF(py_code);
5672 PDATA_APPEND(self->stack, obj, -1);
5673 return 0;
5674 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005675 if (PyErr_Occurred()) {
5676 Py_DECREF(py_code);
5677 return -1;
5678 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005679
5680 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005681 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005682 if (pair == NULL) {
5683 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005684 if (!PyErr_Occurred()) {
5685 PyErr_Format(PyExc_ValueError, "unregistered extension "
5686 "code %ld", code);
5687 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005688 return -1;
5689 }
5690 /* Since the extension registry is manipulable via Python code,
5691 * confirm that pair is really a 2-tuple of strings.
5692 */
5693 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5694 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5695 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5696 Py_DECREF(py_code);
5697 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5698 "isn't a 2-tuple of strings", code);
5699 return -1;
5700 }
5701 /* Load the object. */
5702 obj = find_class(self, module_name, class_name);
5703 if (obj == NULL) {
5704 Py_DECREF(py_code);
5705 return -1;
5706 }
5707 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005708 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005709 Py_DECREF(py_code);
5710 if (code < 0) {
5711 Py_DECREF(obj);
5712 return -1;
5713 }
5714 PDATA_PUSH(self->stack, obj, -1);
5715 return 0;
5716}
5717
5718static int
5719load_put(UnpicklerObject *self)
5720{
5721 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005722 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005723 Py_ssize_t len;
5724 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005725
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005726 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005727 return -1;
5728 if (len < 2)
5729 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005730 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005731 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005732 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005733
5734 key = PyLong_FromString(s, NULL, 10);
5735 if (key == NULL)
5736 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005737 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005738 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005739 if (idx < 0) {
5740 if (!PyErr_Occurred())
5741 PyErr_SetString(PyExc_ValueError,
5742 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005743 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005744 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005745
5746 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005747}
5748
5749static int
5750load_binput(UnpicklerObject *self)
5751{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005752 PyObject *value;
5753 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005754 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005755
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005756 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005757 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005758
5759 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005760 return stack_underflow();
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
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005763 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005764
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005765 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005766}
5767
5768static int
5769load_long_binput(UnpicklerObject *self)
5770{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005771 PyObject *value;
5772 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005773 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005774
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005775 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005776 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005777
5778 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005779 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005780 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005781
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005782 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005783 if (idx < 0) {
5784 PyErr_SetString(PyExc_ValueError,
5785 "negative LONG_BINPUT argument");
5786 return -1;
5787 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005788
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005789 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005790}
5791
5792static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005793load_memoize(UnpicklerObject *self)
5794{
5795 PyObject *value;
5796
5797 if (Py_SIZE(self->stack) <= 0)
5798 return stack_underflow();
5799 value = self->stack->data[Py_SIZE(self->stack) - 1];
5800
5801 return _Unpickler_MemoPut(self, self->memo_len, value);
5802}
5803
5804static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005805do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005806{
5807 PyObject *value;
5808 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005809 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005810
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005811 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005812 if (x > len || x <= 0)
5813 return stack_underflow();
5814 if (len == x) /* nothing to do */
5815 return 0;
5816
5817 list = self->stack->data[x - 1];
5818
5819 if (PyList_Check(list)) {
5820 PyObject *slice;
5821 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005822 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005823
5824 slice = Pdata_poplist(self->stack, x);
5825 if (!slice)
5826 return -1;
5827 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005828 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005829 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005830 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005831 }
5832 else {
5833 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005834 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005835
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005836 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005837 if (append_func == NULL)
5838 return -1;
5839 for (i = x; i < len; i++) {
5840 PyObject *result;
5841
5842 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005843 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005844 if (result == NULL) {
5845 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005846 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005847 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005848 return -1;
5849 }
5850 Py_DECREF(result);
5851 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005852 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005853 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005854 }
5855
5856 return 0;
5857}
5858
5859static int
5860load_append(UnpicklerObject *self)
5861{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005862 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005863}
5864
5865static int
5866load_appends(UnpicklerObject *self)
5867{
5868 return do_append(self, marker(self));
5869}
5870
5871static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005872do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005873{
5874 PyObject *value, *key;
5875 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005876 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005877 int status = 0;
5878
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005879 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005880 if (x > len || x <= 0)
5881 return stack_underflow();
5882 if (len == x) /* nothing to do */
5883 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005884 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005885 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005886 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005887 PyErr_SetString(st->UnpicklingError,
5888 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005889 return -1;
5890 }
5891
5892 /* Here, dict does not actually need to be a PyDict; it could be anything
5893 that supports the __setitem__ attribute. */
5894 dict = self->stack->data[x - 1];
5895
5896 for (i = x + 1; i < len; i += 2) {
5897 key = self->stack->data[i - 1];
5898 value = self->stack->data[i];
5899 if (PyObject_SetItem(dict, key, value) < 0) {
5900 status = -1;
5901 break;
5902 }
5903 }
5904
5905 Pdata_clear(self->stack, x);
5906 return status;
5907}
5908
5909static int
5910load_setitem(UnpicklerObject *self)
5911{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005912 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005913}
5914
5915static int
5916load_setitems(UnpicklerObject *self)
5917{
5918 return do_setitems(self, marker(self));
5919}
5920
5921static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005922load_additems(UnpicklerObject *self)
5923{
5924 PyObject *set;
5925 Py_ssize_t mark, len, i;
5926
5927 mark = marker(self);
5928 len = Py_SIZE(self->stack);
5929 if (mark > len || mark <= 0)
5930 return stack_underflow();
5931 if (len == mark) /* nothing to do */
5932 return 0;
5933
5934 set = self->stack->data[mark - 1];
5935
5936 if (PySet_Check(set)) {
5937 PyObject *items;
5938 int status;
5939
5940 items = Pdata_poptuple(self->stack, mark);
5941 if (items == NULL)
5942 return -1;
5943
5944 status = _PySet_Update(set, items);
5945 Py_DECREF(items);
5946 return status;
5947 }
5948 else {
5949 PyObject *add_func;
5950 _Py_IDENTIFIER(add);
5951
5952 add_func = _PyObject_GetAttrId(set, &PyId_add);
5953 if (add_func == NULL)
5954 return -1;
5955 for (i = mark; i < len; i++) {
5956 PyObject *result;
5957 PyObject *item;
5958
5959 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005960 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005961 if (result == NULL) {
5962 Pdata_clear(self->stack, i + 1);
5963 Py_SIZE(self->stack) = mark;
5964 return -1;
5965 }
5966 Py_DECREF(result);
5967 }
5968 Py_SIZE(self->stack) = mark;
5969 }
5970
5971 return 0;
5972}
5973
5974static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005975load_build(UnpicklerObject *self)
5976{
5977 PyObject *state, *inst, *slotstate;
5978 PyObject *setstate;
5979 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005980 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005981
5982 /* Stack is ... instance, state. We want to leave instance at
5983 * the stack top, possibly mutated via instance.__setstate__(state).
5984 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005985 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005986 return stack_underflow();
5987
5988 PDATA_POP(self->stack, state);
5989 if (state == NULL)
5990 return -1;
5991
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005992 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005993
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005994 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005995 if (setstate == NULL) {
5996 if (PyErr_ExceptionMatches(PyExc_AttributeError))
5997 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00005998 else {
5999 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006000 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006001 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006002 }
6003 else {
6004 PyObject *result;
6005
6006 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006007 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006008 Py_DECREF(setstate);
6009 if (result == NULL)
6010 return -1;
6011 Py_DECREF(result);
6012 return 0;
6013 }
6014
6015 /* A default __setstate__. First see whether state embeds a
6016 * slot state dict too (a proto 2 addition).
6017 */
6018 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
6019 PyObject *tmp = state;
6020
6021 state = PyTuple_GET_ITEM(tmp, 0);
6022 slotstate = PyTuple_GET_ITEM(tmp, 1);
6023 Py_INCREF(state);
6024 Py_INCREF(slotstate);
6025 Py_DECREF(tmp);
6026 }
6027 else
6028 slotstate = NULL;
6029
6030 /* Set inst.__dict__ from the state dict (if any). */
6031 if (state != Py_None) {
6032 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006033 PyObject *d_key, *d_value;
6034 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006035 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006036
6037 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006038 PickleState *st = _Pickle_GetGlobalState();
6039 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006040 goto error;
6041 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006042 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006043 if (dict == NULL)
6044 goto error;
6045
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006046 i = 0;
6047 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6048 /* normally the keys for instance attributes are
6049 interned. we should try to do that here. */
6050 Py_INCREF(d_key);
6051 if (PyUnicode_CheckExact(d_key))
6052 PyUnicode_InternInPlace(&d_key);
6053 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6054 Py_DECREF(d_key);
6055 goto error;
6056 }
6057 Py_DECREF(d_key);
6058 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006059 Py_DECREF(dict);
6060 }
6061
6062 /* Also set instance attributes from the slotstate dict (if any). */
6063 if (slotstate != NULL) {
6064 PyObject *d_key, *d_value;
6065 Py_ssize_t i;
6066
6067 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006068 PickleState *st = _Pickle_GetGlobalState();
6069 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006070 "slot state is not a dictionary");
6071 goto error;
6072 }
6073 i = 0;
6074 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6075 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6076 goto error;
6077 }
6078 }
6079
6080 if (0) {
6081 error:
6082 status = -1;
6083 }
6084
6085 Py_DECREF(state);
6086 Py_XDECREF(slotstate);
6087 return status;
6088}
6089
6090static int
6091load_mark(UnpicklerObject *self)
6092{
6093
6094 /* Note that we split the (pickle.py) stack into two stacks, an
6095 * object stack and a mark stack. Here we push a mark onto the
6096 * mark stack.
6097 */
6098
6099 if ((self->num_marks + 1) >= self->marks_size) {
6100 size_t alloc;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006101 Py_ssize_t *marks;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006102
6103 /* Use the size_t type to check for overflow. */
6104 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006105 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006106 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006107 PyErr_NoMemory();
6108 return -1;
6109 }
6110
6111 if (self->marks == NULL)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006112 marks = (Py_ssize_t *) PyMem_Malloc(alloc * sizeof(Py_ssize_t));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006113 else
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006114 marks = (Py_ssize_t *) PyMem_Realloc(self->marks,
6115 alloc * sizeof(Py_ssize_t));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006116 if (marks == NULL) {
6117 PyErr_NoMemory();
6118 return -1;
6119 }
6120 self->marks = marks;
6121 self->marks_size = (Py_ssize_t)alloc;
6122 }
6123
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006124 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006125
6126 return 0;
6127}
6128
6129static int
6130load_reduce(UnpicklerObject *self)
6131{
6132 PyObject *callable = NULL;
6133 PyObject *argtup = NULL;
6134 PyObject *obj = NULL;
6135
6136 PDATA_POP(self->stack, argtup);
6137 if (argtup == NULL)
6138 return -1;
6139 PDATA_POP(self->stack, callable);
6140 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006141 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006142 Py_DECREF(callable);
6143 }
6144 Py_DECREF(argtup);
6145
6146 if (obj == NULL)
6147 return -1;
6148
6149 PDATA_PUSH(self->stack, obj, -1);
6150 return 0;
6151}
6152
6153/* Just raises an error if we don't know the protocol specified. PROTO
6154 * is the first opcode for protocols >= 2.
6155 */
6156static int
6157load_proto(UnpicklerObject *self)
6158{
6159 char *s;
6160 int i;
6161
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006162 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006163 return -1;
6164
6165 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006166 if (i <= HIGHEST_PROTOCOL) {
6167 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006168 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006169 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006170
6171 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6172 return -1;
6173}
6174
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006175static int
6176load_frame(UnpicklerObject *self)
6177{
6178 char *s;
6179 Py_ssize_t frame_len;
6180
6181 if (_Unpickler_Read(self, &s, 8) < 0)
6182 return -1;
6183
6184 frame_len = calc_binsize(s, 8);
6185 if (frame_len < 0) {
6186 PyErr_Format(PyExc_OverflowError,
6187 "FRAME length exceeds system's maximum of %zd bytes",
6188 PY_SSIZE_T_MAX);
6189 return -1;
6190 }
6191
6192 if (_Unpickler_Read(self, &s, frame_len) < 0)
6193 return -1;
6194
6195 /* Rewind to start of frame */
6196 self->next_read_idx -= frame_len;
6197 return 0;
6198}
6199
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006200static PyObject *
6201load(UnpicklerObject *self)
6202{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006203 PyObject *value = NULL;
6204 char *s;
6205
6206 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006207 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006208 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006209 Pdata_clear(self->stack, 0);
6210
6211 /* Convenient macros for the dispatch while-switch loop just below. */
6212#define OP(opcode, load_func) \
6213 case opcode: if (load_func(self) < 0) break; continue;
6214
6215#define OP_ARG(opcode, load_func, arg) \
6216 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6217
6218 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006219 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006220 break;
6221
6222 switch ((enum opcode)s[0]) {
6223 OP(NONE, load_none)
6224 OP(BININT, load_binint)
6225 OP(BININT1, load_binint1)
6226 OP(BININT2, load_binint2)
6227 OP(INT, load_int)
6228 OP(LONG, load_long)
6229 OP_ARG(LONG1, load_counted_long, 1)
6230 OP_ARG(LONG4, load_counted_long, 4)
6231 OP(FLOAT, load_float)
6232 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006233 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6234 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6235 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6236 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6237 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006238 OP(STRING, load_string)
6239 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006240 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6241 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6242 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006243 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6244 OP_ARG(TUPLE1, load_counted_tuple, 1)
6245 OP_ARG(TUPLE2, load_counted_tuple, 2)
6246 OP_ARG(TUPLE3, load_counted_tuple, 3)
6247 OP(TUPLE, load_tuple)
6248 OP(EMPTY_LIST, load_empty_list)
6249 OP(LIST, load_list)
6250 OP(EMPTY_DICT, load_empty_dict)
6251 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006252 OP(EMPTY_SET, load_empty_set)
6253 OP(ADDITEMS, load_additems)
6254 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006255 OP(OBJ, load_obj)
6256 OP(INST, load_inst)
6257 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006258 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006259 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006260 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006261 OP(APPEND, load_append)
6262 OP(APPENDS, load_appends)
6263 OP(BUILD, load_build)
6264 OP(DUP, load_dup)
6265 OP(BINGET, load_binget)
6266 OP(LONG_BINGET, load_long_binget)
6267 OP(GET, load_get)
6268 OP(MARK, load_mark)
6269 OP(BINPUT, load_binput)
6270 OP(LONG_BINPUT, load_long_binput)
6271 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006272 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006273 OP(POP, load_pop)
6274 OP(POP_MARK, load_pop_mark)
6275 OP(SETITEM, load_setitem)
6276 OP(SETITEMS, load_setitems)
6277 OP(PERSID, load_persid)
6278 OP(BINPERSID, load_binpersid)
6279 OP(REDUCE, load_reduce)
6280 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006281 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006282 OP_ARG(EXT1, load_extension, 1)
6283 OP_ARG(EXT2, load_extension, 2)
6284 OP_ARG(EXT4, load_extension, 4)
6285 OP_ARG(NEWTRUE, load_bool, Py_True)
6286 OP_ARG(NEWFALSE, load_bool, Py_False)
6287
6288 case STOP:
6289 break;
6290
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006291 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006292 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006293 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006294 }
6295 else {
6296 PickleState *st = _Pickle_GetGlobalState();
6297 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006298 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006299 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006300 return NULL;
6301 }
6302
6303 break; /* and we are done! */
6304 }
6305
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006306 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006307 return NULL;
6308 }
6309
Victor Stinner2ae57e32013-10-31 13:39:23 +01006310 if (_Unpickler_SkipConsumed(self) < 0)
6311 return NULL;
6312
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006313 PDATA_POP(self->stack, value);
6314 return value;
6315}
6316
Larry Hastings61272b72014-01-07 12:41:53 -08006317/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006318
6319_pickle.Unpickler.load
6320
6321Load a pickle.
6322
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006323Read a pickled object representation from the open file object given
6324in the constructor, and return the reconstituted object hierarchy
6325specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006326[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006327
6328PyDoc_STRVAR(_pickle_Unpickler_load__doc__,
6329"load()\n"
6330"Load a pickle.\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006331"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006332"Read a pickled object representation from the open file object given\n"
6333"in the constructor, and return the reconstituted object hierarchy\n"
6334"specified therein.");
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006335
6336#define _PICKLE_UNPICKLER_LOAD_METHODDEF \
6337 {"load", (PyCFunction)_pickle_Unpickler_load, METH_NOARGS, _pickle_Unpickler_load__doc__},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006338
6339static PyObject *
Larry Hastings3cceb382014-01-04 11:09:09 -08006340_pickle_Unpickler_load_impl(PyObject *self);
6341
6342static PyObject *
6343_pickle_Unpickler_load(PyObject *self, PyObject *Py_UNUSED(ignored))
6344{
6345 PyObject *return_value = NULL;
6346
6347 return_value = _pickle_Unpickler_load_impl(self);
6348
6349 return return_value;
6350}
6351
6352static PyObject *
6353_pickle_Unpickler_load_impl(PyObject *self)
Larry Hastings61272b72014-01-07 12:41:53 -08006354/*[clinic end generated code: checksum=55f35fcaf034817e75c355ec50b7878577355899]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006355{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006356 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006357
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006358 /* Check whether the Unpickler was initialized correctly. This prevents
6359 segfaulting if a subclass overridden __init__ with a function that does
6360 not call Unpickler.__init__(). Here, we simply ensure that self->read
6361 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006362 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006363 PickleState *st = _Pickle_GetGlobalState();
6364 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006365 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006366 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006367 return NULL;
6368 }
6369
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006370 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006371}
6372
6373/* The name of find_class() is misleading. In newer pickle protocols, this
6374 function is used for loading any global (i.e., functions), not just
6375 classes. The name is kept only for backward compatibility. */
6376
Larry Hastings61272b72014-01-07 12:41:53 -08006377/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006378
6379_pickle.Unpickler.find_class
6380
6381 self: UnpicklerObject
6382 module_name: object
6383 global_name: object
6384 /
6385
6386Return an object from a specified module.
6387
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006388If necessary, the module will be imported. Subclasses may override
6389this method (e.g. to restrict unpickling of arbitrary classes and
6390functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006391
6392This method is called whenever a class or a function object is
6393needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006394[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006395
6396PyDoc_STRVAR(_pickle_Unpickler_find_class__doc__,
6397"find_class(module_name, global_name)\n"
6398"Return an object from a specified module.\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006399"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006400"If necessary, the module will be imported. Subclasses may override\n"
6401"this method (e.g. to restrict unpickling of arbitrary classes and\n"
6402"functions).\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006403"\n"
6404"This method is called whenever a class or a function object is\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006405"needed. Both arguments passed are str objects.");
6406
6407#define _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF \
6408 {"find_class", (PyCFunction)_pickle_Unpickler_find_class, METH_VARARGS, _pickle_Unpickler_find_class__doc__},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006409
6410static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006411_pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name);
6412
6413static PyObject *
6414_pickle_Unpickler_find_class(PyObject *self, PyObject *args)
6415{
6416 PyObject *return_value = NULL;
6417 PyObject *module_name;
6418 PyObject *global_name;
6419
6420 if (!PyArg_ParseTuple(args,
6421 "OO:find_class",
6422 &module_name, &global_name))
6423 goto exit;
6424 return_value = _pickle_Unpickler_find_class_impl((UnpicklerObject *)self, module_name, global_name);
6425
6426exit:
6427 return return_value;
6428}
6429
6430static PyObject *
6431_pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
Larry Hastings61272b72014-01-07 12:41:53 -08006432/*[clinic end generated code: checksum=1f353d13a32c9d94feb1466b3c2d0529a7e5650e]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006433{
6434 PyObject *global;
6435 PyObject *modules_dict;
6436 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006437 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006438
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006439 /* Try to map the old names used in Python 2.x to the new ones used in
6440 Python 3.x. We do this only with old pickle protocols and when the
6441 user has not disabled the feature. */
6442 if (self->proto < 3 && self->fix_imports) {
6443 PyObject *key;
6444 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006445 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006446
6447 /* Check if the global (i.e., a function or a class) was renamed
6448 or moved to another module. */
6449 key = PyTuple_Pack(2, module_name, global_name);
6450 if (key == NULL)
6451 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006452 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006453 Py_DECREF(key);
6454 if (item) {
6455 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6456 PyErr_Format(PyExc_RuntimeError,
6457 "_compat_pickle.NAME_MAPPING values should be "
6458 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6459 return NULL;
6460 }
6461 module_name = PyTuple_GET_ITEM(item, 0);
6462 global_name = PyTuple_GET_ITEM(item, 1);
6463 if (!PyUnicode_Check(module_name) ||
6464 !PyUnicode_Check(global_name)) {
6465 PyErr_Format(PyExc_RuntimeError,
6466 "_compat_pickle.NAME_MAPPING values should be "
6467 "pairs of str, not (%.200s, %.200s)",
6468 Py_TYPE(module_name)->tp_name,
6469 Py_TYPE(global_name)->tp_name);
6470 return NULL;
6471 }
6472 }
6473 else if (PyErr_Occurred()) {
6474 return NULL;
6475 }
6476
6477 /* Check if the module was renamed. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006478 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006479 if (item) {
6480 if (!PyUnicode_Check(item)) {
6481 PyErr_Format(PyExc_RuntimeError,
6482 "_compat_pickle.IMPORT_MAPPING values should be "
6483 "strings, not %.200s", Py_TYPE(item)->tp_name);
6484 return NULL;
6485 }
6486 module_name = item;
6487 }
6488 else if (PyErr_Occurred()) {
6489 return NULL;
6490 }
6491 }
6492
Victor Stinnerbb520202013-11-06 22:40:41 +01006493 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006494 if (modules_dict == NULL) {
6495 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006496 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006497 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006498
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006499 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006500 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006501 if (PyErr_Occurred())
6502 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006503 module = PyImport_Import(module_name);
6504 if (module == NULL)
6505 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006506 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006507 Py_DECREF(module);
6508 }
Victor Stinner121aab42011-09-29 23:40:53 +02006509 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006510 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006511 }
6512 return global;
6513}
6514
6515static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006516 _PICKLE_UNPICKLER_LOAD_METHODDEF
6517 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006518 {NULL, NULL} /* sentinel */
6519};
6520
6521static void
6522Unpickler_dealloc(UnpicklerObject *self)
6523{
6524 PyObject_GC_UnTrack((PyObject *)self);
6525 Py_XDECREF(self->readline);
6526 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006527 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006528 Py_XDECREF(self->stack);
6529 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006530 if (self->buffer.buf != NULL) {
6531 PyBuffer_Release(&self->buffer);
6532 self->buffer.buf = NULL;
6533 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006534
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006535 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006536 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006537 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006538 PyMem_Free(self->encoding);
6539 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006540
6541 Py_TYPE(self)->tp_free((PyObject *)self);
6542}
6543
6544static int
6545Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6546{
6547 Py_VISIT(self->readline);
6548 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006549 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006550 Py_VISIT(self->stack);
6551 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006552 return 0;
6553}
6554
6555static int
6556Unpickler_clear(UnpicklerObject *self)
6557{
6558 Py_CLEAR(self->readline);
6559 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006560 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006561 Py_CLEAR(self->stack);
6562 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006563 if (self->buffer.buf != NULL) {
6564 PyBuffer_Release(&self->buffer);
6565 self->buffer.buf = NULL;
6566 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006567
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006568 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006569 PyMem_Free(self->marks);
6570 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006571 PyMem_Free(self->input_line);
6572 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006573 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006574 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006575 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006576 self->errors = NULL;
6577
6578 return 0;
6579}
6580
Larry Hastings61272b72014-01-07 12:41:53 -08006581/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006582
6583_pickle.Unpickler.__init__
6584
6585 self: UnpicklerObject
6586 file: object
6587 *
6588 fix_imports: bool = True
6589 encoding: str = 'ASCII'
6590 errors: str = 'strict'
6591
6592This takes a binary file for reading a pickle data stream.
6593
6594The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006595protocol argument is needed. Bytes past the pickled object's
6596representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006597
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006598The argument *file* must have two methods, a read() method that takes
6599an integer argument, and a readline() method that requires no
6600arguments. Both methods should return bytes. Thus *file* can be a
6601binary file object opened for reading, a io.BytesIO object, or any
6602other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006603
6604Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6605which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006606generated by Python 2. If *fix_imports* is True, pickle will try to
6607map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006608*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006609instances pickled by Python 2; these default to 'ASCII' and 'strict',
6610respectively. The *encoding* can be 'bytes' to read these 8-bit
6611string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006612[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006613
6614PyDoc_STRVAR(_pickle_Unpickler___init____doc__,
6615"__init__(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006616"This takes a binary file for reading a pickle data stream.\n"
6617"\n"
6618"The protocol version of the pickle is detected automatically, so no\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006619"protocol argument is needed. Bytes past the pickled object\'s\n"
6620"representation are ignored.\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006621"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006622"The argument *file* must have two methods, a read() method that takes\n"
6623"an integer argument, and a readline() method that requires no\n"
6624"arguments. Both methods should return bytes. Thus *file* can be a\n"
6625"binary file object opened for reading, a io.BytesIO object, or any\n"
6626"other custom object that meets this interface.\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006627"\n"
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006628"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
6629"which are used to control compatiblity support for pickle stream\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006630"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
6631"map the old Python 2 names to the new names used in Python 3. The\n"
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006632"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006633"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
6634"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
6635"string instances as bytes objects.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006636
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006637static PyObject *
6638_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors);
6639
6640static PyObject *
6641_pickle_Unpickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006642{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006643 PyObject *return_value = NULL;
6644 static char *_keywords[] = {"file", "fix_imports", "encoding", "errors", NULL};
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006645 PyObject *file;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006646 int fix_imports = 1;
6647 const char *encoding = "ASCII";
6648 const char *errors = "strict";
6649
6650 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
6651 "O|$pss:__init__", _keywords,
6652 &file, &fix_imports, &encoding, &errors))
6653 goto exit;
6654 return_value = _pickle_Unpickler___init___impl((UnpicklerObject *)self, file, fix_imports, encoding, errors);
6655
6656exit:
6657 return return_value;
6658}
6659
6660static PyObject *
6661_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Larry Hastings8666e652014-01-12 14:12:59 -08006662/*[clinic end generated code: checksum=26c1d4a06841a8e51d29a0c244ba7f4607ff358a]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006663{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006664 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006665
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006666 /* In case of multiple __init__() calls, clear previous content. */
6667 if (self->read != NULL)
6668 (void)Unpickler_clear(self);
6669
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006670 if (_Unpickler_SetInputStream(self, file) < 0)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006671 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006672
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006673 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006674 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006675
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006676 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006677 if (self->fix_imports == -1)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006678 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006679
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006680 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006681 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6682 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006683 if (self->pers_func == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006684 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006685 }
6686 else {
6687 self->pers_func = NULL;
6688 }
6689
6690 self->stack = (Pdata *)Pdata_New();
6691 if (self->stack == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006692 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006693
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006694 self->memo_size = 32;
6695 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006696 if (self->memo == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006697 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006698
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006699 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006700
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006701 Py_RETURN_NONE;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006702}
6703
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006704/* Wrap the Clinic generated signature to slot it in tp_init. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006705static int
6706Unpickler_init(PyObject *self, PyObject *args, PyObject *kwargs)
6707{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006708 PyObject *result = _pickle_Unpickler___init__(self, args, kwargs);
6709 if (result == NULL) {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006710 return -1;
6711 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006712 Py_DECREF(result);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006713 return 0;
6714}
6715
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006716/* Define a proxy object for the Unpickler's internal memo object. This is to
6717 * avoid breaking code like:
6718 * unpickler.memo.clear()
6719 * and
6720 * unpickler.memo = saved_memo
6721 * Is this a good idea? Not really, but we don't want to break code that uses
6722 * it. Note that we don't implement the entire mapping API here. This is
6723 * intentional, as these should be treated as black-box implementation details.
6724 *
6725 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006726 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006727 */
6728
6729typedef struct {
6730 PyObject_HEAD
6731 UnpicklerObject *unpickler;
6732} UnpicklerMemoProxyObject;
6733
Larry Hastings61272b72014-01-07 12:41:53 -08006734/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006735_pickle.UnpicklerMemoProxy.clear
6736
6737 self: UnpicklerMemoProxyObject
6738
6739Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006740[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006741
6742PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_clear__doc__,
6743"clear()\n"
6744"Remove all items from memo.");
6745
6746#define _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF \
6747 {"clear", (PyCFunction)_pickle_UnpicklerMemoProxy_clear, METH_NOARGS, _pickle_UnpicklerMemoProxy_clear__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006748
6749static PyObject *
Larry Hastings3cceb382014-01-04 11:09:09 -08006750_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self);
6751
6752static PyObject *
6753_pickle_UnpicklerMemoProxy_clear(PyObject *self, PyObject *Py_UNUSED(ignored))
6754{
6755 PyObject *return_value = NULL;
6756
6757 return_value = _pickle_UnpicklerMemoProxy_clear_impl((UnpicklerMemoProxyObject *)self);
6758
6759 return return_value;
6760}
6761
6762static PyObject *
6763_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings61272b72014-01-07 12:41:53 -08006764/*[clinic end generated code: checksum=e0f99c26d48444a3f58f598bec3190c66595fce7]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006765{
6766 _Unpickler_MemoCleanup(self->unpickler);
6767 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6768 if (self->unpickler->memo == NULL)
6769 return NULL;
6770 Py_RETURN_NONE;
6771}
6772
Larry Hastings61272b72014-01-07 12:41:53 -08006773/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006774_pickle.UnpicklerMemoProxy.copy
6775
6776 self: UnpicklerMemoProxyObject
6777
6778Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006779[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006780
6781PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_copy__doc__,
6782"copy()\n"
6783"Copy the memo to a new object.");
6784
6785#define _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF \
6786 {"copy", (PyCFunction)_pickle_UnpicklerMemoProxy_copy, METH_NOARGS, _pickle_UnpicklerMemoProxy_copy__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006787
6788static PyObject *
Larry Hastings3cceb382014-01-04 11:09:09 -08006789_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self);
6790
6791static PyObject *
6792_pickle_UnpicklerMemoProxy_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
6793{
6794 PyObject *return_value = NULL;
6795
6796 return_value = _pickle_UnpicklerMemoProxy_copy_impl((UnpicklerMemoProxyObject *)self);
6797
6798 return return_value;
6799}
6800
6801static PyObject *
6802_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings61272b72014-01-07 12:41:53 -08006803/*[clinic end generated code: checksum=8c0ab91c0b694ea71a1774650898a760d1ab4765]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006804{
6805 Py_ssize_t i;
6806 PyObject *new_memo = PyDict_New();
6807 if (new_memo == NULL)
6808 return NULL;
6809
6810 for (i = 0; i < self->unpickler->memo_size; i++) {
6811 int status;
6812 PyObject *key, *value;
6813
6814 value = self->unpickler->memo[i];
6815 if (value == NULL)
6816 continue;
6817
6818 key = PyLong_FromSsize_t(i);
6819 if (key == NULL)
6820 goto error;
6821 status = PyDict_SetItem(new_memo, key, value);
6822 Py_DECREF(key);
6823 if (status < 0)
6824 goto error;
6825 }
6826 return new_memo;
6827
6828error:
6829 Py_DECREF(new_memo);
6830 return NULL;
6831}
6832
Larry Hastings61272b72014-01-07 12:41:53 -08006833/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006834_pickle.UnpicklerMemoProxy.__reduce__
6835
6836 self: UnpicklerMemoProxyObject
6837
6838Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006839[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006840
6841PyDoc_STRVAR(_pickle_UnpicklerMemoProxy___reduce____doc__,
6842"__reduce__()\n"
6843"Implement pickling support.");
6844
6845#define _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF \
6846 {"__reduce__", (PyCFunction)_pickle_UnpicklerMemoProxy___reduce__, METH_NOARGS, _pickle_UnpicklerMemoProxy___reduce____doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006847
6848static PyObject *
Larry Hastings3cceb382014-01-04 11:09:09 -08006849_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self);
6850
6851static PyObject *
6852_pickle_UnpicklerMemoProxy___reduce__(PyObject *self, PyObject *Py_UNUSED(ignored))
6853{
6854 PyObject *return_value = NULL;
6855
6856 return_value = _pickle_UnpicklerMemoProxy___reduce___impl((UnpicklerMemoProxyObject *)self);
6857
6858 return return_value;
6859}
6860
6861static PyObject *
6862_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings61272b72014-01-07 12:41:53 -08006863/*[clinic end generated code: checksum=4ee76a65511291f0de2e9e63db395d2e5d6d8df6]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006864{
6865 PyObject *reduce_value;
6866 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006867 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006868 if (contents == NULL)
6869 return NULL;
6870
6871 reduce_value = PyTuple_New(2);
6872 if (reduce_value == NULL) {
6873 Py_DECREF(contents);
6874 return NULL;
6875 }
6876 constructor_args = PyTuple_New(1);
6877 if (constructor_args == NULL) {
6878 Py_DECREF(contents);
6879 Py_DECREF(reduce_value);
6880 return NULL;
6881 }
6882 PyTuple_SET_ITEM(constructor_args, 0, contents);
6883 Py_INCREF((PyObject *)&PyDict_Type);
6884 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6885 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6886 return reduce_value;
6887}
6888
6889static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006890 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6891 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6892 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006893 {NULL, NULL} /* sentinel */
6894};
6895
6896static void
6897UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6898{
6899 PyObject_GC_UnTrack(self);
6900 Py_XDECREF(self->unpickler);
6901 PyObject_GC_Del((PyObject *)self);
6902}
6903
6904static int
6905UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6906 visitproc visit, void *arg)
6907{
6908 Py_VISIT(self->unpickler);
6909 return 0;
6910}
6911
6912static int
6913UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6914{
6915 Py_CLEAR(self->unpickler);
6916 return 0;
6917}
6918
6919static PyTypeObject UnpicklerMemoProxyType = {
6920 PyVarObject_HEAD_INIT(NULL, 0)
6921 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6922 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6923 0,
6924 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6925 0, /* tp_print */
6926 0, /* tp_getattr */
6927 0, /* tp_setattr */
6928 0, /* tp_compare */
6929 0, /* tp_repr */
6930 0, /* tp_as_number */
6931 0, /* tp_as_sequence */
6932 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006933 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006934 0, /* tp_call */
6935 0, /* tp_str */
6936 PyObject_GenericGetAttr, /* tp_getattro */
6937 PyObject_GenericSetAttr, /* tp_setattro */
6938 0, /* tp_as_buffer */
6939 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6940 0, /* tp_doc */
6941 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6942 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6943 0, /* tp_richcompare */
6944 0, /* tp_weaklistoffset */
6945 0, /* tp_iter */
6946 0, /* tp_iternext */
6947 unpicklerproxy_methods, /* tp_methods */
6948};
6949
6950static PyObject *
6951UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6952{
6953 UnpicklerMemoProxyObject *self;
6954
6955 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6956 &UnpicklerMemoProxyType);
6957 if (self == NULL)
6958 return NULL;
6959 Py_INCREF(unpickler);
6960 self->unpickler = unpickler;
6961 PyObject_GC_Track(self);
6962 return (PyObject *)self;
6963}
6964
6965/*****************************************************************************/
6966
6967
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006968static PyObject *
6969Unpickler_get_memo(UnpicklerObject *self)
6970{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006971 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006972}
6973
6974static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006975Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006976{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006977 PyObject **new_memo;
6978 Py_ssize_t new_memo_size = 0;
6979 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006980
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006981 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006982 PyErr_SetString(PyExc_TypeError,
6983 "attribute deletion is not supported");
6984 return -1;
6985 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006986
6987 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6988 UnpicklerObject *unpickler =
6989 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6990
6991 new_memo_size = unpickler->memo_size;
6992 new_memo = _Unpickler_NewMemo(new_memo_size);
6993 if (new_memo == NULL)
6994 return -1;
6995
6996 for (i = 0; i < new_memo_size; i++) {
6997 Py_XINCREF(unpickler->memo[i]);
6998 new_memo[i] = unpickler->memo[i];
6999 }
7000 }
7001 else if (PyDict_Check(obj)) {
7002 Py_ssize_t i = 0;
7003 PyObject *key, *value;
7004
7005 new_memo_size = PyDict_Size(obj);
7006 new_memo = _Unpickler_NewMemo(new_memo_size);
7007 if (new_memo == NULL)
7008 return -1;
7009
7010 while (PyDict_Next(obj, &i, &key, &value)) {
7011 Py_ssize_t idx;
7012 if (!PyLong_Check(key)) {
7013 PyErr_SetString(PyExc_TypeError,
7014 "memo key must be integers");
7015 goto error;
7016 }
7017 idx = PyLong_AsSsize_t(key);
7018 if (idx == -1 && PyErr_Occurred())
7019 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02007020 if (idx < 0) {
7021 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02007022 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02007023 goto error;
7024 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007025 if (_Unpickler_MemoPut(self, idx, value) < 0)
7026 goto error;
7027 }
7028 }
7029 else {
7030 PyErr_Format(PyExc_TypeError,
7031 "'memo' attribute must be an UnpicklerMemoProxy object"
7032 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007033 return -1;
7034 }
7035
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007036 _Unpickler_MemoCleanup(self);
7037 self->memo_size = new_memo_size;
7038 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007039
7040 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007041
7042 error:
7043 if (new_memo_size) {
7044 i = new_memo_size;
7045 while (--i >= 0) {
7046 Py_XDECREF(new_memo[i]);
7047 }
7048 PyMem_FREE(new_memo);
7049 }
7050 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007051}
7052
7053static PyObject *
7054Unpickler_get_persload(UnpicklerObject *self)
7055{
7056 if (self->pers_func == NULL)
7057 PyErr_SetString(PyExc_AttributeError, "persistent_load");
7058 else
7059 Py_INCREF(self->pers_func);
7060 return self->pers_func;
7061}
7062
7063static int
7064Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
7065{
7066 PyObject *tmp;
7067
7068 if (value == NULL) {
7069 PyErr_SetString(PyExc_TypeError,
7070 "attribute deletion is not supported");
7071 return -1;
7072 }
7073 if (!PyCallable_Check(value)) {
7074 PyErr_SetString(PyExc_TypeError,
7075 "persistent_load must be a callable taking "
7076 "one argument");
7077 return -1;
7078 }
7079
7080 tmp = self->pers_func;
7081 Py_INCREF(value);
7082 self->pers_func = value;
7083 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
7084
7085 return 0;
7086}
7087
7088static PyGetSetDef Unpickler_getsets[] = {
7089 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
7090 {"persistent_load", (getter)Unpickler_get_persload,
7091 (setter)Unpickler_set_persload},
7092 {NULL}
7093};
7094
7095static PyTypeObject Unpickler_Type = {
7096 PyVarObject_HEAD_INIT(NULL, 0)
7097 "_pickle.Unpickler", /*tp_name*/
7098 sizeof(UnpicklerObject), /*tp_basicsize*/
7099 0, /*tp_itemsize*/
7100 (destructor)Unpickler_dealloc, /*tp_dealloc*/
7101 0, /*tp_print*/
7102 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007103 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00007104 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007105 0, /*tp_repr*/
7106 0, /*tp_as_number*/
7107 0, /*tp_as_sequence*/
7108 0, /*tp_as_mapping*/
7109 0, /*tp_hash*/
7110 0, /*tp_call*/
7111 0, /*tp_str*/
7112 0, /*tp_getattro*/
7113 0, /*tp_setattro*/
7114 0, /*tp_as_buffer*/
7115 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007116 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007117 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7118 (inquiry)Unpickler_clear, /*tp_clear*/
7119 0, /*tp_richcompare*/
7120 0, /*tp_weaklistoffset*/
7121 0, /*tp_iter*/
7122 0, /*tp_iternext*/
7123 Unpickler_methods, /*tp_methods*/
7124 0, /*tp_members*/
7125 Unpickler_getsets, /*tp_getset*/
7126 0, /*tp_base*/
7127 0, /*tp_dict*/
7128 0, /*tp_descr_get*/
7129 0, /*tp_descr_set*/
7130 0, /*tp_dictoffset*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007131 Unpickler_init, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007132 PyType_GenericAlloc, /*tp_alloc*/
7133 PyType_GenericNew, /*tp_new*/
7134 PyObject_GC_Del, /*tp_free*/
7135 0, /*tp_is_gc*/
7136};
7137
Larry Hastings61272b72014-01-07 12:41:53 -08007138/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007139
7140_pickle.dump
7141
7142 obj: object
7143 file: object
7144 protocol: object = NULL
7145 *
7146 fix_imports: bool = True
7147
7148Write a pickled representation of obj to the open file object file.
7149
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007150This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7151be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007152
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007153The optional *protocol* argument tells the pickler to use the given
7154protocol supported protocols are 0, 1, 2, 3 and 4. The default
7155protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007156
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007157Specifying a negative protocol version selects the highest protocol
7158version supported. The higher the protocol used, the more recent the
7159version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007160
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007161The *file* argument must have a write() method that accepts a single
7162bytes argument. It can thus be a file object opened for binary
7163writing, a io.BytesIO instance, or any other custom object that meets
7164this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007165
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007166If *fix_imports* is True and protocol is less than 3, pickle will try
7167to map the new Python 3 names to the old module names used in Python
71682, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007169[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007170
7171PyDoc_STRVAR(_pickle_dump__doc__,
7172"dump(obj, file, protocol=None, *, fix_imports=True)\n"
7173"Write a pickled representation of obj to the open file object file.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007174"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007175"This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may\n"
7176"be more efficient.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007177"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007178"The optional *protocol* argument tells the pickler to use the given\n"
7179"protocol supported protocols are 0, 1, 2, 3 and 4. The default\n"
7180"protocol is 3; a backward-incompatible protocol designed for Python 3.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007181"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007182"Specifying a negative protocol version selects the highest protocol\n"
7183"version supported. The higher the protocol used, the more recent the\n"
7184"version of Python needed to read the pickle produced.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007185"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007186"The *file* argument must have a write() method that accepts a single\n"
7187"bytes argument. It can thus be a file object opened for binary\n"
7188"writing, a io.BytesIO instance, or any other custom object that meets\n"
7189"this interface.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007190"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007191"If *fix_imports* is True and protocol is less than 3, pickle will try\n"
7192"to map the new Python 3 names to the old module names used in Python\n"
7193"2, so that the pickle data stream is readable with Python 2.");
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007194
7195#define _PICKLE_DUMP_METHODDEF \
7196 {"dump", (PyCFunction)_pickle_dump, METH_VARARGS|METH_KEYWORDS, _pickle_dump__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007197
7198static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007199_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports);
7200
7201static PyObject *
7202_pickle_dump(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007203{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007204 PyObject *return_value = NULL;
7205 static char *_keywords[] = {"obj", "file", "protocol", "fix_imports", NULL};
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007206 PyObject *obj;
7207 PyObject *file;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007208 PyObject *protocol = NULL;
7209 int fix_imports = 1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007210
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007211 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
7212 "OO|O$p:dump", _keywords,
7213 &obj, &file, &protocol, &fix_imports))
7214 goto exit;
7215 return_value = _pickle_dump_impl(module, obj, file, protocol, fix_imports);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007216
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007217exit:
7218 return return_value;
7219}
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007220
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007221static PyObject *
7222_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports)
Larry Hastings61272b72014-01-07 12:41:53 -08007223/*[clinic end generated code: checksum=eb5c23e64da34477178230b704d2cc9c6b6650ea]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007224{
7225 PicklerObject *pickler = _Pickler_New();
7226
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007227 if (pickler == NULL)
7228 return NULL;
7229
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007230 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007231 goto error;
7232
7233 if (_Pickler_SetOutputStream(pickler, file) < 0)
7234 goto error;
7235
7236 if (dump(pickler, obj) < 0)
7237 goto error;
7238
7239 if (_Pickler_FlushToFile(pickler) < 0)
7240 goto error;
7241
7242 Py_DECREF(pickler);
7243 Py_RETURN_NONE;
7244
7245 error:
7246 Py_XDECREF(pickler);
7247 return NULL;
7248}
7249
Larry Hastings61272b72014-01-07 12:41:53 -08007250/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007251
7252_pickle.dumps
7253
7254 obj: object
7255 protocol: object = NULL
7256 *
7257 fix_imports: bool = True
7258
7259Return the pickled representation of the object as a bytes object.
7260
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007261The optional *protocol* argument tells the pickler to use the given
7262protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7263protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007264
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007265Specifying a negative protocol version selects the highest protocol
7266version supported. The higher the protocol used, the more recent the
7267version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007268
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007269If *fix_imports* is True and *protocol* is less than 3, pickle will
7270try to map the new Python 3 names to the old module names used in
7271Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007272[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007273
7274PyDoc_STRVAR(_pickle_dumps__doc__,
7275"dumps(obj, protocol=None, *, fix_imports=True)\n"
7276"Return the pickled representation of the object as a bytes object.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007277"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007278"The optional *protocol* argument tells the pickler to use the given\n"
7279"protocol; supported protocols are 0, 1, 2, 3 and 4. The default\n"
7280"protocol is 3; a backward-incompatible protocol designed for Python 3.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007281"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007282"Specifying a negative protocol version selects the highest protocol\n"
7283"version supported. The higher the protocol used, the more recent the\n"
7284"version of Python needed to read the pickle produced.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007285"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007286"If *fix_imports* is True and *protocol* is less than 3, pickle will\n"
7287"try to map the new Python 3 names to the old module names used in\n"
7288"Python 2, so that the pickle data stream is readable with Python 2.");
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007289
7290#define _PICKLE_DUMPS_METHODDEF \
7291 {"dumps", (PyCFunction)_pickle_dumps, METH_VARARGS|METH_KEYWORDS, _pickle_dumps__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007292
7293static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007294_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports);
7295
7296static PyObject *
7297_pickle_dumps(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007298{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007299 PyObject *return_value = NULL;
7300 static char *_keywords[] = {"obj", "protocol", "fix_imports", NULL};
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007301 PyObject *obj;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007302 PyObject *protocol = NULL;
7303 int fix_imports = 1;
7304
7305 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
7306 "O|O$p:dumps", _keywords,
7307 &obj, &protocol, &fix_imports))
7308 goto exit;
7309 return_value = _pickle_dumps_impl(module, obj, protocol, fix_imports);
7310
7311exit:
7312 return return_value;
7313}
7314
7315static PyObject *
7316_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports)
Larry Hastings61272b72014-01-07 12:41:53 -08007317/*[clinic end generated code: checksum=e9b915d61202a9692cb6c6718db74fe54fc9c4d1]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007318{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007319 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007320 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007321
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007322 if (pickler == NULL)
7323 return NULL;
7324
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007325 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007326 goto error;
7327
7328 if (dump(pickler, obj) < 0)
7329 goto error;
7330
7331 result = _Pickler_GetString(pickler);
7332 Py_DECREF(pickler);
7333 return result;
7334
7335 error:
7336 Py_XDECREF(pickler);
7337 return NULL;
7338}
7339
Larry Hastings61272b72014-01-07 12:41:53 -08007340/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007341
7342_pickle.load
7343
7344 file: object
7345 *
7346 fix_imports: bool = True
7347 encoding: str = 'ASCII'
7348 errors: str = 'strict'
7349
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007350Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007351
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007352This is equivalent to ``Unpickler(file).load()``, but may be more
7353efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007354
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007355The protocol version of the pickle is detected automatically, so no
7356protocol argument is needed. Bytes past the pickled object's
7357representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007358
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007359The argument *file* must have two methods, a read() method that takes
7360an integer argument, and a readline() method that requires no
7361arguments. Both methods should return bytes. Thus *file* can be a
7362binary file object opened for reading, a io.BytesIO object, or any
7363other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007364
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007365Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7366which are used to control compatiblity support for pickle stream
7367generated by Python 2. If *fix_imports* is True, pickle will try to
7368map the old Python 2 names to the new names used in Python 3. The
7369*encoding* and *errors* tell pickle how to decode 8-bit string
7370instances pickled by Python 2; these default to 'ASCII' and 'strict',
7371respectively. The *encoding* can be 'bytes' to read these 8-bit
7372string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007373[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007374
7375PyDoc_STRVAR(_pickle_load__doc__,
7376"load(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007377"Read and return an object from the pickle data stored in a file.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007378"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007379"This is equivalent to ``Unpickler(file).load()``, but may be more\n"
7380"efficient.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007381"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007382"The protocol version of the pickle is detected automatically, so no\n"
7383"protocol argument is needed. Bytes past the pickled object\'s\n"
7384"representation are ignored.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007385"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007386"The argument *file* must have two methods, a read() method that takes\n"
7387"an integer argument, and a readline() method that requires no\n"
7388"arguments. Both methods should return bytes. Thus *file* can be a\n"
7389"binary file object opened for reading, a io.BytesIO object, or any\n"
7390"other custom object that meets this interface.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007391"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007392"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
7393"which are used to control compatiblity support for pickle stream\n"
7394"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
7395"map the old Python 2 names to the new names used in Python 3. The\n"
7396"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
7397"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
7398"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
7399"string instances as bytes objects.");
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007400
7401#define _PICKLE_LOAD_METHODDEF \
7402 {"load", (PyCFunction)_pickle_load, METH_VARARGS|METH_KEYWORDS, _pickle_load__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007403
7404static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007405_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors);
7406
7407static PyObject *
7408_pickle_load(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007409{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007410 PyObject *return_value = NULL;
7411 static char *_keywords[] = {"file", "fix_imports", "encoding", "errors", NULL};
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007412 PyObject *file;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007413 int fix_imports = 1;
7414 const char *encoding = "ASCII";
7415 const char *errors = "strict";
7416
7417 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
7418 "O|$pss:load", _keywords,
7419 &file, &fix_imports, &encoding, &errors))
7420 goto exit;
7421 return_value = _pickle_load_impl(module, file, fix_imports, encoding, errors);
7422
7423exit:
7424 return return_value;
7425}
7426
7427static PyObject *
7428_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Larry Hastings61272b72014-01-07 12:41:53 -08007429/*[clinic end generated code: checksum=b41f06970e57acf2fd602e4b7f88e3f3e1e53087]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007430{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007431 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007432 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007433
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007434 if (unpickler == NULL)
7435 return NULL;
7436
7437 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7438 goto error;
7439
7440 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7441 goto error;
7442
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007443 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007444
7445 result = load(unpickler);
7446 Py_DECREF(unpickler);
7447 return result;
7448
7449 error:
7450 Py_XDECREF(unpickler);
7451 return NULL;
7452}
7453
Larry Hastings61272b72014-01-07 12:41:53 -08007454/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007455
7456_pickle.loads
7457
7458 data: object
7459 *
7460 fix_imports: bool = True
7461 encoding: str = 'ASCII'
7462 errors: str = 'strict'
7463
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007464Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007465
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007466The protocol version of the pickle is detected automatically, so no
7467protocol argument is needed. Bytes past the pickled object's
7468representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007469
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007470Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7471which are used to control compatiblity support for pickle stream
7472generated by Python 2. If *fix_imports* is True, pickle will try to
7473map the old Python 2 names to the new names used in Python 3. The
7474*encoding* and *errors* tell pickle how to decode 8-bit string
7475instances pickled by Python 2; these default to 'ASCII' and 'strict',
7476respectively. The *encoding* can be 'bytes' to read these 8-bit
7477string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007478[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007479
7480PyDoc_STRVAR(_pickle_loads__doc__,
7481"loads(data, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007482"Read and return an object from the given pickle data.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007483"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007484"The protocol version of the pickle is detected automatically, so no\n"
7485"protocol argument is needed. Bytes past the pickled object\'s\n"
7486"representation are ignored.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007487"\n"
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007488"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
7489"which are used to control compatiblity support for pickle stream\n"
7490"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
7491"map the old Python 2 names to the new names used in Python 3. The\n"
7492"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
7493"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
7494"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
7495"string instances as bytes objects.");
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007496
7497#define _PICKLE_LOADS_METHODDEF \
7498 {"loads", (PyCFunction)_pickle_loads, METH_VARARGS|METH_KEYWORDS, _pickle_loads__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007499
7500static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007501_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors);
7502
7503static PyObject *
7504_pickle_loads(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007505{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007506 PyObject *return_value = NULL;
7507 static char *_keywords[] = {"data", "fix_imports", "encoding", "errors", NULL};
7508 PyObject *data;
7509 int fix_imports = 1;
7510 const char *encoding = "ASCII";
7511 const char *errors = "strict";
7512
7513 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
7514 "O|$pss:loads", _keywords,
7515 &data, &fix_imports, &encoding, &errors))
7516 goto exit;
7517 return_value = _pickle_loads_impl(module, data, fix_imports, encoding, errors);
7518
7519exit:
7520 return return_value;
7521}
7522
7523static PyObject *
7524_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors)
Larry Hastings61272b72014-01-07 12:41:53 -08007525/*[clinic end generated code: checksum=0663de43aca6c21508a777e29d98c9c3a6e7f72d]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007526{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007527 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007528 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007529
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007530 if (unpickler == NULL)
7531 return NULL;
7532
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007533 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007534 goto error;
7535
7536 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7537 goto error;
7538
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007539 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007540
7541 result = load(unpickler);
7542 Py_DECREF(unpickler);
7543 return result;
7544
7545 error:
7546 Py_XDECREF(unpickler);
7547 return NULL;
7548}
7549
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007550static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007551 _PICKLE_DUMP_METHODDEF
7552 _PICKLE_DUMPS_METHODDEF
7553 _PICKLE_LOAD_METHODDEF
7554 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007555 {NULL, NULL} /* sentinel */
7556};
7557
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007558static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007559pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007560{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007561 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007562 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007563}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007564
Stefan Krahf483b0f2013-12-14 13:43:10 +01007565static void
7566pickle_free(PyObject *m)
7567{
7568 _Pickle_ClearState(_Pickle_GetState(m));
7569}
7570
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007571static int
7572pickle_traverse(PyObject *m, visitproc visit, void *arg)
7573{
7574 PickleState *st = _Pickle_GetState(m);
7575 Py_VISIT(st->PickleError);
7576 Py_VISIT(st->PicklingError);
7577 Py_VISIT(st->UnpicklingError);
7578 Py_VISIT(st->dispatch_table);
7579 Py_VISIT(st->extension_registry);
7580 Py_VISIT(st->extension_cache);
7581 Py_VISIT(st->inverted_registry);
7582 Py_VISIT(st->name_mapping_2to3);
7583 Py_VISIT(st->import_mapping_2to3);
7584 Py_VISIT(st->name_mapping_3to2);
7585 Py_VISIT(st->import_mapping_3to2);
7586 Py_VISIT(st->codecs_encode);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007587 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007588}
7589
7590static struct PyModuleDef _picklemodule = {
7591 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007592 "_pickle", /* m_name */
7593 pickle_module_doc, /* m_doc */
7594 sizeof(PickleState), /* m_size */
7595 pickle_methods, /* m_methods */
7596 NULL, /* m_reload */
7597 pickle_traverse, /* m_traverse */
7598 pickle_clear, /* m_clear */
7599 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007600};
7601
7602PyMODINIT_FUNC
7603PyInit__pickle(void)
7604{
7605 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007606 PickleState *st;
7607
7608 m = PyState_FindModule(&_picklemodule);
7609 if (m) {
7610 Py_INCREF(m);
7611 return m;
7612 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007613
7614 if (PyType_Ready(&Unpickler_Type) < 0)
7615 return NULL;
7616 if (PyType_Ready(&Pickler_Type) < 0)
7617 return NULL;
7618 if (PyType_Ready(&Pdata_Type) < 0)
7619 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007620 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7621 return NULL;
7622 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7623 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007624
7625 /* Create the module and add the functions. */
7626 m = PyModule_Create(&_picklemodule);
7627 if (m == NULL)
7628 return NULL;
7629
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007630 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007631 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7632 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007633 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007634 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7635 return NULL;
7636
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007637 st = _Pickle_GetState(m);
7638
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007639 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007640 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7641 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007642 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007643 st->PicklingError = \
7644 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7645 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007646 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007647 st->UnpicklingError = \
7648 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7649 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007650 return NULL;
7651
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007652 Py_INCREF(st->PickleError);
7653 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007654 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007655 Py_INCREF(st->PicklingError);
7656 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007657 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007658 Py_INCREF(st->UnpicklingError);
7659 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007660 return NULL;
7661
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007662 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007663 return NULL;
7664
7665 return m;
7666}