blob: 914ebb3eebeedf480bedf218b1e21cf32bba9449 [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
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300692 Py_XSETREF(self->last_string, str);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000693
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000694 if (! (*s = PyString_AsString(str))) return -1;
Amaury Forgeot d'Arc74b30162009-07-23 19:26:02 +0000695
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000696 if (PyString_GET_SIZE(str) != n) {
697 PyErr_SetNone(PyExc_EOFError);
698 return -1;
699 }
Amaury Forgeot d'Arc74b30162009-07-23 19:26:02 +0000700
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000701 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000702}
703
704
Martin v. Löwis18e16552006-02-15 17:27:45 +0000705static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000706readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000707{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000708 PyObject *str;
709 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000710
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000711 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
712 return -1;
713 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000714
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000715 if ((str_size = PyString_Size(str)) < 0)
716 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000717
Serhiy Storchakabc62af12016-04-06 09:51:18 +0300718 Py_XSETREF(self->last_string, str);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000719
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000720 if (! (*s = PyString_AsString(str)))
721 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000722
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000723 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000724}
725
Tim Petersee1a53c2003-02-02 02:57:53 +0000726/* Copy the first n bytes from s into newly malloc'ed memory, plus a
727 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
728 * The caller is responsible for free()'ing the return value.
729 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000730static char *
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200731pystrndup(const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000732{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000733 char *r = (char *)malloc(n+1);
734 if (r == NULL)
735 return (char*)PyErr_NoMemory();
736 memcpy(r, s, n);
737 r[n] = 0;
738 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000739}
740
741
742static int
Tim Peterscba30e22003-02-01 06:24:36 +0000743get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000744{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000745 PyObject *value, *mv;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200746 Py_ssize_t c_value;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000747 char s[30];
748 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000749
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000750 if (!( mv = PyDict_GetItem(self->memo, id))) {
751 PyErr_SetObject(PyExc_KeyError, id);
752 return -1;
753 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000754
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000755 if (!( value = PyTuple_GetItem(mv, 0)))
756 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000757
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000758 if (!( PyInt_Check(value))) {
759 PyErr_SetString(PicklingError, "no int where int expected in memo");
760 return -1;
761 }
762 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000763
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000764 if (!self->bin) {
765 s[0] = GET;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200766 PyOS_snprintf(s + 1, sizeof(s) - 1,
767 "%" PY_FORMAT_SIZE_T "d\n", c_value);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000768 len = strlen(s);
769 }
770 else if (Pdata_Check(self->file)) {
771 if (write_other(self, NULL, 0) < 0) return -1;
772 PDATA_APPEND(self->file, mv, -1);
773 return 0;
774 }
775 else {
776 if (c_value < 256) {
777 s[0] = BINGET;
778 s[1] = (int)(c_value & 0xff);
779 len = 2;
780 }
Serhiy Storchaka1aa18032017-03-14 07:29:33 +0200781 else if (c_value < 0x7fffffffL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000782 s[0] = LONG_BINGET;
783 s[1] = (int)(c_value & 0xff);
784 s[2] = (int)((c_value >> 8) & 0xff);
785 s[3] = (int)((c_value >> 16) & 0xff);
786 s[4] = (int)((c_value >> 24) & 0xff);
787 len = 5;
788 }
Serhiy Storchaka1aa18032017-03-14 07:29:33 +0200789 else { /* unlikely */
790 PyErr_SetString(PicklingError,
791 "memo id too large for LONG_BINGET");
792 return -1;
793 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000794 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000795
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000796 if (self->write_func(self, s, len) < 0)
797 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000798
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000799 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000800}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000801
Guido van Rossum60456fd1997-04-09 17:36:32 +0000802
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000803static int
Tim Peterscba30e22003-02-01 06:24:36 +0000804put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000805{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000806 if (Py_REFCNT(ob) < 2 || self->fast)
807 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000808
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000809 return put2(self, ob);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000810}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000811
Guido van Rossum053b8df1998-11-25 16:18:00 +0000812
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000813static int
Tim Peterscba30e22003-02-01 06:24:36 +0000814put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000815{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000816 char c_str[30];
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200817 Py_ssize_t len, p;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000818 int res = -1;
819 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000820
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000821 if (self->fast)
822 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000823
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000824 if ((p = PyDict_Size(self->memo)) < 0)
825 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000826
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000827 /* Make sure memo keys are positive! */
828 /* XXX Why?
829 * XXX And does "positive" really mean non-negative?
830 * XXX pickle.py starts with PUT index 0, not 1. This makes for
831 * XXX gratuitous differences between the pickling modules.
832 */
833 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000834
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000835 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
836 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000837
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000838 if (!( memo_len = PyInt_FromLong(p)))
839 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000840
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000841 if (!( t = PyTuple_New(2)))
842 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000843
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000844 PyTuple_SET_ITEM(t, 0, memo_len);
845 Py_INCREF(memo_len);
846 PyTuple_SET_ITEM(t, 1, ob);
847 Py_INCREF(ob);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000848
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000849 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
850 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000851
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000852 if (!self->bin) {
853 c_str[0] = PUT;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200854 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1,
855 "%" PY_FORMAT_SIZE_T "d\n", p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000856 len = strlen(c_str);
857 }
858 else if (Pdata_Check(self->file)) {
859 if (write_other(self, NULL, 0) < 0) return -1;
860 PDATA_APPEND(self->file, memo_len, -1);
861 res=0; /* Job well done ;) */
862 goto finally;
863 }
864 else {
Serhiy Storchaka1aa18032017-03-14 07:29:33 +0200865 if (p < 256) {
866 c_str[0] = BINPUT;
867 c_str[1] = p;
868 len = 2;
869 }
870 else if (p < 0x7fffffffL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000871 c_str[0] = LONG_BINPUT;
872 c_str[1] = (int)(p & 0xff);
873 c_str[2] = (int)((p >> 8) & 0xff);
874 c_str[3] = (int)((p >> 16) & 0xff);
875 c_str[4] = (int)((p >> 24) & 0xff);
876 len = 5;
877 }
Serhiy Storchaka1aa18032017-03-14 07:29:33 +0200878 else { /* unlikely */
879 PyErr_SetString(PicklingError,
880 "memo id too large for LONG_BINPUT");
881 goto finally;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000882 }
883 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000884
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000885 if (self->write_func(self, c_str, len) < 0)
886 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000887
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000888 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000889
890 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000891 Py_XDECREF(py_ob_id);
892 Py_XDECREF(memo_len);
893 Py_XDECREF(t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000894
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000895 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000896}
897
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000898static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000899whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000900{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000901 Py_ssize_t i, j;
902 PyObject *module = 0, *modules_dict = 0,
903 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000904
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000905 module = PyObject_GetAttrString(global, "__module__");
906 if (module)
907 return module;
908 if (PyErr_ExceptionMatches(PyExc_AttributeError))
909 PyErr_Clear();
910 else
911 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000912
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000913 if (!( modules_dict = PySys_GetObject("modules")))
914 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000915
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000916 i = 0;
917 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000918
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000919 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000920
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000921 global_name_attr = PyObject_GetAttr(module, global_name);
922 if (!global_name_attr) {
923 if (PyErr_ExceptionMatches(PyExc_AttributeError))
924 PyErr_Clear();
925 else
926 return NULL;
927 continue;
928 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000929
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000930 if (global_name_attr != global) {
931 Py_DECREF(global_name_attr);
932 continue;
933 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000934
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000935 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000936
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000937 break;
938 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000939
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000940 /* The following implements the rule in pickle.py added in 1.5
941 that used __main__ if no module is found. I don't actually
942 like this rule. jlf
943 */
944 if (!j) {
945 name=__main___str;
946 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000947
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000948 Py_INCREF(name);
949 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000950}
951
952
Guido van Rossum60456fd1997-04-09 17:36:32 +0000953static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000954fast_save_enter(Picklerobject *self, PyObject *obj)
955{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000956 /* if fast_container < 0, we're doing an error exit. */
957 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
958 PyObject *key = NULL;
959 if (self->fast_memo == NULL) {
960 self->fast_memo = PyDict_New();
961 if (self->fast_memo == NULL) {
962 self->fast_container = -1;
963 return 0;
964 }
965 }
966 key = PyLong_FromVoidPtr(obj);
967 if (key == NULL)
968 return 0;
969 if (PyDict_GetItem(self->fast_memo, key)) {
970 Py_DECREF(key);
971 PyErr_Format(PyExc_ValueError,
972 "fast mode: can't pickle cyclic objects "
973 "including object type %s at %p",
974 Py_TYPE(obj)->tp_name, obj);
975 self->fast_container = -1;
976 return 0;
977 }
978 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
979 Py_DECREF(key);
980 self->fast_container = -1;
981 return 0;
982 }
983 Py_DECREF(key);
984 }
985 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000986}
987
Tim Peterscba30e22003-02-01 06:24:36 +0000988int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000989fast_save_leave(Picklerobject *self, PyObject *obj)
990{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000991 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
992 PyObject *key = PyLong_FromVoidPtr(obj);
993 if (key == NULL)
994 return 0;
995 if (PyDict_DelItem(self->fast_memo, key) < 0) {
996 Py_DECREF(key);
997 return 0;
998 }
999 Py_DECREF(key);
1000 }
1001 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +00001002}
1003
1004static int
Tim Peterscba30e22003-02-01 06:24:36 +00001005save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001006{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001007 static char none = NONE;
1008 if (self->write_func(self, &none, 1) < 0)
1009 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001010
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001011 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001012}
1013
Guido van Rossum77f6a652002-04-03 22:41:51 +00001014static int
Tim Peterscba30e22003-02-01 06:24:36 +00001015save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +00001016{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001017 static const char *buf[2] = {FALSE, TRUE};
1018 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
1019 long l = PyInt_AS_LONG((PyIntObject *)args);
Guido van Rossum77f6a652002-04-03 22:41:51 +00001020
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001021 if (self->proto >= 2) {
1022 char opcode = l ? NEWTRUE : NEWFALSE;
1023 if (self->write_func(self, &opcode, 1) < 0)
1024 return -1;
1025 }
1026 else if (self->write_func(self, buf[l], len[l]) < 0)
1027 return -1;
1028 return 0;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001029}
Tim Peters84e87f32001-03-17 04:50:51 +00001030
Guido van Rossum60456fd1997-04-09 17:36:32 +00001031static int
Tim Peterscba30e22003-02-01 06:24:36 +00001032save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001033{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001034 char c_str[32];
1035 long l = PyInt_AS_LONG((PyIntObject *)args);
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001036 Py_ssize_t len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001037
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001038 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001039#if SIZEOF_LONG > 4
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001040 || l > 0x7fffffffL
1041 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001042#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001043 ) {
1044 /* Text-mode pickle, or long too big to fit in the 4-byte
1045 * signed BININT format: store as a string.
1046 */
1047 c_str[0] = INT;
1048 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
1049 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1050 return -1;
1051 }
1052 else {
1053 /* Binary pickle and l fits in a signed 4-byte int. */
1054 c_str[1] = (int)( l & 0xff);
1055 c_str[2] = (int)((l >> 8) & 0xff);
1056 c_str[3] = (int)((l >> 16) & 0xff);
1057 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001058
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001059 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1060 if (c_str[2] == 0) {
1061 c_str[0] = BININT1;
1062 len = 2;
1063 }
1064 else {
1065 c_str[0] = BININT2;
1066 len = 3;
1067 }
1068 }
1069 else {
1070 c_str[0] = BININT;
1071 len = 5;
1072 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001073
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001074 if (self->write_func(self, c_str, len) < 0)
1075 return -1;
1076 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001077
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001078 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001079}
1080
1081
1082static int
Tim Peterscba30e22003-02-01 06:24:36 +00001083save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001084{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001085 Py_ssize_t size;
1086 int res = -1;
1087 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001088
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001089 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001090
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001091 if (self->proto >= 2) {
1092 /* Linear-time pickling. */
1093 size_t nbits;
1094 size_t nbytes;
1095 unsigned char *pdata;
1096 char c_str[5];
1097 int i;
1098 int sign = _PyLong_Sign(args);
Tim Petersee1a53c2003-02-02 02:57:53 +00001099
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001100 if (sign == 0) {
1101 /* It's 0 -- an empty bytestring. */
1102 c_str[0] = LONG1;
1103 c_str[1] = 0;
1104 i = self->write_func(self, c_str, 2);
1105 if (i < 0) goto finally;
1106 res = 0;
1107 goto finally;
1108 }
1109 nbits = _PyLong_NumBits(args);
1110 if (nbits == (size_t)-1 && PyErr_Occurred())
1111 goto finally;
1112 /* How many bytes do we need? There are nbits >> 3 full
1113 * bytes of data, and nbits & 7 leftover bits. If there
1114 * are any leftover bits, then we clearly need another
1115 * byte. Wnat's not so obvious is that we *probably*
1116 * need another byte even if there aren't any leftovers:
1117 * the most-significant bit of the most-significant byte
1118 * acts like a sign bit, and it's usually got a sense
1119 * opposite of the one we need. The exception is longs
1120 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1121 * its own 256's-complement, so has the right sign bit
1122 * even without the extra byte. That's a pain to check
1123 * for in advance, though, so we always grab an extra
1124 * byte at the start, and cut it back later if possible.
1125 */
1126 nbytes = (nbits >> 3) + 1;
1127 if (nbytes > INT_MAX) {
1128 PyErr_SetString(PyExc_OverflowError, "long too large "
1129 "to pickle");
1130 goto finally;
1131 }
1132 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1133 if (repr == NULL) goto finally;
1134 pdata = (unsigned char *)PyString_AS_STRING(repr);
1135 i = _PyLong_AsByteArray((PyLongObject *)args,
1136 pdata, nbytes,
1137 1 /* little endian */, 1 /* signed */);
1138 if (i < 0) goto finally;
1139 /* If the long is negative, this may be a byte more than
1140 * needed. This is so iff the MSB is all redundant sign
1141 * bits.
1142 */
1143 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1144 (pdata[nbytes - 2] & 0x80) != 0)
1145 --nbytes;
Tim Petersee1a53c2003-02-02 02:57:53 +00001146
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001147 if (nbytes < 256) {
1148 c_str[0] = LONG1;
1149 c_str[1] = (char)nbytes;
1150 size = 2;
1151 }
1152 else {
1153 c_str[0] = LONG4;
1154 size = (int)nbytes;
1155 for (i = 1; i < 5; i++) {
1156 c_str[i] = (char)(size & 0xff);
1157 size >>= 8;
1158 }
1159 size = 5;
1160 }
1161 i = self->write_func(self, c_str, size);
1162 if (i < 0) goto finally;
1163 i = self->write_func(self, (char *)pdata, (int)nbytes);
1164 if (i < 0) goto finally;
1165 res = 0;
1166 goto finally;
1167 }
Tim Petersee1a53c2003-02-02 02:57:53 +00001168
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001169 /* proto < 2: write the repr and newline. This is quadratic-time
1170 * (in the number of digits), in both directions.
1171 */
1172 if (!( repr = PyObject_Repr(args)))
1173 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001174
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001175 if ((size = PyString_Size(repr)) < 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, &l, 1) < 0)
1179 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001180
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001181 if (self->write_func(self,
1182 PyString_AS_STRING((PyStringObject *)repr),
1183 size) < 0)
1184 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001185
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001186 if (self->write_func(self, "\n", 1) < 0)
1187 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001188
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001189 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001190
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001191 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001192 Py_XDECREF(repr);
1193 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001194}
1195
1196
1197static int
Tim Peterscba30e22003-02-01 06:24:36 +00001198save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001199{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001200 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001201
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001202 if (self->bin) {
1203 char str[9];
1204 str[0] = BINFLOAT;
1205 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
1206 return -1;
1207 if (self->write_func(self, str, 9) < 0)
1208 return -1;
1209 }
1210 else {
1211 int result = -1;
1212 char *buf = NULL;
1213 char op = FLOAT;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001214
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001215 if (self->write_func(self, &op, 1) < 0)
1216 goto done;
Eric Smithb05d3be2009-10-26 15:06:39 +00001217
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001218 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
1219 if (!buf) {
1220 PyErr_NoMemory();
1221 goto done;
1222 }
Eric Smithb05d3be2009-10-26 15:06:39 +00001223
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001224 if (self->write_func(self, buf, strlen(buf)) < 0)
1225 goto done;
Eric Smithb05d3be2009-10-26 15:06:39 +00001226
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001227 if (self->write_func(self, "\n", 1) < 0)
1228 goto done;
Eric Smithb05d3be2009-10-26 15:06:39 +00001229
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001230 result = 0;
Eric Smithb05d3be2009-10-26 15:06:39 +00001231done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001232 PyMem_Free(buf);
1233 return result;
1234 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001235
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001236 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001237}
1238
1239
1240static int
Tim Peterscba30e22003-02-01 06:24:36 +00001241save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001242{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001243 Py_ssize_t size, len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001244 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001245
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001246 if ((size = PyString_Size(args)) < 0)
1247 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001248
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001249 if (!self->bin) {
1250 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001251
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001252 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001253
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001254 if (!( repr = PyObject_Repr(args)))
1255 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001256
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001257 if ((len = PyString_Size(repr)) < 0)
1258 goto err;
1259 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001260
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001261 if (self->write_func(self, &string, 1) < 0)
1262 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001263
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001264 if (self->write_func(self, repr_str, len) < 0)
1265 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001266
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001267 if (self->write_func(self, "\n", 1) < 0)
1268 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001269
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001270 Py_XDECREF(repr);
1271 }
1272 else {
1273 int i;
1274 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001275
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001276 if (size < 256) {
1277 c_str[0] = SHORT_BINSTRING;
1278 c_str[1] = size;
1279 len = 2;
1280 }
Serhiy Storchaka1aa18032017-03-14 07:29:33 +02001281 else if (size <= 0x7fffffffL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001282 c_str[0] = BINSTRING;
1283 for (i = 1; i < 5; i++)
1284 c_str[i] = (int)(size >> ((i - 1) * 8));
1285 len = 5;
1286 }
Serhiy Storchaka1aa18032017-03-14 07:29:33 +02001287 else {
1288 PyErr_SetString(PyExc_OverflowError,
1289 "cannot serialize a string larger than 2 GiB");
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001290 return -1; /* string too large */
Serhiy Storchaka1aa18032017-03-14 07:29:33 +02001291 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001292
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001293 if (self->write_func(self, c_str, len) < 0)
1294 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001295
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001296 if (size > 128 && Pdata_Check(self->file)) {
1297 if (write_other(self, NULL, 0) < 0) return -1;
1298 PDATA_APPEND(self->file, args, -1);
1299 }
1300 else {
1301 if (self->write_func(self,
1302 PyString_AS_STRING(
1303 (PyStringObject *)args),
1304 size) < 0)
1305 return -1;
1306 }
1307 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001308
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001309 if (doput)
1310 if (put(self, args) < 0)
1311 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001312
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001313 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001314
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001315 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001316 Py_XDECREF(repr);
1317 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001318}
1319
1320
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001321#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001322/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1323 backslash and newline characters to \uXXXX escapes. */
1324static PyObject *
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001325modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001326{
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001327 PyObject *repr;
1328 char *p;
1329 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001330
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001331 static const char *hexdigit = "0123456789abcdef";
1332#ifdef Py_UNICODE_WIDE
1333 const Py_ssize_t expandsize = 10;
1334#else
1335 const Py_ssize_t expandsize = 6;
1336#endif
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001337
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001338 if (size > PY_SSIZE_T_MAX / expandsize)
Martin Panterca56dd42016-09-17 07:54:55 +00001339 return PyErr_NoMemory();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001340
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001341 repr = PyString_FromStringAndSize(NULL, expandsize * size);
1342 if (repr == NULL)
Martin Panterca56dd42016-09-17 07:54:55 +00001343 return NULL;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001344 if (size == 0)
Martin Panterca56dd42016-09-17 07:54:55 +00001345 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001346
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001347 p = q = PyString_AS_STRING(repr);
1348 while (size-- > 0) {
Martin Panterca56dd42016-09-17 07:54:55 +00001349 Py_UNICODE ch = *s++;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001350#ifdef Py_UNICODE_WIDE
Martin Panterca56dd42016-09-17 07:54:55 +00001351 /* Map 32-bit characters to '\Uxxxxxxxx' */
1352 if (ch >= 0x10000) {
1353 *p++ = '\\';
1354 *p++ = 'U';
1355 *p++ = hexdigit[(ch >> 28) & 0xf];
1356 *p++ = hexdigit[(ch >> 24) & 0xf];
1357 *p++ = hexdigit[(ch >> 20) & 0xf];
1358 *p++ = hexdigit[(ch >> 16) & 0xf];
1359 *p++ = hexdigit[(ch >> 12) & 0xf];
1360 *p++ = hexdigit[(ch >> 8) & 0xf];
1361 *p++ = hexdigit[(ch >> 4) & 0xf];
1362 *p++ = hexdigit[ch & 15];
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001363 }
Martin Panterca56dd42016-09-17 07:54:55 +00001364 else
1365#else
1366 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1367 if (ch >= 0xD800 && ch < 0xDC00) {
1368 Py_UNICODE ch2;
1369 Py_UCS4 ucs;
1370
1371 ch2 = *s++;
1372 size--;
1373 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1374 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1375 *p++ = '\\';
1376 *p++ = 'U';
1377 *p++ = hexdigit[(ucs >> 28) & 0xf];
1378 *p++ = hexdigit[(ucs >> 24) & 0xf];
1379 *p++ = hexdigit[(ucs >> 20) & 0xf];
1380 *p++ = hexdigit[(ucs >> 16) & 0xf];
1381 *p++ = hexdigit[(ucs >> 12) & 0xf];
1382 *p++ = hexdigit[(ucs >> 8) & 0xf];
1383 *p++ = hexdigit[(ucs >> 4) & 0xf];
1384 *p++ = hexdigit[ucs & 0xf];
1385 continue;
1386 }
1387 /* Fall through: isolated surrogates are copied as-is */
1388 s--;
1389 size++;
1390 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001391#endif
Martin Panterca56dd42016-09-17 07:54:55 +00001392 /* Map 16-bit characters to '\uxxxx' */
1393 if (ch >= 256 || ch == '\\' || ch == '\n') {
1394 *p++ = '\\';
1395 *p++ = 'u';
1396 *p++ = hexdigit[(ch >> 12) & 0xf];
1397 *p++ = hexdigit[(ch >> 8) & 0xf];
1398 *p++ = hexdigit[(ch >> 4) & 0xf];
1399 *p++ = hexdigit[ch & 15];
1400 }
1401 /* Copy everything else as-is */
1402 else
1403 *p++ = (char) ch;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001404 }
1405 *p = '\0';
1406 _PyString_Resize(&repr, p - q);
1407 return repr;
1408}
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001409
Guido van Rossum60456fd1997-04-09 17:36:32 +00001410static int
Tim Peterscba30e22003-02-01 06:24:36 +00001411save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001412{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001413 Py_ssize_t size, len;
1414 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001415
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001416 if (!PyUnicode_Check(args))
1417 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001418
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001419 if (!self->bin) {
1420 char *repr_str;
1421 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001422
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001423 repr = modified_EncodeRawUnicodeEscape(
1424 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1425 if (!repr)
1426 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001427
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001428 if ((len = PyString_Size(repr)) < 0)
1429 goto err;
1430 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001431
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001432 if (self->write_func(self, &string, 1) < 0)
1433 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001434
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001435 if (self->write_func(self, repr_str, len) < 0)
1436 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001437
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001438 if (self->write_func(self, "\n", 1) < 0)
1439 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001440
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001441 Py_XDECREF(repr);
1442 }
1443 else {
1444 int i;
1445 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001446
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001447 if (!( repr = PyUnicode_AsUTF8String(args)))
1448 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001449
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001450 if ((size = PyString_Size(repr)) < 0)
1451 goto err;
Serhiy Storchaka1aa18032017-03-14 07:29:33 +02001452 if (size > 0x7fffffffL) {
1453 PyErr_SetString(PyExc_OverflowError,
1454 "cannot serialize a Unicode string larger than 2 GiB");
1455 goto err; /* string too large */
1456 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001457
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001458 c_str[0] = BINUNICODE;
1459 for (i = 1; i < 5; i++)
1460 c_str[i] = (int)(size >> ((i - 1) * 8));
1461 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001462
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001463 if (self->write_func(self, c_str, len) < 0)
1464 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001465
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001466 if (size > 128 && Pdata_Check(self->file)) {
1467 if (write_other(self, NULL, 0) < 0)
1468 goto err;
1469 PDATA_APPEND(self->file, repr, -1);
1470 }
1471 else {
1472 if (self->write_func(self, PyString_AS_STRING(repr),
1473 size) < 0)
1474 goto err;
1475 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001476
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001477 Py_DECREF(repr);
1478 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001479
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001480 if (doput)
1481 if (put(self, args) < 0)
1482 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001483
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001484 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001485
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001486 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001487 Py_XDECREF(repr);
1488 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001489}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001490#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001491
Tim Peters1d63c9f2003-02-02 20:29:39 +00001492/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1493static int
Tim Peters67920142003-02-05 03:46:17 +00001494store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001495{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001496 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001497 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001498
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001499 assert(PyTuple_Size(t) == len);
Tim Peters1d63c9f2003-02-02 20:29:39 +00001500
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001501 for (i = 0; i < len; i++) {
1502 PyObject *element = PyTuple_GET_ITEM(t, i);
Tim Peters1d63c9f2003-02-02 20:29:39 +00001503
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001504 if (element == NULL)
1505 goto finally;
1506 if (save(self, element, 0) < 0)
1507 goto finally;
1508 }
1509 res = 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001510
1511 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001512 return res;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001513}
1514
1515/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1516 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001517 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001518 * (a tuple can be reached from itself), and that requires some subtle
1519 * magic so that it works in all cases. IOW, this is a long routine.
1520 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001521static int
Tim Peterscba30e22003-02-01 06:24:36 +00001522save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001523{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001524 PyObject *py_tuple_id = NULL;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001525 Py_ssize_t len, i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001526 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001527
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001528 static char tuple = TUPLE;
1529 static char pop = POP;
1530 static char pop_mark = POP_MARK;
1531 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001532
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001533 if ((len = PyTuple_Size(args)) < 0)
1534 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001535
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001536 if (len == 0) {
1537 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001538
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001539 if (self->proto) {
1540 c_str[0] = EMPTY_TUPLE;
1541 len = 1;
1542 }
1543 else {
1544 c_str[0] = MARK;
1545 c_str[1] = TUPLE;
1546 len = 2;
1547 }
1548 if (self->write_func(self, c_str, len) >= 0)
1549 res = 0;
1550 /* Don't memoize an empty tuple. */
1551 goto finally;
1552 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001553
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001554 /* A non-empty tuple. */
Tim Peters1d63c9f2003-02-02 20:29:39 +00001555
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001556 /* id(tuple) isn't in the memo now. If it shows up there after
1557 * saving the tuple elements, the tuple must be recursive, in
1558 * which case we'll pop everything we put on the stack, and fetch
1559 * its value from the memo.
1560 */
1561 py_tuple_id = PyLong_FromVoidPtr(args);
1562 if (py_tuple_id == NULL)
1563 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001564
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001565 if (len <= 3 && self->proto >= 2) {
1566 /* Use TUPLE{1,2,3} opcodes. */
1567 if (store_tuple_elements(self, args, len) < 0)
1568 goto finally;
1569 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1570 /* pop the len elements */
1571 for (i = 0; i < len; ++i)
1572 if (self->write_func(self, &pop, 1) < 0)
1573 goto finally;
1574 /* fetch from memo */
1575 if (get(self, py_tuple_id) < 0)
1576 goto finally;
1577 res = 0;
1578 goto finally;
1579 }
1580 /* Not recursive. */
1581 if (self->write_func(self, len2opcode + len, 1) < 0)
1582 goto finally;
1583 goto memoize;
1584 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001585
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001586 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1587 * Generate MARK elt1 elt2 ... TUPLE
1588 */
1589 if (self->write_func(self, &MARKv, 1) < 0)
1590 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001591
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001592 if (store_tuple_elements(self, args, len) < 0)
1593 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001594
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001595 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1596 /* pop the stack stuff we pushed */
1597 if (self->bin) {
1598 if (self->write_func(self, &pop_mark, 1) < 0)
1599 goto finally;
1600 }
1601 else {
1602 /* Note that we pop one more than len, to remove
1603 * the MARK too.
1604 */
1605 for (i = 0; i <= len; i++)
1606 if (self->write_func(self, &pop, 1) < 0)
1607 goto finally;
1608 }
1609 /* fetch from memo */
1610 if (get(self, py_tuple_id) >= 0)
1611 res = 0;
1612 goto finally;
1613 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001614
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001615 /* Not recursive. */
1616 if (self->write_func(self, &tuple, 1) < 0)
1617 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001618
Tim Peters1d63c9f2003-02-02 20:29:39 +00001619 memoize:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001620 if (put(self, args) >= 0)
1621 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001622
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001623 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001624 Py_XDECREF(py_tuple_id);
1625 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001626}
1627
Tim Peters1092d642003-02-11 21:06:20 +00001628/* iter is an iterator giving items, and we batch up chunks of
1629 * MARK item item ... item APPENDS
1630 * opcode sequences. Calling code should have arranged to first create an
1631 * empty list, or list-like object, for the APPENDS to operate on.
1632 * Returns 0 on success, <0 on error.
1633 */
1634static int
1635batch_list(Picklerobject *self, PyObject *iter)
1636{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001637 PyObject *obj = NULL;
1638 PyObject *firstitem = NULL;
1639 int i, n;
Tim Peters1092d642003-02-11 21:06:20 +00001640
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001641 static char append = APPEND;
1642 static char appends = APPENDS;
Tim Peters1092d642003-02-11 21:06:20 +00001643
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001644 assert(iter != NULL);
Tim Peters1092d642003-02-11 21:06:20 +00001645
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001646 if (self->proto == 0) {
1647 /* APPENDS isn't available; do one at a time. */
1648 for (;;) {
1649 obj = PyIter_Next(iter);
1650 if (obj == NULL) {
1651 if (PyErr_Occurred())
1652 return -1;
1653 break;
1654 }
1655 i = save(self, obj, 0);
1656 Py_DECREF(obj);
1657 if (i < 0)
1658 return -1;
1659 if (self->write_func(self, &append, 1) < 0)
1660 return -1;
1661 }
1662 return 0;
1663 }
Tim Peters1092d642003-02-11 21:06:20 +00001664
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001665 /* proto > 0: write in batches of BATCHSIZE. */
1666 do {
1667 /* Get first item */
1668 firstitem = PyIter_Next(iter);
1669 if (firstitem == NULL) {
1670 if (PyErr_Occurred())
1671 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001672
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001673 /* nothing more to add */
1674 break;
1675 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001676
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001677 /* Try to get a second item */
1678 obj = PyIter_Next(iter);
1679 if (obj == NULL) {
1680 if (PyErr_Occurred())
1681 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001682
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001683 /* Only one item to write */
1684 if (save(self, firstitem, 0) < 0)
1685 goto BatchFailed;
1686 if (self->write_func(self, &append, 1) < 0)
1687 goto BatchFailed;
1688 Py_CLEAR(firstitem);
1689 break;
1690 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001691
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001692 /* More than one item to write */
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001693
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001694 /* Pump out MARK, items, APPENDS. */
1695 if (self->write_func(self, &MARKv, 1) < 0)
1696 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001697
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001698 if (save(self, firstitem, 0) < 0)
1699 goto BatchFailed;
1700 Py_CLEAR(firstitem);
1701 n = 1;
Tim Peters1092d642003-02-11 21:06:20 +00001702
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001703 /* Fetch and save up to BATCHSIZE items */
1704 while (obj) {
1705 if (save(self, obj, 0) < 0)
1706 goto BatchFailed;
1707 Py_CLEAR(obj);
1708 n += 1;
Tim Peters1092d642003-02-11 21:06:20 +00001709
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001710 if (n == BATCHSIZE)
1711 break;
1712
1713 obj = PyIter_Next(iter);
1714 if (obj == NULL) {
1715 if (PyErr_Occurred())
1716 goto BatchFailed;
1717 break;
1718 }
1719 }
1720
1721 if (self->write_func(self, &appends, 1) < 0)
1722 goto BatchFailed;
1723
1724 } while (n == BATCHSIZE);
1725 return 0;
Tim Peters1092d642003-02-11 21:06:20 +00001726
1727BatchFailed:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001728 Py_XDECREF(firstitem);
1729 Py_XDECREF(obj);
1730 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001731}
1732
Guido van Rossum60456fd1997-04-09 17:36:32 +00001733static int
Tim Peterscba30e22003-02-01 06:24:36 +00001734save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001735{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001736 int res = -1;
1737 char s[3];
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001738 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001739 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001740
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001741 if (self->fast && !fast_save_enter(self, args))
1742 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001743
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001744 /* Create an empty list. */
1745 if (self->bin) {
1746 s[0] = EMPTY_LIST;
1747 len = 1;
1748 }
1749 else {
1750 s[0] = MARK;
1751 s[1] = LIST;
1752 len = 2;
1753 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001754
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001755 if (self->write_func(self, s, len) < 0)
1756 goto finally;
Tim Peters1092d642003-02-11 21:06:20 +00001757
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001758 /* Get list length, and bow out early if empty. */
1759 if ((len = PyList_Size(args)) < 0)
1760 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001761
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001762 /* Memoize. */
1763 if (len == 0) {
1764 if (put(self, args) >= 0)
1765 res = 0;
1766 goto finally;
1767 }
1768 if (put2(self, args) < 0)
1769 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001770
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001771 /* Materialize the list elements. */
1772 iter = PyObject_GetIter(args);
1773 if (iter == NULL)
1774 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001775
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001776 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1777 {
1778 res = batch_list(self, iter);
1779 Py_LeaveRecursiveCall();
1780 }
1781 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001782
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001783 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001784 if (self->fast && !fast_save_leave(self, args))
1785 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001786
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001787 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001788}
1789
1790
Tim Peters42f08ac2003-02-11 22:43:24 +00001791/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1792 * MARK key value ... key value SETITEMS
1793 * opcode sequences. Calling code should have arranged to first create an
1794 * empty dict, or dict-like object, for the SETITEMS to operate on.
1795 * Returns 0 on success, <0 on error.
1796 *
1797 * This is very much like batch_list(). The difference between saving
1798 * elements directly, and picking apart two-tuples, is so long-winded at
1799 * the C level, though, that attempts to combine these routines were too
1800 * ugly to bear.
1801 */
1802static int
1803batch_dict(Picklerobject *self, PyObject *iter)
1804{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001805 PyObject *p = NULL;
1806 PyObject *firstitem = NULL;
1807 int i, n;
Tim Peters42f08ac2003-02-11 22:43:24 +00001808
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001809 static char setitem = SETITEM;
1810 static char setitems = SETITEMS;
Tim Peters42f08ac2003-02-11 22:43:24 +00001811
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001812 assert(iter != NULL);
Tim Peters42f08ac2003-02-11 22:43:24 +00001813
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001814 if (self->proto == 0) {
1815 /* SETITEMS isn't available; do one at a time. */
1816 for (;;) {
1817 p = PyIter_Next(iter);
1818 if (p == NULL) {
1819 if (PyErr_Occurred())
1820 return -1;
1821 break;
1822 }
1823 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1824 PyErr_SetString(PyExc_TypeError, "dict items "
1825 "iterator must return 2-tuples");
1826 return -1;
1827 }
1828 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1829 if (i >= 0)
1830 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1831 Py_DECREF(p);
1832 if (i < 0)
1833 return -1;
1834 if (self->write_func(self, &setitem, 1) < 0)
1835 return -1;
1836 }
1837 return 0;
1838 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001839
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001840 /* proto > 0: write in batches of BATCHSIZE. */
1841 do {
1842 /* Get first item */
1843 firstitem = PyIter_Next(iter);
1844 if (firstitem == NULL) {
1845 if (PyErr_Occurred())
1846 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001847
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001848 /* nothing more to add */
1849 break;
1850 }
1851 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1852 PyErr_SetString(PyExc_TypeError, "dict items "
1853 "iterator must return 2-tuples");
1854 goto BatchFailed;
1855 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001856
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001857 /* Try to get a second item */
1858 p = PyIter_Next(iter);
1859 if (p == NULL) {
1860 if (PyErr_Occurred())
1861 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001862
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001863 /* Only one item to write */
1864 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1865 goto BatchFailed;
1866 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1867 goto BatchFailed;
1868 if (self->write_func(self, &setitem, 1) < 0)
1869 goto BatchFailed;
1870 Py_CLEAR(firstitem);
1871 break;
1872 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001873
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001874 /* More than one item to write */
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001875
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001876 /* Pump out MARK, items, SETITEMS. */
1877 if (self->write_func(self, &MARKv, 1) < 0)
1878 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001879
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001880 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1881 goto BatchFailed;
1882 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1883 goto BatchFailed;
1884 Py_CLEAR(firstitem);
1885 n = 1;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001886
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001887 /* Fetch and save up to BATCHSIZE items */
1888 while (p) {
1889 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1890 PyErr_SetString(PyExc_TypeError, "dict items "
1891 "iterator must return 2-tuples");
1892 goto BatchFailed;
1893 }
1894 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1895 goto BatchFailed;
1896 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1897 goto BatchFailed;
1898 Py_CLEAR(p);
1899 n += 1;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001900
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001901 if (n == BATCHSIZE)
1902 break;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001903
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001904 p = PyIter_Next(iter);
1905 if (p == NULL) {
1906 if (PyErr_Occurred())
1907 goto BatchFailed;
1908 break;
1909 }
1910 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001911
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001912 if (self->write_func(self, &setitems, 1) < 0)
1913 goto BatchFailed;
Tim Peters42f08ac2003-02-11 22:43:24 +00001914
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001915 } while (n == BATCHSIZE);
1916 return 0;
Tim Peters42f08ac2003-02-11 22:43:24 +00001917
1918BatchFailed:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001919 Py_XDECREF(firstitem);
1920 Py_XDECREF(p);
1921 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001922}
1923
Collin Winter179bf212009-05-25 04:34:39 +00001924/* This is a variant of batch_dict() above that specializes for dicts, with no
1925 * support for dict subclasses. Like batch_dict(), we batch up chunks of
1926 * MARK key value ... key value SETITEMS
1927 * opcode sequences. Calling code should have arranged to first create an
1928 * empty dict, or dict-like object, for the SETITEMS to operate on.
1929 * Returns 0 on success, -1 on error.
1930 *
1931 * Note that this currently doesn't work for protocol 0.
1932 */
1933static int
1934batch_dict_exact(Picklerobject *self, PyObject *obj)
1935{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001936 PyObject *key = NULL, *value = NULL;
1937 int i;
1938 Py_ssize_t dict_size, ppos = 0;
Collin Winter179bf212009-05-25 04:34:39 +00001939
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001940 static char setitem = SETITEM;
1941 static char setitems = SETITEMS;
Collin Winter179bf212009-05-25 04:34:39 +00001942
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001943 assert(obj != NULL);
1944 assert(self->proto > 0);
Collin Winter179bf212009-05-25 04:34:39 +00001945
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001946 dict_size = PyDict_Size(obj);
Collin Winter179bf212009-05-25 04:34:39 +00001947
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001948 /* Special-case len(d) == 1 to save space. */
1949 if (dict_size == 1) {
1950 PyDict_Next(obj, &ppos, &key, &value);
1951 if (save(self, key, 0) < 0)
1952 return -1;
1953 if (save(self, value, 0) < 0)
1954 return -1;
1955 if (self->write_func(self, &setitem, 1) < 0)
1956 return -1;
1957 return 0;
1958 }
Collin Winter179bf212009-05-25 04:34:39 +00001959
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001960 /* Write in batches of BATCHSIZE. */
1961 do {
1962 i = 0;
1963 if (self->write_func(self, &MARKv, 1) < 0)
1964 return -1;
1965 while (PyDict_Next(obj, &ppos, &key, &value)) {
1966 if (save(self, key, 0) < 0)
1967 return -1;
1968 if (save(self, value, 0) < 0)
1969 return -1;
1970 if (++i == BATCHSIZE)
1971 break;
1972 }
1973 if (self->write_func(self, &setitems, 1) < 0)
1974 return -1;
1975 if (PyDict_Size(obj) != dict_size) {
1976 PyErr_Format(
1977 PyExc_RuntimeError,
1978 "dictionary changed size during iteration");
1979 return -1;
1980 }
Collin Winter179bf212009-05-25 04:34:39 +00001981
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001982 } while (i == BATCHSIZE);
1983 return 0;
Collin Winter179bf212009-05-25 04:34:39 +00001984}
1985
Guido van Rossum60456fd1997-04-09 17:36:32 +00001986static int
Tim Peterscba30e22003-02-01 06:24:36 +00001987save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001988{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001989 int res = -1;
1990 char s[3];
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001991 Py_ssize_t len;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001992
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001993 if (self->fast && !fast_save_enter(self, args))
1994 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001995
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001996 /* Create an empty dict. */
1997 if (self->bin) {
1998 s[0] = EMPTY_DICT;
1999 len = 1;
2000 }
2001 else {
2002 s[0] = MARK;
2003 s[1] = DICT;
2004 len = 2;
2005 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002006
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002007 if (self->write_func(self, s, len) < 0)
2008 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002009
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002010 /* Get dict size, and bow out early if empty. */
2011 if ((len = PyDict_Size(args)) < 0)
2012 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002013
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002014 if (len == 0) {
2015 if (put(self, args) >= 0)
2016 res = 0;
2017 goto finally;
2018 }
2019 if (put2(self, args) < 0)
2020 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002021
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002022 /* Materialize the dict items. */
2023 if (PyDict_CheckExact(args) && self->proto > 0) {
2024 /* We can take certain shortcuts if we know this is a dict and
2025 not a dict subclass. */
2026 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
2027 res = batch_dict_exact(self, args);
2028 Py_LeaveRecursiveCall();
2029 }
2030 } else {
2031 PyObject *iter = PyObject_CallMethod(args, "iteritems", "()");
2032 if (iter == NULL)
2033 goto finally;
2034 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
2035 res = batch_dict(self, iter);
2036 Py_LeaveRecursiveCall();
2037 }
2038 Py_DECREF(iter);
2039 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002040
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002041 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002042 if (self->fast && !fast_save_leave(self, args))
2043 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002044
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002045 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002046}
2047
2048
Tim Peters84e87f32001-03-17 04:50:51 +00002049static int
Tim Peterscba30e22003-02-01 06:24:36 +00002050save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002051{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002052 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
2053 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
2054 char *module_str, *name_str;
2055 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002056
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002057 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002058
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002059 if (self->fast && !fast_save_enter(self, args))
2060 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002061
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002062 if (self->write_func(self, &MARKv, 1) < 0)
2063 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002064
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002065 if (!( class = PyObject_GetAttr(args, __class___str)))
2066 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002067
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002068 if (self->bin) {
2069 if (save(self, class, 0) < 0)
2070 goto finally;
2071 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002072
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002073 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
2074 PyObject *element = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002075 Py_ssize_t i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002076
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002077 if (!( class_args =
2078 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
2079 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002080
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002081 if ((len = PyObject_Size(class_args)) < 0)
2082 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002083
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002084 for (i = 0; i < len; i++) {
2085 if (!( element = PySequence_GetItem(class_args, i)))
2086 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002087
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002088 if (save(self, element, 0) < 0) {
2089 Py_DECREF(element);
2090 goto finally;
2091 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002092
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002093 Py_DECREF(element);
2094 }
2095 }
2096 else {
2097 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2098 PyErr_Clear();
2099 else
2100 goto finally;
2101 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002102
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002103 if (!self->bin) {
2104 if (!( name = ((PyClassObject *)class)->cl_name )) {
2105 PyErr_SetString(PicklingError, "class has no name");
2106 goto finally;
2107 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002108
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002109 if (!( module = whichmodule(class, name)))
2110 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002111
Tim Peters84e87f32001-03-17 04:50:51 +00002112
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002113 if ((module_size = PyString_Size(module)) < 0 ||
2114 (name_size = PyString_Size(name)) < 0)
2115 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002116
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002117 module_str = PyString_AS_STRING((PyStringObject *)module);
2118 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002119
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002120 if (self->write_func(self, &inst, 1) < 0)
2121 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002122
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002123 if (self->write_func(self, module_str, module_size) < 0)
2124 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002125
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002126 if (self->write_func(self, "\n", 1) < 0)
2127 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002128
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002129 if (self->write_func(self, name_str, name_size) < 0)
2130 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002131
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002132 if (self->write_func(self, "\n", 1) < 0)
2133 goto finally;
2134 }
2135 else if (self->write_func(self, &obj, 1) < 0) {
2136 goto finally;
2137 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002138
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002139 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2140 state = PyObject_Call(getstate_func, empty_tuple, NULL);
2141 if (!state)
2142 goto finally;
2143 }
2144 else {
2145 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2146 PyErr_Clear();
2147 else
2148 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002149
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002150 if (!( state = PyObject_GetAttr(args, __dict___str))) {
2151 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2152 PyErr_Clear();
2153 else
2154 goto finally;
2155 res = 0;
2156 goto finally;
2157 }
2158 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002159
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002160 if (!PyDict_Check(state)) {
2161 if (put2(self, args) < 0)
2162 goto finally;
2163 }
2164 else {
2165 if (put(self, args) < 0)
2166 goto finally;
2167 }
Tim Peters84e87f32001-03-17 04:50:51 +00002168
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002169 if (save(self, state, 0) < 0)
2170 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002171
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002172 if (self->write_func(self, &build, 1) < 0)
2173 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002174
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002175 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002176
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002177 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002178 if (self->fast && !fast_save_leave(self, args))
2179 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002180
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002181 Py_XDECREF(module);
2182 Py_XDECREF(class);
2183 Py_XDECREF(state);
2184 Py_XDECREF(getinitargs_func);
2185 Py_XDECREF(getstate_func);
2186 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002187
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002188 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002189}
2190
2191
Guido van Rossum60456fd1997-04-09 17:36:32 +00002192static int
Tim Peterscba30e22003-02-01 06:24:36 +00002193save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002194{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002195 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
2196 char *name_str, *module_str;
2197 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002198
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002199 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002200
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002201 if (name) {
2202 global_name = name;
2203 Py_INCREF(global_name);
2204 }
2205 else {
2206 if (!( global_name = PyObject_GetAttr(args, __name___str)))
2207 goto finally;
2208 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002209
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002210 if (!( module = whichmodule(args, global_name)))
2211 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002212
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002213 if ((module_size = PyString_Size(module)) < 0 ||
2214 (name_size = PyString_Size(global_name)) < 0)
2215 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002216
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002217 module_str = PyString_AS_STRING((PyStringObject *)module);
2218 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002219
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002220 /* XXX This can be doing a relative import. Clearly it shouldn't,
2221 but I don't know how to stop it. :-( */
2222 mod = PyImport_ImportModule(module_str);
2223 if (mod == NULL) {
2224 cPickle_ErrFormat(PicklingError,
2225 "Can't pickle %s: import of module %s "
2226 "failed",
2227 "OS", args, module);
2228 goto finally;
2229 }
2230 klass = PyObject_GetAttrString(mod, name_str);
2231 if (klass == NULL) {
2232 cPickle_ErrFormat(PicklingError,
2233 "Can't pickle %s: attribute lookup %s.%s "
2234 "failed",
2235 "OSS", args, module, global_name);
2236 goto finally;
2237 }
2238 if (klass != args) {
2239 Py_DECREF(klass);
2240 cPickle_ErrFormat(PicklingError,
2241 "Can't pickle %s: it's not the same object "
2242 "as %s.%s",
2243 "OSS", args, module, global_name);
2244 goto finally;
2245 }
2246 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002247
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002248 if (self->proto >= 2) {
2249 /* See whether this is in the extension registry, and if
2250 * so generate an EXT opcode.
2251 */
2252 PyObject *py_code; /* extension code as Python object */
2253 long code; /* extension code as C value */
2254 char c_str[5];
2255 int n;
Tim Peters731098b2003-02-04 20:56:09 +00002256
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002257 PyTuple_SET_ITEM(two_tuple, 0, module);
2258 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2259 py_code = PyDict_GetItem(extension_registry, two_tuple);
2260 if (py_code == NULL)
2261 goto gen_global; /* not registered */
Tim Peters731098b2003-02-04 20:56:09 +00002262
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002263 /* Verify py_code has the right type and value. */
2264 if (!PyInt_Check(py_code)) {
2265 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2266 "extension code %s isn't an integer",
2267 "OO", args, py_code);
2268 goto finally;
2269 }
2270 code = PyInt_AS_LONG(py_code);
2271 if (code <= 0 || code > 0x7fffffffL) {
2272 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2273 "extension code %ld is out of range",
2274 "Ol", args, code);
2275 goto finally;
2276 }
Tim Peters731098b2003-02-04 20:56:09 +00002277
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002278 /* Generate an EXT opcode. */
2279 if (code <= 0xff) {
2280 c_str[0] = EXT1;
2281 c_str[1] = (char)code;
2282 n = 2;
2283 }
2284 else if (code <= 0xffff) {
2285 c_str[0] = EXT2;
2286 c_str[1] = (char)(code & 0xff);
2287 c_str[2] = (char)((code >> 8) & 0xff);
2288 n = 3;
2289 }
2290 else {
2291 c_str[0] = EXT4;
2292 c_str[1] = (char)(code & 0xff);
2293 c_str[2] = (char)((code >> 8) & 0xff);
2294 c_str[3] = (char)((code >> 16) & 0xff);
2295 c_str[4] = (char)((code >> 24) & 0xff);
2296 n = 5;
2297 }
Tim Peters731098b2003-02-04 20:56:09 +00002298
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002299 if (self->write_func(self, c_str, n) >= 0)
2300 res = 0;
2301 goto finally; /* and don't memoize */
2302 }
Tim Peters731098b2003-02-04 20:56:09 +00002303
2304 gen_global:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002305 if (self->write_func(self, &global, 1) < 0)
2306 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002307
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002308 if (self->write_func(self, module_str, module_size) < 0)
2309 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002310
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002311 if (self->write_func(self, "\n", 1) < 0)
2312 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002313
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002314 if (self->write_func(self, name_str, name_size) < 0)
2315 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002316
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002317 if (self->write_func(self, "\n", 1) < 0)
2318 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002319
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002320 if (put(self, args) < 0)
2321 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002322
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002323 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002324
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002325 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002326 Py_XDECREF(module);
2327 Py_XDECREF(global_name);
2328 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002329
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002330 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002331}
2332
Guido van Rossum60456fd1997-04-09 17:36:32 +00002333static int
Tim Peterscba30e22003-02-01 06:24:36 +00002334save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002335{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002336 PyObject *pid = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002337 Py_ssize_t size;
2338 int res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002339
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002340 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002341
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002342 Py_INCREF(args);
2343 ARG_TUP(self, args);
2344 if (self->arg) {
2345 pid = PyObject_Call(f, self->arg, NULL);
2346 FREE_ARG_TUP(self);
2347 }
2348 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002349
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002350 if (pid != Py_None) {
2351 if (!self->bin) {
2352 if (!PyString_Check(pid)) {
2353 PyErr_SetString(PicklingError,
2354 "persistent id must be string");
2355 goto finally;
2356 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002357
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002358 if (self->write_func(self, &persid, 1) < 0)
2359 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002360
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002361 if ((size = PyString_Size(pid)) < 0)
2362 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002363
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002364 if (self->write_func(self,
2365 PyString_AS_STRING(
2366 (PyStringObject *)pid),
2367 size) < 0)
2368 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002369
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002370 if (self->write_func(self, "\n", 1) < 0)
2371 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002372
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002373 res = 1;
2374 goto finally;
2375 }
2376 else if (save(self, pid, 1) >= 0) {
2377 if (self->write_func(self, &binpersid, 1) < 0)
2378 res = -1;
2379 else
2380 res = 1;
2381 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002382
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002383 goto finally;
2384 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002385
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002386 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002387
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002388 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002389 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002390
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002391 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002392}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002393
Tim Peters71fcda52003-02-14 23:05:28 +00002394/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2395 * appropriate __reduce__ method for ob.
2396 */
Tim Peters84e87f32001-03-17 04:50:51 +00002397static int
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002398save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002399{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002400 PyObject *callable;
2401 PyObject *argtup;
2402 PyObject *state = NULL;
2403 PyObject *listitems = Py_None;
2404 PyObject *dictitems = Py_None;
2405 Py_ssize_t size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002406
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002407 int use_newobj = self->proto >= 2;
Tim Peters71fcda52003-02-14 23:05:28 +00002408
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002409 static char reduce = REDUCE;
2410 static char build = BUILD;
2411 static char newobj = NEWOBJ;
Tim Peters71fcda52003-02-14 23:05:28 +00002412
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002413 size = PyTuple_Size(args);
2414 if (size < 2 || size > 5) {
2415 cPickle_ErrFormat(PicklingError, "tuple returned by "
2416 "%s must contain 2 through 5 elements",
2417 "O", fn);
2418 return -1;
2419 }
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002420
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002421 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2422 &callable,
2423 &argtup,
2424 &state,
2425 &listitems,
2426 &dictitems))
2427 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002428
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002429 if (!PyTuple_Check(argtup)) {
2430 cPickle_ErrFormat(PicklingError, "Second element of "
2431 "tuple returned by %s must be a tuple",
2432 "O", fn);
2433 return -1;
2434 }
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002435
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002436 if (state == Py_None)
2437 state = NULL;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002438
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002439 if (listitems == Py_None)
2440 listitems = NULL;
2441 else if (!PyIter_Check(listitems)) {
2442 cPickle_ErrFormat(PicklingError, "Fourth element of "
2443 "tuple returned by %s must be an iterator, not %s",
2444 "Os", fn, Py_TYPE(listitems)->tp_name);
2445 return -1;
2446 }
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002447
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002448 if (dictitems == Py_None)
2449 dictitems = NULL;
2450 else if (!PyIter_Check(dictitems)) {
2451 cPickle_ErrFormat(PicklingError, "Fifth element of "
2452 "tuple returned by %s must be an iterator, not %s",
2453 "Os", fn, Py_TYPE(dictitems)->tp_name);
2454 return -1;
2455 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002456
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002457 /* Protocol 2 special case: if callable's name is __newobj__, use
2458 * NEWOBJ. This consumes a lot of code.
2459 */
2460 if (use_newobj) {
2461 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002462
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002463 if (temp == NULL) {
2464 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2465 PyErr_Clear();
2466 else
2467 return -1;
2468 use_newobj = 0;
2469 }
2470 else {
2471 use_newobj = PyString_Check(temp) &&
2472 strcmp(PyString_AS_STRING(temp),
2473 "__newobj__") == 0;
2474 Py_DECREF(temp);
2475 }
2476 }
2477 if (use_newobj) {
2478 PyObject *cls;
2479 PyObject *newargtup;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002480 Py_ssize_t n, i;
Tim Peters71fcda52003-02-14 23:05:28 +00002481
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002482 /* Sanity checks. */
2483 n = PyTuple_Size(argtup);
2484 if (n < 1) {
2485 PyErr_SetString(PicklingError, "__newobj__ arglist "
2486 "is empty");
2487 return -1;
2488 }
Tim Peters71fcda52003-02-14 23:05:28 +00002489
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002490 cls = PyTuple_GET_ITEM(argtup, 0);
2491 if (! PyObject_HasAttrString(cls, "__new__")) {
2492 PyErr_SetString(PicklingError, "args[0] from "
2493 "__newobj__ args has no __new__");
2494 return -1;
2495 }
Tim Peters71fcda52003-02-14 23:05:28 +00002496
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002497 /* XXX How could ob be NULL? */
2498 if (ob != NULL) {
2499 PyObject *ob_dot_class;
Tim Peters71fcda52003-02-14 23:05:28 +00002500
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002501 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2502 if (ob_dot_class == NULL) {
2503 if (PyErr_ExceptionMatches(
2504 PyExc_AttributeError))
2505 PyErr_Clear();
2506 else
2507 return -1;
2508 }
2509 i = ob_dot_class != cls; /* true iff a problem */
2510 Py_XDECREF(ob_dot_class);
2511 if (i) {
2512 PyErr_SetString(PicklingError, "args[0] from "
2513 "__newobj__ args has the wrong class");
2514 return -1;
2515 }
2516 }
Tim Peters71fcda52003-02-14 23:05:28 +00002517
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002518 /* Save the class and its __new__ arguments. */
2519 if (save(self, cls, 0) < 0)
2520 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002521
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002522 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2523 if (newargtup == NULL)
2524 return -1;
2525 for (i = 1; i < n; ++i) {
2526 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2527 Py_INCREF(temp);
2528 PyTuple_SET_ITEM(newargtup, i-1, temp);
2529 }
2530 i = save(self, newargtup, 0);
2531 Py_DECREF(newargtup);
2532 if (i < 0)
2533 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002534
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002535 /* Add NEWOBJ opcode. */
2536 if (self->write_func(self, &newobj, 1) < 0)
2537 return -1;
2538 }
2539 else {
2540 /* Not using NEWOBJ. */
2541 if (save(self, callable, 0) < 0 ||
2542 save(self, argtup, 0) < 0 ||
2543 self->write_func(self, &reduce, 1) < 0)
2544 return -1;
2545 }
Tim Peters71fcda52003-02-14 23:05:28 +00002546
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002547 /* Memoize. */
2548 /* XXX How can ob be NULL? */
2549 if (ob != NULL) {
Serhiy Storchakada87e452015-11-07 11:15:32 +02002550 /* If the object is already in the memo, this means it is
2551 recursive. In this case, throw away everything we put on the
2552 stack, and fetch the object back from the memo. */
2553 if (Py_REFCNT(ob) > 1 && !self->fast) {
2554 PyObject *py_ob_id = PyLong_FromVoidPtr(ob);
2555 if (!py_ob_id)
2556 return -1;
2557 if (PyDict_GetItem(self->memo, py_ob_id)) {
2558 const char pop_op = POP;
2559 if (self->write_func(self, &pop_op, 1) < 0 ||
2560 get(self, py_ob_id) < 0) {
2561 Py_DECREF(py_ob_id);
2562 return -1;
2563 }
2564 Py_DECREF(py_ob_id);
2565 return 0;
2566 }
2567 Py_DECREF(py_ob_id);
2568 if (PyErr_Occurred())
2569 return -1;
2570 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002571 if (state && !PyDict_Check(state)) {
2572 if (put2(self, ob) < 0)
2573 return -1;
2574 }
2575 else if (put(self, ob) < 0)
2576 return -1;
2577 }
Tim Peters84e87f32001-03-17 04:50:51 +00002578
Guido van Rossum60456fd1997-04-09 17:36:32 +00002579
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002580 if (listitems && batch_list(self, listitems) < 0)
2581 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002582
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002583 if (dictitems && batch_dict(self, dictitems) < 0)
2584 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002585
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002586 if (state) {
2587 if (save(self, state, 0) < 0 ||
2588 self->write_func(self, &build, 1) < 0)
2589 return -1;
2590 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002591
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002592 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002593}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002594
Guido van Rossum60456fd1997-04-09 17:36:32 +00002595static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002596save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002597{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002598 PyTypeObject *type;
2599 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2600 int res = -1;
2601 int tmp;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002602
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002603 if (Py_EnterRecursiveCall(" while pickling an object"))
2604 return -1;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002605
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002606 if (!pers_save && self->pers_func) {
2607 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2608 res = tmp;
2609 goto finally;
2610 }
2611 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002612
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002613 if (args == Py_None) {
2614 res = save_none(self, args);
2615 goto finally;
2616 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002617
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002618 type = Py_TYPE(args);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002619
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002620 switch (type->tp_name[0]) {
2621 case 'b':
2622 if (args == Py_False || args == Py_True) {
2623 res = save_bool(self, args);
2624 goto finally;
2625 }
2626 break;
2627 case 'i':
2628 if (type == &PyInt_Type) {
2629 res = save_int(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 'l':
2635 if (type == &PyLong_Type) {
2636 res = save_long(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 'f':
2642 if (type == &PyFloat_Type) {
2643 res = save_float(self, args);
2644 goto finally;
2645 }
2646 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002647
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002648 case 't':
2649 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2650 res = save_tuple(self, args);
2651 goto finally;
2652 }
2653 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002654
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002655 case 's':
2656 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2657 res = save_string(self, args, 0);
2658 goto finally;
2659 }
2660 break;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002661
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002662#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002663 case 'u':
2664 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2665 res = save_unicode(self, args, 0);
2666 goto finally;
2667 }
2668 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002669#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002670 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002671
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002672 if (Py_REFCNT(args) > 1) {
2673 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2674 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002675
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002676 if (PyDict_GetItem(self->memo, py_ob_id)) {
2677 if (get(self, py_ob_id) < 0)
2678 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002679
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002680 res = 0;
2681 goto finally;
2682 }
2683 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002684
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002685 switch (type->tp_name[0]) {
2686 case 's':
2687 if (type == &PyString_Type) {
2688 res = save_string(self, args, 1);
2689 goto finally;
2690 }
2691 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002692
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002693#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002694 case 'u':
2695 if (type == &PyUnicode_Type) {
2696 res = save_unicode(self, args, 1);
2697 goto finally;
2698 }
2699 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002700#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002701
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002702 case 't':
2703 if (type == &PyTuple_Type) {
2704 res = save_tuple(self, args);
2705 goto finally;
2706 }
2707 if (type == &PyType_Type) {
Alexandre Vassalottidf9460f2013-11-30 17:43:42 -08002708 res = save_global(self, args, NULL);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002709 goto finally;
2710 }
2711 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002712
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002713 case 'l':
2714 if (type == &PyList_Type) {
2715 res = save_list(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 'd':
2721 if (type == &PyDict_Type) {
2722 res = save_dict(self, args);
2723 goto finally;
2724 }
2725 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002726
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002727 case 'i':
2728 if (type == &PyInstance_Type) {
2729 res = save_inst(self, args);
2730 goto finally;
2731 }
2732 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002733
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002734 case 'c':
2735 if (type == &PyClass_Type) {
2736 res = save_global(self, args, NULL);
2737 goto finally;
2738 }
2739 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002740
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002741 case 'f':
2742 if (type == &PyFunction_Type) {
2743 res = save_global(self, args, NULL);
2744 if (res && PyErr_ExceptionMatches(PickleError)) {
2745 /* fall back to reduce */
2746 PyErr_Clear();
2747 break;
2748 }
2749 goto finally;
2750 }
2751 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002752
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002753 case 'b':
2754 if (type == &PyCFunction_Type) {
2755 res = save_global(self, args, NULL);
2756 goto finally;
2757 }
2758 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002759
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002760 if (!pers_save && self->inst_pers_func) {
2761 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2762 res = tmp;
2763 goto finally;
2764 }
2765 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002766
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002767 /* Get a reduction callable, and call it. This may come from
2768 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2769 * or the object's __reduce__ method.
2770 */
2771 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2772 if (__reduce__ != NULL) {
2773 Py_INCREF(__reduce__);
2774 Py_INCREF(args);
2775 ARG_TUP(self, args);
2776 if (self->arg) {
2777 t = PyObject_Call(__reduce__, self->arg, NULL);
2778 FREE_ARG_TUP(self);
2779 }
2780 }
2781 else {
Antoine Pitrou561a8212011-10-04 09:34:48 +02002782 if (PyType_IsSubtype(type, &PyType_Type)) {
2783 res = save_global(self, args, NULL);
2784 goto finally;
2785 }
2786
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002787 /* Check for a __reduce_ex__ method. */
2788 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2789 if (__reduce__ != NULL) {
2790 t = PyInt_FromLong(self->proto);
2791 if (t != NULL) {
2792 ARG_TUP(self, t);
2793 t = NULL;
2794 if (self->arg) {
2795 t = PyObject_Call(__reduce__,
2796 self->arg, NULL);
2797 FREE_ARG_TUP(self);
2798 }
2799 }
2800 }
2801 else {
2802 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2803 PyErr_Clear();
2804 else
2805 goto finally;
2806 /* Check for a __reduce__ method. */
2807 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2808 if (__reduce__ != NULL) {
2809 t = PyObject_Call(__reduce__,
2810 empty_tuple, NULL);
2811 }
2812 else {
2813 PyErr_SetObject(UnpickleableError, args);
2814 goto finally;
2815 }
2816 }
2817 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002818
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002819 if (t == NULL)
2820 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002821
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002822 if (PyString_Check(t)) {
2823 res = save_global(self, args, t);
2824 goto finally;
2825 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002826
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002827 if (!PyTuple_Check(t)) {
2828 cPickle_ErrFormat(PicklingError, "Value returned by "
2829 "%s must be string or tuple",
2830 "O", __reduce__);
2831 goto finally;
2832 }
Tim Peters71fcda52003-02-14 23:05:28 +00002833
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002834 res = save_reduce(self, t, __reduce__, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002835
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002836 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002837 Py_LeaveRecursiveCall();
2838 Py_XDECREF(py_ob_id);
2839 Py_XDECREF(__reduce__);
2840 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002841
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002842 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002843}
2844
2845
2846static int
Tim Peterscba30e22003-02-01 06:24:36 +00002847dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002848{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002849 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002850
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002851 if (self->proto >= 2) {
2852 char bytes[2];
Tim Peters4190fb82003-02-02 16:09:05 +00002853
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002854 bytes[0] = PROTO;
2855 assert(self->proto >= 0 && self->proto < 256);
2856 bytes[1] = (char)self->proto;
2857 if (self->write_func(self, bytes, 2) < 0)
2858 return -1;
2859 }
Tim Peters4190fb82003-02-02 16:09:05 +00002860
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002861 if (save(self, args, 0) < 0)
2862 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002863
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002864 if (self->write_func(self, &stop, 1) < 0)
2865 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002866
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002867 if (self->write_func(self, NULL, 0) < 0)
2868 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002869
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002870 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002871}
2872
2873static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002874Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002875{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002876 if (self->memo)
2877 PyDict_Clear(self->memo);
2878 Py_INCREF(Py_None);
2879 return Py_None;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002880}
2881
2882static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002883Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002884{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002885 Py_ssize_t l, i, rsize, ssize, clear=1, lm;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002886 long ik;
2887 PyObject *k, *r;
2888 char *s, *p, *have_get;
2889 Pdata *data;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002890
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002891 /* Can be called by Python code or C code */
2892 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2893 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002894
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002895 /* Check to make sure we are based on a list */
2896 if (! Pdata_Check(self->file)) {
2897 PyErr_SetString(PicklingError,
2898 "Attempt to getvalue() a non-list-based pickler");
2899 return NULL;
2900 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002901
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002902 /* flush write buffer */
2903 if (write_other(self, NULL, 0) < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002904
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002905 data=(Pdata*)self->file;
2906 l=data->length;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002907
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002908 /* set up an array to hold get/put status */
2909 lm = PyDict_Size(self->memo);
2910 if (lm < 0) return NULL;
2911 lm++;
2912 have_get = malloc(lm);
2913 if (have_get == NULL) return PyErr_NoMemory();
2914 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002915
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002916 /* Scan for gets. */
2917 for (rsize = 0, i = l; --i >= 0; ) {
2918 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002919
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002920 if (PyString_Check(k))
2921 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002922
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002923 else if (PyInt_Check(k)) { /* put */
2924 ik = PyInt_AS_LONG((PyIntObject*)k);
2925 if (ik >= lm || ik == 0) {
2926 PyErr_SetString(PicklingError,
2927 "Invalid get data");
2928 goto err;
2929 }
2930 if (have_get[ik]) /* with matching get */
2931 rsize += ik < 256 ? 2 : 5;
2932 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002933
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002934 else if (! (PyTuple_Check(k) &&
2935 PyTuple_GET_SIZE(k) == 2 &&
2936 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2937 ) {
2938 PyErr_SetString(PicklingError,
2939 "Unexpected data in internal list");
2940 goto err;
2941 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002942
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002943 else { /* put */
2944 ik = PyInt_AS_LONG((PyIntObject *)k);
2945 if (ik >= lm || ik == 0) {
2946 PyErr_SetString(PicklingError,
2947 "Invalid get data");
Benjamin Peterson7f18ac42015-07-25 10:20:13 -07002948 goto err;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002949 }
2950 have_get[ik] = 1;
2951 rsize += ik < 256 ? 2 : 5;
2952 }
2953 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002954
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002955 /* Now generate the result */
2956 r = PyString_FromStringAndSize(NULL, rsize);
2957 if (r == NULL) goto err;
2958 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002959
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002960 for (i = 0; i < l; i++) {
2961 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002962
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002963 if (PyString_Check(k)) {
2964 ssize = PyString_GET_SIZE(k);
2965 if (ssize) {
2966 p=PyString_AS_STRING((PyStringObject *)k);
2967 while (--ssize >= 0)
2968 *s++ = *p++;
2969 }
2970 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002971
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002972 else if (PyTuple_Check(k)) { /* get */
2973 ik = PyInt_AS_LONG((PyIntObject *)
2974 PyTuple_GET_ITEM(k, 0));
2975 if (ik < 256) {
2976 *s++ = BINGET;
2977 *s++ = (int)(ik & 0xff);
2978 }
2979 else {
2980 *s++ = LONG_BINGET;
2981 *s++ = (int)(ik & 0xff);
2982 *s++ = (int)((ik >> 8) & 0xff);
2983 *s++ = (int)((ik >> 16) & 0xff);
2984 *s++ = (int)((ik >> 24) & 0xff);
2985 }
2986 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002987
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002988 else { /* put */
2989 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002990
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002991 if (have_get[ik]) { /* with matching get */
2992 if (ik < 256) {
2993 *s++ = BINPUT;
2994 *s++ = (int)(ik & 0xff);
2995 }
2996 else {
2997 *s++ = LONG_BINPUT;
2998 *s++ = (int)(ik & 0xff);
2999 *s++ = (int)((ik >> 8) & 0xff);
3000 *s++ = (int)((ik >> 16) & 0xff);
3001 *s++ = (int)((ik >> 24) & 0xff);
3002 }
3003 }
3004 }
3005 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003006
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003007 if (clear) {
3008 PyDict_Clear(self->memo);
3009 Pdata_clear(data, 0);
3010 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003011
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003012 free(have_get);
3013 return r;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003014 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003015 free(have_get);
3016 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003017}
3018
3019static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003020Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003021{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003022 PyObject *ob;
3023 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003024
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003025 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
3026 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003027
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003028 if (dump(self, ob) < 0)
3029 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003031 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003032
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003033 /* XXX Why does dump() return self? */
3034 Py_INCREF(self);
3035 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003036}
3037
3038
Tim Peterscba30e22003-02-01 06:24:36 +00003039static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003040{
Neal Norwitzb0493252002-03-31 14:44:22 +00003041 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003042 PyDoc_STR("dump(object) -- "
3043 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00003044 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003045 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00003046 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003047 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00003048 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003049};
3050
3051
3052static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00003053newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003054{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003055 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003056
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003057 if (proto < 0)
3058 proto = HIGHEST_PROTOCOL;
3059 if (proto > HIGHEST_PROTOCOL) {
3060 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
3061 "the highest available protocol is %d",
3062 proto, HIGHEST_PROTOCOL);
3063 return NULL;
3064 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003065
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003066 self = PyObject_GC_New(Picklerobject, &Picklertype);
3067 if (self == NULL)
3068 return NULL;
3069 self->proto = proto;
3070 self->bin = proto > 0;
3071 self->fp = NULL;
3072 self->write = NULL;
3073 self->memo = NULL;
3074 self->arg = NULL;
3075 self->pers_func = NULL;
3076 self->inst_pers_func = NULL;
3077 self->write_buf = NULL;
3078 self->fast = 0;
3079 self->fast_container = 0;
3080 self->fast_memo = NULL;
3081 self->buf_size = 0;
3082 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003083
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003084 self->file = NULL;
3085 if (file)
3086 Py_INCREF(file);
3087 else {
3088 file = Pdata_New();
3089 if (file == NULL)
3090 goto err;
3091 }
3092 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003093
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003094 if (!( self->memo = PyDict_New()))
3095 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003096
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003097 if (PyFile_Check(file)) {
3098 self->fp = PyFile_AsFile(file);
3099 if (self->fp == NULL) {
3100 PyErr_SetString(PyExc_ValueError,
3101 "I/O operation on closed file");
3102 goto err;
3103 }
3104 self->write_func = write_file;
3105 }
3106 else if (PycStringIO_OutputCheck(file)) {
3107 self->write_func = write_cStringIO;
3108 }
3109 else if (file == Py_None) {
3110 self->write_func = write_none;
3111 }
3112 else {
3113 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003114
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003115 if (! Pdata_Check(file)) {
3116 self->write = PyObject_GetAttr(file, write_str);
3117 if (!self->write) {
3118 PyErr_Clear();
3119 PyErr_SetString(PyExc_TypeError,
3120 "argument must have 'write' "
3121 "attribute");
3122 goto err;
3123 }
3124 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003125
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003126 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
3127 if (self->write_buf == NULL) {
3128 PyErr_NoMemory();
3129 goto err;
3130 }
3131 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003132
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003133 if (PyEval_GetRestricted()) {
3134 /* Restricted execution, get private tables */
3135 PyObject *m = PyImport_ImportModule("copy_reg");
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003136
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003137 if (m == NULL)
3138 goto err;
3139 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
3140 Py_DECREF(m);
3141 if (self->dispatch_table == NULL)
3142 goto err;
3143 }
3144 else {
3145 self->dispatch_table = dispatch_table;
3146 Py_INCREF(dispatch_table);
3147 }
3148 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003149
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003150 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003151
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003152 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003153 Py_DECREF(self);
3154 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003155}
3156
3157
3158static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00003159get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003160{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003161 static char *kwlist[] = {"file", "protocol", NULL};
3162 PyObject *file = NULL;
3163 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003164
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003165 /* XXX
3166 * The documented signature is Pickler(file, protocol=0), but this
3167 * accepts Pickler() and Pickler(integer) too. The meaning then
3168 * is clear as mud, undocumented, and not supported by pickle.py.
3169 * I'm told Zope uses this, but I haven't traced into this code
3170 * far enough to figure out what it means.
3171 */
3172 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
3173 PyErr_Clear();
3174 proto = 0;
3175 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
3176 kwlist, &file, &proto))
3177 return NULL;
3178 }
3179 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003180}
3181
3182
3183static void
Tim Peterscba30e22003-02-01 06:24:36 +00003184Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003185{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003186 PyObject_GC_UnTrack(self);
3187 Py_XDECREF(self->write);
3188 Py_XDECREF(self->memo);
3189 Py_XDECREF(self->fast_memo);
3190 Py_XDECREF(self->arg);
3191 Py_XDECREF(self->file);
3192 Py_XDECREF(self->pers_func);
3193 Py_XDECREF(self->inst_pers_func);
3194 Py_XDECREF(self->dispatch_table);
3195 PyMem_Free(self->write_buf);
3196 Py_TYPE(self)->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003197}
3198
3199static int
3200Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
3201{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003202 Py_VISIT(self->write);
3203 Py_VISIT(self->memo);
3204 Py_VISIT(self->fast_memo);
3205 Py_VISIT(self->arg);
3206 Py_VISIT(self->file);
3207 Py_VISIT(self->pers_func);
3208 Py_VISIT(self->inst_pers_func);
3209 Py_VISIT(self->dispatch_table);
3210 return 0;
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003211}
3212
3213static int
3214Pickler_clear(Picklerobject *self)
3215{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003216 Py_CLEAR(self->write);
3217 Py_CLEAR(self->memo);
3218 Py_CLEAR(self->fast_memo);
3219 Py_CLEAR(self->arg);
3220 Py_CLEAR(self->file);
3221 Py_CLEAR(self->pers_func);
3222 Py_CLEAR(self->inst_pers_func);
3223 Py_CLEAR(self->dispatch_table);
3224 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003225}
3226
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003227static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003228Pickler_get_pers_func(Picklerobject *p)
3229{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003230 if (p->pers_func == NULL)
3231 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3232 else
3233 Py_INCREF(p->pers_func);
3234 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003235}
3236
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003237static int
3238Pickler_set_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 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003245 Py_INCREF(v);
Serhiy Storchakabc62af12016-04-06 09:51:18 +03003246 Py_XSETREF(p->pers_func, v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003247 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003248}
3249
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003250static int
3251Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3252{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003253 if (v == NULL) {
3254 PyErr_SetString(PyExc_TypeError,
3255 "attribute deletion is not supported");
3256 return -1;
3257 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003258 Py_INCREF(v);
Serhiy Storchakabc62af12016-04-06 09:51:18 +03003259 Py_XSETREF(p->inst_pers_func, v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003260 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003261}
3262
3263static PyObject *
3264Pickler_get_memo(Picklerobject *p)
3265{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003266 if (p->memo == NULL)
3267 PyErr_SetString(PyExc_AttributeError, "memo");
3268 else
3269 Py_INCREF(p->memo);
3270 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003271}
3272
3273static int
3274Pickler_set_memo(Picklerobject *p, PyObject *v)
3275{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003276 if (v == NULL) {
3277 PyErr_SetString(PyExc_TypeError,
3278 "attribute deletion is not supported");
3279 return -1;
3280 }
3281 if (!PyDict_Check(v)) {
3282 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3283 return -1;
3284 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003285 Py_INCREF(v);
Serhiy Storchakabc62af12016-04-06 09:51:18 +03003286 Py_XSETREF(p->memo, v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003287 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003288}
3289
3290static PyObject *
3291Pickler_get_error(Picklerobject *p)
3292{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003293 /* why is this an attribute on the Pickler? */
3294 Py_INCREF(PicklingError);
3295 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003296}
3297
3298static PyMemberDef Pickler_members[] = {
3299 {"binary", T_INT, offsetof(Picklerobject, bin)},
3300 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003301 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003302};
3303
3304static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003305 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003306 (setter)Pickler_set_pers_func},
3307 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3308 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003309 {"PicklingError", (getter)Pickler_get_error, NULL},
3310 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003311};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003312
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003313PyDoc_STRVAR(Picklertype__doc__,
3314"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003315
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003316static PyTypeObject Picklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003317 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00003318 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003319 sizeof(Picklerobject), /*tp_basicsize*/
3320 0,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003321 (destructor)Pickler_dealloc, /* tp_dealloc */
3322 0, /* tp_print */
3323 0, /* tp_getattr */
3324 0, /* tp_setattr */
3325 0, /* tp_compare */
3326 0, /* tp_repr */
3327 0, /* tp_as_number */
3328 0, /* tp_as_sequence */
3329 0, /* tp_as_mapping */
3330 0, /* tp_hash */
3331 0, /* tp_call */
3332 0, /* tp_str */
3333 PyObject_GenericGetAttr, /* tp_getattro */
3334 PyObject_GenericSetAttr, /* tp_setattro */
3335 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003336 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003337 Picklertype__doc__, /* tp_doc */
3338 (traverseproc)Pickler_traverse, /* tp_traverse */
3339 (inquiry)Pickler_clear, /* tp_clear */
3340 0, /* tp_richcompare */
3341 0, /* tp_weaklistoffset */
3342 0, /* tp_iter */
3343 0, /* tp_iternext */
3344 Pickler_methods, /* tp_methods */
3345 Pickler_members, /* tp_members */
3346 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003347};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003348
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003349static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003350find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003351{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003352 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003353
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003354 if (fc) {
3355 if (fc==Py_None) {
3356 PyErr_SetString(UnpicklingError, "Global and instance "
3357 "pickles are not supported.");
3358 return NULL;
3359 }
3360 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3361 py_global_name, NULL);
3362 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003363
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003364 module = PySys_GetObject("modules");
3365 if (module == NULL)
3366 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003367
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003368 module = PyDict_GetItem(module, py_module_name);
3369 if (module == NULL) {
3370 module = PyImport_Import(py_module_name);
3371 if (!module)
3372 return NULL;
3373 global = PyObject_GetAttr(module, py_global_name);
3374 Py_DECREF(module);
3375 }
3376 else
3377 global = PyObject_GetAttr(module, py_global_name);
3378 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003379}
3380
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003381static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +00003382marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003383{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003384 if (self->num_marks < 1) {
3385 PyErr_SetString(UnpicklingError, "could not find MARK");
3386 return -1;
3387 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003388
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003389 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003390}
3391
Tim Peters84e87f32001-03-17 04:50:51 +00003392
Guido van Rossum60456fd1997-04-09 17:36:32 +00003393static int
Tim Peterscba30e22003-02-01 06:24:36 +00003394load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003395{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003396 PDATA_APPEND(self->stack, Py_None, -1);
3397 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003398}
3399
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003400static int
Tim Peterscba30e22003-02-01 06:24:36 +00003401bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003402{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003403 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3404 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003405}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003406
3407static int
Tim Peterscba30e22003-02-01 06:24:36 +00003408load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003409{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003410 PyObject *py_int = 0;
3411 char *endptr, *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003412 Py_ssize_t len;
3413 int res = -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003414 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003415
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003416 if ((len = self->readline_func(self, &s)) < 0) return -1;
3417 if (len < 2) return bad_readline();
3418 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003419
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003420 errno = 0;
3421 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003422
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003423 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3424 /* Hm, maybe we've got something long. Let's try reading
3425 it as a Python long object. */
3426 errno = 0;
3427 py_int = PyLong_FromString(s, NULL, 0);
3428 if (py_int == NULL) {
3429 PyErr_SetString(PyExc_ValueError,
3430 "could not convert string to int");
3431 goto finally;
3432 }
3433 }
3434 else {
3435 if (len == 3 && (l == 0 || l == 1)) {
3436 if (!( py_int = PyBool_FromLong(l))) goto finally;
3437 }
3438 else {
3439 if (!( py_int = PyInt_FromLong(l))) goto finally;
3440 }
3441 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003442
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003443 free(s);
3444 PDATA_PUSH(self->stack, py_int, -1);
3445 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003446
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003447 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003448 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003449
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003450 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003451}
3452
Tim Peters3c67d792003-02-02 17:59:11 +00003453static int
3454load_bool(Unpicklerobject *self, PyObject *boolean)
3455{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003456 assert(boolean == Py_True || boolean == Py_False);
3457 PDATA_APPEND(self->stack, boolean, -1);
3458 return 0;
Tim Peters3c67d792003-02-02 17:59:11 +00003459}
3460
Tim Petersee1a53c2003-02-02 02:57:53 +00003461/* s contains x bytes of a little-endian integer. Return its value as a
3462 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka9a118f12016-04-17 09:37:36 +03003463 * int, but when x is 4 it's a signed one. This is a historical source
Tim Petersee1a53c2003-02-02 02:57:53 +00003464 * of x-platform bugs.
3465 */
Tim Peters84e87f32001-03-17 04:50:51 +00003466static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003467calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003468{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003469 unsigned char c;
3470 int i;
3471 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003472
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003473 for (i = 0, l = 0L; i < x; i++) {
3474 c = (unsigned char)s[i];
3475 l |= (long)c << (i * 8);
3476 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003477#if SIZEOF_LONG > 4
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003478 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3479 * is signed, so on a box with longs bigger than 4 bytes we need
3480 * to extend a BININT's sign bit to the full width.
3481 */
3482 if (x == 4 && l & (1L << 31))
Gregory P. Smith0a857282015-08-04 16:29:00 -07003483 l |= (~0UL) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003484#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003485 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003486}
3487
3488
3489static int
Tim Peterscba30e22003-02-01 06:24:36 +00003490load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003491{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003492 PyObject *py_int = 0;
3493 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003494
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003495 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003496
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003497 if (!( py_int = PyInt_FromLong(l)))
3498 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003499
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003500 PDATA_PUSH(self->stack, py_int, -1);
3501 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003502}
3503
3504
3505static int
Tim Peterscba30e22003-02-01 06:24:36 +00003506load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003507{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003508 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003509
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003510 if (self->read_func(self, &s, 4) < 0)
3511 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003512
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003513 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003514}
3515
3516
3517static int
Tim Peterscba30e22003-02-01 06:24:36 +00003518load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003519{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003520 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003521
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003522 if (self->read_func(self, &s, 1) < 0)
3523 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003524
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003525 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003526}
3527
3528
3529static int
Tim Peterscba30e22003-02-01 06:24:36 +00003530load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003531{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003532 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003533
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003534 if (self->read_func(self, &s, 2) < 0)
3535 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003536
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003537 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003538}
Tim Peters84e87f32001-03-17 04:50:51 +00003539
Guido van Rossum60456fd1997-04-09 17:36:32 +00003540static int
Tim Peterscba30e22003-02-01 06:24:36 +00003541load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003542{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003543 PyObject *l = 0;
3544 char *end, *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003545 Py_ssize_t len;
3546 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003547
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003548 if ((len = self->readline_func(self, &s)) < 0) return -1;
3549 if (len < 2) return bad_readline();
3550 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003551
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003552 if (!( l = PyLong_FromString(s, &end, 0)))
3553 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003554
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003555 free(s);
3556 PDATA_PUSH(self->stack, l, -1);
3557 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003558
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003559 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003560 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003561
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003562 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003563}
3564
Tim Petersee1a53c2003-02-02 02:57:53 +00003565/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3566 * data following.
3567 */
3568static int
3569load_counted_long(Unpicklerobject *self, int size)
3570{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003571 Py_ssize_t i;
3572 char *nbytes;
3573 unsigned char *pdata;
3574 PyObject *along;
Tim Petersee1a53c2003-02-02 02:57:53 +00003575
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003576 assert(size == 1 || size == 4);
3577 i = self->read_func(self, &nbytes, size);
3578 if (i < 0) return -1;
Tim Petersee1a53c2003-02-02 02:57:53 +00003579
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003580 size = calc_binint(nbytes, size);
3581 if (size < 0) {
3582 /* Corrupt or hostile pickle -- we never write one like
3583 * this.
3584 */
3585 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3586 "byte count");
3587 return -1;
3588 }
Tim Petersee1a53c2003-02-02 02:57:53 +00003589
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003590 if (size == 0)
3591 along = PyLong_FromLong(0L);
3592 else {
3593 /* Read the raw little-endian bytes & convert. */
3594 i = self->read_func(self, (char **)&pdata, size);
3595 if (i < 0) return -1;
3596 along = _PyLong_FromByteArray(pdata, (size_t)size,
3597 1 /* little endian */, 1 /* signed */);
3598 }
3599 if (along == NULL)
3600 return -1;
3601 PDATA_PUSH(self->stack, along, -1);
3602 return 0;
Tim Petersee1a53c2003-02-02 02:57:53 +00003603}
Tim Peters84e87f32001-03-17 04:50:51 +00003604
Guido van Rossum60456fd1997-04-09 17:36:32 +00003605static int
Tim Peterscba30e22003-02-01 06:24:36 +00003606load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003607{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003608 PyObject *py_float = 0;
3609 char *endptr, *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003610 Py_ssize_t len;
3611 int res = -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003612 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003613
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003614 if ((len = self->readline_func(self, &s)) < 0) return -1;
3615 if (len < 2) return bad_readline();
3616 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003617
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003618 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003619
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003620 if (d == -1.0 && PyErr_Occurred()) {
3621 goto finally;
3622 } else if ((endptr[0] != '\n') || (endptr[1] != '\0')) {
3623 PyErr_SetString(PyExc_ValueError,
3624 "could not convert string to float");
3625 goto finally;
3626 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003627
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003628 if (!( py_float = PyFloat_FromDouble(d)))
3629 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003630
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003631 free(s);
3632 PDATA_PUSH(self->stack, py_float, -1);
3633 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003634
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003635 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003636 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003637
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003638 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003639}
3640
Guido van Rossum60456fd1997-04-09 17:36:32 +00003641static int
Tim Peterscba30e22003-02-01 06:24:36 +00003642load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003643{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003644 PyObject *py_float;
3645 double x;
3646 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003647
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003648 if (self->read_func(self, &p, 8) < 0)
3649 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003650
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003651 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3652 if (x == -1.0 && PyErr_Occurred())
3653 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003654
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003655 py_float = PyFloat_FromDouble(x);
3656 if (py_float == NULL)
3657 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003658
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003659 PDATA_PUSH(self->stack, py_float, -1);
3660 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003661}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003662
3663static int
Tim Peterscba30e22003-02-01 06:24:36 +00003664load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003665{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003666 PyObject *str = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003667 Py_ssize_t len;
3668 int res = -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003669 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003670
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003671 if ((len = self->readline_func(self, &s)) < 0) return -1;
3672 if (len < 2) return bad_readline();
3673 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003674
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003675
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003676 /* Strip outermost quotes */
Antoine Pitroube929712013-04-15 21:35:25 +02003677 while (len > 0 && s[len-1] <= ' ')
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003678 len--;
Antoine Pitroube929712013-04-15 21:35:25 +02003679 if (len > 1 && s[0]=='"' && s[len-1]=='"') {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003680 s[len-1] = '\0';
3681 p = s + 1 ;
3682 len -= 2;
Antoine Pitroube929712013-04-15 21:35:25 +02003683 }
3684 else if (len > 1 && s[0]=='\'' && s[len-1]=='\'') {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003685 s[len-1] = '\0';
3686 p = s + 1 ;
3687 len -= 2;
Antoine Pitroube929712013-04-15 21:35:25 +02003688 }
3689 else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003690 goto insecure;
3691 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003692
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003693 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3694 free(s);
3695 if (str) {
3696 PDATA_PUSH(self->stack, str, -1);
3697 res = 0;
3698 }
3699 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003700
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003701 insecure:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003702 free(s);
3703 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3704 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003705}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003706
3707
3708static int
Tim Peterscba30e22003-02-01 06:24:36 +00003709load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003710{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003711 PyObject *py_string = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003712 Py_ssize_t l;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003713 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003714
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003715 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003716
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003717 l = calc_binint(s, 4);
3718 if (l < 0) {
3719 /* Corrupt or hostile pickle -- we never write one like
3720 * this.
3721 */
3722 PyErr_SetString(UnpicklingError,
3723 "BINSTRING pickle has negative byte count");
3724 return -1;
3725 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003726
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003727 if (self->read_func(self, &s, l) < 0)
3728 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003729
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003730 if (!( py_string = PyString_FromStringAndSize(s, l)))
3731 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003732
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003733 PDATA_PUSH(self->stack, py_string, -1);
3734 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003735}
3736
3737
3738static int
Tim Peterscba30e22003-02-01 06:24:36 +00003739load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003740{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003741 PyObject *py_string = 0;
3742 unsigned char l;
3743 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003744
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003745 if (self->read_func(self, &s, 1) < 0)
3746 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003747
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003748 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003749
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003750 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003751
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003752 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003753
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003754 PDATA_PUSH(self->stack, py_string, -1);
3755 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003756}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003757
3758
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003759#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003760static int
Tim Peterscba30e22003-02-01 06:24:36 +00003761load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003762{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003763 PyObject *str = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003764 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003765 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003766
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003767 if ((len = self->readline_func(self, &s)) < 0) return -1;
3768 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003769
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003770 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003771 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003772
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003773 PDATA_PUSH(self->stack, str, -1);
3774 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003775}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003776#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003777
3778
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003779#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003780static int
Tim Peterscba30e22003-02-01 06:24:36 +00003781load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003782{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003783 PyObject *unicode;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003784 Py_ssize_t l;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003785 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003786
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003787 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003788
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003789 l = calc_binint(s, 4);
3790 if (l < 0) {
3791 /* Corrupt or hostile pickle -- we never write one like
3792 * this.
3793 */
3794 PyErr_SetString(UnpicklingError,
3795 "BINUNICODE pickle has negative byte count");
3796 return -1;
3797 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003798
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003799 if (self->read_func(self, &s, l) < 0)
3800 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003801
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003802 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3803 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003804
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003805 PDATA_PUSH(self->stack, unicode, -1);
3806 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003807}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003808#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003809
3810
3811static int
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003812load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003813{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003814 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003815
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003816 if (self->stack->length < len)
3817 return stackUnderflow();
3818
3819 if (!(tup = Pdata_popTuple(self->stack, self->stack->length - len)))
3820 return -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003821 PDATA_PUSH(self->stack, tup, -1);
3822 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003823}
3824
3825static int
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003826load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003827{
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003828 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003829
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003830 if ((i = marker(self)) < 0) return -1;
3831 return load_counted_tuple(self, self->stack->length - i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003832}
3833
3834static int
Tim Peterscba30e22003-02-01 06:24:36 +00003835load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003836{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003837 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003838
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003839 if (!( list=PyList_New(0))) return -1;
3840 PDATA_PUSH(self->stack, list, -1);
3841 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003842}
3843
3844static int
Tim Peterscba30e22003-02-01 06:24:36 +00003845load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003846{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003847 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003848
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003849 if (!( dict=PyDict_New())) return -1;
3850 PDATA_PUSH(self->stack, dict, -1);
3851 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003852}
3853
3854
3855static int
Tim Peterscba30e22003-02-01 06:24:36 +00003856load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003857{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003858 PyObject *list = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003859 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003860
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003861 if ((i = marker(self)) < 0) return -1;
3862 if (!( list=Pdata_popList(self->stack, i))) return -1;
3863 PDATA_PUSH(self->stack, list, -1);
3864 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003865}
3866
3867static int
Tim Peterscba30e22003-02-01 06:24:36 +00003868load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003869{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003870 PyObject *dict, *key, *value;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003871 Py_ssize_t i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003872
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003873 if ((i = marker(self)) < 0) return -1;
3874 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003875
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003876 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003877
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003878 for (k = i+1; k < j; k += 2) {
3879 key =self->stack->data[k-1];
3880 value=self->stack->data[k ];
3881 if (PyDict_SetItem(dict, key, value) < 0) {
3882 Py_DECREF(dict);
3883 return -1;
3884 }
3885 }
3886 Pdata_clear(self->stack, i);
3887 PDATA_PUSH(self->stack, dict, -1);
3888 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003889}
3890
3891static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003892Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003893{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003894 if (PyClass_Check(cls)) {
3895 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003896
Serhiy Storchaka6560e222016-12-15 12:51:34 +02003897 if ((l=PyObject_Size(args)) < 0) return NULL;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003898 if (!( l )) {
Serhiy Storchaka6560e222016-12-15 12:51:34 +02003899 if (!PyObject_HasAttr(cls, __getinitargs___str)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003900 /* We have a class with no __getinitargs__,
3901 so bypass usual construction */
Serhiy Storchaka6560e222016-12-15 12:51:34 +02003902 return PyInstance_NewRaw(cls, NULL);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003903 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003904 }
Tim Peters84e87f32001-03-17 04:50:51 +00003905
Serhiy Storchaka6560e222016-12-15 12:51:34 +02003906 return PyInstance_New(cls, args, NULL);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003907 }
Tim Peters84e87f32001-03-17 04:50:51 +00003908
Serhiy Storchaka6560e222016-12-15 12:51:34 +02003909 return PyObject_CallObject(cls, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003910}
Tim Peters84e87f32001-03-17 04:50:51 +00003911
Guido van Rossum60456fd1997-04-09 17:36:32 +00003912
3913static int
Tim Peterscba30e22003-02-01 06:24:36 +00003914load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003915{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003916 PyObject *class, *tup, *obj=0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003917 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003918
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003919 if ((i = marker(self)) < 0) return -1;
Serhiy Storchaka5c137662015-11-23 15:20:43 +02003920
3921 if (self->stack->length - i < 1)
3922 return stackUnderflow();
3923
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003924 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3925 PDATA_POP(self->stack, class);
3926 if (class) {
3927 obj = Instance_New(class, tup);
3928 Py_DECREF(class);
3929 }
3930 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003931
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003932 if (! obj) return -1;
3933 PDATA_PUSH(self->stack, obj, -1);
3934 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003935}
3936
3937
3938static int
Tim Peterscba30e22003-02-01 06:24:36 +00003939load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003940{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003941 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003942 Py_ssize_t i, len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003943 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003944
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003945 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003946
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003947 if ((len = self->readline_func(self, &s)) < 0) return -1;
3948 if (len < 2) return bad_readline();
3949 module_name = PyString_FromStringAndSize(s, len - 1);
3950 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003951
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003952 if ((len = self->readline_func(self, &s)) >= 0) {
Serhiy Storchaka048e1072015-12-01 00:32:49 +02003953 if (len < 2) {
3954 Py_DECREF(module_name);
3955 return bad_readline();
3956 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003957 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3958 class = find_class(module_name, class_name,
3959 self->find_class);
3960 Py_DECREF(class_name);
3961 }
3962 }
3963 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003964
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003965 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003966
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003967 if ((tup=Pdata_popTuple(self->stack, i))) {
3968 obj = Instance_New(class, tup);
3969 Py_DECREF(tup);
3970 }
3971 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003972
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003973 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003974
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003975 PDATA_PUSH(self->stack, obj, -1);
3976 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003977}
3978
Tim Peterseab7db32003-02-13 18:24:14 +00003979static int
3980load_newobj(Unpicklerobject *self)
3981{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003982 PyObject *args = NULL;
3983 PyObject *clsraw = NULL;
3984 PyTypeObject *cls; /* clsraw cast to its true type */
3985 PyObject *obj;
Tim Peterseab7db32003-02-13 18:24:14 +00003986
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003987 /* Stack is ... cls argtuple, and we want to call
3988 * cls.__new__(cls, *argtuple).
3989 */
3990 PDATA_POP(self->stack, args);
3991 if (args == NULL) goto Fail;
3992 if (! PyTuple_Check(args)) {
3993 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3994 "tuple.");
3995 goto Fail;
3996 }
Tim Peterseab7db32003-02-13 18:24:14 +00003997
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003998 PDATA_POP(self->stack, clsraw);
3999 cls = (PyTypeObject *)clsraw;
4000 if (cls == NULL) goto Fail;
4001 if (! PyType_Check(cls)) {
4002 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
4003 "isn't a type object");
4004 goto Fail;
4005 }
4006 if (cls->tp_new == NULL) {
4007 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
4008 "has NULL tp_new");
4009 goto Fail;
4010 }
Tim Peterseab7db32003-02-13 18:24:14 +00004011
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004012 /* Call __new__. */
4013 obj = cls->tp_new(cls, args, NULL);
4014 if (obj == NULL) goto Fail;
Tim Peterseab7db32003-02-13 18:24:14 +00004015
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004016 Py_DECREF(args);
4017 Py_DECREF(clsraw);
4018 PDATA_PUSH(self->stack, obj, -1);
4019 return 0;
Tim Peterseab7db32003-02-13 18:24:14 +00004020
4021 Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004022 Py_XDECREF(args);
4023 Py_XDECREF(clsraw);
4024 return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004025}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004026
4027static int
Tim Peterscba30e22003-02-01 06:24:36 +00004028load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004029{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004030 PyObject *class = 0, *module_name = 0, *class_name = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004031 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004032 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004033
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004034 if ((len = self->readline_func(self, &s)) < 0) return -1;
4035 if (len < 2) return bad_readline();
4036 module_name = PyString_FromStringAndSize(s, len - 1);
4037 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004038
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004039 if ((len = self->readline_func(self, &s)) >= 0) {
4040 if (len < 2) {
4041 Py_DECREF(module_name);
4042 return bad_readline();
4043 }
4044 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
4045 class = find_class(module_name, class_name,
4046 self->find_class);
4047 Py_DECREF(class_name);
4048 }
4049 }
4050 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004051
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004052 if (! class) return -1;
4053 PDATA_PUSH(self->stack, class, -1);
4054 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004055}
4056
4057
4058static int
Tim Peterscba30e22003-02-01 06:24:36 +00004059load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004060{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004061 PyObject *pid = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004062 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004063 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004064
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004065 if (self->pers_func) {
4066 if ((len = self->readline_func(self, &s)) < 0) return -1;
4067 if (len < 2) return bad_readline();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004068
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004069 pid = PyString_FromStringAndSize(s, len - 1);
4070 if (!pid) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004071
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004072 if (PyList_Check(self->pers_func)) {
4073 if (PyList_Append(self->pers_func, pid) < 0) {
4074 Py_DECREF(pid);
4075 return -1;
4076 }
4077 }
4078 else {
4079 ARG_TUP(self, pid);
4080 if (self->arg) {
4081 pid = PyObject_Call(self->pers_func, self->arg,
4082 NULL);
4083 FREE_ARG_TUP(self);
4084 }
4085 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004086
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004087 if (! pid) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004088
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004089 PDATA_PUSH(self->stack, pid, -1);
4090 return 0;
4091 }
4092 else {
4093 PyErr_SetString(UnpicklingError,
4094 "A load persistent id instruction was encountered,\n"
4095 "but no persistent_load function was specified.");
4096 return -1;
4097 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004098}
4099
4100static int
Tim Peterscba30e22003-02-01 06:24:36 +00004101load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004102{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004103 PyObject *pid = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004104
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004105 if (self->pers_func) {
4106 PDATA_POP(self->stack, pid);
4107 if (! pid) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004108
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004109 if (PyList_Check(self->pers_func)) {
4110 if (PyList_Append(self->pers_func, pid) < 0) {
4111 Py_DECREF(pid);
4112 return -1;
4113 }
4114 }
4115 else {
4116 ARG_TUP(self, pid);
4117 if (self->arg) {
4118 pid = PyObject_Call(self->pers_func, self->arg,
4119 NULL);
4120 FREE_ARG_TUP(self);
4121 }
4122 if (! pid) return -1;
4123 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004124
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004125 PDATA_PUSH(self->stack, pid, -1);
4126 return 0;
4127 }
4128 else {
4129 PyErr_SetString(UnpicklingError,
4130 "A load persistent id instruction was encountered,\n"
4131 "but no persistent_load function was specified.");
4132 return -1;
4133 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004134}
4135
4136
4137static int
Tim Peterscba30e22003-02-01 06:24:36 +00004138load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004139{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004140 Py_ssize_t len = self->stack->length;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004141
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004142 /* Note that we split the (pickle.py) stack into two stacks,
4143 an object stack and a mark stack. We have to be clever and
4144 pop the right one. We do this by looking at the top of the
4145 mark stack first, and only signalling a stack underflow if
4146 the object stack is empty and the mark stack doesn't match
4147 our expectations.
4148 */
4149 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
4150 self->num_marks--;
4151 } else if (len > 0) {
4152 len--;
4153 Py_DECREF(self->stack->data[len]);
4154 self->stack->length = len;
4155 } else {
4156 return stackUnderflow();
4157 }
4158 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004159}
4160
4161
4162static int
Tim Peterscba30e22003-02-01 06:24:36 +00004163load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004164{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004165 Py_ssize_t i;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004166
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004167 if ((i = marker(self)) < 0)
4168 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004169
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004170 Pdata_clear(self->stack, i);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004171
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004172 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004173}
4174
4175
4176static int
Tim Peterscba30e22003-02-01 06:24:36 +00004177load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004178{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004179 PyObject *last;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004180 Py_ssize_t len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004181
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004182 if ((len = self->stack->length) <= 0) return stackUnderflow();
4183 last=self->stack->data[len-1];
4184 Py_INCREF(last);
4185 PDATA_PUSH(self->stack, last, -1);
4186 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004187}
4188
4189
4190static int
Tim Peterscba30e22003-02-01 06:24:36 +00004191load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004192{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004193 PyObject *py_str = 0, *value = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004194 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004195 char *s;
4196 int rc;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004197
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004198 if ((len = self->readline_func(self, &s)) < 0) return -1;
4199 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00004200
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004201 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004202
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004203 value = PyDict_GetItem(self->memo, py_str);
4204 if (! value) {
4205 PyErr_SetObject(BadPickleGet, py_str);
4206 rc = -1;
4207 }
4208 else {
4209 PDATA_APPEND(self->stack, value, -1);
4210 rc = 0;
4211 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004212
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004213 Py_DECREF(py_str);
4214 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004215}
4216
4217
4218static int
Tim Peterscba30e22003-02-01 06:24:36 +00004219load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004220{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004221 PyObject *py_key = 0, *value = 0;
4222 unsigned char key;
4223 char *s;
4224 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004225
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004226 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004227
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004228 key = (unsigned char)s[0];
4229 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00004230
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004231 value = PyDict_GetItem(self->memo, py_key);
4232 if (! value) {
4233 PyErr_SetObject(BadPickleGet, py_key);
4234 rc = -1;
4235 }
4236 else {
4237 PDATA_APPEND(self->stack, value, -1);
4238 rc = 0;
4239 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004240
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004241 Py_DECREF(py_key);
4242 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004243}
4244
4245
4246static int
Tim Peterscba30e22003-02-01 06:24:36 +00004247load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004248{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004249 PyObject *py_key = 0, *value = 0;
4250 unsigned char c;
4251 char *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004252 Py_ssize_t key;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004253 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004254
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004255 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004256
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004257 c = (unsigned char)s[0];
4258 key = (long)c;
4259 c = (unsigned char)s[1];
4260 key |= (long)c << 8;
4261 c = (unsigned char)s[2];
4262 key |= (long)c << 16;
4263 c = (unsigned char)s[3];
4264 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004265
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004266 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004267
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004268 value = PyDict_GetItem(self->memo, py_key);
4269 if (! value) {
4270 PyErr_SetObject(BadPickleGet, py_key);
4271 rc = -1;
4272 }
4273 else {
4274 PDATA_APPEND(self->stack, value, -1);
4275 rc = 0;
4276 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004277
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004278 Py_DECREF(py_key);
4279 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004280}
4281
Tim Peters2d629652003-02-04 05:06:17 +00004282/* Push an object from the extension registry (EXT[124]). nbytes is
4283 * the number of bytes following the opcode, holding the index (code) value.
4284 */
4285static int
4286load_extension(Unpicklerobject *self, int nbytes)
4287{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004288 char *codebytes; /* the nbytes bytes after the opcode */
4289 long code; /* calc_binint returns long */
4290 PyObject *py_code; /* code as a Python int */
4291 PyObject *obj; /* the object to push */
4292 PyObject *pair; /* (module_name, class_name) */
4293 PyObject *module_name, *class_name;
Tim Peters2d629652003-02-04 05:06:17 +00004294
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004295 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4296 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4297 code = calc_binint(codebytes, nbytes);
4298 if (code <= 0) { /* note that 0 is forbidden */
4299 /* Corrupt or hostile pickle. */
4300 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4301 return -1;
4302 }
Tim Peters2d629652003-02-04 05:06:17 +00004303
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004304 /* Look for the code in the cache. */
4305 py_code = PyInt_FromLong(code);
4306 if (py_code == NULL) return -1;
4307 obj = PyDict_GetItem(extension_cache, py_code);
4308 if (obj != NULL) {
4309 /* Bingo. */
4310 Py_DECREF(py_code);
4311 PDATA_APPEND(self->stack, obj, -1);
4312 return 0;
4313 }
Tim Peters2d629652003-02-04 05:06:17 +00004314
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004315 /* Look up the (module_name, class_name) pair. */
4316 pair = PyDict_GetItem(inverted_registry, py_code);
4317 if (pair == NULL) {
4318 Py_DECREF(py_code);
4319 PyErr_Format(PyExc_ValueError, "unregistered extension "
4320 "code %ld", code);
4321 return -1;
4322 }
4323 /* Since the extension registry is manipulable via Python code,
4324 * confirm that pair is really a 2-tuple of strings.
4325 */
4326 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4327 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4328 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4329 Py_DECREF(py_code);
4330 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4331 "isn't a 2-tuple of strings", code);
4332 return -1;
4333 }
4334 /* Load the object. */
4335 obj = find_class(module_name, class_name, self->find_class);
4336 if (obj == NULL) {
4337 Py_DECREF(py_code);
4338 return -1;
4339 }
4340 /* Cache code -> obj. */
4341 code = PyDict_SetItem(extension_cache, py_code, obj);
4342 Py_DECREF(py_code);
4343 if (code < 0) {
4344 Py_DECREF(obj);
4345 return -1;
4346 }
4347 PDATA_PUSH(self->stack, obj, -1);
4348 return 0;
Tim Peters2d629652003-02-04 05:06:17 +00004349}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004350
4351static int
Tim Peterscba30e22003-02-01 06:24:36 +00004352load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004353{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004354 PyObject *py_str = 0, *value = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004355 Py_ssize_t len, l;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004356 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004357
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004358 if ((l = self->readline_func(self, &s)) < 0) return -1;
4359 if (l < 2) return bad_readline();
4360 if (!( len=self->stack->length )) return stackUnderflow();
4361 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4362 value=self->stack->data[len-1];
4363 l=PyDict_SetItem(self->memo, py_str, value);
4364 Py_DECREF(py_str);
4365 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004366}
4367
4368
4369static int
Tim Peterscba30e22003-02-01 06:24:36 +00004370load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004371{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004372 PyObject *py_key = 0, *value = 0;
4373 unsigned char key;
4374 char *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004375 Py_ssize_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004376
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004377 if (self->read_func(self, &s, 1) < 0) return -1;
4378 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004379
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004380 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004381
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004382 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4383 value=self->stack->data[len-1];
4384 len=PyDict_SetItem(self->memo, py_key, value);
4385 Py_DECREF(py_key);
4386 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004387}
4388
4389
4390static int
Tim Peterscba30e22003-02-01 06:24:36 +00004391load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004392{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004393 PyObject *py_key = 0, *value = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004394 Py_ssize_t key;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004395 unsigned char c;
4396 char *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004397 Py_ssize_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004398
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004399 if (self->read_func(self, &s, 4) < 0) return -1;
4400 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004401
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004402 c = (unsigned char)s[0];
4403 key = (long)c;
4404 c = (unsigned char)s[1];
4405 key |= (long)c << 8;
4406 c = (unsigned char)s[2];
4407 key |= (long)c << 16;
4408 c = (unsigned char)s[3];
4409 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004410
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004411 if (!( py_key = PyInt_FromLong(key))) return -1;
4412 value=self->stack->data[len-1];
4413 len=PyDict_SetItem(self->memo, py_key, value);
4414 Py_DECREF(py_key);
4415 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004416}
4417
4418
4419static int
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004420do_append(Unpicklerobject *self, Py_ssize_t x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004421{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004422 PyObject *value = 0, *list = 0, *append_method = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004423 Py_ssize_t len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004424
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004425 len=self->stack->length;
4426 if (!( len >= x && x > 0 )) return stackUnderflow();
4427 /* nothing to do */
4428 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004429
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004430 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004431
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004432 if (PyList_Check(list)) {
4433 PyObject *slice;
4434 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004435
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004436 slice=Pdata_popList(self->stack, x);
4437 if (! slice) return -1;
4438 list_len = PyList_GET_SIZE(list);
4439 i=PyList_SetSlice(list, list_len, list_len, slice);
4440 Py_DECREF(slice);
4441 return i;
4442 }
4443 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004444
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004445 if (!( append_method = PyObject_GetAttr(list, append_str)))
4446 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004447
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004448 for (i = x; i < len; i++) {
4449 PyObject *junk;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004450
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004451 value=self->stack->data[i];
4452 junk=0;
4453 ARG_TUP(self, value);
4454 if (self->arg) {
4455 junk = PyObject_Call(append_method, self->arg,
4456 NULL);
4457 FREE_ARG_TUP(self);
4458 }
4459 if (! junk) {
4460 Pdata_clear(self->stack, i+1);
4461 self->stack->length=x;
4462 Py_DECREF(append_method);
4463 return -1;
4464 }
4465 Py_DECREF(junk);
4466 }
4467 self->stack->length=x;
4468 Py_DECREF(append_method);
4469 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004470
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004471 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004472}
4473
4474
4475static int
Tim Peterscba30e22003-02-01 06:24:36 +00004476load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004477{
Serhiy Storchaka5c137662015-11-23 15:20:43 +02004478 if (self->stack->length - 1 <= 0)
4479 return stackUnderflow();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004480 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004481}
4482
4483
4484static int
Tim Peterscba30e22003-02-01 06:24:36 +00004485load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004486{
Serhiy Storchaka5c137662015-11-23 15:20:43 +02004487 Py_ssize_t i = marker(self);
4488 if (i < 0)
4489 return -1;
4490 return do_append(self, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004491}
4492
4493
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004494static Py_ssize_t
4495do_setitems(Unpicklerobject *self, Py_ssize_t x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004496{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004497 PyObject *value = 0, *key = 0, *dict = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004498 Py_ssize_t len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004499
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004500 if (!( (len=self->stack->length) >= x
4501 && x > 0 )) return stackUnderflow();
Serhiy Storchaka5c137662015-11-23 15:20:43 +02004502 if (len == x) /* nothing to do */
4503 return 0;
4504 if ((len - x) % 2 != 0) {
4505 /* Currupt or hostile pickle -- we never write one like this. */
4506 PyErr_SetString(UnpicklingError,
4507 "odd number of items for SETITEMS");
4508 return -1;
4509 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004510
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004511 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004512
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004513 for (i = x+1; i < len; i += 2) {
4514 key =self->stack->data[i-1];
4515 value=self->stack->data[i ];
4516 if (PyObject_SetItem(dict, key, value) < 0) {
4517 r=-1;
4518 break;
4519 }
4520 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004521
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004522 Pdata_clear(self->stack, x);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004523
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004524 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004525}
4526
4527
Tim Peters84e87f32001-03-17 04:50:51 +00004528static int
Tim Peterscba30e22003-02-01 06:24:36 +00004529load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004530{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004531 return do_setitems(self, self->stack->length - 2);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004532}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004533
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004534static int
Tim Peterscba30e22003-02-01 06:24:36 +00004535load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004536{
Serhiy Storchaka5c137662015-11-23 15:20:43 +02004537 Py_ssize_t i = marker(self);
4538 if (i < 0)
4539 return -1;
4540 return do_setitems(self, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004541}
4542
Tim Peters84e87f32001-03-17 04:50:51 +00004543
Guido van Rossum60456fd1997-04-09 17:36:32 +00004544static int
Tim Peterscba30e22003-02-01 06:24:36 +00004545load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004546{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004547 PyObject *state, *inst, *slotstate;
4548 PyObject *__setstate__;
4549 PyObject *d_key, *d_value;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004550 int res = -1;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004551 Py_ssize_t i;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004552
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004553 /* Stack is ... instance, state. We want to leave instance at
4554 * the stack top, possibly mutated via instance.__setstate__(state).
4555 */
4556 if (self->stack->length < 2)
4557 return stackUnderflow();
4558 PDATA_POP(self->stack, state);
4559 if (state == NULL)
4560 return -1;
4561 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004562
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004563 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4564 if (__setstate__ != NULL) {
4565 PyObject *junk = NULL;
Tim Peters080c88b2003-02-15 03:01:11 +00004566
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004567 /* The explicit __setstate__ is responsible for everything. */
4568 ARG_TUP(self, state);
4569 if (self->arg) {
4570 junk = PyObject_Call(__setstate__, self->arg, NULL);
4571 FREE_ARG_TUP(self);
4572 }
4573 Py_DECREF(__setstate__);
4574 if (junk == NULL)
4575 return -1;
4576 Py_DECREF(junk);
4577 return 0;
4578 }
4579 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4580 return -1;
4581 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004582
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004583 /* A default __setstate__. First see whether state embeds a
4584 * slot state dict too (a proto 2 addition).
4585 */
4586 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4587 PyObject *temp = state;
4588 state = PyTuple_GET_ITEM(temp, 0);
4589 slotstate = PyTuple_GET_ITEM(temp, 1);
4590 Py_INCREF(state);
4591 Py_INCREF(slotstate);
4592 Py_DECREF(temp);
4593 }
4594 else
4595 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004596
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004597 /* Set inst.__dict__ from the state dict (if any). */
4598 if (state != Py_None) {
4599 PyObject *dict;
4600 if (! PyDict_Check(state)) {
4601 PyErr_SetString(UnpicklingError, "state is not a "
4602 "dictionary");
4603 goto finally;
4604 }
4605 dict = PyObject_GetAttr(inst, __dict___str);
4606 if (dict == NULL)
4607 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004608
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004609 i = 0;
4610 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4611 /* normally the keys for instance attributes are
4612 interned. we should try to do that here. */
4613 Py_INCREF(d_key);
4614 if (PyString_CheckExact(d_key))
4615 PyString_InternInPlace(&d_key);
4616 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
4617 Py_DECREF(d_key);
4618 goto finally;
4619 }
4620 Py_DECREF(d_key);
4621 }
4622 Py_DECREF(dict);
4623 }
Tim Peters080c88b2003-02-15 03:01:11 +00004624
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004625 /* Also set instance attributes from the slotstate dict (if any). */
4626 if (slotstate != NULL) {
4627 if (! PyDict_Check(slotstate)) {
4628 PyErr_SetString(UnpicklingError, "slot state is not "
4629 "a dictionary");
4630 goto finally;
4631 }
4632 i = 0;
4633 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4634 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4635 goto finally;
4636 }
4637 }
4638 res = 0;
Tim Peters080c88b2003-02-15 03:01:11 +00004639
4640 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004641 Py_DECREF(state);
4642 Py_XDECREF(slotstate);
4643 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004644}
4645
4646
4647static int
Tim Peterscba30e22003-02-01 06:24:36 +00004648load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004649{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004650 Py_ssize_t s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004651
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004652 /* Note that we split the (pickle.py) stack into two stacks, an
4653 object stack and a mark stack. Here we push a mark onto the
4654 mark stack.
4655 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004656
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004657 if ((self->num_marks + 1) >= self->marks_size) {
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004658 Py_ssize_t *marks;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004659 s=self->marks_size+20;
4660 if (s <= self->num_marks) s=self->num_marks + 1;
4661 if (self->marks == NULL)
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004662 marks=(Py_ssize_t *)malloc(s * sizeof(Py_ssize_t));
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004663 else
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004664 marks=(Py_ssize_t *)realloc(self->marks,
4665 s * sizeof(Py_ssize_t));
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004666 if (!marks) {
4667 PyErr_NoMemory();
4668 return -1;
4669 }
4670 self->marks = marks;
4671 self->marks_size = s;
4672 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004673
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004674 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004675
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004676 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004677}
4678
Guido van Rossum60456fd1997-04-09 17:36:32 +00004679static int
Tim Peterscba30e22003-02-01 06:24:36 +00004680load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004681{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004682 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004683
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004684 PDATA_POP(self->stack, arg_tup);
4685 if (! arg_tup) return -1;
4686 PDATA_POP(self->stack, callable);
4687 if (callable) {
4688 ob = Instance_New(callable, arg_tup);
4689 Py_DECREF(callable);
4690 }
4691 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004692
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004693 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004694
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004695 PDATA_PUSH(self->stack, ob, -1);
4696 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004697}
Tim Peters84e87f32001-03-17 04:50:51 +00004698
Tim Peters4190fb82003-02-02 16:09:05 +00004699/* Just raises an error if we don't know the protocol specified. PROTO
4700 * is the first opcode for protocols >= 2.
4701 */
4702static int
4703load_proto(Unpicklerobject *self)
4704{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004705 int i;
4706 char *protobyte;
Tim Peters4190fb82003-02-02 16:09:05 +00004707
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004708 i = self->read_func(self, &protobyte, 1);
4709 if (i < 0)
4710 return -1;
Tim Peters4190fb82003-02-02 16:09:05 +00004711
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004712 i = calc_binint(protobyte, 1);
4713 /* No point checking for < 0, since calc_binint returns an unsigned
4714 * int when chewing on 1 byte.
4715 */
4716 assert(i >= 0);
4717 if (i <= HIGHEST_PROTOCOL)
4718 return 0;
Tim Peters4190fb82003-02-02 16:09:05 +00004719
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004720 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4721 return -1;
Tim Peters4190fb82003-02-02 16:09:05 +00004722}
4723
Guido van Rossum60456fd1997-04-09 17:36:32 +00004724static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004725load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004726{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004727 PyObject *err = 0, *val = 0;
4728 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004729
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004730 self->num_marks = 0;
4731 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004732
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004733 while (1) {
4734 if (self->read_func(self, &s, 1) < 0)
4735 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004736
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004737 switch (s[0]) {
4738 case NONE:
4739 if (load_none(self) < 0)
4740 break;
4741 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004742
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004743 case BININT:
4744 if (load_binint(self) < 0)
4745 break;
4746 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004747
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004748 case BININT1:
4749 if (load_binint1(self) < 0)
4750 break;
4751 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004752
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004753 case BININT2:
4754 if (load_binint2(self) < 0)
4755 break;
4756 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004757
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004758 case INT:
4759 if (load_int(self) < 0)
4760 break;
4761 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004762
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004763 case LONG:
4764 if (load_long(self) < 0)
4765 break;
4766 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004767
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004768 case LONG1:
4769 if (load_counted_long(self, 1) < 0)
4770 break;
4771 continue;
Tim Petersee1a53c2003-02-02 02:57:53 +00004772
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004773 case LONG4:
4774 if (load_counted_long(self, 4) < 0)
4775 break;
4776 continue;
Tim Petersee1a53c2003-02-02 02:57:53 +00004777
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004778 case FLOAT:
4779 if (load_float(self) < 0)
4780 break;
4781 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004782
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004783 case BINFLOAT:
4784 if (load_binfloat(self) < 0)
4785 break;
4786 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004787
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004788 case BINSTRING:
4789 if (load_binstring(self) < 0)
4790 break;
4791 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004792
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004793 case SHORT_BINSTRING:
4794 if (load_short_binstring(self) < 0)
4795 break;
4796 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004797
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004798 case STRING:
4799 if (load_string(self) < 0)
4800 break;
4801 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004802
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004803#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004804 case UNICODE:
4805 if (load_unicode(self) < 0)
4806 break;
4807 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004808
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004809 case BINUNICODE:
4810 if (load_binunicode(self) < 0)
4811 break;
4812 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004813#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004814
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004815 case EMPTY_TUPLE:
4816 if (load_counted_tuple(self, 0) < 0)
4817 break;
4818 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00004819
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004820 case TUPLE1:
4821 if (load_counted_tuple(self, 1) < 0)
4822 break;
4823 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00004824
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004825 case TUPLE2:
4826 if (load_counted_tuple(self, 2) < 0)
4827 break;
4828 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00004829
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004830 case TUPLE3:
4831 if (load_counted_tuple(self, 3) < 0)
4832 break;
4833 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004834
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004835 case TUPLE:
4836 if (load_tuple(self) < 0)
4837 break;
4838 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004839
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004840 case EMPTY_LIST:
4841 if (load_empty_list(self) < 0)
4842 break;
4843 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004844
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004845 case LIST:
4846 if (load_list(self) < 0)
4847 break;
4848 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004849
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004850 case EMPTY_DICT:
4851 if (load_empty_dict(self) < 0)
4852 break;
4853 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004854
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004855 case DICT:
4856 if (load_dict(self) < 0)
4857 break;
4858 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004859
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004860 case OBJ:
4861 if (load_obj(self) < 0)
4862 break;
4863 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004864
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004865 case INST:
4866 if (load_inst(self) < 0)
4867 break;
4868 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004869
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004870 case NEWOBJ:
4871 if (load_newobj(self) < 0)
4872 break;
4873 continue;
Tim Peterseab7db32003-02-13 18:24:14 +00004874
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004875 case GLOBAL:
4876 if (load_global(self) < 0)
4877 break;
4878 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004879
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004880 case APPEND:
4881 if (load_append(self) < 0)
4882 break;
4883 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004884
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004885 case APPENDS:
4886 if (load_appends(self) < 0)
4887 break;
4888 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004889
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004890 case BUILD:
4891 if (load_build(self) < 0)
4892 break;
4893 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004894
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004895 case DUP:
4896 if (load_dup(self) < 0)
4897 break;
4898 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004899
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004900 case BINGET:
4901 if (load_binget(self) < 0)
4902 break;
4903 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004904
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004905 case LONG_BINGET:
4906 if (load_long_binget(self) < 0)
4907 break;
4908 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004909
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004910 case GET:
4911 if (load_get(self) < 0)
4912 break;
4913 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004914
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004915 case EXT1:
4916 if (load_extension(self, 1) < 0)
4917 break;
4918 continue;
Tim Peters2d629652003-02-04 05:06:17 +00004919
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004920 case EXT2:
4921 if (load_extension(self, 2) < 0)
4922 break;
4923 continue;
Tim Peters2d629652003-02-04 05:06:17 +00004924
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004925 case EXT4:
4926 if (load_extension(self, 4) < 0)
4927 break;
4928 continue;
4929 case MARK:
4930 if (load_mark(self) < 0)
4931 break;
4932 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004933
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004934 case BINPUT:
4935 if (load_binput(self) < 0)
4936 break;
4937 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004938
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004939 case LONG_BINPUT:
4940 if (load_long_binput(self) < 0)
4941 break;
4942 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004943
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004944 case PUT:
4945 if (load_put(self) < 0)
4946 break;
4947 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004948
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004949 case POP:
4950 if (load_pop(self) < 0)
4951 break;
4952 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004953
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004954 case POP_MARK:
4955 if (load_pop_mark(self) < 0)
4956 break;
4957 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004958
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004959 case SETITEM:
4960 if (load_setitem(self) < 0)
4961 break;
4962 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004963
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004964 case SETITEMS:
4965 if (load_setitems(self) < 0)
4966 break;
4967 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004968
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004969 case STOP:
4970 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004971
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004972 case PERSID:
4973 if (load_persid(self) < 0)
4974 break;
4975 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004976
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004977 case BINPERSID:
4978 if (load_binpersid(self) < 0)
4979 break;
4980 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004981
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004982 case REDUCE:
4983 if (load_reduce(self) < 0)
4984 break;
4985 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004986
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004987 case PROTO:
4988 if (load_proto(self) < 0)
4989 break;
4990 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00004991
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004992 case NEWTRUE:
4993 if (load_bool(self, Py_True) < 0)
4994 break;
4995 continue;
Tim Peters3c67d792003-02-02 17:59:11 +00004996
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004997 case NEWFALSE:
4998 if (load_bool(self, Py_False) < 0)
4999 break;
5000 continue;
Tim Peters3c67d792003-02-02 17:59:11 +00005001
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005002 case '\0':
5003 /* end of file */
5004 PyErr_SetNone(PyExc_EOFError);
5005 break;
Tim Peterscba30e22003-02-01 06:24:36 +00005006
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005007 default:
5008 cPickle_ErrFormat(UnpicklingError,
5009 "invalid load key, '%s'.",
5010 "c", s[0]);
5011 return NULL;
5012 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005013
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005014 break;
5015 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005016
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005017 if ((err = PyErr_Occurred())) {
5018 if (err == PyExc_EOFError) {
5019 PyErr_SetNone(PyExc_EOFError);
5020 }
5021 return NULL;
5022 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005023
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005024 PDATA_POP(self->stack, val);
5025 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005026}
Tim Peters84e87f32001-03-17 04:50:51 +00005027
Guido van Rossum60456fd1997-04-09 17:36:32 +00005028
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005029/* No-load functions to support noload, which is used to
5030 find persistent references. */
5031
5032static int
Tim Peterscba30e22003-02-01 06:24:36 +00005033noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005034{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005035 Py_ssize_t i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005036
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005037 if ((i = marker(self)) < 0) return -1;
5038 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005039}
5040
5041
5042static int
Tim Peterscba30e22003-02-01 06:24:36 +00005043noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005044{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005045 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005046 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005047
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005048 if ((i = marker(self)) < 0) return -1;
5049 Pdata_clear(self->stack, i);
5050 if (self->readline_func(self, &s) < 0) return -1;
5051 if (self->readline_func(self, &s) < 0) return -1;
5052 PDATA_APPEND(self->stack, Py_None, -1);
5053 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005054}
5055
5056static int
Tim Peterseab7db32003-02-13 18:24:14 +00005057noload_newobj(Unpicklerobject *self)
5058{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005059 PyObject *obj;
Tim Peterseab7db32003-02-13 18:24:14 +00005060
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005061 PDATA_POP(self->stack, obj); /* pop argtuple */
5062 if (obj == NULL) return -1;
5063 Py_DECREF(obj);
Tim Peterseab7db32003-02-13 18:24:14 +00005064
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005065 PDATA_POP(self->stack, obj); /* pop cls */
5066 if (obj == NULL) return -1;
5067 Py_DECREF(obj);
Tim Peterseab7db32003-02-13 18:24:14 +00005068
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005069 PDATA_APPEND(self->stack, Py_None, -1);
5070 return 0;
Tim Peterseab7db32003-02-13 18:24:14 +00005071}
5072
5073static int
Tim Peterscba30e22003-02-01 06:24:36 +00005074noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005075{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005076 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005077
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005078 if (self->readline_func(self, &s) < 0) return -1;
5079 if (self->readline_func(self, &s) < 0) return -1;
5080 PDATA_APPEND(self->stack, Py_None,-1);
5081 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005082}
5083
5084static int
Tim Peterscba30e22003-02-01 06:24:36 +00005085noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005086{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005087
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005088 if (self->stack->length < 2) return stackUnderflow();
5089 Pdata_clear(self->stack, self->stack->length-2);
5090 PDATA_APPEND(self->stack, Py_None,-1);
5091 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005092}
5093
5094static int
5095noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005096
Guido van Rossum053b8df1998-11-25 16:18:00 +00005097 if (self->stack->length < 1) return stackUnderflow();
5098 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00005099 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005100}
5101
Tim Peters2d629652003-02-04 05:06:17 +00005102static int
5103noload_extension(Unpicklerobject *self, int nbytes)
5104{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005105 char *codebytes;
Tim Peters2d629652003-02-04 05:06:17 +00005106
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005107 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
5108 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
5109 PDATA_APPEND(self->stack, Py_None, -1);
5110 return 0;
Tim Peters2d629652003-02-04 05:06:17 +00005111}
5112
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005113static int
5114noload_append(Unpicklerobject *self)
5115{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005116 return Pdata_clear(self->stack, self->stack->length - 1);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005117}
5118
5119static int
5120noload_appends(Unpicklerobject *self)
5121{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005122 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005123 if ((i = marker(self)) < 0) return -1;
5124 return Pdata_clear(self->stack, i);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005125}
5126
5127static int
5128noload_setitem(Unpicklerobject *self)
5129{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005130 return Pdata_clear(self->stack, self->stack->length - 2);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005131}
5132
5133static int
5134noload_setitems(Unpicklerobject *self)
5135{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005136 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005137 if ((i = marker(self)) < 0) return -1;
5138 return Pdata_clear(self->stack, i);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005139}
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005140
5141static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005142noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005143{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005144 PyObject *err = 0, *val = 0;
5145 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005146
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005147 self->num_marks = 0;
5148 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005149
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005150 while (1) {
5151 if (self->read_func(self, &s, 1) < 0)
5152 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005153
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005154 switch (s[0]) {
5155 case NONE:
5156 if (load_none(self) < 0)
5157 break;
5158 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005159
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005160 case BININT:
5161 if (load_binint(self) < 0)
5162 break;
5163 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005164
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005165 case BININT1:
5166 if (load_binint1(self) < 0)
5167 break;
5168 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005169
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005170 case BININT2:
5171 if (load_binint2(self) < 0)
5172 break;
5173 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005174
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005175 case INT:
5176 if (load_int(self) < 0)
5177 break;
5178 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005179
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005180 case LONG:
5181 if (load_long(self) < 0)
5182 break;
5183 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005184
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005185 case LONG1:
5186 if (load_counted_long(self, 1) < 0)
5187 break;
5188 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005189
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005190 case LONG4:
5191 if (load_counted_long(self, 4) < 0)
5192 break;
5193 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005194
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005195 case FLOAT:
5196 if (load_float(self) < 0)
5197 break;
5198 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005199
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005200 case BINFLOAT:
5201 if (load_binfloat(self) < 0)
5202 break;
5203 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005204
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005205 case BINSTRING:
5206 if (load_binstring(self) < 0)
5207 break;
5208 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005209
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005210 case SHORT_BINSTRING:
5211 if (load_short_binstring(self) < 0)
5212 break;
5213 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005214
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005215 case STRING:
5216 if (load_string(self) < 0)
5217 break;
5218 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005219
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005220#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005221 case UNICODE:
5222 if (load_unicode(self) < 0)
5223 break;
5224 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005225
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005226 case BINUNICODE:
5227 if (load_binunicode(self) < 0)
5228 break;
5229 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005230#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005231
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005232 case EMPTY_TUPLE:
5233 if (load_counted_tuple(self, 0) < 0)
5234 break;
5235 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00005236
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005237 case TUPLE1:
5238 if (load_counted_tuple(self, 1) < 0)
5239 break;
5240 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00005241
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005242 case TUPLE2:
5243 if (load_counted_tuple(self, 2) < 0)
5244 break;
5245 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00005246
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005247 case TUPLE3:
5248 if (load_counted_tuple(self, 3) < 0)
5249 break;
5250 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005251
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005252 case TUPLE:
5253 if (load_tuple(self) < 0)
5254 break;
5255 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005256
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005257 case EMPTY_LIST:
5258 if (load_empty_list(self) < 0)
5259 break;
5260 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005261
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005262 case LIST:
5263 if (load_list(self) < 0)
5264 break;
5265 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005266
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005267 case EMPTY_DICT:
5268 if (load_empty_dict(self) < 0)
5269 break;
5270 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005271
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005272 case DICT:
5273 if (load_dict(self) < 0)
5274 break;
5275 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005276
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005277 case OBJ:
5278 if (noload_obj(self) < 0)
5279 break;
5280 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005281
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005282 case INST:
5283 if (noload_inst(self) < 0)
5284 break;
5285 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005286
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005287 case NEWOBJ:
5288 if (noload_newobj(self) < 0)
5289 break;
5290 continue;
Tim Peterseab7db32003-02-13 18:24:14 +00005291
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005292 case GLOBAL:
5293 if (noload_global(self) < 0)
5294 break;
5295 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005296
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005297 case APPEND:
5298 if (noload_append(self) < 0)
5299 break;
5300 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005301
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005302 case APPENDS:
5303 if (noload_appends(self) < 0)
5304 break;
5305 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005306
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005307 case BUILD:
5308 if (noload_build(self) < 0)
5309 break;
5310 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005311
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005312 case DUP:
5313 if (load_dup(self) < 0)
5314 break;
5315 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005316
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005317 case BINGET:
5318 if (load_binget(self) < 0)
5319 break;
5320 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005321
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005322 case LONG_BINGET:
5323 if (load_long_binget(self) < 0)
5324 break;
5325 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005326
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005327 case GET:
5328 if (load_get(self) < 0)
5329 break;
5330 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005331
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005332 case EXT1:
5333 if (noload_extension(self, 1) < 0)
5334 break;
5335 continue;
Tim Peters2d629652003-02-04 05:06:17 +00005336
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005337 case EXT2:
5338 if (noload_extension(self, 2) < 0)
5339 break;
5340 continue;
Tim Peters2d629652003-02-04 05:06:17 +00005341
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005342 case EXT4:
5343 if (noload_extension(self, 4) < 0)
5344 break;
5345 continue;
Tim Peters2d629652003-02-04 05:06:17 +00005346
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005347 case MARK:
5348 if (load_mark(self) < 0)
5349 break;
5350 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005351
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005352 case BINPUT:
5353 if (load_binput(self) < 0)
5354 break;
5355 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005356
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005357 case LONG_BINPUT:
5358 if (load_long_binput(self) < 0)
5359 break;
5360 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005361
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005362 case PUT:
5363 if (load_put(self) < 0)
5364 break;
5365 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005366
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005367 case POP:
5368 if (load_pop(self) < 0)
5369 break;
5370 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005371
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005372 case POP_MARK:
5373 if (load_pop_mark(self) < 0)
5374 break;
5375 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005376
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005377 case SETITEM:
5378 if (noload_setitem(self) < 0)
5379 break;
5380 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005381
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005382 case SETITEMS:
5383 if (noload_setitems(self) < 0)
5384 break;
5385 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005386
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005387 case STOP:
5388 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005389
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005390 case PERSID:
5391 if (load_persid(self) < 0)
5392 break;
5393 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005394
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005395 case BINPERSID:
5396 if (load_binpersid(self) < 0)
5397 break;
5398 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005399
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005400 case REDUCE:
5401 if (noload_reduce(self) < 0)
5402 break;
5403 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005404
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005405 case PROTO:
5406 if (load_proto(self) < 0)
5407 break;
5408 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005409
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005410 case NEWTRUE:
5411 if (load_bool(self, Py_True) < 0)
5412 break;
5413 continue;
Tim Peters3c67d792003-02-02 17:59:11 +00005414
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005415 case NEWFALSE:
5416 if (load_bool(self, Py_False) < 0)
5417 break;
5418 continue;
5419 default:
5420 cPickle_ErrFormat(UnpicklingError,
5421 "invalid load key, '%s'.",
5422 "c", s[0]);
5423 return NULL;
5424 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005425
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005426 break;
5427 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005428
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005429 if ((err = PyErr_Occurred())) {
5430 if (err == PyExc_EOFError) {
5431 PyErr_SetNone(PyExc_EOFError);
5432 }
5433 return NULL;
5434 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005435
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005436 PDATA_POP(self->stack, val);
5437 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005438}
Tim Peters84e87f32001-03-17 04:50:51 +00005439
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005440
Guido van Rossum60456fd1997-04-09 17:36:32 +00005441static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005442Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005443{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005444 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005445}
5446
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005447static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005448Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005449{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005450 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005451}
5452
Guido van Rossum60456fd1997-04-09 17:36:32 +00005453
5454static struct PyMethodDef Unpickler_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +00005455 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005456 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005457 },
Georg Brandl96a8c392006-05-29 21:04:52 +00005458 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005459 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005460 "noload() -- not load a pickle, but go through most of the motions\n"
5461 "\n"
5462 "This function can be used to read past a pickle without instantiating\n"
5463 "any objects or importing any modules. It can also be used to find all\n"
5464 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005465 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005466 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005467 {NULL, NULL} /* sentinel */
5468};
5469
5470
5471static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005472newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005473{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005474 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005475
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005476 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5477 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005478
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005479 self->file = NULL;
5480 self->arg = NULL;
5481 self->stack = (Pdata*)Pdata_New();
5482 self->pers_func = NULL;
5483 self->last_string = NULL;
5484 self->marks = NULL;
5485 self->num_marks = 0;
5486 self->marks_size = 0;
5487 self->buf_size = 0;
5488 self->read = NULL;
5489 self->readline = NULL;
5490 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005491
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005492 if (!( self->memo = PyDict_New()))
5493 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005494
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005495 if (!self->stack)
5496 goto err;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00005497
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005498 Py_INCREF(f);
5499 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005500
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005501 /* Set read, readline based on type of f */
5502 if (PyFile_Check(f)) {
5503 self->fp = PyFile_AsFile(f);
5504 if (self->fp == NULL) {
5505 PyErr_SetString(PyExc_ValueError,
5506 "I/O operation on closed file");
5507 goto err;
5508 }
5509 self->read_func = read_file;
5510 self->readline_func = readline_file;
5511 }
5512 else if (PycStringIO_InputCheck(f)) {
5513 self->fp = NULL;
5514 self->read_func = read_cStringIO;
5515 self->readline_func = readline_cStringIO;
5516 }
5517 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005518
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005519 self->fp = NULL;
5520 self->read_func = read_other;
5521 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005522
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005523 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5524 (self->read = PyObject_GetAttr(f, read_str)))) {
5525 PyErr_Clear();
5526 PyErr_SetString( PyExc_TypeError,
5527 "argument must have 'read' and "
5528 "'readline' attributes" );
5529 goto err;
5530 }
5531 }
5532 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005533
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005534 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005535
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005536 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005537 Py_DECREF((PyObject *)self);
5538 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005539}
5540
5541
5542static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005543get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005544{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005545 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005546}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005547
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005548
Guido van Rossum60456fd1997-04-09 17:36:32 +00005549static void
Tim Peterscba30e22003-02-01 06:24:36 +00005550Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005551{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005552 PyObject_GC_UnTrack((PyObject *)self);
5553 Py_XDECREF(self->readline);
5554 Py_XDECREF(self->read);
5555 Py_XDECREF(self->file);
5556 Py_XDECREF(self->memo);
5557 Py_XDECREF(self->stack);
5558 Py_XDECREF(self->pers_func);
5559 Py_XDECREF(self->arg);
5560 Py_XDECREF(self->last_string);
5561 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005562
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005563 if (self->marks) {
5564 free(self->marks);
5565 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005566
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005567 if (self->buf_size) {
5568 free(self->buf);
5569 }
Tim Peters84e87f32001-03-17 04:50:51 +00005570
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005571 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005572}
5573
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005574static int
5575Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5576{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005577 Py_VISIT(self->readline);
5578 Py_VISIT(self->read);
5579 Py_VISIT(self->file);
5580 Py_VISIT(self->memo);
5581 Py_VISIT(self->stack);
5582 Py_VISIT(self->pers_func);
5583 Py_VISIT(self->arg);
5584 Py_VISIT(self->last_string);
5585 Py_VISIT(self->find_class);
5586 return 0;
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005587}
5588
5589static int
5590Unpickler_clear(Unpicklerobject *self)
5591{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005592 Py_CLEAR(self->readline);
5593 Py_CLEAR(self->read);
5594 Py_CLEAR(self->file);
5595 Py_CLEAR(self->memo);
5596 Py_CLEAR(self->stack);
5597 Py_CLEAR(self->pers_func);
5598 Py_CLEAR(self->arg);
5599 Py_CLEAR(self->last_string);
5600 Py_CLEAR(self->find_class);
5601 return 0;
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005602}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005603
5604static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005605Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005606{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005607 if (!strcmp(name, "persistent_load")) {
5608 if (!self->pers_func) {
5609 PyErr_SetString(PyExc_AttributeError, name);
5610 return NULL;
5611 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005612
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005613 Py_INCREF(self->pers_func);
5614 return self->pers_func;
5615 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005616
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005617 if (!strcmp(name, "find_global")) {
5618 if (!self->find_class) {
5619 PyErr_SetString(PyExc_AttributeError, name);
5620 return NULL;
5621 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005622
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005623 Py_INCREF(self->find_class);
5624 return self->find_class;
5625 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005626
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005627 if (!strcmp(name, "memo")) {
5628 if (!self->memo) {
5629 PyErr_SetString(PyExc_AttributeError, name);
5630 return NULL;
5631 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005632
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005633 Py_INCREF(self->memo);
5634 return self->memo;
5635 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005636
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005637 if (!strcmp(name, "UnpicklingError")) {
5638 Py_INCREF(UnpicklingError);
5639 return UnpicklingError;
5640 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005641
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005642 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005643}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005644
Guido van Rossum60456fd1997-04-09 17:36:32 +00005645
5646static int
Tim Peterscba30e22003-02-01 06:24:36 +00005647Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005648{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005649
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005650 if (!strcmp(name, "persistent_load")) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005651 Py_XINCREF(value);
Serhiy Storchakabc62af12016-04-06 09:51:18 +03005652 Py_XSETREF(self->pers_func, value);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005653 return 0;
5654 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005655
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005656 if (!strcmp(name, "find_global")) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005657 Py_XINCREF(value);
Serhiy Storchakabc62af12016-04-06 09:51:18 +03005658 Py_XSETREF(self->find_class, value);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005659 return 0;
5660 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005661
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005662 if (! value) {
5663 PyErr_SetString(PyExc_TypeError,
5664 "attribute deletion is not supported");
5665 return -1;
5666 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005667
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005668 if (strcmp(name, "memo") == 0) {
5669 if (!PyDict_Check(value)) {
5670 PyErr_SetString(PyExc_TypeError,
5671 "memo must be a dictionary");
5672 return -1;
5673 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005674 Py_INCREF(value);
Serhiy Storchakabc62af12016-04-06 09:51:18 +03005675 Py_XSETREF(self->memo, value);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005676 return 0;
5677 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005678
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005679 PyErr_SetString(PyExc_AttributeError, name);
5680 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005681}
5682
Tim Peters5bd2a792003-02-01 16:45:06 +00005683/* ---------------------------------------------------------------------------
5684 * Module-level functions.
5685 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005686
Martin v. Löwis544f1192004-07-27 05:22:33 +00005687/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005688static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005689cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005690{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005691 static char *kwlist[] = {"obj", "file", "protocol", NULL};
5692 PyObject *ob, *file, *res = NULL;
5693 Picklerobject *pickler = 0;
5694 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005695
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005696 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5697 &ob, &file, &proto)))
5698 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005699
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005700 if (!( pickler = newPicklerobject(file, proto)))
5701 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005702
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005703 if (dump(pickler, ob) < 0)
5704 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005705
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005706 Py_INCREF(Py_None);
5707 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005709 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005710 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005711
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005712 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005713}
5714
5715
Martin v. Löwis544f1192004-07-27 05:22:33 +00005716/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005717static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005718cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005719{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005720 static char *kwlist[] = {"obj", "protocol", NULL};
5721 PyObject *ob, *file = 0, *res = NULL;
5722 Picklerobject *pickler = 0;
5723 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005724
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005725 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5726 &ob, &proto)))
5727 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005728
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005729 if (!( file = PycStringIO->NewOutput(128)))
5730 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005731
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005732 if (!( pickler = newPicklerobject(file, proto)))
5733 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005734
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005735 if (dump(pickler, ob) < 0)
5736 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005737
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005738 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005739
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005740 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005741 Py_XDECREF(pickler);
5742 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005743
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005744 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005745}
5746
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005747
Tim Peters5bd2a792003-02-01 16:45:06 +00005748/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005749static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005750cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005751{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005752 Unpicklerobject *unpickler = 0;
5753 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005754
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005755 if (!( unpickler = newUnpicklerobject(ob)))
5756 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005757
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005758 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005759
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005760 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005761 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005762
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005763 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005764}
5765
5766
Tim Peters5bd2a792003-02-01 16:45:06 +00005767/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005768static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005769cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005770{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005771 PyObject *ob, *file = 0, *res = NULL;
5772 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005773
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005774 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5775 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005776
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005777 if (!( file = PycStringIO->NewInput(ob)))
5778 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005779
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005780 if (!( unpickler = newUnpicklerobject(file)))
5781 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005782
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005783 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005784
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005785 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005786 Py_XDECREF(file);
5787 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005788
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005789 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005790}
5791
5792
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005793PyDoc_STRVAR(Unpicklertype__doc__,
5794"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005795
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005796static PyTypeObject Unpicklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00005797 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005798 "cPickle.Unpickler", /*tp_name*/
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005799 sizeof(Unpicklerobject), /*tp_basicsize*/
5800 0,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005801 (destructor)Unpickler_dealloc, /* tp_dealloc */
5802 0, /* tp_print */
5803 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5804 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5805 0, /* tp_compare */
5806 0, /* tp_repr */
5807 0, /* tp_as_number */
5808 0, /* tp_as_sequence */
5809 0, /* tp_as_mapping */
5810 0, /* tp_hash */
5811 0, /* tp_call */
5812 0, /* tp_str */
5813 0, /* tp_getattro */
5814 0, /* tp_setattro */
5815 0, /* tp_as_buffer */
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005816 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005817 Unpicklertype__doc__, /* tp_doc */
5818 (traverseproc)Unpickler_traverse, /* tp_traverse */
5819 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005820};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005821
Guido van Rossum60456fd1997-04-09 17:36:32 +00005822static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005823 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5824 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005825 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005826 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005827 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005828 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005829
Martin v. Löwis544f1192004-07-27 05:22:33 +00005830 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5831 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005832 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005833 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005834 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005835 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005836
Georg Brandl96a8c392006-05-29 21:04:52 +00005837 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005838 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005839
Neal Norwitzb0493252002-03-31 14:44:22 +00005840 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005841 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005842
Martin v. Löwis544f1192004-07-27 05:22:33 +00005843 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5844 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005845 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005846 "This takes a file-like object for writing a pickle data stream.\n"
5847 "The optional proto argument tells the pickler to use the given\n"
5848 "protocol; supported protocols are 0, 1, 2. The default\n"
5849 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5850 "only protocol that can be written to a file opened in text\n"
5851 "mode and read back successfully. When using a protocol higher\n"
5852 "than 0, make sure the file is opened in binary mode, both when\n"
5853 "pickling and unpickling.)\n"
5854 "\n"
5855 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5856 "more efficient than protocol 1.\n"
5857 "\n"
5858 "Specifying a negative protocol version selects the highest\n"
5859 "protocol version supported. The higher the protocol used, the\n"
5860 "more recent the version of Python needed to read the pickle\n"
5861 "produced.\n"
5862 "\n"
5863 "The file parameter must have a write() method that accepts a single\n"
5864 "string argument. It can thus be an open file object, a StringIO\n"
5865 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005866 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005867
Georg Brandl96a8c392006-05-29 21:04:52 +00005868 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005869 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5870
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005871 { NULL, NULL }
5872};
5873
Guido van Rossum60456fd1997-04-09 17:36:32 +00005874static int
Tim Peterscba30e22003-02-01 06:24:36 +00005875init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005876{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005877 PyObject *copyreg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005878
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005879#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005880
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005881 if (PyType_Ready(&Unpicklertype) < 0)
5882 return -1;
5883 if (PyType_Ready(&Picklertype) < 0)
5884 return -1;
Tim Peters3cfe7542003-05-21 21:29:48 +00005885
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005886 INIT_STR(__class__);
5887 INIT_STR(__getinitargs__);
5888 INIT_STR(__dict__);
5889 INIT_STR(__getstate__);
5890 INIT_STR(__setstate__);
5891 INIT_STR(__name__);
5892 INIT_STR(__main__);
5893 INIT_STR(__reduce__);
5894 INIT_STR(__reduce_ex__);
5895 INIT_STR(write);
5896 INIT_STR(append);
5897 INIT_STR(read);
5898 INIT_STR(readline);
5899 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005900
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005901 if (!( copyreg = PyImport_ImportModule("copy_reg")))
5902 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005903
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005904 /* This is special because we want to use a different
5905 one in restricted mode. */
5906 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
5907 if (!dispatch_table) return -1;
Tim Peters5b7da392003-02-04 00:21:07 +00005908
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005909 extension_registry = PyObject_GetAttrString(copyreg,
5910 "_extension_registry");
5911 if (!extension_registry) return -1;
Tim Peters5b7da392003-02-04 00:21:07 +00005912
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005913 inverted_registry = PyObject_GetAttrString(copyreg,
5914 "_inverted_registry");
5915 if (!inverted_registry) return -1;
Tim Peters5b7da392003-02-04 00:21:07 +00005916
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005917 extension_cache = PyObject_GetAttrString(copyreg,
5918 "_extension_cache");
5919 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005920
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005921 Py_DECREF(copyreg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005922
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005923 if (!(empty_tuple = PyTuple_New(0)))
5924 return -1;
Tim Peters731098b2003-02-04 20:56:09 +00005925
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005926 two_tuple = PyTuple_New(2);
5927 if (two_tuple == NULL)
5928 return -1;
5929 /* We use this temp container with no regard to refcounts, or to
5930 * keeping containees alive. Exempt from GC, because we don't
5931 * want anything looking at two_tuple() by magic.
5932 */
5933 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005934
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005935 /* Ugh */
5936 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5937 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5938 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005939
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005940 if (!( t=PyDict_New())) return -1;
5941 if (!( r=PyRun_String(
5942 "def __str__(self):\n"
5943 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5944 Py_file_input,
5945 module_dict, t) )) return -1;
5946 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005947
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005948 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5949 if (!PickleError)
5950 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005951
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005952 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005953
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005954 PicklingError = PyErr_NewException("cPickle.PicklingError",
5955 PickleError, NULL);
5956 if (!PicklingError)
5957 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005958
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005959 if (!( t=PyDict_New())) return -1;
5960 if (!( r=PyRun_String(
5961 "def __str__(self):\n"
5962 " a=self.args\n"
5963 " a=a and type(a[0]) or '(what)'\n"
5964 " return 'Cannot pickle %s objects' % a\n"
5965 , Py_file_input,
5966 module_dict, t) )) return -1;
5967 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005968
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005969 if (!( UnpickleableError = PyErr_NewException(
5970 "cPickle.UnpickleableError", PicklingError, t)))
5971 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005972
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005973 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005974
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005975 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5976 PickleError, NULL)))
5977 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005978
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005979 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5980 UnpicklingError, NULL)))
5981 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005982
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005983 if (PyDict_SetItemString(module_dict, "PickleError",
5984 PickleError) < 0)
5985 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005986
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005987 if (PyDict_SetItemString(module_dict, "PicklingError",
5988 PicklingError) < 0)
5989 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005990
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005991 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5992 UnpicklingError) < 0)
5993 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005994
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005995 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5996 UnpickleableError) < 0)
5997 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005998
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005999 if (PyDict_SetItemString(module_dict, "BadPickleGet",
6000 BadPickleGet) < 0)
6001 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00006002
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006003 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00006004
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006005 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006006}
6007
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006008#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
Mark Hammondfe51c6d2002-08-02 02:27:13 +00006009#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00006010#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00006011PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00006012initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00006013{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006014 PyObject *m, *d, *di, *v, *k;
6015 Py_ssize_t i;
6016 char *rev = "1.71"; /* XXX when does this change? */
6017 PyObject *format_version;
6018 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006019
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006020 Py_TYPE(&Picklertype) = &PyType_Type;
6021 Py_TYPE(&Unpicklertype) = &PyType_Type;
6022 Py_TYPE(&PdataType) = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006023
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006024 /* Initialize some pieces. We need to do this before module creation,
6025 * so we're forced to use a temporary dictionary. :(
6026 */
6027 di = PyDict_New();
6028 if (!di) return;
6029 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00006030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006031 /* Create the module and add the functions */
6032 m = Py_InitModule4("cPickle", cPickle_methods,
6033 cPickle_module_documentation,
6034 (PyObject*)NULL,PYTHON_API_VERSION);
6035 if (m == NULL)
6036 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006037
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006038 /* Add some symbolic constants to the module */
6039 d = PyModule_GetDict(m);
6040 v = PyString_FromString(rev);
6041 PyDict_SetItemString(d, "__version__", v);
6042 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00006043
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006044 /* Copy data from di. Waaa. */
6045 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
6046 if (PyObject_SetItem(d, k, v) < 0) {
6047 Py_DECREF(di);
6048 return;
6049 }
6050 }
6051 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00006052
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006053 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
6054 if (i < 0)
6055 return;
Tim Peters8587b3c2003-02-13 15:44:41 +00006056
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006057 /* These are purely informational; no code uses them. */
6058 /* File format version we write. */
6059 format_version = PyString_FromString("2.0");
6060 /* Format versions we can read. */
6061 compatible_formats = Py_BuildValue("[sssss]",
6062 "1.0", /* Original protocol 0 */
6063 "1.1", /* Protocol 0 + INST */
6064 "1.2", /* Original protocol 1 */
6065 "1.3", /* Protocol 1 + BINFLOAT */
6066 "2.0"); /* Original protocol 2 */
6067 PyDict_SetItemString(d, "format_version", format_version);
6068 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
6069 Py_XDECREF(format_version);
6070 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006071}