blob: ce9283a4f5e2a92aac76f0bc9aa65820afb60e95 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001#include "Python.h"
2#include "cStringIO.h"
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003#include "structmember.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005PyDoc_STRVAR(cPickle_module_documentation,
Tim Peters64c04d12003-02-01 06:27:59 +00006"C implementation and optimization of the Python pickle module.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007
Guido van Rossum142eeb81997-08-13 03:14:41 +00008#ifndef Py_eval_input
9#include <graminit.h>
10#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000011#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000012
Guido van Rossum60456fd1997-04-09 17:36:32 +000013#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000014
Guido van Rossum60456fd1997-04-09 17:36:32 +000015#define WRITE_BUF_SIZE 256
16
Tim Peters5bd2a792003-02-01 16:45:06 +000017/* Bump this when new opcodes are added to the pickle protocol. */
Tim Peters8587b3c2003-02-13 15:44:41 +000018#define HIGHEST_PROTOCOL 2
Tim Peters5bd2a792003-02-01 16:45:06 +000019
Tim Peters797ec242003-02-01 06:22:36 +000020/*
Martin v. Löwis9ac49272009-01-02 20:40:14 +000021 * Note: The UNICODE macro controls the TCHAR meaning of the win32 API. Since
22 * all headers have already been included here, we can safely redefine it.
23 */
24#ifdef UNICODE
25# undef UNICODE
26#endif
27
28/*
Tim Peters797ec242003-02-01 06:22:36 +000029 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
30 * docs are in pickletools.py.
31 */
Guido van Rossum60456fd1997-04-09 17:36:32 +000032#define MARK '('
33#define STOP '.'
34#define POP '0'
35#define POP_MARK '1'
36#define DUP '2'
37#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000038#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000039#define INT 'I'
40#define BININT 'J'
41#define BININT1 'K'
42#define LONG 'L'
43#define BININT2 'M'
44#define NONE 'N'
45#define PERSID 'P'
46#define BINPERSID 'Q'
47#define REDUCE 'R'
48#define STRING 'S'
49#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000050#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000051#define UNICODE 'V'
52#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000053#define APPEND 'a'
54#define BUILD 'b'
55#define GLOBAL 'c'
56#define DICT 'd'
57#define EMPTY_DICT '}'
58#define APPENDS 'e'
59#define GET 'g'
60#define BINGET 'h'
61#define INST 'i'
62#define LONG_BINGET 'j'
63#define LIST 'l'
64#define EMPTY_LIST ']'
65#define OBJ 'o'
66#define PUT 'p'
67#define BINPUT 'q'
68#define LONG_BINPUT 'r'
69#define SETITEM 's'
70#define TUPLE 't'
71#define EMPTY_TUPLE ')'
72#define SETITEMS 'u'
Tim Peters797ec242003-02-01 06:22:36 +000073
74/* Protocol 2. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +000075#define PROTO '\x80' /* identify pickle protocol */
Tim Peters797ec242003-02-01 06:22:36 +000076#define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
77#define EXT1 '\x82' /* push object from extension registry; 1-byte index */
78#define EXT2 '\x83' /* ditto, but 2-byte index */
79#define EXT4 '\x84' /* ditto, but 4-byte index */
80#define TUPLE1 '\x85' /* build 1-tuple from stack top */
81#define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
82#define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
83#define NEWTRUE '\x88' /* push True */
84#define NEWFALSE '\x89' /* push False */
85#define LONG1 '\x8a' /* push long from < 256 bytes */
86#define LONG4 '\x8b' /* push really big long */
87
88/* There aren't opcodes -- they're ways to pickle bools before protocol 2,
89 * so that unpicklers written before bools were introduced unpickle them
90 * as ints, but unpicklers after can recognize that bools were intended.
91 * Note that protocol 2 added direct ways to pickle bools.
92 */
Jack Jansen3a967022002-06-26 20:40:42 +000093#undef TRUE
Guido van Rossume2763392002-04-05 19:30:08 +000094#define TRUE "I01\n"
Jack Jansen3a967022002-06-26 20:40:42 +000095#undef FALSE
Guido van Rossume2763392002-04-05 19:30:08 +000096#define FALSE "I00\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +000097
Tim Peters1092d642003-02-11 21:06:20 +000098/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
Tim Peters42f08ac2003-02-11 22:43:24 +000099 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
100 * break if this gets out of synch with pickle.py, but it's unclear that
101 * would help anything either.
Tim Peters1092d642003-02-11 21:06:20 +0000102 */
103#define BATCHSIZE 1000
104
Guido van Rossum60456fd1997-04-09 17:36:32 +0000105static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000106
Guido van Rossumc03158b1999-06-09 15:23:31 +0000107static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000108static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000109static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000110static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000111static PyObject *BadPickleGet;
112
Tim Peters5b7da392003-02-04 00:21:07 +0000113/* As the name says, an empty tuple. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000114static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000115
Georg Brandldffbf5f2008-05-20 07:49:57 +0000116/* copy_reg.dispatch_table, {type_object: pickling_function} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *dispatch_table;
118
119/* For EXT[124] opcodes. */
Georg Brandldffbf5f2008-05-20 07:49:57 +0000120/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000121static PyObject *extension_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000122/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000123static PyObject *inverted_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000124/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000125static PyObject *extension_cache;
126
Georg Brandldffbf5f2008-05-20 07:49:57 +0000127/* For looking up name pairs in copy_reg._extension_registry. */
Tim Peters731098b2003-02-04 20:56:09 +0000128static PyObject *two_tuple;
129
Guido van Rossum60456fd1997-04-09 17:36:32 +0000130static PyObject *__class___str, *__getinitargs___str, *__dict___str,
131 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Guido van Rossumb289b872003-02-19 01:45:13 +0000132 *__reduce_ex___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000133 *write_str, *append_str,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000134 *read_str, *readline_str, *__main___str,
Alexandre Vassalotti8b2d7132009-11-24 17:53:23 +0000135 *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000136
Guido van Rossum053b8df1998-11-25 16:18:00 +0000137/*************************************************************************
138 Internal Data type for pickle data. */
139
140typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000141 PyObject_HEAD
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200142 Py_ssize_t length; /* number of initial slots in data currently used */
143 Py_ssize_t size; /* number of slots in data allocated */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000144 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000145} Pdata;
146
Tim Peters84e87f32001-03-17 04:50:51 +0000147static void
Tim Peterscba30e22003-02-01 06:24:36 +0000148Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000149{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200150 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000151 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000152
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000153 for (i = self->length, p = self->data; --i >= 0; p++) {
154 Py_DECREF(*p);
155 }
156 if (self->data)
157 free(self->data);
158 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000159}
160
161static PyTypeObject PdataType = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000162 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,
163 (destructor)Pdata_dealloc,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000165};
166
Christian Heimese93237d2007-12-19 02:37:44 +0000167#define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000168
169static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000170Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000171{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000172 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000173
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000174 if (!(self = PyObject_New(Pdata, &PdataType)))
175 return NULL;
176 self->size = 8;
177 self->length = 0;
178 self->data = malloc(self->size * sizeof(PyObject*));
179 if (self->data)
180 return (PyObject*)self;
181 Py_DECREF(self);
182 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000183}
184
Tim Peters84e87f32001-03-17 04:50:51 +0000185static int
Tim Peterscba30e22003-02-01 06:24:36 +0000186stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000187{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000188 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
189 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000190}
191
Tim Peters1d63c9f2003-02-02 20:29:39 +0000192/* Retain only the initial clearto items. If clearto >= the current
193 * number of items, this is a (non-erroneous) NOP.
194 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000195static int
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200196Pdata_clear(Pdata *self, Py_ssize_t clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000197{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200198 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000199 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000200
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000201 if (clearto < 0) return stackUnderflow();
202 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000203
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000204 for (i = self->length, p = self->data + clearto;
205 --i >= clearto;
206 p++) {
207 Py_CLEAR(*p);
208 }
209 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000210
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000211 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000212}
213
Tim Peters84e87f32001-03-17 04:50:51 +0000214static int
Tim Peterscba30e22003-02-01 06:24:36 +0000215Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000216{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200217 Py_ssize_t bigger;
218 Py_ssize_t nbytes;
219
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000220 PyObject **tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000221
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200222 if (self->size > (PY_SSIZE_T_MAX >> 1))
223 goto nomemory;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000224 bigger = self->size << 1;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200225 if (bigger > (PY_SSIZE_T_MAX / sizeof(PyObject *)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000226 goto nomemory;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200227 nbytes = bigger * sizeof(PyObject *);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000228 tmp = realloc(self->data, nbytes);
229 if (tmp == NULL)
230 goto nomemory;
231 self->data = tmp;
232 self->size = bigger;
233 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000234
235 nomemory:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000236 PyErr_NoMemory();
237 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000238}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000239
Tim Peterse0a39072003-02-03 15:45:56 +0000240/* D is a Pdata*. Pop the topmost element and store it into V, which
241 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000242 * is raised and V is set to NULL. D and V may be evaluated several times.
243 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000244#define PDATA_POP(D, V) { \
245 if ((D)->length) \
246 (V) = (D)->data[--((D)->length)]; \
247 else { \
248 PyErr_SetString(UnpicklingError, "bad pickle data"); \
249 (V) = NULL; \
250 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000251}
252
Tim Peterse0a39072003-02-03 15:45:56 +0000253/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
254 * D. If the Pdata stack can't be grown to hold the new value, both
255 * raise MemoryError and execute "return ER". The difference is in ownership
256 * of O after: _PUSH transfers ownership of O from the caller to the stack
257 * (no incref of O is done, and in case of error O is decrefed), while
258 * _APPEND pushes a new reference.
259 */
260
261/* Push O on stack D, giving ownership of O to the stack. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000262#define PDATA_PUSH(D, O, ER) { \
263 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
264 Pdata_grow((Pdata*)(D)) < 0) { \
265 Py_DECREF(O); \
266 return ER; \
267 } \
268 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
Tim Peterse0a39072003-02-03 15:45:56 +0000269}
270
271/* Push O on stack D, pushing a new reference. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000272#define PDATA_APPEND(D, O, ER) { \
273 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
274 Pdata_grow((Pdata*)(D)) < 0) \
275 return ER; \
276 Py_INCREF(O); \
277 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
Tim Peterse0a39072003-02-03 15:45:56 +0000278}
279
280
Guido van Rossum053b8df1998-11-25 16:18:00 +0000281static PyObject *
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200282Pdata_popTuple(Pdata *self, Py_ssize_t start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000283{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000284 PyObject *r;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200285 Py_ssize_t i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000286
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000287 l = self->length-start;
288 r = PyTuple_New(l);
289 if (r == NULL)
290 return NULL;
291 for (i = start, j = 0 ; j < l; i++, j++)
292 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000293
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000294 self->length = start;
295 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000296}
297
298static PyObject *
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200299Pdata_popList(Pdata *self, Py_ssize_t start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000300{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000301 PyObject *r;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200302 Py_ssize_t i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000303
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 l=self->length-start;
305 if (!( r=PyList_New(l))) return NULL;
306 for (i=start, j=0 ; j < l; i++, j++)
307 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000308
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000309 self->length=start;
310 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000311}
312
Guido van Rossum053b8df1998-11-25 16:18:00 +0000313/*************************************************************************/
314
315#define ARG_TUP(self, o) { \
316 if (self->arg || (self->arg=PyTuple_New(1))) { \
317 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
318 PyTuple_SET_ITEM(self->arg,0,o); \
319 } \
320 else { \
321 Py_DECREF(o); \
322 } \
323}
324
325#define FREE_ARG_TUP(self) { \
Christian Heimese93237d2007-12-19 02:37:44 +0000326 if (Py_REFCNT(self->arg) > 1) { \
Serhiy Storchaka98a97222014-02-09 13:14:04 +0200327 Py_CLEAR(self->arg); \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000328 } \
329 }
330
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000331typedef struct Picklerobject {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000332 PyObject_HEAD
333 FILE *fp;
334 PyObject *write;
335 PyObject *file;
336 PyObject *memo;
337 PyObject *arg;
338 PyObject *pers_func;
339 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000340
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000341 /* pickle protocol number, >= 0 */
342 int proto;
Tim Peters797ec242003-02-01 06:22:36 +0000343
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000344 /* bool, true if proto > 0 */
345 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000346
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000347 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200348 Py_ssize_t (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000349 char *write_buf;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200350 Py_ssize_t buf_size;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000351 PyObject *dispatch_table;
352 int fast_container; /* count nested container dumps */
353 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000354} Picklerobject;
355
Barry Warsaw52acb492001-12-21 20:04:22 +0000356#ifndef PY_CPICKLE_FAST_LIMIT
357#define PY_CPICKLE_FAST_LIMIT 50
358#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000359
Jeremy Hylton938ace62002-07-17 16:30:39 +0000360static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000361
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000362typedef struct Unpicklerobject {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000363 PyObject_HEAD
364 FILE *fp;
365 PyObject *file;
366 PyObject *readline;
367 PyObject *read;
368 PyObject *memo;
369 PyObject *arg;
370 Pdata *stack;
371 PyObject *mark;
372 PyObject *pers_func;
373 PyObject *last_string;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200374 Py_ssize_t *marks;
375 Py_ssize_t num_marks;
376 Py_ssize_t marks_size;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000377 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
378 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200379 Py_ssize_t buf_size;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000380 char *buf;
381 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000382} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000383
Jeremy Hylton938ace62002-07-17 16:30:39 +0000384static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000385
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000386/* Forward decls that need the above structs */
387static int save(Picklerobject *, PyObject *, int);
388static int put2(Picklerobject *, PyObject *);
389
Guido van Rossumd385d591997-04-09 17:47:47 +0000390static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000391PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000392cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
393{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000394 va_list va;
395 PyObject *args=0, *retval=0;
396 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000397
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000398 if (format) args = Py_VaBuildValue(format, va);
399 va_end(va);
400 if (format && ! args) return NULL;
401 if (stringformat && !(retval=PyString_FromString(stringformat)))
402 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000403
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000404 if (retval) {
405 if (args) {
406 PyObject *v;
407 v=PyString_Format(retval, args);
408 Py_DECREF(retval);
409 Py_DECREF(args);
410 if (! v) return NULL;
411 retval=v;
412 }
413 }
414 else
415 if (args) retval=args;
416 else {
417 PyErr_SetObject(ErrType,Py_None);
418 return NULL;
419 }
420 PyErr_SetObject(ErrType,retval);
421 Py_DECREF(retval);
422 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000423}
424
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200425static Py_ssize_t
Martin v. Löwis18e16552006-02-15 17:27:45 +0000426write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000427{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000428 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000429
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000430 if (s == NULL) {
431 return 0;
432 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000433
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000434 PyFile_IncUseCount((PyFileObject *)self->file);
435 Py_BEGIN_ALLOW_THREADS
436 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
437 Py_END_ALLOW_THREADS
438 PyFile_DecUseCount((PyFileObject *)self->file);
439 if (nbyteswritten != (size_t)n) {
440 PyErr_SetFromErrno(PyExc_IOError);
441 return -1;
442 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000443
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200444 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000445}
446
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200447static Py_ssize_t
Martin v. Löwis18e16552006-02-15 17:27:45 +0000448write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000449{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200450 Py_ssize_t len = n;
451
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000452 if (s == NULL) {
453 return 0;
454 }
Tim Peterscba30e22003-02-01 06:24:36 +0000455
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200456 while (n > INT_MAX) {
457 if (PycStringIO->cwrite((PyObject *)self->file, s, INT_MAX) != INT_MAX) {
458 return -1;
459 }
460 n -= INT_MAX;
461 }
462
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000463 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
464 return -1;
465 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000466
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200467 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000468}
469
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200470static Py_ssize_t
Martin v. Löwis18e16552006-02-15 17:27:45 +0000471write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000472{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000473 if (s == NULL) return 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200474 return n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000475}
476
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200477static Py_ssize_t
478write_other(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000479{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000480 PyObject *py_str = 0, *junk = 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000481
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000482 if (s == NULL) {
483 if (!( self->buf_size )) return 0;
484 py_str = PyString_FromStringAndSize(self->write_buf,
485 self->buf_size);
486 if (!py_str)
487 return -1;
488 }
489 else {
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200490 if (self->buf_size && n > WRITE_BUF_SIZE - self->buf_size) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000491 if (write_other(self, NULL, 0) < 0)
492 return -1;
493 }
Tim Peterscba30e22003-02-01 06:24:36 +0000494
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000495 if (n > WRITE_BUF_SIZE) {
496 if (!( py_str =
497 PyString_FromStringAndSize(s, n)))
498 return -1;
499 }
500 else {
501 memcpy(self->write_buf + self->buf_size, s, n);
502 self->buf_size += n;
503 return n;
504 }
505 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000506
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000507 if (self->write) {
508 /* object with write method */
509 ARG_TUP(self, py_str);
510 if (self->arg) {
511 junk = PyObject_Call(self->write, self->arg, NULL);
512 FREE_ARG_TUP(self);
513 }
514 if (junk) Py_DECREF(junk);
515 else return -1;
516 }
517 else
518 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000519
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000520 self->buf_size = 0;
521 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000522}
523
524
Martin v. Löwis18e16552006-02-15 17:27:45 +0000525static Py_ssize_t
526read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000527{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000528 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000529
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000530 if (self->buf_size == 0) {
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200531 Py_ssize_t size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000532
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000533 size = ((n < 32) ? 32 : n);
534 if (!( self->buf = (char *)malloc(size))) {
535 PyErr_NoMemory();
536 return -1;
537 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000538
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000539 self->buf_size = size;
540 }
541 else if (n > self->buf_size) {
542 char *newbuf = (char *)realloc(self->buf, n);
543 if (!newbuf) {
544 PyErr_NoMemory();
545 return -1;
546 }
547 self->buf = newbuf;
548 self->buf_size = n;
549 }
Tim Peters84e87f32001-03-17 04:50:51 +0000550
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000551 PyFile_IncUseCount((PyFileObject *)self->file);
552 Py_BEGIN_ALLOW_THREADS
553 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
554 Py_END_ALLOW_THREADS
555 PyFile_DecUseCount((PyFileObject *)self->file);
556 if (nbytesread != (size_t)n) {
557 if (feof(self->fp)) {
558 PyErr_SetNone(PyExc_EOFError);
559 return -1;
560 }
Tim Peterscba30e22003-02-01 06:24:36 +0000561
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000562 PyErr_SetFromErrno(PyExc_IOError);
563 return -1;
564 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000565
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000566 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000567
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000568 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000569}
570
571
Martin v. Löwis18e16552006-02-15 17:27:45 +0000572static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000573readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000574{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200575 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000576
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000577 if (self->buf_size == 0) {
578 if (!( self->buf = (char *)malloc(40))) {
579 PyErr_NoMemory();
580 return -1;
581 }
582 self->buf_size = 40;
583 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000584
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000585 i = 0;
586 while (1) {
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200587 Py_ssize_t bigger;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000588 char *newbuf;
589 for (; i < (self->buf_size - 1); i++) {
590 if (feof(self->fp) ||
591 (self->buf[i] = getc(self->fp)) == '\n') {
592 self->buf[i + 1] = '\0';
593 *s = self->buf;
594 return i + 1;
595 }
596 }
Serhiy Storchakad36d4e02013-02-26 10:07:36 +0200597 if (self->buf_size > (PY_SSIZE_T_MAX >> 1)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000598 PyErr_NoMemory();
599 return -1;
600 }
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200601 bigger = self->buf_size << 1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000602 newbuf = (char *)realloc(self->buf, bigger);
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200603 if (newbuf == NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000604 PyErr_NoMemory();
605 return -1;
606 }
607 self->buf = newbuf;
608 self->buf_size = bigger;
609 }
Tim Peters84e87f32001-03-17 04:50:51 +0000610}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000611
612
Martin v. Löwis18e16552006-02-15 17:27:45 +0000613static Py_ssize_t
614read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000615{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200616 Py_ssize_t len = n;
617 char *start, *end = NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000618
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200619 while (1) {
620 int k;
621 char *ptr;
622 if (n > INT_MAX)
623 k = INT_MAX;
624 else
625 k = (int)n;
626 if (PycStringIO->cread((PyObject *)self->file, &ptr, k) != k) {
627 PyErr_SetNone(PyExc_EOFError);
628 return -1;
629 }
630 if (end == NULL)
631 start = ptr;
632 else if (ptr != end) {
633 /* non-continuous area */
634 return -1;
635 }
636 if (n <= INT_MAX)
637 break;
638 end = ptr + INT_MAX;
639 n -= INT_MAX;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000640 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000641
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200642 *s = start;
Tim Peterscba30e22003-02-01 06:24:36 +0000643
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200644 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000645}
646
647
Martin v. Löwis18e16552006-02-15 17:27:45 +0000648static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000649readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000650{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200651 Py_ssize_t n = 0;
652 char *start = NULL, *end = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000653
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200654 while (1) {
655 int k;
656 char *ptr;
657 if ((k = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
658 return -1;
659 }
660 n += k;
661 if (end == NULL)
662 start = ptr;
663 else if (ptr != end) {
664 /* non-continuous area */
665 return -1;
666 }
667 if (k == 0 || ptr[k - 1] == '\n')
668 break;
669 end = ptr + k;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000670 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200672 *s = start;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000673
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000674 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000675}
676
677
Martin v. Löwis18e16552006-02-15 17:27:45 +0000678static Py_ssize_t
679read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000680{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000681 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000682
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000683 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000684
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000685 ARG_TUP(self, bytes);
686 if (self->arg) {
687 str = PyObject_Call(self->read, self->arg, NULL);
688 FREE_ARG_TUP(self);
689 }
690 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000691
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000692 Py_XDECREF(self->last_string);
693 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000694
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000695 if (! (*s = PyString_AsString(str))) return -1;
Amaury Forgeot d'Arc74b30162009-07-23 19:26:02 +0000696
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000697 if (PyString_GET_SIZE(str) != n) {
698 PyErr_SetNone(PyExc_EOFError);
699 return -1;
700 }
Amaury Forgeot d'Arc74b30162009-07-23 19:26:02 +0000701
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000702 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000703}
704
705
Martin v. Löwis18e16552006-02-15 17:27:45 +0000706static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000707readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000708{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000709 PyObject *str;
710 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000711
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000712 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
713 return -1;
714 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000715
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000716 if ((str_size = PyString_Size(str)) < 0)
717 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000718
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000719 Py_XDECREF(self->last_string);
720 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000721
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000722 if (! (*s = PyString_AsString(str)))
723 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000724
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000725 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000726}
727
Tim Petersee1a53c2003-02-02 02:57:53 +0000728/* Copy the first n bytes from s into newly malloc'ed memory, plus a
729 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
730 * The caller is responsible for free()'ing the return value.
731 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000732static char *
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200733pystrndup(const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000734{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000735 char *r = (char *)malloc(n+1);
736 if (r == NULL)
737 return (char*)PyErr_NoMemory();
738 memcpy(r, s, n);
739 r[n] = 0;
740 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000741}
742
743
744static int
Tim Peterscba30e22003-02-01 06:24:36 +0000745get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000746{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000747 PyObject *value, *mv;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200748 Py_ssize_t c_value;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000749 char s[30];
750 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000751
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000752 if (!( mv = PyDict_GetItem(self->memo, id))) {
753 PyErr_SetObject(PyExc_KeyError, id);
754 return -1;
755 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000756
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000757 if (!( value = PyTuple_GetItem(mv, 0)))
758 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000759
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000760 if (!( PyInt_Check(value))) {
761 PyErr_SetString(PicklingError, "no int where int expected in memo");
762 return -1;
763 }
764 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000765
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000766 if (!self->bin) {
767 s[0] = GET;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200768 PyOS_snprintf(s + 1, sizeof(s) - 1,
769 "%" PY_FORMAT_SIZE_T "d\n", c_value);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000770 len = strlen(s);
771 }
772 else if (Pdata_Check(self->file)) {
773 if (write_other(self, NULL, 0) < 0) return -1;
774 PDATA_APPEND(self->file, mv, -1);
775 return 0;
776 }
777 else {
778 if (c_value < 256) {
779 s[0] = BINGET;
780 s[1] = (int)(c_value & 0xff);
781 len = 2;
782 }
783 else {
784 s[0] = LONG_BINGET;
785 s[1] = (int)(c_value & 0xff);
786 s[2] = (int)((c_value >> 8) & 0xff);
787 s[3] = (int)((c_value >> 16) & 0xff);
788 s[4] = (int)((c_value >> 24) & 0xff);
789 len = 5;
790 }
791 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000792
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000793 if (self->write_func(self, s, len) < 0)
794 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000795
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000796 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000797}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000798
Guido van Rossum60456fd1997-04-09 17:36:32 +0000799
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000800static int
Tim Peterscba30e22003-02-01 06:24:36 +0000801put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000802{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000803 if (Py_REFCNT(ob) < 2 || self->fast)
804 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000805
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000806 return put2(self, ob);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000807}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000808
Guido van Rossum053b8df1998-11-25 16:18:00 +0000809
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000810static int
Tim Peterscba30e22003-02-01 06:24:36 +0000811put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000812{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000813 char c_str[30];
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200814 Py_ssize_t len, p;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000815 int res = -1;
816 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000817
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000818 if (self->fast)
819 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000820
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000821 if ((p = PyDict_Size(self->memo)) < 0)
822 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000823
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000824 /* Make sure memo keys are positive! */
825 /* XXX Why?
826 * XXX And does "positive" really mean non-negative?
827 * XXX pickle.py starts with PUT index 0, not 1. This makes for
828 * XXX gratuitous differences between the pickling modules.
829 */
830 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000831
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000832 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
833 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000834
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000835 if (!( memo_len = PyInt_FromLong(p)))
836 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000837
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000838 if (!( t = PyTuple_New(2)))
839 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000840
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000841 PyTuple_SET_ITEM(t, 0, memo_len);
842 Py_INCREF(memo_len);
843 PyTuple_SET_ITEM(t, 1, ob);
844 Py_INCREF(ob);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000845
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000846 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
847 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000848
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000849 if (!self->bin) {
850 c_str[0] = PUT;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200851 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1,
852 "%" PY_FORMAT_SIZE_T "d\n", p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000853 len = strlen(c_str);
854 }
855 else if (Pdata_Check(self->file)) {
856 if (write_other(self, NULL, 0) < 0) return -1;
857 PDATA_APPEND(self->file, memo_len, -1);
858 res=0; /* Job well done ;) */
859 goto finally;
860 }
861 else {
862 if (p >= 256) {
863 c_str[0] = LONG_BINPUT;
864 c_str[1] = (int)(p & 0xff);
865 c_str[2] = (int)((p >> 8) & 0xff);
866 c_str[3] = (int)((p >> 16) & 0xff);
867 c_str[4] = (int)((p >> 24) & 0xff);
868 len = 5;
869 }
870 else {
871 c_str[0] = BINPUT;
872 c_str[1] = p;
873 len = 2;
874 }
875 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000876
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000877 if (self->write_func(self, c_str, len) < 0)
878 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000879
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000880 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000881
882 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000883 Py_XDECREF(py_ob_id);
884 Py_XDECREF(memo_len);
885 Py_XDECREF(t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000886
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000887 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000888}
889
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000890static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000891whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000892{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000893 Py_ssize_t i, j;
894 PyObject *module = 0, *modules_dict = 0,
895 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000896
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000897 module = PyObject_GetAttrString(global, "__module__");
898 if (module)
899 return module;
900 if (PyErr_ExceptionMatches(PyExc_AttributeError))
901 PyErr_Clear();
902 else
903 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000904
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000905 if (!( modules_dict = PySys_GetObject("modules")))
906 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000907
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000908 i = 0;
909 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000910
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000911 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000912
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000913 global_name_attr = PyObject_GetAttr(module, global_name);
914 if (!global_name_attr) {
915 if (PyErr_ExceptionMatches(PyExc_AttributeError))
916 PyErr_Clear();
917 else
918 return NULL;
919 continue;
920 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000921
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000922 if (global_name_attr != global) {
923 Py_DECREF(global_name_attr);
924 continue;
925 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000926
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000927 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000928
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000929 break;
930 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000931
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000932 /* The following implements the rule in pickle.py added in 1.5
933 that used __main__ if no module is found. I don't actually
934 like this rule. jlf
935 */
936 if (!j) {
937 name=__main___str;
938 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000939
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000940 Py_INCREF(name);
941 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000942}
943
944
Guido van Rossum60456fd1997-04-09 17:36:32 +0000945static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000946fast_save_enter(Picklerobject *self, PyObject *obj)
947{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000948 /* if fast_container < 0, we're doing an error exit. */
949 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
950 PyObject *key = NULL;
951 if (self->fast_memo == NULL) {
952 self->fast_memo = PyDict_New();
953 if (self->fast_memo == NULL) {
954 self->fast_container = -1;
955 return 0;
956 }
957 }
958 key = PyLong_FromVoidPtr(obj);
959 if (key == NULL)
960 return 0;
961 if (PyDict_GetItem(self->fast_memo, key)) {
962 Py_DECREF(key);
963 PyErr_Format(PyExc_ValueError,
964 "fast mode: can't pickle cyclic objects "
965 "including object type %s at %p",
966 Py_TYPE(obj)->tp_name, obj);
967 self->fast_container = -1;
968 return 0;
969 }
970 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
971 Py_DECREF(key);
972 self->fast_container = -1;
973 return 0;
974 }
975 Py_DECREF(key);
976 }
977 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000978}
979
Tim Peterscba30e22003-02-01 06:24:36 +0000980int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000981fast_save_leave(Picklerobject *self, PyObject *obj)
982{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000983 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
984 PyObject *key = PyLong_FromVoidPtr(obj);
985 if (key == NULL)
986 return 0;
987 if (PyDict_DelItem(self->fast_memo, key) < 0) {
988 Py_DECREF(key);
989 return 0;
990 }
991 Py_DECREF(key);
992 }
993 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000994}
995
996static int
Tim Peterscba30e22003-02-01 06:24:36 +0000997save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000998{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000999 static char none = NONE;
1000 if (self->write_func(self, &none, 1) < 0)
1001 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001002
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001003 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001004}
1005
Guido van Rossum77f6a652002-04-03 22:41:51 +00001006static int
Tim Peterscba30e22003-02-01 06:24:36 +00001007save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +00001008{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001009 static const char *buf[2] = {FALSE, TRUE};
1010 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
1011 long l = PyInt_AS_LONG((PyIntObject *)args);
Guido van Rossum77f6a652002-04-03 22:41:51 +00001012
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001013 if (self->proto >= 2) {
1014 char opcode = l ? NEWTRUE : NEWFALSE;
1015 if (self->write_func(self, &opcode, 1) < 0)
1016 return -1;
1017 }
1018 else if (self->write_func(self, buf[l], len[l]) < 0)
1019 return -1;
1020 return 0;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001021}
Tim Peters84e87f32001-03-17 04:50:51 +00001022
Guido van Rossum60456fd1997-04-09 17:36:32 +00001023static int
Tim Peterscba30e22003-02-01 06:24:36 +00001024save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001025{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001026 char c_str[32];
1027 long l = PyInt_AS_LONG((PyIntObject *)args);
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001028 Py_ssize_t len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001029
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001030 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001031#if SIZEOF_LONG > 4
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001032 || l > 0x7fffffffL
1033 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001034#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001035 ) {
1036 /* Text-mode pickle, or long too big to fit in the 4-byte
1037 * signed BININT format: store as a string.
1038 */
1039 c_str[0] = INT;
1040 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
1041 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1042 return -1;
1043 }
1044 else {
1045 /* Binary pickle and l fits in a signed 4-byte int. */
1046 c_str[1] = (int)( l & 0xff);
1047 c_str[2] = (int)((l >> 8) & 0xff);
1048 c_str[3] = (int)((l >> 16) & 0xff);
1049 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001050
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001051 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1052 if (c_str[2] == 0) {
1053 c_str[0] = BININT1;
1054 len = 2;
1055 }
1056 else {
1057 c_str[0] = BININT2;
1058 len = 3;
1059 }
1060 }
1061 else {
1062 c_str[0] = BININT;
1063 len = 5;
1064 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001065
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001066 if (self->write_func(self, c_str, len) < 0)
1067 return -1;
1068 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001069
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001070 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001071}
1072
1073
1074static int
Tim Peterscba30e22003-02-01 06:24:36 +00001075save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001076{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001077 Py_ssize_t size;
1078 int res = -1;
1079 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001080
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001081 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001082
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001083 if (self->proto >= 2) {
1084 /* Linear-time pickling. */
1085 size_t nbits;
1086 size_t nbytes;
1087 unsigned char *pdata;
1088 char c_str[5];
1089 int i;
1090 int sign = _PyLong_Sign(args);
Tim Petersee1a53c2003-02-02 02:57:53 +00001091
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001092 if (sign == 0) {
1093 /* It's 0 -- an empty bytestring. */
1094 c_str[0] = LONG1;
1095 c_str[1] = 0;
1096 i = self->write_func(self, c_str, 2);
1097 if (i < 0) goto finally;
1098 res = 0;
1099 goto finally;
1100 }
1101 nbits = _PyLong_NumBits(args);
1102 if (nbits == (size_t)-1 && PyErr_Occurred())
1103 goto finally;
1104 /* How many bytes do we need? There are nbits >> 3 full
1105 * bytes of data, and nbits & 7 leftover bits. If there
1106 * are any leftover bits, then we clearly need another
1107 * byte. Wnat's not so obvious is that we *probably*
1108 * need another byte even if there aren't any leftovers:
1109 * the most-significant bit of the most-significant byte
1110 * acts like a sign bit, and it's usually got a sense
1111 * opposite of the one we need. The exception is longs
1112 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1113 * its own 256's-complement, so has the right sign bit
1114 * even without the extra byte. That's a pain to check
1115 * for in advance, though, so we always grab an extra
1116 * byte at the start, and cut it back later if possible.
1117 */
1118 nbytes = (nbits >> 3) + 1;
1119 if (nbytes > INT_MAX) {
1120 PyErr_SetString(PyExc_OverflowError, "long too large "
1121 "to pickle");
1122 goto finally;
1123 }
1124 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1125 if (repr == NULL) goto finally;
1126 pdata = (unsigned char *)PyString_AS_STRING(repr);
1127 i = _PyLong_AsByteArray((PyLongObject *)args,
1128 pdata, nbytes,
1129 1 /* little endian */, 1 /* signed */);
1130 if (i < 0) goto finally;
1131 /* If the long is negative, this may be a byte more than
1132 * needed. This is so iff the MSB is all redundant sign
1133 * bits.
1134 */
1135 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1136 (pdata[nbytes - 2] & 0x80) != 0)
1137 --nbytes;
Tim Petersee1a53c2003-02-02 02:57:53 +00001138
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001139 if (nbytes < 256) {
1140 c_str[0] = LONG1;
1141 c_str[1] = (char)nbytes;
1142 size = 2;
1143 }
1144 else {
1145 c_str[0] = LONG4;
1146 size = (int)nbytes;
1147 for (i = 1; i < 5; i++) {
1148 c_str[i] = (char)(size & 0xff);
1149 size >>= 8;
1150 }
1151 size = 5;
1152 }
1153 i = self->write_func(self, c_str, size);
1154 if (i < 0) goto finally;
1155 i = self->write_func(self, (char *)pdata, (int)nbytes);
1156 if (i < 0) goto finally;
1157 res = 0;
1158 goto finally;
1159 }
Tim Petersee1a53c2003-02-02 02:57:53 +00001160
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001161 /* proto < 2: write the repr and newline. This is quadratic-time
1162 * (in the number of digits), in both directions.
1163 */
1164 if (!( repr = PyObject_Repr(args)))
1165 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001166
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001167 if ((size = PyString_Size(repr)) < 0)
1168 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001169
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001170 if (self->write_func(self, &l, 1) < 0)
1171 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001172
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001173 if (self->write_func(self,
1174 PyString_AS_STRING((PyStringObject *)repr),
1175 size) < 0)
1176 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001177
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001178 if (self->write_func(self, "\n", 1) < 0)
1179 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001180
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001181 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001182
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001183 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001184 Py_XDECREF(repr);
1185 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001186}
1187
1188
1189static int
Tim Peterscba30e22003-02-01 06:24:36 +00001190save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001191{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001192 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001193
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001194 if (self->bin) {
1195 char str[9];
1196 str[0] = BINFLOAT;
1197 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
1198 return -1;
1199 if (self->write_func(self, str, 9) < 0)
1200 return -1;
1201 }
1202 else {
1203 int result = -1;
1204 char *buf = NULL;
1205 char op = FLOAT;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001206
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001207 if (self->write_func(self, &op, 1) < 0)
1208 goto done;
Eric Smithb05d3be2009-10-26 15:06:39 +00001209
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001210 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
1211 if (!buf) {
1212 PyErr_NoMemory();
1213 goto done;
1214 }
Eric Smithb05d3be2009-10-26 15:06:39 +00001215
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001216 if (self->write_func(self, buf, strlen(buf)) < 0)
1217 goto done;
Eric Smithb05d3be2009-10-26 15:06:39 +00001218
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001219 if (self->write_func(self, "\n", 1) < 0)
1220 goto done;
Eric Smithb05d3be2009-10-26 15:06:39 +00001221
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001222 result = 0;
Eric Smithb05d3be2009-10-26 15:06:39 +00001223done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001224 PyMem_Free(buf);
1225 return result;
1226 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001227
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001228 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001229}
1230
1231
1232static int
Tim Peterscba30e22003-02-01 06:24:36 +00001233save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001234{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001235 Py_ssize_t size, len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001236 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001237
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001238 if ((size = PyString_Size(args)) < 0)
1239 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001240
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001241 if (!self->bin) {
1242 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001243
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001244 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001245
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001246 if (!( repr = PyObject_Repr(args)))
1247 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001248
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001249 if ((len = PyString_Size(repr)) < 0)
1250 goto err;
1251 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001252
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001253 if (self->write_func(self, &string, 1) < 0)
1254 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001255
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001256 if (self->write_func(self, repr_str, len) < 0)
1257 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001258
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001259 if (self->write_func(self, "\n", 1) < 0)
1260 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001261
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001262 Py_XDECREF(repr);
1263 }
1264 else {
1265 int i;
1266 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001267
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001268 if (size < 256) {
1269 c_str[0] = SHORT_BINSTRING;
1270 c_str[1] = size;
1271 len = 2;
1272 }
1273 else if (size <= INT_MAX) {
1274 c_str[0] = BINSTRING;
1275 for (i = 1; i < 5; i++)
1276 c_str[i] = (int)(size >> ((i - 1) * 8));
1277 len = 5;
1278 }
1279 else
1280 return -1; /* string too large */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001281
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001282 if (self->write_func(self, c_str, len) < 0)
1283 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001284
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001285 if (size > 128 && Pdata_Check(self->file)) {
1286 if (write_other(self, NULL, 0) < 0) return -1;
1287 PDATA_APPEND(self->file, args, -1);
1288 }
1289 else {
1290 if (self->write_func(self,
1291 PyString_AS_STRING(
1292 (PyStringObject *)args),
1293 size) < 0)
1294 return -1;
1295 }
1296 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001297
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001298 if (doput)
1299 if (put(self, args) < 0)
1300 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001301
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001302 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001303
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001304 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001305 Py_XDECREF(repr);
1306 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001307}
1308
1309
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001310#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001311/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1312 backslash and newline characters to \uXXXX escapes. */
1313static PyObject *
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001314modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001315{
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001316 PyObject *repr;
1317 char *p;
1318 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001319
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001320 static const char *hexdigit = "0123456789abcdef";
1321#ifdef Py_UNICODE_WIDE
1322 const Py_ssize_t expandsize = 10;
1323#else
1324 const Py_ssize_t expandsize = 6;
1325#endif
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001326
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001327 if (size > PY_SSIZE_T_MAX / expandsize)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001328 return PyErr_NoMemory();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001329
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001330 repr = PyString_FromStringAndSize(NULL, expandsize * size);
1331 if (repr == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001332 return NULL;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001333 if (size == 0)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001334 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001335
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001336 p = q = PyString_AS_STRING(repr);
1337 while (size-- > 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001338 Py_UNICODE ch = *s++;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001339#ifdef Py_UNICODE_WIDE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001340 /* Map 32-bit characters to '\Uxxxxxxxx' */
1341 if (ch >= 0x10000) {
1342 *p++ = '\\';
1343 *p++ = 'U';
1344 *p++ = hexdigit[(ch >> 28) & 0xf];
1345 *p++ = hexdigit[(ch >> 24) & 0xf];
1346 *p++ = hexdigit[(ch >> 20) & 0xf];
1347 *p++ = hexdigit[(ch >> 16) & 0xf];
1348 *p++ = hexdigit[(ch >> 12) & 0xf];
1349 *p++ = hexdigit[(ch >> 8) & 0xf];
1350 *p++ = hexdigit[(ch >> 4) & 0xf];
1351 *p++ = hexdigit[ch & 15];
1352 }
1353 else
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001354#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001355 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1356 if (ch >= 0xD800 && ch < 0xDC00) {
1357 Py_UNICODE ch2;
1358 Py_UCS4 ucs;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001359
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001360 ch2 = *s++;
1361 size--;
1362 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1363 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1364 *p++ = '\\';
1365 *p++ = 'U';
1366 *p++ = hexdigit[(ucs >> 28) & 0xf];
1367 *p++ = hexdigit[(ucs >> 24) & 0xf];
1368 *p++ = hexdigit[(ucs >> 20) & 0xf];
1369 *p++ = hexdigit[(ucs >> 16) & 0xf];
1370 *p++ = hexdigit[(ucs >> 12) & 0xf];
1371 *p++ = hexdigit[(ucs >> 8) & 0xf];
1372 *p++ = hexdigit[(ucs >> 4) & 0xf];
1373 *p++ = hexdigit[ucs & 0xf];
1374 continue;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001375 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001376 /* Fall through: isolated surrogates are copied as-is */
1377 s--;
1378 size++;
1379 }
1380#endif
1381 /* Map 16-bit characters to '\uxxxx' */
1382 if (ch >= 256 || ch == '\\' || ch == '\n') {
1383 *p++ = '\\';
1384 *p++ = 'u';
1385 *p++ = hexdigit[(ch >> 12) & 0xf];
1386 *p++ = hexdigit[(ch >> 8) & 0xf];
1387 *p++ = hexdigit[(ch >> 4) & 0xf];
1388 *p++ = hexdigit[ch & 15];
1389 }
1390 /* Copy everything else as-is */
1391 else
1392 *p++ = (char) ch;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001393 }
1394 *p = '\0';
1395 _PyString_Resize(&repr, p - q);
1396 return repr;
1397}
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001398
Guido van Rossum60456fd1997-04-09 17:36:32 +00001399static int
Tim Peterscba30e22003-02-01 06:24:36 +00001400save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001401{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001402 Py_ssize_t size, len;
1403 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001404
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001405 if (!PyUnicode_Check(args))
1406 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001407
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001408 if (!self->bin) {
1409 char *repr_str;
1410 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001411
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001412 repr = modified_EncodeRawUnicodeEscape(
1413 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1414 if (!repr)
1415 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001416
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001417 if ((len = PyString_Size(repr)) < 0)
1418 goto err;
1419 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001420
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001421 if (self->write_func(self, &string, 1) < 0)
1422 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001423
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001424 if (self->write_func(self, repr_str, len) < 0)
1425 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001426
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001427 if (self->write_func(self, "\n", 1) < 0)
1428 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001429
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001430 Py_XDECREF(repr);
1431 }
1432 else {
1433 int i;
1434 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001435
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001436 if (!( repr = PyUnicode_AsUTF8String(args)))
1437 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001438
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001439 if ((size = PyString_Size(repr)) < 0)
1440 goto err;
1441 if (size > INT_MAX)
1442 return -1; /* string too large */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001443
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001444 c_str[0] = BINUNICODE;
1445 for (i = 1; i < 5; i++)
1446 c_str[i] = (int)(size >> ((i - 1) * 8));
1447 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001448
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001449 if (self->write_func(self, c_str, len) < 0)
1450 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001451
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001452 if (size > 128 && Pdata_Check(self->file)) {
1453 if (write_other(self, NULL, 0) < 0)
1454 goto err;
1455 PDATA_APPEND(self->file, repr, -1);
1456 }
1457 else {
1458 if (self->write_func(self, PyString_AS_STRING(repr),
1459 size) < 0)
1460 goto err;
1461 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001462
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001463 Py_DECREF(repr);
1464 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001465
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001466 if (doput)
1467 if (put(self, args) < 0)
1468 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001469
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001470 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001471
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001472 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001473 Py_XDECREF(repr);
1474 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001475}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001476#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001477
Tim Peters1d63c9f2003-02-02 20:29:39 +00001478/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1479static int
Tim Peters67920142003-02-05 03:46:17 +00001480store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001481{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001482 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001483 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001484
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001485 assert(PyTuple_Size(t) == len);
Tim Peters1d63c9f2003-02-02 20:29:39 +00001486
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001487 for (i = 0; i < len; i++) {
1488 PyObject *element = PyTuple_GET_ITEM(t, i);
Tim Peters1d63c9f2003-02-02 20:29:39 +00001489
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001490 if (element == NULL)
1491 goto finally;
1492 if (save(self, element, 0) < 0)
1493 goto finally;
1494 }
1495 res = 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001496
1497 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001498 return res;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001499}
1500
1501/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1502 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001503 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001504 * (a tuple can be reached from itself), and that requires some subtle
1505 * magic so that it works in all cases. IOW, this is a long routine.
1506 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001507static int
Tim Peterscba30e22003-02-01 06:24:36 +00001508save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001509{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001510 PyObject *py_tuple_id = NULL;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001511 Py_ssize_t len, i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001512 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001513
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001514 static char tuple = TUPLE;
1515 static char pop = POP;
1516 static char pop_mark = POP_MARK;
1517 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001518
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001519 if ((len = PyTuple_Size(args)) < 0)
1520 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001521
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001522 if (len == 0) {
1523 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001524
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001525 if (self->proto) {
1526 c_str[0] = EMPTY_TUPLE;
1527 len = 1;
1528 }
1529 else {
1530 c_str[0] = MARK;
1531 c_str[1] = TUPLE;
1532 len = 2;
1533 }
1534 if (self->write_func(self, c_str, len) >= 0)
1535 res = 0;
1536 /* Don't memoize an empty tuple. */
1537 goto finally;
1538 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001539
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001540 /* A non-empty tuple. */
Tim Peters1d63c9f2003-02-02 20:29:39 +00001541
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001542 /* id(tuple) isn't in the memo now. If it shows up there after
1543 * saving the tuple elements, the tuple must be recursive, in
1544 * which case we'll pop everything we put on the stack, and fetch
1545 * its value from the memo.
1546 */
1547 py_tuple_id = PyLong_FromVoidPtr(args);
1548 if (py_tuple_id == NULL)
1549 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001550
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001551 if (len <= 3 && self->proto >= 2) {
1552 /* Use TUPLE{1,2,3} opcodes. */
1553 if (store_tuple_elements(self, args, len) < 0)
1554 goto finally;
1555 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1556 /* pop the len elements */
1557 for (i = 0; i < len; ++i)
1558 if (self->write_func(self, &pop, 1) < 0)
1559 goto finally;
1560 /* fetch from memo */
1561 if (get(self, py_tuple_id) < 0)
1562 goto finally;
1563 res = 0;
1564 goto finally;
1565 }
1566 /* Not recursive. */
1567 if (self->write_func(self, len2opcode + len, 1) < 0)
1568 goto finally;
1569 goto memoize;
1570 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001571
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001572 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1573 * Generate MARK elt1 elt2 ... TUPLE
1574 */
1575 if (self->write_func(self, &MARKv, 1) < 0)
1576 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001577
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001578 if (store_tuple_elements(self, args, len) < 0)
1579 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001580
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001581 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1582 /* pop the stack stuff we pushed */
1583 if (self->bin) {
1584 if (self->write_func(self, &pop_mark, 1) < 0)
1585 goto finally;
1586 }
1587 else {
1588 /* Note that we pop one more than len, to remove
1589 * the MARK too.
1590 */
1591 for (i = 0; i <= len; i++)
1592 if (self->write_func(self, &pop, 1) < 0)
1593 goto finally;
1594 }
1595 /* fetch from memo */
1596 if (get(self, py_tuple_id) >= 0)
1597 res = 0;
1598 goto finally;
1599 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001600
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001601 /* Not recursive. */
1602 if (self->write_func(self, &tuple, 1) < 0)
1603 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001604
Tim Peters1d63c9f2003-02-02 20:29:39 +00001605 memoize:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001606 if (put(self, args) >= 0)
1607 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001608
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001609 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001610 Py_XDECREF(py_tuple_id);
1611 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001612}
1613
Tim Peters1092d642003-02-11 21:06:20 +00001614/* iter is an iterator giving items, and we batch up chunks of
1615 * MARK item item ... item APPENDS
1616 * opcode sequences. Calling code should have arranged to first create an
1617 * empty list, or list-like object, for the APPENDS to operate on.
1618 * Returns 0 on success, <0 on error.
1619 */
1620static int
1621batch_list(Picklerobject *self, PyObject *iter)
1622{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001623 PyObject *obj = NULL;
1624 PyObject *firstitem = NULL;
1625 int i, n;
Tim Peters1092d642003-02-11 21:06:20 +00001626
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001627 static char append = APPEND;
1628 static char appends = APPENDS;
Tim Peters1092d642003-02-11 21:06:20 +00001629
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001630 assert(iter != NULL);
Tim Peters1092d642003-02-11 21:06:20 +00001631
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001632 if (self->proto == 0) {
1633 /* APPENDS isn't available; do one at a time. */
1634 for (;;) {
1635 obj = PyIter_Next(iter);
1636 if (obj == NULL) {
1637 if (PyErr_Occurred())
1638 return -1;
1639 break;
1640 }
1641 i = save(self, obj, 0);
1642 Py_DECREF(obj);
1643 if (i < 0)
1644 return -1;
1645 if (self->write_func(self, &append, 1) < 0)
1646 return -1;
1647 }
1648 return 0;
1649 }
Tim Peters1092d642003-02-11 21:06:20 +00001650
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001651 /* proto > 0: write in batches of BATCHSIZE. */
1652 do {
1653 /* Get first item */
1654 firstitem = PyIter_Next(iter);
1655 if (firstitem == NULL) {
1656 if (PyErr_Occurred())
1657 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001658
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001659 /* nothing more to add */
1660 break;
1661 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001662
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001663 /* Try to get a second item */
1664 obj = PyIter_Next(iter);
1665 if (obj == NULL) {
1666 if (PyErr_Occurred())
1667 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001668
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001669 /* Only one item to write */
1670 if (save(self, firstitem, 0) < 0)
1671 goto BatchFailed;
1672 if (self->write_func(self, &append, 1) < 0)
1673 goto BatchFailed;
1674 Py_CLEAR(firstitem);
1675 break;
1676 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001677
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001678 /* More than one item to write */
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001679
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001680 /* Pump out MARK, items, APPENDS. */
1681 if (self->write_func(self, &MARKv, 1) < 0)
1682 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001683
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001684 if (save(self, firstitem, 0) < 0)
1685 goto BatchFailed;
1686 Py_CLEAR(firstitem);
1687 n = 1;
Tim Peters1092d642003-02-11 21:06:20 +00001688
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001689 /* Fetch and save up to BATCHSIZE items */
1690 while (obj) {
1691 if (save(self, obj, 0) < 0)
1692 goto BatchFailed;
1693 Py_CLEAR(obj);
1694 n += 1;
Tim Peters1092d642003-02-11 21:06:20 +00001695
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001696 if (n == BATCHSIZE)
1697 break;
1698
1699 obj = PyIter_Next(iter);
1700 if (obj == NULL) {
1701 if (PyErr_Occurred())
1702 goto BatchFailed;
1703 break;
1704 }
1705 }
1706
1707 if (self->write_func(self, &appends, 1) < 0)
1708 goto BatchFailed;
1709
1710 } while (n == BATCHSIZE);
1711 return 0;
Tim Peters1092d642003-02-11 21:06:20 +00001712
1713BatchFailed:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001714 Py_XDECREF(firstitem);
1715 Py_XDECREF(obj);
1716 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001717}
1718
Guido van Rossum60456fd1997-04-09 17:36:32 +00001719static int
Tim Peterscba30e22003-02-01 06:24:36 +00001720save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001721{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001722 int res = -1;
1723 char s[3];
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001724 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001725 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001726
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001727 if (self->fast && !fast_save_enter(self, args))
1728 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001729
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001730 /* Create an empty list. */
1731 if (self->bin) {
1732 s[0] = EMPTY_LIST;
1733 len = 1;
1734 }
1735 else {
1736 s[0] = MARK;
1737 s[1] = LIST;
1738 len = 2;
1739 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001740
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001741 if (self->write_func(self, s, len) < 0)
1742 goto finally;
Tim Peters1092d642003-02-11 21:06:20 +00001743
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001744 /* Get list length, and bow out early if empty. */
1745 if ((len = PyList_Size(args)) < 0)
1746 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001747
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001748 /* Memoize. */
1749 if (len == 0) {
1750 if (put(self, args) >= 0)
1751 res = 0;
1752 goto finally;
1753 }
1754 if (put2(self, args) < 0)
1755 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001756
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001757 /* Materialize the list elements. */
1758 iter = PyObject_GetIter(args);
1759 if (iter == NULL)
1760 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001761
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001762 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1763 {
1764 res = batch_list(self, iter);
1765 Py_LeaveRecursiveCall();
1766 }
1767 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001768
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001769 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001770 if (self->fast && !fast_save_leave(self, args))
1771 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001772
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001773 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001774}
1775
1776
Tim Peters42f08ac2003-02-11 22:43:24 +00001777/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1778 * MARK key value ... key value SETITEMS
1779 * opcode sequences. Calling code should have arranged to first create an
1780 * empty dict, or dict-like object, for the SETITEMS to operate on.
1781 * Returns 0 on success, <0 on error.
1782 *
1783 * This is very much like batch_list(). The difference between saving
1784 * elements directly, and picking apart two-tuples, is so long-winded at
1785 * the C level, though, that attempts to combine these routines were too
1786 * ugly to bear.
1787 */
1788static int
1789batch_dict(Picklerobject *self, PyObject *iter)
1790{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001791 PyObject *p = NULL;
1792 PyObject *firstitem = NULL;
1793 int i, n;
Tim Peters42f08ac2003-02-11 22:43:24 +00001794
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001795 static char setitem = SETITEM;
1796 static char setitems = SETITEMS;
Tim Peters42f08ac2003-02-11 22:43:24 +00001797
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001798 assert(iter != NULL);
Tim Peters42f08ac2003-02-11 22:43:24 +00001799
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001800 if (self->proto == 0) {
1801 /* SETITEMS isn't available; do one at a time. */
1802 for (;;) {
1803 p = PyIter_Next(iter);
1804 if (p == NULL) {
1805 if (PyErr_Occurred())
1806 return -1;
1807 break;
1808 }
1809 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1810 PyErr_SetString(PyExc_TypeError, "dict items "
1811 "iterator must return 2-tuples");
1812 return -1;
1813 }
1814 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1815 if (i >= 0)
1816 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1817 Py_DECREF(p);
1818 if (i < 0)
1819 return -1;
1820 if (self->write_func(self, &setitem, 1) < 0)
1821 return -1;
1822 }
1823 return 0;
1824 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001825
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001826 /* proto > 0: write in batches of BATCHSIZE. */
1827 do {
1828 /* Get first item */
1829 firstitem = PyIter_Next(iter);
1830 if (firstitem == NULL) {
1831 if (PyErr_Occurred())
1832 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001833
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001834 /* nothing more to add */
1835 break;
1836 }
1837 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1838 PyErr_SetString(PyExc_TypeError, "dict items "
1839 "iterator must return 2-tuples");
1840 goto BatchFailed;
1841 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001842
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001843 /* Try to get a second item */
1844 p = PyIter_Next(iter);
1845 if (p == NULL) {
1846 if (PyErr_Occurred())
1847 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001848
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001849 /* Only one item to write */
1850 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1851 goto BatchFailed;
1852 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1853 goto BatchFailed;
1854 if (self->write_func(self, &setitem, 1) < 0)
1855 goto BatchFailed;
1856 Py_CLEAR(firstitem);
1857 break;
1858 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001859
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001860 /* More than one item to write */
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001861
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001862 /* Pump out MARK, items, SETITEMS. */
1863 if (self->write_func(self, &MARKv, 1) < 0)
1864 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001865
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001866 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1867 goto BatchFailed;
1868 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1869 goto BatchFailed;
1870 Py_CLEAR(firstitem);
1871 n = 1;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001872
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001873 /* Fetch and save up to BATCHSIZE items */
1874 while (p) {
1875 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1876 PyErr_SetString(PyExc_TypeError, "dict items "
1877 "iterator must return 2-tuples");
1878 goto BatchFailed;
1879 }
1880 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1881 goto BatchFailed;
1882 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1883 goto BatchFailed;
1884 Py_CLEAR(p);
1885 n += 1;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001886
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001887 if (n == BATCHSIZE)
1888 break;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001889
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001890 p = PyIter_Next(iter);
1891 if (p == NULL) {
1892 if (PyErr_Occurred())
1893 goto BatchFailed;
1894 break;
1895 }
1896 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001897
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001898 if (self->write_func(self, &setitems, 1) < 0)
1899 goto BatchFailed;
Tim Peters42f08ac2003-02-11 22:43:24 +00001900
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001901 } while (n == BATCHSIZE);
1902 return 0;
Tim Peters42f08ac2003-02-11 22:43:24 +00001903
1904BatchFailed:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001905 Py_XDECREF(firstitem);
1906 Py_XDECREF(p);
1907 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001908}
1909
Collin Winter179bf212009-05-25 04:34:39 +00001910/* This is a variant of batch_dict() above that specializes for dicts, with no
1911 * support for dict subclasses. Like batch_dict(), we batch up chunks of
1912 * MARK key value ... key value SETITEMS
1913 * opcode sequences. Calling code should have arranged to first create an
1914 * empty dict, or dict-like object, for the SETITEMS to operate on.
1915 * Returns 0 on success, -1 on error.
1916 *
1917 * Note that this currently doesn't work for protocol 0.
1918 */
1919static int
1920batch_dict_exact(Picklerobject *self, PyObject *obj)
1921{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001922 PyObject *key = NULL, *value = NULL;
1923 int i;
1924 Py_ssize_t dict_size, ppos = 0;
Collin Winter179bf212009-05-25 04:34:39 +00001925
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001926 static char setitem = SETITEM;
1927 static char setitems = SETITEMS;
Collin Winter179bf212009-05-25 04:34:39 +00001928
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001929 assert(obj != NULL);
1930 assert(self->proto > 0);
Collin Winter179bf212009-05-25 04:34:39 +00001931
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001932 dict_size = PyDict_Size(obj);
Collin Winter179bf212009-05-25 04:34:39 +00001933
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001934 /* Special-case len(d) == 1 to save space. */
1935 if (dict_size == 1) {
1936 PyDict_Next(obj, &ppos, &key, &value);
1937 if (save(self, key, 0) < 0)
1938 return -1;
1939 if (save(self, value, 0) < 0)
1940 return -1;
1941 if (self->write_func(self, &setitem, 1) < 0)
1942 return -1;
1943 return 0;
1944 }
Collin Winter179bf212009-05-25 04:34:39 +00001945
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001946 /* Write in batches of BATCHSIZE. */
1947 do {
1948 i = 0;
1949 if (self->write_func(self, &MARKv, 1) < 0)
1950 return -1;
1951 while (PyDict_Next(obj, &ppos, &key, &value)) {
1952 if (save(self, key, 0) < 0)
1953 return -1;
1954 if (save(self, value, 0) < 0)
1955 return -1;
1956 if (++i == BATCHSIZE)
1957 break;
1958 }
1959 if (self->write_func(self, &setitems, 1) < 0)
1960 return -1;
1961 if (PyDict_Size(obj) != dict_size) {
1962 PyErr_Format(
1963 PyExc_RuntimeError,
1964 "dictionary changed size during iteration");
1965 return -1;
1966 }
Collin Winter179bf212009-05-25 04:34:39 +00001967
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001968 } while (i == BATCHSIZE);
1969 return 0;
Collin Winter179bf212009-05-25 04:34:39 +00001970}
1971
Guido van Rossum60456fd1997-04-09 17:36:32 +00001972static int
Tim Peterscba30e22003-02-01 06:24:36 +00001973save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001974{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001975 int res = -1;
1976 char s[3];
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001977 Py_ssize_t len;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001978
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001979 if (self->fast && !fast_save_enter(self, args))
1980 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001981
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001982 /* Create an empty dict. */
1983 if (self->bin) {
1984 s[0] = EMPTY_DICT;
1985 len = 1;
1986 }
1987 else {
1988 s[0] = MARK;
1989 s[1] = DICT;
1990 len = 2;
1991 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001992
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001993 if (self->write_func(self, s, len) < 0)
1994 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001995
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001996 /* Get dict size, and bow out early if empty. */
1997 if ((len = PyDict_Size(args)) < 0)
1998 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001999
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002000 if (len == 0) {
2001 if (put(self, args) >= 0)
2002 res = 0;
2003 goto finally;
2004 }
2005 if (put2(self, args) < 0)
2006 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002007
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002008 /* Materialize the dict items. */
2009 if (PyDict_CheckExact(args) && self->proto > 0) {
2010 /* We can take certain shortcuts if we know this is a dict and
2011 not a dict subclass. */
2012 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
2013 res = batch_dict_exact(self, args);
2014 Py_LeaveRecursiveCall();
2015 }
2016 } else {
2017 PyObject *iter = PyObject_CallMethod(args, "iteritems", "()");
2018 if (iter == NULL)
2019 goto finally;
2020 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
2021 res = batch_dict(self, iter);
2022 Py_LeaveRecursiveCall();
2023 }
2024 Py_DECREF(iter);
2025 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002026
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002027 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002028 if (self->fast && !fast_save_leave(self, args))
2029 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002031 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002032}
2033
2034
Tim Peters84e87f32001-03-17 04:50:51 +00002035static int
Tim Peterscba30e22003-02-01 06:24:36 +00002036save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002037{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002038 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
2039 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
2040 char *module_str, *name_str;
2041 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002042
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002043 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002044
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002045 if (self->fast && !fast_save_enter(self, args))
2046 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002047
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002048 if (self->write_func(self, &MARKv, 1) < 0)
2049 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002050
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002051 if (!( class = PyObject_GetAttr(args, __class___str)))
2052 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002053
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002054 if (self->bin) {
2055 if (save(self, class, 0) < 0)
2056 goto finally;
2057 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002058
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002059 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
2060 PyObject *element = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002061 Py_ssize_t i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002062
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002063 if (!( class_args =
2064 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
2065 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002066
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002067 if ((len = PyObject_Size(class_args)) < 0)
2068 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002069
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002070 for (i = 0; i < len; i++) {
2071 if (!( element = PySequence_GetItem(class_args, i)))
2072 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002073
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002074 if (save(self, element, 0) < 0) {
2075 Py_DECREF(element);
2076 goto finally;
2077 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002078
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002079 Py_DECREF(element);
2080 }
2081 }
2082 else {
2083 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2084 PyErr_Clear();
2085 else
2086 goto finally;
2087 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002088
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002089 if (!self->bin) {
2090 if (!( name = ((PyClassObject *)class)->cl_name )) {
2091 PyErr_SetString(PicklingError, "class has no name");
2092 goto finally;
2093 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002094
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002095 if (!( module = whichmodule(class, name)))
2096 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002097
Tim Peters84e87f32001-03-17 04:50:51 +00002098
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002099 if ((module_size = PyString_Size(module)) < 0 ||
2100 (name_size = PyString_Size(name)) < 0)
2101 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002102
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002103 module_str = PyString_AS_STRING((PyStringObject *)module);
2104 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002105
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002106 if (self->write_func(self, &inst, 1) < 0)
2107 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002108
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002109 if (self->write_func(self, module_str, module_size) < 0)
2110 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002111
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002112 if (self->write_func(self, "\n", 1) < 0)
2113 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002114
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002115 if (self->write_func(self, name_str, name_size) < 0)
2116 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002117
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002118 if (self->write_func(self, "\n", 1) < 0)
2119 goto finally;
2120 }
2121 else if (self->write_func(self, &obj, 1) < 0) {
2122 goto finally;
2123 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002124
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002125 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2126 state = PyObject_Call(getstate_func, empty_tuple, NULL);
2127 if (!state)
2128 goto finally;
2129 }
2130 else {
2131 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2132 PyErr_Clear();
2133 else
2134 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002135
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002136 if (!( state = PyObject_GetAttr(args, __dict___str))) {
2137 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2138 PyErr_Clear();
2139 else
2140 goto finally;
2141 res = 0;
2142 goto finally;
2143 }
2144 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002145
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002146 if (!PyDict_Check(state)) {
2147 if (put2(self, args) < 0)
2148 goto finally;
2149 }
2150 else {
2151 if (put(self, args) < 0)
2152 goto finally;
2153 }
Tim Peters84e87f32001-03-17 04:50:51 +00002154
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002155 if (save(self, state, 0) < 0)
2156 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002157
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002158 if (self->write_func(self, &build, 1) < 0)
2159 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002160
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002161 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002162
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002163 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002164 if (self->fast && !fast_save_leave(self, args))
2165 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002166
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002167 Py_XDECREF(module);
2168 Py_XDECREF(class);
2169 Py_XDECREF(state);
2170 Py_XDECREF(getinitargs_func);
2171 Py_XDECREF(getstate_func);
2172 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002173
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002174 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002175}
2176
2177
Guido van Rossum60456fd1997-04-09 17:36:32 +00002178static int
Tim Peterscba30e22003-02-01 06:24:36 +00002179save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002180{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002181 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
2182 char *name_str, *module_str;
2183 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002184
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002185 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002186
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002187 if (name) {
2188 global_name = name;
2189 Py_INCREF(global_name);
2190 }
2191 else {
2192 if (!( global_name = PyObject_GetAttr(args, __name___str)))
2193 goto finally;
2194 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002195
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002196 if (!( module = whichmodule(args, global_name)))
2197 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002198
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002199 if ((module_size = PyString_Size(module)) < 0 ||
2200 (name_size = PyString_Size(global_name)) < 0)
2201 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002202
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002203 module_str = PyString_AS_STRING((PyStringObject *)module);
2204 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002205
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002206 /* XXX This can be doing a relative import. Clearly it shouldn't,
2207 but I don't know how to stop it. :-( */
2208 mod = PyImport_ImportModule(module_str);
2209 if (mod == NULL) {
2210 cPickle_ErrFormat(PicklingError,
2211 "Can't pickle %s: import of module %s "
2212 "failed",
2213 "OS", args, module);
2214 goto finally;
2215 }
2216 klass = PyObject_GetAttrString(mod, name_str);
2217 if (klass == NULL) {
2218 cPickle_ErrFormat(PicklingError,
2219 "Can't pickle %s: attribute lookup %s.%s "
2220 "failed",
2221 "OSS", args, module, global_name);
2222 goto finally;
2223 }
2224 if (klass != args) {
2225 Py_DECREF(klass);
2226 cPickle_ErrFormat(PicklingError,
2227 "Can't pickle %s: it's not the same object "
2228 "as %s.%s",
2229 "OSS", args, module, global_name);
2230 goto finally;
2231 }
2232 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002233
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002234 if (self->proto >= 2) {
2235 /* See whether this is in the extension registry, and if
2236 * so generate an EXT opcode.
2237 */
2238 PyObject *py_code; /* extension code as Python object */
2239 long code; /* extension code as C value */
2240 char c_str[5];
2241 int n;
Tim Peters731098b2003-02-04 20:56:09 +00002242
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002243 PyTuple_SET_ITEM(two_tuple, 0, module);
2244 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2245 py_code = PyDict_GetItem(extension_registry, two_tuple);
2246 if (py_code == NULL)
2247 goto gen_global; /* not registered */
Tim Peters731098b2003-02-04 20:56:09 +00002248
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002249 /* Verify py_code has the right type and value. */
2250 if (!PyInt_Check(py_code)) {
2251 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2252 "extension code %s isn't an integer",
2253 "OO", args, py_code);
2254 goto finally;
2255 }
2256 code = PyInt_AS_LONG(py_code);
2257 if (code <= 0 || code > 0x7fffffffL) {
2258 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2259 "extension code %ld is out of range",
2260 "Ol", args, code);
2261 goto finally;
2262 }
Tim Peters731098b2003-02-04 20:56:09 +00002263
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002264 /* Generate an EXT opcode. */
2265 if (code <= 0xff) {
2266 c_str[0] = EXT1;
2267 c_str[1] = (char)code;
2268 n = 2;
2269 }
2270 else if (code <= 0xffff) {
2271 c_str[0] = EXT2;
2272 c_str[1] = (char)(code & 0xff);
2273 c_str[2] = (char)((code >> 8) & 0xff);
2274 n = 3;
2275 }
2276 else {
2277 c_str[0] = EXT4;
2278 c_str[1] = (char)(code & 0xff);
2279 c_str[2] = (char)((code >> 8) & 0xff);
2280 c_str[3] = (char)((code >> 16) & 0xff);
2281 c_str[4] = (char)((code >> 24) & 0xff);
2282 n = 5;
2283 }
Tim Peters731098b2003-02-04 20:56:09 +00002284
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002285 if (self->write_func(self, c_str, n) >= 0)
2286 res = 0;
2287 goto finally; /* and don't memoize */
2288 }
Tim Peters731098b2003-02-04 20:56:09 +00002289
2290 gen_global:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002291 if (self->write_func(self, &global, 1) < 0)
2292 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002293
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002294 if (self->write_func(self, module_str, module_size) < 0)
2295 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002296
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002297 if (self->write_func(self, "\n", 1) < 0)
2298 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002299
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002300 if (self->write_func(self, name_str, name_size) < 0)
2301 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002302
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002303 if (self->write_func(self, "\n", 1) < 0)
2304 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002305
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002306 if (put(self, args) < 0)
2307 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002308
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002309 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002310
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002311 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002312 Py_XDECREF(module);
2313 Py_XDECREF(global_name);
2314 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002315
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002316 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002317}
2318
Guido van Rossum60456fd1997-04-09 17:36:32 +00002319static int
Tim Peterscba30e22003-02-01 06:24:36 +00002320save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002321{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002322 PyObject *pid = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002323 Py_ssize_t size;
2324 int res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002325
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002326 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002327
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002328 Py_INCREF(args);
2329 ARG_TUP(self, args);
2330 if (self->arg) {
2331 pid = PyObject_Call(f, self->arg, NULL);
2332 FREE_ARG_TUP(self);
2333 }
2334 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002335
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002336 if (pid != Py_None) {
2337 if (!self->bin) {
2338 if (!PyString_Check(pid)) {
2339 PyErr_SetString(PicklingError,
2340 "persistent id must be string");
2341 goto finally;
2342 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002343
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002344 if (self->write_func(self, &persid, 1) < 0)
2345 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002346
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002347 if ((size = PyString_Size(pid)) < 0)
2348 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002349
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002350 if (self->write_func(self,
2351 PyString_AS_STRING(
2352 (PyStringObject *)pid),
2353 size) < 0)
2354 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002355
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002356 if (self->write_func(self, "\n", 1) < 0)
2357 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002358
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002359 res = 1;
2360 goto finally;
2361 }
2362 else if (save(self, pid, 1) >= 0) {
2363 if (self->write_func(self, &binpersid, 1) < 0)
2364 res = -1;
2365 else
2366 res = 1;
2367 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002368
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002369 goto finally;
2370 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002371
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002372 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002373
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002374 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002375 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002376
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002377 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002378}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002379
Tim Peters71fcda52003-02-14 23:05:28 +00002380/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2381 * appropriate __reduce__ method for ob.
2382 */
Tim Peters84e87f32001-03-17 04:50:51 +00002383static int
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002384save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002385{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002386 PyObject *callable;
2387 PyObject *argtup;
2388 PyObject *state = NULL;
2389 PyObject *listitems = Py_None;
2390 PyObject *dictitems = Py_None;
2391 Py_ssize_t size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002392
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002393 int use_newobj = self->proto >= 2;
Tim Peters71fcda52003-02-14 23:05:28 +00002394
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002395 static char reduce = REDUCE;
2396 static char build = BUILD;
2397 static char newobj = NEWOBJ;
Tim Peters71fcda52003-02-14 23:05:28 +00002398
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002399 size = PyTuple_Size(args);
2400 if (size < 2 || size > 5) {
2401 cPickle_ErrFormat(PicklingError, "tuple returned by "
2402 "%s must contain 2 through 5 elements",
2403 "O", fn);
2404 return -1;
2405 }
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002406
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002407 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2408 &callable,
2409 &argtup,
2410 &state,
2411 &listitems,
2412 &dictitems))
2413 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002414
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002415 if (!PyTuple_Check(argtup)) {
2416 cPickle_ErrFormat(PicklingError, "Second element of "
2417 "tuple returned by %s must be a tuple",
2418 "O", fn);
2419 return -1;
2420 }
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002421
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002422 if (state == Py_None)
2423 state = NULL;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002424
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002425 if (listitems == Py_None)
2426 listitems = NULL;
2427 else if (!PyIter_Check(listitems)) {
2428 cPickle_ErrFormat(PicklingError, "Fourth element of "
2429 "tuple returned by %s must be an iterator, not %s",
2430 "Os", fn, Py_TYPE(listitems)->tp_name);
2431 return -1;
2432 }
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002433
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002434 if (dictitems == Py_None)
2435 dictitems = NULL;
2436 else if (!PyIter_Check(dictitems)) {
2437 cPickle_ErrFormat(PicklingError, "Fifth element of "
2438 "tuple returned by %s must be an iterator, not %s",
2439 "Os", fn, Py_TYPE(dictitems)->tp_name);
2440 return -1;
2441 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002442
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002443 /* Protocol 2 special case: if callable's name is __newobj__, use
2444 * NEWOBJ. This consumes a lot of code.
2445 */
2446 if (use_newobj) {
2447 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002448
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002449 if (temp == NULL) {
2450 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2451 PyErr_Clear();
2452 else
2453 return -1;
2454 use_newobj = 0;
2455 }
2456 else {
2457 use_newobj = PyString_Check(temp) &&
2458 strcmp(PyString_AS_STRING(temp),
2459 "__newobj__") == 0;
2460 Py_DECREF(temp);
2461 }
2462 }
2463 if (use_newobj) {
2464 PyObject *cls;
2465 PyObject *newargtup;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002466 Py_ssize_t n, i;
Tim Peters71fcda52003-02-14 23:05:28 +00002467
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002468 /* Sanity checks. */
2469 n = PyTuple_Size(argtup);
2470 if (n < 1) {
2471 PyErr_SetString(PicklingError, "__newobj__ arglist "
2472 "is empty");
2473 return -1;
2474 }
Tim Peters71fcda52003-02-14 23:05:28 +00002475
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002476 cls = PyTuple_GET_ITEM(argtup, 0);
2477 if (! PyObject_HasAttrString(cls, "__new__")) {
2478 PyErr_SetString(PicklingError, "args[0] from "
2479 "__newobj__ args has no __new__");
2480 return -1;
2481 }
Tim Peters71fcda52003-02-14 23:05:28 +00002482
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002483 /* XXX How could ob be NULL? */
2484 if (ob != NULL) {
2485 PyObject *ob_dot_class;
Tim Peters71fcda52003-02-14 23:05:28 +00002486
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002487 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2488 if (ob_dot_class == NULL) {
2489 if (PyErr_ExceptionMatches(
2490 PyExc_AttributeError))
2491 PyErr_Clear();
2492 else
2493 return -1;
2494 }
2495 i = ob_dot_class != cls; /* true iff a problem */
2496 Py_XDECREF(ob_dot_class);
2497 if (i) {
2498 PyErr_SetString(PicklingError, "args[0] from "
2499 "__newobj__ args has the wrong class");
2500 return -1;
2501 }
2502 }
Tim Peters71fcda52003-02-14 23:05:28 +00002503
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002504 /* Save the class and its __new__ arguments. */
2505 if (save(self, cls, 0) < 0)
2506 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002507
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002508 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2509 if (newargtup == NULL)
2510 return -1;
2511 for (i = 1; i < n; ++i) {
2512 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2513 Py_INCREF(temp);
2514 PyTuple_SET_ITEM(newargtup, i-1, temp);
2515 }
2516 i = save(self, newargtup, 0);
2517 Py_DECREF(newargtup);
2518 if (i < 0)
2519 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002520
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002521 /* Add NEWOBJ opcode. */
2522 if (self->write_func(self, &newobj, 1) < 0)
2523 return -1;
2524 }
2525 else {
2526 /* Not using NEWOBJ. */
2527 if (save(self, callable, 0) < 0 ||
2528 save(self, argtup, 0) < 0 ||
2529 self->write_func(self, &reduce, 1) < 0)
2530 return -1;
2531 }
Tim Peters71fcda52003-02-14 23:05:28 +00002532
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002533 /* Memoize. */
2534 /* XXX How can ob be NULL? */
2535 if (ob != NULL) {
Serhiy Storchakada87e452015-11-07 11:15:32 +02002536 /* If the object is already in the memo, this means it is
2537 recursive. In this case, throw away everything we put on the
2538 stack, and fetch the object back from the memo. */
2539 if (Py_REFCNT(ob) > 1 && !self->fast) {
2540 PyObject *py_ob_id = PyLong_FromVoidPtr(ob);
2541 if (!py_ob_id)
2542 return -1;
2543 if (PyDict_GetItem(self->memo, py_ob_id)) {
2544 const char pop_op = POP;
2545 if (self->write_func(self, &pop_op, 1) < 0 ||
2546 get(self, py_ob_id) < 0) {
2547 Py_DECREF(py_ob_id);
2548 return -1;
2549 }
2550 Py_DECREF(py_ob_id);
2551 return 0;
2552 }
2553 Py_DECREF(py_ob_id);
2554 if (PyErr_Occurred())
2555 return -1;
2556 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002557 if (state && !PyDict_Check(state)) {
2558 if (put2(self, ob) < 0)
2559 return -1;
2560 }
2561 else if (put(self, ob) < 0)
2562 return -1;
2563 }
Tim Peters84e87f32001-03-17 04:50:51 +00002564
Guido van Rossum60456fd1997-04-09 17:36:32 +00002565
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002566 if (listitems && batch_list(self, listitems) < 0)
2567 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002568
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002569 if (dictitems && batch_dict(self, dictitems) < 0)
2570 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002571
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002572 if (state) {
2573 if (save(self, state, 0) < 0 ||
2574 self->write_func(self, &build, 1) < 0)
2575 return -1;
2576 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002577
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002578 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002579}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002580
Guido van Rossum60456fd1997-04-09 17:36:32 +00002581static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002582save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002583{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002584 PyTypeObject *type;
2585 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2586 int res = -1;
2587 int tmp;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002588
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002589 if (Py_EnterRecursiveCall(" while pickling an object"))
2590 return -1;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002591
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002592 if (!pers_save && self->pers_func) {
2593 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2594 res = tmp;
2595 goto finally;
2596 }
2597 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002598
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002599 if (args == Py_None) {
2600 res = save_none(self, args);
2601 goto finally;
2602 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002603
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002604 type = Py_TYPE(args);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002605
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002606 switch (type->tp_name[0]) {
2607 case 'b':
2608 if (args == Py_False || args == Py_True) {
2609 res = save_bool(self, args);
2610 goto finally;
2611 }
2612 break;
2613 case 'i':
2614 if (type == &PyInt_Type) {
2615 res = save_int(self, args);
2616 goto finally;
2617 }
2618 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002619
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002620 case 'l':
2621 if (type == &PyLong_Type) {
2622 res = save_long(self, args);
2623 goto finally;
2624 }
2625 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002626
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002627 case 'f':
2628 if (type == &PyFloat_Type) {
2629 res = save_float(self, args);
2630 goto finally;
2631 }
2632 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002633
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002634 case 't':
2635 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2636 res = save_tuple(self, args);
2637 goto finally;
2638 }
2639 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002640
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002641 case 's':
2642 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2643 res = save_string(self, args, 0);
2644 goto finally;
2645 }
2646 break;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002647
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002648#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002649 case 'u':
2650 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2651 res = save_unicode(self, args, 0);
2652 goto finally;
2653 }
2654 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002655#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002656 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002657
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002658 if (Py_REFCNT(args) > 1) {
2659 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2660 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002661
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002662 if (PyDict_GetItem(self->memo, py_ob_id)) {
2663 if (get(self, py_ob_id) < 0)
2664 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002665
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002666 res = 0;
2667 goto finally;
2668 }
2669 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002670
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002671 switch (type->tp_name[0]) {
2672 case 's':
2673 if (type == &PyString_Type) {
2674 res = save_string(self, args, 1);
2675 goto finally;
2676 }
2677 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002678
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002679#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002680 case 'u':
2681 if (type == &PyUnicode_Type) {
2682 res = save_unicode(self, args, 1);
2683 goto finally;
2684 }
2685 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002686#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002687
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002688 case 't':
2689 if (type == &PyTuple_Type) {
2690 res = save_tuple(self, args);
2691 goto finally;
2692 }
2693 if (type == &PyType_Type) {
Alexandre Vassalottidf9460f2013-11-30 17:43:42 -08002694 res = save_global(self, args, NULL);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002695 goto finally;
2696 }
2697 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002698
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002699 case 'l':
2700 if (type == &PyList_Type) {
2701 res = save_list(self, args);
2702 goto finally;
2703 }
2704 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002705
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002706 case 'd':
2707 if (type == &PyDict_Type) {
2708 res = save_dict(self, args);
2709 goto finally;
2710 }
2711 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002712
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002713 case 'i':
2714 if (type == &PyInstance_Type) {
2715 res = save_inst(self, args);
2716 goto finally;
2717 }
2718 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002719
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002720 case 'c':
2721 if (type == &PyClass_Type) {
2722 res = save_global(self, args, NULL);
2723 goto finally;
2724 }
2725 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002726
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002727 case 'f':
2728 if (type == &PyFunction_Type) {
2729 res = save_global(self, args, NULL);
2730 if (res && PyErr_ExceptionMatches(PickleError)) {
2731 /* fall back to reduce */
2732 PyErr_Clear();
2733 break;
2734 }
2735 goto finally;
2736 }
2737 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002738
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002739 case 'b':
2740 if (type == &PyCFunction_Type) {
2741 res = save_global(self, args, NULL);
2742 goto finally;
2743 }
2744 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002745
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002746 if (!pers_save && self->inst_pers_func) {
2747 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2748 res = tmp;
2749 goto finally;
2750 }
2751 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002752
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002753 /* Get a reduction callable, and call it. This may come from
2754 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2755 * or the object's __reduce__ method.
2756 */
2757 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2758 if (__reduce__ != NULL) {
2759 Py_INCREF(__reduce__);
2760 Py_INCREF(args);
2761 ARG_TUP(self, args);
2762 if (self->arg) {
2763 t = PyObject_Call(__reduce__, self->arg, NULL);
2764 FREE_ARG_TUP(self);
2765 }
2766 }
2767 else {
Antoine Pitrou561a8212011-10-04 09:34:48 +02002768 if (PyType_IsSubtype(type, &PyType_Type)) {
2769 res = save_global(self, args, NULL);
2770 goto finally;
2771 }
2772
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002773 /* Check for a __reduce_ex__ method. */
2774 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2775 if (__reduce__ != NULL) {
2776 t = PyInt_FromLong(self->proto);
2777 if (t != NULL) {
2778 ARG_TUP(self, t);
2779 t = NULL;
2780 if (self->arg) {
2781 t = PyObject_Call(__reduce__,
2782 self->arg, NULL);
2783 FREE_ARG_TUP(self);
2784 }
2785 }
2786 }
2787 else {
2788 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2789 PyErr_Clear();
2790 else
2791 goto finally;
2792 /* Check for a __reduce__ method. */
2793 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2794 if (__reduce__ != NULL) {
2795 t = PyObject_Call(__reduce__,
2796 empty_tuple, NULL);
2797 }
2798 else {
2799 PyErr_SetObject(UnpickleableError, args);
2800 goto finally;
2801 }
2802 }
2803 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002804
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002805 if (t == NULL)
2806 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002807
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002808 if (PyString_Check(t)) {
2809 res = save_global(self, args, t);
2810 goto finally;
2811 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002812
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002813 if (!PyTuple_Check(t)) {
2814 cPickle_ErrFormat(PicklingError, "Value returned by "
2815 "%s must be string or tuple",
2816 "O", __reduce__);
2817 goto finally;
2818 }
Tim Peters71fcda52003-02-14 23:05:28 +00002819
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002820 res = save_reduce(self, t, __reduce__, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002821
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002822 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002823 Py_LeaveRecursiveCall();
2824 Py_XDECREF(py_ob_id);
2825 Py_XDECREF(__reduce__);
2826 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002827
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002828 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002829}
2830
2831
2832static int
Tim Peterscba30e22003-02-01 06:24:36 +00002833dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002834{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002835 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002836
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002837 if (self->proto >= 2) {
2838 char bytes[2];
Tim Peters4190fb82003-02-02 16:09:05 +00002839
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002840 bytes[0] = PROTO;
2841 assert(self->proto >= 0 && self->proto < 256);
2842 bytes[1] = (char)self->proto;
2843 if (self->write_func(self, bytes, 2) < 0)
2844 return -1;
2845 }
Tim Peters4190fb82003-02-02 16:09:05 +00002846
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002847 if (save(self, args, 0) < 0)
2848 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002849
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002850 if (self->write_func(self, &stop, 1) < 0)
2851 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002852
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002853 if (self->write_func(self, NULL, 0) < 0)
2854 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002855
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002856 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002857}
2858
2859static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002860Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002861{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002862 if (self->memo)
2863 PyDict_Clear(self->memo);
2864 Py_INCREF(Py_None);
2865 return Py_None;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002866}
2867
2868static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002869Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002870{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002871 Py_ssize_t l, i, rsize, ssize, clear=1, lm;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002872 long ik;
2873 PyObject *k, *r;
2874 char *s, *p, *have_get;
2875 Pdata *data;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002876
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002877 /* Can be called by Python code or C code */
2878 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2879 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002880
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002881 /* Check to make sure we are based on a list */
2882 if (! Pdata_Check(self->file)) {
2883 PyErr_SetString(PicklingError,
2884 "Attempt to getvalue() a non-list-based pickler");
2885 return NULL;
2886 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002887
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002888 /* flush write buffer */
2889 if (write_other(self, NULL, 0) < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002890
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002891 data=(Pdata*)self->file;
2892 l=data->length;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002893
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002894 /* set up an array to hold get/put status */
2895 lm = PyDict_Size(self->memo);
2896 if (lm < 0) return NULL;
2897 lm++;
2898 have_get = malloc(lm);
2899 if (have_get == NULL) return PyErr_NoMemory();
2900 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002901
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002902 /* Scan for gets. */
2903 for (rsize = 0, i = l; --i >= 0; ) {
2904 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002905
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002906 if (PyString_Check(k))
2907 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002908
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002909 else if (PyInt_Check(k)) { /* put */
2910 ik = PyInt_AS_LONG((PyIntObject*)k);
2911 if (ik >= lm || ik == 0) {
2912 PyErr_SetString(PicklingError,
2913 "Invalid get data");
2914 goto err;
2915 }
2916 if (have_get[ik]) /* with matching get */
2917 rsize += ik < 256 ? 2 : 5;
2918 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002919
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002920 else if (! (PyTuple_Check(k) &&
2921 PyTuple_GET_SIZE(k) == 2 &&
2922 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2923 ) {
2924 PyErr_SetString(PicklingError,
2925 "Unexpected data in internal list");
2926 goto err;
2927 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002928
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002929 else { /* put */
2930 ik = PyInt_AS_LONG((PyIntObject *)k);
2931 if (ik >= lm || ik == 0) {
2932 PyErr_SetString(PicklingError,
2933 "Invalid get data");
Benjamin Peterson7f18ac42015-07-25 10:20:13 -07002934 goto err;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002935 }
2936 have_get[ik] = 1;
2937 rsize += ik < 256 ? 2 : 5;
2938 }
2939 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002940
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002941 /* Now generate the result */
2942 r = PyString_FromStringAndSize(NULL, rsize);
2943 if (r == NULL) goto err;
2944 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002945
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002946 for (i = 0; i < l; i++) {
2947 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002948
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002949 if (PyString_Check(k)) {
2950 ssize = PyString_GET_SIZE(k);
2951 if (ssize) {
2952 p=PyString_AS_STRING((PyStringObject *)k);
2953 while (--ssize >= 0)
2954 *s++ = *p++;
2955 }
2956 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002957
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002958 else if (PyTuple_Check(k)) { /* get */
2959 ik = PyInt_AS_LONG((PyIntObject *)
2960 PyTuple_GET_ITEM(k, 0));
2961 if (ik < 256) {
2962 *s++ = BINGET;
2963 *s++ = (int)(ik & 0xff);
2964 }
2965 else {
2966 *s++ = LONG_BINGET;
2967 *s++ = (int)(ik & 0xff);
2968 *s++ = (int)((ik >> 8) & 0xff);
2969 *s++ = (int)((ik >> 16) & 0xff);
2970 *s++ = (int)((ik >> 24) & 0xff);
2971 }
2972 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002973
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002974 else { /* put */
2975 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002976
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002977 if (have_get[ik]) { /* with matching get */
2978 if (ik < 256) {
2979 *s++ = BINPUT;
2980 *s++ = (int)(ik & 0xff);
2981 }
2982 else {
2983 *s++ = LONG_BINPUT;
2984 *s++ = (int)(ik & 0xff);
2985 *s++ = (int)((ik >> 8) & 0xff);
2986 *s++ = (int)((ik >> 16) & 0xff);
2987 *s++ = (int)((ik >> 24) & 0xff);
2988 }
2989 }
2990 }
2991 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002992
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002993 if (clear) {
2994 PyDict_Clear(self->memo);
2995 Pdata_clear(data, 0);
2996 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002997
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002998 free(have_get);
2999 return r;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003000 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003001 free(have_get);
3002 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003003}
3004
3005static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003006Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003007{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003008 PyObject *ob;
3009 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003010
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003011 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
3012 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003013
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003014 if (dump(self, ob) < 0)
3015 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003016
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003017 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003018
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003019 /* XXX Why does dump() return self? */
3020 Py_INCREF(self);
3021 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003022}
3023
3024
Tim Peterscba30e22003-02-01 06:24:36 +00003025static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003026{
Neal Norwitzb0493252002-03-31 14:44:22 +00003027 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003028 PyDoc_STR("dump(object) -- "
3029 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00003030 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003031 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00003032 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003033 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00003034 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003035};
3036
3037
3038static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00003039newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003040{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003041 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003042
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003043 if (proto < 0)
3044 proto = HIGHEST_PROTOCOL;
3045 if (proto > HIGHEST_PROTOCOL) {
3046 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
3047 "the highest available protocol is %d",
3048 proto, HIGHEST_PROTOCOL);
3049 return NULL;
3050 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003051
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003052 self = PyObject_GC_New(Picklerobject, &Picklertype);
3053 if (self == NULL)
3054 return NULL;
3055 self->proto = proto;
3056 self->bin = proto > 0;
3057 self->fp = NULL;
3058 self->write = NULL;
3059 self->memo = NULL;
3060 self->arg = NULL;
3061 self->pers_func = NULL;
3062 self->inst_pers_func = NULL;
3063 self->write_buf = NULL;
3064 self->fast = 0;
3065 self->fast_container = 0;
3066 self->fast_memo = NULL;
3067 self->buf_size = 0;
3068 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003069
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003070 self->file = NULL;
3071 if (file)
3072 Py_INCREF(file);
3073 else {
3074 file = Pdata_New();
3075 if (file == NULL)
3076 goto err;
3077 }
3078 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003079
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003080 if (!( self->memo = PyDict_New()))
3081 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003082
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003083 if (PyFile_Check(file)) {
3084 self->fp = PyFile_AsFile(file);
3085 if (self->fp == NULL) {
3086 PyErr_SetString(PyExc_ValueError,
3087 "I/O operation on closed file");
3088 goto err;
3089 }
3090 self->write_func = write_file;
3091 }
3092 else if (PycStringIO_OutputCheck(file)) {
3093 self->write_func = write_cStringIO;
3094 }
3095 else if (file == Py_None) {
3096 self->write_func = write_none;
3097 }
3098 else {
3099 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003100
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003101 if (! Pdata_Check(file)) {
3102 self->write = PyObject_GetAttr(file, write_str);
3103 if (!self->write) {
3104 PyErr_Clear();
3105 PyErr_SetString(PyExc_TypeError,
3106 "argument must have 'write' "
3107 "attribute");
3108 goto err;
3109 }
3110 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003111
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003112 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
3113 if (self->write_buf == NULL) {
3114 PyErr_NoMemory();
3115 goto err;
3116 }
3117 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003118
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003119 if (PyEval_GetRestricted()) {
3120 /* Restricted execution, get private tables */
3121 PyObject *m = PyImport_ImportModule("copy_reg");
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003122
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003123 if (m == NULL)
3124 goto err;
3125 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
3126 Py_DECREF(m);
3127 if (self->dispatch_table == NULL)
3128 goto err;
3129 }
3130 else {
3131 self->dispatch_table = dispatch_table;
3132 Py_INCREF(dispatch_table);
3133 }
3134 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003135
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003136 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003138 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003139 Py_DECREF(self);
3140 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003141}
3142
3143
3144static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00003145get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003146{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003147 static char *kwlist[] = {"file", "protocol", NULL};
3148 PyObject *file = NULL;
3149 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003150
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003151 /* XXX
3152 * The documented signature is Pickler(file, protocol=0), but this
3153 * accepts Pickler() and Pickler(integer) too. The meaning then
3154 * is clear as mud, undocumented, and not supported by pickle.py.
3155 * I'm told Zope uses this, but I haven't traced into this code
3156 * far enough to figure out what it means.
3157 */
3158 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
3159 PyErr_Clear();
3160 proto = 0;
3161 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
3162 kwlist, &file, &proto))
3163 return NULL;
3164 }
3165 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003166}
3167
3168
3169static void
Tim Peterscba30e22003-02-01 06:24:36 +00003170Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003171{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003172 PyObject_GC_UnTrack(self);
3173 Py_XDECREF(self->write);
3174 Py_XDECREF(self->memo);
3175 Py_XDECREF(self->fast_memo);
3176 Py_XDECREF(self->arg);
3177 Py_XDECREF(self->file);
3178 Py_XDECREF(self->pers_func);
3179 Py_XDECREF(self->inst_pers_func);
3180 Py_XDECREF(self->dispatch_table);
3181 PyMem_Free(self->write_buf);
3182 Py_TYPE(self)->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003183}
3184
3185static int
3186Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
3187{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003188 Py_VISIT(self->write);
3189 Py_VISIT(self->memo);
3190 Py_VISIT(self->fast_memo);
3191 Py_VISIT(self->arg);
3192 Py_VISIT(self->file);
3193 Py_VISIT(self->pers_func);
3194 Py_VISIT(self->inst_pers_func);
3195 Py_VISIT(self->dispatch_table);
3196 return 0;
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003197}
3198
3199static int
3200Pickler_clear(Picklerobject *self)
3201{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003202 Py_CLEAR(self->write);
3203 Py_CLEAR(self->memo);
3204 Py_CLEAR(self->fast_memo);
3205 Py_CLEAR(self->arg);
3206 Py_CLEAR(self->file);
3207 Py_CLEAR(self->pers_func);
3208 Py_CLEAR(self->inst_pers_func);
3209 Py_CLEAR(self->dispatch_table);
3210 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003211}
3212
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003213static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003214Pickler_get_pers_func(Picklerobject *p)
3215{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003216 if (p->pers_func == NULL)
3217 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3218 else
3219 Py_INCREF(p->pers_func);
3220 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003221}
3222
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003223static int
3224Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3225{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003226 if (v == NULL) {
3227 PyErr_SetString(PyExc_TypeError,
3228 "attribute deletion is not supported");
3229 return -1;
3230 }
3231 Py_XDECREF(p->pers_func);
3232 Py_INCREF(v);
3233 p->pers_func = v;
3234 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003235}
3236
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003237static int
3238Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3239{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003240 if (v == NULL) {
3241 PyErr_SetString(PyExc_TypeError,
3242 "attribute deletion is not supported");
3243 return -1;
3244 }
3245 Py_XDECREF(p->inst_pers_func);
3246 Py_INCREF(v);
3247 p->inst_pers_func = v;
3248 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003249}
3250
3251static PyObject *
3252Pickler_get_memo(Picklerobject *p)
3253{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003254 if (p->memo == NULL)
3255 PyErr_SetString(PyExc_AttributeError, "memo");
3256 else
3257 Py_INCREF(p->memo);
3258 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003259}
3260
3261static int
3262Pickler_set_memo(Picklerobject *p, PyObject *v)
3263{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003264 if (v == NULL) {
3265 PyErr_SetString(PyExc_TypeError,
3266 "attribute deletion is not supported");
3267 return -1;
3268 }
3269 if (!PyDict_Check(v)) {
3270 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3271 return -1;
3272 }
3273 Py_XDECREF(p->memo);
3274 Py_INCREF(v);
3275 p->memo = v;
3276 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003277}
3278
3279static PyObject *
3280Pickler_get_error(Picklerobject *p)
3281{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003282 /* why is this an attribute on the Pickler? */
3283 Py_INCREF(PicklingError);
3284 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003285}
3286
3287static PyMemberDef Pickler_members[] = {
3288 {"binary", T_INT, offsetof(Picklerobject, bin)},
3289 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003290 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003291};
3292
3293static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003294 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003295 (setter)Pickler_set_pers_func},
3296 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3297 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003298 {"PicklingError", (getter)Pickler_get_error, NULL},
3299 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003300};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003301
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003302PyDoc_STRVAR(Picklertype__doc__,
3303"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003304
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003305static PyTypeObject Picklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003306 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00003307 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003308 sizeof(Picklerobject), /*tp_basicsize*/
3309 0,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003310 (destructor)Pickler_dealloc, /* tp_dealloc */
3311 0, /* tp_print */
3312 0, /* tp_getattr */
3313 0, /* tp_setattr */
3314 0, /* tp_compare */
3315 0, /* tp_repr */
3316 0, /* tp_as_number */
3317 0, /* tp_as_sequence */
3318 0, /* tp_as_mapping */
3319 0, /* tp_hash */
3320 0, /* tp_call */
3321 0, /* tp_str */
3322 PyObject_GenericGetAttr, /* tp_getattro */
3323 PyObject_GenericSetAttr, /* tp_setattro */
3324 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003325 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003326 Picklertype__doc__, /* tp_doc */
3327 (traverseproc)Pickler_traverse, /* tp_traverse */
3328 (inquiry)Pickler_clear, /* tp_clear */
3329 0, /* tp_richcompare */
3330 0, /* tp_weaklistoffset */
3331 0, /* tp_iter */
3332 0, /* tp_iternext */
3333 Pickler_methods, /* tp_methods */
3334 Pickler_members, /* tp_members */
3335 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003336};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003337
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003338static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003339find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003340{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003341 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003342
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003343 if (fc) {
3344 if (fc==Py_None) {
3345 PyErr_SetString(UnpicklingError, "Global and instance "
3346 "pickles are not supported.");
3347 return NULL;
3348 }
3349 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3350 py_global_name, NULL);
3351 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003352
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003353 module = PySys_GetObject("modules");
3354 if (module == NULL)
3355 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003356
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003357 module = PyDict_GetItem(module, py_module_name);
3358 if (module == NULL) {
3359 module = PyImport_Import(py_module_name);
3360 if (!module)
3361 return NULL;
3362 global = PyObject_GetAttr(module, py_global_name);
3363 Py_DECREF(module);
3364 }
3365 else
3366 global = PyObject_GetAttr(module, py_global_name);
3367 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003368}
3369
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003370static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +00003371marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003372{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003373 if (self->num_marks < 1) {
3374 PyErr_SetString(UnpicklingError, "could not find MARK");
3375 return -1;
3376 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003377
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003378 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003379}
3380
Tim Peters84e87f32001-03-17 04:50:51 +00003381
Guido van Rossum60456fd1997-04-09 17:36:32 +00003382static int
Tim Peterscba30e22003-02-01 06:24:36 +00003383load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003384{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003385 PDATA_APPEND(self->stack, Py_None, -1);
3386 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003387}
3388
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003389static int
Tim Peterscba30e22003-02-01 06:24:36 +00003390bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003391{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003392 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3393 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003394}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003395
3396static int
Tim Peterscba30e22003-02-01 06:24:36 +00003397load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003398{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003399 PyObject *py_int = 0;
3400 char *endptr, *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003401 Py_ssize_t len;
3402 int res = -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003403 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003404
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003405 if ((len = self->readline_func(self, &s)) < 0) return -1;
3406 if (len < 2) return bad_readline();
3407 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003408
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003409 errno = 0;
3410 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003411
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003412 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3413 /* Hm, maybe we've got something long. Let's try reading
3414 it as a Python long object. */
3415 errno = 0;
3416 py_int = PyLong_FromString(s, NULL, 0);
3417 if (py_int == NULL) {
3418 PyErr_SetString(PyExc_ValueError,
3419 "could not convert string to int");
3420 goto finally;
3421 }
3422 }
3423 else {
3424 if (len == 3 && (l == 0 || l == 1)) {
3425 if (!( py_int = PyBool_FromLong(l))) goto finally;
3426 }
3427 else {
3428 if (!( py_int = PyInt_FromLong(l))) goto finally;
3429 }
3430 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003431
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003432 free(s);
3433 PDATA_PUSH(self->stack, py_int, -1);
3434 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003435
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003436 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003437 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003438
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003439 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003440}
3441
Tim Peters3c67d792003-02-02 17:59:11 +00003442static int
3443load_bool(Unpicklerobject *self, PyObject *boolean)
3444{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003445 assert(boolean == Py_True || boolean == Py_False);
3446 PDATA_APPEND(self->stack, boolean, -1);
3447 return 0;
Tim Peters3c67d792003-02-02 17:59:11 +00003448}
3449
Tim Petersee1a53c2003-02-02 02:57:53 +00003450/* s contains x bytes of a little-endian integer. Return its value as a
3451 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3452 * int, but when x is 4 it's a signed one. This is an historical source
3453 * of x-platform bugs.
3454 */
Tim Peters84e87f32001-03-17 04:50:51 +00003455static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003456calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003457{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003458 unsigned char c;
3459 int i;
3460 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003461
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003462 for (i = 0, l = 0L; i < x; i++) {
3463 c = (unsigned char)s[i];
3464 l |= (long)c << (i * 8);
3465 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003466#if SIZEOF_LONG > 4
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003467 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3468 * is signed, so on a box with longs bigger than 4 bytes we need
3469 * to extend a BININT's sign bit to the full width.
3470 */
3471 if (x == 4 && l & (1L << 31))
Gregory P. Smith0a857282015-08-04 16:29:00 -07003472 l |= (~0UL) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003473#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003474 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003475}
3476
3477
3478static int
Tim Peterscba30e22003-02-01 06:24:36 +00003479load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003480{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003481 PyObject *py_int = 0;
3482 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003483
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003484 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003485
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003486 if (!( py_int = PyInt_FromLong(l)))
3487 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003488
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003489 PDATA_PUSH(self->stack, py_int, -1);
3490 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003491}
3492
3493
3494static int
Tim Peterscba30e22003-02-01 06:24:36 +00003495load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003496{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003497 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003498
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003499 if (self->read_func(self, &s, 4) < 0)
3500 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003501
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003502 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003503}
3504
3505
3506static int
Tim Peterscba30e22003-02-01 06:24:36 +00003507load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003508{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003509 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003510
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003511 if (self->read_func(self, &s, 1) < 0)
3512 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003513
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003514 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003515}
3516
3517
3518static int
Tim Peterscba30e22003-02-01 06:24:36 +00003519load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003520{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003521 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003522
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003523 if (self->read_func(self, &s, 2) < 0)
3524 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003525
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003526 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003527}
Tim Peters84e87f32001-03-17 04:50:51 +00003528
Guido van Rossum60456fd1997-04-09 17:36:32 +00003529static int
Tim Peterscba30e22003-02-01 06:24:36 +00003530load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003531{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003532 PyObject *l = 0;
3533 char *end, *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003534 Py_ssize_t len;
3535 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003536
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003537 if ((len = self->readline_func(self, &s)) < 0) return -1;
3538 if (len < 2) return bad_readline();
3539 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003540
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003541 if (!( l = PyLong_FromString(s, &end, 0)))
3542 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003543
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003544 free(s);
3545 PDATA_PUSH(self->stack, l, -1);
3546 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003547
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003548 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003549 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003550
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003551 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003552}
3553
Tim Petersee1a53c2003-02-02 02:57:53 +00003554/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3555 * data following.
3556 */
3557static int
3558load_counted_long(Unpicklerobject *self, int size)
3559{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003560 Py_ssize_t i;
3561 char *nbytes;
3562 unsigned char *pdata;
3563 PyObject *along;
Tim Petersee1a53c2003-02-02 02:57:53 +00003564
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003565 assert(size == 1 || size == 4);
3566 i = self->read_func(self, &nbytes, size);
3567 if (i < 0) return -1;
Tim Petersee1a53c2003-02-02 02:57:53 +00003568
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003569 size = calc_binint(nbytes, size);
3570 if (size < 0) {
3571 /* Corrupt or hostile pickle -- we never write one like
3572 * this.
3573 */
3574 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3575 "byte count");
3576 return -1;
3577 }
Tim Petersee1a53c2003-02-02 02:57:53 +00003578
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003579 if (size == 0)
3580 along = PyLong_FromLong(0L);
3581 else {
3582 /* Read the raw little-endian bytes & convert. */
3583 i = self->read_func(self, (char **)&pdata, size);
3584 if (i < 0) return -1;
3585 along = _PyLong_FromByteArray(pdata, (size_t)size,
3586 1 /* little endian */, 1 /* signed */);
3587 }
3588 if (along == NULL)
3589 return -1;
3590 PDATA_PUSH(self->stack, along, -1);
3591 return 0;
Tim Petersee1a53c2003-02-02 02:57:53 +00003592}
Tim Peters84e87f32001-03-17 04:50:51 +00003593
Guido van Rossum60456fd1997-04-09 17:36:32 +00003594static int
Tim Peterscba30e22003-02-01 06:24:36 +00003595load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003596{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003597 PyObject *py_float = 0;
3598 char *endptr, *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003599 Py_ssize_t len;
3600 int res = -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003601 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003602
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003603 if ((len = self->readline_func(self, &s)) < 0) return -1;
3604 if (len < 2) return bad_readline();
3605 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003606
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003607 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003608
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003609 if (d == -1.0 && PyErr_Occurred()) {
3610 goto finally;
3611 } else if ((endptr[0] != '\n') || (endptr[1] != '\0')) {
3612 PyErr_SetString(PyExc_ValueError,
3613 "could not convert string to float");
3614 goto finally;
3615 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003616
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003617 if (!( py_float = PyFloat_FromDouble(d)))
3618 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003619
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003620 free(s);
3621 PDATA_PUSH(self->stack, py_float, -1);
3622 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003624 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003625 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003626
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003627 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003628}
3629
Guido van Rossum60456fd1997-04-09 17:36:32 +00003630static int
Tim Peterscba30e22003-02-01 06:24:36 +00003631load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003632{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003633 PyObject *py_float;
3634 double x;
3635 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003636
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003637 if (self->read_func(self, &p, 8) < 0)
3638 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003639
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003640 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3641 if (x == -1.0 && PyErr_Occurred())
3642 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003643
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003644 py_float = PyFloat_FromDouble(x);
3645 if (py_float == NULL)
3646 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003647
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003648 PDATA_PUSH(self->stack, py_float, -1);
3649 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003650}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003651
3652static int
Tim Peterscba30e22003-02-01 06:24:36 +00003653load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003654{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003655 PyObject *str = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003656 Py_ssize_t len;
3657 int res = -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003658 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003659
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003660 if ((len = self->readline_func(self, &s)) < 0) return -1;
3661 if (len < 2) return bad_readline();
3662 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003663
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003664
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003665 /* Strip outermost quotes */
Antoine Pitroube929712013-04-15 21:35:25 +02003666 while (len > 0 && s[len-1] <= ' ')
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003667 len--;
Antoine Pitroube929712013-04-15 21:35:25 +02003668 if (len > 1 && s[0]=='"' && s[len-1]=='"') {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003669 s[len-1] = '\0';
3670 p = s + 1 ;
3671 len -= 2;
Antoine Pitroube929712013-04-15 21:35:25 +02003672 }
3673 else if (len > 1 && s[0]=='\'' && s[len-1]=='\'') {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003674 s[len-1] = '\0';
3675 p = s + 1 ;
3676 len -= 2;
Antoine Pitroube929712013-04-15 21:35:25 +02003677 }
3678 else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003679 goto insecure;
3680 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003681
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003682 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3683 free(s);
3684 if (str) {
3685 PDATA_PUSH(self->stack, str, -1);
3686 res = 0;
3687 }
3688 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003689
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003690 insecure:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003691 free(s);
3692 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3693 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003694}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003695
3696
3697static int
Tim Peterscba30e22003-02-01 06:24:36 +00003698load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003699{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003700 PyObject *py_string = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003701 Py_ssize_t l;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003702 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003703
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003704 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003705
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003706 l = calc_binint(s, 4);
3707 if (l < 0) {
3708 /* Corrupt or hostile pickle -- we never write one like
3709 * this.
3710 */
3711 PyErr_SetString(UnpicklingError,
3712 "BINSTRING pickle has negative byte count");
3713 return -1;
3714 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003715
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003716 if (self->read_func(self, &s, l) < 0)
3717 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003718
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003719 if (!( py_string = PyString_FromStringAndSize(s, l)))
3720 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003721
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003722 PDATA_PUSH(self->stack, py_string, -1);
3723 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003724}
3725
3726
3727static int
Tim Peterscba30e22003-02-01 06:24:36 +00003728load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003729{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003730 PyObject *py_string = 0;
3731 unsigned char l;
3732 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003733
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003734 if (self->read_func(self, &s, 1) < 0)
3735 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003736
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003737 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003738
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003739 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003740
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003741 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003742
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003743 PDATA_PUSH(self->stack, py_string, -1);
3744 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003745}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003746
3747
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003748#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003749static int
Tim Peterscba30e22003-02-01 06:24:36 +00003750load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003751{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003752 PyObject *str = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003753 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003754 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003755
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003756 if ((len = self->readline_func(self, &s)) < 0) return -1;
3757 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003758
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003759 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003760 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003761
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003762 PDATA_PUSH(self->stack, str, -1);
3763 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003764}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003765#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003766
3767
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003768#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003769static int
Tim Peterscba30e22003-02-01 06:24:36 +00003770load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003771{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003772 PyObject *unicode;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003773 Py_ssize_t l;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003774 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003775
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003776 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003777
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003778 l = calc_binint(s, 4);
3779 if (l < 0) {
3780 /* Corrupt or hostile pickle -- we never write one like
3781 * this.
3782 */
3783 PyErr_SetString(UnpicklingError,
3784 "BINUNICODE pickle has negative byte count");
3785 return -1;
3786 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003787
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003788 if (self->read_func(self, &s, l) < 0)
3789 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003790
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003791 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3792 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003793
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003794 PDATA_PUSH(self->stack, unicode, -1);
3795 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003796}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003797#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003798
3799
3800static int
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003801load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003802{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003803 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003804
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003805 if (self->stack->length < len)
3806 return stackUnderflow();
3807
3808 if (!(tup = Pdata_popTuple(self->stack, self->stack->length - len)))
3809 return -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003810 PDATA_PUSH(self->stack, tup, -1);
3811 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003812}
3813
3814static int
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003815load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003816{
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003817 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003818
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003819 if ((i = marker(self)) < 0) return -1;
3820 return load_counted_tuple(self, self->stack->length - i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003821}
3822
3823static int
Tim Peterscba30e22003-02-01 06:24:36 +00003824load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003825{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003826 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003827
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003828 if (!( list=PyList_New(0))) return -1;
3829 PDATA_PUSH(self->stack, list, -1);
3830 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003831}
3832
3833static int
Tim Peterscba30e22003-02-01 06:24:36 +00003834load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003835{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003836 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003837
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003838 if (!( dict=PyDict_New())) return -1;
3839 PDATA_PUSH(self->stack, dict, -1);
3840 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003841}
3842
3843
3844static int
Tim Peterscba30e22003-02-01 06:24:36 +00003845load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003846{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003847 PyObject *list = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003848 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003849
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003850 if ((i = marker(self)) < 0) return -1;
3851 if (!( list=Pdata_popList(self->stack, i))) return -1;
3852 PDATA_PUSH(self->stack, list, -1);
3853 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003854}
3855
3856static int
Tim Peterscba30e22003-02-01 06:24:36 +00003857load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003858{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003859 PyObject *dict, *key, *value;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003860 Py_ssize_t i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003861
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003862 if ((i = marker(self)) < 0) return -1;
3863 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003864
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003865 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003866
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003867 for (k = i+1; k < j; k += 2) {
3868 key =self->stack->data[k-1];
3869 value=self->stack->data[k ];
3870 if (PyDict_SetItem(dict, key, value) < 0) {
3871 Py_DECREF(dict);
3872 return -1;
3873 }
3874 }
3875 Pdata_clear(self->stack, i);
3876 PDATA_PUSH(self->stack, dict, -1);
3877 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003878}
3879
3880static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003881Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003882{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003883 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003884
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003885 if (PyClass_Check(cls)) {
3886 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003887
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003888 if ((l=PyObject_Size(args)) < 0) goto err;
3889 if (!( l )) {
3890 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003891
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003892 __getinitargs__ = PyObject_GetAttr(cls,
3893 __getinitargs___str);
3894 if (!__getinitargs__) {
3895 /* We have a class with no __getinitargs__,
3896 so bypass usual construction */
3897 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003898
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003899 PyErr_Clear();
3900 if (!( inst=PyInstance_NewRaw(cls, NULL)))
3901 goto err;
3902 return inst;
3903 }
3904 Py_DECREF(__getinitargs__);
3905 }
Tim Peters84e87f32001-03-17 04:50:51 +00003906
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003907 if ((r=PyInstance_New(cls, args, NULL))) return r;
3908 else goto err;
3909 }
Tim Peters84e87f32001-03-17 04:50:51 +00003910
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003911 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003912
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003913 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003914 {
3915 PyObject *tp, *v, *tb, *tmp_value;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003916
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003917 PyErr_Fetch(&tp, &v, &tb);
3918 tmp_value = v;
3919 /* NULL occurs when there was a KeyboardInterrupt */
3920 if (tmp_value == NULL)
3921 tmp_value = Py_None;
3922 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3923 Py_XDECREF(v);
3924 v=r;
3925 }
3926 PyErr_Restore(tp,v,tb);
3927 }
3928 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003929}
Tim Peters84e87f32001-03-17 04:50:51 +00003930
Guido van Rossum60456fd1997-04-09 17:36:32 +00003931
3932static int
Tim Peterscba30e22003-02-01 06:24:36 +00003933load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003934{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003935 PyObject *class, *tup, *obj=0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003936 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003937
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003938 if ((i = marker(self)) < 0) return -1;
Serhiy Storchaka5c137662015-11-23 15:20:43 +02003939
3940 if (self->stack->length - i < 1)
3941 return stackUnderflow();
3942
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003943 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3944 PDATA_POP(self->stack, class);
3945 if (class) {
3946 obj = Instance_New(class, tup);
3947 Py_DECREF(class);
3948 }
3949 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003950
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003951 if (! obj) return -1;
3952 PDATA_PUSH(self->stack, obj, -1);
3953 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003954}
3955
3956
3957static int
Tim Peterscba30e22003-02-01 06:24:36 +00003958load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003959{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003960 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003961 Py_ssize_t i, len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003962 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003963
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003964 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003965
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003966 if ((len = self->readline_func(self, &s)) < 0) return -1;
3967 if (len < 2) return bad_readline();
3968 module_name = PyString_FromStringAndSize(s, len - 1);
3969 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003970
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003971 if ((len = self->readline_func(self, &s)) >= 0) {
Serhiy Storchaka048e1072015-12-01 00:32:49 +02003972 if (len < 2) {
3973 Py_DECREF(module_name);
3974 return bad_readline();
3975 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003976 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3977 class = find_class(module_name, class_name,
3978 self->find_class);
3979 Py_DECREF(class_name);
3980 }
3981 }
3982 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003983
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003984 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003985
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003986 if ((tup=Pdata_popTuple(self->stack, i))) {
3987 obj = Instance_New(class, tup);
3988 Py_DECREF(tup);
3989 }
3990 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003991
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003992 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003993
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003994 PDATA_PUSH(self->stack, obj, -1);
3995 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003996}
3997
Tim Peterseab7db32003-02-13 18:24:14 +00003998static int
3999load_newobj(Unpicklerobject *self)
4000{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004001 PyObject *args = NULL;
4002 PyObject *clsraw = NULL;
4003 PyTypeObject *cls; /* clsraw cast to its true type */
4004 PyObject *obj;
Tim Peterseab7db32003-02-13 18:24:14 +00004005
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004006 /* Stack is ... cls argtuple, and we want to call
4007 * cls.__new__(cls, *argtuple).
4008 */
4009 PDATA_POP(self->stack, args);
4010 if (args == NULL) goto Fail;
4011 if (! PyTuple_Check(args)) {
4012 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
4013 "tuple.");
4014 goto Fail;
4015 }
Tim Peterseab7db32003-02-13 18:24:14 +00004016
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004017 PDATA_POP(self->stack, clsraw);
4018 cls = (PyTypeObject *)clsraw;
4019 if (cls == NULL) goto Fail;
4020 if (! PyType_Check(cls)) {
4021 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
4022 "isn't a type object");
4023 goto Fail;
4024 }
4025 if (cls->tp_new == NULL) {
4026 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
4027 "has NULL tp_new");
4028 goto Fail;
4029 }
Tim Peterseab7db32003-02-13 18:24:14 +00004030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004031 /* Call __new__. */
4032 obj = cls->tp_new(cls, args, NULL);
4033 if (obj == NULL) goto Fail;
Tim Peterseab7db32003-02-13 18:24:14 +00004034
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004035 Py_DECREF(args);
4036 Py_DECREF(clsraw);
4037 PDATA_PUSH(self->stack, obj, -1);
4038 return 0;
Tim Peterseab7db32003-02-13 18:24:14 +00004039
4040 Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004041 Py_XDECREF(args);
4042 Py_XDECREF(clsraw);
4043 return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004044}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004045
4046static int
Tim Peterscba30e22003-02-01 06:24:36 +00004047load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004048{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004049 PyObject *class = 0, *module_name = 0, *class_name = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004050 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004051 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004052
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004053 if ((len = self->readline_func(self, &s)) < 0) return -1;
4054 if (len < 2) return bad_readline();
4055 module_name = PyString_FromStringAndSize(s, len - 1);
4056 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004057
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004058 if ((len = self->readline_func(self, &s)) >= 0) {
4059 if (len < 2) {
4060 Py_DECREF(module_name);
4061 return bad_readline();
4062 }
4063 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
4064 class = find_class(module_name, class_name,
4065 self->find_class);
4066 Py_DECREF(class_name);
4067 }
4068 }
4069 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004070
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004071 if (! class) return -1;
4072 PDATA_PUSH(self->stack, class, -1);
4073 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004074}
4075
4076
4077static int
Tim Peterscba30e22003-02-01 06:24:36 +00004078load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004079{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004080 PyObject *pid = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004081 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004082 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004083
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004084 if (self->pers_func) {
4085 if ((len = self->readline_func(self, &s)) < 0) return -1;
4086 if (len < 2) return bad_readline();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004087
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004088 pid = PyString_FromStringAndSize(s, len - 1);
4089 if (!pid) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004090
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004091 if (PyList_Check(self->pers_func)) {
4092 if (PyList_Append(self->pers_func, pid) < 0) {
4093 Py_DECREF(pid);
4094 return -1;
4095 }
4096 }
4097 else {
4098 ARG_TUP(self, pid);
4099 if (self->arg) {
4100 pid = PyObject_Call(self->pers_func, self->arg,
4101 NULL);
4102 FREE_ARG_TUP(self);
4103 }
4104 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004105
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004106 if (! pid) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004107
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004108 PDATA_PUSH(self->stack, pid, -1);
4109 return 0;
4110 }
4111 else {
4112 PyErr_SetString(UnpicklingError,
4113 "A load persistent id instruction was encountered,\n"
4114 "but no persistent_load function was specified.");
4115 return -1;
4116 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004117}
4118
4119static int
Tim Peterscba30e22003-02-01 06:24:36 +00004120load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004121{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004122 PyObject *pid = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004123
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004124 if (self->pers_func) {
4125 PDATA_POP(self->stack, pid);
4126 if (! pid) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004127
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004128 if (PyList_Check(self->pers_func)) {
4129 if (PyList_Append(self->pers_func, pid) < 0) {
4130 Py_DECREF(pid);
4131 return -1;
4132 }
4133 }
4134 else {
4135 ARG_TUP(self, pid);
4136 if (self->arg) {
4137 pid = PyObject_Call(self->pers_func, self->arg,
4138 NULL);
4139 FREE_ARG_TUP(self);
4140 }
4141 if (! pid) return -1;
4142 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004143
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004144 PDATA_PUSH(self->stack, pid, -1);
4145 return 0;
4146 }
4147 else {
4148 PyErr_SetString(UnpicklingError,
4149 "A load persistent id instruction was encountered,\n"
4150 "but no persistent_load function was specified.");
4151 return -1;
4152 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004153}
4154
4155
4156static int
Tim Peterscba30e22003-02-01 06:24:36 +00004157load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004158{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004159 Py_ssize_t len = self->stack->length;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004160
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004161 /* Note that we split the (pickle.py) stack into two stacks,
4162 an object stack and a mark stack. We have to be clever and
4163 pop the right one. We do this by looking at the top of the
4164 mark stack first, and only signalling a stack underflow if
4165 the object stack is empty and the mark stack doesn't match
4166 our expectations.
4167 */
4168 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
4169 self->num_marks--;
4170 } else if (len > 0) {
4171 len--;
4172 Py_DECREF(self->stack->data[len]);
4173 self->stack->length = len;
4174 } else {
4175 return stackUnderflow();
4176 }
4177 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004178}
4179
4180
4181static int
Tim Peterscba30e22003-02-01 06:24:36 +00004182load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004183{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004184 Py_ssize_t i;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004185
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004186 if ((i = marker(self)) < 0)
4187 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004188
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004189 Pdata_clear(self->stack, i);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004190
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004191 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004192}
4193
4194
4195static int
Tim Peterscba30e22003-02-01 06:24:36 +00004196load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004197{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004198 PyObject *last;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004199 Py_ssize_t len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004200
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004201 if ((len = self->stack->length) <= 0) return stackUnderflow();
4202 last=self->stack->data[len-1];
4203 Py_INCREF(last);
4204 PDATA_PUSH(self->stack, last, -1);
4205 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004206}
4207
4208
4209static int
Tim Peterscba30e22003-02-01 06:24:36 +00004210load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004211{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004212 PyObject *py_str = 0, *value = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004213 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004214 char *s;
4215 int rc;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004216
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004217 if ((len = self->readline_func(self, &s)) < 0) return -1;
4218 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00004219
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004220 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004221
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004222 value = PyDict_GetItem(self->memo, py_str);
4223 if (! value) {
4224 PyErr_SetObject(BadPickleGet, py_str);
4225 rc = -1;
4226 }
4227 else {
4228 PDATA_APPEND(self->stack, value, -1);
4229 rc = 0;
4230 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004231
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004232 Py_DECREF(py_str);
4233 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004234}
4235
4236
4237static int
Tim Peterscba30e22003-02-01 06:24:36 +00004238load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004239{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004240 PyObject *py_key = 0, *value = 0;
4241 unsigned char key;
4242 char *s;
4243 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004244
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004245 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004246
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004247 key = (unsigned char)s[0];
4248 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00004249
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004250 value = PyDict_GetItem(self->memo, py_key);
4251 if (! value) {
4252 PyErr_SetObject(BadPickleGet, py_key);
4253 rc = -1;
4254 }
4255 else {
4256 PDATA_APPEND(self->stack, value, -1);
4257 rc = 0;
4258 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004259
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004260 Py_DECREF(py_key);
4261 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004262}
4263
4264
4265static int
Tim Peterscba30e22003-02-01 06:24:36 +00004266load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004267{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004268 PyObject *py_key = 0, *value = 0;
4269 unsigned char c;
4270 char *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004271 Py_ssize_t key;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004272 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004273
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004274 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004275
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004276 c = (unsigned char)s[0];
4277 key = (long)c;
4278 c = (unsigned char)s[1];
4279 key |= (long)c << 8;
4280 c = (unsigned char)s[2];
4281 key |= (long)c << 16;
4282 c = (unsigned char)s[3];
4283 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004284
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004285 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004286
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004287 value = PyDict_GetItem(self->memo, py_key);
4288 if (! value) {
4289 PyErr_SetObject(BadPickleGet, py_key);
4290 rc = -1;
4291 }
4292 else {
4293 PDATA_APPEND(self->stack, value, -1);
4294 rc = 0;
4295 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004296
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004297 Py_DECREF(py_key);
4298 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004299}
4300
Tim Peters2d629652003-02-04 05:06:17 +00004301/* Push an object from the extension registry (EXT[124]). nbytes is
4302 * the number of bytes following the opcode, holding the index (code) value.
4303 */
4304static int
4305load_extension(Unpicklerobject *self, int nbytes)
4306{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004307 char *codebytes; /* the nbytes bytes after the opcode */
4308 long code; /* calc_binint returns long */
4309 PyObject *py_code; /* code as a Python int */
4310 PyObject *obj; /* the object to push */
4311 PyObject *pair; /* (module_name, class_name) */
4312 PyObject *module_name, *class_name;
Tim Peters2d629652003-02-04 05:06:17 +00004313
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004314 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4315 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4316 code = calc_binint(codebytes, nbytes);
4317 if (code <= 0) { /* note that 0 is forbidden */
4318 /* Corrupt or hostile pickle. */
4319 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4320 return -1;
4321 }
Tim Peters2d629652003-02-04 05:06:17 +00004322
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004323 /* Look for the code in the cache. */
4324 py_code = PyInt_FromLong(code);
4325 if (py_code == NULL) return -1;
4326 obj = PyDict_GetItem(extension_cache, py_code);
4327 if (obj != NULL) {
4328 /* Bingo. */
4329 Py_DECREF(py_code);
4330 PDATA_APPEND(self->stack, obj, -1);
4331 return 0;
4332 }
Tim Peters2d629652003-02-04 05:06:17 +00004333
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004334 /* Look up the (module_name, class_name) pair. */
4335 pair = PyDict_GetItem(inverted_registry, py_code);
4336 if (pair == NULL) {
4337 Py_DECREF(py_code);
4338 PyErr_Format(PyExc_ValueError, "unregistered extension "
4339 "code %ld", code);
4340 return -1;
4341 }
4342 /* Since the extension registry is manipulable via Python code,
4343 * confirm that pair is really a 2-tuple of strings.
4344 */
4345 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4346 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4347 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4348 Py_DECREF(py_code);
4349 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4350 "isn't a 2-tuple of strings", code);
4351 return -1;
4352 }
4353 /* Load the object. */
4354 obj = find_class(module_name, class_name, self->find_class);
4355 if (obj == NULL) {
4356 Py_DECREF(py_code);
4357 return -1;
4358 }
4359 /* Cache code -> obj. */
4360 code = PyDict_SetItem(extension_cache, py_code, obj);
4361 Py_DECREF(py_code);
4362 if (code < 0) {
4363 Py_DECREF(obj);
4364 return -1;
4365 }
4366 PDATA_PUSH(self->stack, obj, -1);
4367 return 0;
Tim Peters2d629652003-02-04 05:06:17 +00004368}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004369
4370static int
Tim Peterscba30e22003-02-01 06:24:36 +00004371load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004372{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004373 PyObject *py_str = 0, *value = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004374 Py_ssize_t len, l;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004375 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004376
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004377 if ((l = self->readline_func(self, &s)) < 0) return -1;
4378 if (l < 2) return bad_readline();
4379 if (!( len=self->stack->length )) return stackUnderflow();
4380 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4381 value=self->stack->data[len-1];
4382 l=PyDict_SetItem(self->memo, py_str, value);
4383 Py_DECREF(py_str);
4384 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004385}
4386
4387
4388static int
Tim Peterscba30e22003-02-01 06:24:36 +00004389load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004390{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004391 PyObject *py_key = 0, *value = 0;
4392 unsigned char key;
4393 char *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004394 Py_ssize_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004395
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004396 if (self->read_func(self, &s, 1) < 0) return -1;
4397 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004398
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004399 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004400
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004401 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4402 value=self->stack->data[len-1];
4403 len=PyDict_SetItem(self->memo, py_key, value);
4404 Py_DECREF(py_key);
4405 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004406}
4407
4408
4409static int
Tim Peterscba30e22003-02-01 06:24:36 +00004410load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004411{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004412 PyObject *py_key = 0, *value = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004413 Py_ssize_t key;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004414 unsigned char c;
4415 char *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004416 Py_ssize_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004417
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004418 if (self->read_func(self, &s, 4) < 0) return -1;
4419 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004420
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004421 c = (unsigned char)s[0];
4422 key = (long)c;
4423 c = (unsigned char)s[1];
4424 key |= (long)c << 8;
4425 c = (unsigned char)s[2];
4426 key |= (long)c << 16;
4427 c = (unsigned char)s[3];
4428 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004429
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004430 if (!( py_key = PyInt_FromLong(key))) return -1;
4431 value=self->stack->data[len-1];
4432 len=PyDict_SetItem(self->memo, py_key, value);
4433 Py_DECREF(py_key);
4434 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004435}
4436
4437
4438static int
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004439do_append(Unpicklerobject *self, Py_ssize_t x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004440{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004441 PyObject *value = 0, *list = 0, *append_method = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004442 Py_ssize_t len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004443
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004444 len=self->stack->length;
4445 if (!( len >= x && x > 0 )) return stackUnderflow();
4446 /* nothing to do */
4447 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004448
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004449 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004450
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004451 if (PyList_Check(list)) {
4452 PyObject *slice;
4453 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004454
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004455 slice=Pdata_popList(self->stack, x);
4456 if (! slice) return -1;
4457 list_len = PyList_GET_SIZE(list);
4458 i=PyList_SetSlice(list, list_len, list_len, slice);
4459 Py_DECREF(slice);
4460 return i;
4461 }
4462 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004463
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004464 if (!( append_method = PyObject_GetAttr(list, append_str)))
4465 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004466
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004467 for (i = x; i < len; i++) {
4468 PyObject *junk;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004469
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004470 value=self->stack->data[i];
4471 junk=0;
4472 ARG_TUP(self, value);
4473 if (self->arg) {
4474 junk = PyObject_Call(append_method, self->arg,
4475 NULL);
4476 FREE_ARG_TUP(self);
4477 }
4478 if (! junk) {
4479 Pdata_clear(self->stack, i+1);
4480 self->stack->length=x;
4481 Py_DECREF(append_method);
4482 return -1;
4483 }
4484 Py_DECREF(junk);
4485 }
4486 self->stack->length=x;
4487 Py_DECREF(append_method);
4488 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004489
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004490 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004491}
4492
4493
4494static int
Tim Peterscba30e22003-02-01 06:24:36 +00004495load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004496{
Serhiy Storchaka5c137662015-11-23 15:20:43 +02004497 if (self->stack->length - 1 <= 0)
4498 return stackUnderflow();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004499 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004500}
4501
4502
4503static int
Tim Peterscba30e22003-02-01 06:24:36 +00004504load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004505{
Serhiy Storchaka5c137662015-11-23 15:20:43 +02004506 Py_ssize_t i = marker(self);
4507 if (i < 0)
4508 return -1;
4509 return do_append(self, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004510}
4511
4512
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004513static Py_ssize_t
4514do_setitems(Unpicklerobject *self, Py_ssize_t x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004515{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004516 PyObject *value = 0, *key = 0, *dict = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004517 Py_ssize_t len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004518
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004519 if (!( (len=self->stack->length) >= x
4520 && x > 0 )) return stackUnderflow();
Serhiy Storchaka5c137662015-11-23 15:20:43 +02004521 if (len == x) /* nothing to do */
4522 return 0;
4523 if ((len - x) % 2 != 0) {
4524 /* Currupt or hostile pickle -- we never write one like this. */
4525 PyErr_SetString(UnpicklingError,
4526 "odd number of items for SETITEMS");
4527 return -1;
4528 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004529
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004530 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004531
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004532 for (i = x+1; i < len; i += 2) {
4533 key =self->stack->data[i-1];
4534 value=self->stack->data[i ];
4535 if (PyObject_SetItem(dict, key, value) < 0) {
4536 r=-1;
4537 break;
4538 }
4539 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004540
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004541 Pdata_clear(self->stack, x);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004542
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004543 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004544}
4545
4546
Tim Peters84e87f32001-03-17 04:50:51 +00004547static int
Tim Peterscba30e22003-02-01 06:24:36 +00004548load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004549{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004550 return do_setitems(self, self->stack->length - 2);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004551}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004552
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004553static int
Tim Peterscba30e22003-02-01 06:24:36 +00004554load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004555{
Serhiy Storchaka5c137662015-11-23 15:20:43 +02004556 Py_ssize_t i = marker(self);
4557 if (i < 0)
4558 return -1;
4559 return do_setitems(self, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004560}
4561
Tim Peters84e87f32001-03-17 04:50:51 +00004562
Guido van Rossum60456fd1997-04-09 17:36:32 +00004563static int
Tim Peterscba30e22003-02-01 06:24:36 +00004564load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004565{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004566 PyObject *state, *inst, *slotstate;
4567 PyObject *__setstate__;
4568 PyObject *d_key, *d_value;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004569 int res = -1;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004570 Py_ssize_t i;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004571
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004572 /* Stack is ... instance, state. We want to leave instance at
4573 * the stack top, possibly mutated via instance.__setstate__(state).
4574 */
4575 if (self->stack->length < 2)
4576 return stackUnderflow();
4577 PDATA_POP(self->stack, state);
4578 if (state == NULL)
4579 return -1;
4580 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004581
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004582 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4583 if (__setstate__ != NULL) {
4584 PyObject *junk = NULL;
Tim Peters080c88b2003-02-15 03:01:11 +00004585
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004586 /* The explicit __setstate__ is responsible for everything. */
4587 ARG_TUP(self, state);
4588 if (self->arg) {
4589 junk = PyObject_Call(__setstate__, self->arg, NULL);
4590 FREE_ARG_TUP(self);
4591 }
4592 Py_DECREF(__setstate__);
4593 if (junk == NULL)
4594 return -1;
4595 Py_DECREF(junk);
4596 return 0;
4597 }
4598 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4599 return -1;
4600 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004601
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004602 /* A default __setstate__. First see whether state embeds a
4603 * slot state dict too (a proto 2 addition).
4604 */
4605 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4606 PyObject *temp = state;
4607 state = PyTuple_GET_ITEM(temp, 0);
4608 slotstate = PyTuple_GET_ITEM(temp, 1);
4609 Py_INCREF(state);
4610 Py_INCREF(slotstate);
4611 Py_DECREF(temp);
4612 }
4613 else
4614 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004615
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004616 /* Set inst.__dict__ from the state dict (if any). */
4617 if (state != Py_None) {
4618 PyObject *dict;
4619 if (! PyDict_Check(state)) {
4620 PyErr_SetString(UnpicklingError, "state is not a "
4621 "dictionary");
4622 goto finally;
4623 }
4624 dict = PyObject_GetAttr(inst, __dict___str);
4625 if (dict == NULL)
4626 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004627
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004628 i = 0;
4629 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4630 /* normally the keys for instance attributes are
4631 interned. we should try to do that here. */
4632 Py_INCREF(d_key);
4633 if (PyString_CheckExact(d_key))
4634 PyString_InternInPlace(&d_key);
4635 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
4636 Py_DECREF(d_key);
4637 goto finally;
4638 }
4639 Py_DECREF(d_key);
4640 }
4641 Py_DECREF(dict);
4642 }
Tim Peters080c88b2003-02-15 03:01:11 +00004643
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004644 /* Also set instance attributes from the slotstate dict (if any). */
4645 if (slotstate != NULL) {
4646 if (! PyDict_Check(slotstate)) {
4647 PyErr_SetString(UnpicklingError, "slot state is not "
4648 "a dictionary");
4649 goto finally;
4650 }
4651 i = 0;
4652 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4653 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4654 goto finally;
4655 }
4656 }
4657 res = 0;
Tim Peters080c88b2003-02-15 03:01:11 +00004658
4659 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004660 Py_DECREF(state);
4661 Py_XDECREF(slotstate);
4662 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004663}
4664
4665
4666static int
Tim Peterscba30e22003-02-01 06:24:36 +00004667load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004668{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004669 Py_ssize_t s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004670
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004671 /* Note that we split the (pickle.py) stack into two stacks, an
4672 object stack and a mark stack. Here we push a mark onto the
4673 mark stack.
4674 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004675
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004676 if ((self->num_marks + 1) >= self->marks_size) {
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004677 Py_ssize_t *marks;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004678 s=self->marks_size+20;
4679 if (s <= self->num_marks) s=self->num_marks + 1;
4680 if (self->marks == NULL)
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004681 marks=(Py_ssize_t *)malloc(s * sizeof(Py_ssize_t));
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004682 else
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004683 marks=(Py_ssize_t *)realloc(self->marks,
4684 s * sizeof(Py_ssize_t));
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004685 if (!marks) {
4686 PyErr_NoMemory();
4687 return -1;
4688 }
4689 self->marks = marks;
4690 self->marks_size = s;
4691 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004692
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004693 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004694
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004695 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004696}
4697
Guido van Rossum60456fd1997-04-09 17:36:32 +00004698static int
Tim Peterscba30e22003-02-01 06:24:36 +00004699load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004700{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004701 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004702
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004703 PDATA_POP(self->stack, arg_tup);
4704 if (! arg_tup) return -1;
4705 PDATA_POP(self->stack, callable);
4706 if (callable) {
4707 ob = Instance_New(callable, arg_tup);
4708 Py_DECREF(callable);
4709 }
4710 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004711
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004712 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004713
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004714 PDATA_PUSH(self->stack, ob, -1);
4715 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004716}
Tim Peters84e87f32001-03-17 04:50:51 +00004717
Tim Peters4190fb82003-02-02 16:09:05 +00004718/* Just raises an error if we don't know the protocol specified. PROTO
4719 * is the first opcode for protocols >= 2.
4720 */
4721static int
4722load_proto(Unpicklerobject *self)
4723{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004724 int i;
4725 char *protobyte;
Tim Peters4190fb82003-02-02 16:09:05 +00004726
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004727 i = self->read_func(self, &protobyte, 1);
4728 if (i < 0)
4729 return -1;
Tim Peters4190fb82003-02-02 16:09:05 +00004730
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004731 i = calc_binint(protobyte, 1);
4732 /* No point checking for < 0, since calc_binint returns an unsigned
4733 * int when chewing on 1 byte.
4734 */
4735 assert(i >= 0);
4736 if (i <= HIGHEST_PROTOCOL)
4737 return 0;
Tim Peters4190fb82003-02-02 16:09:05 +00004738
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004739 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4740 return -1;
Tim Peters4190fb82003-02-02 16:09:05 +00004741}
4742
Guido van Rossum60456fd1997-04-09 17:36:32 +00004743static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004744load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004745{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004746 PyObject *err = 0, *val = 0;
4747 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004748
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004749 self->num_marks = 0;
4750 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004751
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004752 while (1) {
4753 if (self->read_func(self, &s, 1) < 0)
4754 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004755
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004756 switch (s[0]) {
4757 case NONE:
4758 if (load_none(self) < 0)
4759 break;
4760 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004761
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004762 case BININT:
4763 if (load_binint(self) < 0)
4764 break;
4765 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004766
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004767 case BININT1:
4768 if (load_binint1(self) < 0)
4769 break;
4770 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004771
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004772 case BININT2:
4773 if (load_binint2(self) < 0)
4774 break;
4775 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004776
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004777 case INT:
4778 if (load_int(self) < 0)
4779 break;
4780 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004781
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004782 case LONG:
4783 if (load_long(self) < 0)
4784 break;
4785 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004786
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004787 case LONG1:
4788 if (load_counted_long(self, 1) < 0)
4789 break;
4790 continue;
Tim Petersee1a53c2003-02-02 02:57:53 +00004791
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004792 case LONG4:
4793 if (load_counted_long(self, 4) < 0)
4794 break;
4795 continue;
Tim Petersee1a53c2003-02-02 02:57:53 +00004796
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004797 case FLOAT:
4798 if (load_float(self) < 0)
4799 break;
4800 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004801
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004802 case BINFLOAT:
4803 if (load_binfloat(self) < 0)
4804 break;
4805 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004806
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004807 case BINSTRING:
4808 if (load_binstring(self) < 0)
4809 break;
4810 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004811
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004812 case SHORT_BINSTRING:
4813 if (load_short_binstring(self) < 0)
4814 break;
4815 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004816
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004817 case STRING:
4818 if (load_string(self) < 0)
4819 break;
4820 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004821
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004822#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004823 case UNICODE:
4824 if (load_unicode(self) < 0)
4825 break;
4826 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004827
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004828 case BINUNICODE:
4829 if (load_binunicode(self) < 0)
4830 break;
4831 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004832#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004833
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004834 case EMPTY_TUPLE:
4835 if (load_counted_tuple(self, 0) < 0)
4836 break;
4837 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00004838
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004839 case TUPLE1:
4840 if (load_counted_tuple(self, 1) < 0)
4841 break;
4842 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00004843
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004844 case TUPLE2:
4845 if (load_counted_tuple(self, 2) < 0)
4846 break;
4847 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00004848
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004849 case TUPLE3:
4850 if (load_counted_tuple(self, 3) < 0)
4851 break;
4852 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004853
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004854 case TUPLE:
4855 if (load_tuple(self) < 0)
4856 break;
4857 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004858
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004859 case EMPTY_LIST:
4860 if (load_empty_list(self) < 0)
4861 break;
4862 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004863
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004864 case LIST:
4865 if (load_list(self) < 0)
4866 break;
4867 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004868
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004869 case EMPTY_DICT:
4870 if (load_empty_dict(self) < 0)
4871 break;
4872 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004873
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004874 case DICT:
4875 if (load_dict(self) < 0)
4876 break;
4877 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004878
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004879 case OBJ:
4880 if (load_obj(self) < 0)
4881 break;
4882 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004883
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004884 case INST:
4885 if (load_inst(self) < 0)
4886 break;
4887 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004888
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004889 case NEWOBJ:
4890 if (load_newobj(self) < 0)
4891 break;
4892 continue;
Tim Peterseab7db32003-02-13 18:24:14 +00004893
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004894 case GLOBAL:
4895 if (load_global(self) < 0)
4896 break;
4897 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004898
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004899 case APPEND:
4900 if (load_append(self) < 0)
4901 break;
4902 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004903
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004904 case APPENDS:
4905 if (load_appends(self) < 0)
4906 break;
4907 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004908
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004909 case BUILD:
4910 if (load_build(self) < 0)
4911 break;
4912 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004913
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004914 case DUP:
4915 if (load_dup(self) < 0)
4916 break;
4917 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004918
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004919 case BINGET:
4920 if (load_binget(self) < 0)
4921 break;
4922 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004923
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004924 case LONG_BINGET:
4925 if (load_long_binget(self) < 0)
4926 break;
4927 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004928
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004929 case GET:
4930 if (load_get(self) < 0)
4931 break;
4932 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004933
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004934 case EXT1:
4935 if (load_extension(self, 1) < 0)
4936 break;
4937 continue;
Tim Peters2d629652003-02-04 05:06:17 +00004938
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004939 case EXT2:
4940 if (load_extension(self, 2) < 0)
4941 break;
4942 continue;
Tim Peters2d629652003-02-04 05:06:17 +00004943
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004944 case EXT4:
4945 if (load_extension(self, 4) < 0)
4946 break;
4947 continue;
4948 case MARK:
4949 if (load_mark(self) < 0)
4950 break;
4951 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004952
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004953 case BINPUT:
4954 if (load_binput(self) < 0)
4955 break;
4956 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004957
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004958 case LONG_BINPUT:
4959 if (load_long_binput(self) < 0)
4960 break;
4961 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004962
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004963 case PUT:
4964 if (load_put(self) < 0)
4965 break;
4966 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004967
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004968 case POP:
4969 if (load_pop(self) < 0)
4970 break;
4971 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004972
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004973 case POP_MARK:
4974 if (load_pop_mark(self) < 0)
4975 break;
4976 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004977
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004978 case SETITEM:
4979 if (load_setitem(self) < 0)
4980 break;
4981 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004982
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004983 case SETITEMS:
4984 if (load_setitems(self) < 0)
4985 break;
4986 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004987
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004988 case STOP:
4989 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004990
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004991 case PERSID:
4992 if (load_persid(self) < 0)
4993 break;
4994 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004995
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004996 case BINPERSID:
4997 if (load_binpersid(self) < 0)
4998 break;
4999 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005000
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005001 case REDUCE:
5002 if (load_reduce(self) < 0)
5003 break;
5004 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005005
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005006 case PROTO:
5007 if (load_proto(self) < 0)
5008 break;
5009 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005010
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005011 case NEWTRUE:
5012 if (load_bool(self, Py_True) < 0)
5013 break;
5014 continue;
Tim Peters3c67d792003-02-02 17:59:11 +00005015
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005016 case NEWFALSE:
5017 if (load_bool(self, Py_False) < 0)
5018 break;
5019 continue;
Tim Peters3c67d792003-02-02 17:59:11 +00005020
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005021 case '\0':
5022 /* end of file */
5023 PyErr_SetNone(PyExc_EOFError);
5024 break;
Tim Peterscba30e22003-02-01 06:24:36 +00005025
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005026 default:
5027 cPickle_ErrFormat(UnpicklingError,
5028 "invalid load key, '%s'.",
5029 "c", s[0]);
5030 return NULL;
5031 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005032
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005033 break;
5034 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005035
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005036 if ((err = PyErr_Occurred())) {
5037 if (err == PyExc_EOFError) {
5038 PyErr_SetNone(PyExc_EOFError);
5039 }
5040 return NULL;
5041 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005042
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005043 PDATA_POP(self->stack, val);
5044 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005045}
Tim Peters84e87f32001-03-17 04:50:51 +00005046
Guido van Rossum60456fd1997-04-09 17:36:32 +00005047
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005048/* No-load functions to support noload, which is used to
5049 find persistent references. */
5050
5051static int
Tim Peterscba30e22003-02-01 06:24:36 +00005052noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005053{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005054 Py_ssize_t i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005055
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005056 if ((i = marker(self)) < 0) return -1;
5057 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005058}
5059
5060
5061static int
Tim Peterscba30e22003-02-01 06:24:36 +00005062noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005063{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005064 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005065 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005066
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005067 if ((i = marker(self)) < 0) return -1;
5068 Pdata_clear(self->stack, i);
5069 if (self->readline_func(self, &s) < 0) return -1;
5070 if (self->readline_func(self, &s) < 0) return -1;
5071 PDATA_APPEND(self->stack, Py_None, -1);
5072 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005073}
5074
5075static int
Tim Peterseab7db32003-02-13 18:24:14 +00005076noload_newobj(Unpicklerobject *self)
5077{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005078 PyObject *obj;
Tim Peterseab7db32003-02-13 18:24:14 +00005079
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005080 PDATA_POP(self->stack, obj); /* pop argtuple */
5081 if (obj == NULL) return -1;
5082 Py_DECREF(obj);
Tim Peterseab7db32003-02-13 18:24:14 +00005083
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005084 PDATA_POP(self->stack, obj); /* pop cls */
5085 if (obj == NULL) return -1;
5086 Py_DECREF(obj);
Tim Peterseab7db32003-02-13 18:24:14 +00005087
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005088 PDATA_APPEND(self->stack, Py_None, -1);
5089 return 0;
Tim Peterseab7db32003-02-13 18:24:14 +00005090}
5091
5092static int
Tim Peterscba30e22003-02-01 06:24:36 +00005093noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005094{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005095 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005096
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005097 if (self->readline_func(self, &s) < 0) return -1;
5098 if (self->readline_func(self, &s) < 0) return -1;
5099 PDATA_APPEND(self->stack, Py_None,-1);
5100 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005101}
5102
5103static int
Tim Peterscba30e22003-02-01 06:24:36 +00005104noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005105{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005106
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005107 if (self->stack->length < 2) return stackUnderflow();
5108 Pdata_clear(self->stack, self->stack->length-2);
5109 PDATA_APPEND(self->stack, Py_None,-1);
5110 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005111}
5112
5113static int
5114noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005115
Guido van Rossum053b8df1998-11-25 16:18:00 +00005116 if (self->stack->length < 1) return stackUnderflow();
5117 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00005118 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005119}
5120
Tim Peters2d629652003-02-04 05:06:17 +00005121static int
5122noload_extension(Unpicklerobject *self, int nbytes)
5123{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005124 char *codebytes;
Tim Peters2d629652003-02-04 05:06:17 +00005125
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005126 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
5127 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
5128 PDATA_APPEND(self->stack, Py_None, -1);
5129 return 0;
Tim Peters2d629652003-02-04 05:06:17 +00005130}
5131
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005132static int
5133noload_append(Unpicklerobject *self)
5134{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005135 return Pdata_clear(self->stack, self->stack->length - 1);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005136}
5137
5138static int
5139noload_appends(Unpicklerobject *self)
5140{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005141 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005142 if ((i = marker(self)) < 0) return -1;
5143 return Pdata_clear(self->stack, i);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005144}
5145
5146static int
5147noload_setitem(Unpicklerobject *self)
5148{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005149 return Pdata_clear(self->stack, self->stack->length - 2);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005150}
5151
5152static int
5153noload_setitems(Unpicklerobject *self)
5154{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005155 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005156 if ((i = marker(self)) < 0) return -1;
5157 return Pdata_clear(self->stack, i);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005158}
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005159
5160static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005161noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005162{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005163 PyObject *err = 0, *val = 0;
5164 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005165
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005166 self->num_marks = 0;
5167 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005168
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005169 while (1) {
5170 if (self->read_func(self, &s, 1) < 0)
5171 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005172
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005173 switch (s[0]) {
5174 case NONE:
5175 if (load_none(self) < 0)
5176 break;
5177 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005178
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005179 case BININT:
5180 if (load_binint(self) < 0)
5181 break;
5182 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005183
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005184 case BININT1:
5185 if (load_binint1(self) < 0)
5186 break;
5187 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005188
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005189 case BININT2:
5190 if (load_binint2(self) < 0)
5191 break;
5192 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005193
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005194 case INT:
5195 if (load_int(self) < 0)
5196 break;
5197 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005198
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005199 case LONG:
5200 if (load_long(self) < 0)
5201 break;
5202 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005203
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005204 case LONG1:
5205 if (load_counted_long(self, 1) < 0)
5206 break;
5207 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005208
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005209 case LONG4:
5210 if (load_counted_long(self, 4) < 0)
5211 break;
5212 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005213
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005214 case FLOAT:
5215 if (load_float(self) < 0)
5216 break;
5217 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005218
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005219 case BINFLOAT:
5220 if (load_binfloat(self) < 0)
5221 break;
5222 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005223
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005224 case BINSTRING:
5225 if (load_binstring(self) < 0)
5226 break;
5227 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005228
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005229 case SHORT_BINSTRING:
5230 if (load_short_binstring(self) < 0)
5231 break;
5232 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005233
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005234 case STRING:
5235 if (load_string(self) < 0)
5236 break;
5237 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005238
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005239#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005240 case UNICODE:
5241 if (load_unicode(self) < 0)
5242 break;
5243 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005244
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005245 case BINUNICODE:
5246 if (load_binunicode(self) < 0)
5247 break;
5248 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005249#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005250
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005251 case EMPTY_TUPLE:
5252 if (load_counted_tuple(self, 0) < 0)
5253 break;
5254 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00005255
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005256 case TUPLE1:
5257 if (load_counted_tuple(self, 1) < 0)
5258 break;
5259 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00005260
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005261 case TUPLE2:
5262 if (load_counted_tuple(self, 2) < 0)
5263 break;
5264 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00005265
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005266 case TUPLE3:
5267 if (load_counted_tuple(self, 3) < 0)
5268 break;
5269 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005270
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005271 case TUPLE:
5272 if (load_tuple(self) < 0)
5273 break;
5274 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005275
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005276 case EMPTY_LIST:
5277 if (load_empty_list(self) < 0)
5278 break;
5279 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005280
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005281 case LIST:
5282 if (load_list(self) < 0)
5283 break;
5284 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005285
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005286 case EMPTY_DICT:
5287 if (load_empty_dict(self) < 0)
5288 break;
5289 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005290
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005291 case DICT:
5292 if (load_dict(self) < 0)
5293 break;
5294 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005295
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005296 case OBJ:
5297 if (noload_obj(self) < 0)
5298 break;
5299 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005300
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005301 case INST:
5302 if (noload_inst(self) < 0)
5303 break;
5304 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005305
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005306 case NEWOBJ:
5307 if (noload_newobj(self) < 0)
5308 break;
5309 continue;
Tim Peterseab7db32003-02-13 18:24:14 +00005310
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005311 case GLOBAL:
5312 if (noload_global(self) < 0)
5313 break;
5314 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005315
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005316 case APPEND:
5317 if (noload_append(self) < 0)
5318 break;
5319 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005320
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005321 case APPENDS:
5322 if (noload_appends(self) < 0)
5323 break;
5324 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005325
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005326 case BUILD:
5327 if (noload_build(self) < 0)
5328 break;
5329 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005330
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005331 case DUP:
5332 if (load_dup(self) < 0)
5333 break;
5334 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005335
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005336 case BINGET:
5337 if (load_binget(self) < 0)
5338 break;
5339 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005340
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005341 case LONG_BINGET:
5342 if (load_long_binget(self) < 0)
5343 break;
5344 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005345
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005346 case GET:
5347 if (load_get(self) < 0)
5348 break;
5349 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005350
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005351 case EXT1:
5352 if (noload_extension(self, 1) < 0)
5353 break;
5354 continue;
Tim Peters2d629652003-02-04 05:06:17 +00005355
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005356 case EXT2:
5357 if (noload_extension(self, 2) < 0)
5358 break;
5359 continue;
Tim Peters2d629652003-02-04 05:06:17 +00005360
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005361 case EXT4:
5362 if (noload_extension(self, 4) < 0)
5363 break;
5364 continue;
Tim Peters2d629652003-02-04 05:06:17 +00005365
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005366 case MARK:
5367 if (load_mark(self) < 0)
5368 break;
5369 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005370
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005371 case BINPUT:
5372 if (load_binput(self) < 0)
5373 break;
5374 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005375
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005376 case LONG_BINPUT:
5377 if (load_long_binput(self) < 0)
5378 break;
5379 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005380
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005381 case PUT:
5382 if (load_put(self) < 0)
5383 break;
5384 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005385
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005386 case POP:
5387 if (load_pop(self) < 0)
5388 break;
5389 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005390
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005391 case POP_MARK:
5392 if (load_pop_mark(self) < 0)
5393 break;
5394 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005395
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005396 case SETITEM:
5397 if (noload_setitem(self) < 0)
5398 break;
5399 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005400
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005401 case SETITEMS:
5402 if (noload_setitems(self) < 0)
5403 break;
5404 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005405
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005406 case STOP:
5407 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005408
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005409 case PERSID:
5410 if (load_persid(self) < 0)
5411 break;
5412 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005413
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005414 case BINPERSID:
5415 if (load_binpersid(self) < 0)
5416 break;
5417 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005418
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005419 case REDUCE:
5420 if (noload_reduce(self) < 0)
5421 break;
5422 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005423
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005424 case PROTO:
5425 if (load_proto(self) < 0)
5426 break;
5427 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005428
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005429 case NEWTRUE:
5430 if (load_bool(self, Py_True) < 0)
5431 break;
5432 continue;
Tim Peters3c67d792003-02-02 17:59:11 +00005433
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005434 case NEWFALSE:
5435 if (load_bool(self, Py_False) < 0)
5436 break;
5437 continue;
5438 default:
5439 cPickle_ErrFormat(UnpicklingError,
5440 "invalid load key, '%s'.",
5441 "c", s[0]);
5442 return NULL;
5443 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005444
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005445 break;
5446 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005447
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005448 if ((err = PyErr_Occurred())) {
5449 if (err == PyExc_EOFError) {
5450 PyErr_SetNone(PyExc_EOFError);
5451 }
5452 return NULL;
5453 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005454
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005455 PDATA_POP(self->stack, val);
5456 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005457}
Tim Peters84e87f32001-03-17 04:50:51 +00005458
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005459
Guido van Rossum60456fd1997-04-09 17:36:32 +00005460static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005461Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005462{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005463 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005464}
5465
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005466static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005467Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005468{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005469 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005470}
5471
Guido van Rossum60456fd1997-04-09 17:36:32 +00005472
5473static struct PyMethodDef Unpickler_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +00005474 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005475 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005476 },
Georg Brandl96a8c392006-05-29 21:04:52 +00005477 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005478 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005479 "noload() -- not load a pickle, but go through most of the motions\n"
5480 "\n"
5481 "This function can be used to read past a pickle without instantiating\n"
5482 "any objects or importing any modules. It can also be used to find all\n"
5483 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005484 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005485 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005486 {NULL, NULL} /* sentinel */
5487};
5488
5489
5490static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005491newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005492{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005493 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005494
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005495 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5496 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005497
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005498 self->file = NULL;
5499 self->arg = NULL;
5500 self->stack = (Pdata*)Pdata_New();
5501 self->pers_func = NULL;
5502 self->last_string = NULL;
5503 self->marks = NULL;
5504 self->num_marks = 0;
5505 self->marks_size = 0;
5506 self->buf_size = 0;
5507 self->read = NULL;
5508 self->readline = NULL;
5509 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005510
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005511 if (!( self->memo = PyDict_New()))
5512 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005513
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005514 if (!self->stack)
5515 goto err;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00005516
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005517 Py_INCREF(f);
5518 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005519
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005520 /* Set read, readline based on type of f */
5521 if (PyFile_Check(f)) {
5522 self->fp = PyFile_AsFile(f);
5523 if (self->fp == NULL) {
5524 PyErr_SetString(PyExc_ValueError,
5525 "I/O operation on closed file");
5526 goto err;
5527 }
5528 self->read_func = read_file;
5529 self->readline_func = readline_file;
5530 }
5531 else if (PycStringIO_InputCheck(f)) {
5532 self->fp = NULL;
5533 self->read_func = read_cStringIO;
5534 self->readline_func = readline_cStringIO;
5535 }
5536 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005537
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005538 self->fp = NULL;
5539 self->read_func = read_other;
5540 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005541
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005542 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5543 (self->read = PyObject_GetAttr(f, read_str)))) {
5544 PyErr_Clear();
5545 PyErr_SetString( PyExc_TypeError,
5546 "argument must have 'read' and "
5547 "'readline' attributes" );
5548 goto err;
5549 }
5550 }
5551 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005552
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005553 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005554
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005555 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005556 Py_DECREF((PyObject *)self);
5557 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005558}
5559
5560
5561static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005562get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005563{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005564 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005565}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005566
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005567
Guido van Rossum60456fd1997-04-09 17:36:32 +00005568static void
Tim Peterscba30e22003-02-01 06:24:36 +00005569Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005570{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005571 PyObject_GC_UnTrack((PyObject *)self);
5572 Py_XDECREF(self->readline);
5573 Py_XDECREF(self->read);
5574 Py_XDECREF(self->file);
5575 Py_XDECREF(self->memo);
5576 Py_XDECREF(self->stack);
5577 Py_XDECREF(self->pers_func);
5578 Py_XDECREF(self->arg);
5579 Py_XDECREF(self->last_string);
5580 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005581
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005582 if (self->marks) {
5583 free(self->marks);
5584 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005585
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005586 if (self->buf_size) {
5587 free(self->buf);
5588 }
Tim Peters84e87f32001-03-17 04:50:51 +00005589
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005590 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005591}
5592
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005593static int
5594Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5595{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005596 Py_VISIT(self->readline);
5597 Py_VISIT(self->read);
5598 Py_VISIT(self->file);
5599 Py_VISIT(self->memo);
5600 Py_VISIT(self->stack);
5601 Py_VISIT(self->pers_func);
5602 Py_VISIT(self->arg);
5603 Py_VISIT(self->last_string);
5604 Py_VISIT(self->find_class);
5605 return 0;
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005606}
5607
5608static int
5609Unpickler_clear(Unpicklerobject *self)
5610{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005611 Py_CLEAR(self->readline);
5612 Py_CLEAR(self->read);
5613 Py_CLEAR(self->file);
5614 Py_CLEAR(self->memo);
5615 Py_CLEAR(self->stack);
5616 Py_CLEAR(self->pers_func);
5617 Py_CLEAR(self->arg);
5618 Py_CLEAR(self->last_string);
5619 Py_CLEAR(self->find_class);
5620 return 0;
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005621}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005622
5623static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005624Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005625{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005626 if (!strcmp(name, "persistent_load")) {
5627 if (!self->pers_func) {
5628 PyErr_SetString(PyExc_AttributeError, name);
5629 return NULL;
5630 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005631
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005632 Py_INCREF(self->pers_func);
5633 return self->pers_func;
5634 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005635
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005636 if (!strcmp(name, "find_global")) {
5637 if (!self->find_class) {
5638 PyErr_SetString(PyExc_AttributeError, name);
5639 return NULL;
5640 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005641
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005642 Py_INCREF(self->find_class);
5643 return self->find_class;
5644 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005645
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005646 if (!strcmp(name, "memo")) {
5647 if (!self->memo) {
5648 PyErr_SetString(PyExc_AttributeError, name);
5649 return NULL;
5650 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005651
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005652 Py_INCREF(self->memo);
5653 return self->memo;
5654 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005655
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005656 if (!strcmp(name, "UnpicklingError")) {
5657 Py_INCREF(UnpicklingError);
5658 return UnpicklingError;
5659 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005660
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005661 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005662}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005663
Guido van Rossum60456fd1997-04-09 17:36:32 +00005664
5665static int
Tim Peterscba30e22003-02-01 06:24:36 +00005666Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005667{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005668
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005669 if (!strcmp(name, "persistent_load")) {
5670 Py_XDECREF(self->pers_func);
5671 self->pers_func = value;
5672 Py_XINCREF(value);
5673 return 0;
5674 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005675
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005676 if (!strcmp(name, "find_global")) {
5677 Py_XDECREF(self->find_class);
5678 self->find_class = value;
5679 Py_XINCREF(value);
5680 return 0;
5681 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005682
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005683 if (! value) {
5684 PyErr_SetString(PyExc_TypeError,
5685 "attribute deletion is not supported");
5686 return -1;
5687 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005688
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005689 if (strcmp(name, "memo") == 0) {
5690 if (!PyDict_Check(value)) {
5691 PyErr_SetString(PyExc_TypeError,
5692 "memo must be a dictionary");
5693 return -1;
5694 }
5695 Py_XDECREF(self->memo);
5696 self->memo = value;
5697 Py_INCREF(value);
5698 return 0;
5699 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005700
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005701 PyErr_SetString(PyExc_AttributeError, name);
5702 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005703}
5704
Tim Peters5bd2a792003-02-01 16:45:06 +00005705/* ---------------------------------------------------------------------------
5706 * Module-level functions.
5707 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005708
Martin v. Löwis544f1192004-07-27 05:22:33 +00005709/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005710static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005711cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005712{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005713 static char *kwlist[] = {"obj", "file", "protocol", NULL};
5714 PyObject *ob, *file, *res = NULL;
5715 Picklerobject *pickler = 0;
5716 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005717
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005718 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5719 &ob, &file, &proto)))
5720 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005721
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005722 if (!( pickler = newPicklerobject(file, proto)))
5723 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005724
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005725 if (dump(pickler, ob) < 0)
5726 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005727
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005728 Py_INCREF(Py_None);
5729 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005730
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005731 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005732 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005733
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005734 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005735}
5736
5737
Martin v. Löwis544f1192004-07-27 05:22:33 +00005738/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005739static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005740cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005741{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005742 static char *kwlist[] = {"obj", "protocol", NULL};
5743 PyObject *ob, *file = 0, *res = NULL;
5744 Picklerobject *pickler = 0;
5745 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005746
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005747 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5748 &ob, &proto)))
5749 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005750
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005751 if (!( file = PycStringIO->NewOutput(128)))
5752 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005753
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005754 if (!( pickler = newPicklerobject(file, proto)))
5755 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005756
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005757 if (dump(pickler, ob) < 0)
5758 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005759
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005760 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005761
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005762 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005763 Py_XDECREF(pickler);
5764 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005765
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005766 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005767}
5768
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005769
Tim Peters5bd2a792003-02-01 16:45:06 +00005770/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005771static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005772cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005773{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005774 Unpicklerobject *unpickler = 0;
5775 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005776
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005777 if (!( unpickler = newUnpicklerobject(ob)))
5778 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005779
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005780 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005781
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005782 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005783 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005784
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005785 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005786}
5787
5788
Tim Peters5bd2a792003-02-01 16:45:06 +00005789/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005790static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005791cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005792{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005793 PyObject *ob, *file = 0, *res = NULL;
5794 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005795
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005796 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5797 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005798
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005799 if (!( file = PycStringIO->NewInput(ob)))
5800 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005801
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005802 if (!( unpickler = newUnpicklerobject(file)))
5803 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005804
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005805 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005806
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005807 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005808 Py_XDECREF(file);
5809 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005810
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005811 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005812}
5813
5814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005815PyDoc_STRVAR(Unpicklertype__doc__,
5816"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005817
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005818static PyTypeObject Unpicklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00005819 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005820 "cPickle.Unpickler", /*tp_name*/
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005821 sizeof(Unpicklerobject), /*tp_basicsize*/
5822 0,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005823 (destructor)Unpickler_dealloc, /* tp_dealloc */
5824 0, /* tp_print */
5825 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5826 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5827 0, /* tp_compare */
5828 0, /* tp_repr */
5829 0, /* tp_as_number */
5830 0, /* tp_as_sequence */
5831 0, /* tp_as_mapping */
5832 0, /* tp_hash */
5833 0, /* tp_call */
5834 0, /* tp_str */
5835 0, /* tp_getattro */
5836 0, /* tp_setattro */
5837 0, /* tp_as_buffer */
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005838 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005839 Unpicklertype__doc__, /* tp_doc */
5840 (traverseproc)Unpickler_traverse, /* tp_traverse */
5841 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005842};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005843
Guido van Rossum60456fd1997-04-09 17:36:32 +00005844static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005845 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5846 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005847 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005848 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005849 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005850 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005851
Martin v. Löwis544f1192004-07-27 05:22:33 +00005852 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5853 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005854 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005855 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005856 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005857 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005858
Georg Brandl96a8c392006-05-29 21:04:52 +00005859 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005860 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005861
Neal Norwitzb0493252002-03-31 14:44:22 +00005862 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005863 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005864
Martin v. Löwis544f1192004-07-27 05:22:33 +00005865 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5866 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005867 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005868 "This takes a file-like object for writing a pickle data stream.\n"
5869 "The optional proto argument tells the pickler to use the given\n"
5870 "protocol; supported protocols are 0, 1, 2. The default\n"
5871 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5872 "only protocol that can be written to a file opened in text\n"
5873 "mode and read back successfully. When using a protocol higher\n"
5874 "than 0, make sure the file is opened in binary mode, both when\n"
5875 "pickling and unpickling.)\n"
5876 "\n"
5877 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5878 "more efficient than protocol 1.\n"
5879 "\n"
5880 "Specifying a negative protocol version selects the highest\n"
5881 "protocol version supported. The higher the protocol used, the\n"
5882 "more recent the version of Python needed to read the pickle\n"
5883 "produced.\n"
5884 "\n"
5885 "The file parameter must have a write() method that accepts a single\n"
5886 "string argument. It can thus be an open file object, a StringIO\n"
5887 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005888 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005889
Georg Brandl96a8c392006-05-29 21:04:52 +00005890 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005891 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5892
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005893 { NULL, NULL }
5894};
5895
Guido van Rossum60456fd1997-04-09 17:36:32 +00005896static int
Tim Peterscba30e22003-02-01 06:24:36 +00005897init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005898{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005899 PyObject *copyreg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005900
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005901#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005902
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005903 if (PyType_Ready(&Unpicklertype) < 0)
5904 return -1;
5905 if (PyType_Ready(&Picklertype) < 0)
5906 return -1;
Tim Peters3cfe7542003-05-21 21:29:48 +00005907
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005908 INIT_STR(__class__);
5909 INIT_STR(__getinitargs__);
5910 INIT_STR(__dict__);
5911 INIT_STR(__getstate__);
5912 INIT_STR(__setstate__);
5913 INIT_STR(__name__);
5914 INIT_STR(__main__);
5915 INIT_STR(__reduce__);
5916 INIT_STR(__reduce_ex__);
5917 INIT_STR(write);
5918 INIT_STR(append);
5919 INIT_STR(read);
5920 INIT_STR(readline);
5921 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005922
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005923 if (!( copyreg = PyImport_ImportModule("copy_reg")))
5924 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005925
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005926 /* This is special because we want to use a different
5927 one in restricted mode. */
5928 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
5929 if (!dispatch_table) return -1;
Tim Peters5b7da392003-02-04 00:21:07 +00005930
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005931 extension_registry = PyObject_GetAttrString(copyreg,
5932 "_extension_registry");
5933 if (!extension_registry) return -1;
Tim Peters5b7da392003-02-04 00:21:07 +00005934
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005935 inverted_registry = PyObject_GetAttrString(copyreg,
5936 "_inverted_registry");
5937 if (!inverted_registry) return -1;
Tim Peters5b7da392003-02-04 00:21:07 +00005938
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005939 extension_cache = PyObject_GetAttrString(copyreg,
5940 "_extension_cache");
5941 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005942
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005943 Py_DECREF(copyreg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005944
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005945 if (!(empty_tuple = PyTuple_New(0)))
5946 return -1;
Tim Peters731098b2003-02-04 20:56:09 +00005947
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005948 two_tuple = PyTuple_New(2);
5949 if (two_tuple == NULL)
5950 return -1;
5951 /* We use this temp container with no regard to refcounts, or to
5952 * keeping containees alive. Exempt from GC, because we don't
5953 * want anything looking at two_tuple() by magic.
5954 */
5955 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005956
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005957 /* Ugh */
5958 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5959 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5960 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005961
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005962 if (!( t=PyDict_New())) return -1;
5963 if (!( r=PyRun_String(
5964 "def __str__(self):\n"
5965 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5966 Py_file_input,
5967 module_dict, t) )) return -1;
5968 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005969
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005970 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5971 if (!PickleError)
5972 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005973
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005974 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005975
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005976 PicklingError = PyErr_NewException("cPickle.PicklingError",
5977 PickleError, NULL);
5978 if (!PicklingError)
5979 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005980
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005981 if (!( t=PyDict_New())) return -1;
5982 if (!( r=PyRun_String(
5983 "def __str__(self):\n"
5984 " a=self.args\n"
5985 " a=a and type(a[0]) or '(what)'\n"
5986 " return 'Cannot pickle %s objects' % a\n"
5987 , Py_file_input,
5988 module_dict, t) )) return -1;
5989 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005990
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005991 if (!( UnpickleableError = PyErr_NewException(
5992 "cPickle.UnpickleableError", PicklingError, t)))
5993 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005994
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005995 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005996
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005997 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5998 PickleError, NULL)))
5999 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00006000
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006001 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
6002 UnpicklingError, NULL)))
6003 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00006004
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006005 if (PyDict_SetItemString(module_dict, "PickleError",
6006 PickleError) < 0)
6007 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00006008
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006009 if (PyDict_SetItemString(module_dict, "PicklingError",
6010 PicklingError) < 0)
6011 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00006012
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006013 if (PyDict_SetItemString(module_dict, "UnpicklingError",
6014 UnpicklingError) < 0)
6015 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00006016
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006017 if (PyDict_SetItemString(module_dict, "UnpickleableError",
6018 UnpickleableError) < 0)
6019 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00006020
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006021 if (PyDict_SetItemString(module_dict, "BadPickleGet",
6022 BadPickleGet) < 0)
6023 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00006024
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006025 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00006026
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006027 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006028}
6029
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006030#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
Mark Hammondfe51c6d2002-08-02 02:27:13 +00006031#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00006032#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00006033PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00006034initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00006035{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006036 PyObject *m, *d, *di, *v, *k;
6037 Py_ssize_t i;
6038 char *rev = "1.71"; /* XXX when does this change? */
6039 PyObject *format_version;
6040 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006041
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006042 Py_TYPE(&Picklertype) = &PyType_Type;
6043 Py_TYPE(&Unpicklertype) = &PyType_Type;
6044 Py_TYPE(&PdataType) = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006045
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006046 /* Initialize some pieces. We need to do this before module creation,
6047 * so we're forced to use a temporary dictionary. :(
6048 */
6049 di = PyDict_New();
6050 if (!di) return;
6051 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00006052
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006053 /* Create the module and add the functions */
6054 m = Py_InitModule4("cPickle", cPickle_methods,
6055 cPickle_module_documentation,
6056 (PyObject*)NULL,PYTHON_API_VERSION);
6057 if (m == NULL)
6058 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006059
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006060 /* Add some symbolic constants to the module */
6061 d = PyModule_GetDict(m);
6062 v = PyString_FromString(rev);
6063 PyDict_SetItemString(d, "__version__", v);
6064 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00006065
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006066 /* Copy data from di. Waaa. */
6067 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
6068 if (PyObject_SetItem(d, k, v) < 0) {
6069 Py_DECREF(di);
6070 return;
6071 }
6072 }
6073 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00006074
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006075 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
6076 if (i < 0)
6077 return;
Tim Peters8587b3c2003-02-13 15:44:41 +00006078
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006079 /* These are purely informational; no code uses them. */
6080 /* File format version we write. */
6081 format_version = PyString_FromString("2.0");
6082 /* Format versions we can read. */
6083 compatible_formats = Py_BuildValue("[sssss]",
6084 "1.0", /* Original protocol 0 */
6085 "1.1", /* Protocol 0 + INST */
6086 "1.2", /* Original protocol 1 */
6087 "1.3", /* Protocol 1 + BINFLOAT */
6088 "2.0"); /* Original protocol 2 */
6089 PyDict_SetItemString(d, "format_version", format_version);
6090 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
6091 Py_XDECREF(format_version);
6092 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006093}