blob: b01a8b275c70af960004ef3c840ad38beaaaf5f5 [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 {
9 HIGHEST_PROTOCOL = 3,
10 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',
Victor Stinner132ef6c2010-11-09 09:39:41 +000074 SHORT_BINBYTES = 'C'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000075};
76
77/* These aren't opcodes -- they're ways to pickle bools before protocol 2
78 * so that unpicklers written before bools were introduced unpickle them
79 * as ints, but unpicklers after can recognize that bools were intended.
80 * Note that protocol 2 added direct ways to pickle bools.
81 */
82#undef TRUE
83#define TRUE "I01\n"
84#undef FALSE
85#define FALSE "I00\n"
86
87enum {
88 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
89 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
90 break if this gets out of synch with pickle.py, but it's unclear that would
91 help anything either. */
92 BATCHSIZE = 1000,
93
94 /* Nesting limit until Pickler, when running in "fast mode", starts
95 checking for self-referential data-structures. */
96 FAST_NESTING_LIMIT = 50,
97
Antoine Pitrouea99c5c2010-09-09 18:33:21 +000098 /* Initial size of the write buffer of Pickler. */
99 WRITE_BUF_SIZE = 4096,
100
101 /* Maximum size of the write buffer of Pickler when pickling to a
102 stream. This is ignored for in-memory pickling. */
103 MAX_WRITE_BUF_SIZE = 64 * 1024,
Antoine Pitrou04248a82010-10-12 20:51:21 +0000104
105 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Victor Stinner132ef6c2010-11-09 09:39:41 +0000106 PREFETCH = 8192 * 16
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000107};
108
109/* Exception classes for pickle. These should override the ones defined in
110 pickle.py, when the C-optimized Pickler and Unpickler are used. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000111static PyObject *PickleError = NULL;
112static PyObject *PicklingError = NULL;
113static PyObject *UnpicklingError = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000114
115/* copyreg.dispatch_table, {type_object: pickling_function} */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000116static PyObject *dispatch_table = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000117/* For EXT[124] opcodes. */
118/* copyreg._extension_registry, {(module_name, function_name): code} */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000119static PyObject *extension_registry = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000120/* copyreg._inverted_registry, {code: (module_name, function_name)} */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000121static PyObject *inverted_registry = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000122/* copyreg._extension_cache, {code: object} */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000123static PyObject *extension_cache = NULL;
124
125/* _compat_pickle.NAME_MAPPING, {(oldmodule, oldname): (newmodule, newname)} */
126static PyObject *name_mapping_2to3 = NULL;
127/* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
128static PyObject *import_mapping_2to3 = NULL;
129/* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
130static PyObject *name_mapping_3to2 = NULL;
131static PyObject *import_mapping_3to2 = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000132
133/* XXX: Are these really nescessary? */
134/* As the name says, an empty tuple. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000135static PyObject *empty_tuple = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000136/* For looking up name pairs in copyreg._extension_registry. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000137static PyObject *two_tuple = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000138
139static int
140stack_underflow(void)
141{
142 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
143 return -1;
144}
145
146/* Internal data type used as the unpickling stack. */
147typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000148 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000149 PyObject **data;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000150 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000151} Pdata;
152
153static void
154Pdata_dealloc(Pdata *self)
155{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000156 int i = Py_SIZE(self);
157 while (--i >= 0) {
158 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000159 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000160 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000161 PyObject_Del(self);
162}
163
164static PyTypeObject Pdata_Type = {
165 PyVarObject_HEAD_INIT(NULL, 0)
166 "_pickle.Pdata", /*tp_name*/
167 sizeof(Pdata), /*tp_basicsize*/
168 0, /*tp_itemsize*/
169 (destructor)Pdata_dealloc, /*tp_dealloc*/
170};
171
172static PyObject *
173Pdata_New(void)
174{
175 Pdata *self;
176
177 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
178 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000179 Py_SIZE(self) = 0;
180 self->allocated = 8;
181 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000182 if (self->data)
183 return (PyObject *)self;
184 Py_DECREF(self);
185 return PyErr_NoMemory();
186}
187
188
189/* Retain only the initial clearto items. If clearto >= the current
190 * number of items, this is a (non-erroneous) NOP.
191 */
192static int
193Pdata_clear(Pdata *self, int clearto)
194{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000195 int i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000196
197 if (clearto < 0)
198 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000199 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000200 return 0;
201
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000202 while (--i >= clearto) {
203 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000204 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000205 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000206 return 0;
207}
208
209static int
210Pdata_grow(Pdata *self)
211{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000212 PyObject **data = self->data;
213 Py_ssize_t allocated = self->allocated;
214 Py_ssize_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000215
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000216 new_allocated = (allocated >> 3) + 6;
217 /* check for integer overflow */
218 if (new_allocated > PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000219 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000220 new_allocated += allocated;
221 if (new_allocated > (PY_SSIZE_T_MAX / sizeof(PyObject *)))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000222 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000223 data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *));
224 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000225 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000226
227 self->data = data;
228 self->allocated = new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000229 return 0;
230
231 nomemory:
232 PyErr_NoMemory();
233 return -1;
234}
235
236/* D is a Pdata*. Pop the topmost element and store it into V, which
237 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
238 * is raised and V is set to NULL.
239 */
240static PyObject *
241Pdata_pop(Pdata *self)
242{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000243 if (Py_SIZE(self) == 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000244 PyErr_SetString(UnpicklingError, "bad pickle data");
245 return NULL;
246 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000247 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000248}
249#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
250
251static int
252Pdata_push(Pdata *self, PyObject *obj)
253{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000254 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000255 return -1;
256 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000257 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000258 return 0;
259}
260
261/* Push an object on stack, transferring its ownership to the stack. */
262#define PDATA_PUSH(D, O, ER) do { \
263 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
264
265/* Push an object on stack, adding a new reference to the object. */
266#define PDATA_APPEND(D, O, ER) do { \
267 Py_INCREF((O)); \
268 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
269
270static PyObject *
271Pdata_poptuple(Pdata *self, Py_ssize_t start)
272{
273 PyObject *tuple;
274 Py_ssize_t len, i, j;
275
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000276 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000277 tuple = PyTuple_New(len);
278 if (tuple == NULL)
279 return NULL;
280 for (i = start, j = 0; j < len; i++, j++)
281 PyTuple_SET_ITEM(tuple, j, self->data[i]);
282
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000283 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000284 return tuple;
285}
286
287static PyObject *
288Pdata_poplist(Pdata *self, Py_ssize_t start)
289{
290 PyObject *list;
291 Py_ssize_t len, i, j;
292
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000293 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000294 list = PyList_New(len);
295 if (list == NULL)
296 return NULL;
297 for (i = start, j = 0; j < len; i++, j++)
298 PyList_SET_ITEM(list, j, self->data[i]);
299
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000300 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000301 return list;
302}
303
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000304typedef struct {
305 PyObject *me_key;
306 long me_value;
307} PyMemoEntry;
308
309typedef struct {
310 Py_ssize_t mt_mask;
311 Py_ssize_t mt_used;
312 Py_ssize_t mt_allocated;
313 PyMemoEntry *mt_table;
314} PyMemoTable;
315
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000316typedef struct PicklerObject {
317 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000318 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000319 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000320 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000321 PyObject *pers_func; /* persistent_id() method, can be NULL */
322 PyObject *arg;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000323
324 PyObject *write; /* write() method of the output stream. */
325 PyObject *output_buffer; /* Write into a local bytearray buffer before
326 flushing to the stream. */
327 Py_ssize_t output_len; /* Length of output_buffer. */
328 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000329 int proto; /* Pickle protocol number, >= 0 */
330 int bin; /* Boolean, true if proto > 0 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000331 int buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000332 int fast; /* Enable fast mode if set to a true value.
333 The fast mode disable the usage of memo,
334 therefore speeding the pickling process by
335 not generating superfluous PUT opcodes. It
336 should not be used if with self-referential
337 objects. */
338 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000339 int fix_imports; /* Indicate whether Pickler should fix
340 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000341 PyObject *fast_memo;
342} PicklerObject;
343
344typedef struct UnpicklerObject {
345 PyObject_HEAD
346 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000347
348 /* The unpickler memo is just an array of PyObject *s. Using a dict
349 is unnecessary, since the keys are contiguous ints. */
350 PyObject **memo;
351 Py_ssize_t memo_size;
352
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000353 PyObject *arg;
354 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000355
356 Py_buffer buffer;
357 char *input_buffer;
358 char *input_line;
359 Py_ssize_t input_len;
360 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000361 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000362 PyObject *read; /* read() method of the input stream. */
363 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000364 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000365
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000366 char *encoding; /* Name of the encoding to be used for
367 decoding strings pickled using Python
368 2.x. The default value is "ASCII" */
369 char *errors; /* Name of errors handling scheme to used when
370 decoding strings. The default value is
371 "strict". */
372 int *marks; /* Mark stack, used for unpickling container
373 objects. */
374 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
375 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000376 int proto; /* Protocol of the pickle loaded. */
377 int fix_imports; /* Indicate whether Unpickler should fix
378 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000379} UnpicklerObject;
380
381/* Forward declarations */
382static int save(PicklerObject *, PyObject *, int);
383static int save_reduce(PicklerObject *, PyObject *, PyObject *);
384static PyTypeObject Pickler_Type;
385static PyTypeObject Unpickler_Type;
386
387
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000388/*************************************************************************
389 A custom hashtable mapping void* to longs. This is used by the pickler for
390 memoization. Using a custom hashtable rather than PyDict allows us to skip
391 a bunch of unnecessary object creation. This makes a huge performance
392 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000393
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000394#define MT_MINSIZE 8
395#define PERTURB_SHIFT 5
396
397
398static PyMemoTable *
399PyMemoTable_New(void)
400{
401 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
402 if (memo == NULL) {
403 PyErr_NoMemory();
404 return NULL;
405 }
406
407 memo->mt_used = 0;
408 memo->mt_allocated = MT_MINSIZE;
409 memo->mt_mask = MT_MINSIZE - 1;
410 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
411 if (memo->mt_table == NULL) {
412 PyMem_FREE(memo);
413 PyErr_NoMemory();
414 return NULL;
415 }
416 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
417
418 return memo;
419}
420
421static PyMemoTable *
422PyMemoTable_Copy(PyMemoTable *self)
423{
424 Py_ssize_t i;
425 PyMemoTable *new = PyMemoTable_New();
426 if (new == NULL)
427 return NULL;
428
429 new->mt_used = self->mt_used;
430 new->mt_allocated = self->mt_allocated;
431 new->mt_mask = self->mt_mask;
432 /* The table we get from _New() is probably smaller than we wanted.
433 Free it and allocate one that's the right size. */
434 PyMem_FREE(new->mt_table);
435 new->mt_table = PyMem_MALLOC(self->mt_allocated * sizeof(PyMemoEntry));
436 if (new->mt_table == NULL) {
437 PyMem_FREE(new);
438 return NULL;
439 }
440 for (i = 0; i < self->mt_allocated; i++) {
441 Py_XINCREF(self->mt_table[i].me_key);
442 }
443 memcpy(new->mt_table, self->mt_table,
444 sizeof(PyMemoEntry) * self->mt_allocated);
445
446 return new;
447}
448
449static Py_ssize_t
450PyMemoTable_Size(PyMemoTable *self)
451{
452 return self->mt_used;
453}
454
455static int
456PyMemoTable_Clear(PyMemoTable *self)
457{
458 Py_ssize_t i = self->mt_allocated;
459
460 while (--i >= 0) {
461 Py_XDECREF(self->mt_table[i].me_key);
462 }
463 self->mt_used = 0;
464 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
465 return 0;
466}
467
468static void
469PyMemoTable_Del(PyMemoTable *self)
470{
471 if (self == NULL)
472 return;
473 PyMemoTable_Clear(self);
474
475 PyMem_FREE(self->mt_table);
476 PyMem_FREE(self);
477}
478
479/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
480 can be considerably simpler than dictobject.c's lookdict(). */
481static PyMemoEntry *
482_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
483{
484 size_t i;
485 size_t perturb;
486 size_t mask = (size_t)self->mt_mask;
487 PyMemoEntry *table = self->mt_table;
488 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000489 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000490
491 i = hash & mask;
492 entry = &table[i];
493 if (entry->me_key == NULL || entry->me_key == key)
494 return entry;
495
496 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
497 i = (i << 2) + i + perturb + 1;
498 entry = &table[i & mask];
499 if (entry->me_key == NULL || entry->me_key == key)
500 return entry;
501 }
502 assert(0); /* Never reached */
503 return NULL;
504}
505
506/* Returns -1 on failure, 0 on success. */
507static int
508_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
509{
510 PyMemoEntry *oldtable = NULL;
511 PyMemoEntry *oldentry, *newentry;
512 Py_ssize_t new_size = MT_MINSIZE;
513 Py_ssize_t to_process;
514
515 assert(min_size > 0);
516
517 /* Find the smallest valid table size >= min_size. */
518 while (new_size < min_size && new_size > 0)
519 new_size <<= 1;
520 if (new_size <= 0) {
521 PyErr_NoMemory();
522 return -1;
523 }
524 /* new_size needs to be a power of two. */
525 assert((new_size & (new_size - 1)) == 0);
526
527 /* Allocate new table. */
528 oldtable = self->mt_table;
529 self->mt_table = PyMem_MALLOC(new_size * sizeof(PyMemoEntry));
530 if (self->mt_table == NULL) {
531 PyMem_FREE(oldtable);
532 PyErr_NoMemory();
533 return -1;
534 }
535 self->mt_allocated = new_size;
536 self->mt_mask = new_size - 1;
537 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
538
539 /* Copy entries from the old table. */
540 to_process = self->mt_used;
541 for (oldentry = oldtable; to_process > 0; oldentry++) {
542 if (oldentry->me_key != NULL) {
543 to_process--;
544 /* newentry is a pointer to a chunk of the new
545 mt_table, so we're setting the key:value pair
546 in-place. */
547 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
548 newentry->me_key = oldentry->me_key;
549 newentry->me_value = oldentry->me_value;
550 }
551 }
552
553 /* Deallocate the old table. */
554 PyMem_FREE(oldtable);
555 return 0;
556}
557
558/* Returns NULL on failure, a pointer to the value otherwise. */
559static long *
560PyMemoTable_Get(PyMemoTable *self, PyObject *key)
561{
562 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
563 if (entry->me_key == NULL)
564 return NULL;
565 return &entry->me_value;
566}
567
568/* Returns -1 on failure, 0 on success. */
569static int
570PyMemoTable_Set(PyMemoTable *self, PyObject *key, long value)
571{
572 PyMemoEntry *entry;
573
574 assert(key != NULL);
575
576 entry = _PyMemoTable_Lookup(self, key);
577 if (entry->me_key != NULL) {
578 entry->me_value = value;
579 return 0;
580 }
581 Py_INCREF(key);
582 entry->me_key = key;
583 entry->me_value = value;
584 self->mt_used++;
585
586 /* If we added a key, we can safely resize. Otherwise just return!
587 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
588 *
589 * Quadrupling the size improves average table sparseness
590 * (reducing collisions) at the cost of some memory. It also halves
591 * the number of expensive resize operations in a growing memo table.
592 *
593 * Very large memo tables (over 50K items) use doubling instead.
594 * This may help applications with severe memory constraints.
595 */
596 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
597 return 0;
598 return _PyMemoTable_ResizeTable(self,
599 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
600}
601
602#undef MT_MINSIZE
603#undef PERTURB_SHIFT
604
605/*************************************************************************/
606
607/* Helpers for creating the argument tuple passed to functions. This has the
608 performance advantage of calling PyTuple_New() only once.
609
610 XXX(avassalotti): Inline directly in _Pickler_FastCall() and
611 _Unpickler_FastCall(). */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000612#define ARG_TUP(self, obj) do { \
613 if ((self)->arg || ((self)->arg=PyTuple_New(1))) { \
614 Py_XDECREF(PyTuple_GET_ITEM((self)->arg, 0)); \
615 PyTuple_SET_ITEM((self)->arg, 0, (obj)); \
616 } \
617 else { \
618 Py_DECREF((obj)); \
619 } \
620 } while (0)
621
622#define FREE_ARG_TUP(self) do { \
623 if ((self)->arg->ob_refcnt > 1) \
624 Py_CLEAR((self)->arg); \
625 } while (0)
626
627/* A temporary cleaner API for fast single argument function call.
628
629 XXX: Does caching the argument tuple provides any real performance benefits?
630
631 A quick benchmark, on a 2.0GHz Athlon64 3200+ running Linux 2.6.24 with
632 glibc 2.7, tells me that it takes roughly 20,000,000 PyTuple_New(1) calls
633 when the tuple is retrieved from the freelist (i.e, call PyTuple_New() then
634 immediately DECREF it) and 1,200,000 calls when allocating brand new tuples
635 (i.e, call PyTuple_New() and store the returned value in an array), to save
636 one second (wall clock time). Either ways, the loading time a pickle stream
637 large enough to generate this number of calls would be massively
638 overwhelmed by other factors, like I/O throughput, the GC traversal and
639 object allocation overhead. So, I really doubt these functions provide any
640 real benefits.
641
642 On the other hand, oprofile reports that pickle spends a lot of time in
643 these functions. But, that is probably more related to the function call
644 overhead, than the argument tuple allocation.
645
646 XXX: And, what is the reference behavior of these? Steal, borrow? At first
647 glance, it seems to steal the reference of 'arg' and borrow the reference
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000648 of 'func'. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000649static PyObject *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000650_Pickler_FastCall(PicklerObject *self, PyObject *func, PyObject *arg)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000651{
652 PyObject *result = NULL;
653
654 ARG_TUP(self, arg);
655 if (self->arg) {
656 result = PyObject_Call(func, self->arg, NULL);
657 FREE_ARG_TUP(self);
658 }
659 return result;
660}
661
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000662static int
663_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000664{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000665 Py_CLEAR(self->output_buffer);
666 self->output_buffer =
667 PyBytes_FromStringAndSize(NULL, self->max_output_len);
668 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000669 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000670 self->output_len = 0;
671 return 0;
672}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000673
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000674static PyObject *
675_Pickler_GetString(PicklerObject *self)
676{
677 PyObject *output_buffer = self->output_buffer;
678
679 assert(self->output_buffer != NULL);
680 self->output_buffer = NULL;
681 /* Resize down to exact size */
682 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
683 return NULL;
684 return output_buffer;
685}
686
687static int
688_Pickler_FlushToFile(PicklerObject *self)
689{
690 PyObject *output, *result;
691
692 assert(self->write != NULL);
693
694 output = _Pickler_GetString(self);
695 if (output == NULL)
696 return -1;
697
698 result = _Pickler_FastCall(self, self->write, output);
699 Py_XDECREF(result);
700 return (result == NULL) ? -1 : 0;
701}
702
703static int
704_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t n)
705{
706 Py_ssize_t i, required;
707 char *buffer;
708
709 assert(s != NULL);
710
711 required = self->output_len + n;
712 if (required > self->max_output_len) {
713 if (self->write != NULL && required > MAX_WRITE_BUF_SIZE) {
714 /* XXX This reallocates a new buffer every time, which is a bit
715 wasteful. */
716 if (_Pickler_FlushToFile(self) < 0)
717 return -1;
718 if (_Pickler_ClearBuffer(self) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000719 return -1;
720 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000721 if (self->write != NULL && n > MAX_WRITE_BUF_SIZE) {
722 /* we already flushed above, so the buffer is empty */
723 PyObject *result;
724 /* XXX we could spare an intermediate copy and pass
725 a memoryview instead */
726 PyObject *output = PyBytes_FromStringAndSize(s, n);
727 if (s == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000728 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000729 result = _Pickler_FastCall(self, self->write, output);
730 Py_XDECREF(result);
731 return (result == NULL) ? -1 : 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000732 }
733 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000734 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
735 PyErr_NoMemory();
736 return -1;
737 }
738 self->max_output_len = (self->output_len + n) * 2;
739 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
740 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000741 }
742 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000743 buffer = PyBytes_AS_STRING(self->output_buffer);
744 if (n < 8) {
745 /* This is faster than memcpy when the string is short. */
746 for (i = 0; i < n; i++) {
747 buffer[self->output_len + i] = s[i];
748 }
749 }
750 else {
751 memcpy(buffer + self->output_len, s, n);
752 }
753 self->output_len += n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000754 return n;
755}
756
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000757static PicklerObject *
758_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000759{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000760 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000761
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000762 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
763 if (self == NULL)
764 return NULL;
765
766 self->pers_func = NULL;
767 self->arg = NULL;
768 self->write = NULL;
769 self->proto = 0;
770 self->bin = 0;
771 self->fast = 0;
772 self->fast_nesting = 0;
773 self->fix_imports = 0;
774 self->fast_memo = NULL;
775
776 self->memo = PyMemoTable_New();
777 if (self->memo == NULL) {
778 Py_DECREF(self);
779 return NULL;
780 }
781 self->max_output_len = WRITE_BUF_SIZE;
782 self->output_len = 0;
783 self->output_buffer = PyBytes_FromStringAndSize(NULL,
784 self->max_output_len);
785 if (self->output_buffer == NULL) {
786 Py_DECREF(self);
787 return NULL;
788 }
789 return self;
790}
791
792static int
793_Pickler_SetProtocol(PicklerObject *self, PyObject *proto_obj,
794 PyObject *fix_imports_obj)
795{
796 long proto = 0;
797 int fix_imports;
798
799 if (proto_obj == NULL || proto_obj == Py_None)
800 proto = DEFAULT_PROTOCOL;
801 else {
802 proto = PyLong_AsLong(proto_obj);
803 if (proto == -1 && PyErr_Occurred())
804 return -1;
805 }
806 if (proto < 0)
807 proto = HIGHEST_PROTOCOL;
808 if (proto > HIGHEST_PROTOCOL) {
809 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
810 HIGHEST_PROTOCOL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000811 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000812 }
813 fix_imports = PyObject_IsTrue(fix_imports_obj);
814 if (fix_imports == -1)
815 return -1;
816
817 self->proto = proto;
818 self->bin = proto > 0;
819 self->fix_imports = fix_imports && proto < 3;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000820
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000821 return 0;
822}
823
824/* Returns -1 (with an exception set) on failure, 0 on success. This may
825 be called once on a freshly created Pickler. */
826static int
827_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
828{
829 assert(file != NULL);
830 self->write = PyObject_GetAttrString(file, "write");
831 if (self->write == NULL) {
832 if (PyErr_ExceptionMatches(PyExc_AttributeError))
833 PyErr_SetString(PyExc_TypeError,
834 "file must have a 'write' attribute");
835 return -1;
836 }
837
838 return 0;
839}
840
841/* See documentation for _Pickler_FastCall(). */
842static PyObject *
843_Unpickler_FastCall(UnpicklerObject *self, PyObject *func, PyObject *arg)
844{
845 PyObject *result = NULL;
846
847 ARG_TUP(self, arg);
848 if (self->arg) {
849 result = PyObject_Call(func, self->arg, NULL);
850 FREE_ARG_TUP(self);
851 }
852 return result;
853}
854
855/* Returns the size of the input on success, -1 on failure. This takes its
856 own reference to `input`. */
857static Py_ssize_t
858_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
859{
860 if (self->buffer.buf != NULL)
861 PyBuffer_Release(&self->buffer);
862 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
863 return -1;
864 self->input_buffer = self->buffer.buf;
865 self->input_len = self->buffer.len;
866 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000867 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000868 return self->input_len;
869}
870
Antoine Pitrou04248a82010-10-12 20:51:21 +0000871static int
872_Unpickler_SkipConsumed(UnpicklerObject *self)
873{
874 Py_ssize_t consumed = self->next_read_idx - self->prefetched_idx;
875
876 if (consumed > 0) {
877 PyObject *r;
878 assert(self->peek); /* otherwise we did something wrong */
879 /* This makes an useless copy... */
880 r = PyObject_CallFunction(self->read, "n", consumed);
881 if (r == NULL)
882 return -1;
883 Py_DECREF(r);
884 self->prefetched_idx = self->next_read_idx;
885 }
886 return 0;
887}
888
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000889static const Py_ssize_t READ_WHOLE_LINE = -1;
890
891/* If reading from a file, we need to only pull the bytes we need, since there
892 may be multiple pickle objects arranged contiguously in the same input
893 buffer.
894
895 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
896 bytes from the input stream/buffer.
897
898 Update the unpickler's input buffer with the newly-read data. Returns -1 on
899 failure; on success, returns the number of bytes read from the file.
900
901 On success, self->input_len will be 0; this is intentional so that when
902 unpickling from a file, the "we've run out of data" code paths will trigger,
903 causing the Unpickler to go back to the file for more data. Use the returned
904 size to tell you how much data you can process. */
905static Py_ssize_t
906_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
907{
908 PyObject *data;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000909 Py_ssize_t read_size, prefetched_size = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000910
911 assert(self->read != NULL);
Antoine Pitrou04248a82010-10-12 20:51:21 +0000912
913 if (_Unpickler_SkipConsumed(self) < 0)
914 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000915
916 if (n == READ_WHOLE_LINE)
917 data = PyObject_Call(self->readline, empty_tuple, NULL);
918 else {
919 PyObject *len = PyLong_FromSsize_t(n);
920 if (len == NULL)
921 return -1;
922 data = _Unpickler_FastCall(self, self->read, len);
923 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000924 if (data == NULL)
925 return -1;
926
Antoine Pitrou04248a82010-10-12 20:51:21 +0000927 /* Prefetch some data without advancing the file pointer, if possible */
928 if (self->peek) {
929 PyObject *len, *prefetched;
930 len = PyLong_FromSsize_t(PREFETCH);
931 if (len == NULL) {
932 Py_DECREF(data);
933 return -1;
934 }
935 prefetched = _Unpickler_FastCall(self, self->peek, len);
936 if (prefetched == NULL) {
937 if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) {
938 /* peek() is probably not supported by the given file object */
939 PyErr_Clear();
940 Py_CLEAR(self->peek);
941 }
942 else {
943 Py_DECREF(data);
944 return -1;
945 }
946 }
947 else {
948 assert(PyBytes_Check(prefetched));
949 prefetched_size = PyBytes_GET_SIZE(prefetched);
950 PyBytes_ConcatAndDel(&data, prefetched);
951 if (data == NULL)
952 return -1;
953 }
954 }
955
956 read_size = _Unpickler_SetStringInput(self, data) - prefetched_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000957 Py_DECREF(data);
Antoine Pitrou04248a82010-10-12 20:51:21 +0000958 self->prefetched_idx = read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000959 return read_size;
960}
961
962/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
963
964 This should be used for all data reads, rather than accessing the unpickler's
965 input buffer directly. This method deals correctly with reading from input
966 streams, which the input buffer doesn't deal with.
967
968 Note that when reading from a file-like object, self->next_read_idx won't
969 be updated (it should remain at 0 for the entire unpickling process). You
970 should use this function's return value to know how many bytes you can
971 consume.
972
973 Returns -1 (with an exception set) on failure. On success, return the
974 number of chars read. */
975static Py_ssize_t
976_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
977{
Antoine Pitrou04248a82010-10-12 20:51:21 +0000978 Py_ssize_t num_read;
979
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000980 if (n == 0) {
981 *s = NULL;
982 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000983 }
984
Antoine Pitrou04248a82010-10-12 20:51:21 +0000985 if (self->next_read_idx + n <= self->input_len) {
986 *s = self->input_buffer + self->next_read_idx;
987 self->next_read_idx += n;
988 return n;
989 }
990 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000991 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +0000992 return -1;
993 }
Antoine Pitrou04248a82010-10-12 20:51:21 +0000994 num_read = _Unpickler_ReadFromFile(self, n);
995 if (num_read < 0)
996 return -1;
997 if (num_read < n) {
998 PyErr_Format(PyExc_EOFError, "Ran out of input");
999 return -1;
1000 }
1001 *s = self->input_buffer;
1002 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001003 return n;
1004}
1005
1006static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001007_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1008 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001009{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001010 char *input_line = PyMem_Realloc(self->input_line, len + 1);
1011 if (input_line == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001012 return -1;
1013
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001014 memcpy(input_line, line, len);
1015 input_line[len] = '\0';
1016 self->input_line = input_line;
1017 *result = self->input_line;
1018 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001019}
1020
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001021/* Read a line from the input stream/buffer. If we run off the end of the input
1022 before hitting \n, return the data we found.
1023
1024 Returns the number of chars read, or -1 on failure. */
1025static Py_ssize_t
1026_Unpickler_Readline(UnpicklerObject *self, char **result)
1027{
1028 Py_ssize_t i, num_read;
1029
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001030 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001031 if (self->input_buffer[i] == '\n') {
1032 char *line_start = self->input_buffer + self->next_read_idx;
1033 num_read = i - self->next_read_idx + 1;
1034 self->next_read_idx = i + 1;
1035 return _Unpickler_CopyLine(self, line_start, num_read, result);
1036 }
1037 }
1038 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001039 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1040 if (num_read < 0)
1041 return -1;
1042 *result = self->input_buffer;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001043 self->next_read_idx = num_read;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001044 return num_read;
1045 }
1046
1047 /* If we get here, we've run off the end of the input string. Return the
1048 remaining string and let the caller figure it out. */
1049 *result = self->input_buffer + self->next_read_idx;
1050 num_read = i - self->next_read_idx;
1051 self->next_read_idx = i;
1052 return num_read;
1053}
1054
1055/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1056 will be modified in place. */
1057static int
1058_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1059{
1060 Py_ssize_t i;
1061 PyObject **memo;
1062
1063 assert(new_size > self->memo_size);
1064
1065 memo = PyMem_REALLOC(self->memo, new_size * sizeof(PyObject *));
1066 if (memo == NULL) {
1067 PyErr_NoMemory();
1068 return -1;
1069 }
1070 self->memo = memo;
1071 for (i = self->memo_size; i < new_size; i++)
1072 self->memo[i] = NULL;
1073 self->memo_size = new_size;
1074 return 0;
1075}
1076
1077/* Returns NULL if idx is out of bounds. */
1078static PyObject *
1079_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1080{
1081 if (idx < 0 || idx >= self->memo_size)
1082 return NULL;
1083
1084 return self->memo[idx];
1085}
1086
1087/* Returns -1 (with an exception set) on failure, 0 on success.
1088 This takes its own reference to `value`. */
1089static int
1090_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1091{
1092 PyObject *old_item;
1093
1094 if (idx >= self->memo_size) {
1095 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1096 return -1;
1097 assert(idx < self->memo_size);
1098 }
1099 Py_INCREF(value);
1100 old_item = self->memo[idx];
1101 self->memo[idx] = value;
1102 Py_XDECREF(old_item);
1103 return 0;
1104}
1105
1106static PyObject **
1107_Unpickler_NewMemo(Py_ssize_t new_size)
1108{
1109 PyObject **memo = PyMem_MALLOC(new_size * sizeof(PyObject *));
1110 if (memo == NULL)
1111 return NULL;
1112 memset(memo, 0, new_size * sizeof(PyObject *));
1113 return memo;
1114}
1115
1116/* Free the unpickler's memo, taking care to decref any items left in it. */
1117static void
1118_Unpickler_MemoCleanup(UnpicklerObject *self)
1119{
1120 Py_ssize_t i;
1121 PyObject **memo = self->memo;
1122
1123 if (self->memo == NULL)
1124 return;
1125 self->memo = NULL;
1126 i = self->memo_size;
1127 while (--i >= 0) {
1128 Py_XDECREF(memo[i]);
1129 }
1130 PyMem_FREE(memo);
1131}
1132
1133static UnpicklerObject *
1134_Unpickler_New(void)
1135{
1136 UnpicklerObject *self;
1137
1138 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1139 if (self == NULL)
1140 return NULL;
1141
1142 self->stack = (Pdata *)Pdata_New();
1143 if (self->stack == NULL) {
1144 Py_DECREF(self);
1145 return NULL;
1146 }
1147 memset(&self->buffer, 0, sizeof(Py_buffer));
1148
1149 self->memo_size = 32;
1150 self->memo = _Unpickler_NewMemo(self->memo_size);
1151 if (self->memo == NULL) {
1152 Py_DECREF(self);
1153 return NULL;
1154 }
1155
1156 self->arg = NULL;
1157 self->pers_func = NULL;
1158 self->input_buffer = NULL;
1159 self->input_line = NULL;
1160 self->input_len = 0;
1161 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001162 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001163 self->read = NULL;
1164 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001165 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001166 self->encoding = NULL;
1167 self->errors = NULL;
1168 self->marks = NULL;
1169 self->num_marks = 0;
1170 self->marks_size = 0;
1171 self->proto = 0;
1172 self->fix_imports = 0;
1173
1174 return self;
1175}
1176
1177/* Returns -1 (with an exception set) on failure, 0 on success. This may
1178 be called once on a freshly created Pickler. */
1179static int
1180_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1181{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001182 self->peek = PyObject_GetAttrString(file, "peek");
1183 if (self->peek == NULL) {
1184 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1185 PyErr_Clear();
1186 else
1187 return -1;
1188 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001189 self->read = PyObject_GetAttrString(file, "read");
1190 self->readline = PyObject_GetAttrString(file, "readline");
1191 if (self->readline == NULL || self->read == NULL) {
1192 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1193 PyErr_SetString(PyExc_TypeError,
1194 "file must have 'read' and 'readline' attributes");
1195 Py_CLEAR(self->read);
1196 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001197 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001198 return -1;
1199 }
1200 return 0;
1201}
1202
1203/* Returns -1 (with an exception set) on failure, 0 on success. This may
1204 be called once on a freshly created Pickler. */
1205static int
1206_Unpickler_SetInputEncoding(UnpicklerObject *self,
1207 const char *encoding,
1208 const char *errors)
1209{
1210 if (encoding == NULL)
1211 encoding = "ASCII";
1212 if (errors == NULL)
1213 errors = "strict";
1214
1215 self->encoding = strdup(encoding);
1216 self->errors = strdup(errors);
1217 if (self->encoding == NULL || self->errors == NULL) {
1218 PyErr_NoMemory();
1219 return -1;
1220 }
1221 return 0;
1222}
1223
1224/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001225static int
1226memo_get(PicklerObject *self, PyObject *key)
1227{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001228 long *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001229 char pdata[30];
1230 int len;
1231
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001232 value = PyMemoTable_Get(self->memo, key);
1233 if (value == NULL) {
1234 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001235 return -1;
1236 }
1237
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001238 if (!self->bin) {
1239 pdata[0] = GET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001240 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, "%ld\n", *value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001241 len = (int)strlen(pdata);
1242 }
1243 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001244 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001245 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001246 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001247 len = 2;
1248 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001249 else if (*value <= 0xffffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001250 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001251 pdata[1] = (unsigned char)(*value & 0xff);
1252 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1253 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1254 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001255 len = 5;
1256 }
1257 else { /* unlikely */
1258 PyErr_SetString(PicklingError,
1259 "memo id too large for LONG_BINGET");
1260 return -1;
1261 }
1262 }
1263
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001264 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001265 return -1;
1266
1267 return 0;
1268}
1269
1270/* Store an object in the memo, assign it a new unique ID based on the number
1271 of objects currently stored in the memo and generate a PUT opcode. */
1272static int
1273memo_put(PicklerObject *self, PyObject *obj)
1274{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001275 long x;
1276 char pdata[30];
1277 int len;
1278 int status = 0;
1279
1280 if (self->fast)
1281 return 0;
1282
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001283 x = PyMemoTable_Size(self->memo);
1284 if (PyMemoTable_Set(self->memo, obj, x) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001285 goto error;
1286
1287 if (!self->bin) {
1288 pdata[0] = PUT;
1289 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, "%ld\n", x);
1290 len = strlen(pdata);
1291 }
1292 else {
1293 if (x < 256) {
1294 pdata[0] = BINPUT;
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00001295 pdata[1] = (unsigned char)x;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001296 len = 2;
1297 }
1298 else if (x <= 0xffffffffL) {
1299 pdata[0] = LONG_BINPUT;
1300 pdata[1] = (unsigned char)(x & 0xff);
1301 pdata[2] = (unsigned char)((x >> 8) & 0xff);
1302 pdata[3] = (unsigned char)((x >> 16) & 0xff);
1303 pdata[4] = (unsigned char)((x >> 24) & 0xff);
1304 len = 5;
1305 }
1306 else { /* unlikely */
1307 PyErr_SetString(PicklingError,
1308 "memo id too large for LONG_BINPUT");
1309 return -1;
1310 }
1311 }
1312
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001313 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001314 goto error;
1315
1316 if (0) {
1317 error:
1318 status = -1;
1319 }
1320
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001321 return status;
1322}
1323
1324static PyObject *
1325whichmodule(PyObject *global, PyObject *global_name)
1326{
1327 Py_ssize_t i, j;
1328 static PyObject *module_str = NULL;
1329 static PyObject *main_str = NULL;
1330 PyObject *module_name;
1331 PyObject *modules_dict;
1332 PyObject *module;
1333 PyObject *obj;
1334
1335 if (module_str == NULL) {
1336 module_str = PyUnicode_InternFromString("__module__");
1337 if (module_str == NULL)
1338 return NULL;
1339 main_str = PyUnicode_InternFromString("__main__");
1340 if (main_str == NULL)
1341 return NULL;
1342 }
1343
1344 module_name = PyObject_GetAttr(global, module_str);
1345
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00001346 /* In some rare cases (e.g., bound methods of extension types),
1347 __module__ can be None. If it is so, then search sys.modules
1348 for the module of global. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001349 if (module_name == Py_None) {
1350 Py_DECREF(module_name);
1351 goto search;
1352 }
1353
1354 if (module_name) {
1355 return module_name;
1356 }
1357 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1358 PyErr_Clear();
1359 else
1360 return NULL;
1361
1362 search:
1363 modules_dict = PySys_GetObject("modules");
1364 if (modules_dict == NULL)
1365 return NULL;
1366
1367 i = 0;
1368 module_name = NULL;
1369 while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) {
Mark Dickinson211c6252009-02-01 10:28:51 +00001370 if (PyObject_RichCompareBool(module_name, main_str, Py_EQ) == 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001371 continue;
1372
1373 obj = PyObject_GetAttr(module, global_name);
1374 if (obj == NULL) {
1375 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1376 PyErr_Clear();
1377 else
1378 return NULL;
1379 continue;
1380 }
1381
1382 if (obj != global) {
1383 Py_DECREF(obj);
1384 continue;
1385 }
1386
1387 Py_DECREF(obj);
1388 break;
1389 }
1390
1391 /* If no module is found, use __main__. */
1392 if (!j) {
1393 module_name = main_str;
1394 }
1395
1396 Py_INCREF(module_name);
1397 return module_name;
1398}
1399
1400/* fast_save_enter() and fast_save_leave() are guards against recursive
1401 objects when Pickler is used with the "fast mode" (i.e., with object
1402 memoization disabled). If the nesting of a list or dict object exceed
1403 FAST_NESTING_LIMIT, these guards will start keeping an internal
1404 reference to the seen list or dict objects and check whether these objects
1405 are recursive. These are not strictly necessary, since save() has a
1406 hard-coded recursion limit, but they give a nicer error message than the
1407 typical RuntimeError. */
1408static int
1409fast_save_enter(PicklerObject *self, PyObject *obj)
1410{
1411 /* if fast_nesting < 0, we're doing an error exit. */
1412 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1413 PyObject *key = NULL;
1414 if (self->fast_memo == NULL) {
1415 self->fast_memo = PyDict_New();
1416 if (self->fast_memo == NULL) {
1417 self->fast_nesting = -1;
1418 return 0;
1419 }
1420 }
1421 key = PyLong_FromVoidPtr(obj);
1422 if (key == NULL)
1423 return 0;
1424 if (PyDict_GetItem(self->fast_memo, key)) {
1425 Py_DECREF(key);
1426 PyErr_Format(PyExc_ValueError,
1427 "fast mode: can't pickle cyclic objects "
1428 "including object type %.200s at %p",
1429 obj->ob_type->tp_name, obj);
1430 self->fast_nesting = -1;
1431 return 0;
1432 }
1433 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1434 Py_DECREF(key);
1435 self->fast_nesting = -1;
1436 return 0;
1437 }
1438 Py_DECREF(key);
1439 }
1440 return 1;
1441}
1442
1443static int
1444fast_save_leave(PicklerObject *self, PyObject *obj)
1445{
1446 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1447 PyObject *key = PyLong_FromVoidPtr(obj);
1448 if (key == NULL)
1449 return 0;
1450 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1451 Py_DECREF(key);
1452 return 0;
1453 }
1454 Py_DECREF(key);
1455 }
1456 return 1;
1457}
1458
1459static int
1460save_none(PicklerObject *self, PyObject *obj)
1461{
1462 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001463 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001464 return -1;
1465
1466 return 0;
1467}
1468
1469static int
1470save_bool(PicklerObject *self, PyObject *obj)
1471{
1472 static const char *buf[2] = { FALSE, TRUE };
1473 const char len[2] = {sizeof(FALSE) - 1, sizeof(TRUE) - 1};
1474 int p = (obj == Py_True);
1475
1476 if (self->proto >= 2) {
1477 const char bool_op = p ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001478 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001479 return -1;
1480 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001481 else if (_Pickler_Write(self, buf[p], len[p]) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001482 return -1;
1483
1484 return 0;
1485}
1486
1487static int
1488save_int(PicklerObject *self, long x)
1489{
1490 char pdata[32];
1491 int len = 0;
1492
1493 if (!self->bin
1494#if SIZEOF_LONG > 4
1495 || x > 0x7fffffffL || x < -0x80000000L
1496#endif
1497 ) {
1498 /* Text-mode pickle, or long too big to fit in the 4-byte
1499 * signed BININT format: store as a string.
1500 */
Mark Dickinson8dd05142009-01-20 20:43:58 +00001501 pdata[0] = LONG; /* use LONG for consistency with pickle.py */
1502 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, "%ldL\n", x);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001503 if (_Pickler_Write(self, pdata, strlen(pdata)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001504 return -1;
1505 }
1506 else {
1507 /* Binary pickle and x fits in a signed 4-byte int. */
1508 pdata[1] = (unsigned char)(x & 0xff);
1509 pdata[2] = (unsigned char)((x >> 8) & 0xff);
1510 pdata[3] = (unsigned char)((x >> 16) & 0xff);
1511 pdata[4] = (unsigned char)((x >> 24) & 0xff);
1512
1513 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1514 if (pdata[2] == 0) {
1515 pdata[0] = BININT1;
1516 len = 2;
1517 }
1518 else {
1519 pdata[0] = BININT2;
1520 len = 3;
1521 }
1522 }
1523 else {
1524 pdata[0] = BININT;
1525 len = 5;
1526 }
1527
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001528 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001529 return -1;
1530 }
1531
1532 return 0;
1533}
1534
1535static int
1536save_long(PicklerObject *self, PyObject *obj)
1537{
1538 PyObject *repr = NULL;
1539 Py_ssize_t size;
1540 long val = PyLong_AsLong(obj);
1541 int status = 0;
1542
1543 const char long_op = LONG;
1544
1545 if (val == -1 && PyErr_Occurred()) {
1546 /* out of range for int pickling */
1547 PyErr_Clear();
1548 }
1549 else
1550 return save_int(self, val);
1551
1552 if (self->proto >= 2) {
1553 /* Linear-time pickling. */
1554 size_t nbits;
1555 size_t nbytes;
1556 unsigned char *pdata;
1557 char header[5];
1558 int i;
1559 int sign = _PyLong_Sign(obj);
1560
1561 if (sign == 0) {
1562 header[0] = LONG1;
1563 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001564 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001565 goto error;
1566 return 0;
1567 }
1568 nbits = _PyLong_NumBits(obj);
1569 if (nbits == (size_t)-1 && PyErr_Occurred())
1570 goto error;
1571 /* How many bytes do we need? There are nbits >> 3 full
1572 * bytes of data, and nbits & 7 leftover bits. If there
1573 * are any leftover bits, then we clearly need another
1574 * byte. Wnat's not so obvious is that we *probably*
1575 * need another byte even if there aren't any leftovers:
1576 * the most-significant bit of the most-significant byte
1577 * acts like a sign bit, and it's usually got a sense
1578 * opposite of the one we need. The exception is longs
1579 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1580 * its own 256's-complement, so has the right sign bit
1581 * even without the extra byte. That's a pain to check
1582 * for in advance, though, so we always grab an extra
1583 * byte at the start, and cut it back later if possible.
1584 */
1585 nbytes = (nbits >> 3) + 1;
1586 if (nbytes > INT_MAX) {
1587 PyErr_SetString(PyExc_OverflowError,
1588 "long too large to pickle");
1589 goto error;
1590 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001591 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001592 if (repr == NULL)
1593 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001594 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001595 i = _PyLong_AsByteArray((PyLongObject *)obj,
1596 pdata, nbytes,
1597 1 /* little endian */ , 1 /* signed */ );
1598 if (i < 0)
1599 goto error;
1600 /* If the long is negative, this may be a byte more than
1601 * needed. This is so iff the MSB is all redundant sign
1602 * bits.
1603 */
1604 if (sign < 0 &&
1605 nbytes > 1 &&
1606 pdata[nbytes - 1] == 0xff &&
1607 (pdata[nbytes - 2] & 0x80) != 0) {
1608 nbytes--;
1609 }
1610
1611 if (nbytes < 256) {
1612 header[0] = LONG1;
1613 header[1] = (unsigned char)nbytes;
1614 size = 2;
1615 }
1616 else {
1617 header[0] = LONG4;
1618 size = (int)nbytes;
1619 for (i = 1; i < 5; i++) {
1620 header[i] = (unsigned char)(size & 0xff);
1621 size >>= 8;
1622 }
1623 size = 5;
1624 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001625 if (_Pickler_Write(self, header, size) < 0 ||
1626 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001627 goto error;
1628 }
1629 else {
1630 char *string;
1631
Mark Dickinson8dd05142009-01-20 20:43:58 +00001632 /* proto < 2: write the repr and newline. This is quadratic-time (in
1633 the number of digits), in both directions. We add a trailing 'L'
1634 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001635
1636 repr = PyObject_Repr(obj);
1637 if (repr == NULL)
1638 goto error;
1639
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001640 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001641 if (string == NULL)
1642 goto error;
1643
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001644 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1645 _Pickler_Write(self, string, size) < 0 ||
1646 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001647 goto error;
1648 }
1649
1650 if (0) {
1651 error:
1652 status = -1;
1653 }
1654 Py_XDECREF(repr);
1655
1656 return status;
1657}
1658
1659static int
1660save_float(PicklerObject *self, PyObject *obj)
1661{
1662 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1663
1664 if (self->bin) {
1665 char pdata[9];
1666 pdata[0] = BINFLOAT;
1667 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1668 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001669 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001670 return -1;
Eric Smith0923d1d2009-04-16 20:16:10 +00001671 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001672 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001673 int result = -1;
1674 char *buf = NULL;
1675 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001676
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001677 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001678 goto done;
1679
Mark Dickinson3e09f432009-04-17 08:41:23 +00001680 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001681 if (!buf) {
1682 PyErr_NoMemory();
1683 goto done;
1684 }
1685
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001686 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001687 goto done;
1688
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001689 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001690 goto done;
1691
1692 result = 0;
1693done:
1694 PyMem_Free(buf);
1695 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001696 }
1697
1698 return 0;
1699}
1700
1701static int
1702save_bytes(PicklerObject *self, PyObject *obj)
1703{
1704 if (self->proto < 3) {
1705 /* Older pickle protocols do not have an opcode for pickling bytes
1706 objects. Therefore, we need to fake the copy protocol (i.e.,
1707 the __reduce__ method) to permit bytes object unpickling. */
1708 PyObject *reduce_value = NULL;
1709 PyObject *bytelist = NULL;
1710 int status;
1711
1712 bytelist = PySequence_List(obj);
1713 if (bytelist == NULL)
1714 return -1;
1715
1716 reduce_value = Py_BuildValue("(O(O))", (PyObject *)&PyBytes_Type,
1717 bytelist);
1718 if (reduce_value == NULL) {
1719 Py_DECREF(bytelist);
1720 return -1;
1721 }
1722
1723 /* save_reduce() will memoize the object automatically. */
1724 status = save_reduce(self, reduce_value, obj);
1725 Py_DECREF(reduce_value);
1726 Py_DECREF(bytelist);
1727 return status;
1728 }
1729 else {
1730 Py_ssize_t size;
1731 char header[5];
1732 int len;
1733
1734 size = PyBytes_Size(obj);
1735 if (size < 0)
1736 return -1;
1737
1738 if (size < 256) {
1739 header[0] = SHORT_BINBYTES;
1740 header[1] = (unsigned char)size;
1741 len = 2;
1742 }
1743 else if (size <= 0xffffffffL) {
1744 header[0] = BINBYTES;
1745 header[1] = (unsigned char)(size & 0xff);
1746 header[2] = (unsigned char)((size >> 8) & 0xff);
1747 header[3] = (unsigned char)((size >> 16) & 0xff);
1748 header[4] = (unsigned char)((size >> 24) & 0xff);
1749 len = 5;
1750 }
1751 else {
1752 return -1; /* string too large */
1753 }
1754
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001755 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001756 return -1;
1757
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001758 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001759 return -1;
1760
1761 if (memo_put(self, obj) < 0)
1762 return -1;
1763
1764 return 0;
1765 }
1766}
1767
1768/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1769 backslash and newline characters to \uXXXX escapes. */
1770static PyObject *
1771raw_unicode_escape(const Py_UNICODE *s, Py_ssize_t size)
1772{
1773 PyObject *repr, *result;
1774 char *p;
1775 char *q;
1776
1777 static const char *hexdigits = "0123456789abcdef";
1778
1779#ifdef Py_UNICODE_WIDE
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00001780 const Py_ssize_t expandsize = 10;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001781#else
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00001782 const Py_ssize_t expandsize = 6;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001783#endif
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00001784
1785 if (size > PY_SSIZE_T_MAX / expandsize)
1786 return PyErr_NoMemory();
1787
1788 repr = PyByteArray_FromStringAndSize(NULL, expandsize * size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001789 if (repr == NULL)
1790 return NULL;
1791 if (size == 0)
1792 goto done;
1793
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00001794 p = q = PyByteArray_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001795 while (size-- > 0) {
1796 Py_UNICODE ch = *s++;
1797#ifdef Py_UNICODE_WIDE
1798 /* Map 32-bit characters to '\Uxxxxxxxx' */
1799 if (ch >= 0x10000) {
1800 *p++ = '\\';
1801 *p++ = 'U';
1802 *p++ = hexdigits[(ch >> 28) & 0xf];
1803 *p++ = hexdigits[(ch >> 24) & 0xf];
1804 *p++ = hexdigits[(ch >> 20) & 0xf];
1805 *p++ = hexdigits[(ch >> 16) & 0xf];
1806 *p++ = hexdigits[(ch >> 12) & 0xf];
1807 *p++ = hexdigits[(ch >> 8) & 0xf];
1808 *p++ = hexdigits[(ch >> 4) & 0xf];
1809 *p++ = hexdigits[ch & 15];
1810 }
1811 else
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00001812#else
1813 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1814 if (ch >= 0xD800 && ch < 0xDC00) {
1815 Py_UNICODE ch2;
1816 Py_UCS4 ucs;
1817
1818 ch2 = *s++;
1819 size--;
1820 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1821 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1822 *p++ = '\\';
1823 *p++ = 'U';
1824 *p++ = hexdigits[(ucs >> 28) & 0xf];
1825 *p++ = hexdigits[(ucs >> 24) & 0xf];
1826 *p++ = hexdigits[(ucs >> 20) & 0xf];
1827 *p++ = hexdigits[(ucs >> 16) & 0xf];
1828 *p++ = hexdigits[(ucs >> 12) & 0xf];
1829 *p++ = hexdigits[(ucs >> 8) & 0xf];
1830 *p++ = hexdigits[(ucs >> 4) & 0xf];
1831 *p++ = hexdigits[ucs & 0xf];
1832 continue;
1833 }
1834 /* Fall through: isolated surrogates are copied as-is */
1835 s--;
1836 size++;
1837 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001838#endif
1839 /* Map 16-bit characters to '\uxxxx' */
1840 if (ch >= 256 || ch == '\\' || ch == '\n') {
1841 *p++ = '\\';
1842 *p++ = 'u';
1843 *p++ = hexdigits[(ch >> 12) & 0xf];
1844 *p++ = hexdigits[(ch >> 8) & 0xf];
1845 *p++ = hexdigits[(ch >> 4) & 0xf];
1846 *p++ = hexdigits[ch & 15];
1847 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00001848 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001849 else
1850 *p++ = (char) ch;
1851 }
1852 size = p - q;
1853
1854 done:
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00001855 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001856 Py_DECREF(repr);
1857 return result;
1858}
1859
1860static int
1861save_unicode(PicklerObject *self, PyObject *obj)
1862{
1863 Py_ssize_t size;
1864 PyObject *encoded = NULL;
1865
1866 if (self->bin) {
1867 char pdata[5];
1868
Victor Stinner485fb562010-04-13 11:07:24 +00001869 encoded = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(obj),
1870 PyUnicode_GET_SIZE(obj),
1871 "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001872 if (encoded == NULL)
1873 goto error;
1874
1875 size = PyBytes_GET_SIZE(encoded);
1876 if (size < 0 || size > 0xffffffffL)
1877 goto error; /* string too large */
1878
1879 pdata[0] = BINUNICODE;
1880 pdata[1] = (unsigned char)(size & 0xff);
1881 pdata[2] = (unsigned char)((size >> 8) & 0xff);
1882 pdata[3] = (unsigned char)((size >> 16) & 0xff);
1883 pdata[4] = (unsigned char)((size >> 24) & 0xff);
1884
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001885 if (_Pickler_Write(self, pdata, 5) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001886 goto error;
1887
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001888 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001889 goto error;
1890 }
1891 else {
1892 const char unicode_op = UNICODE;
1893
1894 encoded = raw_unicode_escape(PyUnicode_AS_UNICODE(obj),
1895 PyUnicode_GET_SIZE(obj));
1896 if (encoded == NULL)
1897 goto error;
1898
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001899 if (_Pickler_Write(self, &unicode_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001900 goto error;
1901
1902 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001903 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001904 goto error;
1905
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001906 if (_Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001907 goto error;
1908 }
1909 if (memo_put(self, obj) < 0)
1910 goto error;
1911
1912 Py_DECREF(encoded);
1913 return 0;
1914
1915 error:
1916 Py_XDECREF(encoded);
1917 return -1;
1918}
1919
1920/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1921static int
1922store_tuple_elements(PicklerObject *self, PyObject *t, int len)
1923{
1924 int i;
1925
1926 assert(PyTuple_Size(t) == len);
1927
1928 for (i = 0; i < len; i++) {
1929 PyObject *element = PyTuple_GET_ITEM(t, i);
1930
1931 if (element == NULL)
1932 return -1;
1933 if (save(self, element, 0) < 0)
1934 return -1;
1935 }
1936
1937 return 0;
1938}
1939
1940/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1941 * used across protocols to minimize the space needed to pickle them.
1942 * Tuples are also the only builtin immutable type that can be recursive
1943 * (a tuple can be reached from itself), and that requires some subtle
1944 * magic so that it works in all cases. IOW, this is a long routine.
1945 */
1946static int
1947save_tuple(PicklerObject *self, PyObject *obj)
1948{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001949 int len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001950
1951 const char mark_op = MARK;
1952 const char tuple_op = TUPLE;
1953 const char pop_op = POP;
1954 const char pop_mark_op = POP_MARK;
1955 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
1956
1957 if ((len = PyTuple_Size(obj)) < 0)
1958 return -1;
1959
1960 if (len == 0) {
1961 char pdata[2];
1962
1963 if (self->proto) {
1964 pdata[0] = EMPTY_TUPLE;
1965 len = 1;
1966 }
1967 else {
1968 pdata[0] = MARK;
1969 pdata[1] = TUPLE;
1970 len = 2;
1971 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001972 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001973 return -1;
1974 return 0;
1975 }
1976
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001977 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001978 * saving the tuple elements, the tuple must be recursive, in
1979 * which case we'll pop everything we put on the stack, and fetch
1980 * its value from the memo.
1981 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001982 if (len <= 3 && self->proto >= 2) {
1983 /* Use TUPLE{1,2,3} opcodes. */
1984 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001985 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001986
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001987 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001988 /* pop the len elements */
1989 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001990 if (_Pickler_Write(self, &pop_op, 1) < 0)
1991 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001992 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001993 if (memo_get(self, obj) < 0)
1994 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001995
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001996 return 0;
1997 }
1998 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001999 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2000 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002001 }
2002 goto memoize;
2003 }
2004
2005 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2006 * Generate MARK e1 e2 ... TUPLE
2007 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002008 if (_Pickler_Write(self, &mark_op, 1) < 0)
2009 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002010
2011 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002012 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002013
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002014 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002015 /* pop the stack stuff we pushed */
2016 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002017 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2018 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002019 }
2020 else {
2021 /* Note that we pop one more than len, to remove
2022 * the MARK too.
2023 */
2024 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002025 if (_Pickler_Write(self, &pop_op, 1) < 0)
2026 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002027 }
2028 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002029 if (memo_get(self, obj) < 0)
2030 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002031
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 return 0;
2033 }
2034 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002035 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2036 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002037 }
2038
2039 memoize:
2040 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002041 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002042
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002043 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002044}
2045
2046/* iter is an iterator giving items, and we batch up chunks of
2047 * MARK item item ... item APPENDS
2048 * opcode sequences. Calling code should have arranged to first create an
2049 * empty list, or list-like object, for the APPENDS to operate on.
2050 * Returns 0 on success, <0 on error.
2051 */
2052static int
2053batch_list(PicklerObject *self, PyObject *iter)
2054{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002055 PyObject *obj = NULL;
2056 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002057 int i, n;
2058
2059 const char mark_op = MARK;
2060 const char append_op = APPEND;
2061 const char appends_op = APPENDS;
2062
2063 assert(iter != NULL);
2064
2065 /* XXX: I think this function could be made faster by avoiding the
2066 iterator interface and fetching objects directly from list using
2067 PyList_GET_ITEM.
2068 */
2069
2070 if (self->proto == 0) {
2071 /* APPENDS isn't available; do one at a time. */
2072 for (;;) {
2073 obj = PyIter_Next(iter);
2074 if (obj == NULL) {
2075 if (PyErr_Occurred())
2076 return -1;
2077 break;
2078 }
2079 i = save(self, obj, 0);
2080 Py_DECREF(obj);
2081 if (i < 0)
2082 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002083 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002084 return -1;
2085 }
2086 return 0;
2087 }
2088
2089 /* proto > 0: write in batches of BATCHSIZE. */
2090 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002091 /* Get first item */
2092 firstitem = PyIter_Next(iter);
2093 if (firstitem == NULL) {
2094 if (PyErr_Occurred())
2095 goto error;
2096
2097 /* nothing more to add */
2098 break;
2099 }
2100
2101 /* Try to get a second item */
2102 obj = PyIter_Next(iter);
2103 if (obj == NULL) {
2104 if (PyErr_Occurred())
2105 goto error;
2106
2107 /* Only one item to write */
2108 if (save(self, firstitem, 0) < 0)
2109 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002110 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002111 goto error;
2112 Py_CLEAR(firstitem);
2113 break;
2114 }
2115
2116 /* More than one item to write */
2117
2118 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002119 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002120 goto error;
2121
2122 if (save(self, firstitem, 0) < 0)
2123 goto error;
2124 Py_CLEAR(firstitem);
2125 n = 1;
2126
2127 /* Fetch and save up to BATCHSIZE items */
2128 while (obj) {
2129 if (save(self, obj, 0) < 0)
2130 goto error;
2131 Py_CLEAR(obj);
2132 n += 1;
2133
2134 if (n == BATCHSIZE)
2135 break;
2136
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002137 obj = PyIter_Next(iter);
2138 if (obj == NULL) {
2139 if (PyErr_Occurred())
2140 goto error;
2141 break;
2142 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002143 }
2144
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002145 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002146 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002147
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002148 } while (n == BATCHSIZE);
2149 return 0;
2150
2151 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002152 Py_XDECREF(firstitem);
2153 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002154 return -1;
2155}
2156
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002157/* This is a variant of batch_list() above, specialized for lists (with no
2158 * support for list subclasses). Like batch_list(), we batch up chunks of
2159 * MARK item item ... item APPENDS
2160 * opcode sequences. Calling code should have arranged to first create an
2161 * empty list, or list-like object, for the APPENDS to operate on.
2162 * Returns 0 on success, -1 on error.
2163 *
2164 * This version is considerably faster than batch_list(), if less general.
2165 *
2166 * Note that this only works for protocols > 0.
2167 */
2168static int
2169batch_list_exact(PicklerObject *self, PyObject *obj)
2170{
2171 PyObject *item = NULL;
2172 int this_batch, total;
2173
2174 const char append_op = APPEND;
2175 const char appends_op = APPENDS;
2176 const char mark_op = MARK;
2177
2178 assert(obj != NULL);
2179 assert(self->proto > 0);
2180 assert(PyList_CheckExact(obj));
2181
2182 if (PyList_GET_SIZE(obj) == 1) {
2183 item = PyList_GET_ITEM(obj, 0);
2184 if (save(self, item, 0) < 0)
2185 return -1;
2186 if (_Pickler_Write(self, &append_op, 1) < 0)
2187 return -1;
2188 return 0;
2189 }
2190
2191 /* Write in batches of BATCHSIZE. */
2192 total = 0;
2193 do {
2194 this_batch = 0;
2195 if (_Pickler_Write(self, &mark_op, 1) < 0)
2196 return -1;
2197 while (total < PyList_GET_SIZE(obj)) {
2198 item = PyList_GET_ITEM(obj, total);
2199 if (save(self, item, 0) < 0)
2200 return -1;
2201 total++;
2202 if (++this_batch == BATCHSIZE)
2203 break;
2204 }
2205 if (_Pickler_Write(self, &appends_op, 1) < 0)
2206 return -1;
2207
2208 } while (total < PyList_GET_SIZE(obj));
2209
2210 return 0;
2211}
2212
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002213static int
2214save_list(PicklerObject *self, PyObject *obj)
2215{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002216 char header[3];
2217 int len;
2218 int status = 0;
2219
2220 if (self->fast && !fast_save_enter(self, obj))
2221 goto error;
2222
2223 /* Create an empty list. */
2224 if (self->bin) {
2225 header[0] = EMPTY_LIST;
2226 len = 1;
2227 }
2228 else {
2229 header[0] = MARK;
2230 header[1] = LIST;
2231 len = 2;
2232 }
2233
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002234 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002235 goto error;
2236
2237 /* Get list length, and bow out early if empty. */
2238 if ((len = PyList_Size(obj)) < 0)
2239 goto error;
2240
2241 if (memo_put(self, obj) < 0)
2242 goto error;
2243
2244 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002245 /* Materialize the list elements. */
2246 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002247 if (Py_EnterRecursiveCall(" while pickling an object"))
2248 goto error;
2249 status = batch_list_exact(self, obj);
2250 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002251 } else {
2252 PyObject *iter = PyObject_GetIter(obj);
2253 if (iter == NULL)
2254 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002255
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002256 if (Py_EnterRecursiveCall(" while pickling an object")) {
2257 Py_DECREF(iter);
2258 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002259 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002260 status = batch_list(self, iter);
2261 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002262 Py_DECREF(iter);
2263 }
2264 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002265 if (0) {
2266 error:
2267 status = -1;
2268 }
2269
2270 if (self->fast && !fast_save_leave(self, obj))
2271 status = -1;
2272
2273 return status;
2274}
2275
2276/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2277 * MARK key value ... key value SETITEMS
2278 * opcode sequences. Calling code should have arranged to first create an
2279 * empty dict, or dict-like object, for the SETITEMS to operate on.
2280 * Returns 0 on success, <0 on error.
2281 *
2282 * This is very much like batch_list(). The difference between saving
2283 * elements directly, and picking apart two-tuples, is so long-winded at
2284 * the C level, though, that attempts to combine these routines were too
2285 * ugly to bear.
2286 */
2287static int
2288batch_dict(PicklerObject *self, PyObject *iter)
2289{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002290 PyObject *obj = NULL;
2291 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002292 int i, n;
2293
2294 const char mark_op = MARK;
2295 const char setitem_op = SETITEM;
2296 const char setitems_op = SETITEMS;
2297
2298 assert(iter != NULL);
2299
2300 if (self->proto == 0) {
2301 /* SETITEMS isn't available; do one at a time. */
2302 for (;;) {
2303 obj = PyIter_Next(iter);
2304 if (obj == NULL) {
2305 if (PyErr_Occurred())
2306 return -1;
2307 break;
2308 }
2309 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2310 PyErr_SetString(PyExc_TypeError, "dict items "
2311 "iterator must return 2-tuples");
2312 return -1;
2313 }
2314 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2315 if (i >= 0)
2316 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2317 Py_DECREF(obj);
2318 if (i < 0)
2319 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002320 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002321 return -1;
2322 }
2323 return 0;
2324 }
2325
2326 /* proto > 0: write in batches of BATCHSIZE. */
2327 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002328 /* Get first item */
2329 firstitem = PyIter_Next(iter);
2330 if (firstitem == NULL) {
2331 if (PyErr_Occurred())
2332 goto error;
2333
2334 /* nothing more to add */
2335 break;
2336 }
2337 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2338 PyErr_SetString(PyExc_TypeError, "dict items "
2339 "iterator must return 2-tuples");
2340 goto error;
2341 }
2342
2343 /* Try to get a second item */
2344 obj = PyIter_Next(iter);
2345 if (obj == NULL) {
2346 if (PyErr_Occurred())
2347 goto error;
2348
2349 /* Only one item to write */
2350 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2351 goto error;
2352 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2353 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002354 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002355 goto error;
2356 Py_CLEAR(firstitem);
2357 break;
2358 }
2359
2360 /* More than one item to write */
2361
2362 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002363 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002364 goto error;
2365
2366 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2367 goto error;
2368 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2369 goto error;
2370 Py_CLEAR(firstitem);
2371 n = 1;
2372
2373 /* Fetch and save up to BATCHSIZE items */
2374 while (obj) {
2375 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2376 PyErr_SetString(PyExc_TypeError, "dict items "
2377 "iterator must return 2-tuples");
2378 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002379 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002380 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2381 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2382 goto error;
2383 Py_CLEAR(obj);
2384 n += 1;
2385
2386 if (n == BATCHSIZE)
2387 break;
2388
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002389 obj = PyIter_Next(iter);
2390 if (obj == NULL) {
2391 if (PyErr_Occurred())
2392 goto error;
2393 break;
2394 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002395 }
2396
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002397 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002398 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002399
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002400 } while (n == BATCHSIZE);
2401 return 0;
2402
2403 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002404 Py_XDECREF(firstitem);
2405 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002406 return -1;
2407}
2408
Collin Winter5c9b02d2009-05-25 05:43:30 +00002409/* This is a variant of batch_dict() above that specializes for dicts, with no
2410 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2411 * MARK key value ... key value SETITEMS
2412 * opcode sequences. Calling code should have arranged to first create an
2413 * empty dict, or dict-like object, for the SETITEMS to operate on.
2414 * Returns 0 on success, -1 on error.
2415 *
2416 * Note that this currently doesn't work for protocol 0.
2417 */
2418static int
2419batch_dict_exact(PicklerObject *self, PyObject *obj)
2420{
2421 PyObject *key = NULL, *value = NULL;
2422 int i;
2423 Py_ssize_t dict_size, ppos = 0;
2424
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002425 const char mark_op = MARK;
2426 const char setitem_op = SETITEM;
2427 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002428
2429 assert(obj != NULL);
2430 assert(self->proto > 0);
2431
2432 dict_size = PyDict_Size(obj);
2433
2434 /* Special-case len(d) == 1 to save space. */
2435 if (dict_size == 1) {
2436 PyDict_Next(obj, &ppos, &key, &value);
2437 if (save(self, key, 0) < 0)
2438 return -1;
2439 if (save(self, value, 0) < 0)
2440 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002441 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002442 return -1;
2443 return 0;
2444 }
2445
2446 /* Write in batches of BATCHSIZE. */
2447 do {
2448 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002449 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002450 return -1;
2451 while (PyDict_Next(obj, &ppos, &key, &value)) {
2452 if (save(self, key, 0) < 0)
2453 return -1;
2454 if (save(self, value, 0) < 0)
2455 return -1;
2456 if (++i == BATCHSIZE)
2457 break;
2458 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002459 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002460 return -1;
2461 if (PyDict_Size(obj) != dict_size) {
2462 PyErr_Format(
2463 PyExc_RuntimeError,
2464 "dictionary changed size during iteration");
2465 return -1;
2466 }
2467
2468 } while (i == BATCHSIZE);
2469 return 0;
2470}
2471
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002472static int
2473save_dict(PicklerObject *self, PyObject *obj)
2474{
2475 PyObject *items, *iter;
2476 char header[3];
2477 int len;
2478 int status = 0;
2479
2480 if (self->fast && !fast_save_enter(self, obj))
2481 goto error;
2482
2483 /* Create an empty dict. */
2484 if (self->bin) {
2485 header[0] = EMPTY_DICT;
2486 len = 1;
2487 }
2488 else {
2489 header[0] = MARK;
2490 header[1] = DICT;
2491 len = 2;
2492 }
2493
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002494 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002495 goto error;
2496
2497 /* Get dict size, and bow out early if empty. */
2498 if ((len = PyDict_Size(obj)) < 0)
2499 goto error;
2500
2501 if (memo_put(self, obj) < 0)
2502 goto error;
2503
2504 if (len != 0) {
2505 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002506 if (PyDict_CheckExact(obj) && self->proto > 0) {
2507 /* We can take certain shortcuts if we know this is a dict and
2508 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002509 if (Py_EnterRecursiveCall(" while pickling an object"))
2510 goto error;
2511 status = batch_dict_exact(self, obj);
2512 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002513 } else {
2514 items = PyObject_CallMethod(obj, "items", "()");
2515 if (items == NULL)
2516 goto error;
2517 iter = PyObject_GetIter(items);
2518 Py_DECREF(items);
2519 if (iter == NULL)
2520 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002521 if (Py_EnterRecursiveCall(" while pickling an object")) {
2522 Py_DECREF(iter);
2523 goto error;
2524 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002525 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002526 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002527 Py_DECREF(iter);
2528 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002529 }
2530
2531 if (0) {
2532 error:
2533 status = -1;
2534 }
2535
2536 if (self->fast && !fast_save_leave(self, obj))
2537 status = -1;
2538
2539 return status;
2540}
2541
2542static int
2543save_global(PicklerObject *self, PyObject *obj, PyObject *name)
2544{
2545 static PyObject *name_str = NULL;
2546 PyObject *global_name = NULL;
2547 PyObject *module_name = NULL;
2548 PyObject *module = NULL;
2549 PyObject *cls;
2550 int status = 0;
2551
2552 const char global_op = GLOBAL;
2553
2554 if (name_str == NULL) {
2555 name_str = PyUnicode_InternFromString("__name__");
2556 if (name_str == NULL)
2557 goto error;
2558 }
2559
2560 if (name) {
2561 global_name = name;
2562 Py_INCREF(global_name);
2563 }
2564 else {
2565 global_name = PyObject_GetAttr(obj, name_str);
2566 if (global_name == NULL)
2567 goto error;
2568 }
2569
2570 module_name = whichmodule(obj, global_name);
2571 if (module_name == NULL)
2572 goto error;
2573
2574 /* XXX: Change to use the import C API directly with level=0 to disallow
2575 relative imports.
2576
2577 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
2578 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
2579 custom import functions (IMHO, this would be a nice security
2580 feature). The import C API would need to be extended to support the
2581 extra parameters of __import__ to fix that. */
2582 module = PyImport_Import(module_name);
2583 if (module == NULL) {
2584 PyErr_Format(PicklingError,
2585 "Can't pickle %R: import of module %R failed",
2586 obj, module_name);
2587 goto error;
2588 }
2589 cls = PyObject_GetAttr(module, global_name);
2590 if (cls == NULL) {
2591 PyErr_Format(PicklingError,
2592 "Can't pickle %R: attribute lookup %S.%S failed",
2593 obj, module_name, global_name);
2594 goto error;
2595 }
2596 if (cls != obj) {
2597 Py_DECREF(cls);
2598 PyErr_Format(PicklingError,
2599 "Can't pickle %R: it's not the same object as %S.%S",
2600 obj, module_name, global_name);
2601 goto error;
2602 }
2603 Py_DECREF(cls);
2604
2605 if (self->proto >= 2) {
2606 /* See whether this is in the extension registry, and if
2607 * so generate an EXT opcode.
2608 */
2609 PyObject *code_obj; /* extension code as Python object */
2610 long code; /* extension code as C value */
2611 char pdata[5];
2612 int n;
2613
2614 PyTuple_SET_ITEM(two_tuple, 0, module_name);
2615 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2616 code_obj = PyDict_GetItem(extension_registry, two_tuple);
2617 /* The object is not registered in the extension registry.
2618 This is the most likely code path. */
2619 if (code_obj == NULL)
2620 goto gen_global;
2621
2622 /* XXX: pickle.py doesn't check neither the type, nor the range
2623 of the value returned by the extension_registry. It should for
2624 consistency. */
2625
2626 /* Verify code_obj has the right type and value. */
2627 if (!PyLong_Check(code_obj)) {
2628 PyErr_Format(PicklingError,
2629 "Can't pickle %R: extension code %R isn't an integer",
2630 obj, code_obj);
2631 goto error;
2632 }
2633 code = PyLong_AS_LONG(code_obj);
2634 if (code <= 0 || code > 0x7fffffffL) {
2635 PyErr_Format(PicklingError,
2636 "Can't pickle %R: extension code %ld is out of range",
2637 obj, code);
2638 goto error;
2639 }
2640
2641 /* Generate an EXT opcode. */
2642 if (code <= 0xff) {
2643 pdata[0] = EXT1;
2644 pdata[1] = (unsigned char)code;
2645 n = 2;
2646 }
2647 else if (code <= 0xffff) {
2648 pdata[0] = EXT2;
2649 pdata[1] = (unsigned char)(code & 0xff);
2650 pdata[2] = (unsigned char)((code >> 8) & 0xff);
2651 n = 3;
2652 }
2653 else {
2654 pdata[0] = EXT4;
2655 pdata[1] = (unsigned char)(code & 0xff);
2656 pdata[2] = (unsigned char)((code >> 8) & 0xff);
2657 pdata[3] = (unsigned char)((code >> 16) & 0xff);
2658 pdata[4] = (unsigned char)((code >> 24) & 0xff);
2659 n = 5;
2660 }
2661
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002662 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002663 goto error;
2664 }
2665 else {
2666 /* Generate a normal global opcode if we are using a pickle
2667 protocol <= 2, or if the object is not registered in the
2668 extension registry. */
2669 PyObject *encoded;
2670 PyObject *(*unicode_encoder)(PyObject *);
2671
2672 gen_global:
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002673 if (_Pickler_Write(self, &global_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002674 goto error;
2675
2676 /* Since Python 3.0 now supports non-ASCII identifiers, we encode both
2677 the module name and the global name using UTF-8. We do so only when
2678 we are using the pickle protocol newer than version 3. This is to
2679 ensure compatibility with older Unpickler running on Python 2.x. */
2680 if (self->proto >= 3) {
2681 unicode_encoder = PyUnicode_AsUTF8String;
2682 }
2683 else {
2684 unicode_encoder = PyUnicode_AsASCIIString;
2685 }
2686
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00002687 /* For protocol < 3 and if the user didn't request against doing so,
2688 we convert module names to the old 2.x module names. */
2689 if (self->fix_imports) {
2690 PyObject *key;
2691 PyObject *item;
2692
2693 key = PyTuple_Pack(2, module_name, global_name);
2694 if (key == NULL)
2695 goto error;
2696 item = PyDict_GetItemWithError(name_mapping_3to2, key);
2697 Py_DECREF(key);
2698 if (item) {
2699 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
2700 PyErr_Format(PyExc_RuntimeError,
2701 "_compat_pickle.REVERSE_NAME_MAPPING values "
2702 "should be 2-tuples, not %.200s",
2703 Py_TYPE(item)->tp_name);
2704 goto error;
2705 }
2706 Py_CLEAR(module_name);
2707 Py_CLEAR(global_name);
2708 module_name = PyTuple_GET_ITEM(item, 0);
2709 global_name = PyTuple_GET_ITEM(item, 1);
2710 if (!PyUnicode_Check(module_name) ||
2711 !PyUnicode_Check(global_name)) {
2712 PyErr_Format(PyExc_RuntimeError,
2713 "_compat_pickle.REVERSE_NAME_MAPPING values "
2714 "should be pairs of str, not (%.200s, %.200s)",
2715 Py_TYPE(module_name)->tp_name,
2716 Py_TYPE(global_name)->tp_name);
2717 goto error;
2718 }
2719 Py_INCREF(module_name);
2720 Py_INCREF(global_name);
2721 }
2722 else if (PyErr_Occurred()) {
2723 goto error;
2724 }
2725
2726 item = PyDict_GetItemWithError(import_mapping_3to2, module_name);
2727 if (item) {
2728 if (!PyUnicode_Check(item)) {
2729 PyErr_Format(PyExc_RuntimeError,
2730 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
2731 "should be strings, not %.200s",
2732 Py_TYPE(item)->tp_name);
2733 goto error;
2734 }
2735 Py_CLEAR(module_name);
2736 module_name = item;
2737 Py_INCREF(module_name);
2738 }
2739 else if (PyErr_Occurred()) {
2740 goto error;
2741 }
2742 }
2743
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002744 /* Save the name of the module. */
2745 encoded = unicode_encoder(module_name);
2746 if (encoded == NULL) {
2747 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
2748 PyErr_Format(PicklingError,
2749 "can't pickle module identifier '%S' using "
2750 "pickle protocol %i", module_name, self->proto);
2751 goto error;
2752 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002753 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002754 PyBytes_GET_SIZE(encoded)) < 0) {
2755 Py_DECREF(encoded);
2756 goto error;
2757 }
2758 Py_DECREF(encoded);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002759 if(_Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002760 goto error;
2761
2762 /* Save the name of the module. */
2763 encoded = unicode_encoder(global_name);
2764 if (encoded == NULL) {
2765 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
2766 PyErr_Format(PicklingError,
2767 "can't pickle global identifier '%S' using "
2768 "pickle protocol %i", global_name, self->proto);
2769 goto error;
2770 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002771 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002772 PyBytes_GET_SIZE(encoded)) < 0) {
2773 Py_DECREF(encoded);
2774 goto error;
2775 }
2776 Py_DECREF(encoded);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002777 if(_Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002778 goto error;
2779
2780 /* Memoize the object. */
2781 if (memo_put(self, obj) < 0)
2782 goto error;
2783 }
2784
2785 if (0) {
2786 error:
2787 status = -1;
2788 }
2789 Py_XDECREF(module_name);
2790 Py_XDECREF(global_name);
2791 Py_XDECREF(module);
2792
2793 return status;
2794}
2795
2796static int
2797save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
2798{
2799 PyObject *pid = NULL;
2800 int status = 0;
2801
2802 const char persid_op = PERSID;
2803 const char binpersid_op = BINPERSID;
2804
2805 Py_INCREF(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002806 pid = _Pickler_FastCall(self, func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002807 if (pid == NULL)
2808 return -1;
2809
2810 if (pid != Py_None) {
2811 if (self->bin) {
2812 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002813 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002814 goto error;
2815 }
2816 else {
2817 PyObject *pid_str = NULL;
2818 char *pid_ascii_bytes;
2819 Py_ssize_t size;
2820
2821 pid_str = PyObject_Str(pid);
2822 if (pid_str == NULL)
2823 goto error;
2824
2825 /* XXX: Should it check whether the persistent id only contains
2826 ASCII characters? And what if the pid contains embedded
2827 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00002828 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002829 Py_DECREF(pid_str);
2830 if (pid_ascii_bytes == NULL)
2831 goto error;
2832
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002833 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
2834 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
2835 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002836 goto error;
2837 }
2838 status = 1;
2839 }
2840
2841 if (0) {
2842 error:
2843 status = -1;
2844 }
2845 Py_XDECREF(pid);
2846
2847 return status;
2848}
2849
2850/* We're saving obj, and args is the 2-thru-5 tuple returned by the
2851 * appropriate __reduce__ method for obj.
2852 */
2853static int
2854save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
2855{
2856 PyObject *callable;
2857 PyObject *argtup;
2858 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00002859 PyObject *listitems = Py_None;
2860 PyObject *dictitems = Py_None;
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00002861 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002862
2863 int use_newobj = self->proto >= 2;
2864
2865 const char reduce_op = REDUCE;
2866 const char build_op = BUILD;
2867 const char newobj_op = NEWOBJ;
2868
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00002869 size = PyTuple_Size(args);
2870 if (size < 2 || size > 5) {
2871 PyErr_SetString(PicklingError, "tuple returned by "
2872 "__reduce__ must contain 2 through 5 elements");
2873 return -1;
2874 }
2875
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002876 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2877 &callable, &argtup, &state, &listitems, &dictitems))
2878 return -1;
2879
2880 if (!PyCallable_Check(callable)) {
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00002881 PyErr_SetString(PicklingError, "first item of the tuple "
2882 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002883 return -1;
2884 }
2885 if (!PyTuple_Check(argtup)) {
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00002886 PyErr_SetString(PicklingError, "second item of the tuple "
2887 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002888 return -1;
2889 }
2890
2891 if (state == Py_None)
2892 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00002893
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002894 if (listitems == Py_None)
2895 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00002896 else if (!PyIter_Check(listitems)) {
2897 PyErr_Format(PicklingError, "Fourth element of tuple"
2898 "returned by __reduce__ must be an iterator, not %s",
2899 Py_TYPE(listitems)->tp_name);
2900 return -1;
2901 }
2902
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002903 if (dictitems == Py_None)
2904 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00002905 else if (!PyIter_Check(dictitems)) {
2906 PyErr_Format(PicklingError, "Fifth element of tuple"
2907 "returned by __reduce__ must be an iterator, not %s",
2908 Py_TYPE(dictitems)->tp_name);
2909 return -1;
2910 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002911
2912 /* Protocol 2 special case: if callable's name is __newobj__, use
2913 NEWOBJ. */
2914 if (use_newobj) {
Antoine Pitrouff150f22010-10-22 21:41:05 +00002915 static PyObject *newobj_str = NULL;
2916 PyObject *name_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002917
2918 if (newobj_str == NULL) {
2919 newobj_str = PyUnicode_InternFromString("__newobj__");
Antoine Pitrouff150f22010-10-22 21:41:05 +00002920 if (newobj_str == NULL)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002921 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002922 }
2923
Antoine Pitrouff150f22010-10-22 21:41:05 +00002924 name_str = PyObject_GetAttrString(callable, "__name__");
2925 if (name_str == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002926 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2927 PyErr_Clear();
2928 else
2929 return -1;
2930 use_newobj = 0;
2931 }
2932 else {
Antoine Pitrouff150f22010-10-22 21:41:05 +00002933 use_newobj = PyUnicode_Check(name_str) &&
2934 PyUnicode_Compare(name_str, newobj_str) == 0;
2935 Py_DECREF(name_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002936 }
2937 }
2938 if (use_newobj) {
2939 PyObject *cls;
2940 PyObject *newargtup;
2941 PyObject *obj_class;
2942 int p;
2943
2944 /* Sanity checks. */
2945 if (Py_SIZE(argtup) < 1) {
2946 PyErr_SetString(PicklingError, "__newobj__ arglist is empty");
2947 return -1;
2948 }
2949
2950 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrouff150f22010-10-22 21:41:05 +00002951 if (!PyObject_HasAttrString(cls, "__new__")) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002952 PyErr_SetString(PicklingError, "args[0] from "
Antoine Pitrouff150f22010-10-22 21:41:05 +00002953 "__newobj__ args has no __new__");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002954 return -1;
2955 }
2956
2957 if (obj != NULL) {
Antoine Pitrouff150f22010-10-22 21:41:05 +00002958 obj_class = PyObject_GetAttrString(obj, "__class__");
2959 if (obj_class == NULL) {
2960 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2961 PyErr_Clear();
2962 else
2963 return -1;
2964 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002965 p = obj_class != cls; /* true iff a problem */
2966 Py_DECREF(obj_class);
2967 if (p) {
2968 PyErr_SetString(PicklingError, "args[0] from "
2969 "__newobj__ args has the wrong class");
2970 return -1;
2971 }
2972 }
2973 /* XXX: These calls save() are prone to infinite recursion. Imagine
2974 what happen if the value returned by the __reduce__() method of
2975 some extension type contains another object of the same type. Ouch!
2976
2977 Here is a quick example, that I ran into, to illustrate what I
2978 mean:
2979
2980 >>> import pickle, copyreg
2981 >>> copyreg.dispatch_table.pop(complex)
2982 >>> pickle.dumps(1+2j)
2983 Traceback (most recent call last):
2984 ...
2985 RuntimeError: maximum recursion depth exceeded
2986
2987 Removing the complex class from copyreg.dispatch_table made the
2988 __reduce_ex__() method emit another complex object:
2989
2990 >>> (1+1j).__reduce_ex__(2)
2991 (<function __newobj__ at 0xb7b71c3c>,
2992 (<class 'complex'>, (1+1j)), None, None, None)
2993
2994 Thus when save() was called on newargstup (the 2nd item) recursion
2995 ensued. Of course, the bug was in the complex class which had a
2996 broken __getnewargs__() that emitted another complex object. But,
2997 the point, here, is it is quite easy to end up with a broken reduce
2998 function. */
2999
3000 /* Save the class and its __new__ arguments. */
3001 if (save(self, cls, 0) < 0)
3002 return -1;
3003
3004 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3005 if (newargtup == NULL)
3006 return -1;
3007
3008 p = save(self, newargtup, 0);
3009 Py_DECREF(newargtup);
3010 if (p < 0)
3011 return -1;
3012
3013 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003014 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003015 return -1;
3016 }
3017 else { /* Not using NEWOBJ. */
3018 if (save(self, callable, 0) < 0 ||
3019 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003020 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003021 return -1;
3022 }
3023
3024 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3025 the caller do not want to memoize the object. Not particularly useful,
3026 but that is to mimic the behavior save_reduce() in pickle.py when
3027 obj is None. */
3028 if (obj && memo_put(self, obj) < 0)
3029 return -1;
3030
3031 if (listitems && batch_list(self, listitems) < 0)
3032 return -1;
3033
3034 if (dictitems && batch_dict(self, dictitems) < 0)
3035 return -1;
3036
3037 if (state) {
3038 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003039 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003040 return -1;
3041 }
3042
3043 return 0;
3044}
3045
3046static int
3047save(PicklerObject *self, PyObject *obj, int pers_save)
3048{
3049 PyTypeObject *type;
3050 PyObject *reduce_func = NULL;
3051 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003052 int status = 0;
3053
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003054 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003055 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003056
3057 /* The extra pers_save argument is necessary to avoid calling save_pers()
3058 on its returned object. */
3059 if (!pers_save && self->pers_func) {
3060 /* save_pers() returns:
3061 -1 to signal an error;
3062 0 if it did nothing successfully;
3063 1 if a persistent id was saved.
3064 */
3065 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3066 goto done;
3067 }
3068
3069 type = Py_TYPE(obj);
3070
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003071 /* The old cPickle had an optimization that used switch-case statement
3072 dispatching on the first letter of the type name. This has was removed
3073 since benchmarks shown that this optimization was actually slowing
3074 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003075
3076 /* Atom types; these aren't memoized, so don't check the memo. */
3077
3078 if (obj == Py_None) {
3079 status = save_none(self, obj);
3080 goto done;
3081 }
3082 else if (obj == Py_False || obj == Py_True) {
3083 status = save_bool(self, obj);
3084 goto done;
3085 }
3086 else if (type == &PyLong_Type) {
3087 status = save_long(self, obj);
3088 goto done;
3089 }
3090 else if (type == &PyFloat_Type) {
3091 status = save_float(self, obj);
3092 goto done;
3093 }
3094
3095 /* Check the memo to see if it has the object. If so, generate
3096 a GET (or BINGET) opcode, instead of pickling the object
3097 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003098 if (PyMemoTable_Get(self->memo, obj)) {
3099 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003100 goto error;
3101 goto done;
3102 }
3103
3104 if (type == &PyBytes_Type) {
3105 status = save_bytes(self, obj);
3106 goto done;
3107 }
3108 else if (type == &PyUnicode_Type) {
3109 status = save_unicode(self, obj);
3110 goto done;
3111 }
3112 else if (type == &PyDict_Type) {
3113 status = save_dict(self, obj);
3114 goto done;
3115 }
3116 else if (type == &PyList_Type) {
3117 status = save_list(self, obj);
3118 goto done;
3119 }
3120 else if (type == &PyTuple_Type) {
3121 status = save_tuple(self, obj);
3122 goto done;
3123 }
3124 else if (type == &PyType_Type) {
3125 status = save_global(self, obj, NULL);
3126 goto done;
3127 }
3128 else if (type == &PyFunction_Type) {
3129 status = save_global(self, obj, NULL);
3130 if (status < 0 && PyErr_ExceptionMatches(PickleError)) {
3131 /* fall back to reduce */
3132 PyErr_Clear();
3133 }
3134 else {
3135 goto done;
3136 }
3137 }
3138 else if (type == &PyCFunction_Type) {
3139 status = save_global(self, obj, NULL);
3140 goto done;
3141 }
3142 else if (PyType_IsSubtype(type, &PyType_Type)) {
3143 status = save_global(self, obj, NULL);
3144 goto done;
3145 }
3146
3147 /* XXX: This part needs some unit tests. */
3148
3149 /* Get a reduction callable, and call it. This may come from
3150 * copyreg.dispatch_table, the object's __reduce_ex__ method,
3151 * or the object's __reduce__ method.
3152 */
3153 reduce_func = PyDict_GetItem(dispatch_table, (PyObject *)type);
3154 if (reduce_func != NULL) {
3155 /* Here, the reference count of the reduce_func object returned by
3156 PyDict_GetItem needs to be increased to be consistent with the one
3157 returned by PyObject_GetAttr. This is allow us to blindly DECREF
3158 reduce_func at the end of the save() routine.
3159 */
3160 Py_INCREF(reduce_func);
3161 Py_INCREF(obj);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003162 reduce_value = _Pickler_FastCall(self, reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003163 }
3164 else {
3165 static PyObject *reduce_str = NULL;
3166 static PyObject *reduce_ex_str = NULL;
3167
3168 /* Cache the name of the reduce methods. */
3169 if (reduce_str == NULL) {
3170 reduce_str = PyUnicode_InternFromString("__reduce__");
3171 if (reduce_str == NULL)
3172 goto error;
3173 reduce_ex_str = PyUnicode_InternFromString("__reduce_ex__");
3174 if (reduce_ex_str == NULL)
3175 goto error;
3176 }
3177
3178 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3179 automatically defined as __reduce__. While this is convenient, this
3180 make it impossible to know which method was actually called. Of
3181 course, this is not a big deal. But still, it would be nice to let
3182 the user know which method was called when something go
3183 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3184 don't actually have to check for a __reduce__ method. */
3185
3186 /* Check for a __reduce_ex__ method. */
3187 reduce_func = PyObject_GetAttr(obj, reduce_ex_str);
3188 if (reduce_func != NULL) {
3189 PyObject *proto;
3190 proto = PyLong_FromLong(self->proto);
3191 if (proto != NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003192 reduce_value = _Pickler_FastCall(self, reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003193 }
3194 }
3195 else {
3196 if (PyErr_ExceptionMatches(PyExc_AttributeError))
3197 PyErr_Clear();
3198 else
3199 goto error;
3200 /* Check for a __reduce__ method. */
3201 reduce_func = PyObject_GetAttr(obj, reduce_str);
3202 if (reduce_func != NULL) {
3203 reduce_value = PyObject_Call(reduce_func, empty_tuple, NULL);
3204 }
3205 else {
3206 PyErr_Format(PicklingError, "can't pickle '%.200s' object: %R",
3207 type->tp_name, obj);
3208 goto error;
3209 }
3210 }
3211 }
3212
3213 if (reduce_value == NULL)
3214 goto error;
3215
3216 if (PyUnicode_Check(reduce_value)) {
3217 status = save_global(self, obj, reduce_value);
3218 goto done;
3219 }
3220
3221 if (!PyTuple_Check(reduce_value)) {
3222 PyErr_SetString(PicklingError,
3223 "__reduce__ must return a string or tuple");
3224 goto error;
3225 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003226
3227 status = save_reduce(self, reduce_value, obj);
3228
3229 if (0) {
3230 error:
3231 status = -1;
3232 }
3233 done:
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003234 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003235 Py_XDECREF(reduce_func);
3236 Py_XDECREF(reduce_value);
3237
3238 return status;
3239}
3240
3241static int
3242dump(PicklerObject *self, PyObject *obj)
3243{
3244 const char stop_op = STOP;
3245
3246 if (self->proto >= 2) {
3247 char header[2];
3248
3249 header[0] = PROTO;
3250 assert(self->proto >= 0 && self->proto < 256);
3251 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003252 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003253 return -1;
3254 }
3255
3256 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003257 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003258 return -1;
3259
3260 return 0;
3261}
3262
3263PyDoc_STRVAR(Pickler_clear_memo_doc,
3264"clear_memo() -> None. Clears the pickler's \"memo\"."
3265"\n"
3266"The memo is the data structure that remembers which objects the\n"
3267"pickler has already seen, so that shared or recursive objects are\n"
3268"pickled by reference and not by value. This method is useful when\n"
3269"re-using picklers.");
3270
3271static PyObject *
3272Pickler_clear_memo(PicklerObject *self)
3273{
3274 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003275 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003276
3277 Py_RETURN_NONE;
3278}
3279
3280PyDoc_STRVAR(Pickler_dump_doc,
3281"dump(obj) -> None. Write a pickled representation of obj to the open file.");
3282
3283static PyObject *
3284Pickler_dump(PicklerObject *self, PyObject *args)
3285{
3286 PyObject *obj;
3287
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003288 /* Check whether the Pickler was initialized correctly (issue3664).
3289 Developers often forget to call __init__() in their subclasses, which
3290 would trigger a segfault without this check. */
3291 if (self->write == NULL) {
3292 PyErr_Format(PicklingError,
3293 "Pickler.__init__() was not called by %s.__init__()",
3294 Py_TYPE(self)->tp_name);
3295 return NULL;
3296 }
3297
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003298 if (!PyArg_ParseTuple(args, "O:dump", &obj))
3299 return NULL;
3300
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003301 if (_Pickler_ClearBuffer(self) < 0)
3302 return NULL;
3303
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003304 if (dump(self, obj) < 0)
3305 return NULL;
3306
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003307 if (_Pickler_FlushToFile(self) < 0)
3308 return NULL;
3309
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003310 Py_RETURN_NONE;
3311}
3312
3313static struct PyMethodDef Pickler_methods[] = {
3314 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
3315 Pickler_dump_doc},
3316 {"clear_memo", (PyCFunction)Pickler_clear_memo, METH_NOARGS,
3317 Pickler_clear_memo_doc},
3318 {NULL, NULL} /* sentinel */
3319};
3320
3321static void
3322Pickler_dealloc(PicklerObject *self)
3323{
3324 PyObject_GC_UnTrack(self);
3325
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003326 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003327 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003328 Py_XDECREF(self->pers_func);
3329 Py_XDECREF(self->arg);
3330 Py_XDECREF(self->fast_memo);
3331
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003332 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003333
3334 Py_TYPE(self)->tp_free((PyObject *)self);
3335}
3336
3337static int
3338Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
3339{
3340 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003341 Py_VISIT(self->pers_func);
3342 Py_VISIT(self->arg);
3343 Py_VISIT(self->fast_memo);
3344 return 0;
3345}
3346
3347static int
3348Pickler_clear(PicklerObject *self)
3349{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003350 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003351 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003352 Py_CLEAR(self->pers_func);
3353 Py_CLEAR(self->arg);
3354 Py_CLEAR(self->fast_memo);
3355
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003356 if (self->memo != NULL) {
3357 PyMemoTable *memo = self->memo;
3358 self->memo = NULL;
3359 PyMemoTable_Del(memo);
3360 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003361 return 0;
3362}
3363
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003364
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003365PyDoc_STRVAR(Pickler_doc,
3366"Pickler(file, protocol=None)"
3367"\n"
3368"This takes a binary file for writing a pickle data stream.\n"
3369"\n"
3370"The optional protocol argument tells the pickler to use the\n"
3371"given protocol; supported protocols are 0, 1, 2, 3. The default\n"
3372"protocol is 3; a backward-incompatible protocol designed for\n"
3373"Python 3.0.\n"
3374"\n"
3375"Specifying a negative protocol version selects the highest\n"
3376"protocol version supported. The higher the protocol used, the\n"
3377"more recent the version of Python needed to read the pickle\n"
3378"produced.\n"
3379"\n"
3380"The file argument must have a write() method that accepts a single\n"
3381"bytes argument. It can thus be a file object opened for binary\n"
3382"writing, a io.BytesIO instance, or any other custom object that\n"
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003383"meets this interface.\n"
3384"\n"
3385"If fix_imports is True and protocol is less than 3, pickle will try to\n"
3386"map the new Python 3.x names to the old module names used in Python\n"
3387"2.x, so that the pickle data stream is readable with Python 2.x.\n");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003388
3389static int
3390Pickler_init(PicklerObject *self, PyObject *args, PyObject *kwds)
3391{
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003392 static char *kwlist[] = {"file", "protocol", "fix_imports", 0};
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003393 PyObject *file;
3394 PyObject *proto_obj = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003395 PyObject *fix_imports = Py_True;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003396
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003397 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO:Pickler",
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003398 kwlist, &file, &proto_obj, &fix_imports))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003399 return -1;
3400
3401 /* In case of multiple __init__() calls, clear previous content. */
3402 if (self->write != NULL)
3403 (void)Pickler_clear(self);
3404
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003405 if (_Pickler_SetProtocol(self, proto_obj, fix_imports) < 0)
3406 return -1;
3407
3408 if (_Pickler_SetOutputStream(self, file) < 0)
3409 return -1;
3410
3411 /* memo and output_buffer may have already been created in _Pickler_New */
3412 if (self->memo == NULL) {
3413 self->memo = PyMemoTable_New();
3414 if (self->memo == NULL)
3415 return -1;
3416 }
3417 self->output_len = 0;
3418 if (self->output_buffer == NULL) {
3419 self->max_output_len = WRITE_BUF_SIZE;
3420 self->output_buffer = PyBytes_FromStringAndSize(NULL,
3421 self->max_output_len);
3422 if (self->output_buffer == NULL)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003423 return -1;
3424 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003425
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003426 self->arg = NULL;
3427 self->fast = 0;
3428 self->fast_nesting = 0;
3429 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003430 self->pers_func = NULL;
3431 if (PyObject_HasAttrString((PyObject *)self, "persistent_id")) {
3432 self->pers_func = PyObject_GetAttrString((PyObject *)self,
3433 "persistent_id");
3434 if (self->pers_func == NULL)
3435 return -1;
3436 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003437 return 0;
3438}
3439
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003440/* Define a proxy object for the Pickler's internal memo object. This is to
3441 * avoid breaking code like:
3442 * pickler.memo.clear()
3443 * and
3444 * pickler.memo = saved_memo
3445 * Is this a good idea? Not really, but we don't want to break code that uses
3446 * it. Note that we don't implement the entire mapping API here. This is
3447 * intentional, as these should be treated as black-box implementation details.
3448 */
3449
3450typedef struct {
3451 PyObject_HEAD
3452 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
3453} PicklerMemoProxyObject;
3454
3455PyDoc_STRVAR(pmp_clear_doc,
3456"memo.clear() -> None. Remove all items from memo.");
3457
3458static PyObject *
3459pmp_clear(PicklerMemoProxyObject *self)
3460{
3461 if (self->pickler->memo)
3462 PyMemoTable_Clear(self->pickler->memo);
3463 Py_RETURN_NONE;
3464}
3465
3466PyDoc_STRVAR(pmp_copy_doc,
3467"memo.copy() -> new_memo. Copy the memo to a new object.");
3468
3469static PyObject *
3470pmp_copy(PicklerMemoProxyObject *self)
3471{
3472 Py_ssize_t i;
3473 PyMemoTable *memo;
3474 PyObject *new_memo = PyDict_New();
3475 if (new_memo == NULL)
3476 return NULL;
3477
3478 memo = self->pickler->memo;
3479 for (i = 0; i < memo->mt_allocated; ++i) {
3480 PyMemoEntry entry = memo->mt_table[i];
3481 if (entry.me_key != NULL) {
3482 int status;
3483 PyObject *key, *value;
3484
3485 key = PyLong_FromVoidPtr(entry.me_key);
3486 value = Py_BuildValue("lO", entry.me_value, entry.me_key);
3487
3488 if (key == NULL || value == NULL) {
3489 Py_XDECREF(key);
3490 Py_XDECREF(value);
3491 goto error;
3492 }
3493 status = PyDict_SetItem(new_memo, key, value);
3494 Py_DECREF(key);
3495 Py_DECREF(value);
3496 if (status < 0)
3497 goto error;
3498 }
3499 }
3500 return new_memo;
3501
3502 error:
3503 Py_XDECREF(new_memo);
3504 return NULL;
3505}
3506
3507PyDoc_STRVAR(pmp_reduce_doc,
3508"memo.__reduce__(). Pickling support.");
3509
3510static PyObject *
3511pmp_reduce(PicklerMemoProxyObject *self, PyObject *args)
3512{
3513 PyObject *reduce_value, *dict_args;
3514 PyObject *contents = pmp_copy(self);
3515 if (contents == NULL)
3516 return NULL;
3517
3518 reduce_value = PyTuple_New(2);
3519 if (reduce_value == NULL) {
3520 Py_DECREF(contents);
3521 return NULL;
3522 }
3523 dict_args = PyTuple_New(1);
3524 if (dict_args == NULL) {
3525 Py_DECREF(contents);
3526 Py_DECREF(reduce_value);
3527 return NULL;
3528 }
3529 PyTuple_SET_ITEM(dict_args, 0, contents);
3530 Py_INCREF((PyObject *)&PyDict_Type);
3531 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
3532 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
3533 return reduce_value;
3534}
3535
3536static PyMethodDef picklerproxy_methods[] = {
3537 {"clear", (PyCFunction)pmp_clear, METH_NOARGS, pmp_clear_doc},
3538 {"copy", (PyCFunction)pmp_copy, METH_NOARGS, pmp_copy_doc},
3539 {"__reduce__", (PyCFunction)pmp_reduce, METH_VARARGS, pmp_reduce_doc},
3540 {NULL, NULL} /* sentinel */
3541};
3542
3543static void
3544PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
3545{
3546 PyObject_GC_UnTrack(self);
3547 Py_XDECREF(self->pickler);
3548 PyObject_GC_Del((PyObject *)self);
3549}
3550
3551static int
3552PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
3553 visitproc visit, void *arg)
3554{
3555 Py_VISIT(self->pickler);
3556 return 0;
3557}
3558
3559static int
3560PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
3561{
3562 Py_CLEAR(self->pickler);
3563 return 0;
3564}
3565
3566static PyTypeObject PicklerMemoProxyType = {
3567 PyVarObject_HEAD_INIT(NULL, 0)
3568 "_pickle.PicklerMemoProxy", /*tp_name*/
3569 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
3570 0,
3571 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
3572 0, /* tp_print */
3573 0, /* tp_getattr */
3574 0, /* tp_setattr */
3575 0, /* tp_compare */
3576 0, /* tp_repr */
3577 0, /* tp_as_number */
3578 0, /* tp_as_sequence */
3579 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00003580 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003581 0, /* tp_call */
3582 0, /* tp_str */
3583 PyObject_GenericGetAttr, /* tp_getattro */
3584 PyObject_GenericSetAttr, /* tp_setattro */
3585 0, /* tp_as_buffer */
3586 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3587 0, /* tp_doc */
3588 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
3589 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
3590 0, /* tp_richcompare */
3591 0, /* tp_weaklistoffset */
3592 0, /* tp_iter */
3593 0, /* tp_iternext */
3594 picklerproxy_methods, /* tp_methods */
3595};
3596
3597static PyObject *
3598PicklerMemoProxy_New(PicklerObject *pickler)
3599{
3600 PicklerMemoProxyObject *self;
3601
3602 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
3603 if (self == NULL)
3604 return NULL;
3605 Py_INCREF(pickler);
3606 self->pickler = pickler;
3607 PyObject_GC_Track(self);
3608 return (PyObject *)self;
3609}
3610
3611/*****************************************************************************/
3612
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003613static PyObject *
3614Pickler_get_memo(PicklerObject *self)
3615{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003616 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003617}
3618
3619static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003620Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003621{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003622 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003623
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003624 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003625 PyErr_SetString(PyExc_TypeError,
3626 "attribute deletion is not supported");
3627 return -1;
3628 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003629
3630 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
3631 PicklerObject *pickler =
3632 ((PicklerMemoProxyObject *)obj)->pickler;
3633
3634 new_memo = PyMemoTable_Copy(pickler->memo);
3635 if (new_memo == NULL)
3636 return -1;
3637 }
3638 else if (PyDict_Check(obj)) {
3639 Py_ssize_t i = 0;
3640 PyObject *key, *value;
3641
3642 new_memo = PyMemoTable_New();
3643 if (new_memo == NULL)
3644 return -1;
3645
3646 while (PyDict_Next(obj, &i, &key, &value)) {
3647 long memo_id;
3648 PyObject *memo_obj;
3649
3650 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
3651 PyErr_SetString(PyExc_TypeError,
3652 "'memo' values must be 2-item tuples");
3653 goto error;
3654 }
3655 memo_id = PyLong_AsLong(PyTuple_GET_ITEM(value, 0));
3656 if (memo_id == -1 && PyErr_Occurred())
3657 goto error;
3658 memo_obj = PyTuple_GET_ITEM(value, 1);
3659 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
3660 goto error;
3661 }
3662 }
3663 else {
3664 PyErr_Format(PyExc_TypeError,
3665 "'memo' attribute must be an PicklerMemoProxy object"
3666 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003667 return -1;
3668 }
3669
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003670 PyMemoTable_Del(self->memo);
3671 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003672
3673 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003674
3675 error:
3676 if (new_memo)
3677 PyMemoTable_Del(new_memo);
3678 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003679}
3680
3681static PyObject *
3682Pickler_get_persid(PicklerObject *self)
3683{
3684 if (self->pers_func == NULL)
3685 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3686 else
3687 Py_INCREF(self->pers_func);
3688 return self->pers_func;
3689}
3690
3691static int
3692Pickler_set_persid(PicklerObject *self, PyObject *value)
3693{
3694 PyObject *tmp;
3695
3696 if (value == NULL) {
3697 PyErr_SetString(PyExc_TypeError,
3698 "attribute deletion is not supported");
3699 return -1;
3700 }
3701 if (!PyCallable_Check(value)) {
3702 PyErr_SetString(PyExc_TypeError,
3703 "persistent_id must be a callable taking one argument");
3704 return -1;
3705 }
3706
3707 tmp = self->pers_func;
3708 Py_INCREF(value);
3709 self->pers_func = value;
3710 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
3711
3712 return 0;
3713}
3714
3715static PyMemberDef Pickler_members[] = {
3716 {"bin", T_INT, offsetof(PicklerObject, bin)},
3717 {"fast", T_INT, offsetof(PicklerObject, fast)},
3718 {NULL}
3719};
3720
3721static PyGetSetDef Pickler_getsets[] = {
3722 {"memo", (getter)Pickler_get_memo,
3723 (setter)Pickler_set_memo},
3724 {"persistent_id", (getter)Pickler_get_persid,
3725 (setter)Pickler_set_persid},
3726 {NULL}
3727};
3728
3729static PyTypeObject Pickler_Type = {
3730 PyVarObject_HEAD_INIT(NULL, 0)
3731 "_pickle.Pickler" , /*tp_name*/
3732 sizeof(PicklerObject), /*tp_basicsize*/
3733 0, /*tp_itemsize*/
3734 (destructor)Pickler_dealloc, /*tp_dealloc*/
3735 0, /*tp_print*/
3736 0, /*tp_getattr*/
3737 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00003738 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003739 0, /*tp_repr*/
3740 0, /*tp_as_number*/
3741 0, /*tp_as_sequence*/
3742 0, /*tp_as_mapping*/
3743 0, /*tp_hash*/
3744 0, /*tp_call*/
3745 0, /*tp_str*/
3746 0, /*tp_getattro*/
3747 0, /*tp_setattro*/
3748 0, /*tp_as_buffer*/
3749 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3750 Pickler_doc, /*tp_doc*/
3751 (traverseproc)Pickler_traverse, /*tp_traverse*/
3752 (inquiry)Pickler_clear, /*tp_clear*/
3753 0, /*tp_richcompare*/
3754 0, /*tp_weaklistoffset*/
3755 0, /*tp_iter*/
3756 0, /*tp_iternext*/
3757 Pickler_methods, /*tp_methods*/
3758 Pickler_members, /*tp_members*/
3759 Pickler_getsets, /*tp_getset*/
3760 0, /*tp_base*/
3761 0, /*tp_dict*/
3762 0, /*tp_descr_get*/
3763 0, /*tp_descr_set*/
3764 0, /*tp_dictoffset*/
3765 (initproc)Pickler_init, /*tp_init*/
3766 PyType_GenericAlloc, /*tp_alloc*/
3767 PyType_GenericNew, /*tp_new*/
3768 PyObject_GC_Del, /*tp_free*/
3769 0, /*tp_is_gc*/
3770};
3771
3772/* Temporary helper for calling self.find_class().
3773
3774 XXX: It would be nice to able to avoid Python function call overhead, by
3775 using directly the C version of find_class(), when find_class() is not
3776 overridden by a subclass. Although, this could become rather hackish. A
3777 simpler optimization would be to call the C function when self is not a
3778 subclass instance. */
3779static PyObject *
3780find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
3781{
3782 return PyObject_CallMethod((PyObject *)self, "find_class", "OO",
3783 module_name, global_name);
3784}
3785
3786static int
3787marker(UnpicklerObject *self)
3788{
3789 if (self->num_marks < 1) {
3790 PyErr_SetString(UnpicklingError, "could not find MARK");
3791 return -1;
3792 }
3793
3794 return self->marks[--self->num_marks];
3795}
3796
3797static int
3798load_none(UnpicklerObject *self)
3799{
3800 PDATA_APPEND(self->stack, Py_None, -1);
3801 return 0;
3802}
3803
3804static int
3805bad_readline(void)
3806{
3807 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3808 return -1;
3809}
3810
3811static int
3812load_int(UnpicklerObject *self)
3813{
3814 PyObject *value;
3815 char *endptr, *s;
3816 Py_ssize_t len;
3817 long x;
3818
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003819 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003820 return -1;
3821 if (len < 2)
3822 return bad_readline();
3823
3824 errno = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003825 /* XXX: Should the base argument of strtol() be explicitly set to 10?
3826 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003827 x = strtol(s, &endptr, 0);
3828
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003829 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003830 /* Hm, maybe we've got something long. Let's try reading
3831 * it as a Python long object. */
3832 errno = 0;
3833 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003834 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003835 if (value == NULL) {
3836 PyErr_SetString(PyExc_ValueError,
3837 "could not convert string to int");
3838 return -1;
3839 }
3840 }
3841 else {
3842 if (len == 3 && (x == 0 || x == 1)) {
3843 if ((value = PyBool_FromLong(x)) == NULL)
3844 return -1;
3845 }
3846 else {
3847 if ((value = PyLong_FromLong(x)) == NULL)
3848 return -1;
3849 }
3850 }
3851
3852 PDATA_PUSH(self->stack, value, -1);
3853 return 0;
3854}
3855
3856static int
3857load_bool(UnpicklerObject *self, PyObject *boolean)
3858{
3859 assert(boolean == Py_True || boolean == Py_False);
3860 PDATA_APPEND(self->stack, boolean, -1);
3861 return 0;
3862}
3863
3864/* s contains x bytes of a little-endian integer. Return its value as a
3865 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3866 * int, but when x is 4 it's a signed one. This is an historical source
3867 * of x-platform bugs.
3868 */
3869static long
3870calc_binint(char *bytes, int size)
3871{
3872 unsigned char *s = (unsigned char *)bytes;
3873 int i = size;
3874 long x = 0;
3875
3876 for (i = 0; i < size; i++) {
3877 x |= (long)s[i] << (i * 8);
3878 }
3879
3880 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3881 * is signed, so on a box with longs bigger than 4 bytes we need
3882 * to extend a BININT's sign bit to the full width.
3883 */
3884 if (SIZEOF_LONG > 4 && size == 4) {
3885 x |= -(x & (1L << 31));
3886 }
3887
3888 return x;
3889}
3890
3891static int
3892load_binintx(UnpicklerObject *self, char *s, int size)
3893{
3894 PyObject *value;
3895 long x;
3896
3897 x = calc_binint(s, size);
3898
3899 if ((value = PyLong_FromLong(x)) == NULL)
3900 return -1;
3901
3902 PDATA_PUSH(self->stack, value, -1);
3903 return 0;
3904}
3905
3906static int
3907load_binint(UnpicklerObject *self)
3908{
3909 char *s;
3910
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003911 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003912 return -1;
3913
3914 return load_binintx(self, s, 4);
3915}
3916
3917static int
3918load_binint1(UnpicklerObject *self)
3919{
3920 char *s;
3921
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003922 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003923 return -1;
3924
3925 return load_binintx(self, s, 1);
3926}
3927
3928static int
3929load_binint2(UnpicklerObject *self)
3930{
3931 char *s;
3932
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003933 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003934 return -1;
3935
3936 return load_binintx(self, s, 2);
3937}
3938
3939static int
3940load_long(UnpicklerObject *self)
3941{
3942 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00003943 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003944 Py_ssize_t len;
3945
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003946 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003947 return -1;
3948 if (len < 2)
3949 return bad_readline();
3950
Mark Dickinson8dd05142009-01-20 20:43:58 +00003951 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
3952 the 'L' before calling PyLong_FromString. In order to maintain
3953 compatibility with Python 3.0.0, we don't actually *require*
3954 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003955 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00003956 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00003957 /* XXX: Should the base argument explicitly set to 10? */
3958 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00003959 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003960 return -1;
3961
3962 PDATA_PUSH(self->stack, value, -1);
3963 return 0;
3964}
3965
3966/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3967 * data following.
3968 */
3969static int
3970load_counted_long(UnpicklerObject *self, int size)
3971{
3972 PyObject *value;
3973 char *nbytes;
3974 char *pdata;
3975
3976 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003977 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003978 return -1;
3979
3980 size = calc_binint(nbytes, size);
3981 if (size < 0) {
3982 /* Corrupt or hostile pickle -- we never write one like this */
3983 PyErr_SetString(UnpicklingError,
3984 "LONG pickle has negative byte count");
3985 return -1;
3986 }
3987
3988 if (size == 0)
3989 value = PyLong_FromLong(0L);
3990 else {
3991 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003992 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003993 return -1;
3994 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
3995 1 /* little endian */ , 1 /* signed */ );
3996 }
3997 if (value == NULL)
3998 return -1;
3999 PDATA_PUSH(self->stack, value, -1);
4000 return 0;
4001}
4002
4003static int
4004load_float(UnpicklerObject *self)
4005{
4006 PyObject *value;
4007 char *endptr, *s;
4008 Py_ssize_t len;
4009 double d;
4010
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004011 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004012 return -1;
4013 if (len < 2)
4014 return bad_readline();
4015
4016 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004017 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4018 if (d == -1.0 && PyErr_Occurred())
4019 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004020 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004021 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4022 return -1;
4023 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004024 value = PyFloat_FromDouble(d);
4025 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004026 return -1;
4027
4028 PDATA_PUSH(self->stack, value, -1);
4029 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004030}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004031
4032static int
4033load_binfloat(UnpicklerObject *self)
4034{
4035 PyObject *value;
4036 double x;
4037 char *s;
4038
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004039 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004040 return -1;
4041
4042 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4043 if (x == -1.0 && PyErr_Occurred())
4044 return -1;
4045
4046 if ((value = PyFloat_FromDouble(x)) == NULL)
4047 return -1;
4048
4049 PDATA_PUSH(self->stack, value, -1);
4050 return 0;
4051}
4052
4053static int
4054load_string(UnpicklerObject *self)
4055{
4056 PyObject *bytes;
4057 PyObject *str = NULL;
4058 Py_ssize_t len;
4059 char *s, *p;
4060
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004061 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004062 return -1;
4063 if (len < 3)
4064 return bad_readline();
4065 if ((s = strdup(s)) == NULL) {
4066 PyErr_NoMemory();
4067 return -1;
4068 }
4069
4070 /* Strip outermost quotes */
4071 while (s[len - 1] <= ' ')
4072 len--;
4073 if (s[0] == '"' && s[len - 1] == '"') {
4074 s[len - 1] = '\0';
4075 p = s + 1;
4076 len -= 2;
4077 }
4078 else if (s[0] == '\'' && s[len - 1] == '\'') {
4079 s[len - 1] = '\0';
4080 p = s + 1;
4081 len -= 2;
4082 }
4083 else {
4084 free(s);
4085 PyErr_SetString(PyExc_ValueError, "insecure string pickle");
4086 return -1;
4087 }
4088
4089 /* Use the PyBytes API to decode the string, since that is what is used
4090 to encode, and then coerce the result to Unicode. */
4091 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
4092 free(s);
4093 if (bytes == NULL)
4094 return -1;
4095 str = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4096 Py_DECREF(bytes);
4097 if (str == NULL)
4098 return -1;
4099
4100 PDATA_PUSH(self->stack, str, -1);
4101 return 0;
4102}
4103
4104static int
4105load_binbytes(UnpicklerObject *self)
4106{
4107 PyObject *bytes;
4108 long x;
4109 char *s;
4110
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004111 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004112 return -1;
4113
4114 x = calc_binint(s, 4);
4115 if (x < 0) {
4116 PyErr_SetString(UnpicklingError,
4117 "BINBYTES pickle has negative byte count");
4118 return -1;
4119 }
4120
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004121 if (_Unpickler_Read(self, &s, x) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004122 return -1;
4123 bytes = PyBytes_FromStringAndSize(s, x);
4124 if (bytes == NULL)
4125 return -1;
4126
4127 PDATA_PUSH(self->stack, bytes, -1);
4128 return 0;
4129}
4130
4131static int
4132load_short_binbytes(UnpicklerObject *self)
4133{
4134 PyObject *bytes;
4135 unsigned char x;
4136 char *s;
4137
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004138 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004139 return -1;
4140
4141 x = (unsigned char)s[0];
4142
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004143 if (_Unpickler_Read(self, &s, x) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004144 return -1;
4145
4146 bytes = PyBytes_FromStringAndSize(s, x);
4147 if (bytes == NULL)
4148 return -1;
4149
4150 PDATA_PUSH(self->stack, bytes, -1);
4151 return 0;
4152}
4153
4154static int
4155load_binstring(UnpicklerObject *self)
4156{
4157 PyObject *str;
4158 long x;
4159 char *s;
4160
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004161 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004162 return -1;
4163
4164 x = calc_binint(s, 4);
4165 if (x < 0) {
4166 PyErr_SetString(UnpicklingError,
4167 "BINSTRING pickle has negative byte count");
4168 return -1;
4169 }
4170
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004171 if (_Unpickler_Read(self, &s, x) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004172 return -1;
4173
4174 /* Convert Python 2.x strings to unicode. */
4175 str = PyUnicode_Decode(s, x, self->encoding, self->errors);
4176 if (str == NULL)
4177 return -1;
4178
4179 PDATA_PUSH(self->stack, str, -1);
4180 return 0;
4181}
4182
4183static int
4184load_short_binstring(UnpicklerObject *self)
4185{
4186 PyObject *str;
4187 unsigned char x;
4188 char *s;
4189
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004190 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004191 return -1;
4192
4193 x = (unsigned char)s[0];
4194
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004195 if (_Unpickler_Read(self, &s, x) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004196 return -1;
4197
4198 /* Convert Python 2.x strings to unicode. */
4199 str = PyUnicode_Decode(s, x, self->encoding, self->errors);
4200 if (str == NULL)
4201 return -1;
4202
4203 PDATA_PUSH(self->stack, str, -1);
4204 return 0;
4205}
4206
4207static int
4208load_unicode(UnpicklerObject *self)
4209{
4210 PyObject *str;
4211 Py_ssize_t len;
4212 char *s;
4213
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004214 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004215 return -1;
4216 if (len < 1)
4217 return bad_readline();
4218
4219 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
4220 if (str == NULL)
4221 return -1;
4222
4223 PDATA_PUSH(self->stack, str, -1);
4224 return 0;
4225}
4226
4227static int
4228load_binunicode(UnpicklerObject *self)
4229{
4230 PyObject *str;
4231 long size;
4232 char *s;
4233
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004234 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004235 return -1;
4236
4237 size = calc_binint(s, 4);
4238 if (size < 0) {
4239 PyErr_SetString(UnpicklingError,
4240 "BINUNICODE pickle has negative byte count");
4241 return -1;
4242 }
4243
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004244 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004245 return -1;
4246
Victor Stinner485fb562010-04-13 11:07:24 +00004247 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004248 if (str == NULL)
4249 return -1;
4250
4251 PDATA_PUSH(self->stack, str, -1);
4252 return 0;
4253}
4254
4255static int
4256load_tuple(UnpicklerObject *self)
4257{
4258 PyObject *tuple;
4259 int i;
4260
4261 if ((i = marker(self)) < 0)
4262 return -1;
4263
4264 tuple = Pdata_poptuple(self->stack, i);
4265 if (tuple == NULL)
4266 return -1;
4267 PDATA_PUSH(self->stack, tuple, -1);
4268 return 0;
4269}
4270
4271static int
4272load_counted_tuple(UnpicklerObject *self, int len)
4273{
4274 PyObject *tuple;
4275
4276 tuple = PyTuple_New(len);
4277 if (tuple == NULL)
4278 return -1;
4279
4280 while (--len >= 0) {
4281 PyObject *item;
4282
4283 PDATA_POP(self->stack, item);
4284 if (item == NULL)
4285 return -1;
4286 PyTuple_SET_ITEM(tuple, len, item);
4287 }
4288 PDATA_PUSH(self->stack, tuple, -1);
4289 return 0;
4290}
4291
4292static int
4293load_empty_list(UnpicklerObject *self)
4294{
4295 PyObject *list;
4296
4297 if ((list = PyList_New(0)) == NULL)
4298 return -1;
4299 PDATA_PUSH(self->stack, list, -1);
4300 return 0;
4301}
4302
4303static int
4304load_empty_dict(UnpicklerObject *self)
4305{
4306 PyObject *dict;
4307
4308 if ((dict = PyDict_New()) == NULL)
4309 return -1;
4310 PDATA_PUSH(self->stack, dict, -1);
4311 return 0;
4312}
4313
4314static int
4315load_list(UnpicklerObject *self)
4316{
4317 PyObject *list;
4318 int i;
4319
4320 if ((i = marker(self)) < 0)
4321 return -1;
4322
4323 list = Pdata_poplist(self->stack, i);
4324 if (list == NULL)
4325 return -1;
4326 PDATA_PUSH(self->stack, list, -1);
4327 return 0;
4328}
4329
4330static int
4331load_dict(UnpicklerObject *self)
4332{
4333 PyObject *dict, *key, *value;
4334 int i, j, k;
4335
4336 if ((i = marker(self)) < 0)
4337 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004338 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004339
4340 if ((dict = PyDict_New()) == NULL)
4341 return -1;
4342
4343 for (k = i + 1; k < j; k += 2) {
4344 key = self->stack->data[k - 1];
4345 value = self->stack->data[k];
4346 if (PyDict_SetItem(dict, key, value) < 0) {
4347 Py_DECREF(dict);
4348 return -1;
4349 }
4350 }
4351 Pdata_clear(self->stack, i);
4352 PDATA_PUSH(self->stack, dict, -1);
4353 return 0;
4354}
4355
4356static PyObject *
4357instantiate(PyObject *cls, PyObject *args)
4358{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00004359 PyObject *result = NULL;
4360 /* Caller must assure args are a tuple. Normally, args come from
4361 Pdata_poptuple which packs objects from the top of the stack
4362 into a newly created tuple. */
4363 assert(PyTuple_Check(args));
4364 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
4365 PyObject_HasAttrString(cls, "__getinitargs__")) {
4366 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004367 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00004368 else {
4369 result = PyObject_CallMethod(cls, "__new__", "O", cls);
4370 }
4371 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004372}
4373
4374static int
4375load_obj(UnpicklerObject *self)
4376{
4377 PyObject *cls, *args, *obj = NULL;
4378 int i;
4379
4380 if ((i = marker(self)) < 0)
4381 return -1;
4382
4383 args = Pdata_poptuple(self->stack, i + 1);
4384 if (args == NULL)
4385 return -1;
4386
4387 PDATA_POP(self->stack, cls);
4388 if (cls) {
4389 obj = instantiate(cls, args);
4390 Py_DECREF(cls);
4391 }
4392 Py_DECREF(args);
4393 if (obj == NULL)
4394 return -1;
4395
4396 PDATA_PUSH(self->stack, obj, -1);
4397 return 0;
4398}
4399
4400static int
4401load_inst(UnpicklerObject *self)
4402{
4403 PyObject *cls = NULL;
4404 PyObject *args = NULL;
4405 PyObject *obj = NULL;
4406 PyObject *module_name;
4407 PyObject *class_name;
4408 Py_ssize_t len;
4409 int i;
4410 char *s;
4411
4412 if ((i = marker(self)) < 0)
4413 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004414 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004415 return -1;
4416 if (len < 2)
4417 return bad_readline();
4418
4419 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
4420 identifiers are permitted in Python 3.0, since the INST opcode is only
4421 supported by older protocols on Python 2.x. */
4422 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
4423 if (module_name == NULL)
4424 return -1;
4425
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004426 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004427 if (len < 2)
4428 return bad_readline();
4429 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00004430 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004431 cls = find_class(self, module_name, class_name);
4432 Py_DECREF(class_name);
4433 }
4434 }
4435 Py_DECREF(module_name);
4436
4437 if (cls == NULL)
4438 return -1;
4439
4440 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
4441 obj = instantiate(cls, args);
4442 Py_DECREF(args);
4443 }
4444 Py_DECREF(cls);
4445
4446 if (obj == NULL)
4447 return -1;
4448
4449 PDATA_PUSH(self->stack, obj, -1);
4450 return 0;
4451}
4452
4453static int
4454load_newobj(UnpicklerObject *self)
4455{
4456 PyObject *args = NULL;
4457 PyObject *clsraw = NULL;
4458 PyTypeObject *cls; /* clsraw cast to its true type */
4459 PyObject *obj;
4460
4461 /* Stack is ... cls argtuple, and we want to call
4462 * cls.__new__(cls, *argtuple).
4463 */
4464 PDATA_POP(self->stack, args);
4465 if (args == NULL)
4466 goto error;
4467 if (!PyTuple_Check(args)) {
4468 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg " "tuple.");
4469 goto error;
4470 }
4471
4472 PDATA_POP(self->stack, clsraw);
4473 cls = (PyTypeObject *)clsraw;
4474 if (cls == NULL)
4475 goto error;
4476 if (!PyType_Check(cls)) {
4477 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
4478 "isn't a type object");
4479 goto error;
4480 }
4481 if (cls->tp_new == NULL) {
4482 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
4483 "has NULL tp_new");
4484 goto error;
4485 }
4486
4487 /* Call __new__. */
4488 obj = cls->tp_new(cls, args, NULL);
4489 if (obj == NULL)
4490 goto error;
4491
4492 Py_DECREF(args);
4493 Py_DECREF(clsraw);
4494 PDATA_PUSH(self->stack, obj, -1);
4495 return 0;
4496
4497 error:
4498 Py_XDECREF(args);
4499 Py_XDECREF(clsraw);
4500 return -1;
4501}
4502
4503static int
4504load_global(UnpicklerObject *self)
4505{
4506 PyObject *global = NULL;
4507 PyObject *module_name;
4508 PyObject *global_name;
4509 Py_ssize_t len;
4510 char *s;
4511
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004512 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004513 return -1;
4514 if (len < 2)
4515 return bad_readline();
4516 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
4517 if (!module_name)
4518 return -1;
4519
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004520 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004521 if (len < 2) {
4522 Py_DECREF(module_name);
4523 return bad_readline();
4524 }
4525 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
4526 if (global_name) {
4527 global = find_class(self, module_name, global_name);
4528 Py_DECREF(global_name);
4529 }
4530 }
4531 Py_DECREF(module_name);
4532
4533 if (global == NULL)
4534 return -1;
4535 PDATA_PUSH(self->stack, global, -1);
4536 return 0;
4537}
4538
4539static int
4540load_persid(UnpicklerObject *self)
4541{
4542 PyObject *pid;
4543 Py_ssize_t len;
4544 char *s;
4545
4546 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004547 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004548 return -1;
4549 if (len < 2)
4550 return bad_readline();
4551
4552 pid = PyBytes_FromStringAndSize(s, len - 1);
4553 if (pid == NULL)
4554 return -1;
4555
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004556 /* Ugh... this does not leak since _Unpickler_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004557 reference to pid first. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004558 pid = _Unpickler_FastCall(self, self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004559 if (pid == NULL)
4560 return -1;
4561
4562 PDATA_PUSH(self->stack, pid, -1);
4563 return 0;
4564 }
4565 else {
4566 PyErr_SetString(UnpicklingError,
4567 "A load persistent id instruction was encountered,\n"
4568 "but no persistent_load function was specified.");
4569 return -1;
4570 }
4571}
4572
4573static int
4574load_binpersid(UnpicklerObject *self)
4575{
4576 PyObject *pid;
4577
4578 if (self->pers_func) {
4579 PDATA_POP(self->stack, pid);
4580 if (pid == NULL)
4581 return -1;
4582
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004583 /* Ugh... this does not leak since _Unpickler_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004584 reference to pid first. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004585 pid = _Unpickler_FastCall(self, self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004586 if (pid == NULL)
4587 return -1;
4588
4589 PDATA_PUSH(self->stack, pid, -1);
4590 return 0;
4591 }
4592 else {
4593 PyErr_SetString(UnpicklingError,
4594 "A load persistent id instruction was encountered,\n"
4595 "but no persistent_load function was specified.");
4596 return -1;
4597 }
4598}
4599
4600static int
4601load_pop(UnpicklerObject *self)
4602{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004603 int len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004604
4605 /* Note that we split the (pickle.py) stack into two stacks,
4606 * an object stack and a mark stack. We have to be clever and
4607 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00004608 * mark stack first, and only signalling a stack underflow if
4609 * the object stack is empty and the mark stack doesn't match
4610 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004611 */
Collin Winter8ca69de2009-05-26 16:53:41 +00004612 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004613 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00004614 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004615 len--;
4616 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004617 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00004618 } else {
4619 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004620 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004621 return 0;
4622}
4623
4624static int
4625load_pop_mark(UnpicklerObject *self)
4626{
4627 int i;
4628
4629 if ((i = marker(self)) < 0)
4630 return -1;
4631
4632 Pdata_clear(self->stack, i);
4633
4634 return 0;
4635}
4636
4637static int
4638load_dup(UnpicklerObject *self)
4639{
4640 PyObject *last;
4641 int len;
4642
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004643 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004644 return stack_underflow();
4645 last = self->stack->data[len - 1];
4646 PDATA_APPEND(self->stack, last, -1);
4647 return 0;
4648}
4649
4650static int
4651load_get(UnpicklerObject *self)
4652{
4653 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004654 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004655 Py_ssize_t len;
4656 char *s;
4657
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004658 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004659 return -1;
4660 if (len < 2)
4661 return bad_readline();
4662
4663 key = PyLong_FromString(s, NULL, 10);
4664 if (key == NULL)
4665 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004666 idx = PyLong_AsSsize_t(key);
4667 if (idx == -1 && PyErr_Occurred()) {
4668 Py_DECREF(key);
4669 return -1;
4670 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004671
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004672 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004673 if (value == NULL) {
4674 if (!PyErr_Occurred())
4675 PyErr_SetObject(PyExc_KeyError, key);
4676 Py_DECREF(key);
4677 return -1;
4678 }
4679 Py_DECREF(key);
4680
4681 PDATA_APPEND(self->stack, value, -1);
4682 return 0;
4683}
4684
4685static int
4686load_binget(UnpicklerObject *self)
4687{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004688 PyObject *value;
4689 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004690 char *s;
4691
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004692 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004693 return -1;
4694
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004695 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004696
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004697 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004698 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004699 PyObject *key = PyLong_FromSsize_t(idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004700 if (!PyErr_Occurred())
4701 PyErr_SetObject(PyExc_KeyError, key);
4702 Py_DECREF(key);
4703 return -1;
4704 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004705
4706 PDATA_APPEND(self->stack, value, -1);
4707 return 0;
4708}
4709
4710static int
4711load_long_binget(UnpicklerObject *self)
4712{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004713 PyObject *value;
4714 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004715 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004716
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004717 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004718 return -1;
4719
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004720 idx = (long)Py_CHARMASK(s[0]);
4721 idx |= (long)Py_CHARMASK(s[1]) << 8;
4722 idx |= (long)Py_CHARMASK(s[2]) << 16;
4723 idx |= (long)Py_CHARMASK(s[3]) << 24;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004724
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004725 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004726 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004727 PyObject *key = PyLong_FromSsize_t(idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004728 if (!PyErr_Occurred())
4729 PyErr_SetObject(PyExc_KeyError, key);
4730 Py_DECREF(key);
4731 return -1;
4732 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004733
4734 PDATA_APPEND(self->stack, value, -1);
4735 return 0;
4736}
4737
4738/* Push an object from the extension registry (EXT[124]). nbytes is
4739 * the number of bytes following the opcode, holding the index (code) value.
4740 */
4741static int
4742load_extension(UnpicklerObject *self, int nbytes)
4743{
4744 char *codebytes; /* the nbytes bytes after the opcode */
4745 long code; /* calc_binint returns long */
4746 PyObject *py_code; /* code as a Python int */
4747 PyObject *obj; /* the object to push */
4748 PyObject *pair; /* (module_name, class_name) */
4749 PyObject *module_name, *class_name;
4750
4751 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004752 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004753 return -1;
4754 code = calc_binint(codebytes, nbytes);
4755 if (code <= 0) { /* note that 0 is forbidden */
4756 /* Corrupt or hostile pickle. */
4757 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4758 return -1;
4759 }
4760
4761 /* Look for the code in the cache. */
4762 py_code = PyLong_FromLong(code);
4763 if (py_code == NULL)
4764 return -1;
4765 obj = PyDict_GetItem(extension_cache, py_code);
4766 if (obj != NULL) {
4767 /* Bingo. */
4768 Py_DECREF(py_code);
4769 PDATA_APPEND(self->stack, obj, -1);
4770 return 0;
4771 }
4772
4773 /* Look up the (module_name, class_name) pair. */
4774 pair = PyDict_GetItem(inverted_registry, py_code);
4775 if (pair == NULL) {
4776 Py_DECREF(py_code);
4777 PyErr_Format(PyExc_ValueError, "unregistered extension "
4778 "code %ld", code);
4779 return -1;
4780 }
4781 /* Since the extension registry is manipulable via Python code,
4782 * confirm that pair is really a 2-tuple of strings.
4783 */
4784 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4785 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4786 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4787 Py_DECREF(py_code);
4788 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4789 "isn't a 2-tuple of strings", code);
4790 return -1;
4791 }
4792 /* Load the object. */
4793 obj = find_class(self, module_name, class_name);
4794 if (obj == NULL) {
4795 Py_DECREF(py_code);
4796 return -1;
4797 }
4798 /* Cache code -> obj. */
4799 code = PyDict_SetItem(extension_cache, py_code, obj);
4800 Py_DECREF(py_code);
4801 if (code < 0) {
4802 Py_DECREF(obj);
4803 return -1;
4804 }
4805 PDATA_PUSH(self->stack, obj, -1);
4806 return 0;
4807}
4808
4809static int
4810load_put(UnpicklerObject *self)
4811{
4812 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004813 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004814 Py_ssize_t len;
4815 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004816
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004817 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004818 return -1;
4819 if (len < 2)
4820 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004821 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004822 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004823 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004824
4825 key = PyLong_FromString(s, NULL, 10);
4826 if (key == NULL)
4827 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004828 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004829 Py_DECREF(key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004830 if (idx == -1 && PyErr_Occurred())
4831 return -1;
4832
4833 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004834}
4835
4836static int
4837load_binput(UnpicklerObject *self)
4838{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004839 PyObject *value;
4840 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004841 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004842
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004843 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004844 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004845
4846 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004847 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004848 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004849
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004850 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004851
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004852 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004853}
4854
4855static int
4856load_long_binput(UnpicklerObject *self)
4857{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004858 PyObject *value;
4859 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004860 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004861
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004862 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004863 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004864
4865 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004866 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004867 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004868
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004869 idx = (long)Py_CHARMASK(s[0]);
4870 idx |= (long)Py_CHARMASK(s[1]) << 8;
4871 idx |= (long)Py_CHARMASK(s[2]) << 16;
4872 idx |= (long)Py_CHARMASK(s[3]) << 24;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004873
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004874 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004875}
4876
4877static int
4878do_append(UnpicklerObject *self, int x)
4879{
4880 PyObject *value;
4881 PyObject *list;
4882 int len, i;
4883
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004884 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004885 if (x > len || x <= 0)
4886 return stack_underflow();
4887 if (len == x) /* nothing to do */
4888 return 0;
4889
4890 list = self->stack->data[x - 1];
4891
4892 if (PyList_Check(list)) {
4893 PyObject *slice;
4894 Py_ssize_t list_len;
4895
4896 slice = Pdata_poplist(self->stack, x);
4897 if (!slice)
4898 return -1;
4899 list_len = PyList_GET_SIZE(list);
4900 i = PyList_SetSlice(list, list_len, list_len, slice);
4901 Py_DECREF(slice);
4902 return i;
4903 }
4904 else {
4905 PyObject *append_func;
4906
4907 append_func = PyObject_GetAttrString(list, "append");
4908 if (append_func == NULL)
4909 return -1;
4910 for (i = x; i < len; i++) {
4911 PyObject *result;
4912
4913 value = self->stack->data[i];
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004914 result = _Unpickler_FastCall(self, append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004915 if (result == NULL) {
4916 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004917 Py_SIZE(self->stack) = x;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004918 return -1;
4919 }
4920 Py_DECREF(result);
4921 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004922 Py_SIZE(self->stack) = x;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004923 }
4924
4925 return 0;
4926}
4927
4928static int
4929load_append(UnpicklerObject *self)
4930{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004931 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004932}
4933
4934static int
4935load_appends(UnpicklerObject *self)
4936{
4937 return do_append(self, marker(self));
4938}
4939
4940static int
4941do_setitems(UnpicklerObject *self, int x)
4942{
4943 PyObject *value, *key;
4944 PyObject *dict;
4945 int len, i;
4946 int status = 0;
4947
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004948 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004949 if (x > len || x <= 0)
4950 return stack_underflow();
4951 if (len == x) /* nothing to do */
4952 return 0;
4953 if ((len - x) % 2 != 0) {
4954 /* Currupt or hostile pickle -- we never write one like this. */
4955 PyErr_SetString(UnpicklingError, "odd number of items for SETITEMS");
4956 return -1;
4957 }
4958
4959 /* Here, dict does not actually need to be a PyDict; it could be anything
4960 that supports the __setitem__ attribute. */
4961 dict = self->stack->data[x - 1];
4962
4963 for (i = x + 1; i < len; i += 2) {
4964 key = self->stack->data[i - 1];
4965 value = self->stack->data[i];
4966 if (PyObject_SetItem(dict, key, value) < 0) {
4967 status = -1;
4968 break;
4969 }
4970 }
4971
4972 Pdata_clear(self->stack, x);
4973 return status;
4974}
4975
4976static int
4977load_setitem(UnpicklerObject *self)
4978{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004979 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004980}
4981
4982static int
4983load_setitems(UnpicklerObject *self)
4984{
4985 return do_setitems(self, marker(self));
4986}
4987
4988static int
4989load_build(UnpicklerObject *self)
4990{
4991 PyObject *state, *inst, *slotstate;
4992 PyObject *setstate;
4993 int status = 0;
4994
4995 /* Stack is ... instance, state. We want to leave instance at
4996 * the stack top, possibly mutated via instance.__setstate__(state).
4997 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004998 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004999 return stack_underflow();
5000
5001 PDATA_POP(self->stack, state);
5002 if (state == NULL)
5003 return -1;
5004
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005005 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005006
5007 setstate = PyObject_GetAttrString(inst, "__setstate__");
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005008 if (setstate == NULL) {
5009 if (PyErr_ExceptionMatches(PyExc_AttributeError))
5010 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00005011 else {
5012 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005013 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00005014 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005015 }
5016 else {
5017 PyObject *result;
5018
5019 /* The explicit __setstate__ is responsible for everything. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005020 /* Ugh... this does not leak since _Unpickler_FastCall() steals the
Antoine Pitroud79dc622008-09-05 00:03:33 +00005021 reference to state first. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005022 result = _Unpickler_FastCall(self, setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005023 Py_DECREF(setstate);
5024 if (result == NULL)
5025 return -1;
5026 Py_DECREF(result);
5027 return 0;
5028 }
5029
5030 /* A default __setstate__. First see whether state embeds a
5031 * slot state dict too (a proto 2 addition).
5032 */
5033 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
5034 PyObject *tmp = state;
5035
5036 state = PyTuple_GET_ITEM(tmp, 0);
5037 slotstate = PyTuple_GET_ITEM(tmp, 1);
5038 Py_INCREF(state);
5039 Py_INCREF(slotstate);
5040 Py_DECREF(tmp);
5041 }
5042 else
5043 slotstate = NULL;
5044
5045 /* Set inst.__dict__ from the state dict (if any). */
5046 if (state != Py_None) {
5047 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005048 PyObject *d_key, *d_value;
5049 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005050
5051 if (!PyDict_Check(state)) {
5052 PyErr_SetString(UnpicklingError, "state is not a dictionary");
5053 goto error;
5054 }
5055 dict = PyObject_GetAttrString(inst, "__dict__");
5056 if (dict == NULL)
5057 goto error;
5058
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005059 i = 0;
5060 while (PyDict_Next(state, &i, &d_key, &d_value)) {
5061 /* normally the keys for instance attributes are
5062 interned. we should try to do that here. */
5063 Py_INCREF(d_key);
5064 if (PyUnicode_CheckExact(d_key))
5065 PyUnicode_InternInPlace(&d_key);
5066 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
5067 Py_DECREF(d_key);
5068 goto error;
5069 }
5070 Py_DECREF(d_key);
5071 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005072 Py_DECREF(dict);
5073 }
5074
5075 /* Also set instance attributes from the slotstate dict (if any). */
5076 if (slotstate != NULL) {
5077 PyObject *d_key, *d_value;
5078 Py_ssize_t i;
5079
5080 if (!PyDict_Check(slotstate)) {
5081 PyErr_SetString(UnpicklingError,
5082 "slot state is not a dictionary");
5083 goto error;
5084 }
5085 i = 0;
5086 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
5087 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
5088 goto error;
5089 }
5090 }
5091
5092 if (0) {
5093 error:
5094 status = -1;
5095 }
5096
5097 Py_DECREF(state);
5098 Py_XDECREF(slotstate);
5099 return status;
5100}
5101
5102static int
5103load_mark(UnpicklerObject *self)
5104{
5105
5106 /* Note that we split the (pickle.py) stack into two stacks, an
5107 * object stack and a mark stack. Here we push a mark onto the
5108 * mark stack.
5109 */
5110
5111 if ((self->num_marks + 1) >= self->marks_size) {
5112 size_t alloc;
5113 int *marks;
5114
5115 /* Use the size_t type to check for overflow. */
5116 alloc = ((size_t)self->num_marks << 1) + 20;
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00005117 if (alloc > PY_SSIZE_T_MAX ||
5118 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005119 PyErr_NoMemory();
5120 return -1;
5121 }
5122
5123 if (self->marks == NULL)
5124 marks = (int *)PyMem_Malloc(alloc * sizeof(int));
5125 else
5126 marks = (int *)PyMem_Realloc(self->marks, alloc * sizeof(int));
5127 if (marks == NULL) {
5128 PyErr_NoMemory();
5129 return -1;
5130 }
5131 self->marks = marks;
5132 self->marks_size = (Py_ssize_t)alloc;
5133 }
5134
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005135 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005136
5137 return 0;
5138}
5139
5140static int
5141load_reduce(UnpicklerObject *self)
5142{
5143 PyObject *callable = NULL;
5144 PyObject *argtup = NULL;
5145 PyObject *obj = NULL;
5146
5147 PDATA_POP(self->stack, argtup);
5148 if (argtup == NULL)
5149 return -1;
5150 PDATA_POP(self->stack, callable);
5151 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005152 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005153 Py_DECREF(callable);
5154 }
5155 Py_DECREF(argtup);
5156
5157 if (obj == NULL)
5158 return -1;
5159
5160 PDATA_PUSH(self->stack, obj, -1);
5161 return 0;
5162}
5163
5164/* Just raises an error if we don't know the protocol specified. PROTO
5165 * is the first opcode for protocols >= 2.
5166 */
5167static int
5168load_proto(UnpicklerObject *self)
5169{
5170 char *s;
5171 int i;
5172
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005173 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005174 return -1;
5175
5176 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00005177 if (i <= HIGHEST_PROTOCOL) {
5178 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005179 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00005180 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005181
5182 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
5183 return -1;
5184}
5185
5186static PyObject *
5187load(UnpicklerObject *self)
5188{
5189 PyObject *err;
5190 PyObject *value = NULL;
5191 char *s;
5192
5193 self->num_marks = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005194 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005195 Pdata_clear(self->stack, 0);
5196
5197 /* Convenient macros for the dispatch while-switch loop just below. */
5198#define OP(opcode, load_func) \
5199 case opcode: if (load_func(self) < 0) break; continue;
5200
5201#define OP_ARG(opcode, load_func, arg) \
5202 case opcode: if (load_func(self, (arg)) < 0) break; continue;
5203
5204 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005205 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005206 break;
5207
5208 switch ((enum opcode)s[0]) {
5209 OP(NONE, load_none)
5210 OP(BININT, load_binint)
5211 OP(BININT1, load_binint1)
5212 OP(BININT2, load_binint2)
5213 OP(INT, load_int)
5214 OP(LONG, load_long)
5215 OP_ARG(LONG1, load_counted_long, 1)
5216 OP_ARG(LONG4, load_counted_long, 4)
5217 OP(FLOAT, load_float)
5218 OP(BINFLOAT, load_binfloat)
5219 OP(BINBYTES, load_binbytes)
5220 OP(SHORT_BINBYTES, load_short_binbytes)
5221 OP(BINSTRING, load_binstring)
5222 OP(SHORT_BINSTRING, load_short_binstring)
5223 OP(STRING, load_string)
5224 OP(UNICODE, load_unicode)
5225 OP(BINUNICODE, load_binunicode)
5226 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
5227 OP_ARG(TUPLE1, load_counted_tuple, 1)
5228 OP_ARG(TUPLE2, load_counted_tuple, 2)
5229 OP_ARG(TUPLE3, load_counted_tuple, 3)
5230 OP(TUPLE, load_tuple)
5231 OP(EMPTY_LIST, load_empty_list)
5232 OP(LIST, load_list)
5233 OP(EMPTY_DICT, load_empty_dict)
5234 OP(DICT, load_dict)
5235 OP(OBJ, load_obj)
5236 OP(INST, load_inst)
5237 OP(NEWOBJ, load_newobj)
5238 OP(GLOBAL, load_global)
5239 OP(APPEND, load_append)
5240 OP(APPENDS, load_appends)
5241 OP(BUILD, load_build)
5242 OP(DUP, load_dup)
5243 OP(BINGET, load_binget)
5244 OP(LONG_BINGET, load_long_binget)
5245 OP(GET, load_get)
5246 OP(MARK, load_mark)
5247 OP(BINPUT, load_binput)
5248 OP(LONG_BINPUT, load_long_binput)
5249 OP(PUT, load_put)
5250 OP(POP, load_pop)
5251 OP(POP_MARK, load_pop_mark)
5252 OP(SETITEM, load_setitem)
5253 OP(SETITEMS, load_setitems)
5254 OP(PERSID, load_persid)
5255 OP(BINPERSID, load_binpersid)
5256 OP(REDUCE, load_reduce)
5257 OP(PROTO, load_proto)
5258 OP_ARG(EXT1, load_extension, 1)
5259 OP_ARG(EXT2, load_extension, 2)
5260 OP_ARG(EXT4, load_extension, 4)
5261 OP_ARG(NEWTRUE, load_bool, Py_True)
5262 OP_ARG(NEWFALSE, load_bool, Py_False)
5263
5264 case STOP:
5265 break;
5266
5267 case '\0':
5268 PyErr_SetNone(PyExc_EOFError);
5269 return NULL;
5270
5271 default:
5272 PyErr_Format(UnpicklingError,
5273 "invalid load key, '%c'.", s[0]);
5274 return NULL;
5275 }
5276
5277 break; /* and we are done! */
5278 }
5279
Antoine Pitrou04248a82010-10-12 20:51:21 +00005280 if (_Unpickler_SkipConsumed(self) < 0)
5281 return NULL;
5282
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005283 /* XXX: It is not clear what this is actually for. */
5284 if ((err = PyErr_Occurred())) {
5285 if (err == PyExc_EOFError) {
5286 PyErr_SetNone(PyExc_EOFError);
5287 }
5288 return NULL;
5289 }
5290
5291 PDATA_POP(self->stack, value);
5292 return value;
5293}
5294
5295PyDoc_STRVAR(Unpickler_load_doc,
5296"load() -> object. Load a pickle."
5297"\n"
5298"Read a pickled object representation from the open file object given in\n"
5299"the constructor, and return the reconstituted object hierarchy specified\n"
5300"therein.\n");
5301
5302static PyObject *
5303Unpickler_load(UnpicklerObject *self)
5304{
5305 /* Check whether the Unpickler was initialized correctly. This prevents
5306 segfaulting if a subclass overridden __init__ with a function that does
5307 not call Unpickler.__init__(). Here, we simply ensure that self->read
5308 is not NULL. */
5309 if (self->read == NULL) {
5310 PyErr_Format(UnpicklingError,
5311 "Unpickler.__init__() was not called by %s.__init__()",
5312 Py_TYPE(self)->tp_name);
5313 return NULL;
5314 }
5315
5316 return load(self);
5317}
5318
5319/* The name of find_class() is misleading. In newer pickle protocols, this
5320 function is used for loading any global (i.e., functions), not just
5321 classes. The name is kept only for backward compatibility. */
5322
5323PyDoc_STRVAR(Unpickler_find_class_doc,
5324"find_class(module_name, global_name) -> object.\n"
5325"\n"
5326"Return an object from a specified module, importing the module if\n"
5327"necessary. Subclasses may override this method (e.g. to restrict\n"
5328"unpickling of arbitrary classes and functions).\n"
5329"\n"
5330"This method is called whenever a class or a function object is\n"
5331"needed. Both arguments passed are str objects.\n");
5332
5333static PyObject *
5334Unpickler_find_class(UnpicklerObject *self, PyObject *args)
5335{
5336 PyObject *global;
5337 PyObject *modules_dict;
5338 PyObject *module;
5339 PyObject *module_name, *global_name;
5340
5341 if (!PyArg_UnpackTuple(args, "find_class", 2, 2,
5342 &module_name, &global_name))
5343 return NULL;
5344
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00005345 /* Try to map the old names used in Python 2.x to the new ones used in
5346 Python 3.x. We do this only with old pickle protocols and when the
5347 user has not disabled the feature. */
5348 if (self->proto < 3 && self->fix_imports) {
5349 PyObject *key;
5350 PyObject *item;
5351
5352 /* Check if the global (i.e., a function or a class) was renamed
5353 or moved to another module. */
5354 key = PyTuple_Pack(2, module_name, global_name);
5355 if (key == NULL)
5356 return NULL;
5357 item = PyDict_GetItemWithError(name_mapping_2to3, key);
5358 Py_DECREF(key);
5359 if (item) {
5360 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
5361 PyErr_Format(PyExc_RuntimeError,
5362 "_compat_pickle.NAME_MAPPING values should be "
5363 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
5364 return NULL;
5365 }
5366 module_name = PyTuple_GET_ITEM(item, 0);
5367 global_name = PyTuple_GET_ITEM(item, 1);
5368 if (!PyUnicode_Check(module_name) ||
5369 !PyUnicode_Check(global_name)) {
5370 PyErr_Format(PyExc_RuntimeError,
5371 "_compat_pickle.NAME_MAPPING values should be "
5372 "pairs of str, not (%.200s, %.200s)",
5373 Py_TYPE(module_name)->tp_name,
5374 Py_TYPE(global_name)->tp_name);
5375 return NULL;
5376 }
5377 }
5378 else if (PyErr_Occurred()) {
5379 return NULL;
5380 }
5381
5382 /* Check if the module was renamed. */
5383 item = PyDict_GetItemWithError(import_mapping_2to3, module_name);
5384 if (item) {
5385 if (!PyUnicode_Check(item)) {
5386 PyErr_Format(PyExc_RuntimeError,
5387 "_compat_pickle.IMPORT_MAPPING values should be "
5388 "strings, not %.200s", Py_TYPE(item)->tp_name);
5389 return NULL;
5390 }
5391 module_name = item;
5392 }
5393 else if (PyErr_Occurred()) {
5394 return NULL;
5395 }
5396 }
5397
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005398 modules_dict = PySys_GetObject("modules");
5399 if (modules_dict == NULL)
5400 return NULL;
5401
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00005402 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005403 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00005404 if (PyErr_Occurred())
5405 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005406 module = PyImport_Import(module_name);
5407 if (module == NULL)
5408 return NULL;
5409 global = PyObject_GetAttr(module, global_name);
5410 Py_DECREF(module);
5411 }
5412 else {
5413 global = PyObject_GetAttr(module, global_name);
5414 }
5415 return global;
5416}
5417
5418static struct PyMethodDef Unpickler_methods[] = {
5419 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
5420 Unpickler_load_doc},
5421 {"find_class", (PyCFunction)Unpickler_find_class, METH_VARARGS,
5422 Unpickler_find_class_doc},
5423 {NULL, NULL} /* sentinel */
5424};
5425
5426static void
5427Unpickler_dealloc(UnpicklerObject *self)
5428{
5429 PyObject_GC_UnTrack((PyObject *)self);
5430 Py_XDECREF(self->readline);
5431 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00005432 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005433 Py_XDECREF(self->stack);
5434 Py_XDECREF(self->pers_func);
5435 Py_XDECREF(self->arg);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005436 if (self->buffer.buf != NULL) {
5437 PyBuffer_Release(&self->buffer);
5438 self->buffer.buf = NULL;
5439 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005440
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005441 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005442 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005443 PyMem_Free(self->input_line);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005444 free(self->encoding);
5445 free(self->errors);
5446
5447 Py_TYPE(self)->tp_free((PyObject *)self);
5448}
5449
5450static int
5451Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
5452{
5453 Py_VISIT(self->readline);
5454 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00005455 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005456 Py_VISIT(self->stack);
5457 Py_VISIT(self->pers_func);
5458 Py_VISIT(self->arg);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005459 return 0;
5460}
5461
5462static int
5463Unpickler_clear(UnpicklerObject *self)
5464{
5465 Py_CLEAR(self->readline);
5466 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00005467 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005468 Py_CLEAR(self->stack);
5469 Py_CLEAR(self->pers_func);
5470 Py_CLEAR(self->arg);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005471 if (self->buffer.buf != NULL) {
5472 PyBuffer_Release(&self->buffer);
5473 self->buffer.buf = NULL;
5474 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005475
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005476 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005477 PyMem_Free(self->marks);
5478 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005479 PyMem_Free(self->input_line);
5480 self->input_line = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005481 free(self->encoding);
5482 self->encoding = NULL;
5483 free(self->errors);
5484 self->errors = NULL;
5485
5486 return 0;
5487}
5488
5489PyDoc_STRVAR(Unpickler_doc,
5490"Unpickler(file, *, encoding='ASCII', errors='strict')"
5491"\n"
5492"This takes a binary file for reading a pickle data stream.\n"
5493"\n"
5494"The protocol version of the pickle is detected automatically, so no\n"
5495"proto argument is needed.\n"
5496"\n"
5497"The file-like object must have two methods, a read() method\n"
5498"that takes an integer argument, and a readline() method that\n"
5499"requires no arguments. Both methods should return bytes.\n"
5500"Thus file-like object can be a binary file object opened for\n"
5501"reading, a BytesIO object, or any other custom object that\n"
5502"meets this interface.\n"
5503"\n"
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00005504"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
5505"which are used to control compatiblity support for pickle stream\n"
5506"generated by Python 2.x. If *fix_imports* is True, pickle will try to\n"
5507"map the old Python 2.x names to the new names used in Python 3.x. The\n"
5508"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
5509"instances pickled by Python 2.x; these default to 'ASCII' and\n"
5510"'strict', respectively.\n");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005511
5512static int
5513Unpickler_init(UnpicklerObject *self, PyObject *args, PyObject *kwds)
5514{
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00005515 static char *kwlist[] = {"file", "fix_imports", "encoding", "errors", 0};
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005516 PyObject *file;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005517 PyObject *fix_imports = Py_True;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005518 char *encoding = NULL;
5519 char *errors = NULL;
5520
5521 /* XXX: That is an horrible error message. But, I don't know how to do
5522 better... */
5523 if (Py_SIZE(args) != 1) {
5524 PyErr_Format(PyExc_TypeError,
5525 "%s takes exactly one positional argument (%zd given)",
5526 Py_TYPE(self)->tp_name, Py_SIZE(args));
5527 return -1;
5528 }
5529
5530 /* Arguments parsing needs to be done in the __init__() method to allow
5531 subclasses to define their own __init__() method, which may (or may
5532 not) support Unpickler arguments. However, this means we need to be
5533 extra careful in the other Unpickler methods, since a subclass could
5534 forget to call Unpickler.__init__() thus breaking our internal
5535 invariants. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005536 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oss:Unpickler", kwlist,
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00005537 &file, &fix_imports, &encoding, &errors))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005538 return -1;
5539
5540 /* In case of multiple __init__() calls, clear previous content. */
5541 if (self->read != NULL)
5542 (void)Unpickler_clear(self);
5543
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005544 if (_Unpickler_SetInputStream(self, file) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005545 return -1;
5546
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005547 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005548 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005549
5550 self->fix_imports = PyObject_IsTrue(fix_imports);
5551 if (self->fix_imports == -1)
5552 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005553
5554 if (PyObject_HasAttrString((PyObject *)self, "persistent_load")) {
5555 self->pers_func = PyObject_GetAttrString((PyObject *)self,
5556 "persistent_load");
5557 if (self->pers_func == NULL)
5558 return -1;
5559 }
5560 else {
5561 self->pers_func = NULL;
5562 }
5563
5564 self->stack = (Pdata *)Pdata_New();
5565 if (self->stack == NULL)
5566 return -1;
5567
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005568 self->memo_size = 32;
5569 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005570 if (self->memo == NULL)
5571 return -1;
5572
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00005573 self->arg = NULL;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00005574 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00005575
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005576 return 0;
5577}
5578
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005579/* Define a proxy object for the Unpickler's internal memo object. This is to
5580 * avoid breaking code like:
5581 * unpickler.memo.clear()
5582 * and
5583 * unpickler.memo = saved_memo
5584 * Is this a good idea? Not really, but we don't want to break code that uses
5585 * it. Note that we don't implement the entire mapping API here. This is
5586 * intentional, as these should be treated as black-box implementation details.
5587 *
5588 * We do, however, have to implement pickling/unpickling support because of
5589 * real-world code like cvs2svn.
5590 */
5591
5592typedef struct {
5593 PyObject_HEAD
5594 UnpicklerObject *unpickler;
5595} UnpicklerMemoProxyObject;
5596
5597PyDoc_STRVAR(ump_clear_doc,
5598"memo.clear() -> None. Remove all items from memo.");
5599
5600static PyObject *
5601ump_clear(UnpicklerMemoProxyObject *self)
5602{
5603 _Unpickler_MemoCleanup(self->unpickler);
5604 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
5605 if (self->unpickler->memo == NULL)
5606 return NULL;
5607 Py_RETURN_NONE;
5608}
5609
5610PyDoc_STRVAR(ump_copy_doc,
5611"memo.copy() -> new_memo. Copy the memo to a new object.");
5612
5613static PyObject *
5614ump_copy(UnpicklerMemoProxyObject *self)
5615{
5616 Py_ssize_t i;
5617 PyObject *new_memo = PyDict_New();
5618 if (new_memo == NULL)
5619 return NULL;
5620
5621 for (i = 0; i < self->unpickler->memo_size; i++) {
5622 int status;
5623 PyObject *key, *value;
5624
5625 value = self->unpickler->memo[i];
5626 if (value == NULL)
5627 continue;
5628
5629 key = PyLong_FromSsize_t(i);
5630 if (key == NULL)
5631 goto error;
5632 status = PyDict_SetItem(new_memo, key, value);
5633 Py_DECREF(key);
5634 if (status < 0)
5635 goto error;
5636 }
5637 return new_memo;
5638
5639error:
5640 Py_DECREF(new_memo);
5641 return NULL;
5642}
5643
5644PyDoc_STRVAR(ump_reduce_doc,
5645"memo.__reduce__(). Pickling support.");
5646
5647static PyObject *
5648ump_reduce(UnpicklerMemoProxyObject *self, PyObject *args)
5649{
5650 PyObject *reduce_value;
5651 PyObject *constructor_args;
5652 PyObject *contents = ump_copy(self);
5653 if (contents == NULL)
5654 return NULL;
5655
5656 reduce_value = PyTuple_New(2);
5657 if (reduce_value == NULL) {
5658 Py_DECREF(contents);
5659 return NULL;
5660 }
5661 constructor_args = PyTuple_New(1);
5662 if (constructor_args == NULL) {
5663 Py_DECREF(contents);
5664 Py_DECREF(reduce_value);
5665 return NULL;
5666 }
5667 PyTuple_SET_ITEM(constructor_args, 0, contents);
5668 Py_INCREF((PyObject *)&PyDict_Type);
5669 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
5670 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
5671 return reduce_value;
5672}
5673
5674static PyMethodDef unpicklerproxy_methods[] = {
5675 {"clear", (PyCFunction)ump_clear, METH_NOARGS, ump_clear_doc},
5676 {"copy", (PyCFunction)ump_copy, METH_NOARGS, ump_copy_doc},
5677 {"__reduce__", (PyCFunction)ump_reduce, METH_VARARGS, ump_reduce_doc},
5678 {NULL, NULL} /* sentinel */
5679};
5680
5681static void
5682UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
5683{
5684 PyObject_GC_UnTrack(self);
5685 Py_XDECREF(self->unpickler);
5686 PyObject_GC_Del((PyObject *)self);
5687}
5688
5689static int
5690UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
5691 visitproc visit, void *arg)
5692{
5693 Py_VISIT(self->unpickler);
5694 return 0;
5695}
5696
5697static int
5698UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
5699{
5700 Py_CLEAR(self->unpickler);
5701 return 0;
5702}
5703
5704static PyTypeObject UnpicklerMemoProxyType = {
5705 PyVarObject_HEAD_INIT(NULL, 0)
5706 "_pickle.UnpicklerMemoProxy", /*tp_name*/
5707 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
5708 0,
5709 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
5710 0, /* tp_print */
5711 0, /* tp_getattr */
5712 0, /* tp_setattr */
5713 0, /* tp_compare */
5714 0, /* tp_repr */
5715 0, /* tp_as_number */
5716 0, /* tp_as_sequence */
5717 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00005718 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005719 0, /* tp_call */
5720 0, /* tp_str */
5721 PyObject_GenericGetAttr, /* tp_getattro */
5722 PyObject_GenericSetAttr, /* tp_setattro */
5723 0, /* tp_as_buffer */
5724 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5725 0, /* tp_doc */
5726 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
5727 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
5728 0, /* tp_richcompare */
5729 0, /* tp_weaklistoffset */
5730 0, /* tp_iter */
5731 0, /* tp_iternext */
5732 unpicklerproxy_methods, /* tp_methods */
5733};
5734
5735static PyObject *
5736UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
5737{
5738 UnpicklerMemoProxyObject *self;
5739
5740 self = PyObject_GC_New(UnpicklerMemoProxyObject,
5741 &UnpicklerMemoProxyType);
5742 if (self == NULL)
5743 return NULL;
5744 Py_INCREF(unpickler);
5745 self->unpickler = unpickler;
5746 PyObject_GC_Track(self);
5747 return (PyObject *)self;
5748}
5749
5750/*****************************************************************************/
5751
5752
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005753static PyObject *
5754Unpickler_get_memo(UnpicklerObject *self)
5755{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005756 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005757}
5758
5759static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005760Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005761{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005762 PyObject **new_memo;
5763 Py_ssize_t new_memo_size = 0;
5764 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005766 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005767 PyErr_SetString(PyExc_TypeError,
5768 "attribute deletion is not supported");
5769 return -1;
5770 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005771
5772 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
5773 UnpicklerObject *unpickler =
5774 ((UnpicklerMemoProxyObject *)obj)->unpickler;
5775
5776 new_memo_size = unpickler->memo_size;
5777 new_memo = _Unpickler_NewMemo(new_memo_size);
5778 if (new_memo == NULL)
5779 return -1;
5780
5781 for (i = 0; i < new_memo_size; i++) {
5782 Py_XINCREF(unpickler->memo[i]);
5783 new_memo[i] = unpickler->memo[i];
5784 }
5785 }
5786 else if (PyDict_Check(obj)) {
5787 Py_ssize_t i = 0;
5788 PyObject *key, *value;
5789
5790 new_memo_size = PyDict_Size(obj);
5791 new_memo = _Unpickler_NewMemo(new_memo_size);
5792 if (new_memo == NULL)
5793 return -1;
5794
5795 while (PyDict_Next(obj, &i, &key, &value)) {
5796 Py_ssize_t idx;
5797 if (!PyLong_Check(key)) {
5798 PyErr_SetString(PyExc_TypeError,
5799 "memo key must be integers");
5800 goto error;
5801 }
5802 idx = PyLong_AsSsize_t(key);
5803 if (idx == -1 && PyErr_Occurred())
5804 goto error;
5805 if (_Unpickler_MemoPut(self, idx, value) < 0)
5806 goto error;
5807 }
5808 }
5809 else {
5810 PyErr_Format(PyExc_TypeError,
5811 "'memo' attribute must be an UnpicklerMemoProxy object"
5812 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005813 return -1;
5814 }
5815
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005816 _Unpickler_MemoCleanup(self);
5817 self->memo_size = new_memo_size;
5818 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005819
5820 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005821
5822 error:
5823 if (new_memo_size) {
5824 i = new_memo_size;
5825 while (--i >= 0) {
5826 Py_XDECREF(new_memo[i]);
5827 }
5828 PyMem_FREE(new_memo);
5829 }
5830 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005831}
5832
5833static PyObject *
5834Unpickler_get_persload(UnpicklerObject *self)
5835{
5836 if (self->pers_func == NULL)
5837 PyErr_SetString(PyExc_AttributeError, "persistent_load");
5838 else
5839 Py_INCREF(self->pers_func);
5840 return self->pers_func;
5841}
5842
5843static int
5844Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
5845{
5846 PyObject *tmp;
5847
5848 if (value == NULL) {
5849 PyErr_SetString(PyExc_TypeError,
5850 "attribute deletion is not supported");
5851 return -1;
5852 }
5853 if (!PyCallable_Check(value)) {
5854 PyErr_SetString(PyExc_TypeError,
5855 "persistent_load must be a callable taking "
5856 "one argument");
5857 return -1;
5858 }
5859
5860 tmp = self->pers_func;
5861 Py_INCREF(value);
5862 self->pers_func = value;
5863 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
5864
5865 return 0;
5866}
5867
5868static PyGetSetDef Unpickler_getsets[] = {
5869 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
5870 {"persistent_load", (getter)Unpickler_get_persload,
5871 (setter)Unpickler_set_persload},
5872 {NULL}
5873};
5874
5875static PyTypeObject Unpickler_Type = {
5876 PyVarObject_HEAD_INIT(NULL, 0)
5877 "_pickle.Unpickler", /*tp_name*/
5878 sizeof(UnpicklerObject), /*tp_basicsize*/
5879 0, /*tp_itemsize*/
5880 (destructor)Unpickler_dealloc, /*tp_dealloc*/
5881 0, /*tp_print*/
5882 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005883 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00005884 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005885 0, /*tp_repr*/
5886 0, /*tp_as_number*/
5887 0, /*tp_as_sequence*/
5888 0, /*tp_as_mapping*/
5889 0, /*tp_hash*/
5890 0, /*tp_call*/
5891 0, /*tp_str*/
5892 0, /*tp_getattro*/
5893 0, /*tp_setattro*/
5894 0, /*tp_as_buffer*/
5895 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5896 Unpickler_doc, /*tp_doc*/
5897 (traverseproc)Unpickler_traverse, /*tp_traverse*/
5898 (inquiry)Unpickler_clear, /*tp_clear*/
5899 0, /*tp_richcompare*/
5900 0, /*tp_weaklistoffset*/
5901 0, /*tp_iter*/
5902 0, /*tp_iternext*/
5903 Unpickler_methods, /*tp_methods*/
5904 0, /*tp_members*/
5905 Unpickler_getsets, /*tp_getset*/
5906 0, /*tp_base*/
5907 0, /*tp_dict*/
5908 0, /*tp_descr_get*/
5909 0, /*tp_descr_set*/
5910 0, /*tp_dictoffset*/
5911 (initproc)Unpickler_init, /*tp_init*/
5912 PyType_GenericAlloc, /*tp_alloc*/
5913 PyType_GenericNew, /*tp_new*/
5914 PyObject_GC_Del, /*tp_free*/
5915 0, /*tp_is_gc*/
5916};
5917
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005918PyDoc_STRVAR(pickle_dump_doc,
5919"dump(obj, file, protocol=None, *, fix_imports=True) -> None\n"
5920"\n"
5921"Write a pickled representation of obj to the open file object file. This\n"
5922"is equivalent to ``Pickler(file, protocol).dump(obj)``, but may be more\n"
5923"efficient.\n"
5924"\n"
5925"The optional protocol argument tells the pickler to use the given protocol;\n"
5926"supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n"
5927"backward-incompatible protocol designed for Python 3.0.\n"
5928"\n"
5929"Specifying a negative protocol version selects the highest protocol version\n"
5930"supported. The higher the protocol used, the more recent the version of\n"
5931"Python needed to read the pickle produced.\n"
5932"\n"
5933"The file argument must have a write() method that accepts a single bytes\n"
5934"argument. It can thus be a file object opened for binary writing, a\n"
5935"io.BytesIO instance, or any other custom object that meets this interface.\n"
5936"\n"
5937"If fix_imports is True and protocol is less than 3, pickle will try to\n"
5938"map the new Python 3.x names to the old module names used in Python 2.x,\n"
5939"so that the pickle data stream is readable with Python 2.x.\n");
5940
5941static PyObject *
5942pickle_dump(PyObject *self, PyObject *args, PyObject *kwds)
5943{
5944 static char *kwlist[] = {"obj", "file", "protocol", "fix_imports", 0};
5945 PyObject *obj;
5946 PyObject *file;
5947 PyObject *proto = NULL;
5948 PyObject *fix_imports = Py_True;
5949 PicklerObject *pickler;
5950
5951 /* fix_imports is a keyword-only argument. */
5952 if (Py_SIZE(args) > 3) {
5953 PyErr_Format(PyExc_TypeError,
5954 "pickle.dump() takes at most 3 positional "
5955 "argument (%zd given)", Py_SIZE(args));
5956 return NULL;
5957 }
5958
5959 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|OO:dump", kwlist,
5960 &obj, &file, &proto, &fix_imports))
5961 return NULL;
5962
5963 pickler = _Pickler_New();
5964 if (pickler == NULL)
5965 return NULL;
5966
5967 if (_Pickler_SetProtocol(pickler, proto, fix_imports) < 0)
5968 goto error;
5969
5970 if (_Pickler_SetOutputStream(pickler, file) < 0)
5971 goto error;
5972
5973 if (dump(pickler, obj) < 0)
5974 goto error;
5975
5976 if (_Pickler_FlushToFile(pickler) < 0)
5977 goto error;
5978
5979 Py_DECREF(pickler);
5980 Py_RETURN_NONE;
5981
5982 error:
5983 Py_XDECREF(pickler);
5984 return NULL;
5985}
5986
5987PyDoc_STRVAR(pickle_dumps_doc,
5988"dumps(obj, protocol=None, *, fix_imports=True) -> bytes\n"
5989"\n"
5990"Return the pickled representation of the object as a bytes\n"
5991"object, instead of writing it to a file.\n"
5992"\n"
5993"The optional protocol argument tells the pickler to use the given protocol;\n"
5994"supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n"
5995"backward-incompatible protocol designed for Python 3.0.\n"
5996"\n"
5997"Specifying a negative protocol version selects the highest protocol version\n"
5998"supported. The higher the protocol used, the more recent the version of\n"
5999"Python needed to read the pickle produced.\n"
6000"\n"
6001"If fix_imports is True and *protocol* is less than 3, pickle will try to\n"
6002"map the new Python 3.x names to the old module names used in Python 2.x,\n"
6003"so that the pickle data stream is readable with Python 2.x.\n");
6004
6005static PyObject *
6006pickle_dumps(PyObject *self, PyObject *args, PyObject *kwds)
6007{
6008 static char *kwlist[] = {"obj", "protocol", "fix_imports", 0};
6009 PyObject *obj;
6010 PyObject *proto = NULL;
6011 PyObject *result;
6012 PyObject *fix_imports = Py_True;
6013 PicklerObject *pickler;
6014
6015 /* fix_imports is a keyword-only argument. */
6016 if (Py_SIZE(args) > 2) {
6017 PyErr_Format(PyExc_TypeError,
6018 "pickle.dumps() takes at most 2 positional "
6019 "argument (%zd given)", Py_SIZE(args));
6020 return NULL;
6021 }
6022
6023 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO:dumps", kwlist,
6024 &obj, &proto, &fix_imports))
6025 return NULL;
6026
6027 pickler = _Pickler_New();
6028 if (pickler == NULL)
6029 return NULL;
6030
6031 if (_Pickler_SetProtocol(pickler, proto, fix_imports) < 0)
6032 goto error;
6033
6034 if (dump(pickler, obj) < 0)
6035 goto error;
6036
6037 result = _Pickler_GetString(pickler);
6038 Py_DECREF(pickler);
6039 return result;
6040
6041 error:
6042 Py_XDECREF(pickler);
6043 return NULL;
6044}
6045
6046PyDoc_STRVAR(pickle_load_doc,
6047"load(file, *, fix_imports=True, encoding='ASCII', errors='strict') -> object\n"
6048"\n"
6049"Read a pickled object representation from the open file object file and\n"
6050"return the reconstituted object hierarchy specified therein. This is\n"
6051"equivalent to ``Unpickler(file).load()``, but may be more efficient.\n"
6052"\n"
6053"The protocol version of the pickle is detected automatically, so no protocol\n"
6054"argument is needed. Bytes past the pickled object's representation are\n"
6055"ignored.\n"
6056"\n"
6057"The argument file must have two methods, a read() method that takes an\n"
6058"integer argument, and a readline() method that requires no arguments. Both\n"
6059"methods should return bytes. Thus *file* can be a binary file object opened\n"
6060"for reading, a BytesIO object, or any other custom object that meets this\n"
6061"interface.\n"
6062"\n"
6063"Optional keyword arguments are fix_imports, encoding and errors,\n"
6064"which are used to control compatiblity support for pickle stream generated\n"
6065"by Python 2.x. If fix_imports is True, pickle will try to map the old\n"
6066"Python 2.x names to the new names used in Python 3.x. The encoding and\n"
6067"errors tell pickle how to decode 8-bit string instances pickled by Python\n"
6068"2.x; these default to 'ASCII' and 'strict', respectively.\n");
6069
6070static PyObject *
6071pickle_load(PyObject *self, PyObject *args, PyObject *kwds)
6072{
6073 static char *kwlist[] = {"file", "fix_imports", "encoding", "errors", 0};
6074 PyObject *file;
6075 PyObject *fix_imports = Py_True;
6076 PyObject *result;
6077 char *encoding = NULL;
6078 char *errors = NULL;
6079 UnpicklerObject *unpickler;
6080
6081 /* fix_imports, encoding and errors are a keyword-only argument. */
6082 if (Py_SIZE(args) != 1) {
6083 PyErr_Format(PyExc_TypeError,
6084 "pickle.load() takes exactly one positional "
6085 "argument (%zd given)", Py_SIZE(args));
6086 return NULL;
6087 }
6088
6089 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oss:load", kwlist,
6090 &file, &fix_imports, &encoding, &errors))
6091 return NULL;
6092
6093 unpickler = _Unpickler_New();
6094 if (unpickler == NULL)
6095 return NULL;
6096
6097 if (_Unpickler_SetInputStream(unpickler, file) < 0)
6098 goto error;
6099
6100 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
6101 goto error;
6102
6103 unpickler->fix_imports = PyObject_IsTrue(fix_imports);
6104 if (unpickler->fix_imports == -1)
6105 goto error;
6106
6107 result = load(unpickler);
6108 Py_DECREF(unpickler);
6109 return result;
6110
6111 error:
6112 Py_XDECREF(unpickler);
6113 return NULL;
6114}
6115
6116PyDoc_STRVAR(pickle_loads_doc,
6117"loads(input, *, fix_imports=True, encoding='ASCII', errors='strict') -> object\n"
6118"\n"
6119"Read a pickled object hierarchy from a bytes object and return the\n"
6120"reconstituted object hierarchy specified therein\n"
6121"\n"
6122"The protocol version of the pickle is detected automatically, so no protocol\n"
6123"argument is needed. Bytes past the pickled object's representation are\n"
6124"ignored.\n"
6125"\n"
6126"Optional keyword arguments are fix_imports, encoding and errors, which\n"
6127"are used to control compatiblity support for pickle stream generated\n"
6128"by Python 2.x. If fix_imports is True, pickle will try to map the old\n"
6129"Python 2.x names to the new names used in Python 3.x. The encoding and\n"
6130"errors tell pickle how to decode 8-bit string instances pickled by Python\n"
6131"2.x; these default to 'ASCII' and 'strict', respectively.\n");
6132
6133static PyObject *
6134pickle_loads(PyObject *self, PyObject *args, PyObject *kwds)
6135{
6136 static char *kwlist[] = {"input", "fix_imports", "encoding", "errors", 0};
6137 PyObject *input;
6138 PyObject *fix_imports = Py_True;
6139 PyObject *result;
6140 char *encoding = NULL;
6141 char *errors = NULL;
6142 UnpicklerObject *unpickler;
6143
6144 /* fix_imports, encoding and errors are a keyword-only argument. */
6145 if (Py_SIZE(args) != 1) {
6146 PyErr_Format(PyExc_TypeError,
6147 "pickle.loads() takes exactly one positional "
6148 "argument (%zd given)", Py_SIZE(args));
6149 return NULL;
6150 }
6151
6152 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oss:loads", kwlist,
6153 &input, &fix_imports, &encoding, &errors))
6154 return NULL;
6155
6156 unpickler = _Unpickler_New();
6157 if (unpickler == NULL)
6158 return NULL;
6159
6160 if (_Unpickler_SetStringInput(unpickler, input) < 0)
6161 goto error;
6162
6163 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
6164 goto error;
6165
6166 unpickler->fix_imports = PyObject_IsTrue(fix_imports);
6167 if (unpickler->fix_imports == -1)
6168 goto error;
6169
6170 result = load(unpickler);
6171 Py_DECREF(unpickler);
6172 return result;
6173
6174 error:
6175 Py_XDECREF(unpickler);
6176 return NULL;
6177}
6178
6179
6180static struct PyMethodDef pickle_methods[] = {
6181 {"dump", (PyCFunction)pickle_dump, METH_VARARGS|METH_KEYWORDS,
6182 pickle_dump_doc},
6183 {"dumps", (PyCFunction)pickle_dumps, METH_VARARGS|METH_KEYWORDS,
6184 pickle_dumps_doc},
6185 {"load", (PyCFunction)pickle_load, METH_VARARGS|METH_KEYWORDS,
6186 pickle_load_doc},
6187 {"loads", (PyCFunction)pickle_loads, METH_VARARGS|METH_KEYWORDS,
6188 pickle_loads_doc},
6189 {NULL, NULL} /* sentinel */
6190};
6191
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006192static int
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006193initmodule(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006194{
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006195 PyObject *copyreg = NULL;
6196 PyObject *compat_pickle = NULL;
6197
6198 /* XXX: We should ensure that the types of the dictionaries imported are
6199 exactly PyDict objects. Otherwise, it is possible to crash the pickle
6200 since we use the PyDict API directly to access these dictionaries. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006201
6202 copyreg = PyImport_ImportModule("copyreg");
6203 if (!copyreg)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006204 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006205 dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
6206 if (!dispatch_table)
6207 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006208 extension_registry = \
6209 PyObject_GetAttrString(copyreg, "_extension_registry");
6210 if (!extension_registry)
6211 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006212 inverted_registry = PyObject_GetAttrString(copyreg, "_inverted_registry");
6213 if (!inverted_registry)
6214 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006215 extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
6216 if (!extension_cache)
6217 goto error;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006218 Py_CLEAR(copyreg);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006219
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006220 /* Load the 2.x -> 3.x stdlib module mapping tables */
6221 compat_pickle = PyImport_ImportModule("_compat_pickle");
6222 if (!compat_pickle)
6223 goto error;
6224 name_mapping_2to3 = PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
6225 if (!name_mapping_2to3)
6226 goto error;
6227 if (!PyDict_CheckExact(name_mapping_2to3)) {
6228 PyErr_Format(PyExc_RuntimeError,
6229 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
6230 Py_TYPE(name_mapping_2to3)->tp_name);
6231 goto error;
6232 }
6233 import_mapping_2to3 = PyObject_GetAttrString(compat_pickle,
6234 "IMPORT_MAPPING");
6235 if (!import_mapping_2to3)
6236 goto error;
6237 if (!PyDict_CheckExact(import_mapping_2to3)) {
6238 PyErr_Format(PyExc_RuntimeError,
6239 "_compat_pickle.IMPORT_MAPPING should be a dict, "
6240 "not %.200s", Py_TYPE(import_mapping_2to3)->tp_name);
6241 goto error;
6242 }
6243 /* ... and the 3.x -> 2.x mapping tables */
6244 name_mapping_3to2 = PyObject_GetAttrString(compat_pickle,
6245 "REVERSE_NAME_MAPPING");
6246 if (!name_mapping_3to2)
6247 goto error;
6248 if (!PyDict_CheckExact(name_mapping_3to2)) {
6249 PyErr_Format(PyExc_RuntimeError,
6250 "_compat_pickle.REVERSE_NAME_MAPPING shouldbe a dict, "
6251 "not %.200s", Py_TYPE(name_mapping_3to2)->tp_name);
6252 goto error;
6253 }
6254 import_mapping_3to2 = PyObject_GetAttrString(compat_pickle,
6255 "REVERSE_IMPORT_MAPPING");
6256 if (!import_mapping_3to2)
6257 goto error;
6258 if (!PyDict_CheckExact(import_mapping_3to2)) {
6259 PyErr_Format(PyExc_RuntimeError,
6260 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
6261 "not %.200s", Py_TYPE(import_mapping_3to2)->tp_name);
6262 goto error;
6263 }
6264 Py_CLEAR(compat_pickle);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006265
6266 empty_tuple = PyTuple_New(0);
6267 if (empty_tuple == NULL)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006268 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006269 two_tuple = PyTuple_New(2);
6270 if (two_tuple == NULL)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006271 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006272 /* We use this temp container with no regard to refcounts, or to
6273 * keeping containees alive. Exempt from GC, because we don't
6274 * want anything looking at two_tuple() by magic.
6275 */
6276 PyObject_GC_UnTrack(two_tuple);
6277
6278 return 0;
6279
6280 error:
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006281 Py_CLEAR(copyreg);
6282 Py_CLEAR(dispatch_table);
6283 Py_CLEAR(extension_registry);
6284 Py_CLEAR(inverted_registry);
6285 Py_CLEAR(extension_cache);
6286 Py_CLEAR(compat_pickle);
6287 Py_CLEAR(name_mapping_2to3);
6288 Py_CLEAR(import_mapping_2to3);
6289 Py_CLEAR(name_mapping_3to2);
6290 Py_CLEAR(import_mapping_3to2);
6291 Py_CLEAR(empty_tuple);
6292 Py_CLEAR(two_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006293 return -1;
6294}
6295
6296static struct PyModuleDef _picklemodule = {
6297 PyModuleDef_HEAD_INIT,
6298 "_pickle",
6299 pickle_module_doc,
6300 -1,
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006301 pickle_methods,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006302 NULL,
6303 NULL,
6304 NULL,
6305 NULL
6306};
6307
6308PyMODINIT_FUNC
6309PyInit__pickle(void)
6310{
6311 PyObject *m;
6312
6313 if (PyType_Ready(&Unpickler_Type) < 0)
6314 return NULL;
6315 if (PyType_Ready(&Pickler_Type) < 0)
6316 return NULL;
6317 if (PyType_Ready(&Pdata_Type) < 0)
6318 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006319 if (PyType_Ready(&PicklerMemoProxyType) < 0)
6320 return NULL;
6321 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
6322 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006323
6324 /* Create the module and add the functions. */
6325 m = PyModule_Create(&_picklemodule);
6326 if (m == NULL)
6327 return NULL;
6328
6329 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
6330 return NULL;
6331 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
6332 return NULL;
6333
6334 /* Initialize the exceptions. */
6335 PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
6336 if (PickleError == NULL)
6337 return NULL;
6338 PicklingError = \
6339 PyErr_NewException("_pickle.PicklingError", PickleError, NULL);
6340 if (PicklingError == NULL)
6341 return NULL;
6342 UnpicklingError = \
6343 PyErr_NewException("_pickle.UnpicklingError", PickleError, NULL);
6344 if (UnpicklingError == NULL)
6345 return NULL;
6346
6347 if (PyModule_AddObject(m, "PickleError", PickleError) < 0)
6348 return NULL;
6349 if (PyModule_AddObject(m, "PicklingError", PicklingError) < 0)
6350 return NULL;
6351 if (PyModule_AddObject(m, "UnpicklingError", UnpicklingError) < 0)
6352 return NULL;
6353
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006354 if (initmodule() < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006355 return NULL;
6356
6357 return m;
6358}