blob: a00108e1048a9a17a5694bc2cc505bb81b871c7b [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 Storchaka5951f232015-12-24 10:35:35 +0200692 Py_SETREF(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 Storchaka5951f232015-12-24 10:35:35 +0200718 Py_SETREF(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 }
781 else {
782 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 }
789 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000790
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000791 if (self->write_func(self, s, len) < 0)
792 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000793
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000794 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000795}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000796
Guido van Rossum60456fd1997-04-09 17:36:32 +0000797
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000798static int
Tim Peterscba30e22003-02-01 06:24:36 +0000799put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000800{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000801 if (Py_REFCNT(ob) < 2 || self->fast)
802 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000803
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000804 return put2(self, ob);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000805}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000806
Guido van Rossum053b8df1998-11-25 16:18:00 +0000807
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000808static int
Tim Peterscba30e22003-02-01 06:24:36 +0000809put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000810{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000811 char c_str[30];
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200812 Py_ssize_t len, p;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000813 int res = -1;
814 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000815
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000816 if (self->fast)
817 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000818
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000819 if ((p = PyDict_Size(self->memo)) < 0)
820 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000821
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000822 /* Make sure memo keys are positive! */
823 /* XXX Why?
824 * XXX And does "positive" really mean non-negative?
825 * XXX pickle.py starts with PUT index 0, not 1. This makes for
826 * XXX gratuitous differences between the pickling modules.
827 */
828 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000829
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000830 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
831 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000832
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000833 if (!( memo_len = PyInt_FromLong(p)))
834 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000835
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000836 if (!( t = PyTuple_New(2)))
837 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000838
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000839 PyTuple_SET_ITEM(t, 0, memo_len);
840 Py_INCREF(memo_len);
841 PyTuple_SET_ITEM(t, 1, ob);
842 Py_INCREF(ob);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000843
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000844 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
845 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000846
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000847 if (!self->bin) {
848 c_str[0] = PUT;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200849 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1,
850 "%" PY_FORMAT_SIZE_T "d\n", p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000851 len = strlen(c_str);
852 }
853 else if (Pdata_Check(self->file)) {
854 if (write_other(self, NULL, 0) < 0) return -1;
855 PDATA_APPEND(self->file, memo_len, -1);
856 res=0; /* Job well done ;) */
857 goto finally;
858 }
859 else {
860 if (p >= 256) {
861 c_str[0] = LONG_BINPUT;
862 c_str[1] = (int)(p & 0xff);
863 c_str[2] = (int)((p >> 8) & 0xff);
864 c_str[3] = (int)((p >> 16) & 0xff);
865 c_str[4] = (int)((p >> 24) & 0xff);
866 len = 5;
867 }
868 else {
869 c_str[0] = BINPUT;
870 c_str[1] = p;
871 len = 2;
872 }
873 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000874
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000875 if (self->write_func(self, c_str, len) < 0)
876 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000877
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000878 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000879
880 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000881 Py_XDECREF(py_ob_id);
882 Py_XDECREF(memo_len);
883 Py_XDECREF(t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000884
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000885 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000886}
887
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000888static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000889whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000890{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000891 Py_ssize_t i, j;
892 PyObject *module = 0, *modules_dict = 0,
893 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000894
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000895 module = PyObject_GetAttrString(global, "__module__");
896 if (module)
897 return module;
898 if (PyErr_ExceptionMatches(PyExc_AttributeError))
899 PyErr_Clear();
900 else
901 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000902
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000903 if (!( modules_dict = PySys_GetObject("modules")))
904 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000905
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000906 i = 0;
907 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000908
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000909 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000910
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000911 global_name_attr = PyObject_GetAttr(module, global_name);
912 if (!global_name_attr) {
913 if (PyErr_ExceptionMatches(PyExc_AttributeError))
914 PyErr_Clear();
915 else
916 return NULL;
917 continue;
918 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000919
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000920 if (global_name_attr != global) {
921 Py_DECREF(global_name_attr);
922 continue;
923 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000924
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000925 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000926
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000927 break;
928 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000929
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000930 /* The following implements the rule in pickle.py added in 1.5
931 that used __main__ if no module is found. I don't actually
932 like this rule. jlf
933 */
934 if (!j) {
935 name=__main___str;
936 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000937
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000938 Py_INCREF(name);
939 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000940}
941
942
Guido van Rossum60456fd1997-04-09 17:36:32 +0000943static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000944fast_save_enter(Picklerobject *self, PyObject *obj)
945{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000946 /* if fast_container < 0, we're doing an error exit. */
947 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
948 PyObject *key = NULL;
949 if (self->fast_memo == NULL) {
950 self->fast_memo = PyDict_New();
951 if (self->fast_memo == NULL) {
952 self->fast_container = -1;
953 return 0;
954 }
955 }
956 key = PyLong_FromVoidPtr(obj);
957 if (key == NULL)
958 return 0;
959 if (PyDict_GetItem(self->fast_memo, key)) {
960 Py_DECREF(key);
961 PyErr_Format(PyExc_ValueError,
962 "fast mode: can't pickle cyclic objects "
963 "including object type %s at %p",
964 Py_TYPE(obj)->tp_name, obj);
965 self->fast_container = -1;
966 return 0;
967 }
968 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
969 Py_DECREF(key);
970 self->fast_container = -1;
971 return 0;
972 }
973 Py_DECREF(key);
974 }
975 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000976}
977
Tim Peterscba30e22003-02-01 06:24:36 +0000978int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000979fast_save_leave(Picklerobject *self, PyObject *obj)
980{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000981 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
982 PyObject *key = PyLong_FromVoidPtr(obj);
983 if (key == NULL)
984 return 0;
985 if (PyDict_DelItem(self->fast_memo, key) < 0) {
986 Py_DECREF(key);
987 return 0;
988 }
989 Py_DECREF(key);
990 }
991 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000992}
993
994static int
Tim Peterscba30e22003-02-01 06:24:36 +0000995save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000996{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000997 static char none = NONE;
998 if (self->write_func(self, &none, 1) < 0)
999 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001000
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001001 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001002}
1003
Guido van Rossum77f6a652002-04-03 22:41:51 +00001004static int
Tim Peterscba30e22003-02-01 06:24:36 +00001005save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +00001006{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001007 static const char *buf[2] = {FALSE, TRUE};
1008 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
1009 long l = PyInt_AS_LONG((PyIntObject *)args);
Guido van Rossum77f6a652002-04-03 22:41:51 +00001010
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001011 if (self->proto >= 2) {
1012 char opcode = l ? NEWTRUE : NEWFALSE;
1013 if (self->write_func(self, &opcode, 1) < 0)
1014 return -1;
1015 }
1016 else if (self->write_func(self, buf[l], len[l]) < 0)
1017 return -1;
1018 return 0;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001019}
Tim Peters84e87f32001-03-17 04:50:51 +00001020
Guido van Rossum60456fd1997-04-09 17:36:32 +00001021static int
Tim Peterscba30e22003-02-01 06:24:36 +00001022save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001023{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001024 char c_str[32];
1025 long l = PyInt_AS_LONG((PyIntObject *)args);
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001026 Py_ssize_t len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001027
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001028 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001029#if SIZEOF_LONG > 4
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001030 || l > 0x7fffffffL
1031 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001032#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001033 ) {
1034 /* Text-mode pickle, or long too big to fit in the 4-byte
1035 * signed BININT format: store as a string.
1036 */
1037 c_str[0] = INT;
1038 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
1039 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1040 return -1;
1041 }
1042 else {
1043 /* Binary pickle and l fits in a signed 4-byte int. */
1044 c_str[1] = (int)( l & 0xff);
1045 c_str[2] = (int)((l >> 8) & 0xff);
1046 c_str[3] = (int)((l >> 16) & 0xff);
1047 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001048
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001049 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1050 if (c_str[2] == 0) {
1051 c_str[0] = BININT1;
1052 len = 2;
1053 }
1054 else {
1055 c_str[0] = BININT2;
1056 len = 3;
1057 }
1058 }
1059 else {
1060 c_str[0] = BININT;
1061 len = 5;
1062 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001063
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001064 if (self->write_func(self, c_str, len) < 0)
1065 return -1;
1066 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001067
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001068 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001069}
1070
1071
1072static int
Tim Peterscba30e22003-02-01 06:24:36 +00001073save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001074{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001075 Py_ssize_t size;
1076 int res = -1;
1077 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001078
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001079 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001080
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001081 if (self->proto >= 2) {
1082 /* Linear-time pickling. */
1083 size_t nbits;
1084 size_t nbytes;
1085 unsigned char *pdata;
1086 char c_str[5];
1087 int i;
1088 int sign = _PyLong_Sign(args);
Tim Petersee1a53c2003-02-02 02:57:53 +00001089
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001090 if (sign == 0) {
1091 /* It's 0 -- an empty bytestring. */
1092 c_str[0] = LONG1;
1093 c_str[1] = 0;
1094 i = self->write_func(self, c_str, 2);
1095 if (i < 0) goto finally;
1096 res = 0;
1097 goto finally;
1098 }
1099 nbits = _PyLong_NumBits(args);
1100 if (nbits == (size_t)-1 && PyErr_Occurred())
1101 goto finally;
1102 /* How many bytes do we need? There are nbits >> 3 full
1103 * bytes of data, and nbits & 7 leftover bits. If there
1104 * are any leftover bits, then we clearly need another
1105 * byte. Wnat's not so obvious is that we *probably*
1106 * need another byte even if there aren't any leftovers:
1107 * the most-significant bit of the most-significant byte
1108 * acts like a sign bit, and it's usually got a sense
1109 * opposite of the one we need. The exception is longs
1110 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1111 * its own 256's-complement, so has the right sign bit
1112 * even without the extra byte. That's a pain to check
1113 * for in advance, though, so we always grab an extra
1114 * byte at the start, and cut it back later if possible.
1115 */
1116 nbytes = (nbits >> 3) + 1;
1117 if (nbytes > INT_MAX) {
1118 PyErr_SetString(PyExc_OverflowError, "long too large "
1119 "to pickle");
1120 goto finally;
1121 }
1122 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1123 if (repr == NULL) goto finally;
1124 pdata = (unsigned char *)PyString_AS_STRING(repr);
1125 i = _PyLong_AsByteArray((PyLongObject *)args,
1126 pdata, nbytes,
1127 1 /* little endian */, 1 /* signed */);
1128 if (i < 0) goto finally;
1129 /* If the long is negative, this may be a byte more than
1130 * needed. This is so iff the MSB is all redundant sign
1131 * bits.
1132 */
1133 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1134 (pdata[nbytes - 2] & 0x80) != 0)
1135 --nbytes;
Tim Petersee1a53c2003-02-02 02:57:53 +00001136
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001137 if (nbytes < 256) {
1138 c_str[0] = LONG1;
1139 c_str[1] = (char)nbytes;
1140 size = 2;
1141 }
1142 else {
1143 c_str[0] = LONG4;
1144 size = (int)nbytes;
1145 for (i = 1; i < 5; i++) {
1146 c_str[i] = (char)(size & 0xff);
1147 size >>= 8;
1148 }
1149 size = 5;
1150 }
1151 i = self->write_func(self, c_str, size);
1152 if (i < 0) goto finally;
1153 i = self->write_func(self, (char *)pdata, (int)nbytes);
1154 if (i < 0) goto finally;
1155 res = 0;
1156 goto finally;
1157 }
Tim Petersee1a53c2003-02-02 02:57:53 +00001158
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001159 /* proto < 2: write the repr and newline. This is quadratic-time
1160 * (in the number of digits), in both directions.
1161 */
1162 if (!( repr = PyObject_Repr(args)))
1163 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001164
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001165 if ((size = PyString_Size(repr)) < 0)
1166 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001167
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001168 if (self->write_func(self, &l, 1) < 0)
1169 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001170
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001171 if (self->write_func(self,
1172 PyString_AS_STRING((PyStringObject *)repr),
1173 size) < 0)
1174 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001175
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001176 if (self->write_func(self, "\n", 1) < 0)
1177 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001178
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001179 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001180
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001181 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001182 Py_XDECREF(repr);
1183 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001184}
1185
1186
1187static int
Tim Peterscba30e22003-02-01 06:24:36 +00001188save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001189{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001190 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001191
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001192 if (self->bin) {
1193 char str[9];
1194 str[0] = BINFLOAT;
1195 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
1196 return -1;
1197 if (self->write_func(self, str, 9) < 0)
1198 return -1;
1199 }
1200 else {
1201 int result = -1;
1202 char *buf = NULL;
1203 char op = FLOAT;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001204
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001205 if (self->write_func(self, &op, 1) < 0)
1206 goto done;
Eric Smithb05d3be2009-10-26 15:06:39 +00001207
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001208 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
1209 if (!buf) {
1210 PyErr_NoMemory();
1211 goto done;
1212 }
Eric Smithb05d3be2009-10-26 15:06:39 +00001213
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001214 if (self->write_func(self, buf, strlen(buf)) < 0)
1215 goto done;
Eric Smithb05d3be2009-10-26 15:06:39 +00001216
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001217 if (self->write_func(self, "\n", 1) < 0)
1218 goto done;
Eric Smithb05d3be2009-10-26 15:06:39 +00001219
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001220 result = 0;
Eric Smithb05d3be2009-10-26 15:06:39 +00001221done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001222 PyMem_Free(buf);
1223 return result;
1224 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001225
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001226 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001227}
1228
1229
1230static int
Tim Peterscba30e22003-02-01 06:24:36 +00001231save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001232{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001233 Py_ssize_t size, len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001234 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001235
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001236 if ((size = PyString_Size(args)) < 0)
1237 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001238
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001239 if (!self->bin) {
1240 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001241
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001242 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001243
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001244 if (!( repr = PyObject_Repr(args)))
1245 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001246
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001247 if ((len = PyString_Size(repr)) < 0)
1248 goto err;
1249 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001250
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001251 if (self->write_func(self, &string, 1) < 0)
1252 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001253
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001254 if (self->write_func(self, repr_str, len) < 0)
1255 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001256
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001257 if (self->write_func(self, "\n", 1) < 0)
1258 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001259
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001260 Py_XDECREF(repr);
1261 }
1262 else {
1263 int i;
1264 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001265
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001266 if (size < 256) {
1267 c_str[0] = SHORT_BINSTRING;
1268 c_str[1] = size;
1269 len = 2;
1270 }
1271 else if (size <= INT_MAX) {
1272 c_str[0] = BINSTRING;
1273 for (i = 1; i < 5; i++)
1274 c_str[i] = (int)(size >> ((i - 1) * 8));
1275 len = 5;
1276 }
1277 else
1278 return -1; /* string too large */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001279
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001280 if (self->write_func(self, c_str, len) < 0)
1281 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001282
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001283 if (size > 128 && Pdata_Check(self->file)) {
1284 if (write_other(self, NULL, 0) < 0) return -1;
1285 PDATA_APPEND(self->file, args, -1);
1286 }
1287 else {
1288 if (self->write_func(self,
1289 PyString_AS_STRING(
1290 (PyStringObject *)args),
1291 size) < 0)
1292 return -1;
1293 }
1294 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001295
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001296 if (doput)
1297 if (put(self, args) < 0)
1298 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001299
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001300 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001302 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001303 Py_XDECREF(repr);
1304 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001305}
1306
1307
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001308#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001309/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1310 backslash and newline characters to \uXXXX escapes. */
1311static PyObject *
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001312modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001313{
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001314 PyObject *repr;
1315 char *p;
1316 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001317
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001318 static const char *hexdigit = "0123456789abcdef";
1319#ifdef Py_UNICODE_WIDE
1320 const Py_ssize_t expandsize = 10;
1321#else
1322 const Py_ssize_t expandsize = 6;
1323#endif
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001324
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001325 if (size > PY_SSIZE_T_MAX / expandsize)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001326 return PyErr_NoMemory();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001327
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001328 repr = PyString_FromStringAndSize(NULL, expandsize * size);
1329 if (repr == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001330 return NULL;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001331 if (size == 0)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001332 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001333
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001334 p = q = PyString_AS_STRING(repr);
1335 while (size-- > 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001336 Py_UNICODE ch = *s++;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001337#ifdef Py_UNICODE_WIDE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001338 /* Map 32-bit characters to '\Uxxxxxxxx' */
1339 if (ch >= 0x10000) {
1340 *p++ = '\\';
1341 *p++ = 'U';
1342 *p++ = hexdigit[(ch >> 28) & 0xf];
1343 *p++ = hexdigit[(ch >> 24) & 0xf];
1344 *p++ = hexdigit[(ch >> 20) & 0xf];
1345 *p++ = hexdigit[(ch >> 16) & 0xf];
1346 *p++ = hexdigit[(ch >> 12) & 0xf];
1347 *p++ = hexdigit[(ch >> 8) & 0xf];
1348 *p++ = hexdigit[(ch >> 4) & 0xf];
1349 *p++ = hexdigit[ch & 15];
1350 }
1351 else
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001352#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001353 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1354 if (ch >= 0xD800 && ch < 0xDC00) {
1355 Py_UNICODE ch2;
1356 Py_UCS4 ucs;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001357
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001358 ch2 = *s++;
1359 size--;
1360 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1361 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1362 *p++ = '\\';
1363 *p++ = 'U';
1364 *p++ = hexdigit[(ucs >> 28) & 0xf];
1365 *p++ = hexdigit[(ucs >> 24) & 0xf];
1366 *p++ = hexdigit[(ucs >> 20) & 0xf];
1367 *p++ = hexdigit[(ucs >> 16) & 0xf];
1368 *p++ = hexdigit[(ucs >> 12) & 0xf];
1369 *p++ = hexdigit[(ucs >> 8) & 0xf];
1370 *p++ = hexdigit[(ucs >> 4) & 0xf];
1371 *p++ = hexdigit[ucs & 0xf];
1372 continue;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001373 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001374 /* Fall through: isolated surrogates are copied as-is */
1375 s--;
1376 size++;
1377 }
1378#endif
1379 /* Map 16-bit characters to '\uxxxx' */
1380 if (ch >= 256 || ch == '\\' || ch == '\n') {
1381 *p++ = '\\';
1382 *p++ = 'u';
1383 *p++ = hexdigit[(ch >> 12) & 0xf];
1384 *p++ = hexdigit[(ch >> 8) & 0xf];
1385 *p++ = hexdigit[(ch >> 4) & 0xf];
1386 *p++ = hexdigit[ch & 15];
1387 }
1388 /* Copy everything else as-is */
1389 else
1390 *p++ = (char) ch;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001391 }
1392 *p = '\0';
1393 _PyString_Resize(&repr, p - q);
1394 return repr;
1395}
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001396
Guido van Rossum60456fd1997-04-09 17:36:32 +00001397static int
Tim Peterscba30e22003-02-01 06:24:36 +00001398save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001399{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001400 Py_ssize_t size, len;
1401 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001402
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001403 if (!PyUnicode_Check(args))
1404 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001405
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001406 if (!self->bin) {
1407 char *repr_str;
1408 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001409
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001410 repr = modified_EncodeRawUnicodeEscape(
1411 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1412 if (!repr)
1413 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001414
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001415 if ((len = PyString_Size(repr)) < 0)
1416 goto err;
1417 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001418
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001419 if (self->write_func(self, &string, 1) < 0)
1420 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001421
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001422 if (self->write_func(self, repr_str, len) < 0)
1423 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001424
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001425 if (self->write_func(self, "\n", 1) < 0)
1426 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001427
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001428 Py_XDECREF(repr);
1429 }
1430 else {
1431 int i;
1432 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001433
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001434 if (!( repr = PyUnicode_AsUTF8String(args)))
1435 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001436
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001437 if ((size = PyString_Size(repr)) < 0)
1438 goto err;
1439 if (size > INT_MAX)
1440 return -1; /* string too large */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001441
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001442 c_str[0] = BINUNICODE;
1443 for (i = 1; i < 5; i++)
1444 c_str[i] = (int)(size >> ((i - 1) * 8));
1445 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001446
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001447 if (self->write_func(self, c_str, len) < 0)
1448 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001449
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001450 if (size > 128 && Pdata_Check(self->file)) {
1451 if (write_other(self, NULL, 0) < 0)
1452 goto err;
1453 PDATA_APPEND(self->file, repr, -1);
1454 }
1455 else {
1456 if (self->write_func(self, PyString_AS_STRING(repr),
1457 size) < 0)
1458 goto err;
1459 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001460
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001461 Py_DECREF(repr);
1462 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001463
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001464 if (doput)
1465 if (put(self, args) < 0)
1466 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001467
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001468 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001469
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001470 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001471 Py_XDECREF(repr);
1472 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001473}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001474#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001475
Tim Peters1d63c9f2003-02-02 20:29:39 +00001476/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1477static int
Tim Peters67920142003-02-05 03:46:17 +00001478store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001479{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001480 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001481 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001482
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001483 assert(PyTuple_Size(t) == len);
Tim Peters1d63c9f2003-02-02 20:29:39 +00001484
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001485 for (i = 0; i < len; i++) {
1486 PyObject *element = PyTuple_GET_ITEM(t, i);
Tim Peters1d63c9f2003-02-02 20:29:39 +00001487
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001488 if (element == NULL)
1489 goto finally;
1490 if (save(self, element, 0) < 0)
1491 goto finally;
1492 }
1493 res = 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001494
1495 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001496 return res;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001497}
1498
1499/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1500 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001501 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001502 * (a tuple can be reached from itself), and that requires some subtle
1503 * magic so that it works in all cases. IOW, this is a long routine.
1504 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001505static int
Tim Peterscba30e22003-02-01 06:24:36 +00001506save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001507{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001508 PyObject *py_tuple_id = NULL;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001509 Py_ssize_t len, i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001510 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001511
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001512 static char tuple = TUPLE;
1513 static char pop = POP;
1514 static char pop_mark = POP_MARK;
1515 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001516
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001517 if ((len = PyTuple_Size(args)) < 0)
1518 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001519
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001520 if (len == 0) {
1521 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001522
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001523 if (self->proto) {
1524 c_str[0] = EMPTY_TUPLE;
1525 len = 1;
1526 }
1527 else {
1528 c_str[0] = MARK;
1529 c_str[1] = TUPLE;
1530 len = 2;
1531 }
1532 if (self->write_func(self, c_str, len) >= 0)
1533 res = 0;
1534 /* Don't memoize an empty tuple. */
1535 goto finally;
1536 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001537
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001538 /* A non-empty tuple. */
Tim Peters1d63c9f2003-02-02 20:29:39 +00001539
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001540 /* id(tuple) isn't in the memo now. If it shows up there after
1541 * saving the tuple elements, the tuple must be recursive, in
1542 * which case we'll pop everything we put on the stack, and fetch
1543 * its value from the memo.
1544 */
1545 py_tuple_id = PyLong_FromVoidPtr(args);
1546 if (py_tuple_id == NULL)
1547 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001548
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001549 if (len <= 3 && self->proto >= 2) {
1550 /* Use TUPLE{1,2,3} opcodes. */
1551 if (store_tuple_elements(self, args, len) < 0)
1552 goto finally;
1553 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1554 /* pop the len elements */
1555 for (i = 0; i < len; ++i)
1556 if (self->write_func(self, &pop, 1) < 0)
1557 goto finally;
1558 /* fetch from memo */
1559 if (get(self, py_tuple_id) < 0)
1560 goto finally;
1561 res = 0;
1562 goto finally;
1563 }
1564 /* Not recursive. */
1565 if (self->write_func(self, len2opcode + len, 1) < 0)
1566 goto finally;
1567 goto memoize;
1568 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001569
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001570 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1571 * Generate MARK elt1 elt2 ... TUPLE
1572 */
1573 if (self->write_func(self, &MARKv, 1) < 0)
1574 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001575
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001576 if (store_tuple_elements(self, args, len) < 0)
1577 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001578
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001579 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1580 /* pop the stack stuff we pushed */
1581 if (self->bin) {
1582 if (self->write_func(self, &pop_mark, 1) < 0)
1583 goto finally;
1584 }
1585 else {
1586 /* Note that we pop one more than len, to remove
1587 * the MARK too.
1588 */
1589 for (i = 0; i <= len; i++)
1590 if (self->write_func(self, &pop, 1) < 0)
1591 goto finally;
1592 }
1593 /* fetch from memo */
1594 if (get(self, py_tuple_id) >= 0)
1595 res = 0;
1596 goto finally;
1597 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001598
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001599 /* Not recursive. */
1600 if (self->write_func(self, &tuple, 1) < 0)
1601 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001602
Tim Peters1d63c9f2003-02-02 20:29:39 +00001603 memoize:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001604 if (put(self, args) >= 0)
1605 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001606
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001607 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001608 Py_XDECREF(py_tuple_id);
1609 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001610}
1611
Tim Peters1092d642003-02-11 21:06:20 +00001612/* iter is an iterator giving items, and we batch up chunks of
1613 * MARK item item ... item APPENDS
1614 * opcode sequences. Calling code should have arranged to first create an
1615 * empty list, or list-like object, for the APPENDS to operate on.
1616 * Returns 0 on success, <0 on error.
1617 */
1618static int
1619batch_list(Picklerobject *self, PyObject *iter)
1620{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001621 PyObject *obj = NULL;
1622 PyObject *firstitem = NULL;
1623 int i, n;
Tim Peters1092d642003-02-11 21:06:20 +00001624
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001625 static char append = APPEND;
1626 static char appends = APPENDS;
Tim Peters1092d642003-02-11 21:06:20 +00001627
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001628 assert(iter != NULL);
Tim Peters1092d642003-02-11 21:06:20 +00001629
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001630 if (self->proto == 0) {
1631 /* APPENDS isn't available; do one at a time. */
1632 for (;;) {
1633 obj = PyIter_Next(iter);
1634 if (obj == NULL) {
1635 if (PyErr_Occurred())
1636 return -1;
1637 break;
1638 }
1639 i = save(self, obj, 0);
1640 Py_DECREF(obj);
1641 if (i < 0)
1642 return -1;
1643 if (self->write_func(self, &append, 1) < 0)
1644 return -1;
1645 }
1646 return 0;
1647 }
Tim Peters1092d642003-02-11 21:06:20 +00001648
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001649 /* proto > 0: write in batches of BATCHSIZE. */
1650 do {
1651 /* Get first item */
1652 firstitem = PyIter_Next(iter);
1653 if (firstitem == NULL) {
1654 if (PyErr_Occurred())
1655 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001656
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001657 /* nothing more to add */
1658 break;
1659 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001660
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001661 /* Try to get a second item */
1662 obj = PyIter_Next(iter);
1663 if (obj == NULL) {
1664 if (PyErr_Occurred())
1665 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001666
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001667 /* Only one item to write */
1668 if (save(self, firstitem, 0) < 0)
1669 goto BatchFailed;
1670 if (self->write_func(self, &append, 1) < 0)
1671 goto BatchFailed;
1672 Py_CLEAR(firstitem);
1673 break;
1674 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001675
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001676 /* More than one item to write */
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001677
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001678 /* Pump out MARK, items, APPENDS. */
1679 if (self->write_func(self, &MARKv, 1) < 0)
1680 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001681
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001682 if (save(self, firstitem, 0) < 0)
1683 goto BatchFailed;
1684 Py_CLEAR(firstitem);
1685 n = 1;
Tim Peters1092d642003-02-11 21:06:20 +00001686
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001687 /* Fetch and save up to BATCHSIZE items */
1688 while (obj) {
1689 if (save(self, obj, 0) < 0)
1690 goto BatchFailed;
1691 Py_CLEAR(obj);
1692 n += 1;
Tim Peters1092d642003-02-11 21:06:20 +00001693
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001694 if (n == BATCHSIZE)
1695 break;
1696
1697 obj = PyIter_Next(iter);
1698 if (obj == NULL) {
1699 if (PyErr_Occurred())
1700 goto BatchFailed;
1701 break;
1702 }
1703 }
1704
1705 if (self->write_func(self, &appends, 1) < 0)
1706 goto BatchFailed;
1707
1708 } while (n == BATCHSIZE);
1709 return 0;
Tim Peters1092d642003-02-11 21:06:20 +00001710
1711BatchFailed:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001712 Py_XDECREF(firstitem);
1713 Py_XDECREF(obj);
1714 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001715}
1716
Guido van Rossum60456fd1997-04-09 17:36:32 +00001717static int
Tim Peterscba30e22003-02-01 06:24:36 +00001718save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001719{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001720 int res = -1;
1721 char s[3];
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001722 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001723 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001724
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001725 if (self->fast && !fast_save_enter(self, args))
1726 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001727
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001728 /* Create an empty list. */
1729 if (self->bin) {
1730 s[0] = EMPTY_LIST;
1731 len = 1;
1732 }
1733 else {
1734 s[0] = MARK;
1735 s[1] = LIST;
1736 len = 2;
1737 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001738
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001739 if (self->write_func(self, s, len) < 0)
1740 goto finally;
Tim Peters1092d642003-02-11 21:06:20 +00001741
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001742 /* Get list length, and bow out early if empty. */
1743 if ((len = PyList_Size(args)) < 0)
1744 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001745
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001746 /* Memoize. */
1747 if (len == 0) {
1748 if (put(self, args) >= 0)
1749 res = 0;
1750 goto finally;
1751 }
1752 if (put2(self, args) < 0)
1753 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001754
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001755 /* Materialize the list elements. */
1756 iter = PyObject_GetIter(args);
1757 if (iter == NULL)
1758 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001759
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001760 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1761 {
1762 res = batch_list(self, iter);
1763 Py_LeaveRecursiveCall();
1764 }
1765 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001766
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001767 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001768 if (self->fast && !fast_save_leave(self, args))
1769 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001770
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001771 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001772}
1773
1774
Tim Peters42f08ac2003-02-11 22:43:24 +00001775/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1776 * MARK key value ... key value SETITEMS
1777 * opcode sequences. Calling code should have arranged to first create an
1778 * empty dict, or dict-like object, for the SETITEMS to operate on.
1779 * Returns 0 on success, <0 on error.
1780 *
1781 * This is very much like batch_list(). The difference between saving
1782 * elements directly, and picking apart two-tuples, is so long-winded at
1783 * the C level, though, that attempts to combine these routines were too
1784 * ugly to bear.
1785 */
1786static int
1787batch_dict(Picklerobject *self, PyObject *iter)
1788{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001789 PyObject *p = NULL;
1790 PyObject *firstitem = NULL;
1791 int i, n;
Tim Peters42f08ac2003-02-11 22:43:24 +00001792
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001793 static char setitem = SETITEM;
1794 static char setitems = SETITEMS;
Tim Peters42f08ac2003-02-11 22:43:24 +00001795
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001796 assert(iter != NULL);
Tim Peters42f08ac2003-02-11 22:43:24 +00001797
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001798 if (self->proto == 0) {
1799 /* SETITEMS isn't available; do one at a time. */
1800 for (;;) {
1801 p = PyIter_Next(iter);
1802 if (p == NULL) {
1803 if (PyErr_Occurred())
1804 return -1;
1805 break;
1806 }
1807 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1808 PyErr_SetString(PyExc_TypeError, "dict items "
1809 "iterator must return 2-tuples");
1810 return -1;
1811 }
1812 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1813 if (i >= 0)
1814 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1815 Py_DECREF(p);
1816 if (i < 0)
1817 return -1;
1818 if (self->write_func(self, &setitem, 1) < 0)
1819 return -1;
1820 }
1821 return 0;
1822 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001823
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001824 /* proto > 0: write in batches of BATCHSIZE. */
1825 do {
1826 /* Get first item */
1827 firstitem = PyIter_Next(iter);
1828 if (firstitem == NULL) {
1829 if (PyErr_Occurred())
1830 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001831
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001832 /* nothing more to add */
1833 break;
1834 }
1835 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1836 PyErr_SetString(PyExc_TypeError, "dict items "
1837 "iterator must return 2-tuples");
1838 goto BatchFailed;
1839 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001840
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001841 /* Try to get a second item */
1842 p = PyIter_Next(iter);
1843 if (p == NULL) {
1844 if (PyErr_Occurred())
1845 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001846
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001847 /* Only one item to write */
1848 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1849 goto BatchFailed;
1850 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1851 goto BatchFailed;
1852 if (self->write_func(self, &setitem, 1) < 0)
1853 goto BatchFailed;
1854 Py_CLEAR(firstitem);
1855 break;
1856 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001857
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001858 /* More than one item to write */
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001859
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001860 /* Pump out MARK, items, SETITEMS. */
1861 if (self->write_func(self, &MARKv, 1) < 0)
1862 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001863
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001864 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 Py_CLEAR(firstitem);
1869 n = 1;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001870
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001871 /* Fetch and save up to BATCHSIZE items */
1872 while (p) {
1873 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1874 PyErr_SetString(PyExc_TypeError, "dict items "
1875 "iterator must return 2-tuples");
1876 goto BatchFailed;
1877 }
1878 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1879 goto BatchFailed;
1880 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1881 goto BatchFailed;
1882 Py_CLEAR(p);
1883 n += 1;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001884
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001885 if (n == BATCHSIZE)
1886 break;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001887
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001888 p = PyIter_Next(iter);
1889 if (p == NULL) {
1890 if (PyErr_Occurred())
1891 goto BatchFailed;
1892 break;
1893 }
1894 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001895
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001896 if (self->write_func(self, &setitems, 1) < 0)
1897 goto BatchFailed;
Tim Peters42f08ac2003-02-11 22:43:24 +00001898
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001899 } while (n == BATCHSIZE);
1900 return 0;
Tim Peters42f08ac2003-02-11 22:43:24 +00001901
1902BatchFailed:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001903 Py_XDECREF(firstitem);
1904 Py_XDECREF(p);
1905 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001906}
1907
Collin Winter179bf212009-05-25 04:34:39 +00001908/* This is a variant of batch_dict() above that specializes for dicts, with no
1909 * support for dict subclasses. Like batch_dict(), we batch up chunks of
1910 * MARK key value ... key value SETITEMS
1911 * opcode sequences. Calling code should have arranged to first create an
1912 * empty dict, or dict-like object, for the SETITEMS to operate on.
1913 * Returns 0 on success, -1 on error.
1914 *
1915 * Note that this currently doesn't work for protocol 0.
1916 */
1917static int
1918batch_dict_exact(Picklerobject *self, PyObject *obj)
1919{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001920 PyObject *key = NULL, *value = NULL;
1921 int i;
1922 Py_ssize_t dict_size, ppos = 0;
Collin Winter179bf212009-05-25 04:34:39 +00001923
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001924 static char setitem = SETITEM;
1925 static char setitems = SETITEMS;
Collin Winter179bf212009-05-25 04:34:39 +00001926
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001927 assert(obj != NULL);
1928 assert(self->proto > 0);
Collin Winter179bf212009-05-25 04:34:39 +00001929
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001930 dict_size = PyDict_Size(obj);
Collin Winter179bf212009-05-25 04:34:39 +00001931
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001932 /* Special-case len(d) == 1 to save space. */
1933 if (dict_size == 1) {
1934 PyDict_Next(obj, &ppos, &key, &value);
1935 if (save(self, key, 0) < 0)
1936 return -1;
1937 if (save(self, value, 0) < 0)
1938 return -1;
1939 if (self->write_func(self, &setitem, 1) < 0)
1940 return -1;
1941 return 0;
1942 }
Collin Winter179bf212009-05-25 04:34:39 +00001943
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001944 /* Write in batches of BATCHSIZE. */
1945 do {
1946 i = 0;
1947 if (self->write_func(self, &MARKv, 1) < 0)
1948 return -1;
1949 while (PyDict_Next(obj, &ppos, &key, &value)) {
1950 if (save(self, key, 0) < 0)
1951 return -1;
1952 if (save(self, value, 0) < 0)
1953 return -1;
1954 if (++i == BATCHSIZE)
1955 break;
1956 }
1957 if (self->write_func(self, &setitems, 1) < 0)
1958 return -1;
1959 if (PyDict_Size(obj) != dict_size) {
1960 PyErr_Format(
1961 PyExc_RuntimeError,
1962 "dictionary changed size during iteration");
1963 return -1;
1964 }
Collin Winter179bf212009-05-25 04:34:39 +00001965
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001966 } while (i == BATCHSIZE);
1967 return 0;
Collin Winter179bf212009-05-25 04:34:39 +00001968}
1969
Guido van Rossum60456fd1997-04-09 17:36:32 +00001970static int
Tim Peterscba30e22003-02-01 06:24:36 +00001971save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001972{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001973 int res = -1;
1974 char s[3];
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001975 Py_ssize_t len;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001976
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001977 if (self->fast && !fast_save_enter(self, args))
1978 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001979
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001980 /* Create an empty dict. */
1981 if (self->bin) {
1982 s[0] = EMPTY_DICT;
1983 len = 1;
1984 }
1985 else {
1986 s[0] = MARK;
1987 s[1] = DICT;
1988 len = 2;
1989 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001990
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001991 if (self->write_func(self, s, len) < 0)
1992 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001993
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001994 /* Get dict size, and bow out early if empty. */
1995 if ((len = PyDict_Size(args)) < 0)
1996 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001997
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001998 if (len == 0) {
1999 if (put(self, args) >= 0)
2000 res = 0;
2001 goto finally;
2002 }
2003 if (put2(self, args) < 0)
2004 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002005
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002006 /* Materialize the dict items. */
2007 if (PyDict_CheckExact(args) && self->proto > 0) {
2008 /* We can take certain shortcuts if we know this is a dict and
2009 not a dict subclass. */
2010 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
2011 res = batch_dict_exact(self, args);
2012 Py_LeaveRecursiveCall();
2013 }
2014 } else {
2015 PyObject *iter = PyObject_CallMethod(args, "iteritems", "()");
2016 if (iter == NULL)
2017 goto finally;
2018 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
2019 res = batch_dict(self, iter);
2020 Py_LeaveRecursiveCall();
2021 }
2022 Py_DECREF(iter);
2023 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002024
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002025 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002026 if (self->fast && !fast_save_leave(self, args))
2027 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002028
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002029 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002030}
2031
2032
Tim Peters84e87f32001-03-17 04:50:51 +00002033static int
Tim Peterscba30e22003-02-01 06:24:36 +00002034save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002035{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002036 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
2037 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
2038 char *module_str, *name_str;
2039 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002040
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002041 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002042
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002043 if (self->fast && !fast_save_enter(self, args))
2044 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002045
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002046 if (self->write_func(self, &MARKv, 1) < 0)
2047 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002048
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002049 if (!( class = PyObject_GetAttr(args, __class___str)))
2050 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002051
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002052 if (self->bin) {
2053 if (save(self, class, 0) < 0)
2054 goto finally;
2055 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002056
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002057 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
2058 PyObject *element = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002059 Py_ssize_t i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002060
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002061 if (!( class_args =
2062 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
2063 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002064
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002065 if ((len = PyObject_Size(class_args)) < 0)
2066 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002067
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002068 for (i = 0; i < len; i++) {
2069 if (!( element = PySequence_GetItem(class_args, i)))
2070 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002071
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002072 if (save(self, element, 0) < 0) {
2073 Py_DECREF(element);
2074 goto finally;
2075 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002076
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002077 Py_DECREF(element);
2078 }
2079 }
2080 else {
2081 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2082 PyErr_Clear();
2083 else
2084 goto finally;
2085 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002086
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002087 if (!self->bin) {
2088 if (!( name = ((PyClassObject *)class)->cl_name )) {
2089 PyErr_SetString(PicklingError, "class has no name");
2090 goto finally;
2091 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002092
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002093 if (!( module = whichmodule(class, name)))
2094 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002095
Tim Peters84e87f32001-03-17 04:50:51 +00002096
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002097 if ((module_size = PyString_Size(module)) < 0 ||
2098 (name_size = PyString_Size(name)) < 0)
2099 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002100
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002101 module_str = PyString_AS_STRING((PyStringObject *)module);
2102 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002103
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002104 if (self->write_func(self, &inst, 1) < 0)
2105 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002106
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002107 if (self->write_func(self, module_str, module_size) < 0)
2108 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002109
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002110 if (self->write_func(self, "\n", 1) < 0)
2111 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002112
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002113 if (self->write_func(self, name_str, name_size) < 0)
2114 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002115
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002116 if (self->write_func(self, "\n", 1) < 0)
2117 goto finally;
2118 }
2119 else if (self->write_func(self, &obj, 1) < 0) {
2120 goto finally;
2121 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002122
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002123 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2124 state = PyObject_Call(getstate_func, empty_tuple, NULL);
2125 if (!state)
2126 goto finally;
2127 }
2128 else {
2129 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2130 PyErr_Clear();
2131 else
2132 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002133
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002134 if (!( state = PyObject_GetAttr(args, __dict___str))) {
2135 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2136 PyErr_Clear();
2137 else
2138 goto finally;
2139 res = 0;
2140 goto finally;
2141 }
2142 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002143
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002144 if (!PyDict_Check(state)) {
2145 if (put2(self, args) < 0)
2146 goto finally;
2147 }
2148 else {
2149 if (put(self, args) < 0)
2150 goto finally;
2151 }
Tim Peters84e87f32001-03-17 04:50:51 +00002152
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002153 if (save(self, state, 0) < 0)
2154 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002155
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002156 if (self->write_func(self, &build, 1) < 0)
2157 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002158
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002159 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002160
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002161 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002162 if (self->fast && !fast_save_leave(self, args))
2163 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002164
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002165 Py_XDECREF(module);
2166 Py_XDECREF(class);
2167 Py_XDECREF(state);
2168 Py_XDECREF(getinitargs_func);
2169 Py_XDECREF(getstate_func);
2170 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002171
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002172 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002173}
2174
2175
Guido van Rossum60456fd1997-04-09 17:36:32 +00002176static int
Tim Peterscba30e22003-02-01 06:24:36 +00002177save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002178{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002179 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
2180 char *name_str, *module_str;
2181 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002182
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002183 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002184
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002185 if (name) {
2186 global_name = name;
2187 Py_INCREF(global_name);
2188 }
2189 else {
2190 if (!( global_name = PyObject_GetAttr(args, __name___str)))
2191 goto finally;
2192 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002193
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002194 if (!( module = whichmodule(args, global_name)))
2195 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002196
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002197 if ((module_size = PyString_Size(module)) < 0 ||
2198 (name_size = PyString_Size(global_name)) < 0)
2199 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002200
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002201 module_str = PyString_AS_STRING((PyStringObject *)module);
2202 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002203
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002204 /* XXX This can be doing a relative import. Clearly it shouldn't,
2205 but I don't know how to stop it. :-( */
2206 mod = PyImport_ImportModule(module_str);
2207 if (mod == NULL) {
2208 cPickle_ErrFormat(PicklingError,
2209 "Can't pickle %s: import of module %s "
2210 "failed",
2211 "OS", args, module);
2212 goto finally;
2213 }
2214 klass = PyObject_GetAttrString(mod, name_str);
2215 if (klass == NULL) {
2216 cPickle_ErrFormat(PicklingError,
2217 "Can't pickle %s: attribute lookup %s.%s "
2218 "failed",
2219 "OSS", args, module, global_name);
2220 goto finally;
2221 }
2222 if (klass != args) {
2223 Py_DECREF(klass);
2224 cPickle_ErrFormat(PicklingError,
2225 "Can't pickle %s: it's not the same object "
2226 "as %s.%s",
2227 "OSS", args, module, global_name);
2228 goto finally;
2229 }
2230 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002231
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002232 if (self->proto >= 2) {
2233 /* See whether this is in the extension registry, and if
2234 * so generate an EXT opcode.
2235 */
2236 PyObject *py_code; /* extension code as Python object */
2237 long code; /* extension code as C value */
2238 char c_str[5];
2239 int n;
Tim Peters731098b2003-02-04 20:56:09 +00002240
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002241 PyTuple_SET_ITEM(two_tuple, 0, module);
2242 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2243 py_code = PyDict_GetItem(extension_registry, two_tuple);
2244 if (py_code == NULL)
2245 goto gen_global; /* not registered */
Tim Peters731098b2003-02-04 20:56:09 +00002246
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002247 /* Verify py_code has the right type and value. */
2248 if (!PyInt_Check(py_code)) {
2249 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2250 "extension code %s isn't an integer",
2251 "OO", args, py_code);
2252 goto finally;
2253 }
2254 code = PyInt_AS_LONG(py_code);
2255 if (code <= 0 || code > 0x7fffffffL) {
2256 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2257 "extension code %ld is out of range",
2258 "Ol", args, code);
2259 goto finally;
2260 }
Tim Peters731098b2003-02-04 20:56:09 +00002261
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002262 /* Generate an EXT opcode. */
2263 if (code <= 0xff) {
2264 c_str[0] = EXT1;
2265 c_str[1] = (char)code;
2266 n = 2;
2267 }
2268 else if (code <= 0xffff) {
2269 c_str[0] = EXT2;
2270 c_str[1] = (char)(code & 0xff);
2271 c_str[2] = (char)((code >> 8) & 0xff);
2272 n = 3;
2273 }
2274 else {
2275 c_str[0] = EXT4;
2276 c_str[1] = (char)(code & 0xff);
2277 c_str[2] = (char)((code >> 8) & 0xff);
2278 c_str[3] = (char)((code >> 16) & 0xff);
2279 c_str[4] = (char)((code >> 24) & 0xff);
2280 n = 5;
2281 }
Tim Peters731098b2003-02-04 20:56:09 +00002282
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002283 if (self->write_func(self, c_str, n) >= 0)
2284 res = 0;
2285 goto finally; /* and don't memoize */
2286 }
Tim Peters731098b2003-02-04 20:56:09 +00002287
2288 gen_global:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002289 if (self->write_func(self, &global, 1) < 0)
2290 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002291
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002292 if (self->write_func(self, module_str, module_size) < 0)
2293 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002294
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002295 if (self->write_func(self, "\n", 1) < 0)
2296 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002297
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002298 if (self->write_func(self, name_str, name_size) < 0)
2299 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002300
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002301 if (self->write_func(self, "\n", 1) < 0)
2302 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002303
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002304 if (put(self, args) < 0)
2305 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002306
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002307 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002308
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002309 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002310 Py_XDECREF(module);
2311 Py_XDECREF(global_name);
2312 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002313
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002314 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002315}
2316
Guido van Rossum60456fd1997-04-09 17:36:32 +00002317static int
Tim Peterscba30e22003-02-01 06:24:36 +00002318save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002319{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002320 PyObject *pid = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002321 Py_ssize_t size;
2322 int res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002323
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002324 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002325
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002326 Py_INCREF(args);
2327 ARG_TUP(self, args);
2328 if (self->arg) {
2329 pid = PyObject_Call(f, self->arg, NULL);
2330 FREE_ARG_TUP(self);
2331 }
2332 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002333
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002334 if (pid != Py_None) {
2335 if (!self->bin) {
2336 if (!PyString_Check(pid)) {
2337 PyErr_SetString(PicklingError,
2338 "persistent id must be string");
2339 goto finally;
2340 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002341
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002342 if (self->write_func(self, &persid, 1) < 0)
2343 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002344
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002345 if ((size = PyString_Size(pid)) < 0)
2346 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002347
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002348 if (self->write_func(self,
2349 PyString_AS_STRING(
2350 (PyStringObject *)pid),
2351 size) < 0)
2352 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002353
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002354 if (self->write_func(self, "\n", 1) < 0)
2355 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002356
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002357 res = 1;
2358 goto finally;
2359 }
2360 else if (save(self, pid, 1) >= 0) {
2361 if (self->write_func(self, &binpersid, 1) < 0)
2362 res = -1;
2363 else
2364 res = 1;
2365 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002366
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002367 goto finally;
2368 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002369
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002370 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002371
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002372 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002373 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002374
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002375 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002376}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002377
Tim Peters71fcda52003-02-14 23:05:28 +00002378/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2379 * appropriate __reduce__ method for ob.
2380 */
Tim Peters84e87f32001-03-17 04:50:51 +00002381static int
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002382save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002383{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002384 PyObject *callable;
2385 PyObject *argtup;
2386 PyObject *state = NULL;
2387 PyObject *listitems = Py_None;
2388 PyObject *dictitems = Py_None;
2389 Py_ssize_t size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002390
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002391 int use_newobj = self->proto >= 2;
Tim Peters71fcda52003-02-14 23:05:28 +00002392
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002393 static char reduce = REDUCE;
2394 static char build = BUILD;
2395 static char newobj = NEWOBJ;
Tim Peters71fcda52003-02-14 23:05:28 +00002396
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002397 size = PyTuple_Size(args);
2398 if (size < 2 || size > 5) {
2399 cPickle_ErrFormat(PicklingError, "tuple returned by "
2400 "%s must contain 2 through 5 elements",
2401 "O", fn);
2402 return -1;
2403 }
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002404
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002405 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2406 &callable,
2407 &argtup,
2408 &state,
2409 &listitems,
2410 &dictitems))
2411 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002412
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002413 if (!PyTuple_Check(argtup)) {
2414 cPickle_ErrFormat(PicklingError, "Second element of "
2415 "tuple returned by %s must be a tuple",
2416 "O", fn);
2417 return -1;
2418 }
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002419
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002420 if (state == Py_None)
2421 state = NULL;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002422
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002423 if (listitems == Py_None)
2424 listitems = NULL;
2425 else if (!PyIter_Check(listitems)) {
2426 cPickle_ErrFormat(PicklingError, "Fourth element of "
2427 "tuple returned by %s must be an iterator, not %s",
2428 "Os", fn, Py_TYPE(listitems)->tp_name);
2429 return -1;
2430 }
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002431
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002432 if (dictitems == Py_None)
2433 dictitems = NULL;
2434 else if (!PyIter_Check(dictitems)) {
2435 cPickle_ErrFormat(PicklingError, "Fifth element of "
2436 "tuple returned by %s must be an iterator, not %s",
2437 "Os", fn, Py_TYPE(dictitems)->tp_name);
2438 return -1;
2439 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002440
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002441 /* Protocol 2 special case: if callable's name is __newobj__, use
2442 * NEWOBJ. This consumes a lot of code.
2443 */
2444 if (use_newobj) {
2445 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002446
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002447 if (temp == NULL) {
2448 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2449 PyErr_Clear();
2450 else
2451 return -1;
2452 use_newobj = 0;
2453 }
2454 else {
2455 use_newobj = PyString_Check(temp) &&
2456 strcmp(PyString_AS_STRING(temp),
2457 "__newobj__") == 0;
2458 Py_DECREF(temp);
2459 }
2460 }
2461 if (use_newobj) {
2462 PyObject *cls;
2463 PyObject *newargtup;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002464 Py_ssize_t n, i;
Tim Peters71fcda52003-02-14 23:05:28 +00002465
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002466 /* Sanity checks. */
2467 n = PyTuple_Size(argtup);
2468 if (n < 1) {
2469 PyErr_SetString(PicklingError, "__newobj__ arglist "
2470 "is empty");
2471 return -1;
2472 }
Tim Peters71fcda52003-02-14 23:05:28 +00002473
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002474 cls = PyTuple_GET_ITEM(argtup, 0);
2475 if (! PyObject_HasAttrString(cls, "__new__")) {
2476 PyErr_SetString(PicklingError, "args[0] from "
2477 "__newobj__ args has no __new__");
2478 return -1;
2479 }
Tim Peters71fcda52003-02-14 23:05:28 +00002480
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002481 /* XXX How could ob be NULL? */
2482 if (ob != NULL) {
2483 PyObject *ob_dot_class;
Tim Peters71fcda52003-02-14 23:05:28 +00002484
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002485 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2486 if (ob_dot_class == NULL) {
2487 if (PyErr_ExceptionMatches(
2488 PyExc_AttributeError))
2489 PyErr_Clear();
2490 else
2491 return -1;
2492 }
2493 i = ob_dot_class != cls; /* true iff a problem */
2494 Py_XDECREF(ob_dot_class);
2495 if (i) {
2496 PyErr_SetString(PicklingError, "args[0] from "
2497 "__newobj__ args has the wrong class");
2498 return -1;
2499 }
2500 }
Tim Peters71fcda52003-02-14 23:05:28 +00002501
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002502 /* Save the class and its __new__ arguments. */
2503 if (save(self, cls, 0) < 0)
2504 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002505
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002506 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2507 if (newargtup == NULL)
2508 return -1;
2509 for (i = 1; i < n; ++i) {
2510 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2511 Py_INCREF(temp);
2512 PyTuple_SET_ITEM(newargtup, i-1, temp);
2513 }
2514 i = save(self, newargtup, 0);
2515 Py_DECREF(newargtup);
2516 if (i < 0)
2517 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002518
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002519 /* Add NEWOBJ opcode. */
2520 if (self->write_func(self, &newobj, 1) < 0)
2521 return -1;
2522 }
2523 else {
2524 /* Not using NEWOBJ. */
2525 if (save(self, callable, 0) < 0 ||
2526 save(self, argtup, 0) < 0 ||
2527 self->write_func(self, &reduce, 1) < 0)
2528 return -1;
2529 }
Tim Peters71fcda52003-02-14 23:05:28 +00002530
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002531 /* Memoize. */
2532 /* XXX How can ob be NULL? */
2533 if (ob != NULL) {
Serhiy Storchakada87e452015-11-07 11:15:32 +02002534 /* If the object is already in the memo, this means it is
2535 recursive. In this case, throw away everything we put on the
2536 stack, and fetch the object back from the memo. */
2537 if (Py_REFCNT(ob) > 1 && !self->fast) {
2538 PyObject *py_ob_id = PyLong_FromVoidPtr(ob);
2539 if (!py_ob_id)
2540 return -1;
2541 if (PyDict_GetItem(self->memo, py_ob_id)) {
2542 const char pop_op = POP;
2543 if (self->write_func(self, &pop_op, 1) < 0 ||
2544 get(self, py_ob_id) < 0) {
2545 Py_DECREF(py_ob_id);
2546 return -1;
2547 }
2548 Py_DECREF(py_ob_id);
2549 return 0;
2550 }
2551 Py_DECREF(py_ob_id);
2552 if (PyErr_Occurred())
2553 return -1;
2554 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002555 if (state && !PyDict_Check(state)) {
2556 if (put2(self, ob) < 0)
2557 return -1;
2558 }
2559 else if (put(self, ob) < 0)
2560 return -1;
2561 }
Tim Peters84e87f32001-03-17 04:50:51 +00002562
Guido van Rossum60456fd1997-04-09 17:36:32 +00002563
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002564 if (listitems && batch_list(self, listitems) < 0)
2565 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002566
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002567 if (dictitems && batch_dict(self, dictitems) < 0)
2568 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002569
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002570 if (state) {
2571 if (save(self, state, 0) < 0 ||
2572 self->write_func(self, &build, 1) < 0)
2573 return -1;
2574 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002575
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002576 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002577}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002578
Guido van Rossum60456fd1997-04-09 17:36:32 +00002579static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002580save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002581{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002582 PyTypeObject *type;
2583 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2584 int res = -1;
2585 int tmp;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002586
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002587 if (Py_EnterRecursiveCall(" while pickling an object"))
2588 return -1;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002589
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002590 if (!pers_save && self->pers_func) {
2591 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2592 res = tmp;
2593 goto finally;
2594 }
2595 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002596
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002597 if (args == Py_None) {
2598 res = save_none(self, args);
2599 goto finally;
2600 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002601
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002602 type = Py_TYPE(args);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002603
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002604 switch (type->tp_name[0]) {
2605 case 'b':
2606 if (args == Py_False || args == Py_True) {
2607 res = save_bool(self, args);
2608 goto finally;
2609 }
2610 break;
2611 case 'i':
2612 if (type == &PyInt_Type) {
2613 res = save_int(self, args);
2614 goto finally;
2615 }
2616 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002617
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002618 case 'l':
2619 if (type == &PyLong_Type) {
2620 res = save_long(self, args);
2621 goto finally;
2622 }
2623 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002624
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002625 case 'f':
2626 if (type == &PyFloat_Type) {
2627 res = save_float(self, args);
2628 goto finally;
2629 }
2630 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002631
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002632 case 't':
2633 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2634 res = save_tuple(self, args);
2635 goto finally;
2636 }
2637 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002638
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002639 case 's':
2640 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2641 res = save_string(self, args, 0);
2642 goto finally;
2643 }
2644 break;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002645
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002646#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002647 case 'u':
2648 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2649 res = save_unicode(self, args, 0);
2650 goto finally;
2651 }
2652 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002653#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002654 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002655
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002656 if (Py_REFCNT(args) > 1) {
2657 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2658 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002659
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002660 if (PyDict_GetItem(self->memo, py_ob_id)) {
2661 if (get(self, py_ob_id) < 0)
2662 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002663
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002664 res = 0;
2665 goto finally;
2666 }
2667 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002668
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002669 switch (type->tp_name[0]) {
2670 case 's':
2671 if (type == &PyString_Type) {
2672 res = save_string(self, args, 1);
2673 goto finally;
2674 }
2675 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002676
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002677#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002678 case 'u':
2679 if (type == &PyUnicode_Type) {
2680 res = save_unicode(self, args, 1);
2681 goto finally;
2682 }
2683 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002684#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002685
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002686 case 't':
2687 if (type == &PyTuple_Type) {
2688 res = save_tuple(self, args);
2689 goto finally;
2690 }
2691 if (type == &PyType_Type) {
Alexandre Vassalottidf9460f2013-11-30 17:43:42 -08002692 res = save_global(self, args, NULL);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002693 goto finally;
2694 }
2695 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002696
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002697 case 'l':
2698 if (type == &PyList_Type) {
2699 res = save_list(self, args);
2700 goto finally;
2701 }
2702 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002703
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002704 case 'd':
2705 if (type == &PyDict_Type) {
2706 res = save_dict(self, args);
2707 goto finally;
2708 }
2709 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002710
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002711 case 'i':
2712 if (type == &PyInstance_Type) {
2713 res = save_inst(self, args);
2714 goto finally;
2715 }
2716 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002717
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002718 case 'c':
2719 if (type == &PyClass_Type) {
2720 res = save_global(self, args, NULL);
2721 goto finally;
2722 }
2723 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002724
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002725 case 'f':
2726 if (type == &PyFunction_Type) {
2727 res = save_global(self, args, NULL);
2728 if (res && PyErr_ExceptionMatches(PickleError)) {
2729 /* fall back to reduce */
2730 PyErr_Clear();
2731 break;
2732 }
2733 goto finally;
2734 }
2735 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002736
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002737 case 'b':
2738 if (type == &PyCFunction_Type) {
2739 res = save_global(self, args, NULL);
2740 goto finally;
2741 }
2742 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002743
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002744 if (!pers_save && self->inst_pers_func) {
2745 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2746 res = tmp;
2747 goto finally;
2748 }
2749 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002750
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002751 /* Get a reduction callable, and call it. This may come from
2752 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2753 * or the object's __reduce__ method.
2754 */
2755 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2756 if (__reduce__ != NULL) {
2757 Py_INCREF(__reduce__);
2758 Py_INCREF(args);
2759 ARG_TUP(self, args);
2760 if (self->arg) {
2761 t = PyObject_Call(__reduce__, self->arg, NULL);
2762 FREE_ARG_TUP(self);
2763 }
2764 }
2765 else {
Antoine Pitrou561a8212011-10-04 09:34:48 +02002766 if (PyType_IsSubtype(type, &PyType_Type)) {
2767 res = save_global(self, args, NULL);
2768 goto finally;
2769 }
2770
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002771 /* Check for a __reduce_ex__ method. */
2772 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2773 if (__reduce__ != NULL) {
2774 t = PyInt_FromLong(self->proto);
2775 if (t != NULL) {
2776 ARG_TUP(self, t);
2777 t = NULL;
2778 if (self->arg) {
2779 t = PyObject_Call(__reduce__,
2780 self->arg, NULL);
2781 FREE_ARG_TUP(self);
2782 }
2783 }
2784 }
2785 else {
2786 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2787 PyErr_Clear();
2788 else
2789 goto finally;
2790 /* Check for a __reduce__ method. */
2791 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2792 if (__reduce__ != NULL) {
2793 t = PyObject_Call(__reduce__,
2794 empty_tuple, NULL);
2795 }
2796 else {
2797 PyErr_SetObject(UnpickleableError, args);
2798 goto finally;
2799 }
2800 }
2801 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002802
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002803 if (t == NULL)
2804 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002805
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002806 if (PyString_Check(t)) {
2807 res = save_global(self, args, t);
2808 goto finally;
2809 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002810
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002811 if (!PyTuple_Check(t)) {
2812 cPickle_ErrFormat(PicklingError, "Value returned by "
2813 "%s must be string or tuple",
2814 "O", __reduce__);
2815 goto finally;
2816 }
Tim Peters71fcda52003-02-14 23:05:28 +00002817
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002818 res = save_reduce(self, t, __reduce__, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002819
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002820 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002821 Py_LeaveRecursiveCall();
2822 Py_XDECREF(py_ob_id);
2823 Py_XDECREF(__reduce__);
2824 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002825
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002826 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002827}
2828
2829
2830static int
Tim Peterscba30e22003-02-01 06:24:36 +00002831dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002832{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002833 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002834
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002835 if (self->proto >= 2) {
2836 char bytes[2];
Tim Peters4190fb82003-02-02 16:09:05 +00002837
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002838 bytes[0] = PROTO;
2839 assert(self->proto >= 0 && self->proto < 256);
2840 bytes[1] = (char)self->proto;
2841 if (self->write_func(self, bytes, 2) < 0)
2842 return -1;
2843 }
Tim Peters4190fb82003-02-02 16:09:05 +00002844
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002845 if (save(self, args, 0) < 0)
2846 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002847
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002848 if (self->write_func(self, &stop, 1) < 0)
2849 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002850
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002851 if (self->write_func(self, NULL, 0) < 0)
2852 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002853
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002854 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002855}
2856
2857static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002858Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002859{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002860 if (self->memo)
2861 PyDict_Clear(self->memo);
2862 Py_INCREF(Py_None);
2863 return Py_None;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002864}
2865
2866static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002867Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002868{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002869 Py_ssize_t l, i, rsize, ssize, clear=1, lm;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002870 long ik;
2871 PyObject *k, *r;
2872 char *s, *p, *have_get;
2873 Pdata *data;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002874
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002875 /* Can be called by Python code or C code */
2876 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2877 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002878
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002879 /* Check to make sure we are based on a list */
2880 if (! Pdata_Check(self->file)) {
2881 PyErr_SetString(PicklingError,
2882 "Attempt to getvalue() a non-list-based pickler");
2883 return NULL;
2884 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002885
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002886 /* flush write buffer */
2887 if (write_other(self, NULL, 0) < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002888
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002889 data=(Pdata*)self->file;
2890 l=data->length;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002891
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002892 /* set up an array to hold get/put status */
2893 lm = PyDict_Size(self->memo);
2894 if (lm < 0) return NULL;
2895 lm++;
2896 have_get = malloc(lm);
2897 if (have_get == NULL) return PyErr_NoMemory();
2898 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002899
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002900 /* Scan for gets. */
2901 for (rsize = 0, i = l; --i >= 0; ) {
2902 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002903
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002904 if (PyString_Check(k))
2905 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002906
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002907 else if (PyInt_Check(k)) { /* put */
2908 ik = PyInt_AS_LONG((PyIntObject*)k);
2909 if (ik >= lm || ik == 0) {
2910 PyErr_SetString(PicklingError,
2911 "Invalid get data");
2912 goto err;
2913 }
2914 if (have_get[ik]) /* with matching get */
2915 rsize += ik < 256 ? 2 : 5;
2916 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002917
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002918 else if (! (PyTuple_Check(k) &&
2919 PyTuple_GET_SIZE(k) == 2 &&
2920 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2921 ) {
2922 PyErr_SetString(PicklingError,
2923 "Unexpected data in internal list");
2924 goto err;
2925 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002926
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002927 else { /* put */
2928 ik = PyInt_AS_LONG((PyIntObject *)k);
2929 if (ik >= lm || ik == 0) {
2930 PyErr_SetString(PicklingError,
2931 "Invalid get data");
Benjamin Peterson7f18ac42015-07-25 10:20:13 -07002932 goto err;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002933 }
2934 have_get[ik] = 1;
2935 rsize += ik < 256 ? 2 : 5;
2936 }
2937 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002938
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002939 /* Now generate the result */
2940 r = PyString_FromStringAndSize(NULL, rsize);
2941 if (r == NULL) goto err;
2942 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002943
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002944 for (i = 0; i < l; i++) {
2945 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002946
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002947 if (PyString_Check(k)) {
2948 ssize = PyString_GET_SIZE(k);
2949 if (ssize) {
2950 p=PyString_AS_STRING((PyStringObject *)k);
2951 while (--ssize >= 0)
2952 *s++ = *p++;
2953 }
2954 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002955
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002956 else if (PyTuple_Check(k)) { /* get */
2957 ik = PyInt_AS_LONG((PyIntObject *)
2958 PyTuple_GET_ITEM(k, 0));
2959 if (ik < 256) {
2960 *s++ = BINGET;
2961 *s++ = (int)(ik & 0xff);
2962 }
2963 else {
2964 *s++ = LONG_BINGET;
2965 *s++ = (int)(ik & 0xff);
2966 *s++ = (int)((ik >> 8) & 0xff);
2967 *s++ = (int)((ik >> 16) & 0xff);
2968 *s++ = (int)((ik >> 24) & 0xff);
2969 }
2970 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002971
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002972 else { /* put */
2973 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002974
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002975 if (have_get[ik]) { /* with matching get */
2976 if (ik < 256) {
2977 *s++ = BINPUT;
2978 *s++ = (int)(ik & 0xff);
2979 }
2980 else {
2981 *s++ = LONG_BINPUT;
2982 *s++ = (int)(ik & 0xff);
2983 *s++ = (int)((ik >> 8) & 0xff);
2984 *s++ = (int)((ik >> 16) & 0xff);
2985 *s++ = (int)((ik >> 24) & 0xff);
2986 }
2987 }
2988 }
2989 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002990
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002991 if (clear) {
2992 PyDict_Clear(self->memo);
2993 Pdata_clear(data, 0);
2994 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002995
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002996 free(have_get);
2997 return r;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002998 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002999 free(have_get);
3000 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003001}
3002
3003static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003004Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003005{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003006 PyObject *ob;
3007 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003008
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003009 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
3010 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003011
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003012 if (dump(self, ob) < 0)
3013 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003014
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003015 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003016
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003017 /* XXX Why does dump() return self? */
3018 Py_INCREF(self);
3019 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003020}
3021
3022
Tim Peterscba30e22003-02-01 06:24:36 +00003023static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003024{
Neal Norwitzb0493252002-03-31 14:44:22 +00003025 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003026 PyDoc_STR("dump(object) -- "
3027 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00003028 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003029 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00003030 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003031 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00003032 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003033};
3034
3035
3036static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00003037newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003038{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003039 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003040
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003041 if (proto < 0)
3042 proto = HIGHEST_PROTOCOL;
3043 if (proto > HIGHEST_PROTOCOL) {
3044 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
3045 "the highest available protocol is %d",
3046 proto, HIGHEST_PROTOCOL);
3047 return NULL;
3048 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003049
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003050 self = PyObject_GC_New(Picklerobject, &Picklertype);
3051 if (self == NULL)
3052 return NULL;
3053 self->proto = proto;
3054 self->bin = proto > 0;
3055 self->fp = NULL;
3056 self->write = NULL;
3057 self->memo = NULL;
3058 self->arg = NULL;
3059 self->pers_func = NULL;
3060 self->inst_pers_func = NULL;
3061 self->write_buf = NULL;
3062 self->fast = 0;
3063 self->fast_container = 0;
3064 self->fast_memo = NULL;
3065 self->buf_size = 0;
3066 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003067
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003068 self->file = NULL;
3069 if (file)
3070 Py_INCREF(file);
3071 else {
3072 file = Pdata_New();
3073 if (file == NULL)
3074 goto err;
3075 }
3076 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003077
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003078 if (!( self->memo = PyDict_New()))
3079 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003080
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003081 if (PyFile_Check(file)) {
3082 self->fp = PyFile_AsFile(file);
3083 if (self->fp == NULL) {
3084 PyErr_SetString(PyExc_ValueError,
3085 "I/O operation on closed file");
3086 goto err;
3087 }
3088 self->write_func = write_file;
3089 }
3090 else if (PycStringIO_OutputCheck(file)) {
3091 self->write_func = write_cStringIO;
3092 }
3093 else if (file == Py_None) {
3094 self->write_func = write_none;
3095 }
3096 else {
3097 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003098
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003099 if (! Pdata_Check(file)) {
3100 self->write = PyObject_GetAttr(file, write_str);
3101 if (!self->write) {
3102 PyErr_Clear();
3103 PyErr_SetString(PyExc_TypeError,
3104 "argument must have 'write' "
3105 "attribute");
3106 goto err;
3107 }
3108 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003109
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003110 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
3111 if (self->write_buf == NULL) {
3112 PyErr_NoMemory();
3113 goto err;
3114 }
3115 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003116
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003117 if (PyEval_GetRestricted()) {
3118 /* Restricted execution, get private tables */
3119 PyObject *m = PyImport_ImportModule("copy_reg");
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003120
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003121 if (m == NULL)
3122 goto err;
3123 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
3124 Py_DECREF(m);
3125 if (self->dispatch_table == NULL)
3126 goto err;
3127 }
3128 else {
3129 self->dispatch_table = dispatch_table;
3130 Py_INCREF(dispatch_table);
3131 }
3132 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003133
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003134 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003136 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003137 Py_DECREF(self);
3138 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003139}
3140
3141
3142static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00003143get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003144{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003145 static char *kwlist[] = {"file", "protocol", NULL};
3146 PyObject *file = NULL;
3147 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003148
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003149 /* XXX
3150 * The documented signature is Pickler(file, protocol=0), but this
3151 * accepts Pickler() and Pickler(integer) too. The meaning then
3152 * is clear as mud, undocumented, and not supported by pickle.py.
3153 * I'm told Zope uses this, but I haven't traced into this code
3154 * far enough to figure out what it means.
3155 */
3156 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
3157 PyErr_Clear();
3158 proto = 0;
3159 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
3160 kwlist, &file, &proto))
3161 return NULL;
3162 }
3163 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003164}
3165
3166
3167static void
Tim Peterscba30e22003-02-01 06:24:36 +00003168Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003169{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003170 PyObject_GC_UnTrack(self);
3171 Py_XDECREF(self->write);
3172 Py_XDECREF(self->memo);
3173 Py_XDECREF(self->fast_memo);
3174 Py_XDECREF(self->arg);
3175 Py_XDECREF(self->file);
3176 Py_XDECREF(self->pers_func);
3177 Py_XDECREF(self->inst_pers_func);
3178 Py_XDECREF(self->dispatch_table);
3179 PyMem_Free(self->write_buf);
3180 Py_TYPE(self)->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003181}
3182
3183static int
3184Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
3185{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003186 Py_VISIT(self->write);
3187 Py_VISIT(self->memo);
3188 Py_VISIT(self->fast_memo);
3189 Py_VISIT(self->arg);
3190 Py_VISIT(self->file);
3191 Py_VISIT(self->pers_func);
3192 Py_VISIT(self->inst_pers_func);
3193 Py_VISIT(self->dispatch_table);
3194 return 0;
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003195}
3196
3197static int
3198Pickler_clear(Picklerobject *self)
3199{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003200 Py_CLEAR(self->write);
3201 Py_CLEAR(self->memo);
3202 Py_CLEAR(self->fast_memo);
3203 Py_CLEAR(self->arg);
3204 Py_CLEAR(self->file);
3205 Py_CLEAR(self->pers_func);
3206 Py_CLEAR(self->inst_pers_func);
3207 Py_CLEAR(self->dispatch_table);
3208 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003209}
3210
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003211static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003212Pickler_get_pers_func(Picklerobject *p)
3213{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003214 if (p->pers_func == NULL)
3215 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3216 else
3217 Py_INCREF(p->pers_func);
3218 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003219}
3220
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003221static int
3222Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3223{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003224 if (v == NULL) {
3225 PyErr_SetString(PyExc_TypeError,
3226 "attribute deletion is not supported");
3227 return -1;
3228 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003229 Py_INCREF(v);
Serhiy Storchaka5951f232015-12-24 10:35:35 +02003230 Py_SETREF(p->pers_func, v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003231 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003232}
3233
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003234static int
3235Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3236{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003237 if (v == NULL) {
3238 PyErr_SetString(PyExc_TypeError,
3239 "attribute deletion is not supported");
3240 return -1;
3241 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003242 Py_INCREF(v);
Serhiy Storchaka5951f232015-12-24 10:35:35 +02003243 Py_SETREF(p->inst_pers_func, v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003244 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003245}
3246
3247static PyObject *
3248Pickler_get_memo(Picklerobject *p)
3249{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003250 if (p->memo == NULL)
3251 PyErr_SetString(PyExc_AttributeError, "memo");
3252 else
3253 Py_INCREF(p->memo);
3254 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003255}
3256
3257static int
3258Pickler_set_memo(Picklerobject *p, PyObject *v)
3259{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003260 if (v == NULL) {
3261 PyErr_SetString(PyExc_TypeError,
3262 "attribute deletion is not supported");
3263 return -1;
3264 }
3265 if (!PyDict_Check(v)) {
3266 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3267 return -1;
3268 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003269 Py_INCREF(v);
Serhiy Storchaka5951f232015-12-24 10:35:35 +02003270 Py_SETREF(p->memo, v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003271 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003272}
3273
3274static PyObject *
3275Pickler_get_error(Picklerobject *p)
3276{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003277 /* why is this an attribute on the Pickler? */
3278 Py_INCREF(PicklingError);
3279 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003280}
3281
3282static PyMemberDef Pickler_members[] = {
3283 {"binary", T_INT, offsetof(Picklerobject, bin)},
3284 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003285 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003286};
3287
3288static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003289 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003290 (setter)Pickler_set_pers_func},
3291 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3292 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003293 {"PicklingError", (getter)Pickler_get_error, NULL},
3294 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003295};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003296
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003297PyDoc_STRVAR(Picklertype__doc__,
3298"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003299
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003300static PyTypeObject Picklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003301 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00003302 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003303 sizeof(Picklerobject), /*tp_basicsize*/
3304 0,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003305 (destructor)Pickler_dealloc, /* tp_dealloc */
3306 0, /* tp_print */
3307 0, /* tp_getattr */
3308 0, /* tp_setattr */
3309 0, /* tp_compare */
3310 0, /* tp_repr */
3311 0, /* tp_as_number */
3312 0, /* tp_as_sequence */
3313 0, /* tp_as_mapping */
3314 0, /* tp_hash */
3315 0, /* tp_call */
3316 0, /* tp_str */
3317 PyObject_GenericGetAttr, /* tp_getattro */
3318 PyObject_GenericSetAttr, /* tp_setattro */
3319 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003320 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003321 Picklertype__doc__, /* tp_doc */
3322 (traverseproc)Pickler_traverse, /* tp_traverse */
3323 (inquiry)Pickler_clear, /* tp_clear */
3324 0, /* tp_richcompare */
3325 0, /* tp_weaklistoffset */
3326 0, /* tp_iter */
3327 0, /* tp_iternext */
3328 Pickler_methods, /* tp_methods */
3329 Pickler_members, /* tp_members */
3330 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003331};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003332
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003333static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003334find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003335{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003336 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003337
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003338 if (fc) {
3339 if (fc==Py_None) {
3340 PyErr_SetString(UnpicklingError, "Global and instance "
3341 "pickles are not supported.");
3342 return NULL;
3343 }
3344 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3345 py_global_name, NULL);
3346 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003347
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003348 module = PySys_GetObject("modules");
3349 if (module == NULL)
3350 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003351
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003352 module = PyDict_GetItem(module, py_module_name);
3353 if (module == NULL) {
3354 module = PyImport_Import(py_module_name);
3355 if (!module)
3356 return NULL;
3357 global = PyObject_GetAttr(module, py_global_name);
3358 Py_DECREF(module);
3359 }
3360 else
3361 global = PyObject_GetAttr(module, py_global_name);
3362 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003363}
3364
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003365static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +00003366marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003367{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003368 if (self->num_marks < 1) {
3369 PyErr_SetString(UnpicklingError, "could not find MARK");
3370 return -1;
3371 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003372
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003373 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003374}
3375
Tim Peters84e87f32001-03-17 04:50:51 +00003376
Guido van Rossum60456fd1997-04-09 17:36:32 +00003377static int
Tim Peterscba30e22003-02-01 06:24:36 +00003378load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003379{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003380 PDATA_APPEND(self->stack, Py_None, -1);
3381 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003382}
3383
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003384static int
Tim Peterscba30e22003-02-01 06:24:36 +00003385bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003386{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003387 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3388 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003389}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003390
3391static int
Tim Peterscba30e22003-02-01 06:24:36 +00003392load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003393{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003394 PyObject *py_int = 0;
3395 char *endptr, *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003396 Py_ssize_t len;
3397 int res = -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003398 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003399
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003400 if ((len = self->readline_func(self, &s)) < 0) return -1;
3401 if (len < 2) return bad_readline();
3402 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003403
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003404 errno = 0;
3405 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003406
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003407 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3408 /* Hm, maybe we've got something long. Let's try reading
3409 it as a Python long object. */
3410 errno = 0;
3411 py_int = PyLong_FromString(s, NULL, 0);
3412 if (py_int == NULL) {
3413 PyErr_SetString(PyExc_ValueError,
3414 "could not convert string to int");
3415 goto finally;
3416 }
3417 }
3418 else {
3419 if (len == 3 && (l == 0 || l == 1)) {
3420 if (!( py_int = PyBool_FromLong(l))) goto finally;
3421 }
3422 else {
3423 if (!( py_int = PyInt_FromLong(l))) goto finally;
3424 }
3425 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003426
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003427 free(s);
3428 PDATA_PUSH(self->stack, py_int, -1);
3429 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003430
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003431 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003432 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003433
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003434 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003435}
3436
Tim Peters3c67d792003-02-02 17:59:11 +00003437static int
3438load_bool(Unpicklerobject *self, PyObject *boolean)
3439{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003440 assert(boolean == Py_True || boolean == Py_False);
3441 PDATA_APPEND(self->stack, boolean, -1);
3442 return 0;
Tim Peters3c67d792003-02-02 17:59:11 +00003443}
3444
Tim Petersee1a53c2003-02-02 02:57:53 +00003445/* s contains x bytes of a little-endian integer. Return its value as a
3446 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3447 * int, but when x is 4 it's a signed one. This is an historical source
3448 * of x-platform bugs.
3449 */
Tim Peters84e87f32001-03-17 04:50:51 +00003450static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003451calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003452{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003453 unsigned char c;
3454 int i;
3455 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003456
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003457 for (i = 0, l = 0L; i < x; i++) {
3458 c = (unsigned char)s[i];
3459 l |= (long)c << (i * 8);
3460 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003461#if SIZEOF_LONG > 4
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003462 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3463 * is signed, so on a box with longs bigger than 4 bytes we need
3464 * to extend a BININT's sign bit to the full width.
3465 */
3466 if (x == 4 && l & (1L << 31))
Gregory P. Smith0a857282015-08-04 16:29:00 -07003467 l |= (~0UL) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003468#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003469 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003470}
3471
3472
3473static int
Tim Peterscba30e22003-02-01 06:24:36 +00003474load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003475{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003476 PyObject *py_int = 0;
3477 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003478
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003479 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003480
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003481 if (!( py_int = PyInt_FromLong(l)))
3482 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003483
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003484 PDATA_PUSH(self->stack, py_int, -1);
3485 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003486}
3487
3488
3489static int
Tim Peterscba30e22003-02-01 06:24:36 +00003490load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003491{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003492 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003493
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003494 if (self->read_func(self, &s, 4) < 0)
3495 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003496
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003497 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003498}
3499
3500
3501static int
Tim Peterscba30e22003-02-01 06:24:36 +00003502load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003503{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003504 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003505
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003506 if (self->read_func(self, &s, 1) < 0)
3507 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003508
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003509 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003510}
3511
3512
3513static int
Tim Peterscba30e22003-02-01 06:24:36 +00003514load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003515{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003516 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003517
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003518 if (self->read_func(self, &s, 2) < 0)
3519 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003520
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003521 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003522}
Tim Peters84e87f32001-03-17 04:50:51 +00003523
Guido van Rossum60456fd1997-04-09 17:36:32 +00003524static int
Tim Peterscba30e22003-02-01 06:24:36 +00003525load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003526{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003527 PyObject *l = 0;
3528 char *end, *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003529 Py_ssize_t len;
3530 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003531
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003532 if ((len = self->readline_func(self, &s)) < 0) return -1;
3533 if (len < 2) return bad_readline();
3534 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003535
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003536 if (!( l = PyLong_FromString(s, &end, 0)))
3537 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003538
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003539 free(s);
3540 PDATA_PUSH(self->stack, l, -1);
3541 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003542
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003543 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003544 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003545
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003546 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003547}
3548
Tim Petersee1a53c2003-02-02 02:57:53 +00003549/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3550 * data following.
3551 */
3552static int
3553load_counted_long(Unpicklerobject *self, int size)
3554{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003555 Py_ssize_t i;
3556 char *nbytes;
3557 unsigned char *pdata;
3558 PyObject *along;
Tim Petersee1a53c2003-02-02 02:57:53 +00003559
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003560 assert(size == 1 || size == 4);
3561 i = self->read_func(self, &nbytes, size);
3562 if (i < 0) return -1;
Tim Petersee1a53c2003-02-02 02:57:53 +00003563
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003564 size = calc_binint(nbytes, size);
3565 if (size < 0) {
3566 /* Corrupt or hostile pickle -- we never write one like
3567 * this.
3568 */
3569 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3570 "byte count");
3571 return -1;
3572 }
Tim Petersee1a53c2003-02-02 02:57:53 +00003573
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003574 if (size == 0)
3575 along = PyLong_FromLong(0L);
3576 else {
3577 /* Read the raw little-endian bytes & convert. */
3578 i = self->read_func(self, (char **)&pdata, size);
3579 if (i < 0) return -1;
3580 along = _PyLong_FromByteArray(pdata, (size_t)size,
3581 1 /* little endian */, 1 /* signed */);
3582 }
3583 if (along == NULL)
3584 return -1;
3585 PDATA_PUSH(self->stack, along, -1);
3586 return 0;
Tim Petersee1a53c2003-02-02 02:57:53 +00003587}
Tim Peters84e87f32001-03-17 04:50:51 +00003588
Guido van Rossum60456fd1997-04-09 17:36:32 +00003589static int
Tim Peterscba30e22003-02-01 06:24:36 +00003590load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003591{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003592 PyObject *py_float = 0;
3593 char *endptr, *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003594 Py_ssize_t len;
3595 int res = -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003596 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003597
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003598 if ((len = self->readline_func(self, &s)) < 0) return -1;
3599 if (len < 2) return bad_readline();
3600 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003601
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003602 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003603
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003604 if (d == -1.0 && PyErr_Occurred()) {
3605 goto finally;
3606 } else if ((endptr[0] != '\n') || (endptr[1] != '\0')) {
3607 PyErr_SetString(PyExc_ValueError,
3608 "could not convert string to float");
3609 goto finally;
3610 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003611
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003612 if (!( py_float = PyFloat_FromDouble(d)))
3613 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003614
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003615 free(s);
3616 PDATA_PUSH(self->stack, py_float, -1);
3617 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003619 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003620 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003621
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003622 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003623}
3624
Guido van Rossum60456fd1997-04-09 17:36:32 +00003625static int
Tim Peterscba30e22003-02-01 06:24:36 +00003626load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003627{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003628 PyObject *py_float;
3629 double x;
3630 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003631
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003632 if (self->read_func(self, &p, 8) < 0)
3633 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003634
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003635 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3636 if (x == -1.0 && PyErr_Occurred())
3637 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003638
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003639 py_float = PyFloat_FromDouble(x);
3640 if (py_float == NULL)
3641 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003642
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003643 PDATA_PUSH(self->stack, py_float, -1);
3644 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003645}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003646
3647static int
Tim Peterscba30e22003-02-01 06:24:36 +00003648load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003649{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003650 PyObject *str = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003651 Py_ssize_t len;
3652 int res = -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003653 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003654
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003655 if ((len = self->readline_func(self, &s)) < 0) return -1;
3656 if (len < 2) return bad_readline();
3657 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003658
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003659
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003660 /* Strip outermost quotes */
Antoine Pitroube929712013-04-15 21:35:25 +02003661 while (len > 0 && s[len-1] <= ' ')
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003662 len--;
Antoine Pitroube929712013-04-15 21:35:25 +02003663 if (len > 1 && s[0]=='"' && s[len-1]=='"') {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003664 s[len-1] = '\0';
3665 p = s + 1 ;
3666 len -= 2;
Antoine Pitroube929712013-04-15 21:35:25 +02003667 }
3668 else if (len > 1 && s[0]=='\'' && s[len-1]=='\'') {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003669 s[len-1] = '\0';
3670 p = s + 1 ;
3671 len -= 2;
Antoine Pitroube929712013-04-15 21:35:25 +02003672 }
3673 else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003674 goto insecure;
3675 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003676
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003677 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3678 free(s);
3679 if (str) {
3680 PDATA_PUSH(self->stack, str, -1);
3681 res = 0;
3682 }
3683 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003684
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003685 insecure:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003686 free(s);
3687 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3688 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003689}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003690
3691
3692static int
Tim Peterscba30e22003-02-01 06:24:36 +00003693load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003694{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003695 PyObject *py_string = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003696 Py_ssize_t l;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003697 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003698
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003699 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003700
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003701 l = calc_binint(s, 4);
3702 if (l < 0) {
3703 /* Corrupt or hostile pickle -- we never write one like
3704 * this.
3705 */
3706 PyErr_SetString(UnpicklingError,
3707 "BINSTRING pickle has negative byte count");
3708 return -1;
3709 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003710
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003711 if (self->read_func(self, &s, l) < 0)
3712 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003713
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003714 if (!( py_string = PyString_FromStringAndSize(s, l)))
3715 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003716
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003717 PDATA_PUSH(self->stack, py_string, -1);
3718 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003719}
3720
3721
3722static int
Tim Peterscba30e22003-02-01 06:24:36 +00003723load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003724{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003725 PyObject *py_string = 0;
3726 unsigned char l;
3727 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003728
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003729 if (self->read_func(self, &s, 1) < 0)
3730 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003731
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003732 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003733
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003734 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003735
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003736 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003737
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003738 PDATA_PUSH(self->stack, py_string, -1);
3739 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003740}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003741
3742
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003743#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003744static int
Tim Peterscba30e22003-02-01 06:24:36 +00003745load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003746{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003747 PyObject *str = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003748 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003749 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003750
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003751 if ((len = self->readline_func(self, &s)) < 0) return -1;
3752 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003753
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003754 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003755 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003756
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003757 PDATA_PUSH(self->stack, str, -1);
3758 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003759}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003760#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003761
3762
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003763#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003764static int
Tim Peterscba30e22003-02-01 06:24:36 +00003765load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003766{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003767 PyObject *unicode;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003768 Py_ssize_t l;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003769 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003770
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003771 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003772
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003773 l = calc_binint(s, 4);
3774 if (l < 0) {
3775 /* Corrupt or hostile pickle -- we never write one like
3776 * this.
3777 */
3778 PyErr_SetString(UnpicklingError,
3779 "BINUNICODE pickle has negative byte count");
3780 return -1;
3781 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003782
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003783 if (self->read_func(self, &s, l) < 0)
3784 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003785
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003786 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3787 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003788
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003789 PDATA_PUSH(self->stack, unicode, -1);
3790 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003791}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003792#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003793
3794
3795static int
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003796load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003797{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003798 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003799
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003800 if (self->stack->length < len)
3801 return stackUnderflow();
3802
3803 if (!(tup = Pdata_popTuple(self->stack, self->stack->length - len)))
3804 return -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003805 PDATA_PUSH(self->stack, tup, -1);
3806 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003807}
3808
3809static int
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003810load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003811{
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003812 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003813
Serhiy Storchaka80767a32015-11-25 15:07:49 +02003814 if ((i = marker(self)) < 0) return -1;
3815 return load_counted_tuple(self, self->stack->length - i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003816}
3817
3818static int
Tim Peterscba30e22003-02-01 06:24:36 +00003819load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003820{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003821 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003822
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003823 if (!( list=PyList_New(0))) return -1;
3824 PDATA_PUSH(self->stack, list, -1);
3825 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003826}
3827
3828static int
Tim Peterscba30e22003-02-01 06:24:36 +00003829load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003830{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003831 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003832
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003833 if (!( dict=PyDict_New())) return -1;
3834 PDATA_PUSH(self->stack, dict, -1);
3835 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003836}
3837
3838
3839static int
Tim Peterscba30e22003-02-01 06:24:36 +00003840load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003841{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003842 PyObject *list = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003843 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003844
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003845 if ((i = marker(self)) < 0) return -1;
3846 if (!( list=Pdata_popList(self->stack, i))) return -1;
3847 PDATA_PUSH(self->stack, list, -1);
3848 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003849}
3850
3851static int
Tim Peterscba30e22003-02-01 06:24:36 +00003852load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003853{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003854 PyObject *dict, *key, *value;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003855 Py_ssize_t i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003856
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003857 if ((i = marker(self)) < 0) return -1;
3858 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003859
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003860 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003861
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003862 for (k = i+1; k < j; k += 2) {
3863 key =self->stack->data[k-1];
3864 value=self->stack->data[k ];
3865 if (PyDict_SetItem(dict, key, value) < 0) {
3866 Py_DECREF(dict);
3867 return -1;
3868 }
3869 }
3870 Pdata_clear(self->stack, i);
3871 PDATA_PUSH(self->stack, dict, -1);
3872 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003873}
3874
3875static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003876Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003877{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003878 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003879
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003880 if (PyClass_Check(cls)) {
3881 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003882
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003883 if ((l=PyObject_Size(args)) < 0) goto err;
3884 if (!( l )) {
3885 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003886
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003887 __getinitargs__ = PyObject_GetAttr(cls,
3888 __getinitargs___str);
3889 if (!__getinitargs__) {
3890 /* We have a class with no __getinitargs__,
3891 so bypass usual construction */
3892 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003893
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003894 PyErr_Clear();
3895 if (!( inst=PyInstance_NewRaw(cls, NULL)))
3896 goto err;
3897 return inst;
3898 }
3899 Py_DECREF(__getinitargs__);
3900 }
Tim Peters84e87f32001-03-17 04:50:51 +00003901
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003902 if ((r=PyInstance_New(cls, args, NULL))) return r;
3903 else goto err;
3904 }
Tim Peters84e87f32001-03-17 04:50:51 +00003905
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003906 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003907
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003908 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003909 {
3910 PyObject *tp, *v, *tb, *tmp_value;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003911
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003912 PyErr_Fetch(&tp, &v, &tb);
3913 tmp_value = v;
3914 /* NULL occurs when there was a KeyboardInterrupt */
3915 if (tmp_value == NULL)
3916 tmp_value = Py_None;
3917 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3918 Py_XDECREF(v);
3919 v=r;
3920 }
3921 PyErr_Restore(tp,v,tb);
3922 }
3923 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003924}
Tim Peters84e87f32001-03-17 04:50:51 +00003925
Guido van Rossum60456fd1997-04-09 17:36:32 +00003926
3927static int
Tim Peterscba30e22003-02-01 06:24:36 +00003928load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003929{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003930 PyObject *class, *tup, *obj=0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003931 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003932
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003933 if ((i = marker(self)) < 0) return -1;
Serhiy Storchaka5c137662015-11-23 15:20:43 +02003934
3935 if (self->stack->length - i < 1)
3936 return stackUnderflow();
3937
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003938 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3939 PDATA_POP(self->stack, class);
3940 if (class) {
3941 obj = Instance_New(class, tup);
3942 Py_DECREF(class);
3943 }
3944 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003945
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003946 if (! obj) return -1;
3947 PDATA_PUSH(self->stack, obj, -1);
3948 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003949}
3950
3951
3952static int
Tim Peterscba30e22003-02-01 06:24:36 +00003953load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003954{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003955 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003956 Py_ssize_t i, len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003957 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003958
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003959 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003960
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003961 if ((len = self->readline_func(self, &s)) < 0) return -1;
3962 if (len < 2) return bad_readline();
3963 module_name = PyString_FromStringAndSize(s, len - 1);
3964 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003965
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003966 if ((len = self->readline_func(self, &s)) >= 0) {
Serhiy Storchaka048e1072015-12-01 00:32:49 +02003967 if (len < 2) {
3968 Py_DECREF(module_name);
3969 return bad_readline();
3970 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003971 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3972 class = find_class(module_name, class_name,
3973 self->find_class);
3974 Py_DECREF(class_name);
3975 }
3976 }
3977 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003978
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003979 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003980
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003981 if ((tup=Pdata_popTuple(self->stack, i))) {
3982 obj = Instance_New(class, tup);
3983 Py_DECREF(tup);
3984 }
3985 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003986
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003987 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003988
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003989 PDATA_PUSH(self->stack, obj, -1);
3990 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003991}
3992
Tim Peterseab7db32003-02-13 18:24:14 +00003993static int
3994load_newobj(Unpicklerobject *self)
3995{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003996 PyObject *args = NULL;
3997 PyObject *clsraw = NULL;
3998 PyTypeObject *cls; /* clsraw cast to its true type */
3999 PyObject *obj;
Tim Peterseab7db32003-02-13 18:24:14 +00004000
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004001 /* Stack is ... cls argtuple, and we want to call
4002 * cls.__new__(cls, *argtuple).
4003 */
4004 PDATA_POP(self->stack, args);
4005 if (args == NULL) goto Fail;
4006 if (! PyTuple_Check(args)) {
4007 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
4008 "tuple.");
4009 goto Fail;
4010 }
Tim Peterseab7db32003-02-13 18:24:14 +00004011
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004012 PDATA_POP(self->stack, clsraw);
4013 cls = (PyTypeObject *)clsraw;
4014 if (cls == NULL) goto Fail;
4015 if (! PyType_Check(cls)) {
4016 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
4017 "isn't a type object");
4018 goto Fail;
4019 }
4020 if (cls->tp_new == NULL) {
4021 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
4022 "has NULL tp_new");
4023 goto Fail;
4024 }
Tim Peterseab7db32003-02-13 18:24:14 +00004025
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004026 /* Call __new__. */
4027 obj = cls->tp_new(cls, args, NULL);
4028 if (obj == NULL) goto Fail;
Tim Peterseab7db32003-02-13 18:24:14 +00004029
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004030 Py_DECREF(args);
4031 Py_DECREF(clsraw);
4032 PDATA_PUSH(self->stack, obj, -1);
4033 return 0;
Tim Peterseab7db32003-02-13 18:24:14 +00004034
4035 Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004036 Py_XDECREF(args);
4037 Py_XDECREF(clsraw);
4038 return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004039}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004040
4041static int
Tim Peterscba30e22003-02-01 06:24:36 +00004042load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004043{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004044 PyObject *class = 0, *module_name = 0, *class_name = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004045 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004046 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004047
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004048 if ((len = self->readline_func(self, &s)) < 0) return -1;
4049 if (len < 2) return bad_readline();
4050 module_name = PyString_FromStringAndSize(s, len - 1);
4051 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004052
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004053 if ((len = self->readline_func(self, &s)) >= 0) {
4054 if (len < 2) {
4055 Py_DECREF(module_name);
4056 return bad_readline();
4057 }
4058 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
4059 class = find_class(module_name, class_name,
4060 self->find_class);
4061 Py_DECREF(class_name);
4062 }
4063 }
4064 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004065
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004066 if (! class) return -1;
4067 PDATA_PUSH(self->stack, class, -1);
4068 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004069}
4070
4071
4072static int
Tim Peterscba30e22003-02-01 06:24:36 +00004073load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004074{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004075 PyObject *pid = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004076 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004077 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004078
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004079 if (self->pers_func) {
4080 if ((len = self->readline_func(self, &s)) < 0) return -1;
4081 if (len < 2) return bad_readline();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004082
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004083 pid = PyString_FromStringAndSize(s, len - 1);
4084 if (!pid) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004085
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004086 if (PyList_Check(self->pers_func)) {
4087 if (PyList_Append(self->pers_func, pid) < 0) {
4088 Py_DECREF(pid);
4089 return -1;
4090 }
4091 }
4092 else {
4093 ARG_TUP(self, pid);
4094 if (self->arg) {
4095 pid = PyObject_Call(self->pers_func, self->arg,
4096 NULL);
4097 FREE_ARG_TUP(self);
4098 }
4099 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004100
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004101 if (! pid) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004102
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004103 PDATA_PUSH(self->stack, pid, -1);
4104 return 0;
4105 }
4106 else {
4107 PyErr_SetString(UnpicklingError,
4108 "A load persistent id instruction was encountered,\n"
4109 "but no persistent_load function was specified.");
4110 return -1;
4111 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004112}
4113
4114static int
Tim Peterscba30e22003-02-01 06:24:36 +00004115load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004116{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004117 PyObject *pid = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004118
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004119 if (self->pers_func) {
4120 PDATA_POP(self->stack, pid);
4121 if (! pid) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004122
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004123 if (PyList_Check(self->pers_func)) {
4124 if (PyList_Append(self->pers_func, pid) < 0) {
4125 Py_DECREF(pid);
4126 return -1;
4127 }
4128 }
4129 else {
4130 ARG_TUP(self, pid);
4131 if (self->arg) {
4132 pid = PyObject_Call(self->pers_func, self->arg,
4133 NULL);
4134 FREE_ARG_TUP(self);
4135 }
4136 if (! pid) return -1;
4137 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004138
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004139 PDATA_PUSH(self->stack, pid, -1);
4140 return 0;
4141 }
4142 else {
4143 PyErr_SetString(UnpicklingError,
4144 "A load persistent id instruction was encountered,\n"
4145 "but no persistent_load function was specified.");
4146 return -1;
4147 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004148}
4149
4150
4151static int
Tim Peterscba30e22003-02-01 06:24:36 +00004152load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004153{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004154 Py_ssize_t len = self->stack->length;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004155
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004156 /* Note that we split the (pickle.py) stack into two stacks,
4157 an object stack and a mark stack. We have to be clever and
4158 pop the right one. We do this by looking at the top of the
4159 mark stack first, and only signalling a stack underflow if
4160 the object stack is empty and the mark stack doesn't match
4161 our expectations.
4162 */
4163 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
4164 self->num_marks--;
4165 } else if (len > 0) {
4166 len--;
4167 Py_DECREF(self->stack->data[len]);
4168 self->stack->length = len;
4169 } else {
4170 return stackUnderflow();
4171 }
4172 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004173}
4174
4175
4176static int
Tim Peterscba30e22003-02-01 06:24:36 +00004177load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004178{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004179 Py_ssize_t i;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004180
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004181 if ((i = marker(self)) < 0)
4182 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004183
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004184 Pdata_clear(self->stack, i);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004185
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004186 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004187}
4188
4189
4190static int
Tim Peterscba30e22003-02-01 06:24:36 +00004191load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004192{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004193 PyObject *last;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004194 Py_ssize_t len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004195
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004196 if ((len = self->stack->length) <= 0) return stackUnderflow();
4197 last=self->stack->data[len-1];
4198 Py_INCREF(last);
4199 PDATA_PUSH(self->stack, last, -1);
4200 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004201}
4202
4203
4204static int
Tim Peterscba30e22003-02-01 06:24:36 +00004205load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004206{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004207 PyObject *py_str = 0, *value = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004208 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004209 char *s;
4210 int rc;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004211
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004212 if ((len = self->readline_func(self, &s)) < 0) return -1;
4213 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00004214
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004215 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004216
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004217 value = PyDict_GetItem(self->memo, py_str);
4218 if (! value) {
4219 PyErr_SetObject(BadPickleGet, py_str);
4220 rc = -1;
4221 }
4222 else {
4223 PDATA_APPEND(self->stack, value, -1);
4224 rc = 0;
4225 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004226
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004227 Py_DECREF(py_str);
4228 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004229}
4230
4231
4232static int
Tim Peterscba30e22003-02-01 06:24:36 +00004233load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004234{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004235 PyObject *py_key = 0, *value = 0;
4236 unsigned char key;
4237 char *s;
4238 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004239
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004240 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004241
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004242 key = (unsigned char)s[0];
4243 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00004244
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004245 value = PyDict_GetItem(self->memo, py_key);
4246 if (! value) {
4247 PyErr_SetObject(BadPickleGet, py_key);
4248 rc = -1;
4249 }
4250 else {
4251 PDATA_APPEND(self->stack, value, -1);
4252 rc = 0;
4253 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004254
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004255 Py_DECREF(py_key);
4256 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004257}
4258
4259
4260static int
Tim Peterscba30e22003-02-01 06:24:36 +00004261load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004262{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004263 PyObject *py_key = 0, *value = 0;
4264 unsigned char c;
4265 char *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004266 Py_ssize_t key;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004267 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004268
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004269 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004270
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004271 c = (unsigned char)s[0];
4272 key = (long)c;
4273 c = (unsigned char)s[1];
4274 key |= (long)c << 8;
4275 c = (unsigned char)s[2];
4276 key |= (long)c << 16;
4277 c = (unsigned char)s[3];
4278 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004279
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004280 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004281
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004282 value = PyDict_GetItem(self->memo, py_key);
4283 if (! value) {
4284 PyErr_SetObject(BadPickleGet, py_key);
4285 rc = -1;
4286 }
4287 else {
4288 PDATA_APPEND(self->stack, value, -1);
4289 rc = 0;
4290 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004291
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004292 Py_DECREF(py_key);
4293 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004294}
4295
Tim Peters2d629652003-02-04 05:06:17 +00004296/* Push an object from the extension registry (EXT[124]). nbytes is
4297 * the number of bytes following the opcode, holding the index (code) value.
4298 */
4299static int
4300load_extension(Unpicklerobject *self, int nbytes)
4301{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004302 char *codebytes; /* the nbytes bytes after the opcode */
4303 long code; /* calc_binint returns long */
4304 PyObject *py_code; /* code as a Python int */
4305 PyObject *obj; /* the object to push */
4306 PyObject *pair; /* (module_name, class_name) */
4307 PyObject *module_name, *class_name;
Tim Peters2d629652003-02-04 05:06:17 +00004308
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004309 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4310 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4311 code = calc_binint(codebytes, nbytes);
4312 if (code <= 0) { /* note that 0 is forbidden */
4313 /* Corrupt or hostile pickle. */
4314 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4315 return -1;
4316 }
Tim Peters2d629652003-02-04 05:06:17 +00004317
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004318 /* Look for the code in the cache. */
4319 py_code = PyInt_FromLong(code);
4320 if (py_code == NULL) return -1;
4321 obj = PyDict_GetItem(extension_cache, py_code);
4322 if (obj != NULL) {
4323 /* Bingo. */
4324 Py_DECREF(py_code);
4325 PDATA_APPEND(self->stack, obj, -1);
4326 return 0;
4327 }
Tim Peters2d629652003-02-04 05:06:17 +00004328
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004329 /* Look up the (module_name, class_name) pair. */
4330 pair = PyDict_GetItem(inverted_registry, py_code);
4331 if (pair == NULL) {
4332 Py_DECREF(py_code);
4333 PyErr_Format(PyExc_ValueError, "unregistered extension "
4334 "code %ld", code);
4335 return -1;
4336 }
4337 /* Since the extension registry is manipulable via Python code,
4338 * confirm that pair is really a 2-tuple of strings.
4339 */
4340 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4341 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4342 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4343 Py_DECREF(py_code);
4344 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4345 "isn't a 2-tuple of strings", code);
4346 return -1;
4347 }
4348 /* Load the object. */
4349 obj = find_class(module_name, class_name, self->find_class);
4350 if (obj == NULL) {
4351 Py_DECREF(py_code);
4352 return -1;
4353 }
4354 /* Cache code -> obj. */
4355 code = PyDict_SetItem(extension_cache, py_code, obj);
4356 Py_DECREF(py_code);
4357 if (code < 0) {
4358 Py_DECREF(obj);
4359 return -1;
4360 }
4361 PDATA_PUSH(self->stack, obj, -1);
4362 return 0;
Tim Peters2d629652003-02-04 05:06:17 +00004363}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004364
4365static int
Tim Peterscba30e22003-02-01 06:24:36 +00004366load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004367{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004368 PyObject *py_str = 0, *value = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004369 Py_ssize_t len, l;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004370 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004371
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004372 if ((l = self->readline_func(self, &s)) < 0) return -1;
4373 if (l < 2) return bad_readline();
4374 if (!( len=self->stack->length )) return stackUnderflow();
4375 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4376 value=self->stack->data[len-1];
4377 l=PyDict_SetItem(self->memo, py_str, value);
4378 Py_DECREF(py_str);
4379 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004380}
4381
4382
4383static int
Tim Peterscba30e22003-02-01 06:24:36 +00004384load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004385{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004386 PyObject *py_key = 0, *value = 0;
4387 unsigned char key;
4388 char *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004389 Py_ssize_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004390
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004391 if (self->read_func(self, &s, 1) < 0) return -1;
4392 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004393
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004394 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004395
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004396 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4397 value=self->stack->data[len-1];
4398 len=PyDict_SetItem(self->memo, py_key, value);
4399 Py_DECREF(py_key);
4400 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004401}
4402
4403
4404static int
Tim Peterscba30e22003-02-01 06:24:36 +00004405load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004406{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004407 PyObject *py_key = 0, *value = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004408 Py_ssize_t key;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004409 unsigned char c;
4410 char *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004411 Py_ssize_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004412
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004413 if (self->read_func(self, &s, 4) < 0) return -1;
4414 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004415
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004416 c = (unsigned char)s[0];
4417 key = (long)c;
4418 c = (unsigned char)s[1];
4419 key |= (long)c << 8;
4420 c = (unsigned char)s[2];
4421 key |= (long)c << 16;
4422 c = (unsigned char)s[3];
4423 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004424
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004425 if (!( py_key = PyInt_FromLong(key))) return -1;
4426 value=self->stack->data[len-1];
4427 len=PyDict_SetItem(self->memo, py_key, value);
4428 Py_DECREF(py_key);
4429 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004430}
4431
4432
4433static int
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004434do_append(Unpicklerobject *self, Py_ssize_t x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004435{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004436 PyObject *value = 0, *list = 0, *append_method = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004437 Py_ssize_t len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004438
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004439 len=self->stack->length;
4440 if (!( len >= x && x > 0 )) return stackUnderflow();
4441 /* nothing to do */
4442 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004443
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004444 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004445
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004446 if (PyList_Check(list)) {
4447 PyObject *slice;
4448 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004449
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004450 slice=Pdata_popList(self->stack, x);
4451 if (! slice) return -1;
4452 list_len = PyList_GET_SIZE(list);
4453 i=PyList_SetSlice(list, list_len, list_len, slice);
4454 Py_DECREF(slice);
4455 return i;
4456 }
4457 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004458
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004459 if (!( append_method = PyObject_GetAttr(list, append_str)))
4460 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004461
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004462 for (i = x; i < len; i++) {
4463 PyObject *junk;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004464
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004465 value=self->stack->data[i];
4466 junk=0;
4467 ARG_TUP(self, value);
4468 if (self->arg) {
4469 junk = PyObject_Call(append_method, self->arg,
4470 NULL);
4471 FREE_ARG_TUP(self);
4472 }
4473 if (! junk) {
4474 Pdata_clear(self->stack, i+1);
4475 self->stack->length=x;
4476 Py_DECREF(append_method);
4477 return -1;
4478 }
4479 Py_DECREF(junk);
4480 }
4481 self->stack->length=x;
4482 Py_DECREF(append_method);
4483 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004484
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004485 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004486}
4487
4488
4489static int
Tim Peterscba30e22003-02-01 06:24:36 +00004490load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004491{
Serhiy Storchaka5c137662015-11-23 15:20:43 +02004492 if (self->stack->length - 1 <= 0)
4493 return stackUnderflow();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004494 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004495}
4496
4497
4498static int
Tim Peterscba30e22003-02-01 06:24:36 +00004499load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004500{
Serhiy Storchaka5c137662015-11-23 15:20:43 +02004501 Py_ssize_t i = marker(self);
4502 if (i < 0)
4503 return -1;
4504 return do_append(self, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004505}
4506
4507
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004508static Py_ssize_t
4509do_setitems(Unpicklerobject *self, Py_ssize_t x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004510{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004511 PyObject *value = 0, *key = 0, *dict = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004512 Py_ssize_t len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004513
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004514 if (!( (len=self->stack->length) >= x
4515 && x > 0 )) return stackUnderflow();
Serhiy Storchaka5c137662015-11-23 15:20:43 +02004516 if (len == x) /* nothing to do */
4517 return 0;
4518 if ((len - x) % 2 != 0) {
4519 /* Currupt or hostile pickle -- we never write one like this. */
4520 PyErr_SetString(UnpicklingError,
4521 "odd number of items for SETITEMS");
4522 return -1;
4523 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004524
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004525 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004526
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004527 for (i = x+1; i < len; i += 2) {
4528 key =self->stack->data[i-1];
4529 value=self->stack->data[i ];
4530 if (PyObject_SetItem(dict, key, value) < 0) {
4531 r=-1;
4532 break;
4533 }
4534 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004535
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004536 Pdata_clear(self->stack, x);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004537
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004538 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004539}
4540
4541
Tim Peters84e87f32001-03-17 04:50:51 +00004542static int
Tim Peterscba30e22003-02-01 06:24:36 +00004543load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004544{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004545 return do_setitems(self, self->stack->length - 2);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004546}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004547
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004548static int
Tim Peterscba30e22003-02-01 06:24:36 +00004549load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004550{
Serhiy Storchaka5c137662015-11-23 15:20:43 +02004551 Py_ssize_t i = marker(self);
4552 if (i < 0)
4553 return -1;
4554 return do_setitems(self, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004555}
4556
Tim Peters84e87f32001-03-17 04:50:51 +00004557
Guido van Rossum60456fd1997-04-09 17:36:32 +00004558static int
Tim Peterscba30e22003-02-01 06:24:36 +00004559load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004560{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004561 PyObject *state, *inst, *slotstate;
4562 PyObject *__setstate__;
4563 PyObject *d_key, *d_value;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004564 int res = -1;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004565 Py_ssize_t i;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004566
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004567 /* Stack is ... instance, state. We want to leave instance at
4568 * the stack top, possibly mutated via instance.__setstate__(state).
4569 */
4570 if (self->stack->length < 2)
4571 return stackUnderflow();
4572 PDATA_POP(self->stack, state);
4573 if (state == NULL)
4574 return -1;
4575 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004576
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004577 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4578 if (__setstate__ != NULL) {
4579 PyObject *junk = NULL;
Tim Peters080c88b2003-02-15 03:01:11 +00004580
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004581 /* The explicit __setstate__ is responsible for everything. */
4582 ARG_TUP(self, state);
4583 if (self->arg) {
4584 junk = PyObject_Call(__setstate__, self->arg, NULL);
4585 FREE_ARG_TUP(self);
4586 }
4587 Py_DECREF(__setstate__);
4588 if (junk == NULL)
4589 return -1;
4590 Py_DECREF(junk);
4591 return 0;
4592 }
4593 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4594 return -1;
4595 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004596
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004597 /* A default __setstate__. First see whether state embeds a
4598 * slot state dict too (a proto 2 addition).
4599 */
4600 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4601 PyObject *temp = state;
4602 state = PyTuple_GET_ITEM(temp, 0);
4603 slotstate = PyTuple_GET_ITEM(temp, 1);
4604 Py_INCREF(state);
4605 Py_INCREF(slotstate);
4606 Py_DECREF(temp);
4607 }
4608 else
4609 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004610
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004611 /* Set inst.__dict__ from the state dict (if any). */
4612 if (state != Py_None) {
4613 PyObject *dict;
4614 if (! PyDict_Check(state)) {
4615 PyErr_SetString(UnpicklingError, "state is not a "
4616 "dictionary");
4617 goto finally;
4618 }
4619 dict = PyObject_GetAttr(inst, __dict___str);
4620 if (dict == NULL)
4621 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004622
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004623 i = 0;
4624 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4625 /* normally the keys for instance attributes are
4626 interned. we should try to do that here. */
4627 Py_INCREF(d_key);
4628 if (PyString_CheckExact(d_key))
4629 PyString_InternInPlace(&d_key);
4630 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
4631 Py_DECREF(d_key);
4632 goto finally;
4633 }
4634 Py_DECREF(d_key);
4635 }
4636 Py_DECREF(dict);
4637 }
Tim Peters080c88b2003-02-15 03:01:11 +00004638
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004639 /* Also set instance attributes from the slotstate dict (if any). */
4640 if (slotstate != NULL) {
4641 if (! PyDict_Check(slotstate)) {
4642 PyErr_SetString(UnpicklingError, "slot state is not "
4643 "a dictionary");
4644 goto finally;
4645 }
4646 i = 0;
4647 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4648 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4649 goto finally;
4650 }
4651 }
4652 res = 0;
Tim Peters080c88b2003-02-15 03:01:11 +00004653
4654 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004655 Py_DECREF(state);
4656 Py_XDECREF(slotstate);
4657 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004658}
4659
4660
4661static int
Tim Peterscba30e22003-02-01 06:24:36 +00004662load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004663{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004664 Py_ssize_t s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004665
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004666 /* Note that we split the (pickle.py) stack into two stacks, an
4667 object stack and a mark stack. Here we push a mark onto the
4668 mark stack.
4669 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004670
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004671 if ((self->num_marks + 1) >= self->marks_size) {
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004672 Py_ssize_t *marks;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004673 s=self->marks_size+20;
4674 if (s <= self->num_marks) s=self->num_marks + 1;
4675 if (self->marks == NULL)
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004676 marks=(Py_ssize_t *)malloc(s * sizeof(Py_ssize_t));
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004677 else
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004678 marks=(Py_ssize_t *)realloc(self->marks,
4679 s * sizeof(Py_ssize_t));
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004680 if (!marks) {
4681 PyErr_NoMemory();
4682 return -1;
4683 }
4684 self->marks = marks;
4685 self->marks_size = s;
4686 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004687
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004688 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004689
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004690 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004691}
4692
Guido van Rossum60456fd1997-04-09 17:36:32 +00004693static int
Tim Peterscba30e22003-02-01 06:24:36 +00004694load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004695{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004696 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004697
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004698 PDATA_POP(self->stack, arg_tup);
4699 if (! arg_tup) return -1;
4700 PDATA_POP(self->stack, callable);
4701 if (callable) {
4702 ob = Instance_New(callable, arg_tup);
4703 Py_DECREF(callable);
4704 }
4705 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004706
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004707 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004708
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004709 PDATA_PUSH(self->stack, ob, -1);
4710 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004711}
Tim Peters84e87f32001-03-17 04:50:51 +00004712
Tim Peters4190fb82003-02-02 16:09:05 +00004713/* Just raises an error if we don't know the protocol specified. PROTO
4714 * is the first opcode for protocols >= 2.
4715 */
4716static int
4717load_proto(Unpicklerobject *self)
4718{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004719 int i;
4720 char *protobyte;
Tim Peters4190fb82003-02-02 16:09:05 +00004721
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004722 i = self->read_func(self, &protobyte, 1);
4723 if (i < 0)
4724 return -1;
Tim Peters4190fb82003-02-02 16:09:05 +00004725
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004726 i = calc_binint(protobyte, 1);
4727 /* No point checking for < 0, since calc_binint returns an unsigned
4728 * int when chewing on 1 byte.
4729 */
4730 assert(i >= 0);
4731 if (i <= HIGHEST_PROTOCOL)
4732 return 0;
Tim Peters4190fb82003-02-02 16:09:05 +00004733
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004734 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4735 return -1;
Tim Peters4190fb82003-02-02 16:09:05 +00004736}
4737
Guido van Rossum60456fd1997-04-09 17:36:32 +00004738static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004739load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004740{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004741 PyObject *err = 0, *val = 0;
4742 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004743
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004744 self->num_marks = 0;
4745 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004746
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004747 while (1) {
4748 if (self->read_func(self, &s, 1) < 0)
4749 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004750
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004751 switch (s[0]) {
4752 case NONE:
4753 if (load_none(self) < 0)
4754 break;
4755 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004756
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004757 case BININT:
4758 if (load_binint(self) < 0)
4759 break;
4760 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004761
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004762 case BININT1:
4763 if (load_binint1(self) < 0)
4764 break;
4765 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004766
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004767 case BININT2:
4768 if (load_binint2(self) < 0)
4769 break;
4770 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004771
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004772 case INT:
4773 if (load_int(self) < 0)
4774 break;
4775 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004776
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004777 case LONG:
4778 if (load_long(self) < 0)
4779 break;
4780 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004781
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004782 case LONG1:
4783 if (load_counted_long(self, 1) < 0)
4784 break;
4785 continue;
Tim Petersee1a53c2003-02-02 02:57:53 +00004786
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004787 case LONG4:
4788 if (load_counted_long(self, 4) < 0)
4789 break;
4790 continue;
Tim Petersee1a53c2003-02-02 02:57:53 +00004791
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004792 case FLOAT:
4793 if (load_float(self) < 0)
4794 break;
4795 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004796
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004797 case BINFLOAT:
4798 if (load_binfloat(self) < 0)
4799 break;
4800 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004801
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004802 case BINSTRING:
4803 if (load_binstring(self) < 0)
4804 break;
4805 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004806
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004807 case SHORT_BINSTRING:
4808 if (load_short_binstring(self) < 0)
4809 break;
4810 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004811
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004812 case STRING:
4813 if (load_string(self) < 0)
4814 break;
4815 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004816
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004817#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004818 case UNICODE:
4819 if (load_unicode(self) < 0)
4820 break;
4821 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004822
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004823 case BINUNICODE:
4824 if (load_binunicode(self) < 0)
4825 break;
4826 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004827#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004828
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004829 case EMPTY_TUPLE:
4830 if (load_counted_tuple(self, 0) < 0)
4831 break;
4832 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00004833
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004834 case TUPLE1:
4835 if (load_counted_tuple(self, 1) < 0)
4836 break;
4837 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00004838
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004839 case TUPLE2:
4840 if (load_counted_tuple(self, 2) < 0)
4841 break;
4842 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00004843
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004844 case TUPLE3:
4845 if (load_counted_tuple(self, 3) < 0)
4846 break;
4847 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004848
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004849 case TUPLE:
4850 if (load_tuple(self) < 0)
4851 break;
4852 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004853
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004854 case EMPTY_LIST:
4855 if (load_empty_list(self) < 0)
4856 break;
4857 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004858
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004859 case LIST:
4860 if (load_list(self) < 0)
4861 break;
4862 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004863
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004864 case EMPTY_DICT:
4865 if (load_empty_dict(self) < 0)
4866 break;
4867 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004868
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004869 case DICT:
4870 if (load_dict(self) < 0)
4871 break;
4872 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004873
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004874 case OBJ:
4875 if (load_obj(self) < 0)
4876 break;
4877 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004878
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004879 case INST:
4880 if (load_inst(self) < 0)
4881 break;
4882 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004883
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004884 case NEWOBJ:
4885 if (load_newobj(self) < 0)
4886 break;
4887 continue;
Tim Peterseab7db32003-02-13 18:24:14 +00004888
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004889 case GLOBAL:
4890 if (load_global(self) < 0)
4891 break;
4892 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004893
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004894 case APPEND:
4895 if (load_append(self) < 0)
4896 break;
4897 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004898
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004899 case APPENDS:
4900 if (load_appends(self) < 0)
4901 break;
4902 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004903
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004904 case BUILD:
4905 if (load_build(self) < 0)
4906 break;
4907 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004908
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004909 case DUP:
4910 if (load_dup(self) < 0)
4911 break;
4912 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004913
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004914 case BINGET:
4915 if (load_binget(self) < 0)
4916 break;
4917 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004918
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004919 case LONG_BINGET:
4920 if (load_long_binget(self) < 0)
4921 break;
4922 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004923
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004924 case GET:
4925 if (load_get(self) < 0)
4926 break;
4927 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004928
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004929 case EXT1:
4930 if (load_extension(self, 1) < 0)
4931 break;
4932 continue;
Tim Peters2d629652003-02-04 05:06:17 +00004933
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004934 case EXT2:
4935 if (load_extension(self, 2) < 0)
4936 break;
4937 continue;
Tim Peters2d629652003-02-04 05:06:17 +00004938
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004939 case EXT4:
4940 if (load_extension(self, 4) < 0)
4941 break;
4942 continue;
4943 case MARK:
4944 if (load_mark(self) < 0)
4945 break;
4946 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004947
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004948 case BINPUT:
4949 if (load_binput(self) < 0)
4950 break;
4951 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004952
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004953 case LONG_BINPUT:
4954 if (load_long_binput(self) < 0)
4955 break;
4956 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004957
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004958 case PUT:
4959 if (load_put(self) < 0)
4960 break;
4961 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004962
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004963 case POP:
4964 if (load_pop(self) < 0)
4965 break;
4966 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004967
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004968 case POP_MARK:
4969 if (load_pop_mark(self) < 0)
4970 break;
4971 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004972
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004973 case SETITEM:
4974 if (load_setitem(self) < 0)
4975 break;
4976 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004977
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004978 case SETITEMS:
4979 if (load_setitems(self) < 0)
4980 break;
4981 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004982
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004983 case STOP:
4984 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004985
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004986 case PERSID:
4987 if (load_persid(self) < 0)
4988 break;
4989 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004990
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004991 case BINPERSID:
4992 if (load_binpersid(self) < 0)
4993 break;
4994 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004995
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004996 case REDUCE:
4997 if (load_reduce(self) < 0)
4998 break;
4999 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005000
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005001 case PROTO:
5002 if (load_proto(self) < 0)
5003 break;
5004 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005005
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005006 case NEWTRUE:
5007 if (load_bool(self, Py_True) < 0)
5008 break;
5009 continue;
Tim Peters3c67d792003-02-02 17:59:11 +00005010
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005011 case NEWFALSE:
5012 if (load_bool(self, Py_False) < 0)
5013 break;
5014 continue;
Tim Peters3c67d792003-02-02 17:59:11 +00005015
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005016 case '\0':
5017 /* end of file */
5018 PyErr_SetNone(PyExc_EOFError);
5019 break;
Tim Peterscba30e22003-02-01 06:24:36 +00005020
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005021 default:
5022 cPickle_ErrFormat(UnpicklingError,
5023 "invalid load key, '%s'.",
5024 "c", s[0]);
5025 return NULL;
5026 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005027
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005028 break;
5029 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005031 if ((err = PyErr_Occurred())) {
5032 if (err == PyExc_EOFError) {
5033 PyErr_SetNone(PyExc_EOFError);
5034 }
5035 return NULL;
5036 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005037
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005038 PDATA_POP(self->stack, val);
5039 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005040}
Tim Peters84e87f32001-03-17 04:50:51 +00005041
Guido van Rossum60456fd1997-04-09 17:36:32 +00005042
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005043/* No-load functions to support noload, which is used to
5044 find persistent references. */
5045
5046static int
Tim Peterscba30e22003-02-01 06:24:36 +00005047noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005048{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005049 Py_ssize_t i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005050
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005051 if ((i = marker(self)) < 0) return -1;
5052 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005053}
5054
5055
5056static int
Tim Peterscba30e22003-02-01 06:24:36 +00005057noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005058{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005059 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005060 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005061
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005062 if ((i = marker(self)) < 0) return -1;
5063 Pdata_clear(self->stack, i);
5064 if (self->readline_func(self, &s) < 0) return -1;
5065 if (self->readline_func(self, &s) < 0) return -1;
5066 PDATA_APPEND(self->stack, Py_None, -1);
5067 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005068}
5069
5070static int
Tim Peterseab7db32003-02-13 18:24:14 +00005071noload_newobj(Unpicklerobject *self)
5072{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005073 PyObject *obj;
Tim Peterseab7db32003-02-13 18:24:14 +00005074
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005075 PDATA_POP(self->stack, obj); /* pop argtuple */
5076 if (obj == NULL) return -1;
5077 Py_DECREF(obj);
Tim Peterseab7db32003-02-13 18:24:14 +00005078
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005079 PDATA_POP(self->stack, obj); /* pop cls */
5080 if (obj == NULL) return -1;
5081 Py_DECREF(obj);
Tim Peterseab7db32003-02-13 18:24:14 +00005082
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005083 PDATA_APPEND(self->stack, Py_None, -1);
5084 return 0;
Tim Peterseab7db32003-02-13 18:24:14 +00005085}
5086
5087static int
Tim Peterscba30e22003-02-01 06:24:36 +00005088noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005089{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005090 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005091
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005092 if (self->readline_func(self, &s) < 0) return -1;
5093 if (self->readline_func(self, &s) < 0) return -1;
5094 PDATA_APPEND(self->stack, Py_None,-1);
5095 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005096}
5097
5098static int
Tim Peterscba30e22003-02-01 06:24:36 +00005099noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005100{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005101
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005102 if (self->stack->length < 2) return stackUnderflow();
5103 Pdata_clear(self->stack, self->stack->length-2);
5104 PDATA_APPEND(self->stack, Py_None,-1);
5105 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005106}
5107
5108static int
5109noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005110
Guido van Rossum053b8df1998-11-25 16:18:00 +00005111 if (self->stack->length < 1) return stackUnderflow();
5112 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00005113 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005114}
5115
Tim Peters2d629652003-02-04 05:06:17 +00005116static int
5117noload_extension(Unpicklerobject *self, int nbytes)
5118{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005119 char *codebytes;
Tim Peters2d629652003-02-04 05:06:17 +00005120
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005121 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
5122 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
5123 PDATA_APPEND(self->stack, Py_None, -1);
5124 return 0;
Tim Peters2d629652003-02-04 05:06:17 +00005125}
5126
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005127static int
5128noload_append(Unpicklerobject *self)
5129{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005130 return Pdata_clear(self->stack, self->stack->length - 1);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005131}
5132
5133static int
5134noload_appends(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}
5140
5141static int
5142noload_setitem(Unpicklerobject *self)
5143{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005144 return Pdata_clear(self->stack, self->stack->length - 2);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005145}
5146
5147static int
5148noload_setitems(Unpicklerobject *self)
5149{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005150 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005151 if ((i = marker(self)) < 0) return -1;
5152 return Pdata_clear(self->stack, i);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005153}
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005154
5155static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005156noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005157{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005158 PyObject *err = 0, *val = 0;
5159 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005160
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005161 self->num_marks = 0;
5162 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005163
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005164 while (1) {
5165 if (self->read_func(self, &s, 1) < 0)
5166 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005167
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005168 switch (s[0]) {
5169 case NONE:
5170 if (load_none(self) < 0)
5171 break;
5172 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005173
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005174 case BININT:
5175 if (load_binint(self) < 0)
5176 break;
5177 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005178
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005179 case BININT1:
5180 if (load_binint1(self) < 0)
5181 break;
5182 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005183
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005184 case BININT2:
5185 if (load_binint2(self) < 0)
5186 break;
5187 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005188
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005189 case INT:
5190 if (load_int(self) < 0)
5191 break;
5192 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005193
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005194 case LONG:
5195 if (load_long(self) < 0)
5196 break;
5197 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005198
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005199 case LONG1:
5200 if (load_counted_long(self, 1) < 0)
5201 break;
5202 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005203
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005204 case LONG4:
5205 if (load_counted_long(self, 4) < 0)
5206 break;
5207 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005208
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005209 case FLOAT:
5210 if (load_float(self) < 0)
5211 break;
5212 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005213
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005214 case BINFLOAT:
5215 if (load_binfloat(self) < 0)
5216 break;
5217 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005218
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005219 case BINSTRING:
5220 if (load_binstring(self) < 0)
5221 break;
5222 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005223
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005224 case SHORT_BINSTRING:
5225 if (load_short_binstring(self) < 0)
5226 break;
5227 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005228
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005229 case STRING:
5230 if (load_string(self) < 0)
5231 break;
5232 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005233
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005234#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005235 case UNICODE:
5236 if (load_unicode(self) < 0)
5237 break;
5238 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005239
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005240 case BINUNICODE:
5241 if (load_binunicode(self) < 0)
5242 break;
5243 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005244#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005245
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005246 case EMPTY_TUPLE:
5247 if (load_counted_tuple(self, 0) < 0)
5248 break;
5249 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00005250
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005251 case TUPLE1:
5252 if (load_counted_tuple(self, 1) < 0)
5253 break;
5254 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00005255
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005256 case TUPLE2:
5257 if (load_counted_tuple(self, 2) < 0)
5258 break;
5259 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00005260
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005261 case TUPLE3:
5262 if (load_counted_tuple(self, 3) < 0)
5263 break;
5264 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005265
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005266 case TUPLE:
5267 if (load_tuple(self) < 0)
5268 break;
5269 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005270
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005271 case EMPTY_LIST:
5272 if (load_empty_list(self) < 0)
5273 break;
5274 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005275
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005276 case LIST:
5277 if (load_list(self) < 0)
5278 break;
5279 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005280
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005281 case EMPTY_DICT:
5282 if (load_empty_dict(self) < 0)
5283 break;
5284 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005285
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005286 case DICT:
5287 if (load_dict(self) < 0)
5288 break;
5289 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005290
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005291 case OBJ:
5292 if (noload_obj(self) < 0)
5293 break;
5294 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005295
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005296 case INST:
5297 if (noload_inst(self) < 0)
5298 break;
5299 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005300
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005301 case NEWOBJ:
5302 if (noload_newobj(self) < 0)
5303 break;
5304 continue;
Tim Peterseab7db32003-02-13 18:24:14 +00005305
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005306 case GLOBAL:
5307 if (noload_global(self) < 0)
5308 break;
5309 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005310
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005311 case APPEND:
5312 if (noload_append(self) < 0)
5313 break;
5314 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005315
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005316 case APPENDS:
5317 if (noload_appends(self) < 0)
5318 break;
5319 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005320
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005321 case BUILD:
5322 if (noload_build(self) < 0)
5323 break;
5324 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005325
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005326 case DUP:
5327 if (load_dup(self) < 0)
5328 break;
5329 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005330
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005331 case BINGET:
5332 if (load_binget(self) < 0)
5333 break;
5334 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005335
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005336 case LONG_BINGET:
5337 if (load_long_binget(self) < 0)
5338 break;
5339 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005340
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005341 case GET:
5342 if (load_get(self) < 0)
5343 break;
5344 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005345
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005346 case EXT1:
5347 if (noload_extension(self, 1) < 0)
5348 break;
5349 continue;
Tim Peters2d629652003-02-04 05:06:17 +00005350
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005351 case EXT2:
5352 if (noload_extension(self, 2) < 0)
5353 break;
5354 continue;
Tim Peters2d629652003-02-04 05:06:17 +00005355
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005356 case EXT4:
5357 if (noload_extension(self, 4) < 0)
5358 break;
5359 continue;
Tim Peters2d629652003-02-04 05:06:17 +00005360
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005361 case MARK:
5362 if (load_mark(self) < 0)
5363 break;
5364 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005365
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005366 case BINPUT:
5367 if (load_binput(self) < 0)
5368 break;
5369 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005370
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005371 case LONG_BINPUT:
5372 if (load_long_binput(self) < 0)
5373 break;
5374 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005375
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005376 case PUT:
5377 if (load_put(self) < 0)
5378 break;
5379 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005380
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005381 case POP:
5382 if (load_pop(self) < 0)
5383 break;
5384 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005385
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005386 case POP_MARK:
5387 if (load_pop_mark(self) < 0)
5388 break;
5389 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005390
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005391 case SETITEM:
5392 if (noload_setitem(self) < 0)
5393 break;
5394 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005395
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005396 case SETITEMS:
5397 if (noload_setitems(self) < 0)
5398 break;
5399 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005400
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005401 case STOP:
5402 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005403
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005404 case PERSID:
5405 if (load_persid(self) < 0)
5406 break;
5407 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005408
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005409 case BINPERSID:
5410 if (load_binpersid(self) < 0)
5411 break;
5412 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005413
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005414 case REDUCE:
5415 if (noload_reduce(self) < 0)
5416 break;
5417 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005418
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005419 case PROTO:
5420 if (load_proto(self) < 0)
5421 break;
5422 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005423
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005424 case NEWTRUE:
5425 if (load_bool(self, Py_True) < 0)
5426 break;
5427 continue;
Tim Peters3c67d792003-02-02 17:59:11 +00005428
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005429 case NEWFALSE:
5430 if (load_bool(self, Py_False) < 0)
5431 break;
5432 continue;
5433 default:
5434 cPickle_ErrFormat(UnpicklingError,
5435 "invalid load key, '%s'.",
5436 "c", s[0]);
5437 return NULL;
5438 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005439
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005440 break;
5441 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005442
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005443 if ((err = PyErr_Occurred())) {
5444 if (err == PyExc_EOFError) {
5445 PyErr_SetNone(PyExc_EOFError);
5446 }
5447 return NULL;
5448 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005449
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005450 PDATA_POP(self->stack, val);
5451 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005452}
Tim Peters84e87f32001-03-17 04:50:51 +00005453
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005454
Guido van Rossum60456fd1997-04-09 17:36:32 +00005455static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005456Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005457{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005458 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005459}
5460
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005461static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005462Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005463{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005464 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005465}
5466
Guido van Rossum60456fd1997-04-09 17:36:32 +00005467
5468static struct PyMethodDef Unpickler_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +00005469 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005470 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005471 },
Georg Brandl96a8c392006-05-29 21:04:52 +00005472 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005473 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005474 "noload() -- not load a pickle, but go through most of the motions\n"
5475 "\n"
5476 "This function can be used to read past a pickle without instantiating\n"
5477 "any objects or importing any modules. It can also be used to find all\n"
5478 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005479 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005480 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005481 {NULL, NULL} /* sentinel */
5482};
5483
5484
5485static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005486newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005487{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005488 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005489
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005490 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5491 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005492
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005493 self->file = NULL;
5494 self->arg = NULL;
5495 self->stack = (Pdata*)Pdata_New();
5496 self->pers_func = NULL;
5497 self->last_string = NULL;
5498 self->marks = NULL;
5499 self->num_marks = 0;
5500 self->marks_size = 0;
5501 self->buf_size = 0;
5502 self->read = NULL;
5503 self->readline = NULL;
5504 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005505
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005506 if (!( self->memo = PyDict_New()))
5507 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005508
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005509 if (!self->stack)
5510 goto err;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00005511
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005512 Py_INCREF(f);
5513 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005514
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005515 /* Set read, readline based on type of f */
5516 if (PyFile_Check(f)) {
5517 self->fp = PyFile_AsFile(f);
5518 if (self->fp == NULL) {
5519 PyErr_SetString(PyExc_ValueError,
5520 "I/O operation on closed file");
5521 goto err;
5522 }
5523 self->read_func = read_file;
5524 self->readline_func = readline_file;
5525 }
5526 else if (PycStringIO_InputCheck(f)) {
5527 self->fp = NULL;
5528 self->read_func = read_cStringIO;
5529 self->readline_func = readline_cStringIO;
5530 }
5531 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005532
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005533 self->fp = NULL;
5534 self->read_func = read_other;
5535 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005536
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005537 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5538 (self->read = PyObject_GetAttr(f, read_str)))) {
5539 PyErr_Clear();
5540 PyErr_SetString( PyExc_TypeError,
5541 "argument must have 'read' and "
5542 "'readline' attributes" );
5543 goto err;
5544 }
5545 }
5546 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005547
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005548 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005549
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005550 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005551 Py_DECREF((PyObject *)self);
5552 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005553}
5554
5555
5556static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005557get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005558{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005559 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005560}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005561
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005562
Guido van Rossum60456fd1997-04-09 17:36:32 +00005563static void
Tim Peterscba30e22003-02-01 06:24:36 +00005564Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005565{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005566 PyObject_GC_UnTrack((PyObject *)self);
5567 Py_XDECREF(self->readline);
5568 Py_XDECREF(self->read);
5569 Py_XDECREF(self->file);
5570 Py_XDECREF(self->memo);
5571 Py_XDECREF(self->stack);
5572 Py_XDECREF(self->pers_func);
5573 Py_XDECREF(self->arg);
5574 Py_XDECREF(self->last_string);
5575 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005576
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005577 if (self->marks) {
5578 free(self->marks);
5579 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005580
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005581 if (self->buf_size) {
5582 free(self->buf);
5583 }
Tim Peters84e87f32001-03-17 04:50:51 +00005584
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005585 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005586}
5587
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005588static int
5589Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5590{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005591 Py_VISIT(self->readline);
5592 Py_VISIT(self->read);
5593 Py_VISIT(self->file);
5594 Py_VISIT(self->memo);
5595 Py_VISIT(self->stack);
5596 Py_VISIT(self->pers_func);
5597 Py_VISIT(self->arg);
5598 Py_VISIT(self->last_string);
5599 Py_VISIT(self->find_class);
5600 return 0;
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005601}
5602
5603static int
5604Unpickler_clear(Unpicklerobject *self)
5605{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005606 Py_CLEAR(self->readline);
5607 Py_CLEAR(self->read);
5608 Py_CLEAR(self->file);
5609 Py_CLEAR(self->memo);
5610 Py_CLEAR(self->stack);
5611 Py_CLEAR(self->pers_func);
5612 Py_CLEAR(self->arg);
5613 Py_CLEAR(self->last_string);
5614 Py_CLEAR(self->find_class);
5615 return 0;
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005616}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005617
5618static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005619Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005620{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005621 if (!strcmp(name, "persistent_load")) {
5622 if (!self->pers_func) {
5623 PyErr_SetString(PyExc_AttributeError, name);
5624 return NULL;
5625 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005626
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005627 Py_INCREF(self->pers_func);
5628 return self->pers_func;
5629 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005630
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005631 if (!strcmp(name, "find_global")) {
5632 if (!self->find_class) {
5633 PyErr_SetString(PyExc_AttributeError, name);
5634 return NULL;
5635 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005636
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005637 Py_INCREF(self->find_class);
5638 return self->find_class;
5639 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005640
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005641 if (!strcmp(name, "memo")) {
5642 if (!self->memo) {
5643 PyErr_SetString(PyExc_AttributeError, name);
5644 return NULL;
5645 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005646
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005647 Py_INCREF(self->memo);
5648 return self->memo;
5649 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005650
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005651 if (!strcmp(name, "UnpicklingError")) {
5652 Py_INCREF(UnpicklingError);
5653 return UnpicklingError;
5654 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005655
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005656 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005657}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005658
Guido van Rossum60456fd1997-04-09 17:36:32 +00005659
5660static int
Tim Peterscba30e22003-02-01 06:24:36 +00005661Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005662{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005663
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005664 if (!strcmp(name, "persistent_load")) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005665 Py_XINCREF(value);
Serhiy Storchaka5951f232015-12-24 10:35:35 +02005666 Py_SETREF(self->pers_func, value);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005667 return 0;
5668 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005669
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005670 if (!strcmp(name, "find_global")) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005671 Py_XINCREF(value);
Serhiy Storchaka5951f232015-12-24 10:35:35 +02005672 Py_SETREF(self->find_class, value);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005673 return 0;
5674 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005675
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005676 if (! value) {
5677 PyErr_SetString(PyExc_TypeError,
5678 "attribute deletion is not supported");
5679 return -1;
5680 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005681
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005682 if (strcmp(name, "memo") == 0) {
5683 if (!PyDict_Check(value)) {
5684 PyErr_SetString(PyExc_TypeError,
5685 "memo must be a dictionary");
5686 return -1;
5687 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005688 Py_INCREF(value);
Serhiy Storchaka5951f232015-12-24 10:35:35 +02005689 Py_SETREF(self->memo, value);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005690 return 0;
5691 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005692
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005693 PyErr_SetString(PyExc_AttributeError, name);
5694 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005695}
5696
Tim Peters5bd2a792003-02-01 16:45:06 +00005697/* ---------------------------------------------------------------------------
5698 * Module-level functions.
5699 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005700
Martin v. Löwis544f1192004-07-27 05:22:33 +00005701/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005702static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005703cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005704{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005705 static char *kwlist[] = {"obj", "file", "protocol", NULL};
5706 PyObject *ob, *file, *res = NULL;
5707 Picklerobject *pickler = 0;
5708 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005709
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005710 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5711 &ob, &file, &proto)))
5712 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005713
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005714 if (!( pickler = newPicklerobject(file, proto)))
5715 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005716
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005717 if (dump(pickler, ob) < 0)
5718 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005719
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005720 Py_INCREF(Py_None);
5721 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005722
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005723 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005724 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005725
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005726 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005727}
5728
5729
Martin v. Löwis544f1192004-07-27 05:22:33 +00005730/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005731static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005732cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005733{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005734 static char *kwlist[] = {"obj", "protocol", NULL};
5735 PyObject *ob, *file = 0, *res = NULL;
5736 Picklerobject *pickler = 0;
5737 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005738
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005739 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5740 &ob, &proto)))
5741 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005742
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005743 if (!( file = PycStringIO->NewOutput(128)))
5744 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005745
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005746 if (!( pickler = newPicklerobject(file, proto)))
5747 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005748
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005749 if (dump(pickler, ob) < 0)
5750 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005751
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005752 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005753
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005754 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005755 Py_XDECREF(pickler);
5756 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005757
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005758 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005759}
5760
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005761
Tim Peters5bd2a792003-02-01 16:45:06 +00005762/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005763static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005764cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005765{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005766 Unpicklerobject *unpickler = 0;
5767 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005768
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005769 if (!( unpickler = newUnpicklerobject(ob)))
5770 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005771
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005772 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005773
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005774 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005775 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005776
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005777 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005778}
5779
5780
Tim Peters5bd2a792003-02-01 16:45:06 +00005781/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005782static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005783cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005784{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005785 PyObject *ob, *file = 0, *res = NULL;
5786 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005787
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005788 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5789 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005790
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005791 if (!( file = PycStringIO->NewInput(ob)))
5792 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005793
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005794 if (!( unpickler = newUnpicklerobject(file)))
5795 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005796
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005797 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005798
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005799 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005800 Py_XDECREF(file);
5801 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005802
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005803 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005804}
5805
5806
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005807PyDoc_STRVAR(Unpicklertype__doc__,
5808"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005809
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005810static PyTypeObject Unpicklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00005811 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005812 "cPickle.Unpickler", /*tp_name*/
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005813 sizeof(Unpicklerobject), /*tp_basicsize*/
5814 0,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005815 (destructor)Unpickler_dealloc, /* tp_dealloc */
5816 0, /* tp_print */
5817 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5818 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5819 0, /* tp_compare */
5820 0, /* tp_repr */
5821 0, /* tp_as_number */
5822 0, /* tp_as_sequence */
5823 0, /* tp_as_mapping */
5824 0, /* tp_hash */
5825 0, /* tp_call */
5826 0, /* tp_str */
5827 0, /* tp_getattro */
5828 0, /* tp_setattro */
5829 0, /* tp_as_buffer */
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005830 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005831 Unpicklertype__doc__, /* tp_doc */
5832 (traverseproc)Unpickler_traverse, /* tp_traverse */
5833 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005834};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005835
Guido van Rossum60456fd1997-04-09 17:36:32 +00005836static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005837 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5838 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005839 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005840 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005841 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005842 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005843
Martin v. Löwis544f1192004-07-27 05:22:33 +00005844 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5845 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005846 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005847 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005848 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005849 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005850
Georg Brandl96a8c392006-05-29 21:04:52 +00005851 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005852 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005853
Neal Norwitzb0493252002-03-31 14:44:22 +00005854 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005855 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005856
Martin v. Löwis544f1192004-07-27 05:22:33 +00005857 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5858 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005859 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005860 "This takes a file-like object for writing a pickle data stream.\n"
5861 "The optional proto argument tells the pickler to use the given\n"
5862 "protocol; supported protocols are 0, 1, 2. The default\n"
5863 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5864 "only protocol that can be written to a file opened in text\n"
5865 "mode and read back successfully. When using a protocol higher\n"
5866 "than 0, make sure the file is opened in binary mode, both when\n"
5867 "pickling and unpickling.)\n"
5868 "\n"
5869 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5870 "more efficient than protocol 1.\n"
5871 "\n"
5872 "Specifying a negative protocol version selects the highest\n"
5873 "protocol version supported. The higher the protocol used, the\n"
5874 "more recent the version of Python needed to read the pickle\n"
5875 "produced.\n"
5876 "\n"
5877 "The file parameter must have a write() method that accepts a single\n"
5878 "string argument. It can thus be an open file object, a StringIO\n"
5879 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005880 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005881
Georg Brandl96a8c392006-05-29 21:04:52 +00005882 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005883 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5884
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005885 { NULL, NULL }
5886};
5887
Guido van Rossum60456fd1997-04-09 17:36:32 +00005888static int
Tim Peterscba30e22003-02-01 06:24:36 +00005889init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005890{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005891 PyObject *copyreg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005892
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005893#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005894
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005895 if (PyType_Ready(&Unpicklertype) < 0)
5896 return -1;
5897 if (PyType_Ready(&Picklertype) < 0)
5898 return -1;
Tim Peters3cfe7542003-05-21 21:29:48 +00005899
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005900 INIT_STR(__class__);
5901 INIT_STR(__getinitargs__);
5902 INIT_STR(__dict__);
5903 INIT_STR(__getstate__);
5904 INIT_STR(__setstate__);
5905 INIT_STR(__name__);
5906 INIT_STR(__main__);
5907 INIT_STR(__reduce__);
5908 INIT_STR(__reduce_ex__);
5909 INIT_STR(write);
5910 INIT_STR(append);
5911 INIT_STR(read);
5912 INIT_STR(readline);
5913 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005914
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005915 if (!( copyreg = PyImport_ImportModule("copy_reg")))
5916 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005917
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005918 /* This is special because we want to use a different
5919 one in restricted mode. */
5920 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
5921 if (!dispatch_table) return -1;
Tim Peters5b7da392003-02-04 00:21:07 +00005922
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005923 extension_registry = PyObject_GetAttrString(copyreg,
5924 "_extension_registry");
5925 if (!extension_registry) return -1;
Tim Peters5b7da392003-02-04 00:21:07 +00005926
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005927 inverted_registry = PyObject_GetAttrString(copyreg,
5928 "_inverted_registry");
5929 if (!inverted_registry) return -1;
Tim Peters5b7da392003-02-04 00:21:07 +00005930
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005931 extension_cache = PyObject_GetAttrString(copyreg,
5932 "_extension_cache");
5933 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005934
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005935 Py_DECREF(copyreg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005936
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005937 if (!(empty_tuple = PyTuple_New(0)))
5938 return -1;
Tim Peters731098b2003-02-04 20:56:09 +00005939
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005940 two_tuple = PyTuple_New(2);
5941 if (two_tuple == NULL)
5942 return -1;
5943 /* We use this temp container with no regard to refcounts, or to
5944 * keeping containees alive. Exempt from GC, because we don't
5945 * want anything looking at two_tuple() by magic.
5946 */
5947 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005948
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005949 /* Ugh */
5950 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5951 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5952 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005953
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005954 if (!( t=PyDict_New())) return -1;
5955 if (!( r=PyRun_String(
5956 "def __str__(self):\n"
5957 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5958 Py_file_input,
5959 module_dict, t) )) return -1;
5960 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005961
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005962 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5963 if (!PickleError)
5964 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005965
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005966 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005967
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005968 PicklingError = PyErr_NewException("cPickle.PicklingError",
5969 PickleError, NULL);
5970 if (!PicklingError)
5971 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005972
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005973 if (!( t=PyDict_New())) return -1;
5974 if (!( r=PyRun_String(
5975 "def __str__(self):\n"
5976 " a=self.args\n"
5977 " a=a and type(a[0]) or '(what)'\n"
5978 " return 'Cannot pickle %s objects' % a\n"
5979 , Py_file_input,
5980 module_dict, t) )) return -1;
5981 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005982
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005983 if (!( UnpickleableError = PyErr_NewException(
5984 "cPickle.UnpickleableError", PicklingError, t)))
5985 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005986
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005987 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005988
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005989 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5990 PickleError, NULL)))
5991 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005992
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005993 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5994 UnpicklingError, NULL)))
5995 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005996
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005997 if (PyDict_SetItemString(module_dict, "PickleError",
5998 PickleError) < 0)
5999 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00006000
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006001 if (PyDict_SetItemString(module_dict, "PicklingError",
6002 PicklingError) < 0)
6003 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00006004
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006005 if (PyDict_SetItemString(module_dict, "UnpicklingError",
6006 UnpicklingError) < 0)
6007 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00006008
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006009 if (PyDict_SetItemString(module_dict, "UnpickleableError",
6010 UnpickleableError) < 0)
6011 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00006012
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006013 if (PyDict_SetItemString(module_dict, "BadPickleGet",
6014 BadPickleGet) < 0)
6015 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00006016
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006017 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00006018
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006019 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006020}
6021
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006022#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
Mark Hammondfe51c6d2002-08-02 02:27:13 +00006023#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00006024#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00006025PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00006026initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00006027{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006028 PyObject *m, *d, *di, *v, *k;
6029 Py_ssize_t i;
6030 char *rev = "1.71"; /* XXX when does this change? */
6031 PyObject *format_version;
6032 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006033
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006034 Py_TYPE(&Picklertype) = &PyType_Type;
6035 Py_TYPE(&Unpicklertype) = &PyType_Type;
6036 Py_TYPE(&PdataType) = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006037
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006038 /* Initialize some pieces. We need to do this before module creation,
6039 * so we're forced to use a temporary dictionary. :(
6040 */
6041 di = PyDict_New();
6042 if (!di) return;
6043 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00006044
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006045 /* Create the module and add the functions */
6046 m = Py_InitModule4("cPickle", cPickle_methods,
6047 cPickle_module_documentation,
6048 (PyObject*)NULL,PYTHON_API_VERSION);
6049 if (m == NULL)
6050 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006051
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006052 /* Add some symbolic constants to the module */
6053 d = PyModule_GetDict(m);
6054 v = PyString_FromString(rev);
6055 PyDict_SetItemString(d, "__version__", v);
6056 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00006057
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006058 /* Copy data from di. Waaa. */
6059 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
6060 if (PyObject_SetItem(d, k, v) < 0) {
6061 Py_DECREF(di);
6062 return;
6063 }
6064 }
6065 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00006066
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006067 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
6068 if (i < 0)
6069 return;
Tim Peters8587b3c2003-02-13 15:44:41 +00006070
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006071 /* These are purely informational; no code uses them. */
6072 /* File format version we write. */
6073 format_version = PyString_FromString("2.0");
6074 /* Format versions we can read. */
6075 compatible_formats = Py_BuildValue("[sssss]",
6076 "1.0", /* Original protocol 0 */
6077 "1.1", /* Protocol 0 + INST */
6078 "1.2", /* Original protocol 1 */
6079 "1.3", /* Protocol 1 + BINFLOAT */
6080 "2.0"); /* Original protocol 2 */
6081 PyDict_SetItemString(d, "format_version", format_version);
6082 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
6083 Py_XDECREF(format_version);
6084 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006085}