blob: 22ce7a504009aaed21c3fbead7520289715d8dbe [file] [log] [blame]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001#include "Python.h"
2#include "structmember.h"
3
4PyDoc_STRVAR(pickle_module_doc,
5"Optimized C implementation for the Python pickle module.");
6
7/* Bump this when new opcodes are added to the pickle protocol. */
8enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01009 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000010 DEFAULT_PROTOCOL = 3
11};
12
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000013/* Pickle opcodes. These must be kept updated with pickle.py.
14 Extensive docs are in pickletools.py. */
15enum opcode {
16 MARK = '(',
17 STOP = '.',
18 POP = '0',
19 POP_MARK = '1',
20 DUP = '2',
21 FLOAT = 'F',
22 INT = 'I',
23 BININT = 'J',
24 BININT1 = 'K',
25 LONG = 'L',
26 BININT2 = 'M',
27 NONE = 'N',
28 PERSID = 'P',
29 BINPERSID = 'Q',
30 REDUCE = 'R',
31 STRING = 'S',
32 BINSTRING = 'T',
33 SHORT_BINSTRING = 'U',
34 UNICODE = 'V',
35 BINUNICODE = 'X',
36 APPEND = 'a',
37 BUILD = 'b',
38 GLOBAL = 'c',
39 DICT = 'd',
40 EMPTY_DICT = '}',
41 APPENDS = 'e',
42 GET = 'g',
43 BINGET = 'h',
44 INST = 'i',
45 LONG_BINGET = 'j',
46 LIST = 'l',
47 EMPTY_LIST = ']',
48 OBJ = 'o',
49 PUT = 'p',
50 BINPUT = 'q',
51 LONG_BINPUT = 'r',
52 SETITEM = 's',
53 TUPLE = 't',
54 EMPTY_TUPLE = ')',
55 SETITEMS = 'u',
56 BINFLOAT = 'G',
57
58 /* Protocol 2. */
59 PROTO = '\x80',
60 NEWOBJ = '\x81',
61 EXT1 = '\x82',
62 EXT2 = '\x83',
63 EXT4 = '\x84',
64 TUPLE1 = '\x85',
65 TUPLE2 = '\x86',
66 TUPLE3 = '\x87',
67 NEWTRUE = '\x88',
68 NEWFALSE = '\x89',
69 LONG1 = '\x8a',
70 LONG4 = '\x8b',
71
72 /* Protocol 3 (Python 3.x) */
73 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010074 SHORT_BINBYTES = 'C',
75
76 /* Protocol 4 */
77 SHORT_BINUNICODE = '\x8c',
78 BINUNICODE8 = '\x8d',
79 BINBYTES8 = '\x8e',
80 EMPTY_SET = '\x8f',
81 ADDITEMS = '\x90',
82 FROZENSET = '\x91',
83 NEWOBJ_EX = '\x92',
84 STACK_GLOBAL = '\x93',
85 MEMOIZE = '\x94',
86 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000087};
88
89/* These aren't opcodes -- they're ways to pickle bools before protocol 2
90 * so that unpicklers written before bools were introduced unpickle them
91 * as ints, but unpicklers after can recognize that bools were intended.
92 * Note that protocol 2 added direct ways to pickle bools.
93 */
94#undef TRUE
95#define TRUE "I01\n"
96#undef FALSE
97#define FALSE "I00\n"
98
99enum {
100 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
101 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
102 break if this gets out of synch with pickle.py, but it's unclear that would
103 help anything either. */
104 BATCHSIZE = 1000,
105
106 /* Nesting limit until Pickler, when running in "fast mode", starts
107 checking for self-referential data-structures. */
108 FAST_NESTING_LIMIT = 50,
109
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000110 /* Initial size of the write buffer of Pickler. */
111 WRITE_BUF_SIZE = 4096,
112
113 /* Maximum size of the write buffer of Pickler when pickling to a
114 stream. This is ignored for in-memory pickling. */
115 MAX_WRITE_BUF_SIZE = 64 * 1024,
Antoine Pitrou04248a82010-10-12 20:51:21 +0000116
117 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100118 PREFETCH = 8192 * 16,
119
120 FRAME_SIZE_TARGET = 64 * 1024,
121
122 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000123};
124
125/* Exception classes for pickle. These should override the ones defined in
126 pickle.py, when the C-optimized Pickler and Unpickler are used. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000127static PyObject *PickleError = NULL;
128static PyObject *PicklingError = NULL;
129static PyObject *UnpicklingError = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000130
131/* copyreg.dispatch_table, {type_object: pickling_function} */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000132static PyObject *dispatch_table = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000133/* For EXT[124] opcodes. */
134/* copyreg._extension_registry, {(module_name, function_name): code} */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000135static PyObject *extension_registry = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000136/* copyreg._inverted_registry, {code: (module_name, function_name)} */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000137static PyObject *inverted_registry = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000138/* copyreg._extension_cache, {code: object} */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000139static PyObject *extension_cache = NULL;
140
141/* _compat_pickle.NAME_MAPPING, {(oldmodule, oldname): (newmodule, newname)} */
142static PyObject *name_mapping_2to3 = NULL;
143/* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
144static PyObject *import_mapping_2to3 = NULL;
145/* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
146static PyObject *name_mapping_3to2 = NULL;
147static PyObject *import_mapping_3to2 = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000148
149/* XXX: Are these really nescessary? */
150/* As the name says, an empty tuple. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000151static PyObject *empty_tuple = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000152/* For looking up name pairs in copyreg._extension_registry. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000153static PyObject *two_tuple = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000154
155static int
156stack_underflow(void)
157{
158 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
159 return -1;
160}
161
162/* Internal data type used as the unpickling stack. */
163typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000164 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000165 PyObject **data;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000166 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000167} Pdata;
168
169static void
170Pdata_dealloc(Pdata *self)
171{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200172 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000173 while (--i >= 0) {
174 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000175 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000176 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000177 PyObject_Del(self);
178}
179
180static PyTypeObject Pdata_Type = {
181 PyVarObject_HEAD_INIT(NULL, 0)
182 "_pickle.Pdata", /*tp_name*/
183 sizeof(Pdata), /*tp_basicsize*/
184 0, /*tp_itemsize*/
185 (destructor)Pdata_dealloc, /*tp_dealloc*/
186};
187
188static PyObject *
189Pdata_New(void)
190{
191 Pdata *self;
192
193 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
194 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000195 Py_SIZE(self) = 0;
196 self->allocated = 8;
197 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000198 if (self->data)
199 return (PyObject *)self;
200 Py_DECREF(self);
201 return PyErr_NoMemory();
202}
203
204
205/* Retain only the initial clearto items. If clearto >= the current
206 * number of items, this is a (non-erroneous) NOP.
207 */
208static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200209Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000210{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200211 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000212
213 if (clearto < 0)
214 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000215 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000216 return 0;
217
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000218 while (--i >= clearto) {
219 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000220 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000221 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000222 return 0;
223}
224
225static int
226Pdata_grow(Pdata *self)
227{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000228 PyObject **data = self->data;
229 Py_ssize_t allocated = self->allocated;
230 Py_ssize_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000231
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000232 new_allocated = (allocated >> 3) + 6;
233 /* check for integer overflow */
234 if (new_allocated > PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000235 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000236 new_allocated += allocated;
237 if (new_allocated > (PY_SSIZE_T_MAX / sizeof(PyObject *)))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000238 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000239 data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *));
240 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000241 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000242
243 self->data = data;
244 self->allocated = new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000245 return 0;
246
247 nomemory:
248 PyErr_NoMemory();
249 return -1;
250}
251
252/* D is a Pdata*. Pop the topmost element and store it into V, which
253 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
254 * is raised and V is set to NULL.
255 */
256static PyObject *
257Pdata_pop(Pdata *self)
258{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000259 if (Py_SIZE(self) == 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000260 PyErr_SetString(UnpicklingError, "bad pickle data");
261 return NULL;
262 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000263 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000264}
265#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
266
267static int
268Pdata_push(Pdata *self, PyObject *obj)
269{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000270 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000271 return -1;
272 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000273 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000274 return 0;
275}
276
277/* Push an object on stack, transferring its ownership to the stack. */
278#define PDATA_PUSH(D, O, ER) do { \
279 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
280
281/* Push an object on stack, adding a new reference to the object. */
282#define PDATA_APPEND(D, O, ER) do { \
283 Py_INCREF((O)); \
284 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
285
286static PyObject *
287Pdata_poptuple(Pdata *self, Py_ssize_t start)
288{
289 PyObject *tuple;
290 Py_ssize_t len, i, j;
291
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000292 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000293 tuple = PyTuple_New(len);
294 if (tuple == NULL)
295 return NULL;
296 for (i = start, j = 0; j < len; i++, j++)
297 PyTuple_SET_ITEM(tuple, j, self->data[i]);
298
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000299 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000300 return tuple;
301}
302
303static PyObject *
304Pdata_poplist(Pdata *self, Py_ssize_t start)
305{
306 PyObject *list;
307 Py_ssize_t len, i, j;
308
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000309 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000310 list = PyList_New(len);
311 if (list == NULL)
312 return NULL;
313 for (i = start, j = 0; j < len; i++, j++)
314 PyList_SET_ITEM(list, j, self->data[i]);
315
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000316 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000317 return list;
318}
319
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000320typedef struct {
321 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200322 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000323} PyMemoEntry;
324
325typedef struct {
326 Py_ssize_t mt_mask;
327 Py_ssize_t mt_used;
328 Py_ssize_t mt_allocated;
329 PyMemoEntry *mt_table;
330} PyMemoTable;
331
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000332typedef struct PicklerObject {
333 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000334 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000335 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000336 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000337 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100338 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000339 PyObject *arg;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000340
341 PyObject *write; /* write() method of the output stream. */
342 PyObject *output_buffer; /* Write into a local bytearray buffer before
343 flushing to the stream. */
344 Py_ssize_t output_len; /* Length of output_buffer. */
345 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000346 int proto; /* Pickle protocol number, >= 0 */
347 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100348 int framing; /* True when framing is enabled, proto >= 4 */
349 Py_ssize_t frame_start; /* Position in output_buffer where the
350 where the current frame begins. -1 if there
351 is no frame currently open. */
352
353 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000354 int fast; /* Enable fast mode if set to a true value.
355 The fast mode disable the usage of memo,
356 therefore speeding the pickling process by
357 not generating superfluous PUT opcodes. It
358 should not be used if with self-referential
359 objects. */
360 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000361 int fix_imports; /* Indicate whether Pickler should fix
362 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000363 PyObject *fast_memo;
364} PicklerObject;
365
366typedef struct UnpicklerObject {
367 PyObject_HEAD
368 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000369
370 /* The unpickler memo is just an array of PyObject *s. Using a dict
371 is unnecessary, since the keys are contiguous ints. */
372 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100373 Py_ssize_t memo_size; /* Capacity of the memo array */
374 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000375
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000376 PyObject *arg;
377 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000378
379 Py_buffer buffer;
380 char *input_buffer;
381 char *input_line;
382 Py_ssize_t input_len;
383 Py_ssize_t next_read_idx;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100384 Py_ssize_t frame_end_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000385 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100386
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000387 PyObject *read; /* read() method of the input stream. */
388 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000389 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000390
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000391 char *encoding; /* Name of the encoding to be used for
392 decoding strings pickled using Python
393 2.x. The default value is "ASCII" */
394 char *errors; /* Name of errors handling scheme to used when
395 decoding strings. The default value is
396 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500397 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000398 objects. */
399 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
400 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000401 int proto; /* Protocol of the pickle loaded. */
402 int fix_imports; /* Indicate whether Unpickler should fix
403 the name of globals pickled by Python 2.x. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100404 int framing; /* True when framing is enabled, proto >= 4 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000405} UnpicklerObject;
406
407/* Forward declarations */
408static int save(PicklerObject *, PyObject *, int);
409static int save_reduce(PicklerObject *, PyObject *, PyObject *);
410static PyTypeObject Pickler_Type;
411static PyTypeObject Unpickler_Type;
412
413
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000414/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300415 A custom hashtable mapping void* to Python ints. This is used by the pickler
416 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000417 a bunch of unnecessary object creation. This makes a huge performance
418 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000419
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000420#define MT_MINSIZE 8
421#define PERTURB_SHIFT 5
422
423
424static PyMemoTable *
425PyMemoTable_New(void)
426{
427 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
428 if (memo == NULL) {
429 PyErr_NoMemory();
430 return NULL;
431 }
432
433 memo->mt_used = 0;
434 memo->mt_allocated = MT_MINSIZE;
435 memo->mt_mask = MT_MINSIZE - 1;
436 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
437 if (memo->mt_table == NULL) {
438 PyMem_FREE(memo);
439 PyErr_NoMemory();
440 return NULL;
441 }
442 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
443
444 return memo;
445}
446
447static PyMemoTable *
448PyMemoTable_Copy(PyMemoTable *self)
449{
450 Py_ssize_t i;
451 PyMemoTable *new = PyMemoTable_New();
452 if (new == NULL)
453 return NULL;
454
455 new->mt_used = self->mt_used;
456 new->mt_allocated = self->mt_allocated;
457 new->mt_mask = self->mt_mask;
458 /* The table we get from _New() is probably smaller than we wanted.
459 Free it and allocate one that's the right size. */
460 PyMem_FREE(new->mt_table);
461 new->mt_table = PyMem_MALLOC(self->mt_allocated * sizeof(PyMemoEntry));
462 if (new->mt_table == NULL) {
463 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200464 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000465 return NULL;
466 }
467 for (i = 0; i < self->mt_allocated; i++) {
468 Py_XINCREF(self->mt_table[i].me_key);
469 }
470 memcpy(new->mt_table, self->mt_table,
471 sizeof(PyMemoEntry) * self->mt_allocated);
472
473 return new;
474}
475
476static Py_ssize_t
477PyMemoTable_Size(PyMemoTable *self)
478{
479 return self->mt_used;
480}
481
482static int
483PyMemoTable_Clear(PyMemoTable *self)
484{
485 Py_ssize_t i = self->mt_allocated;
486
487 while (--i >= 0) {
488 Py_XDECREF(self->mt_table[i].me_key);
489 }
490 self->mt_used = 0;
491 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
492 return 0;
493}
494
495static void
496PyMemoTable_Del(PyMemoTable *self)
497{
498 if (self == NULL)
499 return;
500 PyMemoTable_Clear(self);
501
502 PyMem_FREE(self->mt_table);
503 PyMem_FREE(self);
504}
505
506/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
507 can be considerably simpler than dictobject.c's lookdict(). */
508static PyMemoEntry *
509_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
510{
511 size_t i;
512 size_t perturb;
513 size_t mask = (size_t)self->mt_mask;
514 PyMemoEntry *table = self->mt_table;
515 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000516 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000517
518 i = hash & mask;
519 entry = &table[i];
520 if (entry->me_key == NULL || entry->me_key == key)
521 return entry;
522
523 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
524 i = (i << 2) + i + perturb + 1;
525 entry = &table[i & mask];
526 if (entry->me_key == NULL || entry->me_key == key)
527 return entry;
528 }
529 assert(0); /* Never reached */
530 return NULL;
531}
532
533/* Returns -1 on failure, 0 on success. */
534static int
535_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
536{
537 PyMemoEntry *oldtable = NULL;
538 PyMemoEntry *oldentry, *newentry;
539 Py_ssize_t new_size = MT_MINSIZE;
540 Py_ssize_t to_process;
541
542 assert(min_size > 0);
543
544 /* Find the smallest valid table size >= min_size. */
545 while (new_size < min_size && new_size > 0)
546 new_size <<= 1;
547 if (new_size <= 0) {
548 PyErr_NoMemory();
549 return -1;
550 }
551 /* new_size needs to be a power of two. */
552 assert((new_size & (new_size - 1)) == 0);
553
554 /* Allocate new table. */
555 oldtable = self->mt_table;
556 self->mt_table = PyMem_MALLOC(new_size * sizeof(PyMemoEntry));
557 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200558 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000559 PyErr_NoMemory();
560 return -1;
561 }
562 self->mt_allocated = new_size;
563 self->mt_mask = new_size - 1;
564 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
565
566 /* Copy entries from the old table. */
567 to_process = self->mt_used;
568 for (oldentry = oldtable; to_process > 0; oldentry++) {
569 if (oldentry->me_key != NULL) {
570 to_process--;
571 /* newentry is a pointer to a chunk of the new
572 mt_table, so we're setting the key:value pair
573 in-place. */
574 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
575 newentry->me_key = oldentry->me_key;
576 newentry->me_value = oldentry->me_value;
577 }
578 }
579
580 /* Deallocate the old table. */
581 PyMem_FREE(oldtable);
582 return 0;
583}
584
585/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200586static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000587PyMemoTable_Get(PyMemoTable *self, PyObject *key)
588{
589 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
590 if (entry->me_key == NULL)
591 return NULL;
592 return &entry->me_value;
593}
594
595/* Returns -1 on failure, 0 on success. */
596static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200597PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000598{
599 PyMemoEntry *entry;
600
601 assert(key != NULL);
602
603 entry = _PyMemoTable_Lookup(self, key);
604 if (entry->me_key != NULL) {
605 entry->me_value = value;
606 return 0;
607 }
608 Py_INCREF(key);
609 entry->me_key = key;
610 entry->me_value = value;
611 self->mt_used++;
612
613 /* If we added a key, we can safely resize. Otherwise just return!
614 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
615 *
616 * Quadrupling the size improves average table sparseness
617 * (reducing collisions) at the cost of some memory. It also halves
618 * the number of expensive resize operations in a growing memo table.
619 *
620 * Very large memo tables (over 50K items) use doubling instead.
621 * This may help applications with severe memory constraints.
622 */
623 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
624 return 0;
625 return _PyMemoTable_ResizeTable(self,
626 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
627}
628
629#undef MT_MINSIZE
630#undef PERTURB_SHIFT
631
632/*************************************************************************/
633
634/* Helpers for creating the argument tuple passed to functions. This has the
Victor Stinner121aab42011-09-29 23:40:53 +0200635 performance advantage of calling PyTuple_New() only once.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000636
637 XXX(avassalotti): Inline directly in _Pickler_FastCall() and
638 _Unpickler_FastCall(). */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000639#define ARG_TUP(self, obj) do { \
640 if ((self)->arg || ((self)->arg=PyTuple_New(1))) { \
641 Py_XDECREF(PyTuple_GET_ITEM((self)->arg, 0)); \
642 PyTuple_SET_ITEM((self)->arg, 0, (obj)); \
643 } \
644 else { \
645 Py_DECREF((obj)); \
646 } \
647 } while (0)
648
649#define FREE_ARG_TUP(self) do { \
650 if ((self)->arg->ob_refcnt > 1) \
651 Py_CLEAR((self)->arg); \
652 } while (0)
653
654/* A temporary cleaner API for fast single argument function call.
655
656 XXX: Does caching the argument tuple provides any real performance benefits?
657
658 A quick benchmark, on a 2.0GHz Athlon64 3200+ running Linux 2.6.24 with
659 glibc 2.7, tells me that it takes roughly 20,000,000 PyTuple_New(1) calls
660 when the tuple is retrieved from the freelist (i.e, call PyTuple_New() then
661 immediately DECREF it) and 1,200,000 calls when allocating brand new tuples
662 (i.e, call PyTuple_New() and store the returned value in an array), to save
663 one second (wall clock time). Either ways, the loading time a pickle stream
664 large enough to generate this number of calls would be massively
665 overwhelmed by other factors, like I/O throughput, the GC traversal and
666 object allocation overhead. So, I really doubt these functions provide any
667 real benefits.
668
669 On the other hand, oprofile reports that pickle spends a lot of time in
670 these functions. But, that is probably more related to the function call
671 overhead, than the argument tuple allocation.
672
673 XXX: And, what is the reference behavior of these? Steal, borrow? At first
674 glance, it seems to steal the reference of 'arg' and borrow the reference
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000675 of 'func'. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000676static PyObject *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000677_Pickler_FastCall(PicklerObject *self, PyObject *func, PyObject *arg)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000678{
679 PyObject *result = NULL;
680
681 ARG_TUP(self, arg);
682 if (self->arg) {
683 result = PyObject_Call(func, self->arg, NULL);
684 FREE_ARG_TUP(self);
685 }
686 return result;
687}
688
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000689static int
690_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000691{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000692 Py_CLEAR(self->output_buffer);
693 self->output_buffer =
694 PyBytes_FromStringAndSize(NULL, self->max_output_len);
695 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000696 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000697 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100698 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000699 return 0;
700}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000701
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100702static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100703_write_size64(char *out, size_t value)
704{
705 out[0] = (unsigned char)(value & 0xff);
706 out[1] = (unsigned char)((value >> 8) & 0xff);
707 out[2] = (unsigned char)((value >> 16) & 0xff);
708 out[3] = (unsigned char)((value >> 24) & 0xff);
709#if SIZEOF_SIZE_T >= 8
710 out[4] = (unsigned char)((value >> 32) & 0xff);
711 out[5] = (unsigned char)((value >> 40) & 0xff);
712 out[6] = (unsigned char)((value >> 48) & 0xff);
713 out[7] = (unsigned char)((value >> 56) & 0xff);
714#else
715 out[4] = out[5] = out[6] = out[7] = 0;
716#endif
717}
718
719static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100720_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
721{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100722 qdata[0] = FRAME;
723 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100724}
725
726static int
727_Pickler_CommitFrame(PicklerObject *self)
728{
729 size_t frame_len;
730 char *qdata;
731
732 if (!self->framing || self->frame_start == -1)
733 return 0;
734 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
735 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
736 _Pickler_WriteFrameHeader(self, qdata, frame_len);
737 self->frame_start = -1;
738 return 0;
739}
740
741static int
742_Pickler_OpcodeBoundary(PicklerObject *self)
743{
744 Py_ssize_t frame_len;
745
746 if (!self->framing || self->frame_start == -1)
747 return 0;
748 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
749 if (frame_len >= FRAME_SIZE_TARGET)
750 return _Pickler_CommitFrame(self);
751 else
752 return 0;
753}
754
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000755static PyObject *
756_Pickler_GetString(PicklerObject *self)
757{
758 PyObject *output_buffer = self->output_buffer;
759
760 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100761
762 if (_Pickler_CommitFrame(self))
763 return NULL;
764
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000765 self->output_buffer = NULL;
766 /* Resize down to exact size */
767 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
768 return NULL;
769 return output_buffer;
770}
771
772static int
773_Pickler_FlushToFile(PicklerObject *self)
774{
775 PyObject *output, *result;
776
777 assert(self->write != NULL);
778
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100779 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000780 output = _Pickler_GetString(self);
781 if (output == NULL)
782 return -1;
783
784 result = _Pickler_FastCall(self, self->write, output);
785 Py_XDECREF(result);
786 return (result == NULL) ? -1 : 0;
787}
788
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200789static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100790_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000791{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100792 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000793 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100794 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000795
796 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100797 need_new_frame = (self->framing && self->frame_start == -1);
798
799 if (need_new_frame)
800 n = data_len + FRAME_HEADER_SIZE;
801 else
802 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000803
804 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100805 if (self->write != NULL && required > MAX_WRITE_BUF_SIZE) {
806 /* XXX This reallocates a new buffer every time, which is a bit
807 wasteful. */
808 if (_Pickler_FlushToFile(self) < 0)
809 return -1;
810 if (_Pickler_ClearBuffer(self) < 0)
811 return -1;
812 /* The previous frame was just committed by _Pickler_FlushToFile */
813 need_new_frame = self->framing;
814 if (need_new_frame)
815 n = data_len + FRAME_HEADER_SIZE;
816 else
817 n = data_len;
818 required = self->output_len + n;
819 }
820 if (self->write != NULL && n > MAX_WRITE_BUF_SIZE) {
821 /* For large pickle chunks, we write directly to the output
822 file instead of buffering. Note the buffer is empty at this
823 point (it was flushed above, since required >= n). */
824 PyObject *output, *result;
825 if (need_new_frame) {
826 char frame_header[FRAME_HEADER_SIZE];
827 _Pickler_WriteFrameHeader(self, frame_header, (size_t) data_len);
828 output = PyBytes_FromStringAndSize(frame_header, FRAME_HEADER_SIZE);
829 if (output == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000830 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000831 result = _Pickler_FastCall(self, self->write, output);
832 Py_XDECREF(result);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100833 if (result == NULL)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000834 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000835 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100836 /* XXX we could spare an intermediate copy and pass
837 a memoryview instead */
838 output = PyBytes_FromStringAndSize(s, data_len);
839 if (output == NULL)
840 return -1;
841 result = _Pickler_FastCall(self, self->write, output);
842 Py_XDECREF(result);
843 return (result == NULL) ? -1 : 0;
844 }
845 if (required > self->max_output_len) {
846 /* Make place in buffer for the pickle chunk */
847 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
848 PyErr_NoMemory();
849 return -1;
850 }
851 self->max_output_len = (self->output_len + n) / 2 * 3;
852 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
853 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000854 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000855 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100856 if (need_new_frame) {
857 /* Setup new frame */
858 Py_ssize_t frame_start = self->output_len;
859 self->frame_start = frame_start;
860 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
861 /* Write an invalid value, for debugging */
862 buffer[frame_start + i] = 0xFE;
863 }
864 self->output_len += FRAME_HEADER_SIZE;
865 }
866 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000867 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100868 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000869 buffer[self->output_len + i] = s[i];
870 }
871 }
872 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100873 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000874 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100875 self->output_len += data_len;
876 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000877}
878
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000879static PicklerObject *
880_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000881{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000882 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000883
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000884 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
885 if (self == NULL)
886 return NULL;
887
888 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100889 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000890 self->arg = NULL;
891 self->write = NULL;
892 self->proto = 0;
893 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100894 self->framing = 0;
895 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000896 self->fast = 0;
897 self->fast_nesting = 0;
898 self->fix_imports = 0;
899 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000900 self->max_output_len = WRITE_BUF_SIZE;
901 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +0200902
903 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000904 self->output_buffer = PyBytes_FromStringAndSize(NULL,
905 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +0200906
907 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +0200908 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000909 return NULL;
910 }
911 return self;
912}
913
914static int
915_Pickler_SetProtocol(PicklerObject *self, PyObject *proto_obj,
916 PyObject *fix_imports_obj)
917{
918 long proto = 0;
919 int fix_imports;
920
921 if (proto_obj == NULL || proto_obj == Py_None)
922 proto = DEFAULT_PROTOCOL;
923 else {
924 proto = PyLong_AsLong(proto_obj);
925 if (proto == -1 && PyErr_Occurred())
926 return -1;
927 }
928 if (proto < 0)
929 proto = HIGHEST_PROTOCOL;
930 if (proto > HIGHEST_PROTOCOL) {
931 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
932 HIGHEST_PROTOCOL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000933 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000934 }
935 fix_imports = PyObject_IsTrue(fix_imports_obj);
936 if (fix_imports == -1)
937 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +0200938
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000939 self->proto = proto;
940 self->bin = proto > 0;
941 self->fix_imports = fix_imports && proto < 3;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000942
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000943 return 0;
944}
945
946/* Returns -1 (with an exception set) on failure, 0 on success. This may
947 be called once on a freshly created Pickler. */
948static int
949_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
950{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200951 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000952 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200953 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000954 if (self->write == NULL) {
955 if (PyErr_ExceptionMatches(PyExc_AttributeError))
956 PyErr_SetString(PyExc_TypeError,
957 "file must have a 'write' attribute");
958 return -1;
959 }
960
961 return 0;
962}
963
964/* See documentation for _Pickler_FastCall(). */
965static PyObject *
966_Unpickler_FastCall(UnpicklerObject *self, PyObject *func, PyObject *arg)
967{
968 PyObject *result = NULL;
969
970 ARG_TUP(self, arg);
971 if (self->arg) {
972 result = PyObject_Call(func, self->arg, NULL);
973 FREE_ARG_TUP(self);
974 }
975 return result;
976}
977
978/* Returns the size of the input on success, -1 on failure. This takes its
979 own reference to `input`. */
980static Py_ssize_t
981_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
982{
983 if (self->buffer.buf != NULL)
984 PyBuffer_Release(&self->buffer);
985 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
986 return -1;
987 self->input_buffer = self->buffer.buf;
988 self->input_len = self->buffer.len;
989 self->next_read_idx = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100990 self->frame_end_idx = -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000991 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000992 return self->input_len;
993}
994
Antoine Pitrou04248a82010-10-12 20:51:21 +0000995static int
996_Unpickler_SkipConsumed(UnpicklerObject *self)
997{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +0100998 Py_ssize_t consumed;
999 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001000
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001001 consumed = self->next_read_idx - self->prefetched_idx;
1002 if (consumed <= 0)
1003 return 0;
1004
1005 assert(self->peek); /* otherwise we did something wrong */
1006 /* This makes an useless copy... */
1007 r = PyObject_CallFunction(self->read, "n", consumed);
1008 if (r == NULL)
1009 return -1;
1010 Py_DECREF(r);
1011
1012 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001013 return 0;
1014}
1015
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001016static const Py_ssize_t READ_WHOLE_LINE = -1;
1017
1018/* If reading from a file, we need to only pull the bytes we need, since there
1019 may be multiple pickle objects arranged contiguously in the same input
1020 buffer.
1021
1022 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1023 bytes from the input stream/buffer.
1024
1025 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1026 failure; on success, returns the number of bytes read from the file.
1027
1028 On success, self->input_len will be 0; this is intentional so that when
1029 unpickling from a file, the "we've run out of data" code paths will trigger,
1030 causing the Unpickler to go back to the file for more data. Use the returned
1031 size to tell you how much data you can process. */
1032static Py_ssize_t
1033_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1034{
1035 PyObject *data;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001036 Py_ssize_t read_size, prefetched_size = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001037
1038 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001039
Antoine Pitrou04248a82010-10-12 20:51:21 +00001040 if (_Unpickler_SkipConsumed(self) < 0)
1041 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001042
1043 if (n == READ_WHOLE_LINE)
1044 data = PyObject_Call(self->readline, empty_tuple, NULL);
1045 else {
1046 PyObject *len = PyLong_FromSsize_t(n);
1047 if (len == NULL)
1048 return -1;
1049 data = _Unpickler_FastCall(self, self->read, len);
1050 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001051 if (data == NULL)
1052 return -1;
1053
Antoine Pitrou04248a82010-10-12 20:51:21 +00001054 /* Prefetch some data without advancing the file pointer, if possible */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001055 if (self->peek && !self->framing) {
Antoine Pitrou04248a82010-10-12 20:51:21 +00001056 PyObject *len, *prefetched;
1057 len = PyLong_FromSsize_t(PREFETCH);
1058 if (len == NULL) {
1059 Py_DECREF(data);
1060 return -1;
1061 }
1062 prefetched = _Unpickler_FastCall(self, self->peek, len);
1063 if (prefetched == NULL) {
1064 if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) {
1065 /* peek() is probably not supported by the given file object */
1066 PyErr_Clear();
1067 Py_CLEAR(self->peek);
1068 }
1069 else {
1070 Py_DECREF(data);
1071 return -1;
1072 }
1073 }
1074 else {
1075 assert(PyBytes_Check(prefetched));
1076 prefetched_size = PyBytes_GET_SIZE(prefetched);
1077 PyBytes_ConcatAndDel(&data, prefetched);
1078 if (data == NULL)
1079 return -1;
1080 }
1081 }
1082
1083 read_size = _Unpickler_SetStringInput(self, data) - prefetched_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001084 Py_DECREF(data);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001085 self->prefetched_idx = read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001086 return read_size;
1087}
1088
1089/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1090
1091 This should be used for all data reads, rather than accessing the unpickler's
1092 input buffer directly. This method deals correctly with reading from input
1093 streams, which the input buffer doesn't deal with.
1094
1095 Note that when reading from a file-like object, self->next_read_idx won't
1096 be updated (it should remain at 0 for the entire unpickling process). You
1097 should use this function's return value to know how many bytes you can
1098 consume.
1099
1100 Returns -1 (with an exception set) on failure. On success, return the
1101 number of chars read. */
1102static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001103_Unpickler_ReadUnframed(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001104{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001105 Py_ssize_t num_read;
1106
Antoine Pitrou04248a82010-10-12 20:51:21 +00001107 if (self->next_read_idx + n <= self->input_len) {
1108 *s = self->input_buffer + self->next_read_idx;
1109 self->next_read_idx += n;
1110 return n;
1111 }
1112 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001113 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001114 return -1;
1115 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001116 num_read = _Unpickler_ReadFromFile(self, n);
1117 if (num_read < 0)
1118 return -1;
1119 if (num_read < n) {
1120 PyErr_Format(PyExc_EOFError, "Ran out of input");
1121 return -1;
1122 }
1123 *s = self->input_buffer;
1124 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001125 return n;
1126}
1127
1128static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001129_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
1130{
1131 if (self->framing &&
1132 (self->frame_end_idx == -1 ||
1133 self->frame_end_idx <= self->next_read_idx)) {
1134 /* Need to read new frame */
Gregory P. Smith2b38fc12013-11-23 20:21:28 +00001135 char *dummy = NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001136 unsigned char *frame_start;
1137 size_t frame_len;
1138 if (_Unpickler_ReadUnframed(self, &dummy, FRAME_HEADER_SIZE) < 0)
1139 return -1;
1140 frame_start = (unsigned char *) dummy;
1141 if (frame_start[0] != (unsigned char)FRAME) {
1142 PyErr_Format(UnpicklingError,
1143 "expected FRAME opcode, got 0x%x instead",
1144 frame_start[0]);
1145 return -1;
1146 }
1147 frame_len = (size_t) frame_start[1];
1148 frame_len |= (size_t) frame_start[2] << 8;
1149 frame_len |= (size_t) frame_start[3] << 16;
1150 frame_len |= (size_t) frame_start[4] << 24;
1151#if SIZEOF_SIZE_T >= 8
1152 frame_len |= (size_t) frame_start[5] << 32;
1153 frame_len |= (size_t) frame_start[6] << 40;
1154 frame_len |= (size_t) frame_start[7] << 48;
1155 frame_len |= (size_t) frame_start[8] << 56;
1156#else
1157 if (frame_start[5] || frame_start[6] ||
1158 frame_start[7] || frame_start[8]) {
1159 PyErr_Format(PyExc_OverflowError,
1160 "Frame size too large for 32-bit build");
1161 return -1;
1162 }
1163#endif
1164 if (frame_len > PY_SSIZE_T_MAX) {
1165 PyErr_Format(UnpicklingError, "Invalid frame length");
1166 return -1;
1167 }
Antoine Pitrouc1207c12013-11-23 21:34:04 +01001168 if ((Py_ssize_t) frame_len < n) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001169 PyErr_Format(UnpicklingError, "Bad framing");
1170 return -1;
1171 }
1172 if (_Unpickler_ReadUnframed(self, &dummy /* unused */,
1173 frame_len) < 0)
1174 return -1;
1175 /* Rewind to start of frame */
1176 self->frame_end_idx = self->next_read_idx;
1177 self->next_read_idx -= frame_len;
1178 }
1179 if (self->framing) {
1180 /* Check for bad input */
1181 if (n + self->next_read_idx > self->frame_end_idx) {
1182 PyErr_Format(UnpicklingError, "Bad framing");
1183 return -1;
1184 }
1185 }
1186 return _Unpickler_ReadUnframed(self, s, n);
1187}
1188
1189static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001190_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1191 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001192{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001193 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001194 if (input_line == NULL) {
1195 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001196 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001197 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001198
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001199 memcpy(input_line, line, len);
1200 input_line[len] = '\0';
1201 self->input_line = input_line;
1202 *result = self->input_line;
1203 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001204}
1205
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001206/* Read a line from the input stream/buffer. If we run off the end of the input
1207 before hitting \n, return the data we found.
1208
1209 Returns the number of chars read, or -1 on failure. */
1210static Py_ssize_t
1211_Unpickler_Readline(UnpicklerObject *self, char **result)
1212{
1213 Py_ssize_t i, num_read;
1214
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001215 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001216 if (self->input_buffer[i] == '\n') {
1217 char *line_start = self->input_buffer + self->next_read_idx;
1218 num_read = i - self->next_read_idx + 1;
1219 self->next_read_idx = i + 1;
1220 return _Unpickler_CopyLine(self, line_start, num_read, result);
1221 }
1222 }
1223 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001224 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1225 if (num_read < 0)
1226 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001227 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001228 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001229 }
Victor Stinner121aab42011-09-29 23:40:53 +02001230
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001231 /* If we get here, we've run off the end of the input string. Return the
1232 remaining string and let the caller figure it out. */
1233 *result = self->input_buffer + self->next_read_idx;
1234 num_read = i - self->next_read_idx;
1235 self->next_read_idx = i;
1236 return num_read;
1237}
1238
1239/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1240 will be modified in place. */
1241static int
1242_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1243{
1244 Py_ssize_t i;
1245 PyObject **memo;
1246
1247 assert(new_size > self->memo_size);
1248
1249 memo = PyMem_REALLOC(self->memo, new_size * sizeof(PyObject *));
1250 if (memo == NULL) {
1251 PyErr_NoMemory();
1252 return -1;
1253 }
1254 self->memo = memo;
1255 for (i = self->memo_size; i < new_size; i++)
1256 self->memo[i] = NULL;
1257 self->memo_size = new_size;
1258 return 0;
1259}
1260
1261/* Returns NULL if idx is out of bounds. */
1262static PyObject *
1263_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1264{
1265 if (idx < 0 || idx >= self->memo_size)
1266 return NULL;
1267
1268 return self->memo[idx];
1269}
1270
1271/* Returns -1 (with an exception set) on failure, 0 on success.
1272 This takes its own reference to `value`. */
1273static int
1274_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1275{
1276 PyObject *old_item;
1277
1278 if (idx >= self->memo_size) {
1279 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1280 return -1;
1281 assert(idx < self->memo_size);
1282 }
1283 Py_INCREF(value);
1284 old_item = self->memo[idx];
1285 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001286 if (old_item != NULL) {
1287 Py_DECREF(old_item);
1288 }
1289 else {
1290 self->memo_len++;
1291 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001292 return 0;
1293}
1294
1295static PyObject **
1296_Unpickler_NewMemo(Py_ssize_t new_size)
1297{
1298 PyObject **memo = PyMem_MALLOC(new_size * sizeof(PyObject *));
Victor Stinner42024562013-07-12 00:53:57 +02001299 if (memo == NULL) {
1300 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001301 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001302 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001303 memset(memo, 0, new_size * sizeof(PyObject *));
1304 return memo;
1305}
1306
1307/* Free the unpickler's memo, taking care to decref any items left in it. */
1308static void
1309_Unpickler_MemoCleanup(UnpicklerObject *self)
1310{
1311 Py_ssize_t i;
1312 PyObject **memo = self->memo;
1313
1314 if (self->memo == NULL)
1315 return;
1316 self->memo = NULL;
1317 i = self->memo_size;
1318 while (--i >= 0) {
1319 Py_XDECREF(memo[i]);
1320 }
1321 PyMem_FREE(memo);
1322}
1323
1324static UnpicklerObject *
1325_Unpickler_New(void)
1326{
1327 UnpicklerObject *self;
1328
1329 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1330 if (self == NULL)
1331 return NULL;
1332
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001333 self->arg = NULL;
1334 self->pers_func = NULL;
1335 self->input_buffer = NULL;
1336 self->input_line = NULL;
1337 self->input_len = 0;
1338 self->next_read_idx = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001339 self->frame_end_idx = -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001340 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001341 self->read = NULL;
1342 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001343 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001344 self->encoding = NULL;
1345 self->errors = NULL;
1346 self->marks = NULL;
1347 self->num_marks = 0;
1348 self->marks_size = 0;
1349 self->proto = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001350 self->framing = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001351 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001352 memset(&self->buffer, 0, sizeof(Py_buffer));
1353 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001354 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001355 self->memo = _Unpickler_NewMemo(self->memo_size);
1356 self->stack = (Pdata *)Pdata_New();
1357
1358 if (self->memo == NULL || self->stack == NULL) {
1359 Py_DECREF(self);
1360 return NULL;
1361 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001362
1363 return self;
1364}
1365
1366/* Returns -1 (with an exception set) on failure, 0 on success. This may
1367 be called once on a freshly created Pickler. */
1368static int
1369_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1370{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001371 _Py_IDENTIFIER(peek);
1372 _Py_IDENTIFIER(read);
1373 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001374
1375 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001376 if (self->peek == NULL) {
1377 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1378 PyErr_Clear();
1379 else
1380 return -1;
1381 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001382 self->read = _PyObject_GetAttrId(file, &PyId_read);
1383 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001384 if (self->readline == NULL || self->read == NULL) {
1385 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1386 PyErr_SetString(PyExc_TypeError,
1387 "file must have 'read' and 'readline' attributes");
1388 Py_CLEAR(self->read);
1389 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001390 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001391 return -1;
1392 }
1393 return 0;
1394}
1395
1396/* Returns -1 (with an exception set) on failure, 0 on success. This may
1397 be called once on a freshly created Pickler. */
1398static int
1399_Unpickler_SetInputEncoding(UnpicklerObject *self,
1400 const char *encoding,
1401 const char *errors)
1402{
1403 if (encoding == NULL)
1404 encoding = "ASCII";
1405 if (errors == NULL)
1406 errors = "strict";
1407
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001408 self->encoding = _PyMem_Strdup(encoding);
1409 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001410 if (self->encoding == NULL || self->errors == NULL) {
1411 PyErr_NoMemory();
1412 return -1;
1413 }
1414 return 0;
1415}
1416
1417/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001418static int
1419memo_get(PicklerObject *self, PyObject *key)
1420{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001421 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001422 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001423 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001424
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001425 value = PyMemoTable_Get(self->memo, key);
1426 if (value == NULL) {
1427 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001428 return -1;
1429 }
1430
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001431 if (!self->bin) {
1432 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001433 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1434 "%" PY_FORMAT_SIZE_T "d\n", *value);
1435 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001436 }
1437 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001438 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001439 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001440 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001441 len = 2;
1442 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001443 else if (*value <= 0xffffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001444 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001445 pdata[1] = (unsigned char)(*value & 0xff);
1446 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1447 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1448 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001449 len = 5;
1450 }
1451 else { /* unlikely */
1452 PyErr_SetString(PicklingError,
1453 "memo id too large for LONG_BINGET");
1454 return -1;
1455 }
1456 }
1457
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001458 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001459 return -1;
1460
1461 return 0;
1462}
1463
1464/* Store an object in the memo, assign it a new unique ID based on the number
1465 of objects currently stored in the memo and generate a PUT opcode. */
1466static int
1467memo_put(PicklerObject *self, PyObject *obj)
1468{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001469 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001470 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001471 Py_ssize_t idx;
1472
1473 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001474
1475 if (self->fast)
1476 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001477 if (_Pickler_OpcodeBoundary(self))
1478 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001479
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001480 idx = PyMemoTable_Size(self->memo);
1481 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1482 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001483
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001484 if (self->proto >= 4) {
1485 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1486 return -1;
1487 return 0;
1488 }
1489 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001490 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001491 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001492 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001493 len = strlen(pdata);
1494 }
1495 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001496 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001497 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001498 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001499 len = 2;
1500 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001501 else if (idx <= 0xffffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001502 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001503 pdata[1] = (unsigned char)(idx & 0xff);
1504 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1505 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1506 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001507 len = 5;
1508 }
1509 else { /* unlikely */
1510 PyErr_SetString(PicklingError,
1511 "memo id too large for LONG_BINPUT");
1512 return -1;
1513 }
1514 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001515 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001516 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001517
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001518 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001519}
1520
1521static PyObject *
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001522getattribute(PyObject *obj, PyObject *name, int allow_qualname) {
1523 PyObject *dotted_path;
1524 Py_ssize_t i;
1525 _Py_static_string(PyId_dot, ".");
1526 _Py_static_string(PyId_locals, "<locals>");
1527
1528 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
1529 if (dotted_path == NULL) {
1530 return NULL;
1531 }
1532 assert(Py_SIZE(dotted_path) >= 1);
1533 if (!allow_qualname && Py_SIZE(dotted_path) > 1) {
1534 PyErr_Format(PyExc_AttributeError,
1535 "Can't get qualified attribute %R on %R;"
1536 "use protocols >= 4 to enable support",
1537 name, obj);
1538 Py_DECREF(dotted_path);
1539 return NULL;
1540 }
1541 Py_INCREF(obj);
1542 for (i = 0; i < Py_SIZE(dotted_path); i++) {
1543 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
1544 PyObject *tmp;
1545 PyObject *result = PyUnicode_RichCompare(
1546 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1547 int is_equal = (result == Py_True);
1548 assert(PyBool_Check(result));
1549 Py_DECREF(result);
1550 if (is_equal) {
1551 PyErr_Format(PyExc_AttributeError,
1552 "Can't get local attribute %R on %R", name, obj);
1553 Py_DECREF(dotted_path);
1554 Py_DECREF(obj);
1555 return NULL;
1556 }
1557 tmp = PyObject_GetAttr(obj, subpath);
1558 Py_DECREF(obj);
1559 if (tmp == NULL) {
1560 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1561 PyErr_Clear();
1562 PyErr_Format(PyExc_AttributeError,
1563 "Can't get attribute %R on %R", name, obj);
1564 }
1565 Py_DECREF(dotted_path);
1566 return NULL;
1567 }
1568 obj = tmp;
1569 }
1570 Py_DECREF(dotted_path);
1571 return obj;
1572}
1573
1574static PyObject *
1575whichmodule(PyObject *global, PyObject *global_name, int allow_qualname)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001576{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001577 PyObject *module_name;
1578 PyObject *modules_dict;
1579 PyObject *module;
1580 PyObject *obj;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001581 Py_ssize_t i, j;
1582 _Py_IDENTIFIER(__module__);
1583 _Py_IDENTIFIER(modules);
1584 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001585
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001586 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1587
1588 if (module_name == NULL) {
1589 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001590 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001591 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001592 }
1593 else {
1594 /* In some rare cases (e.g., bound methods of extension types),
1595 __module__ can be None. If it is so, then search sys.modules for
1596 the module of global. */
1597 if (module_name != Py_None)
1598 return module_name;
1599 Py_CLEAR(module_name);
1600 }
1601 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001602
Victor Stinnerbb520202013-11-06 22:40:41 +01001603 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001604 if (modules_dict == NULL) {
1605 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001606 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001607 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001608
1609 i = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001610 while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001611 PyObject *result = PyUnicode_RichCompare(
1612 module_name, _PyUnicode_FromId(&PyId___main__), Py_EQ);
1613 int is_equal = (result == Py_True);
1614 assert(PyBool_Check(result));
1615 Py_DECREF(result);
1616 if (is_equal)
1617 continue;
1618 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001619 continue;
1620
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001621 obj = getattribute(module, global_name, allow_qualname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001622 if (obj == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001623 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001624 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001625 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001626 continue;
1627 }
1628
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001629 if (obj == global) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001630 Py_DECREF(obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001631 Py_INCREF(module_name);
1632 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001633 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001634 Py_DECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001635 }
1636
1637 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001638 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001639 Py_INCREF(module_name);
1640 return module_name;
1641}
1642
1643/* fast_save_enter() and fast_save_leave() are guards against recursive
1644 objects when Pickler is used with the "fast mode" (i.e., with object
1645 memoization disabled). If the nesting of a list or dict object exceed
1646 FAST_NESTING_LIMIT, these guards will start keeping an internal
1647 reference to the seen list or dict objects and check whether these objects
1648 are recursive. These are not strictly necessary, since save() has a
1649 hard-coded recursion limit, but they give a nicer error message than the
1650 typical RuntimeError. */
1651static int
1652fast_save_enter(PicklerObject *self, PyObject *obj)
1653{
1654 /* if fast_nesting < 0, we're doing an error exit. */
1655 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1656 PyObject *key = NULL;
1657 if (self->fast_memo == NULL) {
1658 self->fast_memo = PyDict_New();
1659 if (self->fast_memo == NULL) {
1660 self->fast_nesting = -1;
1661 return 0;
1662 }
1663 }
1664 key = PyLong_FromVoidPtr(obj);
1665 if (key == NULL)
1666 return 0;
1667 if (PyDict_GetItem(self->fast_memo, key)) {
1668 Py_DECREF(key);
1669 PyErr_Format(PyExc_ValueError,
1670 "fast mode: can't pickle cyclic objects "
1671 "including object type %.200s at %p",
1672 obj->ob_type->tp_name, obj);
1673 self->fast_nesting = -1;
1674 return 0;
1675 }
1676 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1677 Py_DECREF(key);
1678 self->fast_nesting = -1;
1679 return 0;
1680 }
1681 Py_DECREF(key);
1682 }
1683 return 1;
1684}
1685
1686static int
1687fast_save_leave(PicklerObject *self, PyObject *obj)
1688{
1689 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1690 PyObject *key = PyLong_FromVoidPtr(obj);
1691 if (key == NULL)
1692 return 0;
1693 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1694 Py_DECREF(key);
1695 return 0;
1696 }
1697 Py_DECREF(key);
1698 }
1699 return 1;
1700}
1701
1702static int
1703save_none(PicklerObject *self, PyObject *obj)
1704{
1705 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001706 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001707 return -1;
1708
1709 return 0;
1710}
1711
1712static int
1713save_bool(PicklerObject *self, PyObject *obj)
1714{
1715 static const char *buf[2] = { FALSE, TRUE };
1716 const char len[2] = {sizeof(FALSE) - 1, sizeof(TRUE) - 1};
1717 int p = (obj == Py_True);
1718
1719 if (self->proto >= 2) {
1720 const char bool_op = p ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001721 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001722 return -1;
1723 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001724 else if (_Pickler_Write(self, buf[p], len[p]) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001725 return -1;
1726
1727 return 0;
1728}
1729
1730static int
1731save_int(PicklerObject *self, long x)
1732{
1733 char pdata[32];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001734 Py_ssize_t len = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001735
1736 if (!self->bin
1737#if SIZEOF_LONG > 4
1738 || x > 0x7fffffffL || x < -0x80000000L
1739#endif
1740 ) {
1741 /* Text-mode pickle, or long too big to fit in the 4-byte
1742 * signed BININT format: store as a string.
1743 */
Mark Dickinson8dd05142009-01-20 20:43:58 +00001744 pdata[0] = LONG; /* use LONG for consistency with pickle.py */
1745 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, "%ldL\n", x);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001746 if (_Pickler_Write(self, pdata, strlen(pdata)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001747 return -1;
1748 }
1749 else {
1750 /* Binary pickle and x fits in a signed 4-byte int. */
1751 pdata[1] = (unsigned char)(x & 0xff);
1752 pdata[2] = (unsigned char)((x >> 8) & 0xff);
1753 pdata[3] = (unsigned char)((x >> 16) & 0xff);
1754 pdata[4] = (unsigned char)((x >> 24) & 0xff);
1755
1756 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1757 if (pdata[2] == 0) {
1758 pdata[0] = BININT1;
1759 len = 2;
1760 }
1761 else {
1762 pdata[0] = BININT2;
1763 len = 3;
1764 }
1765 }
1766 else {
1767 pdata[0] = BININT;
1768 len = 5;
1769 }
1770
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001771 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001772 return -1;
1773 }
1774
1775 return 0;
1776}
1777
1778static int
1779save_long(PicklerObject *self, PyObject *obj)
1780{
1781 PyObject *repr = NULL;
1782 Py_ssize_t size;
1783 long val = PyLong_AsLong(obj);
1784 int status = 0;
1785
1786 const char long_op = LONG;
1787
1788 if (val == -1 && PyErr_Occurred()) {
1789 /* out of range for int pickling */
1790 PyErr_Clear();
1791 }
Antoine Pitroue58bffb2011-08-13 20:40:32 +02001792 else
1793#if SIZEOF_LONG > 4
1794 if (val <= 0x7fffffffL && val >= -0x80000000L)
1795#endif
1796 return save_int(self, val);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001797
1798 if (self->proto >= 2) {
1799 /* Linear-time pickling. */
1800 size_t nbits;
1801 size_t nbytes;
1802 unsigned char *pdata;
1803 char header[5];
1804 int i;
1805 int sign = _PyLong_Sign(obj);
1806
1807 if (sign == 0) {
1808 header[0] = LONG1;
1809 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001810 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001811 goto error;
1812 return 0;
1813 }
1814 nbits = _PyLong_NumBits(obj);
1815 if (nbits == (size_t)-1 && PyErr_Occurred())
1816 goto error;
1817 /* How many bytes do we need? There are nbits >> 3 full
1818 * bytes of data, and nbits & 7 leftover bits. If there
1819 * are any leftover bits, then we clearly need another
1820 * byte. Wnat's not so obvious is that we *probably*
1821 * need another byte even if there aren't any leftovers:
1822 * the most-significant bit of the most-significant byte
1823 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001824 * opposite of the one we need. The exception is ints
1825 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001826 * its own 256's-complement, so has the right sign bit
1827 * even without the extra byte. That's a pain to check
1828 * for in advance, though, so we always grab an extra
1829 * byte at the start, and cut it back later if possible.
1830 */
1831 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001832 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001833 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001834 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001835 goto error;
1836 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001837 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001838 if (repr == NULL)
1839 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001840 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001841 i = _PyLong_AsByteArray((PyLongObject *)obj,
1842 pdata, nbytes,
1843 1 /* little endian */ , 1 /* signed */ );
1844 if (i < 0)
1845 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001846 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001847 * needed. This is so iff the MSB is all redundant sign
1848 * bits.
1849 */
1850 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001851 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001852 pdata[nbytes - 1] == 0xff &&
1853 (pdata[nbytes - 2] & 0x80) != 0) {
1854 nbytes--;
1855 }
1856
1857 if (nbytes < 256) {
1858 header[0] = LONG1;
1859 header[1] = (unsigned char)nbytes;
1860 size = 2;
1861 }
1862 else {
1863 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001864 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001865 for (i = 1; i < 5; i++) {
1866 header[i] = (unsigned char)(size & 0xff);
1867 size >>= 8;
1868 }
1869 size = 5;
1870 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001871 if (_Pickler_Write(self, header, size) < 0 ||
1872 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001873 goto error;
1874 }
1875 else {
1876 char *string;
1877
Mark Dickinson8dd05142009-01-20 20:43:58 +00001878 /* proto < 2: write the repr and newline. This is quadratic-time (in
1879 the number of digits), in both directions. We add a trailing 'L'
1880 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001881
1882 repr = PyObject_Repr(obj);
1883 if (repr == NULL)
1884 goto error;
1885
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001886 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001887 if (string == NULL)
1888 goto error;
1889
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001890 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1891 _Pickler_Write(self, string, size) < 0 ||
1892 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001893 goto error;
1894 }
1895
1896 if (0) {
1897 error:
1898 status = -1;
1899 }
1900 Py_XDECREF(repr);
1901
1902 return status;
1903}
1904
1905static int
1906save_float(PicklerObject *self, PyObject *obj)
1907{
1908 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1909
1910 if (self->bin) {
1911 char pdata[9];
1912 pdata[0] = BINFLOAT;
1913 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1914 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001915 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001916 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001917 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001918 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001919 int result = -1;
1920 char *buf = NULL;
1921 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001922
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001923 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001924 goto done;
1925
Mark Dickinson3e09f432009-04-17 08:41:23 +00001926 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001927 if (!buf) {
1928 PyErr_NoMemory();
1929 goto done;
1930 }
1931
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001932 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001933 goto done;
1934
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001935 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001936 goto done;
1937
1938 result = 0;
1939done:
1940 PyMem_Free(buf);
1941 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001942 }
1943
1944 return 0;
1945}
1946
1947static int
1948save_bytes(PicklerObject *self, PyObject *obj)
1949{
1950 if (self->proto < 3) {
1951 /* Older pickle protocols do not have an opcode for pickling bytes
1952 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001953 the __reduce__ method) to permit bytes object unpickling.
1954
1955 Here we use a hack to be compatible with Python 2. Since in Python
1956 2 'bytes' is just an alias for 'str' (which has different
1957 parameters than the actual bytes object), we use codecs.encode
1958 to create the appropriate 'str' object when unpickled using
1959 Python 2 *and* the appropriate 'bytes' object when unpickled
1960 using Python 3. Again this is a hack and we don't need to do this
1961 with newer protocols. */
1962 static PyObject *codecs_encode = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001963 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001964 int status;
1965
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001966 if (codecs_encode == NULL) {
1967 PyObject *codecs_module = PyImport_ImportModule("codecs");
1968 if (codecs_module == NULL) {
1969 return -1;
1970 }
1971 codecs_encode = PyObject_GetAttrString(codecs_module, "encode");
1972 Py_DECREF(codecs_module);
1973 if (codecs_encode == NULL) {
1974 return -1;
1975 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001976 }
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 Vassalotti3bfc65a2011-12-13 13:08:09 -05001982 PyObject *unicode_str =
1983 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
1984 PyBytes_GET_SIZE(obj),
1985 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001986 _Py_IDENTIFIER(latin1);
1987
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001988 if (unicode_str == NULL)
1989 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001990 reduce_value = Py_BuildValue("(O(OO))",
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001991 codecs_encode, unicode_str,
1992 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001993 Py_DECREF(unicode_str);
1994 }
1995
1996 if (reduce_value == NULL)
1997 return -1;
1998
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001999 /* save_reduce() will memoize the object automatically. */
2000 status = save_reduce(self, reduce_value, obj);
2001 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002002 return status;
2003 }
2004 else {
2005 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002006 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002007 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002008
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002009 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002010 if (size < 0)
2011 return -1;
2012
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002013 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002014 header[0] = SHORT_BINBYTES;
2015 header[1] = (unsigned char)size;
2016 len = 2;
2017 }
2018 else if (size <= 0xffffffffL) {
2019 header[0] = BINBYTES;
2020 header[1] = (unsigned char)(size & 0xff);
2021 header[2] = (unsigned char)((size >> 8) & 0xff);
2022 header[3] = (unsigned char)((size >> 16) & 0xff);
2023 header[4] = (unsigned char)((size >> 24) & 0xff);
2024 len = 5;
2025 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002026 else if (self->proto >= 4) {
2027 int i;
2028 header[0] = BINBYTES8;
2029 for (i = 0; i < 8; i++) {
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +01002030 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002031 }
2032 len = 8;
2033 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002034 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002035 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002036 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002037 return -1; /* string too large */
2038 }
2039
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002040 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002041 return -1;
2042
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002043 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002044 return -1;
2045
2046 if (memo_put(self, obj) < 0)
2047 return -1;
2048
2049 return 0;
2050 }
2051}
2052
2053/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2054 backslash and newline characters to \uXXXX escapes. */
2055static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002056raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002057{
2058 PyObject *repr, *result;
2059 char *p;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002060 Py_ssize_t i, size, expandsize;
2061 void *data;
2062 unsigned int kind;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002063
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002064 if (PyUnicode_READY(obj))
2065 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002066
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002067 size = PyUnicode_GET_LENGTH(obj);
2068 data = PyUnicode_DATA(obj);
2069 kind = PyUnicode_KIND(obj);
2070 if (kind == PyUnicode_4BYTE_KIND)
2071 expandsize = 10;
2072 else
2073 expandsize = 6;
Victor Stinner121aab42011-09-29 23:40:53 +02002074
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002075 if (size > PY_SSIZE_T_MAX / expandsize)
2076 return PyErr_NoMemory();
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002077 repr = PyByteArray_FromStringAndSize(NULL, expandsize * size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002078 if (repr == NULL)
2079 return NULL;
2080 if (size == 0)
2081 goto done;
2082
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002083 p = PyByteArray_AS_STRING(repr);
2084 for (i=0; i < size; i++) {
2085 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002086 /* Map 32-bit characters to '\Uxxxxxxxx' */
2087 if (ch >= 0x10000) {
2088 *p++ = '\\';
2089 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002090 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2091 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2092 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2093 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2094 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2095 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2096 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2097 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002098 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002099 /* Map 16-bit characters to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002100 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002101 *p++ = '\\';
2102 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002103 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2104 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2105 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2106 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002107 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002108 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002109 else
2110 *p++ = (char) ch;
2111 }
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002112 size = p - PyByteArray_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002113
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002114done:
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002115 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002116 Py_DECREF(repr);
2117 return result;
2118}
2119
2120static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002121write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2122{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002123 char header[9];
2124 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002125
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002126 if (size <= 0xff && self->proto >= 4) {
2127 header[0] = SHORT_BINUNICODE;
2128 header[1] = (unsigned char)(size & 0xff);
2129 len = 2;
2130 }
2131 else if (size <= 0xffffffffUL) {
2132 header[0] = BINUNICODE;
2133 header[1] = (unsigned char)(size & 0xff);
2134 header[2] = (unsigned char)((size >> 8) & 0xff);
2135 header[3] = (unsigned char)((size >> 16) & 0xff);
2136 header[4] = (unsigned char)((size >> 24) & 0xff);
2137 len = 5;
2138 }
2139 else if (self->proto >= 4) {
2140 int i;
2141
2142 header[0] = BINUNICODE8;
2143 for (i = 0; i < 8; i++) {
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +01002144 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002145 }
2146 len = 9;
2147 }
2148 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002149 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002150 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002151 return -1;
2152 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002153
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002154 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002155 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002156 if (_Pickler_Write(self, data, size) < 0)
2157 return -1;
2158
2159 return 0;
2160}
2161
2162static int
2163write_unicode_binary(PicklerObject *self, PyObject *obj)
2164{
2165 PyObject *encoded = NULL;
2166 Py_ssize_t size;
2167 char *data;
2168 int r;
2169
2170 if (PyUnicode_READY(obj))
2171 return -1;
2172
2173 data = PyUnicode_AsUTF8AndSize(obj, &size);
2174 if (data != NULL)
2175 return write_utf8(self, data, size);
2176
2177 /* Issue #8383: for strings with lone surrogates, fallback on the
2178 "surrogatepass" error handler. */
2179 PyErr_Clear();
2180 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2181 if (encoded == NULL)
2182 return -1;
2183
2184 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2185 PyBytes_GET_SIZE(encoded));
2186 Py_DECREF(encoded);
2187 return r;
2188}
2189
2190static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002191save_unicode(PicklerObject *self, PyObject *obj)
2192{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002193 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002194 if (write_unicode_binary(self, obj) < 0)
2195 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002196 }
2197 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002198 PyObject *encoded;
2199 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002200 const char unicode_op = UNICODE;
2201
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002202 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002203 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002204 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002205
Antoine Pitrou299978d2013-04-07 17:38:11 +02002206 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2207 Py_DECREF(encoded);
2208 return -1;
2209 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002210
2211 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002212 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2213 Py_DECREF(encoded);
2214 return -1;
2215 }
2216 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002217
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002218 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002219 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002220 }
2221 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002222 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002223
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002224 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002225}
2226
2227/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2228static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002229store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002230{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002231 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002232
2233 assert(PyTuple_Size(t) == len);
2234
2235 for (i = 0; i < len; i++) {
2236 PyObject *element = PyTuple_GET_ITEM(t, i);
2237
2238 if (element == NULL)
2239 return -1;
2240 if (save(self, element, 0) < 0)
2241 return -1;
2242 }
2243
2244 return 0;
2245}
2246
2247/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2248 * used across protocols to minimize the space needed to pickle them.
2249 * Tuples are also the only builtin immutable type that can be recursive
2250 * (a tuple can be reached from itself), and that requires some subtle
2251 * magic so that it works in all cases. IOW, this is a long routine.
2252 */
2253static int
2254save_tuple(PicklerObject *self, PyObject *obj)
2255{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002256 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002257
2258 const char mark_op = MARK;
2259 const char tuple_op = TUPLE;
2260 const char pop_op = POP;
2261 const char pop_mark_op = POP_MARK;
2262 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2263
2264 if ((len = PyTuple_Size(obj)) < 0)
2265 return -1;
2266
2267 if (len == 0) {
2268 char pdata[2];
2269
2270 if (self->proto) {
2271 pdata[0] = EMPTY_TUPLE;
2272 len = 1;
2273 }
2274 else {
2275 pdata[0] = MARK;
2276 pdata[1] = TUPLE;
2277 len = 2;
2278 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002279 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002280 return -1;
2281 return 0;
2282 }
2283
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002284 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002285 * saving the tuple elements, the tuple must be recursive, in
2286 * which case we'll pop everything we put on the stack, and fetch
2287 * its value from the memo.
2288 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002289 if (len <= 3 && self->proto >= 2) {
2290 /* Use TUPLE{1,2,3} opcodes. */
2291 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002292 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002293
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002294 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002295 /* pop the len elements */
2296 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002297 if (_Pickler_Write(self, &pop_op, 1) < 0)
2298 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002299 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002300 if (memo_get(self, obj) < 0)
2301 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002302
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002303 return 0;
2304 }
2305 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002306 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2307 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002308 }
2309 goto memoize;
2310 }
2311
2312 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2313 * Generate MARK e1 e2 ... TUPLE
2314 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002315 if (_Pickler_Write(self, &mark_op, 1) < 0)
2316 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002317
2318 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002319 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002320
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002321 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002322 /* pop the stack stuff we pushed */
2323 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002324 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2325 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002326 }
2327 else {
2328 /* Note that we pop one more than len, to remove
2329 * the MARK too.
2330 */
2331 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002332 if (_Pickler_Write(self, &pop_op, 1) < 0)
2333 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002334 }
2335 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002336 if (memo_get(self, obj) < 0)
2337 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002338
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002339 return 0;
2340 }
2341 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002342 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2343 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002344 }
2345
2346 memoize:
2347 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002348 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002349
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002350 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002351}
2352
2353/* iter is an iterator giving items, and we batch up chunks of
2354 * MARK item item ... item APPENDS
2355 * opcode sequences. Calling code should have arranged to first create an
2356 * empty list, or list-like object, for the APPENDS to operate on.
2357 * Returns 0 on success, <0 on error.
2358 */
2359static int
2360batch_list(PicklerObject *self, PyObject *iter)
2361{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002362 PyObject *obj = NULL;
2363 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002364 int i, n;
2365
2366 const char mark_op = MARK;
2367 const char append_op = APPEND;
2368 const char appends_op = APPENDS;
2369
2370 assert(iter != NULL);
2371
2372 /* XXX: I think this function could be made faster by avoiding the
2373 iterator interface and fetching objects directly from list using
2374 PyList_GET_ITEM.
2375 */
2376
2377 if (self->proto == 0) {
2378 /* APPENDS isn't available; do one at a time. */
2379 for (;;) {
2380 obj = PyIter_Next(iter);
2381 if (obj == NULL) {
2382 if (PyErr_Occurred())
2383 return -1;
2384 break;
2385 }
2386 i = save(self, obj, 0);
2387 Py_DECREF(obj);
2388 if (i < 0)
2389 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002390 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002391 return -1;
2392 }
2393 return 0;
2394 }
2395
2396 /* proto > 0: write in batches of BATCHSIZE. */
2397 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002398 /* Get first item */
2399 firstitem = PyIter_Next(iter);
2400 if (firstitem == NULL) {
2401 if (PyErr_Occurred())
2402 goto error;
2403
2404 /* nothing more to add */
2405 break;
2406 }
2407
2408 /* Try to get a second item */
2409 obj = PyIter_Next(iter);
2410 if (obj == NULL) {
2411 if (PyErr_Occurred())
2412 goto error;
2413
2414 /* Only one item to write */
2415 if (save(self, firstitem, 0) < 0)
2416 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002417 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002418 goto error;
2419 Py_CLEAR(firstitem);
2420 break;
2421 }
2422
2423 /* More than one item to write */
2424
2425 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002426 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002427 goto error;
2428
2429 if (save(self, firstitem, 0) < 0)
2430 goto error;
2431 Py_CLEAR(firstitem);
2432 n = 1;
2433
2434 /* Fetch and save up to BATCHSIZE items */
2435 while (obj) {
2436 if (save(self, obj, 0) < 0)
2437 goto error;
2438 Py_CLEAR(obj);
2439 n += 1;
2440
2441 if (n == BATCHSIZE)
2442 break;
2443
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002444 obj = PyIter_Next(iter);
2445 if (obj == NULL) {
2446 if (PyErr_Occurred())
2447 goto error;
2448 break;
2449 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002450 }
2451
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002452 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002453 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002454
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002455 } while (n == BATCHSIZE);
2456 return 0;
2457
2458 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002459 Py_XDECREF(firstitem);
2460 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002461 return -1;
2462}
2463
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002464/* This is a variant of batch_list() above, specialized for lists (with no
2465 * support for list subclasses). Like batch_list(), we batch up chunks of
2466 * MARK item item ... item APPENDS
2467 * opcode sequences. Calling code should have arranged to first create an
2468 * empty list, or list-like object, for the APPENDS to operate on.
2469 * Returns 0 on success, -1 on error.
2470 *
2471 * This version is considerably faster than batch_list(), if less general.
2472 *
2473 * Note that this only works for protocols > 0.
2474 */
2475static int
2476batch_list_exact(PicklerObject *self, PyObject *obj)
2477{
2478 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002479 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002480
2481 const char append_op = APPEND;
2482 const char appends_op = APPENDS;
2483 const char mark_op = MARK;
2484
2485 assert(obj != NULL);
2486 assert(self->proto > 0);
2487 assert(PyList_CheckExact(obj));
2488
2489 if (PyList_GET_SIZE(obj) == 1) {
2490 item = PyList_GET_ITEM(obj, 0);
2491 if (save(self, item, 0) < 0)
2492 return -1;
2493 if (_Pickler_Write(self, &append_op, 1) < 0)
2494 return -1;
2495 return 0;
2496 }
2497
2498 /* Write in batches of BATCHSIZE. */
2499 total = 0;
2500 do {
2501 this_batch = 0;
2502 if (_Pickler_Write(self, &mark_op, 1) < 0)
2503 return -1;
2504 while (total < PyList_GET_SIZE(obj)) {
2505 item = PyList_GET_ITEM(obj, total);
2506 if (save(self, item, 0) < 0)
2507 return -1;
2508 total++;
2509 if (++this_batch == BATCHSIZE)
2510 break;
2511 }
2512 if (_Pickler_Write(self, &appends_op, 1) < 0)
2513 return -1;
2514
2515 } while (total < PyList_GET_SIZE(obj));
2516
2517 return 0;
2518}
2519
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002520static int
2521save_list(PicklerObject *self, PyObject *obj)
2522{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002523 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002524 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002525 int status = 0;
2526
2527 if (self->fast && !fast_save_enter(self, obj))
2528 goto error;
2529
2530 /* Create an empty list. */
2531 if (self->bin) {
2532 header[0] = EMPTY_LIST;
2533 len = 1;
2534 }
2535 else {
2536 header[0] = MARK;
2537 header[1] = LIST;
2538 len = 2;
2539 }
2540
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002541 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002542 goto error;
2543
2544 /* Get list length, and bow out early if empty. */
2545 if ((len = PyList_Size(obj)) < 0)
2546 goto error;
2547
2548 if (memo_put(self, obj) < 0)
2549 goto error;
2550
2551 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002552 /* Materialize the list elements. */
2553 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002554 if (Py_EnterRecursiveCall(" while pickling an object"))
2555 goto error;
2556 status = batch_list_exact(self, obj);
2557 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002558 } else {
2559 PyObject *iter = PyObject_GetIter(obj);
2560 if (iter == NULL)
2561 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002562
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002563 if (Py_EnterRecursiveCall(" while pickling an object")) {
2564 Py_DECREF(iter);
2565 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002566 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002567 status = batch_list(self, iter);
2568 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002569 Py_DECREF(iter);
2570 }
2571 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002572 if (0) {
2573 error:
2574 status = -1;
2575 }
2576
2577 if (self->fast && !fast_save_leave(self, obj))
2578 status = -1;
2579
2580 return status;
2581}
2582
2583/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2584 * MARK key value ... key value SETITEMS
2585 * opcode sequences. Calling code should have arranged to first create an
2586 * empty dict, or dict-like object, for the SETITEMS to operate on.
2587 * Returns 0 on success, <0 on error.
2588 *
2589 * This is very much like batch_list(). The difference between saving
2590 * elements directly, and picking apart two-tuples, is so long-winded at
2591 * the C level, though, that attempts to combine these routines were too
2592 * ugly to bear.
2593 */
2594static int
2595batch_dict(PicklerObject *self, PyObject *iter)
2596{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002597 PyObject *obj = NULL;
2598 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002599 int i, n;
2600
2601 const char mark_op = MARK;
2602 const char setitem_op = SETITEM;
2603 const char setitems_op = SETITEMS;
2604
2605 assert(iter != NULL);
2606
2607 if (self->proto == 0) {
2608 /* SETITEMS isn't available; do one at a time. */
2609 for (;;) {
2610 obj = PyIter_Next(iter);
2611 if (obj == NULL) {
2612 if (PyErr_Occurred())
2613 return -1;
2614 break;
2615 }
2616 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2617 PyErr_SetString(PyExc_TypeError, "dict items "
2618 "iterator must return 2-tuples");
2619 return -1;
2620 }
2621 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2622 if (i >= 0)
2623 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2624 Py_DECREF(obj);
2625 if (i < 0)
2626 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002627 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002628 return -1;
2629 }
2630 return 0;
2631 }
2632
2633 /* proto > 0: write in batches of BATCHSIZE. */
2634 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002635 /* Get first item */
2636 firstitem = PyIter_Next(iter);
2637 if (firstitem == NULL) {
2638 if (PyErr_Occurred())
2639 goto error;
2640
2641 /* nothing more to add */
2642 break;
2643 }
2644 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2645 PyErr_SetString(PyExc_TypeError, "dict items "
2646 "iterator must return 2-tuples");
2647 goto error;
2648 }
2649
2650 /* Try to get a second item */
2651 obj = PyIter_Next(iter);
2652 if (obj == NULL) {
2653 if (PyErr_Occurred())
2654 goto error;
2655
2656 /* Only one item to write */
2657 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2658 goto error;
2659 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2660 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002661 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002662 goto error;
2663 Py_CLEAR(firstitem);
2664 break;
2665 }
2666
2667 /* More than one item to write */
2668
2669 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002670 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002671 goto error;
2672
2673 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2674 goto error;
2675 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2676 goto error;
2677 Py_CLEAR(firstitem);
2678 n = 1;
2679
2680 /* Fetch and save up to BATCHSIZE items */
2681 while (obj) {
2682 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2683 PyErr_SetString(PyExc_TypeError, "dict items "
2684 "iterator must return 2-tuples");
2685 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002686 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002687 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2688 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2689 goto error;
2690 Py_CLEAR(obj);
2691 n += 1;
2692
2693 if (n == BATCHSIZE)
2694 break;
2695
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002696 obj = PyIter_Next(iter);
2697 if (obj == NULL) {
2698 if (PyErr_Occurred())
2699 goto error;
2700 break;
2701 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002702 }
2703
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002704 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002705 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002706
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002707 } while (n == BATCHSIZE);
2708 return 0;
2709
2710 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002711 Py_XDECREF(firstitem);
2712 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002713 return -1;
2714}
2715
Collin Winter5c9b02d2009-05-25 05:43:30 +00002716/* This is a variant of batch_dict() above that specializes for dicts, with no
2717 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2718 * MARK key value ... key value SETITEMS
2719 * opcode sequences. Calling code should have arranged to first create an
2720 * empty dict, or dict-like object, for the SETITEMS to operate on.
2721 * Returns 0 on success, -1 on error.
2722 *
2723 * Note that this currently doesn't work for protocol 0.
2724 */
2725static int
2726batch_dict_exact(PicklerObject *self, PyObject *obj)
2727{
2728 PyObject *key = NULL, *value = NULL;
2729 int i;
2730 Py_ssize_t dict_size, ppos = 0;
2731
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002732 const char mark_op = MARK;
2733 const char setitem_op = SETITEM;
2734 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002735
2736 assert(obj != NULL);
2737 assert(self->proto > 0);
2738
2739 dict_size = PyDict_Size(obj);
2740
2741 /* Special-case len(d) == 1 to save space. */
2742 if (dict_size == 1) {
2743 PyDict_Next(obj, &ppos, &key, &value);
2744 if (save(self, key, 0) < 0)
2745 return -1;
2746 if (save(self, value, 0) < 0)
2747 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002748 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002749 return -1;
2750 return 0;
2751 }
2752
2753 /* Write in batches of BATCHSIZE. */
2754 do {
2755 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002756 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002757 return -1;
2758 while (PyDict_Next(obj, &ppos, &key, &value)) {
2759 if (save(self, key, 0) < 0)
2760 return -1;
2761 if (save(self, value, 0) < 0)
2762 return -1;
2763 if (++i == BATCHSIZE)
2764 break;
2765 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002766 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002767 return -1;
2768 if (PyDict_Size(obj) != dict_size) {
2769 PyErr_Format(
2770 PyExc_RuntimeError,
2771 "dictionary changed size during iteration");
2772 return -1;
2773 }
2774
2775 } while (i == BATCHSIZE);
2776 return 0;
2777}
2778
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002779static int
2780save_dict(PicklerObject *self, PyObject *obj)
2781{
2782 PyObject *items, *iter;
2783 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002784 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002785 int status = 0;
2786
2787 if (self->fast && !fast_save_enter(self, obj))
2788 goto error;
2789
2790 /* Create an empty dict. */
2791 if (self->bin) {
2792 header[0] = EMPTY_DICT;
2793 len = 1;
2794 }
2795 else {
2796 header[0] = MARK;
2797 header[1] = DICT;
2798 len = 2;
2799 }
2800
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002801 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002802 goto error;
2803
2804 /* Get dict size, and bow out early if empty. */
2805 if ((len = PyDict_Size(obj)) < 0)
2806 goto error;
2807
2808 if (memo_put(self, obj) < 0)
2809 goto error;
2810
2811 if (len != 0) {
2812 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002813 if (PyDict_CheckExact(obj) && self->proto > 0) {
2814 /* We can take certain shortcuts if we know this is a dict and
2815 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002816 if (Py_EnterRecursiveCall(" while pickling an object"))
2817 goto error;
2818 status = batch_dict_exact(self, obj);
2819 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002820 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002821 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002822
2823 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002824 if (items == NULL)
2825 goto error;
2826 iter = PyObject_GetIter(items);
2827 Py_DECREF(items);
2828 if (iter == NULL)
2829 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002830 if (Py_EnterRecursiveCall(" while pickling an object")) {
2831 Py_DECREF(iter);
2832 goto error;
2833 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002834 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002835 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002836 Py_DECREF(iter);
2837 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002838 }
2839
2840 if (0) {
2841 error:
2842 status = -1;
2843 }
2844
2845 if (self->fast && !fast_save_leave(self, obj))
2846 status = -1;
2847
2848 return status;
2849}
2850
2851static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002852save_set(PicklerObject *self, PyObject *obj)
2853{
2854 PyObject *item;
2855 int i;
2856 Py_ssize_t set_size, ppos = 0;
2857 Py_hash_t hash;
2858
2859 const char empty_set_op = EMPTY_SET;
2860 const char mark_op = MARK;
2861 const char additems_op = ADDITEMS;
2862
2863 if (self->proto < 4) {
2864 PyObject *items;
2865 PyObject *reduce_value;
2866 int status;
2867
2868 items = PySequence_List(obj);
2869 if (items == NULL) {
2870 return -1;
2871 }
2872 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2873 Py_DECREF(items);
2874 if (reduce_value == NULL) {
2875 return -1;
2876 }
2877 /* save_reduce() will memoize the object automatically. */
2878 status = save_reduce(self, reduce_value, obj);
2879 Py_DECREF(reduce_value);
2880 return status;
2881 }
2882
2883 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2884 return -1;
2885
2886 if (memo_put(self, obj) < 0)
2887 return -1;
2888
2889 set_size = PySet_GET_SIZE(obj);
2890 if (set_size == 0)
2891 return 0; /* nothing to do */
2892
2893 /* Write in batches of BATCHSIZE. */
2894 do {
2895 i = 0;
2896 if (_Pickler_Write(self, &mark_op, 1) < 0)
2897 return -1;
2898 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2899 if (save(self, item, 0) < 0)
2900 return -1;
2901 if (++i == BATCHSIZE)
2902 break;
2903 }
2904 if (_Pickler_Write(self, &additems_op, 1) < 0)
2905 return -1;
2906 if (PySet_GET_SIZE(obj) != set_size) {
2907 PyErr_Format(
2908 PyExc_RuntimeError,
2909 "set changed size during iteration");
2910 return -1;
2911 }
2912 } while (i == BATCHSIZE);
2913
2914 return 0;
2915}
2916
2917static int
2918save_frozenset(PicklerObject *self, PyObject *obj)
2919{
2920 PyObject *iter;
2921
2922 const char mark_op = MARK;
2923 const char frozenset_op = FROZENSET;
2924
2925 if (self->fast && !fast_save_enter(self, obj))
2926 return -1;
2927
2928 if (self->proto < 4) {
2929 PyObject *items;
2930 PyObject *reduce_value;
2931 int status;
2932
2933 items = PySequence_List(obj);
2934 if (items == NULL) {
2935 return -1;
2936 }
2937 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2938 items);
2939 Py_DECREF(items);
2940 if (reduce_value == NULL) {
2941 return -1;
2942 }
2943 /* save_reduce() will memoize the object automatically. */
2944 status = save_reduce(self, reduce_value, obj);
2945 Py_DECREF(reduce_value);
2946 return status;
2947 }
2948
2949 if (_Pickler_Write(self, &mark_op, 1) < 0)
2950 return -1;
2951
2952 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002953 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01002954 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002955 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002956 for (;;) {
2957 PyObject *item;
2958
2959 item = PyIter_Next(iter);
2960 if (item == NULL) {
2961 if (PyErr_Occurred()) {
2962 Py_DECREF(iter);
2963 return -1;
2964 }
2965 break;
2966 }
2967 if (save(self, item, 0) < 0) {
2968 Py_DECREF(item);
2969 Py_DECREF(iter);
2970 return -1;
2971 }
2972 Py_DECREF(item);
2973 }
2974 Py_DECREF(iter);
2975
2976 /* If the object is already in the memo, this means it is
2977 recursive. In this case, throw away everything we put on the
2978 stack, and fetch the object back from the memo. */
2979 if (PyMemoTable_Get(self->memo, obj)) {
2980 const char pop_mark_op = POP_MARK;
2981
2982 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2983 return -1;
2984 if (memo_get(self, obj) < 0)
2985 return -1;
2986 return 0;
2987 }
2988
2989 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
2990 return -1;
2991 if (memo_put(self, obj) < 0)
2992 return -1;
2993
2994 return 0;
2995}
2996
2997static int
2998fix_imports(PyObject **module_name, PyObject **global_name)
2999{
3000 PyObject *key;
3001 PyObject *item;
3002
3003 key = PyTuple_Pack(2, *module_name, *global_name);
3004 if (key == NULL)
3005 return -1;
3006 item = PyDict_GetItemWithError(name_mapping_3to2, key);
3007 Py_DECREF(key);
3008 if (item) {
3009 PyObject *fixed_module_name;
3010 PyObject *fixed_global_name;
3011
3012 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3013 PyErr_Format(PyExc_RuntimeError,
3014 "_compat_pickle.REVERSE_NAME_MAPPING values "
3015 "should be 2-tuples, not %.200s",
3016 Py_TYPE(item)->tp_name);
3017 return -1;
3018 }
3019 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3020 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3021 if (!PyUnicode_Check(fixed_module_name) ||
3022 !PyUnicode_Check(fixed_global_name)) {
3023 PyErr_Format(PyExc_RuntimeError,
3024 "_compat_pickle.REVERSE_NAME_MAPPING values "
3025 "should be pairs of str, not (%.200s, %.200s)",
3026 Py_TYPE(fixed_module_name)->tp_name,
3027 Py_TYPE(fixed_global_name)->tp_name);
3028 return -1;
3029 }
3030
3031 Py_CLEAR(*module_name);
3032 Py_CLEAR(*global_name);
3033 Py_INCREF(fixed_module_name);
3034 Py_INCREF(fixed_global_name);
3035 *module_name = fixed_module_name;
3036 *global_name = fixed_global_name;
3037 }
3038 else if (PyErr_Occurred()) {
3039 return -1;
3040 }
3041
3042 item = PyDict_GetItemWithError(import_mapping_3to2, *module_name);
3043 if (item) {
3044 if (!PyUnicode_Check(item)) {
3045 PyErr_Format(PyExc_RuntimeError,
3046 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3047 "should be strings, not %.200s",
3048 Py_TYPE(item)->tp_name);
3049 return -1;
3050 }
3051 Py_CLEAR(*module_name);
3052 Py_INCREF(item);
3053 *module_name = item;
3054 }
3055 else if (PyErr_Occurred()) {
3056 return -1;
3057 }
3058
3059 return 0;
3060}
3061
3062static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003063save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3064{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003065 PyObject *global_name = NULL;
3066 PyObject *module_name = NULL;
3067 PyObject *module = NULL;
3068 PyObject *cls;
3069 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003070 _Py_IDENTIFIER(__name__);
3071 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003072
3073 const char global_op = GLOBAL;
3074
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003075 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003076 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003077 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003078 }
3079 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003080 if (self->proto >= 4) {
3081 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3082 if (global_name == NULL) {
3083 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3084 goto error;
3085 PyErr_Clear();
3086 }
3087 }
3088 if (global_name == NULL) {
3089 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3090 if (global_name == NULL)
3091 goto error;
3092 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003093 }
3094
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003095 module_name = whichmodule(obj, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003096 if (module_name == NULL)
3097 goto error;
3098
3099 /* XXX: Change to use the import C API directly with level=0 to disallow
3100 relative imports.
3101
3102 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3103 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3104 custom import functions (IMHO, this would be a nice security
3105 feature). The import C API would need to be extended to support the
3106 extra parameters of __import__ to fix that. */
3107 module = PyImport_Import(module_name);
3108 if (module == NULL) {
3109 PyErr_Format(PicklingError,
3110 "Can't pickle %R: import of module %R failed",
3111 obj, module_name);
3112 goto error;
3113 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003114 cls = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003115 if (cls == NULL) {
3116 PyErr_Format(PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003117 "Can't pickle %R: attribute lookup %S on %S failed",
3118 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003119 goto error;
3120 }
3121 if (cls != obj) {
3122 Py_DECREF(cls);
3123 PyErr_Format(PicklingError,
3124 "Can't pickle %R: it's not the same object as %S.%S",
3125 obj, module_name, global_name);
3126 goto error;
3127 }
3128 Py_DECREF(cls);
3129
3130 if (self->proto >= 2) {
3131 /* See whether this is in the extension registry, and if
3132 * so generate an EXT opcode.
3133 */
3134 PyObject *code_obj; /* extension code as Python object */
3135 long code; /* extension code as C value */
3136 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003137 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003138
3139 PyTuple_SET_ITEM(two_tuple, 0, module_name);
3140 PyTuple_SET_ITEM(two_tuple, 1, global_name);
3141 code_obj = PyDict_GetItem(extension_registry, two_tuple);
3142 /* The object is not registered in the extension registry.
3143 This is the most likely code path. */
3144 if (code_obj == NULL)
3145 goto gen_global;
3146
3147 /* XXX: pickle.py doesn't check neither the type, nor the range
3148 of the value returned by the extension_registry. It should for
3149 consistency. */
3150
3151 /* Verify code_obj has the right type and value. */
3152 if (!PyLong_Check(code_obj)) {
3153 PyErr_Format(PicklingError,
3154 "Can't pickle %R: extension code %R isn't an integer",
3155 obj, code_obj);
3156 goto error;
3157 }
3158 code = PyLong_AS_LONG(code_obj);
3159 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003160 if (!PyErr_Occurred())
3161 PyErr_Format(PicklingError,
3162 "Can't pickle %R: extension code %ld is out of range",
3163 obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003164 goto error;
3165 }
3166
3167 /* Generate an EXT opcode. */
3168 if (code <= 0xff) {
3169 pdata[0] = EXT1;
3170 pdata[1] = (unsigned char)code;
3171 n = 2;
3172 }
3173 else if (code <= 0xffff) {
3174 pdata[0] = EXT2;
3175 pdata[1] = (unsigned char)(code & 0xff);
3176 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3177 n = 3;
3178 }
3179 else {
3180 pdata[0] = EXT4;
3181 pdata[1] = (unsigned char)(code & 0xff);
3182 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3183 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3184 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3185 n = 5;
3186 }
3187
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003188 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003189 goto error;
3190 }
3191 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003192 gen_global:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003193 if (self->proto >= 4) {
3194 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003195
Christian Heimese8b1ba12013-11-23 21:13:39 +01003196 if (save(self, module_name, 0) < 0)
3197 goto error;
3198 if (save(self, global_name, 0) < 0)
3199 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003200
3201 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3202 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003203 }
3204 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003205 /* Generate a normal global opcode if we are using a pickle
3206 protocol < 4, or if the object is not registered in the
3207 extension registry. */
3208 PyObject *encoded;
3209 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003210
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003211 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003212 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003213
3214 /* For protocol < 3 and if the user didn't request against doing
3215 so, we convert module names to the old 2.x module names. */
3216 if (self->proto < 3 && self->fix_imports) {
3217 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003218 goto error;
3219 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003220 }
3221
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003222 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3223 both the module name and the global name using UTF-8. We do so
3224 only when we are using the pickle protocol newer than version
3225 3. This is to ensure compatibility with older Unpickler running
3226 on Python 2.x. */
3227 if (self->proto == 3) {
3228 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003229 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003230 else {
3231 unicode_encoder = PyUnicode_AsASCIIString;
3232 }
3233 encoded = unicode_encoder(module_name);
3234 if (encoded == NULL) {
3235 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
3236 PyErr_Format(PicklingError,
3237 "can't pickle module identifier '%S' using "
3238 "pickle protocol %i",
3239 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003240 goto error;
3241 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003242 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3243 PyBytes_GET_SIZE(encoded)) < 0) {
3244 Py_DECREF(encoded);
3245 goto error;
3246 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003247 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003248 if(_Pickler_Write(self, "\n", 1) < 0)
3249 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003250
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003251 /* Save the name of the module. */
3252 encoded = unicode_encoder(global_name);
3253 if (encoded == NULL) {
3254 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
3255 PyErr_Format(PicklingError,
3256 "can't pickle global identifier '%S' using "
3257 "pickle protocol %i",
3258 global_name, self->proto);
3259 goto error;
3260 }
3261 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3262 PyBytes_GET_SIZE(encoded)) < 0) {
3263 Py_DECREF(encoded);
3264 goto error;
3265 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003266 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003267 if (_Pickler_Write(self, "\n", 1) < 0)
3268 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003269 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003270 /* Memoize the object. */
3271 if (memo_put(self, obj) < 0)
3272 goto error;
3273 }
3274
3275 if (0) {
3276 error:
3277 status = -1;
3278 }
3279 Py_XDECREF(module_name);
3280 Py_XDECREF(global_name);
3281 Py_XDECREF(module);
3282
3283 return status;
3284}
3285
3286static int
Łukasz Langaf3078fb2012-03-12 19:46:12 +01003287save_ellipsis(PicklerObject *self, PyObject *obj)
3288{
Łukasz Langadbd78252012-03-12 22:59:11 +01003289 PyObject *str = PyUnicode_FromString("Ellipsis");
Benjamin Petersone80b29b2012-03-16 18:45:31 -05003290 int res;
Łukasz Langadbd78252012-03-12 22:59:11 +01003291 if (str == NULL)
Łukasz Langacad1a072012-03-12 23:41:07 +01003292 return -1;
Benjamin Petersone80b29b2012-03-16 18:45:31 -05003293 res = save_global(self, Py_Ellipsis, str);
3294 Py_DECREF(str);
3295 return res;
Łukasz Langaf3078fb2012-03-12 19:46:12 +01003296}
3297
3298static int
3299save_notimplemented(PicklerObject *self, PyObject *obj)
3300{
Łukasz Langadbd78252012-03-12 22:59:11 +01003301 PyObject *str = PyUnicode_FromString("NotImplemented");
Benjamin Petersone80b29b2012-03-16 18:45:31 -05003302 int res;
Łukasz Langadbd78252012-03-12 22:59:11 +01003303 if (str == NULL)
Łukasz Langacad1a072012-03-12 23:41:07 +01003304 return -1;
Benjamin Petersone80b29b2012-03-16 18:45:31 -05003305 res = save_global(self, Py_NotImplemented, str);
3306 Py_DECREF(str);
3307 return res;
Łukasz Langaf3078fb2012-03-12 19:46:12 +01003308}
3309
3310static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003311save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3312{
3313 PyObject *pid = NULL;
3314 int status = 0;
3315
3316 const char persid_op = PERSID;
3317 const char binpersid_op = BINPERSID;
3318
3319 Py_INCREF(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003320 pid = _Pickler_FastCall(self, func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003321 if (pid == NULL)
3322 return -1;
3323
3324 if (pid != Py_None) {
3325 if (self->bin) {
3326 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003327 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003328 goto error;
3329 }
3330 else {
3331 PyObject *pid_str = NULL;
3332 char *pid_ascii_bytes;
3333 Py_ssize_t size;
3334
3335 pid_str = PyObject_Str(pid);
3336 if (pid_str == NULL)
3337 goto error;
3338
3339 /* XXX: Should it check whether the persistent id only contains
3340 ASCII characters? And what if the pid contains embedded
3341 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003342 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003343 Py_DECREF(pid_str);
3344 if (pid_ascii_bytes == NULL)
3345 goto error;
3346
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003347 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3348 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3349 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003350 goto error;
3351 }
3352 status = 1;
3353 }
3354
3355 if (0) {
3356 error:
3357 status = -1;
3358 }
3359 Py_XDECREF(pid);
3360
3361 return status;
3362}
3363
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003364static PyObject *
3365get_class(PyObject *obj)
3366{
3367 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003368 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003369
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003370 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003371 if (cls == NULL) {
3372 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3373 PyErr_Clear();
3374 cls = (PyObject *) Py_TYPE(obj);
3375 Py_INCREF(cls);
3376 }
3377 }
3378 return cls;
3379}
3380
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003381/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3382 * appropriate __reduce__ method for obj.
3383 */
3384static int
3385save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3386{
3387 PyObject *callable;
3388 PyObject *argtup;
3389 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003390 PyObject *listitems = Py_None;
3391 PyObject *dictitems = Py_None;
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003392 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003393 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003394
3395 const char reduce_op = REDUCE;
3396 const char build_op = BUILD;
3397 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003398 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003399
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003400 size = PyTuple_Size(args);
3401 if (size < 2 || size > 5) {
3402 PyErr_SetString(PicklingError, "tuple returned by "
3403 "__reduce__ must contain 2 through 5 elements");
3404 return -1;
3405 }
3406
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003407 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3408 &callable, &argtup, &state, &listitems, &dictitems))
3409 return -1;
3410
3411 if (!PyCallable_Check(callable)) {
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003412 PyErr_SetString(PicklingError, "first item of the tuple "
3413 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003414 return -1;
3415 }
3416 if (!PyTuple_Check(argtup)) {
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003417 PyErr_SetString(PicklingError, "second item of the tuple "
3418 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003419 return -1;
3420 }
3421
3422 if (state == Py_None)
3423 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003424
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003425 if (listitems == Py_None)
3426 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003427 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti00d83f22013-04-14 01:28:01 -07003428 PyErr_Format(PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003429 "returned by __reduce__ must be an iterator, not %s",
3430 Py_TYPE(listitems)->tp_name);
3431 return -1;
3432 }
3433
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003434 if (dictitems == Py_None)
3435 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003436 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti00d83f22013-04-14 01:28:01 -07003437 PyErr_Format(PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003438 "returned by __reduce__ must be an iterator, not %s",
3439 Py_TYPE(dictitems)->tp_name);
3440 return -1;
3441 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003442
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003443 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003444 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003445 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003446
Victor Stinner804e05e2013-11-14 01:26:17 +01003447 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003448 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003449 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003450 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003451 }
3452 PyErr_Clear();
3453 }
3454 else if (self->proto >= 4) {
3455 _Py_IDENTIFIER(__newobj_ex__);
3456 use_newobj_ex = PyUnicode_Check(name) &&
3457 PyUnicode_Compare(
3458 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
3459 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003460 }
3461 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003462 _Py_IDENTIFIER(__newobj__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003463 use_newobj = PyUnicode_Check(name) &&
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003464 PyUnicode_Compare(
3465 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003466 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003467 }
3468 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003469
3470 if (use_newobj_ex) {
3471 PyObject *cls;
3472 PyObject *args;
3473 PyObject *kwargs;
3474
3475 if (Py_SIZE(argtup) != 3) {
3476 PyErr_Format(PicklingError,
3477 "length of the NEWOBJ_EX argument tuple must be "
3478 "exactly 3, not %zd", Py_SIZE(argtup));
3479 return -1;
3480 }
3481
3482 cls = PyTuple_GET_ITEM(argtup, 0);
3483 if (!PyType_Check(cls)) {
3484 PyErr_Format(PicklingError,
3485 "first item from NEWOBJ_EX argument tuple must "
3486 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3487 return -1;
3488 }
3489 args = PyTuple_GET_ITEM(argtup, 1);
3490 if (!PyTuple_Check(args)) {
3491 PyErr_Format(PicklingError,
3492 "second item from NEWOBJ_EX argument tuple must "
3493 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3494 return -1;
3495 }
3496 kwargs = PyTuple_GET_ITEM(argtup, 2);
3497 if (!PyDict_Check(kwargs)) {
3498 PyErr_Format(PicklingError,
3499 "third item from NEWOBJ_EX argument tuple must "
3500 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3501 return -1;
3502 }
3503
3504 if (save(self, cls, 0) < 0 ||
3505 save(self, args, 0) < 0 ||
3506 save(self, kwargs, 0) < 0 ||
3507 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3508 return -1;
3509 }
3510 }
3511 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003512 PyObject *cls;
3513 PyObject *newargtup;
3514 PyObject *obj_class;
3515 int p;
3516
3517 /* Sanity checks. */
3518 if (Py_SIZE(argtup) < 1) {
3519 PyErr_SetString(PicklingError, "__newobj__ arglist is empty");
3520 return -1;
3521 }
3522
3523 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003524 if (!PyType_Check(cls)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003525 PyErr_SetString(PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003526 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003527 return -1;
3528 }
3529
3530 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003531 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003532 p = obj_class != cls; /* true iff a problem */
3533 Py_DECREF(obj_class);
3534 if (p) {
3535 PyErr_SetString(PicklingError, "args[0] from "
3536 "__newobj__ args has the wrong class");
3537 return -1;
3538 }
3539 }
3540 /* XXX: These calls save() are prone to infinite recursion. Imagine
3541 what happen if the value returned by the __reduce__() method of
3542 some extension type contains another object of the same type. Ouch!
3543
3544 Here is a quick example, that I ran into, to illustrate what I
3545 mean:
3546
3547 >>> import pickle, copyreg
3548 >>> copyreg.dispatch_table.pop(complex)
3549 >>> pickle.dumps(1+2j)
3550 Traceback (most recent call last):
3551 ...
3552 RuntimeError: maximum recursion depth exceeded
3553
3554 Removing the complex class from copyreg.dispatch_table made the
3555 __reduce_ex__() method emit another complex object:
3556
3557 >>> (1+1j).__reduce_ex__(2)
3558 (<function __newobj__ at 0xb7b71c3c>,
3559 (<class 'complex'>, (1+1j)), None, None, None)
3560
3561 Thus when save() was called on newargstup (the 2nd item) recursion
3562 ensued. Of course, the bug was in the complex class which had a
3563 broken __getnewargs__() that emitted another complex object. But,
3564 the point, here, is it is quite easy to end up with a broken reduce
3565 function. */
3566
3567 /* Save the class and its __new__ arguments. */
3568 if (save(self, cls, 0) < 0)
3569 return -1;
3570
3571 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3572 if (newargtup == NULL)
3573 return -1;
3574
3575 p = save(self, newargtup, 0);
3576 Py_DECREF(newargtup);
3577 if (p < 0)
3578 return -1;
3579
3580 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003581 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003582 return -1;
3583 }
3584 else { /* Not using NEWOBJ. */
3585 if (save(self, callable, 0) < 0 ||
3586 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003587 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003588 return -1;
3589 }
3590
3591 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3592 the caller do not want to memoize the object. Not particularly useful,
3593 but that is to mimic the behavior save_reduce() in pickle.py when
3594 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003595 if (obj != NULL) {
3596 /* If the object is already in the memo, this means it is
3597 recursive. In this case, throw away everything we put on the
3598 stack, and fetch the object back from the memo. */
3599 if (PyMemoTable_Get(self->memo, obj)) {
3600 const char pop_op = POP;
3601
3602 if (_Pickler_Write(self, &pop_op, 1) < 0)
3603 return -1;
3604 if (memo_get(self, obj) < 0)
3605 return -1;
3606
3607 return 0;
3608 }
3609 else if (memo_put(self, obj) < 0)
3610 return -1;
3611 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003612
3613 if (listitems && batch_list(self, listitems) < 0)
3614 return -1;
3615
3616 if (dictitems && batch_dict(self, dictitems) < 0)
3617 return -1;
3618
3619 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003620 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003621 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003622 return -1;
3623 }
3624
3625 return 0;
3626}
3627
3628static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003629save_method(PicklerObject *self, PyObject *obj)
3630{
3631 PyObject *method_self = PyCFunction_GET_SELF(obj);
3632
3633 if (method_self == NULL || PyModule_Check(method_self)) {
3634 return save_global(self, obj, NULL);
3635 }
3636 else {
3637 PyObject *builtins;
3638 PyObject *getattr;
3639 PyObject *reduce_value;
3640 int status = -1;
3641 _Py_IDENTIFIER(getattr);
3642
3643 builtins = PyEval_GetBuiltins();
3644 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
3645 reduce_value = \
3646 Py_BuildValue("O(Os)", getattr, method_self,
3647 ((PyCFunctionObject *)obj)->m_ml->ml_name);
3648 if (reduce_value != NULL) {
3649 status = save_reduce(self, reduce_value, obj);
3650 Py_DECREF(reduce_value);
3651 }
3652 return status;
3653 }
3654}
3655
3656static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003657save(PicklerObject *self, PyObject *obj, int pers_save)
3658{
3659 PyTypeObject *type;
3660 PyObject *reduce_func = NULL;
3661 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003662 int status = 0;
3663
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003664 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003665 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003666
3667 /* The extra pers_save argument is necessary to avoid calling save_pers()
3668 on its returned object. */
3669 if (!pers_save && self->pers_func) {
3670 /* save_pers() returns:
3671 -1 to signal an error;
3672 0 if it did nothing successfully;
3673 1 if a persistent id was saved.
3674 */
3675 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3676 goto done;
3677 }
3678
3679 type = Py_TYPE(obj);
3680
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003681 /* The old cPickle had an optimization that used switch-case statement
3682 dispatching on the first letter of the type name. This has was removed
3683 since benchmarks shown that this optimization was actually slowing
3684 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003685
3686 /* Atom types; these aren't memoized, so don't check the memo. */
3687
3688 if (obj == Py_None) {
3689 status = save_none(self, obj);
3690 goto done;
3691 }
Łukasz Langaf3078fb2012-03-12 19:46:12 +01003692 else if (obj == Py_Ellipsis) {
3693 status = save_ellipsis(self, obj);
3694 goto done;
3695 }
3696 else if (obj == Py_NotImplemented) {
3697 status = save_notimplemented(self, obj);
3698 goto done;
3699 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003700 else if (obj == Py_False || obj == Py_True) {
3701 status = save_bool(self, obj);
3702 goto done;
3703 }
3704 else if (type == &PyLong_Type) {
3705 status = save_long(self, obj);
3706 goto done;
3707 }
3708 else if (type == &PyFloat_Type) {
3709 status = save_float(self, obj);
3710 goto done;
3711 }
3712
3713 /* Check the memo to see if it has the object. If so, generate
3714 a GET (or BINGET) opcode, instead of pickling the object
3715 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003716 if (PyMemoTable_Get(self->memo, obj)) {
3717 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003718 goto error;
3719 goto done;
3720 }
3721
3722 if (type == &PyBytes_Type) {
3723 status = save_bytes(self, obj);
3724 goto done;
3725 }
3726 else if (type == &PyUnicode_Type) {
3727 status = save_unicode(self, obj);
3728 goto done;
3729 }
3730 else if (type == &PyDict_Type) {
3731 status = save_dict(self, obj);
3732 goto done;
3733 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003734 else if (type == &PySet_Type) {
3735 status = save_set(self, obj);
3736 goto done;
3737 }
3738 else if (type == &PyFrozenSet_Type) {
3739 status = save_frozenset(self, obj);
3740 goto done;
3741 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003742 else if (type == &PyList_Type) {
3743 status = save_list(self, obj);
3744 goto done;
3745 }
3746 else if (type == &PyTuple_Type) {
3747 status = save_tuple(self, obj);
3748 goto done;
3749 }
3750 else if (type == &PyType_Type) {
3751 status = save_global(self, obj, NULL);
3752 goto done;
3753 }
3754 else if (type == &PyFunction_Type) {
3755 status = save_global(self, obj, NULL);
3756 if (status < 0 && PyErr_ExceptionMatches(PickleError)) {
3757 /* fall back to reduce */
3758 PyErr_Clear();
3759 }
3760 else {
3761 goto done;
3762 }
3763 }
3764 else if (type == &PyCFunction_Type) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003765 status = save_method(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003766 goto done;
3767 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003768
3769 /* XXX: This part needs some unit tests. */
3770
3771 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003772 * self.dispatch_table, copyreg.dispatch_table, the object's
3773 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003774 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003775 if (self->dispatch_table == NULL) {
3776 reduce_func = PyDict_GetItem(dispatch_table, (PyObject *)type);
3777 /* PyDict_GetItem() unlike PyObject_GetItem() and
3778 PyObject_GetAttr() returns a borrowed ref */
3779 Py_XINCREF(reduce_func);
3780 } else {
3781 reduce_func = PyObject_GetItem(self->dispatch_table, (PyObject *)type);
3782 if (reduce_func == NULL) {
3783 if (PyErr_ExceptionMatches(PyExc_KeyError))
3784 PyErr_Clear();
3785 else
3786 goto error;
3787 }
3788 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003789 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003790 Py_INCREF(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003791 reduce_value = _Pickler_FastCall(self, reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003792 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003793 else if (PyType_IsSubtype(type, &PyType_Type)) {
3794 status = save_global(self, obj, NULL);
3795 goto done;
3796 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003797 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003798 _Py_IDENTIFIER(__reduce__);
3799 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003800
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003801
3802 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3803 automatically defined as __reduce__. While this is convenient, this
3804 make it impossible to know which method was actually called. Of
3805 course, this is not a big deal. But still, it would be nice to let
3806 the user know which method was called when something go
3807 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3808 don't actually have to check for a __reduce__ method. */
3809
3810 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003811 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003812 if (reduce_func != NULL) {
3813 PyObject *proto;
3814 proto = PyLong_FromLong(self->proto);
3815 if (proto != NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003816 reduce_value = _Pickler_FastCall(self, reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003817 }
3818 }
3819 else {
3820 if (PyErr_ExceptionMatches(PyExc_AttributeError))
3821 PyErr_Clear();
3822 else
3823 goto error;
3824 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003825 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003826 if (reduce_func != NULL) {
3827 reduce_value = PyObject_Call(reduce_func, empty_tuple, NULL);
3828 }
3829 else {
3830 PyErr_Format(PicklingError, "can't pickle '%.200s' object: %R",
3831 type->tp_name, obj);
3832 goto error;
3833 }
3834 }
3835 }
3836
3837 if (reduce_value == NULL)
3838 goto error;
3839
3840 if (PyUnicode_Check(reduce_value)) {
3841 status = save_global(self, obj, reduce_value);
3842 goto done;
3843 }
3844
3845 if (!PyTuple_Check(reduce_value)) {
3846 PyErr_SetString(PicklingError,
3847 "__reduce__ must return a string or tuple");
3848 goto error;
3849 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003850
3851 status = save_reduce(self, reduce_value, obj);
3852
3853 if (0) {
3854 error:
3855 status = -1;
3856 }
3857 done:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003858 if (status == 0)
3859 status = _Pickler_OpcodeBoundary(self);
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003860 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003861 Py_XDECREF(reduce_func);
3862 Py_XDECREF(reduce_value);
3863
3864 return status;
3865}
3866
3867static int
3868dump(PicklerObject *self, PyObject *obj)
3869{
3870 const char stop_op = STOP;
3871
3872 if (self->proto >= 2) {
3873 char header[2];
3874
3875 header[0] = PROTO;
3876 assert(self->proto >= 0 && self->proto < 256);
3877 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003878 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003879 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003880 if (self->proto >= 4)
3881 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003882 }
3883
3884 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003885 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003886 return -1;
3887
3888 return 0;
3889}
3890
3891PyDoc_STRVAR(Pickler_clear_memo_doc,
3892"clear_memo() -> None. Clears the pickler's \"memo\"."
3893"\n"
3894"The memo is the data structure that remembers which objects the\n"
3895"pickler has already seen, so that shared or recursive objects are\n"
3896"pickled by reference and not by value. This method is useful when\n"
3897"re-using picklers.");
3898
3899static PyObject *
3900Pickler_clear_memo(PicklerObject *self)
3901{
3902 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003903 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003904
3905 Py_RETURN_NONE;
3906}
3907
3908PyDoc_STRVAR(Pickler_dump_doc,
3909"dump(obj) -> None. Write a pickled representation of obj to the open file.");
3910
3911static PyObject *
3912Pickler_dump(PicklerObject *self, PyObject *args)
3913{
3914 PyObject *obj;
3915
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003916 /* Check whether the Pickler was initialized correctly (issue3664).
3917 Developers often forget to call __init__() in their subclasses, which
3918 would trigger a segfault without this check. */
3919 if (self->write == NULL) {
Victor Stinner121aab42011-09-29 23:40:53 +02003920 PyErr_Format(PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003921 "Pickler.__init__() was not called by %s.__init__()",
3922 Py_TYPE(self)->tp_name);
3923 return NULL;
3924 }
3925
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003926 if (!PyArg_ParseTuple(args, "O:dump", &obj))
3927 return NULL;
3928
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003929 if (_Pickler_ClearBuffer(self) < 0)
3930 return NULL;
3931
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003932 if (dump(self, obj) < 0)
3933 return NULL;
3934
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003935 if (_Pickler_FlushToFile(self) < 0)
3936 return NULL;
3937
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003938 Py_RETURN_NONE;
3939}
3940
3941static struct PyMethodDef Pickler_methods[] = {
3942 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
3943 Pickler_dump_doc},
3944 {"clear_memo", (PyCFunction)Pickler_clear_memo, METH_NOARGS,
3945 Pickler_clear_memo_doc},
3946 {NULL, NULL} /* sentinel */
3947};
3948
3949static void
3950Pickler_dealloc(PicklerObject *self)
3951{
3952 PyObject_GC_UnTrack(self);
3953
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003954 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003955 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003956 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003957 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003958 Py_XDECREF(self->arg);
3959 Py_XDECREF(self->fast_memo);
3960
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003961 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003962
3963 Py_TYPE(self)->tp_free((PyObject *)self);
3964}
3965
3966static int
3967Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
3968{
3969 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003970 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003971 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003972 Py_VISIT(self->arg);
3973 Py_VISIT(self->fast_memo);
3974 return 0;
3975}
3976
3977static int
3978Pickler_clear(PicklerObject *self)
3979{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003980 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003981 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003982 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003983 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003984 Py_CLEAR(self->arg);
3985 Py_CLEAR(self->fast_memo);
3986
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003987 if (self->memo != NULL) {
3988 PyMemoTable *memo = self->memo;
3989 self->memo = NULL;
3990 PyMemoTable_Del(memo);
3991 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003992 return 0;
3993}
3994
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003995
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003996PyDoc_STRVAR(Pickler_doc,
3997"Pickler(file, protocol=None)"
3998"\n"
3999"This takes a binary file for writing a pickle data stream.\n"
4000"\n"
4001"The optional protocol argument tells the pickler to use the\n"
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004002"given protocol; supported protocols are 0, 1, 2, 3 and 4. The\n"
4003"default protocol is 3; a backward-incompatible protocol designed for\n"
4004"Python 3.\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004005"\n"
4006"Specifying a negative protocol version selects the highest\n"
4007"protocol version supported. The higher the protocol used, the\n"
4008"more recent the version of Python needed to read the pickle\n"
4009"produced.\n"
4010"\n"
4011"The file argument must have a write() method that accepts a single\n"
4012"bytes argument. It can thus be a file object opened for binary\n"
4013"writing, a io.BytesIO instance, or any other custom object that\n"
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004014"meets this interface.\n"
4015"\n"
4016"If fix_imports is True and protocol is less than 3, pickle will try to\n"
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004017"map the new Python 3 names to the old module names used in Python 2,\n"
4018"so that the pickle data stream is readable with Python 2.\n");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004019
4020static int
4021Pickler_init(PicklerObject *self, PyObject *args, PyObject *kwds)
4022{
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004023 static char *kwlist[] = {"file", "protocol", "fix_imports", 0};
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004024 PyObject *file;
4025 PyObject *proto_obj = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004026 PyObject *fix_imports = Py_True;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004027 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004028 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004029
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004030 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO:Pickler",
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004031 kwlist, &file, &proto_obj, &fix_imports))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004032 return -1;
4033
4034 /* In case of multiple __init__() calls, clear previous content. */
4035 if (self->write != NULL)
4036 (void)Pickler_clear(self);
4037
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004038 if (_Pickler_SetProtocol(self, proto_obj, fix_imports) < 0)
4039 return -1;
4040
4041 if (_Pickler_SetOutputStream(self, file) < 0)
4042 return -1;
4043
4044 /* memo and output_buffer may have already been created in _Pickler_New */
4045 if (self->memo == NULL) {
4046 self->memo = PyMemoTable_New();
4047 if (self->memo == NULL)
4048 return -1;
4049 }
4050 self->output_len = 0;
4051 if (self->output_buffer == NULL) {
4052 self->max_output_len = WRITE_BUF_SIZE;
4053 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4054 self->max_output_len);
4055 if (self->output_buffer == NULL)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004056 return -1;
4057 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004058
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004059 self->arg = NULL;
4060 self->fast = 0;
4061 self->fast_nesting = 0;
4062 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004063 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004064 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4065 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4066 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004067 if (self->pers_func == NULL)
4068 return -1;
4069 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004070 self->dispatch_table = NULL;
4071 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4072 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4073 &PyId_dispatch_table);
4074 if (self->dispatch_table == NULL)
4075 return -1;
4076 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004077 return 0;
4078}
4079
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004080/* Define a proxy object for the Pickler's internal memo object. This is to
4081 * avoid breaking code like:
4082 * pickler.memo.clear()
4083 * and
4084 * pickler.memo = saved_memo
4085 * Is this a good idea? Not really, but we don't want to break code that uses
4086 * it. Note that we don't implement the entire mapping API here. This is
4087 * intentional, as these should be treated as black-box implementation details.
4088 */
4089
4090typedef struct {
4091 PyObject_HEAD
4092 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
4093} PicklerMemoProxyObject;
4094
4095PyDoc_STRVAR(pmp_clear_doc,
4096"memo.clear() -> None. Remove all items from memo.");
4097
4098static PyObject *
4099pmp_clear(PicklerMemoProxyObject *self)
4100{
4101 if (self->pickler->memo)
4102 PyMemoTable_Clear(self->pickler->memo);
4103 Py_RETURN_NONE;
4104}
4105
4106PyDoc_STRVAR(pmp_copy_doc,
4107"memo.copy() -> new_memo. Copy the memo to a new object.");
4108
4109static PyObject *
4110pmp_copy(PicklerMemoProxyObject *self)
4111{
4112 Py_ssize_t i;
4113 PyMemoTable *memo;
4114 PyObject *new_memo = PyDict_New();
4115 if (new_memo == NULL)
4116 return NULL;
4117
4118 memo = self->pickler->memo;
4119 for (i = 0; i < memo->mt_allocated; ++i) {
4120 PyMemoEntry entry = memo->mt_table[i];
4121 if (entry.me_key != NULL) {
4122 int status;
4123 PyObject *key, *value;
4124
4125 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004126 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004127
4128 if (key == NULL || value == NULL) {
4129 Py_XDECREF(key);
4130 Py_XDECREF(value);
4131 goto error;
4132 }
4133 status = PyDict_SetItem(new_memo, key, value);
4134 Py_DECREF(key);
4135 Py_DECREF(value);
4136 if (status < 0)
4137 goto error;
4138 }
4139 }
4140 return new_memo;
4141
4142 error:
4143 Py_XDECREF(new_memo);
4144 return NULL;
4145}
4146
4147PyDoc_STRVAR(pmp_reduce_doc,
4148"memo.__reduce__(). Pickling support.");
4149
4150static PyObject *
4151pmp_reduce(PicklerMemoProxyObject *self, PyObject *args)
4152{
4153 PyObject *reduce_value, *dict_args;
4154 PyObject *contents = pmp_copy(self);
4155 if (contents == NULL)
4156 return NULL;
4157
4158 reduce_value = PyTuple_New(2);
4159 if (reduce_value == NULL) {
4160 Py_DECREF(contents);
4161 return NULL;
4162 }
4163 dict_args = PyTuple_New(1);
4164 if (dict_args == NULL) {
4165 Py_DECREF(contents);
4166 Py_DECREF(reduce_value);
4167 return NULL;
4168 }
4169 PyTuple_SET_ITEM(dict_args, 0, contents);
4170 Py_INCREF((PyObject *)&PyDict_Type);
4171 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4172 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4173 return reduce_value;
4174}
4175
4176static PyMethodDef picklerproxy_methods[] = {
4177 {"clear", (PyCFunction)pmp_clear, METH_NOARGS, pmp_clear_doc},
4178 {"copy", (PyCFunction)pmp_copy, METH_NOARGS, pmp_copy_doc},
4179 {"__reduce__", (PyCFunction)pmp_reduce, METH_VARARGS, pmp_reduce_doc},
4180 {NULL, NULL} /* sentinel */
4181};
4182
4183static void
4184PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4185{
4186 PyObject_GC_UnTrack(self);
4187 Py_XDECREF(self->pickler);
4188 PyObject_GC_Del((PyObject *)self);
4189}
4190
4191static int
4192PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4193 visitproc visit, void *arg)
4194{
4195 Py_VISIT(self->pickler);
4196 return 0;
4197}
4198
4199static int
4200PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4201{
4202 Py_CLEAR(self->pickler);
4203 return 0;
4204}
4205
4206static PyTypeObject PicklerMemoProxyType = {
4207 PyVarObject_HEAD_INIT(NULL, 0)
4208 "_pickle.PicklerMemoProxy", /*tp_name*/
4209 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4210 0,
4211 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4212 0, /* tp_print */
4213 0, /* tp_getattr */
4214 0, /* tp_setattr */
4215 0, /* tp_compare */
4216 0, /* tp_repr */
4217 0, /* tp_as_number */
4218 0, /* tp_as_sequence */
4219 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004220 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004221 0, /* tp_call */
4222 0, /* tp_str */
4223 PyObject_GenericGetAttr, /* tp_getattro */
4224 PyObject_GenericSetAttr, /* tp_setattro */
4225 0, /* tp_as_buffer */
4226 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4227 0, /* tp_doc */
4228 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4229 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4230 0, /* tp_richcompare */
4231 0, /* tp_weaklistoffset */
4232 0, /* tp_iter */
4233 0, /* tp_iternext */
4234 picklerproxy_methods, /* tp_methods */
4235};
4236
4237static PyObject *
4238PicklerMemoProxy_New(PicklerObject *pickler)
4239{
4240 PicklerMemoProxyObject *self;
4241
4242 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4243 if (self == NULL)
4244 return NULL;
4245 Py_INCREF(pickler);
4246 self->pickler = pickler;
4247 PyObject_GC_Track(self);
4248 return (PyObject *)self;
4249}
4250
4251/*****************************************************************************/
4252
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004253static PyObject *
4254Pickler_get_memo(PicklerObject *self)
4255{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004256 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004257}
4258
4259static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004260Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004261{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004262 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004263
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004264 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004265 PyErr_SetString(PyExc_TypeError,
4266 "attribute deletion is not supported");
4267 return -1;
4268 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004269
4270 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4271 PicklerObject *pickler =
4272 ((PicklerMemoProxyObject *)obj)->pickler;
4273
4274 new_memo = PyMemoTable_Copy(pickler->memo);
4275 if (new_memo == NULL)
4276 return -1;
4277 }
4278 else if (PyDict_Check(obj)) {
4279 Py_ssize_t i = 0;
4280 PyObject *key, *value;
4281
4282 new_memo = PyMemoTable_New();
4283 if (new_memo == NULL)
4284 return -1;
4285
4286 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004287 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004288 PyObject *memo_obj;
4289
4290 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4291 PyErr_SetString(PyExc_TypeError,
4292 "'memo' values must be 2-item tuples");
4293 goto error;
4294 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004295 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004296 if (memo_id == -1 && PyErr_Occurred())
4297 goto error;
4298 memo_obj = PyTuple_GET_ITEM(value, 1);
4299 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4300 goto error;
4301 }
4302 }
4303 else {
4304 PyErr_Format(PyExc_TypeError,
4305 "'memo' attribute must be an PicklerMemoProxy object"
4306 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004307 return -1;
4308 }
4309
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004310 PyMemoTable_Del(self->memo);
4311 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004312
4313 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004314
4315 error:
4316 if (new_memo)
4317 PyMemoTable_Del(new_memo);
4318 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004319}
4320
4321static PyObject *
4322Pickler_get_persid(PicklerObject *self)
4323{
4324 if (self->pers_func == NULL)
4325 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4326 else
4327 Py_INCREF(self->pers_func);
4328 return self->pers_func;
4329}
4330
4331static int
4332Pickler_set_persid(PicklerObject *self, PyObject *value)
4333{
4334 PyObject *tmp;
4335
4336 if (value == NULL) {
4337 PyErr_SetString(PyExc_TypeError,
4338 "attribute deletion is not supported");
4339 return -1;
4340 }
4341 if (!PyCallable_Check(value)) {
4342 PyErr_SetString(PyExc_TypeError,
4343 "persistent_id must be a callable taking one argument");
4344 return -1;
4345 }
4346
4347 tmp = self->pers_func;
4348 Py_INCREF(value);
4349 self->pers_func = value;
4350 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4351
4352 return 0;
4353}
4354
4355static PyMemberDef Pickler_members[] = {
4356 {"bin", T_INT, offsetof(PicklerObject, bin)},
4357 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004358 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004359 {NULL}
4360};
4361
4362static PyGetSetDef Pickler_getsets[] = {
4363 {"memo", (getter)Pickler_get_memo,
4364 (setter)Pickler_set_memo},
4365 {"persistent_id", (getter)Pickler_get_persid,
4366 (setter)Pickler_set_persid},
4367 {NULL}
4368};
4369
4370static PyTypeObject Pickler_Type = {
4371 PyVarObject_HEAD_INIT(NULL, 0)
4372 "_pickle.Pickler" , /*tp_name*/
4373 sizeof(PicklerObject), /*tp_basicsize*/
4374 0, /*tp_itemsize*/
4375 (destructor)Pickler_dealloc, /*tp_dealloc*/
4376 0, /*tp_print*/
4377 0, /*tp_getattr*/
4378 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004379 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004380 0, /*tp_repr*/
4381 0, /*tp_as_number*/
4382 0, /*tp_as_sequence*/
4383 0, /*tp_as_mapping*/
4384 0, /*tp_hash*/
4385 0, /*tp_call*/
4386 0, /*tp_str*/
4387 0, /*tp_getattro*/
4388 0, /*tp_setattro*/
4389 0, /*tp_as_buffer*/
4390 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4391 Pickler_doc, /*tp_doc*/
4392 (traverseproc)Pickler_traverse, /*tp_traverse*/
4393 (inquiry)Pickler_clear, /*tp_clear*/
4394 0, /*tp_richcompare*/
4395 0, /*tp_weaklistoffset*/
4396 0, /*tp_iter*/
4397 0, /*tp_iternext*/
4398 Pickler_methods, /*tp_methods*/
4399 Pickler_members, /*tp_members*/
4400 Pickler_getsets, /*tp_getset*/
4401 0, /*tp_base*/
4402 0, /*tp_dict*/
4403 0, /*tp_descr_get*/
4404 0, /*tp_descr_set*/
4405 0, /*tp_dictoffset*/
4406 (initproc)Pickler_init, /*tp_init*/
4407 PyType_GenericAlloc, /*tp_alloc*/
4408 PyType_GenericNew, /*tp_new*/
4409 PyObject_GC_Del, /*tp_free*/
4410 0, /*tp_is_gc*/
4411};
4412
Victor Stinner121aab42011-09-29 23:40:53 +02004413/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004414
4415 XXX: It would be nice to able to avoid Python function call overhead, by
4416 using directly the C version of find_class(), when find_class() is not
4417 overridden by a subclass. Although, this could become rather hackish. A
4418 simpler optimization would be to call the C function when self is not a
4419 subclass instance. */
4420static PyObject *
4421find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4422{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004423 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004424
4425 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4426 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004427}
4428
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004429static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004430marker(UnpicklerObject *self)
4431{
4432 if (self->num_marks < 1) {
4433 PyErr_SetString(UnpicklingError, "could not find MARK");
4434 return -1;
4435 }
4436
4437 return self->marks[--self->num_marks];
4438}
4439
4440static int
4441load_none(UnpicklerObject *self)
4442{
4443 PDATA_APPEND(self->stack, Py_None, -1);
4444 return 0;
4445}
4446
4447static int
4448bad_readline(void)
4449{
4450 PyErr_SetString(UnpicklingError, "pickle data was truncated");
4451 return -1;
4452}
4453
4454static int
4455load_int(UnpicklerObject *self)
4456{
4457 PyObject *value;
4458 char *endptr, *s;
4459 Py_ssize_t len;
4460 long x;
4461
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004462 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004463 return -1;
4464 if (len < 2)
4465 return bad_readline();
4466
4467 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004468 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004469 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004470 x = strtol(s, &endptr, 0);
4471
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004472 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004473 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004474 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004475 errno = 0;
4476 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004477 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004478 if (value == NULL) {
4479 PyErr_SetString(PyExc_ValueError,
4480 "could not convert string to int");
4481 return -1;
4482 }
4483 }
4484 else {
4485 if (len == 3 && (x == 0 || x == 1)) {
4486 if ((value = PyBool_FromLong(x)) == NULL)
4487 return -1;
4488 }
4489 else {
4490 if ((value = PyLong_FromLong(x)) == NULL)
4491 return -1;
4492 }
4493 }
4494
4495 PDATA_PUSH(self->stack, value, -1);
4496 return 0;
4497}
4498
4499static int
4500load_bool(UnpicklerObject *self, PyObject *boolean)
4501{
4502 assert(boolean == Py_True || boolean == Py_False);
4503 PDATA_APPEND(self->stack, boolean, -1);
4504 return 0;
4505}
4506
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004507/* s contains x bytes of an unsigned little-endian integer. Return its value
4508 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4509 */
4510static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004511calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004512{
4513 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004514 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004515 size_t x = 0;
4516
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004517 for (i = 0; i < nbytes; i++) {
4518 x |= (size_t) s[i] << (8 * i);
4519 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004520
4521 if (x > PY_SSIZE_T_MAX)
4522 return -1;
4523 else
4524 return (Py_ssize_t) x;
4525}
4526
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004527/* s contains x bytes of a little-endian integer. Return its value as a
4528 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4529 * int, but when x is 4 it's a signed one. This is an historical source
4530 * of x-platform bugs.
4531 */
4532static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004533calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004534{
4535 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004536 int i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004537 long x = 0;
4538
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004539 for (i = 0; i < nbytes; i++) {
4540 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004541 }
4542
4543 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4544 * is signed, so on a box with longs bigger than 4 bytes we need
4545 * to extend a BININT's sign bit to the full width.
4546 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004547 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004548 x |= -(x & (1L << 31));
4549 }
4550
4551 return x;
4552}
4553
4554static int
4555load_binintx(UnpicklerObject *self, char *s, int size)
4556{
4557 PyObject *value;
4558 long x;
4559
4560 x = calc_binint(s, size);
4561
4562 if ((value = PyLong_FromLong(x)) == NULL)
4563 return -1;
4564
4565 PDATA_PUSH(self->stack, value, -1);
4566 return 0;
4567}
4568
4569static int
4570load_binint(UnpicklerObject *self)
4571{
4572 char *s;
4573
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004574 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004575 return -1;
4576
4577 return load_binintx(self, s, 4);
4578}
4579
4580static int
4581load_binint1(UnpicklerObject *self)
4582{
4583 char *s;
4584
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004585 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004586 return -1;
4587
4588 return load_binintx(self, s, 1);
4589}
4590
4591static int
4592load_binint2(UnpicklerObject *self)
4593{
4594 char *s;
4595
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004596 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004597 return -1;
4598
4599 return load_binintx(self, s, 2);
4600}
4601
4602static int
4603load_long(UnpicklerObject *self)
4604{
4605 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004606 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004607 Py_ssize_t len;
4608
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004609 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004610 return -1;
4611 if (len < 2)
4612 return bad_readline();
4613
Mark Dickinson8dd05142009-01-20 20:43:58 +00004614 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4615 the 'L' before calling PyLong_FromString. In order to maintain
4616 compatibility with Python 3.0.0, we don't actually *require*
4617 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004618 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004619 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004620 /* XXX: Should the base argument explicitly set to 10? */
4621 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004622 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004623 return -1;
4624
4625 PDATA_PUSH(self->stack, value, -1);
4626 return 0;
4627}
4628
4629/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4630 * data following.
4631 */
4632static int
4633load_counted_long(UnpicklerObject *self, int size)
4634{
4635 PyObject *value;
4636 char *nbytes;
4637 char *pdata;
4638
4639 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004640 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004641 return -1;
4642
4643 size = calc_binint(nbytes, size);
4644 if (size < 0) {
4645 /* Corrupt or hostile pickle -- we never write one like this */
4646 PyErr_SetString(UnpicklingError,
4647 "LONG pickle has negative byte count");
4648 return -1;
4649 }
4650
4651 if (size == 0)
4652 value = PyLong_FromLong(0L);
4653 else {
4654 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004655 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004656 return -1;
4657 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4658 1 /* little endian */ , 1 /* signed */ );
4659 }
4660 if (value == NULL)
4661 return -1;
4662 PDATA_PUSH(self->stack, value, -1);
4663 return 0;
4664}
4665
4666static int
4667load_float(UnpicklerObject *self)
4668{
4669 PyObject *value;
4670 char *endptr, *s;
4671 Py_ssize_t len;
4672 double d;
4673
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004674 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004675 return -1;
4676 if (len < 2)
4677 return bad_readline();
4678
4679 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004680 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4681 if (d == -1.0 && PyErr_Occurred())
4682 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004683 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004684 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4685 return -1;
4686 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004687 value = PyFloat_FromDouble(d);
4688 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004689 return -1;
4690
4691 PDATA_PUSH(self->stack, value, -1);
4692 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004693}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004694
4695static int
4696load_binfloat(UnpicklerObject *self)
4697{
4698 PyObject *value;
4699 double x;
4700 char *s;
4701
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004702 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004703 return -1;
4704
4705 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4706 if (x == -1.0 && PyErr_Occurred())
4707 return -1;
4708
4709 if ((value = PyFloat_FromDouble(x)) == NULL)
4710 return -1;
4711
4712 PDATA_PUSH(self->stack, value, -1);
4713 return 0;
4714}
4715
4716static int
4717load_string(UnpicklerObject *self)
4718{
4719 PyObject *bytes;
4720 PyObject *str = NULL;
4721 Py_ssize_t len;
4722 char *s, *p;
4723
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004724 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004725 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004726 /* Strip the newline */
4727 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004728 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004729 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004730 p = s + 1;
4731 len -= 2;
4732 }
4733 else {
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004734 PyErr_SetString(UnpicklingError,
4735 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004736 return -1;
4737 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004738 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004739
4740 /* Use the PyBytes API to decode the string, since that is what is used
4741 to encode, and then coerce the result to Unicode. */
4742 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004743 if (bytes == NULL)
4744 return -1;
4745 str = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4746 Py_DECREF(bytes);
4747 if (str == NULL)
4748 return -1;
4749
4750 PDATA_PUSH(self->stack, str, -1);
4751 return 0;
4752}
4753
4754static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004755load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004756{
4757 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004758 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004759 char *s;
4760
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004761 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004762 return -1;
4763
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004764 size = calc_binsize(s, nbytes);
4765 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004766 PyErr_Format(PyExc_OverflowError,
4767 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004768 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004769 return -1;
4770 }
4771
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004772 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004773 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004774
4775 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004776 if (bytes == NULL)
4777 return -1;
4778
4779 PDATA_PUSH(self->stack, bytes, -1);
4780 return 0;
4781}
4782
4783static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004784load_counted_binstring(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004785{
4786 PyObject *str;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004787 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004788 char *s;
4789
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004790 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004791 return -1;
4792
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004793 size = calc_binsize(s, nbytes);
4794 if (size < 0) {
4795 PyErr_Format(UnpicklingError,
4796 "BINSTRING exceeds system's maximum size of %zd bytes",
4797 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004798 return -1;
4799 }
4800
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004801 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004802 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004803 /* Convert Python 2.x strings to unicode. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004804 str = PyUnicode_Decode(s, size, self->encoding, self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004805 if (str == NULL)
4806 return -1;
4807
4808 PDATA_PUSH(self->stack, str, -1);
4809 return 0;
4810}
4811
4812static int
4813load_unicode(UnpicklerObject *self)
4814{
4815 PyObject *str;
4816 Py_ssize_t len;
4817 char *s;
4818
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004819 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004820 return -1;
4821 if (len < 1)
4822 return bad_readline();
4823
4824 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
4825 if (str == NULL)
4826 return -1;
4827
4828 PDATA_PUSH(self->stack, str, -1);
4829 return 0;
4830}
4831
4832static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004833load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004834{
4835 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004836 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004837 char *s;
4838
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004839 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004840 return -1;
4841
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004842 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004843 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004844 PyErr_Format(PyExc_OverflowError,
4845 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004846 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004847 return -1;
4848 }
4849
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004850 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004851 return -1;
4852
Victor Stinner485fb562010-04-13 11:07:24 +00004853 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004854 if (str == NULL)
4855 return -1;
4856
4857 PDATA_PUSH(self->stack, str, -1);
4858 return 0;
4859}
4860
4861static int
4862load_tuple(UnpicklerObject *self)
4863{
4864 PyObject *tuple;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004865 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004866
4867 if ((i = marker(self)) < 0)
4868 return -1;
4869
4870 tuple = Pdata_poptuple(self->stack, i);
4871 if (tuple == NULL)
4872 return -1;
4873 PDATA_PUSH(self->stack, tuple, -1);
4874 return 0;
4875}
4876
4877static int
4878load_counted_tuple(UnpicklerObject *self, int len)
4879{
4880 PyObject *tuple;
4881
4882 tuple = PyTuple_New(len);
4883 if (tuple == NULL)
4884 return -1;
4885
4886 while (--len >= 0) {
4887 PyObject *item;
4888
4889 PDATA_POP(self->stack, item);
4890 if (item == NULL)
4891 return -1;
4892 PyTuple_SET_ITEM(tuple, len, item);
4893 }
4894 PDATA_PUSH(self->stack, tuple, -1);
4895 return 0;
4896}
4897
4898static int
4899load_empty_list(UnpicklerObject *self)
4900{
4901 PyObject *list;
4902
4903 if ((list = PyList_New(0)) == NULL)
4904 return -1;
4905 PDATA_PUSH(self->stack, list, -1);
4906 return 0;
4907}
4908
4909static int
4910load_empty_dict(UnpicklerObject *self)
4911{
4912 PyObject *dict;
4913
4914 if ((dict = PyDict_New()) == NULL)
4915 return -1;
4916 PDATA_PUSH(self->stack, dict, -1);
4917 return 0;
4918}
4919
4920static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004921load_empty_set(UnpicklerObject *self)
4922{
4923 PyObject *set;
4924
4925 if ((set = PySet_New(NULL)) == NULL)
4926 return -1;
4927 PDATA_PUSH(self->stack, set, -1);
4928 return 0;
4929}
4930
4931static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004932load_list(UnpicklerObject *self)
4933{
4934 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004935 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004936
4937 if ((i = marker(self)) < 0)
4938 return -1;
4939
4940 list = Pdata_poplist(self->stack, i);
4941 if (list == NULL)
4942 return -1;
4943 PDATA_PUSH(self->stack, list, -1);
4944 return 0;
4945}
4946
4947static int
4948load_dict(UnpicklerObject *self)
4949{
4950 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004951 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004952
4953 if ((i = marker(self)) < 0)
4954 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004955 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004956
4957 if ((dict = PyDict_New()) == NULL)
4958 return -1;
4959
4960 for (k = i + 1; k < j; k += 2) {
4961 key = self->stack->data[k - 1];
4962 value = self->stack->data[k];
4963 if (PyDict_SetItem(dict, key, value) < 0) {
4964 Py_DECREF(dict);
4965 return -1;
4966 }
4967 }
4968 Pdata_clear(self->stack, i);
4969 PDATA_PUSH(self->stack, dict, -1);
4970 return 0;
4971}
4972
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004973static int
4974load_frozenset(UnpicklerObject *self)
4975{
4976 PyObject *items;
4977 PyObject *frozenset;
4978 Py_ssize_t i;
4979
4980 if ((i = marker(self)) < 0)
4981 return -1;
4982
4983 items = Pdata_poptuple(self->stack, i);
4984 if (items == NULL)
4985 return -1;
4986
4987 frozenset = PyFrozenSet_New(items);
4988 Py_DECREF(items);
4989 if (frozenset == NULL)
4990 return -1;
4991
4992 PDATA_PUSH(self->stack, frozenset, -1);
4993 return 0;
4994}
4995
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004996static PyObject *
4997instantiate(PyObject *cls, PyObject *args)
4998{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00004999 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005000 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005001 /* Caller must assure args are a tuple. Normally, args come from
5002 Pdata_poptuple which packs objects from the top of the stack
5003 into a newly created tuple. */
5004 assert(PyTuple_Check(args));
5005 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005006 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005007 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005008 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005009 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005010 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005011
5012 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005013 }
5014 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005015}
5016
5017static int
5018load_obj(UnpicklerObject *self)
5019{
5020 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005021 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005022
5023 if ((i = marker(self)) < 0)
5024 return -1;
5025
5026 args = Pdata_poptuple(self->stack, i + 1);
5027 if (args == NULL)
5028 return -1;
5029
5030 PDATA_POP(self->stack, cls);
5031 if (cls) {
5032 obj = instantiate(cls, args);
5033 Py_DECREF(cls);
5034 }
5035 Py_DECREF(args);
5036 if (obj == NULL)
5037 return -1;
5038
5039 PDATA_PUSH(self->stack, obj, -1);
5040 return 0;
5041}
5042
5043static int
5044load_inst(UnpicklerObject *self)
5045{
5046 PyObject *cls = NULL;
5047 PyObject *args = NULL;
5048 PyObject *obj = NULL;
5049 PyObject *module_name;
5050 PyObject *class_name;
5051 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005052 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005053 char *s;
5054
5055 if ((i = marker(self)) < 0)
5056 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005057 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005058 return -1;
5059 if (len < 2)
5060 return bad_readline();
5061
5062 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5063 identifiers are permitted in Python 3.0, since the INST opcode is only
5064 supported by older protocols on Python 2.x. */
5065 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5066 if (module_name == NULL)
5067 return -1;
5068
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005069 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005070 if (len < 2)
5071 return bad_readline();
5072 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005073 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005074 cls = find_class(self, module_name, class_name);
5075 Py_DECREF(class_name);
5076 }
5077 }
5078 Py_DECREF(module_name);
5079
5080 if (cls == NULL)
5081 return -1;
5082
5083 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5084 obj = instantiate(cls, args);
5085 Py_DECREF(args);
5086 }
5087 Py_DECREF(cls);
5088
5089 if (obj == NULL)
5090 return -1;
5091
5092 PDATA_PUSH(self->stack, obj, -1);
5093 return 0;
5094}
5095
5096static int
5097load_newobj(UnpicklerObject *self)
5098{
5099 PyObject *args = NULL;
5100 PyObject *clsraw = NULL;
5101 PyTypeObject *cls; /* clsraw cast to its true type */
5102 PyObject *obj;
5103
5104 /* Stack is ... cls argtuple, and we want to call
5105 * cls.__new__(cls, *argtuple).
5106 */
5107 PDATA_POP(self->stack, args);
5108 if (args == NULL)
5109 goto error;
5110 if (!PyTuple_Check(args)) {
5111 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg " "tuple.");
5112 goto error;
5113 }
5114
5115 PDATA_POP(self->stack, clsraw);
5116 cls = (PyTypeObject *)clsraw;
5117 if (cls == NULL)
5118 goto error;
5119 if (!PyType_Check(cls)) {
5120 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
5121 "isn't a type object");
5122 goto error;
5123 }
5124 if (cls->tp_new == NULL) {
5125 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
5126 "has NULL tp_new");
5127 goto error;
5128 }
5129
5130 /* Call __new__. */
5131 obj = cls->tp_new(cls, args, NULL);
5132 if (obj == NULL)
5133 goto error;
5134
5135 Py_DECREF(args);
5136 Py_DECREF(clsraw);
5137 PDATA_PUSH(self->stack, obj, -1);
5138 return 0;
5139
5140 error:
5141 Py_XDECREF(args);
5142 Py_XDECREF(clsraw);
5143 return -1;
5144}
5145
5146static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005147load_newobj_ex(UnpicklerObject *self)
5148{
5149 PyObject *cls, *args, *kwargs;
5150 PyObject *obj;
5151
5152 PDATA_POP(self->stack, kwargs);
5153 if (kwargs == NULL) {
5154 return -1;
5155 }
5156 PDATA_POP(self->stack, args);
5157 if (args == NULL) {
5158 Py_DECREF(kwargs);
5159 return -1;
5160 }
5161 PDATA_POP(self->stack, cls);
5162 if (cls == NULL) {
5163 Py_DECREF(kwargs);
5164 Py_DECREF(args);
5165 return -1;
5166 }
5167
5168 if (!PyType_Check(cls)) {
5169 Py_DECREF(kwargs);
5170 Py_DECREF(args);
5171 Py_DECREF(cls);
5172 PyErr_Format(UnpicklingError,
5173 "NEWOBJ_EX class argument must be a type, not %.200s",
5174 Py_TYPE(cls)->tp_name);
5175 return -1;
5176 }
5177
5178 if (((PyTypeObject *)cls)->tp_new == NULL) {
5179 Py_DECREF(kwargs);
5180 Py_DECREF(args);
5181 Py_DECREF(cls);
5182 PyErr_SetString(UnpicklingError,
5183 "NEWOBJ_EX class argument doesn't have __new__");
5184 return -1;
5185 }
5186 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5187 Py_DECREF(kwargs);
5188 Py_DECREF(args);
5189 Py_DECREF(cls);
5190 if (obj == NULL) {
5191 return -1;
5192 }
5193 PDATA_PUSH(self->stack, obj, -1);
5194 return 0;
5195}
5196
5197static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005198load_global(UnpicklerObject *self)
5199{
5200 PyObject *global = NULL;
5201 PyObject *module_name;
5202 PyObject *global_name;
5203 Py_ssize_t len;
5204 char *s;
5205
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005206 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005207 return -1;
5208 if (len < 2)
5209 return bad_readline();
5210 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5211 if (!module_name)
5212 return -1;
5213
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005214 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005215 if (len < 2) {
5216 Py_DECREF(module_name);
5217 return bad_readline();
5218 }
5219 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5220 if (global_name) {
5221 global = find_class(self, module_name, global_name);
5222 Py_DECREF(global_name);
5223 }
5224 }
5225 Py_DECREF(module_name);
5226
5227 if (global == NULL)
5228 return -1;
5229 PDATA_PUSH(self->stack, global, -1);
5230 return 0;
5231}
5232
5233static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005234load_stack_global(UnpicklerObject *self)
5235{
5236 PyObject *global;
5237 PyObject *module_name;
5238 PyObject *global_name;
5239
5240 PDATA_POP(self->stack, global_name);
5241 PDATA_POP(self->stack, module_name);
5242 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5243 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
5244 PyErr_SetString(UnpicklingError, "STACK_GLOBAL requires str");
5245 Py_XDECREF(global_name);
5246 Py_XDECREF(module_name);
5247 return -1;
5248 }
5249 global = find_class(self, module_name, global_name);
5250 Py_DECREF(global_name);
5251 Py_DECREF(module_name);
5252 if (global == NULL)
5253 return -1;
5254 PDATA_PUSH(self->stack, global, -1);
5255 return 0;
5256}
5257
5258static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005259load_persid(UnpicklerObject *self)
5260{
5261 PyObject *pid;
5262 Py_ssize_t len;
5263 char *s;
5264
5265 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005266 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005267 return -1;
5268 if (len < 2)
5269 return bad_readline();
5270
5271 pid = PyBytes_FromStringAndSize(s, len - 1);
5272 if (pid == NULL)
5273 return -1;
5274
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005275 /* Ugh... this does not leak since _Unpickler_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005276 reference to pid first. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005277 pid = _Unpickler_FastCall(self, self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005278 if (pid == NULL)
5279 return -1;
5280
5281 PDATA_PUSH(self->stack, pid, -1);
5282 return 0;
5283 }
5284 else {
5285 PyErr_SetString(UnpicklingError,
5286 "A load persistent id instruction was encountered,\n"
5287 "but no persistent_load function was specified.");
5288 return -1;
5289 }
5290}
5291
5292static int
5293load_binpersid(UnpicklerObject *self)
5294{
5295 PyObject *pid;
5296
5297 if (self->pers_func) {
5298 PDATA_POP(self->stack, pid);
5299 if (pid == NULL)
5300 return -1;
5301
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005302 /* Ugh... this does not leak since _Unpickler_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005303 reference to pid first. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005304 pid = _Unpickler_FastCall(self, self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005305 if (pid == NULL)
5306 return -1;
5307
5308 PDATA_PUSH(self->stack, pid, -1);
5309 return 0;
5310 }
5311 else {
5312 PyErr_SetString(UnpicklingError,
5313 "A load persistent id instruction was encountered,\n"
5314 "but no persistent_load function was specified.");
5315 return -1;
5316 }
5317}
5318
5319static int
5320load_pop(UnpicklerObject *self)
5321{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005322 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005323
5324 /* Note that we split the (pickle.py) stack into two stacks,
5325 * an object stack and a mark stack. We have to be clever and
5326 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005327 * mark stack first, and only signalling a stack underflow if
5328 * the object stack is empty and the mark stack doesn't match
5329 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005330 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005331 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005332 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005333 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005334 len--;
5335 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005336 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005337 } else {
5338 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005339 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005340 return 0;
5341}
5342
5343static int
5344load_pop_mark(UnpicklerObject *self)
5345{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005346 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005347
5348 if ((i = marker(self)) < 0)
5349 return -1;
5350
5351 Pdata_clear(self->stack, i);
5352
5353 return 0;
5354}
5355
5356static int
5357load_dup(UnpicklerObject *self)
5358{
5359 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005360 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005361
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005362 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005363 return stack_underflow();
5364 last = self->stack->data[len - 1];
5365 PDATA_APPEND(self->stack, last, -1);
5366 return 0;
5367}
5368
5369static int
5370load_get(UnpicklerObject *self)
5371{
5372 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005373 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005374 Py_ssize_t len;
5375 char *s;
5376
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005377 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005378 return -1;
5379 if (len < 2)
5380 return bad_readline();
5381
5382 key = PyLong_FromString(s, NULL, 10);
5383 if (key == NULL)
5384 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005385 idx = PyLong_AsSsize_t(key);
5386 if (idx == -1 && PyErr_Occurred()) {
5387 Py_DECREF(key);
5388 return -1;
5389 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005390
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005391 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005392 if (value == NULL) {
5393 if (!PyErr_Occurred())
5394 PyErr_SetObject(PyExc_KeyError, key);
5395 Py_DECREF(key);
5396 return -1;
5397 }
5398 Py_DECREF(key);
5399
5400 PDATA_APPEND(self->stack, value, -1);
5401 return 0;
5402}
5403
5404static int
5405load_binget(UnpicklerObject *self)
5406{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005407 PyObject *value;
5408 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005409 char *s;
5410
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005411 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005412 return -1;
5413
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005414 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005415
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005416 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005417 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005418 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005419 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005420 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005421 Py_DECREF(key);
5422 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005423 return -1;
5424 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005425
5426 PDATA_APPEND(self->stack, value, -1);
5427 return 0;
5428}
5429
5430static int
5431load_long_binget(UnpicklerObject *self)
5432{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005433 PyObject *value;
5434 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005435 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005436
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005437 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005438 return -1;
5439
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005440 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005441
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005442 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005443 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005444 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005445 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005446 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005447 Py_DECREF(key);
5448 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005449 return -1;
5450 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005451
5452 PDATA_APPEND(self->stack, value, -1);
5453 return 0;
5454}
5455
5456/* Push an object from the extension registry (EXT[124]). nbytes is
5457 * the number of bytes following the opcode, holding the index (code) value.
5458 */
5459static int
5460load_extension(UnpicklerObject *self, int nbytes)
5461{
5462 char *codebytes; /* the nbytes bytes after the opcode */
5463 long code; /* calc_binint returns long */
5464 PyObject *py_code; /* code as a Python int */
5465 PyObject *obj; /* the object to push */
5466 PyObject *pair; /* (module_name, class_name) */
5467 PyObject *module_name, *class_name;
5468
5469 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005470 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005471 return -1;
5472 code = calc_binint(codebytes, nbytes);
5473 if (code <= 0) { /* note that 0 is forbidden */
5474 /* Corrupt or hostile pickle. */
5475 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
5476 return -1;
5477 }
5478
5479 /* Look for the code in the cache. */
5480 py_code = PyLong_FromLong(code);
5481 if (py_code == NULL)
5482 return -1;
5483 obj = PyDict_GetItem(extension_cache, py_code);
5484 if (obj != NULL) {
5485 /* Bingo. */
5486 Py_DECREF(py_code);
5487 PDATA_APPEND(self->stack, obj, -1);
5488 return 0;
5489 }
5490
5491 /* Look up the (module_name, class_name) pair. */
5492 pair = PyDict_GetItem(inverted_registry, py_code);
5493 if (pair == NULL) {
5494 Py_DECREF(py_code);
5495 PyErr_Format(PyExc_ValueError, "unregistered extension "
5496 "code %ld", code);
5497 return -1;
5498 }
5499 /* Since the extension registry is manipulable via Python code,
5500 * confirm that pair is really a 2-tuple of strings.
5501 */
5502 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5503 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5504 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5505 Py_DECREF(py_code);
5506 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5507 "isn't a 2-tuple of strings", code);
5508 return -1;
5509 }
5510 /* Load the object. */
5511 obj = find_class(self, module_name, class_name);
5512 if (obj == NULL) {
5513 Py_DECREF(py_code);
5514 return -1;
5515 }
5516 /* Cache code -> obj. */
5517 code = PyDict_SetItem(extension_cache, py_code, obj);
5518 Py_DECREF(py_code);
5519 if (code < 0) {
5520 Py_DECREF(obj);
5521 return -1;
5522 }
5523 PDATA_PUSH(self->stack, obj, -1);
5524 return 0;
5525}
5526
5527static int
5528load_put(UnpicklerObject *self)
5529{
5530 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005531 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005532 Py_ssize_t len;
5533 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005534
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005535 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005536 return -1;
5537 if (len < 2)
5538 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005539 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005540 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005541 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005542
5543 key = PyLong_FromString(s, NULL, 10);
5544 if (key == NULL)
5545 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005546 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005547 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005548 if (idx < 0) {
5549 if (!PyErr_Occurred())
5550 PyErr_SetString(PyExc_ValueError,
5551 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005552 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005553 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005554
5555 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005556}
5557
5558static int
5559load_binput(UnpicklerObject *self)
5560{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005561 PyObject *value;
5562 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005563 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005564
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005565 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005566 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005567
5568 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005569 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005570 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005571
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005572 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005573
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005574 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005575}
5576
5577static int
5578load_long_binput(UnpicklerObject *self)
5579{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005580 PyObject *value;
5581 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005582 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005583
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005584 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005585 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005586
5587 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005588 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005589 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005590
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005591 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005592 if (idx < 0) {
5593 PyErr_SetString(PyExc_ValueError,
5594 "negative LONG_BINPUT argument");
5595 return -1;
5596 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005597
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005598 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005599}
5600
5601static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005602load_memoize(UnpicklerObject *self)
5603{
5604 PyObject *value;
5605
5606 if (Py_SIZE(self->stack) <= 0)
5607 return stack_underflow();
5608 value = self->stack->data[Py_SIZE(self->stack) - 1];
5609
5610 return _Unpickler_MemoPut(self, self->memo_len, value);
5611}
5612
5613static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005614do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005615{
5616 PyObject *value;
5617 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005618 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005619
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005620 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005621 if (x > len || x <= 0)
5622 return stack_underflow();
5623 if (len == x) /* nothing to do */
5624 return 0;
5625
5626 list = self->stack->data[x - 1];
5627
5628 if (PyList_Check(list)) {
5629 PyObject *slice;
5630 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005631 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005632
5633 slice = Pdata_poplist(self->stack, x);
5634 if (!slice)
5635 return -1;
5636 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005637 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005638 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005639 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005640 }
5641 else {
5642 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005643 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005644
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005645 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005646 if (append_func == NULL)
5647 return -1;
5648 for (i = x; i < len; i++) {
5649 PyObject *result;
5650
5651 value = self->stack->data[i];
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005652 result = _Unpickler_FastCall(self, append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005653 if (result == NULL) {
5654 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005655 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005656 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005657 return -1;
5658 }
5659 Py_DECREF(result);
5660 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005661 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005662 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005663 }
5664
5665 return 0;
5666}
5667
5668static int
5669load_append(UnpicklerObject *self)
5670{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005671 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005672}
5673
5674static int
5675load_appends(UnpicklerObject *self)
5676{
5677 return do_append(self, marker(self));
5678}
5679
5680static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005681do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005682{
5683 PyObject *value, *key;
5684 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005685 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005686 int status = 0;
5687
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005688 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005689 if (x > len || x <= 0)
5690 return stack_underflow();
5691 if (len == x) /* nothing to do */
5692 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005693 if ((len - x) % 2 != 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005694 /* Currupt or hostile pickle -- we never write one like this. */
5695 PyErr_SetString(UnpicklingError, "odd number of items for SETITEMS");
5696 return -1;
5697 }
5698
5699 /* Here, dict does not actually need to be a PyDict; it could be anything
5700 that supports the __setitem__ attribute. */
5701 dict = self->stack->data[x - 1];
5702
5703 for (i = x + 1; i < len; i += 2) {
5704 key = self->stack->data[i - 1];
5705 value = self->stack->data[i];
5706 if (PyObject_SetItem(dict, key, value) < 0) {
5707 status = -1;
5708 break;
5709 }
5710 }
5711
5712 Pdata_clear(self->stack, x);
5713 return status;
5714}
5715
5716static int
5717load_setitem(UnpicklerObject *self)
5718{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005719 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005720}
5721
5722static int
5723load_setitems(UnpicklerObject *self)
5724{
5725 return do_setitems(self, marker(self));
5726}
5727
5728static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005729load_additems(UnpicklerObject *self)
5730{
5731 PyObject *set;
5732 Py_ssize_t mark, len, i;
5733
5734 mark = marker(self);
5735 len = Py_SIZE(self->stack);
5736 if (mark > len || mark <= 0)
5737 return stack_underflow();
5738 if (len == mark) /* nothing to do */
5739 return 0;
5740
5741 set = self->stack->data[mark - 1];
5742
5743 if (PySet_Check(set)) {
5744 PyObject *items;
5745 int status;
5746
5747 items = Pdata_poptuple(self->stack, mark);
5748 if (items == NULL)
5749 return -1;
5750
5751 status = _PySet_Update(set, items);
5752 Py_DECREF(items);
5753 return status;
5754 }
5755 else {
5756 PyObject *add_func;
5757 _Py_IDENTIFIER(add);
5758
5759 add_func = _PyObject_GetAttrId(set, &PyId_add);
5760 if (add_func == NULL)
5761 return -1;
5762 for (i = mark; i < len; i++) {
5763 PyObject *result;
5764 PyObject *item;
5765
5766 item = self->stack->data[i];
5767 result = _Unpickler_FastCall(self, add_func, item);
5768 if (result == NULL) {
5769 Pdata_clear(self->stack, i + 1);
5770 Py_SIZE(self->stack) = mark;
5771 return -1;
5772 }
5773 Py_DECREF(result);
5774 }
5775 Py_SIZE(self->stack) = mark;
5776 }
5777
5778 return 0;
5779}
5780
5781static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005782load_build(UnpicklerObject *self)
5783{
5784 PyObject *state, *inst, *slotstate;
5785 PyObject *setstate;
5786 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005787 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005788
5789 /* Stack is ... instance, state. We want to leave instance at
5790 * the stack top, possibly mutated via instance.__setstate__(state).
5791 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005792 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005793 return stack_underflow();
5794
5795 PDATA_POP(self->stack, state);
5796 if (state == NULL)
5797 return -1;
5798
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005799 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005800
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005801 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005802 if (setstate == NULL) {
5803 if (PyErr_ExceptionMatches(PyExc_AttributeError))
5804 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00005805 else {
5806 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005807 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00005808 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005809 }
5810 else {
5811 PyObject *result;
5812
5813 /* The explicit __setstate__ is responsible for everything. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005814 /* Ugh... this does not leak since _Unpickler_FastCall() steals the
Antoine Pitroud79dc622008-09-05 00:03:33 +00005815 reference to state first. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005816 result = _Unpickler_FastCall(self, setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005817 Py_DECREF(setstate);
5818 if (result == NULL)
5819 return -1;
5820 Py_DECREF(result);
5821 return 0;
5822 }
5823
5824 /* A default __setstate__. First see whether state embeds a
5825 * slot state dict too (a proto 2 addition).
5826 */
5827 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
5828 PyObject *tmp = state;
5829
5830 state = PyTuple_GET_ITEM(tmp, 0);
5831 slotstate = PyTuple_GET_ITEM(tmp, 1);
5832 Py_INCREF(state);
5833 Py_INCREF(slotstate);
5834 Py_DECREF(tmp);
5835 }
5836 else
5837 slotstate = NULL;
5838
5839 /* Set inst.__dict__ from the state dict (if any). */
5840 if (state != Py_None) {
5841 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005842 PyObject *d_key, *d_value;
5843 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005844 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005845
5846 if (!PyDict_Check(state)) {
5847 PyErr_SetString(UnpicklingError, "state is not a dictionary");
5848 goto error;
5849 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005850 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005851 if (dict == NULL)
5852 goto error;
5853
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005854 i = 0;
5855 while (PyDict_Next(state, &i, &d_key, &d_value)) {
5856 /* normally the keys for instance attributes are
5857 interned. we should try to do that here. */
5858 Py_INCREF(d_key);
5859 if (PyUnicode_CheckExact(d_key))
5860 PyUnicode_InternInPlace(&d_key);
5861 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
5862 Py_DECREF(d_key);
5863 goto error;
5864 }
5865 Py_DECREF(d_key);
5866 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005867 Py_DECREF(dict);
5868 }
5869
5870 /* Also set instance attributes from the slotstate dict (if any). */
5871 if (slotstate != NULL) {
5872 PyObject *d_key, *d_value;
5873 Py_ssize_t i;
5874
5875 if (!PyDict_Check(slotstate)) {
5876 PyErr_SetString(UnpicklingError,
5877 "slot state is not a dictionary");
5878 goto error;
5879 }
5880 i = 0;
5881 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
5882 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
5883 goto error;
5884 }
5885 }
5886
5887 if (0) {
5888 error:
5889 status = -1;
5890 }
5891
5892 Py_DECREF(state);
5893 Py_XDECREF(slotstate);
5894 return status;
5895}
5896
5897static int
5898load_mark(UnpicklerObject *self)
5899{
5900
5901 /* Note that we split the (pickle.py) stack into two stacks, an
5902 * object stack and a mark stack. Here we push a mark onto the
5903 * mark stack.
5904 */
5905
5906 if ((self->num_marks + 1) >= self->marks_size) {
5907 size_t alloc;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005908 Py_ssize_t *marks;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005909
5910 /* Use the size_t type to check for overflow. */
5911 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005912 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00005913 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005914 PyErr_NoMemory();
5915 return -1;
5916 }
5917
5918 if (self->marks == NULL)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005919 marks = (Py_ssize_t *) PyMem_Malloc(alloc * sizeof(Py_ssize_t));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005920 else
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005921 marks = (Py_ssize_t *) PyMem_Realloc(self->marks,
5922 alloc * sizeof(Py_ssize_t));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005923 if (marks == NULL) {
5924 PyErr_NoMemory();
5925 return -1;
5926 }
5927 self->marks = marks;
5928 self->marks_size = (Py_ssize_t)alloc;
5929 }
5930
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005931 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005932
5933 return 0;
5934}
5935
5936static int
5937load_reduce(UnpicklerObject *self)
5938{
5939 PyObject *callable = NULL;
5940 PyObject *argtup = NULL;
5941 PyObject *obj = NULL;
5942
5943 PDATA_POP(self->stack, argtup);
5944 if (argtup == NULL)
5945 return -1;
5946 PDATA_POP(self->stack, callable);
5947 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005948 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005949 Py_DECREF(callable);
5950 }
5951 Py_DECREF(argtup);
5952
5953 if (obj == NULL)
5954 return -1;
5955
5956 PDATA_PUSH(self->stack, obj, -1);
5957 return 0;
5958}
5959
5960/* Just raises an error if we don't know the protocol specified. PROTO
5961 * is the first opcode for protocols >= 2.
5962 */
5963static int
5964load_proto(UnpicklerObject *self)
5965{
5966 char *s;
5967 int i;
5968
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005969 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005970 return -1;
5971
5972 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00005973 if (i <= HIGHEST_PROTOCOL) {
5974 self->proto = i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005975 self->framing = (self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005976 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00005977 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005978
5979 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
5980 return -1;
5981}
5982
5983static PyObject *
5984load(UnpicklerObject *self)
5985{
5986 PyObject *err;
5987 PyObject *value = NULL;
5988 char *s;
5989
5990 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005991 self->proto = 0;
5992 self->framing = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005993 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005994 Pdata_clear(self->stack, 0);
5995
5996 /* Convenient macros for the dispatch while-switch loop just below. */
5997#define OP(opcode, load_func) \
5998 case opcode: if (load_func(self) < 0) break; continue;
5999
6000#define OP_ARG(opcode, load_func, arg) \
6001 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6002
6003 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006004 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006005 break;
6006
6007 switch ((enum opcode)s[0]) {
6008 OP(NONE, load_none)
6009 OP(BININT, load_binint)
6010 OP(BININT1, load_binint1)
6011 OP(BININT2, load_binint2)
6012 OP(INT, load_int)
6013 OP(LONG, load_long)
6014 OP_ARG(LONG1, load_counted_long, 1)
6015 OP_ARG(LONG4, load_counted_long, 4)
6016 OP(FLOAT, load_float)
6017 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006018 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6019 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6020 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6021 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6022 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006023 OP(STRING, load_string)
6024 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006025 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6026 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6027 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006028 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6029 OP_ARG(TUPLE1, load_counted_tuple, 1)
6030 OP_ARG(TUPLE2, load_counted_tuple, 2)
6031 OP_ARG(TUPLE3, load_counted_tuple, 3)
6032 OP(TUPLE, load_tuple)
6033 OP(EMPTY_LIST, load_empty_list)
6034 OP(LIST, load_list)
6035 OP(EMPTY_DICT, load_empty_dict)
6036 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006037 OP(EMPTY_SET, load_empty_set)
6038 OP(ADDITEMS, load_additems)
6039 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006040 OP(OBJ, load_obj)
6041 OP(INST, load_inst)
6042 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006043 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006044 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006045 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006046 OP(APPEND, load_append)
6047 OP(APPENDS, load_appends)
6048 OP(BUILD, load_build)
6049 OP(DUP, load_dup)
6050 OP(BINGET, load_binget)
6051 OP(LONG_BINGET, load_long_binget)
6052 OP(GET, load_get)
6053 OP(MARK, load_mark)
6054 OP(BINPUT, load_binput)
6055 OP(LONG_BINPUT, load_long_binput)
6056 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006057 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006058 OP(POP, load_pop)
6059 OP(POP_MARK, load_pop_mark)
6060 OP(SETITEM, load_setitem)
6061 OP(SETITEMS, load_setitems)
6062 OP(PERSID, load_persid)
6063 OP(BINPERSID, load_binpersid)
6064 OP(REDUCE, load_reduce)
6065 OP(PROTO, load_proto)
6066 OP_ARG(EXT1, load_extension, 1)
6067 OP_ARG(EXT2, load_extension, 2)
6068 OP_ARG(EXT4, load_extension, 4)
6069 OP_ARG(NEWTRUE, load_bool, Py_True)
6070 OP_ARG(NEWFALSE, load_bool, Py_False)
6071
6072 case STOP:
6073 break;
6074
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006075 default:
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006076 if (s[0] == '\0')
6077 PyErr_SetNone(PyExc_EOFError);
6078 else
6079 PyErr_Format(UnpicklingError,
6080 "invalid load key, '%c'.", s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006081 return NULL;
6082 }
6083
6084 break; /* and we are done! */
6085 }
6086
6087 /* XXX: It is not clear what this is actually for. */
6088 if ((err = PyErr_Occurred())) {
6089 if (err == PyExc_EOFError) {
6090 PyErr_SetNone(PyExc_EOFError);
6091 }
6092 return NULL;
6093 }
6094
Victor Stinner2ae57e32013-10-31 13:39:23 +01006095 if (_Unpickler_SkipConsumed(self) < 0)
6096 return NULL;
6097
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006098 PDATA_POP(self->stack, value);
6099 return value;
6100}
6101
6102PyDoc_STRVAR(Unpickler_load_doc,
6103"load() -> object. Load a pickle."
6104"\n"
6105"Read a pickled object representation from the open file object given in\n"
6106"the constructor, and return the reconstituted object hierarchy specified\n"
6107"therein.\n");
6108
6109static PyObject *
6110Unpickler_load(UnpicklerObject *self)
6111{
6112 /* Check whether the Unpickler was initialized correctly. This prevents
6113 segfaulting if a subclass overridden __init__ with a function that does
6114 not call Unpickler.__init__(). Here, we simply ensure that self->read
6115 is not NULL. */
6116 if (self->read == NULL) {
Victor Stinner121aab42011-09-29 23:40:53 +02006117 PyErr_Format(UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006118 "Unpickler.__init__() was not called by %s.__init__()",
6119 Py_TYPE(self)->tp_name);
6120 return NULL;
6121 }
6122
6123 return load(self);
6124}
6125
6126/* The name of find_class() is misleading. In newer pickle protocols, this
6127 function is used for loading any global (i.e., functions), not just
6128 classes. The name is kept only for backward compatibility. */
6129
6130PyDoc_STRVAR(Unpickler_find_class_doc,
6131"find_class(module_name, global_name) -> object.\n"
6132"\n"
6133"Return an object from a specified module, importing the module if\n"
6134"necessary. Subclasses may override this method (e.g. to restrict\n"
6135"unpickling of arbitrary classes and functions).\n"
6136"\n"
6137"This method is called whenever a class or a function object is\n"
6138"needed. Both arguments passed are str objects.\n");
6139
6140static PyObject *
6141Unpickler_find_class(UnpicklerObject *self, PyObject *args)
6142{
6143 PyObject *global;
6144 PyObject *modules_dict;
6145 PyObject *module;
6146 PyObject *module_name, *global_name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006147 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006148
6149 if (!PyArg_UnpackTuple(args, "find_class", 2, 2,
6150 &module_name, &global_name))
6151 return NULL;
6152
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006153 /* Try to map the old names used in Python 2.x to the new ones used in
6154 Python 3.x. We do this only with old pickle protocols and when the
6155 user has not disabled the feature. */
6156 if (self->proto < 3 && self->fix_imports) {
6157 PyObject *key;
6158 PyObject *item;
6159
6160 /* Check if the global (i.e., a function or a class) was renamed
6161 or moved to another module. */
6162 key = PyTuple_Pack(2, module_name, global_name);
6163 if (key == NULL)
6164 return NULL;
6165 item = PyDict_GetItemWithError(name_mapping_2to3, key);
6166 Py_DECREF(key);
6167 if (item) {
6168 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6169 PyErr_Format(PyExc_RuntimeError,
6170 "_compat_pickle.NAME_MAPPING values should be "
6171 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6172 return NULL;
6173 }
6174 module_name = PyTuple_GET_ITEM(item, 0);
6175 global_name = PyTuple_GET_ITEM(item, 1);
6176 if (!PyUnicode_Check(module_name) ||
6177 !PyUnicode_Check(global_name)) {
6178 PyErr_Format(PyExc_RuntimeError,
6179 "_compat_pickle.NAME_MAPPING values should be "
6180 "pairs of str, not (%.200s, %.200s)",
6181 Py_TYPE(module_name)->tp_name,
6182 Py_TYPE(global_name)->tp_name);
6183 return NULL;
6184 }
6185 }
6186 else if (PyErr_Occurred()) {
6187 return NULL;
6188 }
6189
6190 /* Check if the module was renamed. */
6191 item = PyDict_GetItemWithError(import_mapping_2to3, module_name);
6192 if (item) {
6193 if (!PyUnicode_Check(item)) {
6194 PyErr_Format(PyExc_RuntimeError,
6195 "_compat_pickle.IMPORT_MAPPING values should be "
6196 "strings, not %.200s", Py_TYPE(item)->tp_name);
6197 return NULL;
6198 }
6199 module_name = item;
6200 }
6201 else if (PyErr_Occurred()) {
6202 return NULL;
6203 }
6204 }
6205
Victor Stinnerbb520202013-11-06 22:40:41 +01006206 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006207 if (modules_dict == NULL) {
6208 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006209 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006210 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006211
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006212 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006213 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006214 if (PyErr_Occurred())
6215 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006216 module = PyImport_Import(module_name);
6217 if (module == NULL)
6218 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006219 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006220 Py_DECREF(module);
6221 }
Victor Stinner121aab42011-09-29 23:40:53 +02006222 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006223 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006224 }
6225 return global;
6226}
6227
6228static struct PyMethodDef Unpickler_methods[] = {
6229 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
6230 Unpickler_load_doc},
6231 {"find_class", (PyCFunction)Unpickler_find_class, METH_VARARGS,
6232 Unpickler_find_class_doc},
6233 {NULL, NULL} /* sentinel */
6234};
6235
6236static void
6237Unpickler_dealloc(UnpicklerObject *self)
6238{
6239 PyObject_GC_UnTrack((PyObject *)self);
6240 Py_XDECREF(self->readline);
6241 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006242 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006243 Py_XDECREF(self->stack);
6244 Py_XDECREF(self->pers_func);
6245 Py_XDECREF(self->arg);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006246 if (self->buffer.buf != NULL) {
6247 PyBuffer_Release(&self->buffer);
6248 self->buffer.buf = NULL;
6249 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006250
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006251 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006252 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006253 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006254 PyMem_Free(self->encoding);
6255 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006256
6257 Py_TYPE(self)->tp_free((PyObject *)self);
6258}
6259
6260static int
6261Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6262{
6263 Py_VISIT(self->readline);
6264 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006265 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006266 Py_VISIT(self->stack);
6267 Py_VISIT(self->pers_func);
6268 Py_VISIT(self->arg);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006269 return 0;
6270}
6271
6272static int
6273Unpickler_clear(UnpicklerObject *self)
6274{
6275 Py_CLEAR(self->readline);
6276 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006277 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006278 Py_CLEAR(self->stack);
6279 Py_CLEAR(self->pers_func);
6280 Py_CLEAR(self->arg);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006281 if (self->buffer.buf != NULL) {
6282 PyBuffer_Release(&self->buffer);
6283 self->buffer.buf = NULL;
6284 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006285
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006286 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006287 PyMem_Free(self->marks);
6288 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006289 PyMem_Free(self->input_line);
6290 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006291 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006292 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006293 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006294 self->errors = NULL;
6295
6296 return 0;
6297}
6298
6299PyDoc_STRVAR(Unpickler_doc,
6300"Unpickler(file, *, encoding='ASCII', errors='strict')"
6301"\n"
6302"This takes a binary file for reading a pickle data stream.\n"
6303"\n"
6304"The protocol version of the pickle is detected automatically, so no\n"
6305"proto argument is needed.\n"
6306"\n"
6307"The file-like object must have two methods, a read() method\n"
6308"that takes an integer argument, and a readline() method that\n"
6309"requires no arguments. Both methods should return bytes.\n"
6310"Thus file-like object can be a binary file object opened for\n"
6311"reading, a BytesIO object, or any other custom object that\n"
6312"meets this interface.\n"
6313"\n"
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006314"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
6315"which are used to control compatiblity support for pickle stream\n"
6316"generated by Python 2.x. If *fix_imports* is True, pickle will try to\n"
6317"map the old Python 2.x names to the new names used in Python 3.x. The\n"
6318"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
6319"instances pickled by Python 2.x; these default to 'ASCII' and\n"
6320"'strict', respectively.\n");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006321
6322static int
6323Unpickler_init(UnpicklerObject *self, PyObject *args, PyObject *kwds)
6324{
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006325 static char *kwlist[] = {"file", "fix_imports", "encoding", "errors", 0};
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006326 PyObject *file;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006327 PyObject *fix_imports = Py_True;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006328 char *encoding = NULL;
6329 char *errors = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006330 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006331
6332 /* XXX: That is an horrible error message. But, I don't know how to do
6333 better... */
6334 if (Py_SIZE(args) != 1) {
6335 PyErr_Format(PyExc_TypeError,
6336 "%s takes exactly one positional argument (%zd given)",
6337 Py_TYPE(self)->tp_name, Py_SIZE(args));
6338 return -1;
6339 }
6340
6341 /* Arguments parsing needs to be done in the __init__() method to allow
6342 subclasses to define their own __init__() method, which may (or may
6343 not) support Unpickler arguments. However, this means we need to be
6344 extra careful in the other Unpickler methods, since a subclass could
6345 forget to call Unpickler.__init__() thus breaking our internal
6346 invariants. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006347 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oss:Unpickler", kwlist,
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006348 &file, &fix_imports, &encoding, &errors))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006349 return -1;
6350
6351 /* In case of multiple __init__() calls, clear previous content. */
6352 if (self->read != NULL)
6353 (void)Unpickler_clear(self);
6354
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006355 if (_Unpickler_SetInputStream(self, file) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006356 return -1;
6357
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006358 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006359 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006360
6361 self->fix_imports = PyObject_IsTrue(fix_imports);
6362 if (self->fix_imports == -1)
6363 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006364
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006365 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006366 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6367 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006368 if (self->pers_func == NULL)
6369 return -1;
6370 }
6371 else {
6372 self->pers_func = NULL;
6373 }
6374
6375 self->stack = (Pdata *)Pdata_New();
6376 if (self->stack == NULL)
6377 return -1;
6378
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006379 self->memo_size = 32;
6380 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006381 if (self->memo == NULL)
6382 return -1;
6383
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006384 self->arg = NULL;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006385 self->proto = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006386 self->framing = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006387
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006388 return 0;
6389}
6390
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006391/* Define a proxy object for the Unpickler's internal memo object. This is to
6392 * avoid breaking code like:
6393 * unpickler.memo.clear()
6394 * and
6395 * unpickler.memo = saved_memo
6396 * Is this a good idea? Not really, but we don't want to break code that uses
6397 * it. Note that we don't implement the entire mapping API here. This is
6398 * intentional, as these should be treated as black-box implementation details.
6399 *
6400 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006401 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006402 */
6403
6404typedef struct {
6405 PyObject_HEAD
6406 UnpicklerObject *unpickler;
6407} UnpicklerMemoProxyObject;
6408
6409PyDoc_STRVAR(ump_clear_doc,
6410"memo.clear() -> None. Remove all items from memo.");
6411
6412static PyObject *
6413ump_clear(UnpicklerMemoProxyObject *self)
6414{
6415 _Unpickler_MemoCleanup(self->unpickler);
6416 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6417 if (self->unpickler->memo == NULL)
6418 return NULL;
6419 Py_RETURN_NONE;
6420}
6421
6422PyDoc_STRVAR(ump_copy_doc,
6423"memo.copy() -> new_memo. Copy the memo to a new object.");
6424
6425static PyObject *
6426ump_copy(UnpicklerMemoProxyObject *self)
6427{
6428 Py_ssize_t i;
6429 PyObject *new_memo = PyDict_New();
6430 if (new_memo == NULL)
6431 return NULL;
6432
6433 for (i = 0; i < self->unpickler->memo_size; i++) {
6434 int status;
6435 PyObject *key, *value;
6436
6437 value = self->unpickler->memo[i];
6438 if (value == NULL)
6439 continue;
6440
6441 key = PyLong_FromSsize_t(i);
6442 if (key == NULL)
6443 goto error;
6444 status = PyDict_SetItem(new_memo, key, value);
6445 Py_DECREF(key);
6446 if (status < 0)
6447 goto error;
6448 }
6449 return new_memo;
6450
6451error:
6452 Py_DECREF(new_memo);
6453 return NULL;
6454}
6455
6456PyDoc_STRVAR(ump_reduce_doc,
6457"memo.__reduce__(). Pickling support.");
6458
6459static PyObject *
6460ump_reduce(UnpicklerMemoProxyObject *self, PyObject *args)
6461{
6462 PyObject *reduce_value;
6463 PyObject *constructor_args;
6464 PyObject *contents = ump_copy(self);
6465 if (contents == NULL)
6466 return NULL;
6467
6468 reduce_value = PyTuple_New(2);
6469 if (reduce_value == NULL) {
6470 Py_DECREF(contents);
6471 return NULL;
6472 }
6473 constructor_args = PyTuple_New(1);
6474 if (constructor_args == NULL) {
6475 Py_DECREF(contents);
6476 Py_DECREF(reduce_value);
6477 return NULL;
6478 }
6479 PyTuple_SET_ITEM(constructor_args, 0, contents);
6480 Py_INCREF((PyObject *)&PyDict_Type);
6481 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6482 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6483 return reduce_value;
6484}
6485
6486static PyMethodDef unpicklerproxy_methods[] = {
6487 {"clear", (PyCFunction)ump_clear, METH_NOARGS, ump_clear_doc},
6488 {"copy", (PyCFunction)ump_copy, METH_NOARGS, ump_copy_doc},
6489 {"__reduce__", (PyCFunction)ump_reduce, METH_VARARGS, ump_reduce_doc},
6490 {NULL, NULL} /* sentinel */
6491};
6492
6493static void
6494UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6495{
6496 PyObject_GC_UnTrack(self);
6497 Py_XDECREF(self->unpickler);
6498 PyObject_GC_Del((PyObject *)self);
6499}
6500
6501static int
6502UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6503 visitproc visit, void *arg)
6504{
6505 Py_VISIT(self->unpickler);
6506 return 0;
6507}
6508
6509static int
6510UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6511{
6512 Py_CLEAR(self->unpickler);
6513 return 0;
6514}
6515
6516static PyTypeObject UnpicklerMemoProxyType = {
6517 PyVarObject_HEAD_INIT(NULL, 0)
6518 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6519 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6520 0,
6521 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6522 0, /* tp_print */
6523 0, /* tp_getattr */
6524 0, /* tp_setattr */
6525 0, /* tp_compare */
6526 0, /* tp_repr */
6527 0, /* tp_as_number */
6528 0, /* tp_as_sequence */
6529 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006530 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006531 0, /* tp_call */
6532 0, /* tp_str */
6533 PyObject_GenericGetAttr, /* tp_getattro */
6534 PyObject_GenericSetAttr, /* tp_setattro */
6535 0, /* tp_as_buffer */
6536 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6537 0, /* tp_doc */
6538 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6539 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6540 0, /* tp_richcompare */
6541 0, /* tp_weaklistoffset */
6542 0, /* tp_iter */
6543 0, /* tp_iternext */
6544 unpicklerproxy_methods, /* tp_methods */
6545};
6546
6547static PyObject *
6548UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6549{
6550 UnpicklerMemoProxyObject *self;
6551
6552 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6553 &UnpicklerMemoProxyType);
6554 if (self == NULL)
6555 return NULL;
6556 Py_INCREF(unpickler);
6557 self->unpickler = unpickler;
6558 PyObject_GC_Track(self);
6559 return (PyObject *)self;
6560}
6561
6562/*****************************************************************************/
6563
6564
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006565static PyObject *
6566Unpickler_get_memo(UnpicklerObject *self)
6567{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006568 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006569}
6570
6571static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006572Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006573{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006574 PyObject **new_memo;
6575 Py_ssize_t new_memo_size = 0;
6576 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006577
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006578 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006579 PyErr_SetString(PyExc_TypeError,
6580 "attribute deletion is not supported");
6581 return -1;
6582 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006583
6584 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6585 UnpicklerObject *unpickler =
6586 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6587
6588 new_memo_size = unpickler->memo_size;
6589 new_memo = _Unpickler_NewMemo(new_memo_size);
6590 if (new_memo == NULL)
6591 return -1;
6592
6593 for (i = 0; i < new_memo_size; i++) {
6594 Py_XINCREF(unpickler->memo[i]);
6595 new_memo[i] = unpickler->memo[i];
6596 }
6597 }
6598 else if (PyDict_Check(obj)) {
6599 Py_ssize_t i = 0;
6600 PyObject *key, *value;
6601
6602 new_memo_size = PyDict_Size(obj);
6603 new_memo = _Unpickler_NewMemo(new_memo_size);
6604 if (new_memo == NULL)
6605 return -1;
6606
6607 while (PyDict_Next(obj, &i, &key, &value)) {
6608 Py_ssize_t idx;
6609 if (!PyLong_Check(key)) {
6610 PyErr_SetString(PyExc_TypeError,
6611 "memo key must be integers");
6612 goto error;
6613 }
6614 idx = PyLong_AsSsize_t(key);
6615 if (idx == -1 && PyErr_Occurred())
6616 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006617 if (idx < 0) {
6618 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006619 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006620 goto error;
6621 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006622 if (_Unpickler_MemoPut(self, idx, value) < 0)
6623 goto error;
6624 }
6625 }
6626 else {
6627 PyErr_Format(PyExc_TypeError,
6628 "'memo' attribute must be an UnpicklerMemoProxy object"
6629 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006630 return -1;
6631 }
6632
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006633 _Unpickler_MemoCleanup(self);
6634 self->memo_size = new_memo_size;
6635 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006636
6637 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006638
6639 error:
6640 if (new_memo_size) {
6641 i = new_memo_size;
6642 while (--i >= 0) {
6643 Py_XDECREF(new_memo[i]);
6644 }
6645 PyMem_FREE(new_memo);
6646 }
6647 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006648}
6649
6650static PyObject *
6651Unpickler_get_persload(UnpicklerObject *self)
6652{
6653 if (self->pers_func == NULL)
6654 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6655 else
6656 Py_INCREF(self->pers_func);
6657 return self->pers_func;
6658}
6659
6660static int
6661Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6662{
6663 PyObject *tmp;
6664
6665 if (value == NULL) {
6666 PyErr_SetString(PyExc_TypeError,
6667 "attribute deletion is not supported");
6668 return -1;
6669 }
6670 if (!PyCallable_Check(value)) {
6671 PyErr_SetString(PyExc_TypeError,
6672 "persistent_load must be a callable taking "
6673 "one argument");
6674 return -1;
6675 }
6676
6677 tmp = self->pers_func;
6678 Py_INCREF(value);
6679 self->pers_func = value;
6680 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6681
6682 return 0;
6683}
6684
6685static PyGetSetDef Unpickler_getsets[] = {
6686 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6687 {"persistent_load", (getter)Unpickler_get_persload,
6688 (setter)Unpickler_set_persload},
6689 {NULL}
6690};
6691
6692static PyTypeObject Unpickler_Type = {
6693 PyVarObject_HEAD_INIT(NULL, 0)
6694 "_pickle.Unpickler", /*tp_name*/
6695 sizeof(UnpicklerObject), /*tp_basicsize*/
6696 0, /*tp_itemsize*/
6697 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6698 0, /*tp_print*/
6699 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006700 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006701 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006702 0, /*tp_repr*/
6703 0, /*tp_as_number*/
6704 0, /*tp_as_sequence*/
6705 0, /*tp_as_mapping*/
6706 0, /*tp_hash*/
6707 0, /*tp_call*/
6708 0, /*tp_str*/
6709 0, /*tp_getattro*/
6710 0, /*tp_setattro*/
6711 0, /*tp_as_buffer*/
6712 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6713 Unpickler_doc, /*tp_doc*/
6714 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6715 (inquiry)Unpickler_clear, /*tp_clear*/
6716 0, /*tp_richcompare*/
6717 0, /*tp_weaklistoffset*/
6718 0, /*tp_iter*/
6719 0, /*tp_iternext*/
6720 Unpickler_methods, /*tp_methods*/
6721 0, /*tp_members*/
6722 Unpickler_getsets, /*tp_getset*/
6723 0, /*tp_base*/
6724 0, /*tp_dict*/
6725 0, /*tp_descr_get*/
6726 0, /*tp_descr_set*/
6727 0, /*tp_dictoffset*/
6728 (initproc)Unpickler_init, /*tp_init*/
6729 PyType_GenericAlloc, /*tp_alloc*/
6730 PyType_GenericNew, /*tp_new*/
6731 PyObject_GC_Del, /*tp_free*/
6732 0, /*tp_is_gc*/
6733};
6734
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006735PyDoc_STRVAR(pickle_dump_doc,
6736"dump(obj, file, protocol=None, *, fix_imports=True) -> None\n"
6737"\n"
6738"Write a pickled representation of obj to the open file object file. This\n"
6739"is equivalent to ``Pickler(file, protocol).dump(obj)``, but may be more\n"
6740"efficient.\n"
6741"\n"
6742"The optional protocol argument tells the pickler to use the given protocol;\n"
6743"supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n"
6744"backward-incompatible protocol designed for Python 3.0.\n"
6745"\n"
6746"Specifying a negative protocol version selects the highest protocol version\n"
6747"supported. The higher the protocol used, the more recent the version of\n"
6748"Python needed to read the pickle produced.\n"
6749"\n"
6750"The file argument must have a write() method that accepts a single bytes\n"
6751"argument. It can thus be a file object opened for binary writing, a\n"
6752"io.BytesIO instance, or any other custom object that meets this interface.\n"
6753"\n"
6754"If fix_imports is True and protocol is less than 3, pickle will try to\n"
6755"map the new Python 3.x names to the old module names used in Python 2.x,\n"
6756"so that the pickle data stream is readable with Python 2.x.\n");
6757
6758static PyObject *
6759pickle_dump(PyObject *self, PyObject *args, PyObject *kwds)
6760{
6761 static char *kwlist[] = {"obj", "file", "protocol", "fix_imports", 0};
6762 PyObject *obj;
6763 PyObject *file;
6764 PyObject *proto = NULL;
6765 PyObject *fix_imports = Py_True;
6766 PicklerObject *pickler;
6767
6768 /* fix_imports is a keyword-only argument. */
6769 if (Py_SIZE(args) > 3) {
6770 PyErr_Format(PyExc_TypeError,
6771 "pickle.dump() takes at most 3 positional "
6772 "argument (%zd given)", Py_SIZE(args));
6773 return NULL;
6774 }
6775
6776 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO:dump", kwlist,
6777 &obj, &file, &proto, &fix_imports))
6778 return NULL;
6779
6780 pickler = _Pickler_New();
6781 if (pickler == NULL)
6782 return NULL;
6783
6784 if (_Pickler_SetProtocol(pickler, proto, fix_imports) < 0)
6785 goto error;
6786
6787 if (_Pickler_SetOutputStream(pickler, file) < 0)
6788 goto error;
6789
6790 if (dump(pickler, obj) < 0)
6791 goto error;
6792
6793 if (_Pickler_FlushToFile(pickler) < 0)
6794 goto error;
6795
6796 Py_DECREF(pickler);
6797 Py_RETURN_NONE;
6798
6799 error:
6800 Py_XDECREF(pickler);
6801 return NULL;
6802}
6803
6804PyDoc_STRVAR(pickle_dumps_doc,
6805"dumps(obj, protocol=None, *, fix_imports=True) -> bytes\n"
6806"\n"
6807"Return the pickled representation of the object as a bytes\n"
6808"object, instead of writing it to a file.\n"
6809"\n"
6810"The optional protocol argument tells the pickler to use the given protocol;\n"
6811"supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n"
6812"backward-incompatible protocol designed for Python 3.0.\n"
6813"\n"
6814"Specifying a negative protocol version selects the highest protocol version\n"
6815"supported. The higher the protocol used, the more recent the version of\n"
6816"Python needed to read the pickle produced.\n"
6817"\n"
6818"If fix_imports is True and *protocol* is less than 3, pickle will try to\n"
6819"map the new Python 3.x names to the old module names used in Python 2.x,\n"
6820"so that the pickle data stream is readable with Python 2.x.\n");
6821
6822static PyObject *
6823pickle_dumps(PyObject *self, PyObject *args, PyObject *kwds)
6824{
6825 static char *kwlist[] = {"obj", "protocol", "fix_imports", 0};
6826 PyObject *obj;
6827 PyObject *proto = NULL;
6828 PyObject *result;
6829 PyObject *fix_imports = Py_True;
6830 PicklerObject *pickler;
6831
6832 /* fix_imports is a keyword-only argument. */
6833 if (Py_SIZE(args) > 2) {
6834 PyErr_Format(PyExc_TypeError,
6835 "pickle.dumps() takes at most 2 positional "
6836 "argument (%zd given)", Py_SIZE(args));
6837 return NULL;
6838 }
6839
6840 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO:dumps", kwlist,
6841 &obj, &proto, &fix_imports))
6842 return NULL;
6843
6844 pickler = _Pickler_New();
6845 if (pickler == NULL)
6846 return NULL;
6847
6848 if (_Pickler_SetProtocol(pickler, proto, fix_imports) < 0)
6849 goto error;
6850
6851 if (dump(pickler, obj) < 0)
6852 goto error;
6853
6854 result = _Pickler_GetString(pickler);
6855 Py_DECREF(pickler);
6856 return result;
6857
6858 error:
6859 Py_XDECREF(pickler);
6860 return NULL;
6861}
6862
6863PyDoc_STRVAR(pickle_load_doc,
6864"load(file, *, fix_imports=True, encoding='ASCII', errors='strict') -> object\n"
6865"\n"
6866"Read a pickled object representation from the open file object file and\n"
6867"return the reconstituted object hierarchy specified therein. This is\n"
6868"equivalent to ``Unpickler(file).load()``, but may be more efficient.\n"
6869"\n"
6870"The protocol version of the pickle is detected automatically, so no protocol\n"
6871"argument is needed. Bytes past the pickled object's representation are\n"
6872"ignored.\n"
6873"\n"
6874"The argument file must have two methods, a read() method that takes an\n"
6875"integer argument, and a readline() method that requires no arguments. Both\n"
6876"methods should return bytes. Thus *file* can be a binary file object opened\n"
6877"for reading, a BytesIO object, or any other custom object that meets this\n"
6878"interface.\n"
6879"\n"
6880"Optional keyword arguments are fix_imports, encoding and errors,\n"
6881"which are used to control compatiblity support for pickle stream generated\n"
6882"by Python 2.x. If fix_imports is True, pickle will try to map the old\n"
6883"Python 2.x names to the new names used in Python 3.x. The encoding and\n"
6884"errors tell pickle how to decode 8-bit string instances pickled by Python\n"
6885"2.x; these default to 'ASCII' and 'strict', respectively.\n");
6886
6887static PyObject *
6888pickle_load(PyObject *self, PyObject *args, PyObject *kwds)
6889{
6890 static char *kwlist[] = {"file", "fix_imports", "encoding", "errors", 0};
6891 PyObject *file;
6892 PyObject *fix_imports = Py_True;
6893 PyObject *result;
6894 char *encoding = NULL;
6895 char *errors = NULL;
6896 UnpicklerObject *unpickler;
6897
6898 /* fix_imports, encoding and errors are a keyword-only argument. */
6899 if (Py_SIZE(args) != 1) {
6900 PyErr_Format(PyExc_TypeError,
6901 "pickle.load() takes exactly one positional "
6902 "argument (%zd given)", Py_SIZE(args));
6903 return NULL;
6904 }
6905
6906 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oss:load", kwlist,
6907 &file, &fix_imports, &encoding, &errors))
6908 return NULL;
6909
6910 unpickler = _Unpickler_New();
6911 if (unpickler == NULL)
6912 return NULL;
6913
6914 if (_Unpickler_SetInputStream(unpickler, file) < 0)
6915 goto error;
6916
6917 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
6918 goto error;
6919
6920 unpickler->fix_imports = PyObject_IsTrue(fix_imports);
6921 if (unpickler->fix_imports == -1)
6922 goto error;
6923
6924 result = load(unpickler);
6925 Py_DECREF(unpickler);
6926 return result;
6927
6928 error:
6929 Py_XDECREF(unpickler);
6930 return NULL;
6931}
6932
6933PyDoc_STRVAR(pickle_loads_doc,
6934"loads(input, *, fix_imports=True, encoding='ASCII', errors='strict') -> object\n"
6935"\n"
6936"Read a pickled object hierarchy from a bytes object and return the\n"
6937"reconstituted object hierarchy specified therein\n"
6938"\n"
6939"The protocol version of the pickle is detected automatically, so no protocol\n"
6940"argument is needed. Bytes past the pickled object's representation are\n"
6941"ignored.\n"
6942"\n"
6943"Optional keyword arguments are fix_imports, encoding and errors, which\n"
6944"are used to control compatiblity support for pickle stream generated\n"
6945"by Python 2.x. If fix_imports is True, pickle will try to map the old\n"
6946"Python 2.x names to the new names used in Python 3.x. The encoding and\n"
6947"errors tell pickle how to decode 8-bit string instances pickled by Python\n"
6948"2.x; these default to 'ASCII' and 'strict', respectively.\n");
6949
6950static PyObject *
6951pickle_loads(PyObject *self, PyObject *args, PyObject *kwds)
6952{
6953 static char *kwlist[] = {"input", "fix_imports", "encoding", "errors", 0};
6954 PyObject *input;
6955 PyObject *fix_imports = Py_True;
6956 PyObject *result;
6957 char *encoding = NULL;
6958 char *errors = NULL;
6959 UnpicklerObject *unpickler;
6960
6961 /* fix_imports, encoding and errors are a keyword-only argument. */
6962 if (Py_SIZE(args) != 1) {
6963 PyErr_Format(PyExc_TypeError,
6964 "pickle.loads() takes exactly one positional "
6965 "argument (%zd given)", Py_SIZE(args));
6966 return NULL;
6967 }
6968
6969 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oss:loads", kwlist,
6970 &input, &fix_imports, &encoding, &errors))
6971 return NULL;
6972
6973 unpickler = _Unpickler_New();
6974 if (unpickler == NULL)
6975 return NULL;
6976
6977 if (_Unpickler_SetStringInput(unpickler, input) < 0)
6978 goto error;
6979
6980 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
6981 goto error;
6982
6983 unpickler->fix_imports = PyObject_IsTrue(fix_imports);
6984 if (unpickler->fix_imports == -1)
6985 goto error;
6986
6987 result = load(unpickler);
6988 Py_DECREF(unpickler);
6989 return result;
6990
6991 error:
6992 Py_XDECREF(unpickler);
6993 return NULL;
6994}
6995
6996
6997static struct PyMethodDef pickle_methods[] = {
6998 {"dump", (PyCFunction)pickle_dump, METH_VARARGS|METH_KEYWORDS,
6999 pickle_dump_doc},
7000 {"dumps", (PyCFunction)pickle_dumps, METH_VARARGS|METH_KEYWORDS,
7001 pickle_dumps_doc},
7002 {"load", (PyCFunction)pickle_load, METH_VARARGS|METH_KEYWORDS,
7003 pickle_load_doc},
7004 {"loads", (PyCFunction)pickle_loads, METH_VARARGS|METH_KEYWORDS,
7005 pickle_loads_doc},
7006 {NULL, NULL} /* sentinel */
7007};
7008
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007009static int
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007010initmodule(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007011{
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007012 PyObject *copyreg = NULL;
7013 PyObject *compat_pickle = NULL;
7014
7015 /* XXX: We should ensure that the types of the dictionaries imported are
7016 exactly PyDict objects. Otherwise, it is possible to crash the pickle
7017 since we use the PyDict API directly to access these dictionaries. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007018
7019 copyreg = PyImport_ImportModule("copyreg");
7020 if (!copyreg)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007021 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007022 dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
7023 if (!dispatch_table)
7024 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007025 extension_registry = \
7026 PyObject_GetAttrString(copyreg, "_extension_registry");
7027 if (!extension_registry)
7028 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007029 inverted_registry = PyObject_GetAttrString(copyreg, "_inverted_registry");
7030 if (!inverted_registry)
7031 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007032 extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
7033 if (!extension_cache)
7034 goto error;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007035 Py_CLEAR(copyreg);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007036
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007037 /* Load the 2.x -> 3.x stdlib module mapping tables */
7038 compat_pickle = PyImport_ImportModule("_compat_pickle");
7039 if (!compat_pickle)
7040 goto error;
7041 name_mapping_2to3 = PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
7042 if (!name_mapping_2to3)
7043 goto error;
7044 if (!PyDict_CheckExact(name_mapping_2to3)) {
7045 PyErr_Format(PyExc_RuntimeError,
7046 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
7047 Py_TYPE(name_mapping_2to3)->tp_name);
7048 goto error;
7049 }
7050 import_mapping_2to3 = PyObject_GetAttrString(compat_pickle,
7051 "IMPORT_MAPPING");
7052 if (!import_mapping_2to3)
7053 goto error;
7054 if (!PyDict_CheckExact(import_mapping_2to3)) {
7055 PyErr_Format(PyExc_RuntimeError,
7056 "_compat_pickle.IMPORT_MAPPING should be a dict, "
7057 "not %.200s", Py_TYPE(import_mapping_2to3)->tp_name);
7058 goto error;
7059 }
7060 /* ... and the 3.x -> 2.x mapping tables */
7061 name_mapping_3to2 = PyObject_GetAttrString(compat_pickle,
7062 "REVERSE_NAME_MAPPING");
7063 if (!name_mapping_3to2)
7064 goto error;
7065 if (!PyDict_CheckExact(name_mapping_3to2)) {
7066 PyErr_Format(PyExc_RuntimeError,
Ezio Melotti13925002011-03-16 11:05:33 +02007067 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007068 "not %.200s", Py_TYPE(name_mapping_3to2)->tp_name);
7069 goto error;
7070 }
7071 import_mapping_3to2 = PyObject_GetAttrString(compat_pickle,
7072 "REVERSE_IMPORT_MAPPING");
7073 if (!import_mapping_3to2)
7074 goto error;
7075 if (!PyDict_CheckExact(import_mapping_3to2)) {
7076 PyErr_Format(PyExc_RuntimeError,
7077 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
7078 "not %.200s", Py_TYPE(import_mapping_3to2)->tp_name);
7079 goto error;
7080 }
7081 Py_CLEAR(compat_pickle);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007082
7083 empty_tuple = PyTuple_New(0);
7084 if (empty_tuple == NULL)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007085 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007086 two_tuple = PyTuple_New(2);
7087 if (two_tuple == NULL)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007088 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007089 /* We use this temp container with no regard to refcounts, or to
7090 * keeping containees alive. Exempt from GC, because we don't
7091 * want anything looking at two_tuple() by magic.
7092 */
7093 PyObject_GC_UnTrack(two_tuple);
7094
7095 return 0;
7096
7097 error:
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007098 Py_CLEAR(copyreg);
7099 Py_CLEAR(dispatch_table);
7100 Py_CLEAR(extension_registry);
7101 Py_CLEAR(inverted_registry);
7102 Py_CLEAR(extension_cache);
7103 Py_CLEAR(compat_pickle);
7104 Py_CLEAR(name_mapping_2to3);
7105 Py_CLEAR(import_mapping_2to3);
7106 Py_CLEAR(name_mapping_3to2);
7107 Py_CLEAR(import_mapping_3to2);
7108 Py_CLEAR(empty_tuple);
7109 Py_CLEAR(two_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007110 return -1;
7111}
7112
7113static struct PyModuleDef _picklemodule = {
7114 PyModuleDef_HEAD_INIT,
7115 "_pickle",
7116 pickle_module_doc,
7117 -1,
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007118 pickle_methods,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007119 NULL,
7120 NULL,
7121 NULL,
7122 NULL
7123};
7124
7125PyMODINIT_FUNC
7126PyInit__pickle(void)
7127{
7128 PyObject *m;
7129
7130 if (PyType_Ready(&Unpickler_Type) < 0)
7131 return NULL;
7132 if (PyType_Ready(&Pickler_Type) < 0)
7133 return NULL;
7134 if (PyType_Ready(&Pdata_Type) < 0)
7135 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007136 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7137 return NULL;
7138 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7139 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007140
7141 /* Create the module and add the functions. */
7142 m = PyModule_Create(&_picklemodule);
7143 if (m == NULL)
7144 return NULL;
7145
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007146 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007147 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7148 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007149 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007150 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7151 return NULL;
7152
7153 /* Initialize the exceptions. */
7154 PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7155 if (PickleError == NULL)
7156 return NULL;
7157 PicklingError = \
7158 PyErr_NewException("_pickle.PicklingError", PickleError, NULL);
7159 if (PicklingError == NULL)
7160 return NULL;
7161 UnpicklingError = \
7162 PyErr_NewException("_pickle.UnpicklingError", PickleError, NULL);
7163 if (UnpicklingError == NULL)
7164 return NULL;
7165
7166 if (PyModule_AddObject(m, "PickleError", PickleError) < 0)
7167 return NULL;
7168 if (PyModule_AddObject(m, "PicklingError", PicklingError) < 0)
7169 return NULL;
7170 if (PyModule_AddObject(m, "UnpicklingError", UnpicklingError) < 0)
7171 return NULL;
7172
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00007173 if (initmodule() < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007174 return NULL;
7175
7176 return m;
7177}