blob: 10199eb3bdb33554aee3551d9b86c95d5065c073 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001#include "Python.h"
2#include "cStringIO.h"
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003#include "structmember.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005PyDoc_STRVAR(cPickle_module_documentation,
Tim Peters64c04d12003-02-01 06:27:59 +00006"C implementation and optimization of the Python pickle module.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007
Guido van Rossum142eeb81997-08-13 03:14:41 +00008#ifndef Py_eval_input
9#include <graminit.h>
10#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000011#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000012
Guido van Rossum60456fd1997-04-09 17:36:32 +000013#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000014
Guido van Rossum60456fd1997-04-09 17:36:32 +000015#define WRITE_BUF_SIZE 256
16
Tim Peters5bd2a792003-02-01 16:45:06 +000017/* Bump this when new opcodes are added to the pickle protocol. */
Tim Peters8587b3c2003-02-13 15:44:41 +000018#define HIGHEST_PROTOCOL 2
Tim Peters5bd2a792003-02-01 16:45:06 +000019
Tim Peters797ec242003-02-01 06:22:36 +000020/*
Martin v. Löwis9ac49272009-01-02 20:40:14 +000021 * Note: The UNICODE macro controls the TCHAR meaning of the win32 API. Since
22 * all headers have already been included here, we can safely redefine it.
23 */
24#ifdef UNICODE
25# undef UNICODE
26#endif
27
28/*
Tim Peters797ec242003-02-01 06:22:36 +000029 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
30 * docs are in pickletools.py.
31 */
Guido van Rossum60456fd1997-04-09 17:36:32 +000032#define MARK '('
33#define STOP '.'
34#define POP '0'
35#define POP_MARK '1'
36#define DUP '2'
37#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000038#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000039#define INT 'I'
40#define BININT 'J'
41#define BININT1 'K'
42#define LONG 'L'
43#define BININT2 'M'
44#define NONE 'N'
45#define PERSID 'P'
46#define BINPERSID 'Q'
47#define REDUCE 'R'
48#define STRING 'S'
49#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000050#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000051#define UNICODE 'V'
52#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000053#define APPEND 'a'
54#define BUILD 'b'
55#define GLOBAL 'c'
56#define DICT 'd'
57#define EMPTY_DICT '}'
58#define APPENDS 'e'
59#define GET 'g'
60#define BINGET 'h'
61#define INST 'i'
62#define LONG_BINGET 'j'
63#define LIST 'l'
64#define EMPTY_LIST ']'
65#define OBJ 'o'
66#define PUT 'p'
67#define BINPUT 'q'
68#define LONG_BINPUT 'r'
69#define SETITEM 's'
70#define TUPLE 't'
71#define EMPTY_TUPLE ')'
72#define SETITEMS 'u'
Tim Peters797ec242003-02-01 06:22:36 +000073
74/* Protocol 2. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +000075#define PROTO '\x80' /* identify pickle protocol */
Tim Peters797ec242003-02-01 06:22:36 +000076#define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
77#define EXT1 '\x82' /* push object from extension registry; 1-byte index */
78#define EXT2 '\x83' /* ditto, but 2-byte index */
79#define EXT4 '\x84' /* ditto, but 4-byte index */
80#define TUPLE1 '\x85' /* build 1-tuple from stack top */
81#define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
82#define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
83#define NEWTRUE '\x88' /* push True */
84#define NEWFALSE '\x89' /* push False */
85#define LONG1 '\x8a' /* push long from < 256 bytes */
86#define LONG4 '\x8b' /* push really big long */
87
88/* There aren't opcodes -- they're ways to pickle bools before protocol 2,
89 * so that unpicklers written before bools were introduced unpickle them
90 * as ints, but unpicklers after can recognize that bools were intended.
91 * Note that protocol 2 added direct ways to pickle bools.
92 */
Jack Jansen3a967022002-06-26 20:40:42 +000093#undef TRUE
Guido van Rossume2763392002-04-05 19:30:08 +000094#define TRUE "I01\n"
Jack Jansen3a967022002-06-26 20:40:42 +000095#undef FALSE
Guido van Rossume2763392002-04-05 19:30:08 +000096#define FALSE "I00\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +000097
Tim Peters1092d642003-02-11 21:06:20 +000098/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
Tim Peters42f08ac2003-02-11 22:43:24 +000099 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
100 * break if this gets out of synch with pickle.py, but it's unclear that
101 * would help anything either.
Tim Peters1092d642003-02-11 21:06:20 +0000102 */
103#define BATCHSIZE 1000
104
Guido van Rossum60456fd1997-04-09 17:36:32 +0000105static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000106
Guido van Rossumc03158b1999-06-09 15:23:31 +0000107static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000108static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000109static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000110static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000111static PyObject *BadPickleGet;
112
Tim Peters5b7da392003-02-04 00:21:07 +0000113/* As the name says, an empty tuple. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000114static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000115
Georg Brandldffbf5f2008-05-20 07:49:57 +0000116/* copy_reg.dispatch_table, {type_object: pickling_function} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *dispatch_table;
118
119/* For EXT[124] opcodes. */
Georg Brandldffbf5f2008-05-20 07:49:57 +0000120/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000121static PyObject *extension_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000122/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000123static PyObject *inverted_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000124/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000125static PyObject *extension_cache;
126
Georg Brandldffbf5f2008-05-20 07:49:57 +0000127/* For looking up name pairs in copy_reg._extension_registry. */
Tim Peters731098b2003-02-04 20:56:09 +0000128static PyObject *two_tuple;
129
Guido van Rossum60456fd1997-04-09 17:36:32 +0000130static PyObject *__class___str, *__getinitargs___str, *__dict___str,
131 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Guido van Rossumb289b872003-02-19 01:45:13 +0000132 *__reduce_ex___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000133 *write_str, *append_str,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000134 *read_str, *readline_str, *__main___str,
Alexandre Vassalotti8b2d7132009-11-24 17:53:23 +0000135 *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000136
Guido van Rossum053b8df1998-11-25 16:18:00 +0000137/*************************************************************************
138 Internal Data type for pickle data. */
139
140typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000141 PyObject_HEAD
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200142 Py_ssize_t length; /* number of initial slots in data currently used */
143 Py_ssize_t size; /* number of slots in data allocated */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000144 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000145} Pdata;
146
Tim Peters84e87f32001-03-17 04:50:51 +0000147static void
Tim Peterscba30e22003-02-01 06:24:36 +0000148Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000149{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200150 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000151 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000152
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000153 for (i = self->length, p = self->data; --i >= 0; p++) {
154 Py_DECREF(*p);
155 }
156 if (self->data)
157 free(self->data);
158 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000159}
160
161static PyTypeObject PdataType = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000162 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,
163 (destructor)Pdata_dealloc,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000165};
166
Christian Heimese93237d2007-12-19 02:37:44 +0000167#define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000168
169static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000170Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000171{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000172 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000173
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000174 if (!(self = PyObject_New(Pdata, &PdataType)))
175 return NULL;
176 self->size = 8;
177 self->length = 0;
178 self->data = malloc(self->size * sizeof(PyObject*));
179 if (self->data)
180 return (PyObject*)self;
181 Py_DECREF(self);
182 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000183}
184
Tim Peters84e87f32001-03-17 04:50:51 +0000185static int
Tim Peterscba30e22003-02-01 06:24:36 +0000186stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000187{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000188 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
189 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000190}
191
Tim Peters1d63c9f2003-02-02 20:29:39 +0000192/* Retain only the initial clearto items. If clearto >= the current
193 * number of items, this is a (non-erroneous) NOP.
194 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000195static int
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200196Pdata_clear(Pdata *self, Py_ssize_t clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000197{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200198 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000199 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000200
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000201 if (clearto < 0) return stackUnderflow();
202 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000203
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000204 for (i = self->length, p = self->data + clearto;
205 --i >= clearto;
206 p++) {
207 Py_CLEAR(*p);
208 }
209 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000210
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000211 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000212}
213
Tim Peters84e87f32001-03-17 04:50:51 +0000214static int
Tim Peterscba30e22003-02-01 06:24:36 +0000215Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000216{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200217 Py_ssize_t bigger;
218 Py_ssize_t nbytes;
219
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000220 PyObject **tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000221
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200222 if (self->size > (PY_SSIZE_T_MAX >> 1))
223 goto nomemory;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000224 bigger = self->size << 1;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200225 if (bigger > (PY_SSIZE_T_MAX / sizeof(PyObject *)))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000226 goto nomemory;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200227 nbytes = bigger * sizeof(PyObject *);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000228 tmp = realloc(self->data, nbytes);
229 if (tmp == NULL)
230 goto nomemory;
231 self->data = tmp;
232 self->size = bigger;
233 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000234
235 nomemory:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000236 PyErr_NoMemory();
237 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000238}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000239
Tim Peterse0a39072003-02-03 15:45:56 +0000240/* D is a Pdata*. Pop the topmost element and store it into V, which
241 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000242 * is raised and V is set to NULL. D and V may be evaluated several times.
243 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000244#define PDATA_POP(D, V) { \
245 if ((D)->length) \
246 (V) = (D)->data[--((D)->length)]; \
247 else { \
248 PyErr_SetString(UnpicklingError, "bad pickle data"); \
249 (V) = NULL; \
250 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000251}
252
Tim Peterse0a39072003-02-03 15:45:56 +0000253/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
254 * D. If the Pdata stack can't be grown to hold the new value, both
255 * raise MemoryError and execute "return ER". The difference is in ownership
256 * of O after: _PUSH transfers ownership of O from the caller to the stack
257 * (no incref of O is done, and in case of error O is decrefed), while
258 * _APPEND pushes a new reference.
259 */
260
261/* Push O on stack D, giving ownership of O to the stack. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000262#define PDATA_PUSH(D, O, ER) { \
263 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
264 Pdata_grow((Pdata*)(D)) < 0) { \
265 Py_DECREF(O); \
266 return ER; \
267 } \
268 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
Tim Peterse0a39072003-02-03 15:45:56 +0000269}
270
271/* Push O on stack D, pushing a new reference. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000272#define PDATA_APPEND(D, O, ER) { \
273 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
274 Pdata_grow((Pdata*)(D)) < 0) \
275 return ER; \
276 Py_INCREF(O); \
277 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
Tim Peterse0a39072003-02-03 15:45:56 +0000278}
279
280
Guido van Rossum053b8df1998-11-25 16:18:00 +0000281static PyObject *
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200282Pdata_popTuple(Pdata *self, Py_ssize_t start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000283{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000284 PyObject *r;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200285 Py_ssize_t i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000286
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000287 l = self->length-start;
288 r = PyTuple_New(l);
289 if (r == NULL)
290 return NULL;
291 for (i = start, j = 0 ; j < l; i++, j++)
292 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000293
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000294 self->length = start;
295 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000296}
297
298static PyObject *
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200299Pdata_popList(Pdata *self, Py_ssize_t start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000300{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000301 PyObject *r;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200302 Py_ssize_t i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000303
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 l=self->length-start;
305 if (!( r=PyList_New(l))) return NULL;
306 for (i=start, j=0 ; j < l; i++, j++)
307 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000308
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000309 self->length=start;
310 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000311}
312
Guido van Rossum053b8df1998-11-25 16:18:00 +0000313/*************************************************************************/
314
315#define ARG_TUP(self, o) { \
316 if (self->arg || (self->arg=PyTuple_New(1))) { \
317 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
318 PyTuple_SET_ITEM(self->arg,0,o); \
319 } \
320 else { \
321 Py_DECREF(o); \
322 } \
323}
324
325#define FREE_ARG_TUP(self) { \
Christian Heimese93237d2007-12-19 02:37:44 +0000326 if (Py_REFCNT(self->arg) > 1) { \
Serhiy Storchaka98a97222014-02-09 13:14:04 +0200327 Py_CLEAR(self->arg); \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000328 } \
329 }
330
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000331typedef struct Picklerobject {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000332 PyObject_HEAD
333 FILE *fp;
334 PyObject *write;
335 PyObject *file;
336 PyObject *memo;
337 PyObject *arg;
338 PyObject *pers_func;
339 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000340
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000341 /* pickle protocol number, >= 0 */
342 int proto;
Tim Peters797ec242003-02-01 06:22:36 +0000343
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000344 /* bool, true if proto > 0 */
345 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000346
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000347 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200348 Py_ssize_t (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000349 char *write_buf;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200350 Py_ssize_t buf_size;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000351 PyObject *dispatch_table;
352 int fast_container; /* count nested container dumps */
353 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000354} Picklerobject;
355
Barry Warsaw52acb492001-12-21 20:04:22 +0000356#ifndef PY_CPICKLE_FAST_LIMIT
357#define PY_CPICKLE_FAST_LIMIT 50
358#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000359
Jeremy Hylton938ace62002-07-17 16:30:39 +0000360static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000361
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000362typedef struct Unpicklerobject {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000363 PyObject_HEAD
364 FILE *fp;
365 PyObject *file;
366 PyObject *readline;
367 PyObject *read;
368 PyObject *memo;
369 PyObject *arg;
370 Pdata *stack;
371 PyObject *mark;
372 PyObject *pers_func;
373 PyObject *last_string;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200374 Py_ssize_t *marks;
375 Py_ssize_t num_marks;
376 Py_ssize_t marks_size;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000377 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
378 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200379 Py_ssize_t buf_size;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000380 char *buf;
381 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000382} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000383
Jeremy Hylton938ace62002-07-17 16:30:39 +0000384static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000385
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000386/* Forward decls that need the above structs */
387static int save(Picklerobject *, PyObject *, int);
388static int put2(Picklerobject *, PyObject *);
389
Guido van Rossumd385d591997-04-09 17:47:47 +0000390static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000391PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000392cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
393{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000394 va_list va;
395 PyObject *args=0, *retval=0;
396 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000397
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000398 if (format) args = Py_VaBuildValue(format, va);
399 va_end(va);
400 if (format && ! args) return NULL;
401 if (stringformat && !(retval=PyString_FromString(stringformat)))
402 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000403
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000404 if (retval) {
405 if (args) {
406 PyObject *v;
407 v=PyString_Format(retval, args);
408 Py_DECREF(retval);
409 Py_DECREF(args);
410 if (! v) return NULL;
411 retval=v;
412 }
413 }
414 else
415 if (args) retval=args;
416 else {
417 PyErr_SetObject(ErrType,Py_None);
418 return NULL;
419 }
420 PyErr_SetObject(ErrType,retval);
421 Py_DECREF(retval);
422 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000423}
424
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200425static Py_ssize_t
Martin v. Löwis18e16552006-02-15 17:27:45 +0000426write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000427{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000428 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000429
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000430 if (s == NULL) {
431 return 0;
432 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000433
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000434 PyFile_IncUseCount((PyFileObject *)self->file);
435 Py_BEGIN_ALLOW_THREADS
436 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
437 Py_END_ALLOW_THREADS
438 PyFile_DecUseCount((PyFileObject *)self->file);
439 if (nbyteswritten != (size_t)n) {
440 PyErr_SetFromErrno(PyExc_IOError);
441 return -1;
442 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000443
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200444 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000445}
446
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200447static Py_ssize_t
Martin v. Löwis18e16552006-02-15 17:27:45 +0000448write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000449{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200450 Py_ssize_t len = n;
451
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000452 if (s == NULL) {
453 return 0;
454 }
Tim Peterscba30e22003-02-01 06:24:36 +0000455
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200456 while (n > INT_MAX) {
457 if (PycStringIO->cwrite((PyObject *)self->file, s, INT_MAX) != INT_MAX) {
458 return -1;
459 }
460 n -= INT_MAX;
461 }
462
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000463 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
464 return -1;
465 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000466
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200467 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000468}
469
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200470static Py_ssize_t
Martin v. Löwis18e16552006-02-15 17:27:45 +0000471write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000472{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000473 if (s == NULL) return 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200474 return n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000475}
476
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200477static Py_ssize_t
478write_other(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000479{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000480 PyObject *py_str = 0, *junk = 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000481
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000482 if (s == NULL) {
483 if (!( self->buf_size )) return 0;
484 py_str = PyString_FromStringAndSize(self->write_buf,
485 self->buf_size);
486 if (!py_str)
487 return -1;
488 }
489 else {
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200490 if (self->buf_size && n > WRITE_BUF_SIZE - self->buf_size) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000491 if (write_other(self, NULL, 0) < 0)
492 return -1;
493 }
Tim Peterscba30e22003-02-01 06:24:36 +0000494
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000495 if (n > WRITE_BUF_SIZE) {
496 if (!( py_str =
497 PyString_FromStringAndSize(s, n)))
498 return -1;
499 }
500 else {
501 memcpy(self->write_buf + self->buf_size, s, n);
502 self->buf_size += n;
503 return n;
504 }
505 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000506
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000507 if (self->write) {
508 /* object with write method */
509 ARG_TUP(self, py_str);
510 if (self->arg) {
511 junk = PyObject_Call(self->write, self->arg, NULL);
512 FREE_ARG_TUP(self);
513 }
514 if (junk) Py_DECREF(junk);
515 else return -1;
516 }
517 else
518 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000519
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000520 self->buf_size = 0;
521 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000522}
523
524
Martin v. Löwis18e16552006-02-15 17:27:45 +0000525static Py_ssize_t
526read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000527{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000528 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000529
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000530 if (self->buf_size == 0) {
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200531 Py_ssize_t size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000532
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000533 size = ((n < 32) ? 32 : n);
534 if (!( self->buf = (char *)malloc(size))) {
535 PyErr_NoMemory();
536 return -1;
537 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000538
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000539 self->buf_size = size;
540 }
541 else if (n > self->buf_size) {
542 char *newbuf = (char *)realloc(self->buf, n);
543 if (!newbuf) {
544 PyErr_NoMemory();
545 return -1;
546 }
547 self->buf = newbuf;
548 self->buf_size = n;
549 }
Tim Peters84e87f32001-03-17 04:50:51 +0000550
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000551 PyFile_IncUseCount((PyFileObject *)self->file);
552 Py_BEGIN_ALLOW_THREADS
553 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
554 Py_END_ALLOW_THREADS
555 PyFile_DecUseCount((PyFileObject *)self->file);
556 if (nbytesread != (size_t)n) {
557 if (feof(self->fp)) {
558 PyErr_SetNone(PyExc_EOFError);
559 return -1;
560 }
Tim Peterscba30e22003-02-01 06:24:36 +0000561
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000562 PyErr_SetFromErrno(PyExc_IOError);
563 return -1;
564 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000565
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000566 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000567
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000568 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000569}
570
571
Martin v. Löwis18e16552006-02-15 17:27:45 +0000572static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000573readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000574{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200575 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000576
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000577 if (self->buf_size == 0) {
578 if (!( self->buf = (char *)malloc(40))) {
579 PyErr_NoMemory();
580 return -1;
581 }
582 self->buf_size = 40;
583 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000584
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000585 i = 0;
586 while (1) {
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200587 Py_ssize_t bigger;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000588 char *newbuf;
589 for (; i < (self->buf_size - 1); i++) {
590 if (feof(self->fp) ||
591 (self->buf[i] = getc(self->fp)) == '\n') {
592 self->buf[i + 1] = '\0';
593 *s = self->buf;
594 return i + 1;
595 }
596 }
Serhiy Storchakad36d4e02013-02-26 10:07:36 +0200597 if (self->buf_size > (PY_SSIZE_T_MAX >> 1)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000598 PyErr_NoMemory();
599 return -1;
600 }
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200601 bigger = self->buf_size << 1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000602 newbuf = (char *)realloc(self->buf, bigger);
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200603 if (newbuf == NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000604 PyErr_NoMemory();
605 return -1;
606 }
607 self->buf = newbuf;
608 self->buf_size = bigger;
609 }
Tim Peters84e87f32001-03-17 04:50:51 +0000610}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000611
612
Martin v. Löwis18e16552006-02-15 17:27:45 +0000613static Py_ssize_t
614read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000615{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200616 Py_ssize_t len = n;
617 char *start, *end = NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000618
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200619 while (1) {
620 int k;
621 char *ptr;
622 if (n > INT_MAX)
623 k = INT_MAX;
624 else
625 k = (int)n;
626 if (PycStringIO->cread((PyObject *)self->file, &ptr, k) != k) {
627 PyErr_SetNone(PyExc_EOFError);
628 return -1;
629 }
630 if (end == NULL)
631 start = ptr;
632 else if (ptr != end) {
633 /* non-continuous area */
634 return -1;
635 }
636 if (n <= INT_MAX)
637 break;
638 end = ptr + INT_MAX;
639 n -= INT_MAX;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000640 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000641
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200642 *s = start;
Tim Peterscba30e22003-02-01 06:24:36 +0000643
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200644 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000645}
646
647
Martin v. Löwis18e16552006-02-15 17:27:45 +0000648static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000649readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000650{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200651 Py_ssize_t n = 0;
652 char *start = NULL, *end = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000653
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200654 while (1) {
655 int k;
656 char *ptr;
657 if ((k = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
658 return -1;
659 }
660 n += k;
661 if (end == NULL)
662 start = ptr;
663 else if (ptr != end) {
664 /* non-continuous area */
665 return -1;
666 }
667 if (k == 0 || ptr[k - 1] == '\n')
668 break;
669 end = ptr + k;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000670 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200672 *s = start;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000673
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000674 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000675}
676
677
Martin v. Löwis18e16552006-02-15 17:27:45 +0000678static Py_ssize_t
679read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000680{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000681 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000682
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000683 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000684
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000685 ARG_TUP(self, bytes);
686 if (self->arg) {
687 str = PyObject_Call(self->read, self->arg, NULL);
688 FREE_ARG_TUP(self);
689 }
690 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000691
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000692 Py_XDECREF(self->last_string);
693 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000694
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000695 if (! (*s = PyString_AsString(str))) return -1;
Amaury Forgeot d'Arc74b30162009-07-23 19:26:02 +0000696
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000697 if (PyString_GET_SIZE(str) != n) {
698 PyErr_SetNone(PyExc_EOFError);
699 return -1;
700 }
Amaury Forgeot d'Arc74b30162009-07-23 19:26:02 +0000701
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000702 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000703}
704
705
Martin v. Löwis18e16552006-02-15 17:27:45 +0000706static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000707readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000708{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000709 PyObject *str;
710 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000711
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000712 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
713 return -1;
714 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000715
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000716 if ((str_size = PyString_Size(str)) < 0)
717 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000718
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000719 Py_XDECREF(self->last_string);
720 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000721
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000722 if (! (*s = PyString_AsString(str)))
723 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000724
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000725 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000726}
727
Tim Petersee1a53c2003-02-02 02:57:53 +0000728/* Copy the first n bytes from s into newly malloc'ed memory, plus a
729 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
730 * The caller is responsible for free()'ing the return value.
731 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000732static char *
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200733pystrndup(const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000734{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000735 char *r = (char *)malloc(n+1);
736 if (r == NULL)
737 return (char*)PyErr_NoMemory();
738 memcpy(r, s, n);
739 r[n] = 0;
740 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000741}
742
743
744static int
Tim Peterscba30e22003-02-01 06:24:36 +0000745get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000746{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000747 PyObject *value, *mv;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200748 Py_ssize_t c_value;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000749 char s[30];
750 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000751
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000752 if (!( mv = PyDict_GetItem(self->memo, id))) {
753 PyErr_SetObject(PyExc_KeyError, id);
754 return -1;
755 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000756
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000757 if (!( value = PyTuple_GetItem(mv, 0)))
758 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000759
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000760 if (!( PyInt_Check(value))) {
761 PyErr_SetString(PicklingError, "no int where int expected in memo");
762 return -1;
763 }
764 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000765
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000766 if (!self->bin) {
767 s[0] = GET;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200768 PyOS_snprintf(s + 1, sizeof(s) - 1,
769 "%" PY_FORMAT_SIZE_T "d\n", c_value);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000770 len = strlen(s);
771 }
772 else if (Pdata_Check(self->file)) {
773 if (write_other(self, NULL, 0) < 0) return -1;
774 PDATA_APPEND(self->file, mv, -1);
775 return 0;
776 }
777 else {
778 if (c_value < 256) {
779 s[0] = BINGET;
780 s[1] = (int)(c_value & 0xff);
781 len = 2;
782 }
783 else {
784 s[0] = LONG_BINGET;
785 s[1] = (int)(c_value & 0xff);
786 s[2] = (int)((c_value >> 8) & 0xff);
787 s[3] = (int)((c_value >> 16) & 0xff);
788 s[4] = (int)((c_value >> 24) & 0xff);
789 len = 5;
790 }
791 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000792
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000793 if (self->write_func(self, s, len) < 0)
794 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000795
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000796 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000797}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000798
Guido van Rossum60456fd1997-04-09 17:36:32 +0000799
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000800static int
Tim Peterscba30e22003-02-01 06:24:36 +0000801put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000802{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000803 if (Py_REFCNT(ob) < 2 || self->fast)
804 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000805
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000806 return put2(self, ob);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000807}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000808
Guido van Rossum053b8df1998-11-25 16:18:00 +0000809
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000810static int
Tim Peterscba30e22003-02-01 06:24:36 +0000811put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000812{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000813 char c_str[30];
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200814 Py_ssize_t len, p;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000815 int res = -1;
816 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000817
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000818 if (self->fast)
819 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000820
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000821 if ((p = PyDict_Size(self->memo)) < 0)
822 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000823
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000824 /* Make sure memo keys are positive! */
825 /* XXX Why?
826 * XXX And does "positive" really mean non-negative?
827 * XXX pickle.py starts with PUT index 0, not 1. This makes for
828 * XXX gratuitous differences between the pickling modules.
829 */
830 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000831
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000832 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
833 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000834
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000835 if (!( memo_len = PyInt_FromLong(p)))
836 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000837
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000838 if (!( t = PyTuple_New(2)))
839 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000840
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000841 PyTuple_SET_ITEM(t, 0, memo_len);
842 Py_INCREF(memo_len);
843 PyTuple_SET_ITEM(t, 1, ob);
844 Py_INCREF(ob);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000845
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000846 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
847 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000848
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000849 if (!self->bin) {
850 c_str[0] = PUT;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +0200851 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1,
852 "%" PY_FORMAT_SIZE_T "d\n", p);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000853 len = strlen(c_str);
854 }
855 else if (Pdata_Check(self->file)) {
856 if (write_other(self, NULL, 0) < 0) return -1;
857 PDATA_APPEND(self->file, memo_len, -1);
858 res=0; /* Job well done ;) */
859 goto finally;
860 }
861 else {
862 if (p >= 256) {
863 c_str[0] = LONG_BINPUT;
864 c_str[1] = (int)(p & 0xff);
865 c_str[2] = (int)((p >> 8) & 0xff);
866 c_str[3] = (int)((p >> 16) & 0xff);
867 c_str[4] = (int)((p >> 24) & 0xff);
868 len = 5;
869 }
870 else {
871 c_str[0] = BINPUT;
872 c_str[1] = p;
873 len = 2;
874 }
875 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000876
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000877 if (self->write_func(self, c_str, len) < 0)
878 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000879
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000880 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000881
882 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000883 Py_XDECREF(py_ob_id);
884 Py_XDECREF(memo_len);
885 Py_XDECREF(t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000886
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000887 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000888}
889
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000890static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000891whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000892{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000893 Py_ssize_t i, j;
894 PyObject *module = 0, *modules_dict = 0,
895 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000896
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000897 module = PyObject_GetAttrString(global, "__module__");
898 if (module)
899 return module;
900 if (PyErr_ExceptionMatches(PyExc_AttributeError))
901 PyErr_Clear();
902 else
903 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000904
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000905 if (!( modules_dict = PySys_GetObject("modules")))
906 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000907
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000908 i = 0;
909 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000910
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000911 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000912
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000913 global_name_attr = PyObject_GetAttr(module, global_name);
914 if (!global_name_attr) {
915 if (PyErr_ExceptionMatches(PyExc_AttributeError))
916 PyErr_Clear();
917 else
918 return NULL;
919 continue;
920 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000921
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000922 if (global_name_attr != global) {
923 Py_DECREF(global_name_attr);
924 continue;
925 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000926
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000927 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000928
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000929 break;
930 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000931
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000932 /* The following implements the rule in pickle.py added in 1.5
933 that used __main__ if no module is found. I don't actually
934 like this rule. jlf
935 */
936 if (!j) {
937 name=__main___str;
938 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000939
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000940 Py_INCREF(name);
941 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000942}
943
944
Guido van Rossum60456fd1997-04-09 17:36:32 +0000945static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000946fast_save_enter(Picklerobject *self, PyObject *obj)
947{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000948 /* if fast_container < 0, we're doing an error exit. */
949 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
950 PyObject *key = NULL;
951 if (self->fast_memo == NULL) {
952 self->fast_memo = PyDict_New();
953 if (self->fast_memo == NULL) {
954 self->fast_container = -1;
955 return 0;
956 }
957 }
958 key = PyLong_FromVoidPtr(obj);
959 if (key == NULL)
960 return 0;
961 if (PyDict_GetItem(self->fast_memo, key)) {
962 Py_DECREF(key);
963 PyErr_Format(PyExc_ValueError,
964 "fast mode: can't pickle cyclic objects "
965 "including object type %s at %p",
966 Py_TYPE(obj)->tp_name, obj);
967 self->fast_container = -1;
968 return 0;
969 }
970 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
971 Py_DECREF(key);
972 self->fast_container = -1;
973 return 0;
974 }
975 Py_DECREF(key);
976 }
977 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000978}
979
Tim Peterscba30e22003-02-01 06:24:36 +0000980int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000981fast_save_leave(Picklerobject *self, PyObject *obj)
982{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000983 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
984 PyObject *key = PyLong_FromVoidPtr(obj);
985 if (key == NULL)
986 return 0;
987 if (PyDict_DelItem(self->fast_memo, key) < 0) {
988 Py_DECREF(key);
989 return 0;
990 }
991 Py_DECREF(key);
992 }
993 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000994}
995
996static int
Tim Peterscba30e22003-02-01 06:24:36 +0000997save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000998{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000999 static char none = NONE;
1000 if (self->write_func(self, &none, 1) < 0)
1001 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001002
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001003 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001004}
1005
Guido van Rossum77f6a652002-04-03 22:41:51 +00001006static int
Tim Peterscba30e22003-02-01 06:24:36 +00001007save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +00001008{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001009 static const char *buf[2] = {FALSE, TRUE};
1010 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
1011 long l = PyInt_AS_LONG((PyIntObject *)args);
Guido van Rossum77f6a652002-04-03 22:41:51 +00001012
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001013 if (self->proto >= 2) {
1014 char opcode = l ? NEWTRUE : NEWFALSE;
1015 if (self->write_func(self, &opcode, 1) < 0)
1016 return -1;
1017 }
1018 else if (self->write_func(self, buf[l], len[l]) < 0)
1019 return -1;
1020 return 0;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001021}
Tim Peters84e87f32001-03-17 04:50:51 +00001022
Guido van Rossum60456fd1997-04-09 17:36:32 +00001023static int
Tim Peterscba30e22003-02-01 06:24:36 +00001024save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001025{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001026 char c_str[32];
1027 long l = PyInt_AS_LONG((PyIntObject *)args);
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001028 Py_ssize_t len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001029
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001030 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001031#if SIZEOF_LONG > 4
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001032 || l > 0x7fffffffL
1033 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001034#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001035 ) {
1036 /* Text-mode pickle, or long too big to fit in the 4-byte
1037 * signed BININT format: store as a string.
1038 */
1039 c_str[0] = INT;
1040 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
1041 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1042 return -1;
1043 }
1044 else {
1045 /* Binary pickle and l fits in a signed 4-byte int. */
1046 c_str[1] = (int)( l & 0xff);
1047 c_str[2] = (int)((l >> 8) & 0xff);
1048 c_str[3] = (int)((l >> 16) & 0xff);
1049 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001050
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001051 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1052 if (c_str[2] == 0) {
1053 c_str[0] = BININT1;
1054 len = 2;
1055 }
1056 else {
1057 c_str[0] = BININT2;
1058 len = 3;
1059 }
1060 }
1061 else {
1062 c_str[0] = BININT;
1063 len = 5;
1064 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001065
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001066 if (self->write_func(self, c_str, len) < 0)
1067 return -1;
1068 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001069
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001070 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001071}
1072
1073
1074static int
Tim Peterscba30e22003-02-01 06:24:36 +00001075save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001076{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001077 Py_ssize_t size;
1078 int res = -1;
1079 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001080
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001081 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001082
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001083 if (self->proto >= 2) {
1084 /* Linear-time pickling. */
1085 size_t nbits;
1086 size_t nbytes;
1087 unsigned char *pdata;
1088 char c_str[5];
1089 int i;
1090 int sign = _PyLong_Sign(args);
Tim Petersee1a53c2003-02-02 02:57:53 +00001091
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001092 if (sign == 0) {
1093 /* It's 0 -- an empty bytestring. */
1094 c_str[0] = LONG1;
1095 c_str[1] = 0;
1096 i = self->write_func(self, c_str, 2);
1097 if (i < 0) goto finally;
1098 res = 0;
1099 goto finally;
1100 }
1101 nbits = _PyLong_NumBits(args);
1102 if (nbits == (size_t)-1 && PyErr_Occurred())
1103 goto finally;
1104 /* How many bytes do we need? There are nbits >> 3 full
1105 * bytes of data, and nbits & 7 leftover bits. If there
1106 * are any leftover bits, then we clearly need another
1107 * byte. Wnat's not so obvious is that we *probably*
1108 * need another byte even if there aren't any leftovers:
1109 * the most-significant bit of the most-significant byte
1110 * acts like a sign bit, and it's usually got a sense
1111 * opposite of the one we need. The exception is longs
1112 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1113 * its own 256's-complement, so has the right sign bit
1114 * even without the extra byte. That's a pain to check
1115 * for in advance, though, so we always grab an extra
1116 * byte at the start, and cut it back later if possible.
1117 */
1118 nbytes = (nbits >> 3) + 1;
1119 if (nbytes > INT_MAX) {
1120 PyErr_SetString(PyExc_OverflowError, "long too large "
1121 "to pickle");
1122 goto finally;
1123 }
1124 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1125 if (repr == NULL) goto finally;
1126 pdata = (unsigned char *)PyString_AS_STRING(repr);
1127 i = _PyLong_AsByteArray((PyLongObject *)args,
1128 pdata, nbytes,
1129 1 /* little endian */, 1 /* signed */);
1130 if (i < 0) goto finally;
1131 /* If the long is negative, this may be a byte more than
1132 * needed. This is so iff the MSB is all redundant sign
1133 * bits.
1134 */
1135 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1136 (pdata[nbytes - 2] & 0x80) != 0)
1137 --nbytes;
Tim Petersee1a53c2003-02-02 02:57:53 +00001138
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001139 if (nbytes < 256) {
1140 c_str[0] = LONG1;
1141 c_str[1] = (char)nbytes;
1142 size = 2;
1143 }
1144 else {
1145 c_str[0] = LONG4;
1146 size = (int)nbytes;
1147 for (i = 1; i < 5; i++) {
1148 c_str[i] = (char)(size & 0xff);
1149 size >>= 8;
1150 }
1151 size = 5;
1152 }
1153 i = self->write_func(self, c_str, size);
1154 if (i < 0) goto finally;
1155 i = self->write_func(self, (char *)pdata, (int)nbytes);
1156 if (i < 0) goto finally;
1157 res = 0;
1158 goto finally;
1159 }
Tim Petersee1a53c2003-02-02 02:57:53 +00001160
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001161 /* proto < 2: write the repr and newline. This is quadratic-time
1162 * (in the number of digits), in both directions.
1163 */
1164 if (!( repr = PyObject_Repr(args)))
1165 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001166
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001167 if ((size = PyString_Size(repr)) < 0)
1168 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001169
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001170 if (self->write_func(self, &l, 1) < 0)
1171 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001172
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001173 if (self->write_func(self,
1174 PyString_AS_STRING((PyStringObject *)repr),
1175 size) < 0)
1176 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001177
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001178 if (self->write_func(self, "\n", 1) < 0)
1179 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001180
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001181 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001182
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001183 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001184 Py_XDECREF(repr);
1185 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001186}
1187
1188
1189static int
Tim Peterscba30e22003-02-01 06:24:36 +00001190save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001191{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001192 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001193
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001194 if (self->bin) {
1195 char str[9];
1196 str[0] = BINFLOAT;
1197 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
1198 return -1;
1199 if (self->write_func(self, str, 9) < 0)
1200 return -1;
1201 }
1202 else {
1203 int result = -1;
1204 char *buf = NULL;
1205 char op = FLOAT;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001206
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001207 if (self->write_func(self, &op, 1) < 0)
1208 goto done;
Eric Smithb05d3be2009-10-26 15:06:39 +00001209
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001210 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
1211 if (!buf) {
1212 PyErr_NoMemory();
1213 goto done;
1214 }
Eric Smithb05d3be2009-10-26 15:06:39 +00001215
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001216 if (self->write_func(self, buf, strlen(buf)) < 0)
1217 goto done;
Eric Smithb05d3be2009-10-26 15:06:39 +00001218
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001219 if (self->write_func(self, "\n", 1) < 0)
1220 goto done;
Eric Smithb05d3be2009-10-26 15:06:39 +00001221
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001222 result = 0;
Eric Smithb05d3be2009-10-26 15:06:39 +00001223done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001224 PyMem_Free(buf);
1225 return result;
1226 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001227
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001228 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001229}
1230
1231
1232static int
Tim Peterscba30e22003-02-01 06:24:36 +00001233save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001234{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001235 Py_ssize_t size, len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001236 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001237
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001238 if ((size = PyString_Size(args)) < 0)
1239 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001240
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001241 if (!self->bin) {
1242 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001243
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001244 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001245
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001246 if (!( repr = PyObject_Repr(args)))
1247 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001248
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001249 if ((len = PyString_Size(repr)) < 0)
1250 goto err;
1251 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001252
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001253 if (self->write_func(self, &string, 1) < 0)
1254 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001255
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001256 if (self->write_func(self, repr_str, len) < 0)
1257 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001258
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001259 if (self->write_func(self, "\n", 1) < 0)
1260 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001261
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001262 Py_XDECREF(repr);
1263 }
1264 else {
1265 int i;
1266 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001267
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001268 if (size < 256) {
1269 c_str[0] = SHORT_BINSTRING;
1270 c_str[1] = size;
1271 len = 2;
1272 }
1273 else if (size <= INT_MAX) {
1274 c_str[0] = BINSTRING;
1275 for (i = 1; i < 5; i++)
1276 c_str[i] = (int)(size >> ((i - 1) * 8));
1277 len = 5;
1278 }
1279 else
1280 return -1; /* string too large */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001281
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001282 if (self->write_func(self, c_str, len) < 0)
1283 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001284
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001285 if (size > 128 && Pdata_Check(self->file)) {
1286 if (write_other(self, NULL, 0) < 0) return -1;
1287 PDATA_APPEND(self->file, args, -1);
1288 }
1289 else {
1290 if (self->write_func(self,
1291 PyString_AS_STRING(
1292 (PyStringObject *)args),
1293 size) < 0)
1294 return -1;
1295 }
1296 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001297
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001298 if (doput)
1299 if (put(self, args) < 0)
1300 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001301
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001302 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001303
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001304 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001305 Py_XDECREF(repr);
1306 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001307}
1308
1309
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001310#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001311/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1312 backslash and newline characters to \uXXXX escapes. */
1313static PyObject *
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001314modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001315{
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001316 PyObject *repr;
1317 char *p;
1318 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001319
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001320 static const char *hexdigit = "0123456789abcdef";
1321#ifdef Py_UNICODE_WIDE
1322 const Py_ssize_t expandsize = 10;
1323#else
1324 const Py_ssize_t expandsize = 6;
1325#endif
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001326
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001327 if (size > PY_SSIZE_T_MAX / expandsize)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001328 return PyErr_NoMemory();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001329
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001330 repr = PyString_FromStringAndSize(NULL, expandsize * size);
1331 if (repr == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001332 return NULL;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001333 if (size == 0)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001334 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001335
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001336 p = q = PyString_AS_STRING(repr);
1337 while (size-- > 0) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001338 Py_UNICODE ch = *s++;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001339#ifdef Py_UNICODE_WIDE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001340 /* Map 32-bit characters to '\Uxxxxxxxx' */
1341 if (ch >= 0x10000) {
1342 *p++ = '\\';
1343 *p++ = 'U';
1344 *p++ = hexdigit[(ch >> 28) & 0xf];
1345 *p++ = hexdigit[(ch >> 24) & 0xf];
1346 *p++ = hexdigit[(ch >> 20) & 0xf];
1347 *p++ = hexdigit[(ch >> 16) & 0xf];
1348 *p++ = hexdigit[(ch >> 12) & 0xf];
1349 *p++ = hexdigit[(ch >> 8) & 0xf];
1350 *p++ = hexdigit[(ch >> 4) & 0xf];
1351 *p++ = hexdigit[ch & 15];
1352 }
1353 else
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001354#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001355 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1356 if (ch >= 0xD800 && ch < 0xDC00) {
1357 Py_UNICODE ch2;
1358 Py_UCS4 ucs;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001359
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001360 ch2 = *s++;
1361 size--;
1362 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1363 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1364 *p++ = '\\';
1365 *p++ = 'U';
1366 *p++ = hexdigit[(ucs >> 28) & 0xf];
1367 *p++ = hexdigit[(ucs >> 24) & 0xf];
1368 *p++ = hexdigit[(ucs >> 20) & 0xf];
1369 *p++ = hexdigit[(ucs >> 16) & 0xf];
1370 *p++ = hexdigit[(ucs >> 12) & 0xf];
1371 *p++ = hexdigit[(ucs >> 8) & 0xf];
1372 *p++ = hexdigit[(ucs >> 4) & 0xf];
1373 *p++ = hexdigit[ucs & 0xf];
1374 continue;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001375 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001376 /* Fall through: isolated surrogates are copied as-is */
1377 s--;
1378 size++;
1379 }
1380#endif
1381 /* Map 16-bit characters to '\uxxxx' */
1382 if (ch >= 256 || ch == '\\' || ch == '\n') {
1383 *p++ = '\\';
1384 *p++ = 'u';
1385 *p++ = hexdigit[(ch >> 12) & 0xf];
1386 *p++ = hexdigit[(ch >> 8) & 0xf];
1387 *p++ = hexdigit[(ch >> 4) & 0xf];
1388 *p++ = hexdigit[ch & 15];
1389 }
1390 /* Copy everything else as-is */
1391 else
1392 *p++ = (char) ch;
Alexandre Vassalottif852bf92008-12-27 07:08:47 +00001393 }
1394 *p = '\0';
1395 _PyString_Resize(&repr, p - q);
1396 return repr;
1397}
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001398
Guido van Rossum60456fd1997-04-09 17:36:32 +00001399static int
Tim Peterscba30e22003-02-01 06:24:36 +00001400save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001401{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001402 Py_ssize_t size, len;
1403 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001404
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001405 if (!PyUnicode_Check(args))
1406 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001407
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001408 if (!self->bin) {
1409 char *repr_str;
1410 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001411
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001412 repr = modified_EncodeRawUnicodeEscape(
1413 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1414 if (!repr)
1415 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001416
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001417 if ((len = PyString_Size(repr)) < 0)
1418 goto err;
1419 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001420
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001421 if (self->write_func(self, &string, 1) < 0)
1422 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001423
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001424 if (self->write_func(self, repr_str, len) < 0)
1425 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001426
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001427 if (self->write_func(self, "\n", 1) < 0)
1428 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001429
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001430 Py_XDECREF(repr);
1431 }
1432 else {
1433 int i;
1434 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001435
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001436 if (!( repr = PyUnicode_AsUTF8String(args)))
1437 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001438
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001439 if ((size = PyString_Size(repr)) < 0)
1440 goto err;
1441 if (size > INT_MAX)
1442 return -1; /* string too large */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001443
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001444 c_str[0] = BINUNICODE;
1445 for (i = 1; i < 5; i++)
1446 c_str[i] = (int)(size >> ((i - 1) * 8));
1447 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001448
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001449 if (self->write_func(self, c_str, len) < 0)
1450 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001451
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001452 if (size > 128 && Pdata_Check(self->file)) {
1453 if (write_other(self, NULL, 0) < 0)
1454 goto err;
1455 PDATA_APPEND(self->file, repr, -1);
1456 }
1457 else {
1458 if (self->write_func(self, PyString_AS_STRING(repr),
1459 size) < 0)
1460 goto err;
1461 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001462
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001463 Py_DECREF(repr);
1464 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001465
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001466 if (doput)
1467 if (put(self, args) < 0)
1468 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001469
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001470 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001471
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001472 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001473 Py_XDECREF(repr);
1474 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001475}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001476#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001477
Tim Peters1d63c9f2003-02-02 20:29:39 +00001478/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1479static int
Tim Peters67920142003-02-05 03:46:17 +00001480store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001481{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001482 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001483 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001484
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001485 assert(PyTuple_Size(t) == len);
Tim Peters1d63c9f2003-02-02 20:29:39 +00001486
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001487 for (i = 0; i < len; i++) {
1488 PyObject *element = PyTuple_GET_ITEM(t, i);
Tim Peters1d63c9f2003-02-02 20:29:39 +00001489
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001490 if (element == NULL)
1491 goto finally;
1492 if (save(self, element, 0) < 0)
1493 goto finally;
1494 }
1495 res = 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001496
1497 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001498 return res;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001499}
1500
1501/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1502 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001503 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001504 * (a tuple can be reached from itself), and that requires some subtle
1505 * magic so that it works in all cases. IOW, this is a long routine.
1506 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001507static int
Tim Peterscba30e22003-02-01 06:24:36 +00001508save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001509{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001510 PyObject *py_tuple_id = NULL;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001511 Py_ssize_t len, i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001512 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001513
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001514 static char tuple = TUPLE;
1515 static char pop = POP;
1516 static char pop_mark = POP_MARK;
1517 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001518
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001519 if ((len = PyTuple_Size(args)) < 0)
1520 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001521
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001522 if (len == 0) {
1523 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001524
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001525 if (self->proto) {
1526 c_str[0] = EMPTY_TUPLE;
1527 len = 1;
1528 }
1529 else {
1530 c_str[0] = MARK;
1531 c_str[1] = TUPLE;
1532 len = 2;
1533 }
1534 if (self->write_func(self, c_str, len) >= 0)
1535 res = 0;
1536 /* Don't memoize an empty tuple. */
1537 goto finally;
1538 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001539
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001540 /* A non-empty tuple. */
Tim Peters1d63c9f2003-02-02 20:29:39 +00001541
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001542 /* id(tuple) isn't in the memo now. If it shows up there after
1543 * saving the tuple elements, the tuple must be recursive, in
1544 * which case we'll pop everything we put on the stack, and fetch
1545 * its value from the memo.
1546 */
1547 py_tuple_id = PyLong_FromVoidPtr(args);
1548 if (py_tuple_id == NULL)
1549 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001550
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001551 if (len <= 3 && self->proto >= 2) {
1552 /* Use TUPLE{1,2,3} opcodes. */
1553 if (store_tuple_elements(self, args, len) < 0)
1554 goto finally;
1555 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1556 /* pop the len elements */
1557 for (i = 0; i < len; ++i)
1558 if (self->write_func(self, &pop, 1) < 0)
1559 goto finally;
1560 /* fetch from memo */
1561 if (get(self, py_tuple_id) < 0)
1562 goto finally;
1563 res = 0;
1564 goto finally;
1565 }
1566 /* Not recursive. */
1567 if (self->write_func(self, len2opcode + len, 1) < 0)
1568 goto finally;
1569 goto memoize;
1570 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001571
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001572 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1573 * Generate MARK elt1 elt2 ... TUPLE
1574 */
1575 if (self->write_func(self, &MARKv, 1) < 0)
1576 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001577
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001578 if (store_tuple_elements(self, args, len) < 0)
1579 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001580
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001581 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1582 /* pop the stack stuff we pushed */
1583 if (self->bin) {
1584 if (self->write_func(self, &pop_mark, 1) < 0)
1585 goto finally;
1586 }
1587 else {
1588 /* Note that we pop one more than len, to remove
1589 * the MARK too.
1590 */
1591 for (i = 0; i <= len; i++)
1592 if (self->write_func(self, &pop, 1) < 0)
1593 goto finally;
1594 }
1595 /* fetch from memo */
1596 if (get(self, py_tuple_id) >= 0)
1597 res = 0;
1598 goto finally;
1599 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001600
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001601 /* Not recursive. */
1602 if (self->write_func(self, &tuple, 1) < 0)
1603 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001604
Tim Peters1d63c9f2003-02-02 20:29:39 +00001605 memoize:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001606 if (put(self, args) >= 0)
1607 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001608
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001609 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001610 Py_XDECREF(py_tuple_id);
1611 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001612}
1613
Tim Peters1092d642003-02-11 21:06:20 +00001614/* iter is an iterator giving items, and we batch up chunks of
1615 * MARK item item ... item APPENDS
1616 * opcode sequences. Calling code should have arranged to first create an
1617 * empty list, or list-like object, for the APPENDS to operate on.
1618 * Returns 0 on success, <0 on error.
1619 */
1620static int
1621batch_list(Picklerobject *self, PyObject *iter)
1622{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001623 PyObject *obj = NULL;
1624 PyObject *firstitem = NULL;
1625 int i, n;
Tim Peters1092d642003-02-11 21:06:20 +00001626
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001627 static char append = APPEND;
1628 static char appends = APPENDS;
Tim Peters1092d642003-02-11 21:06:20 +00001629
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001630 assert(iter != NULL);
Tim Peters1092d642003-02-11 21:06:20 +00001631
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001632 if (self->proto == 0) {
1633 /* APPENDS isn't available; do one at a time. */
1634 for (;;) {
1635 obj = PyIter_Next(iter);
1636 if (obj == NULL) {
1637 if (PyErr_Occurred())
1638 return -1;
1639 break;
1640 }
1641 i = save(self, obj, 0);
1642 Py_DECREF(obj);
1643 if (i < 0)
1644 return -1;
1645 if (self->write_func(self, &append, 1) < 0)
1646 return -1;
1647 }
1648 return 0;
1649 }
Tim Peters1092d642003-02-11 21:06:20 +00001650
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001651 /* proto > 0: write in batches of BATCHSIZE. */
1652 do {
1653 /* Get first item */
1654 firstitem = PyIter_Next(iter);
1655 if (firstitem == NULL) {
1656 if (PyErr_Occurred())
1657 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001658
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001659 /* nothing more to add */
1660 break;
1661 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001662
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001663 /* Try to get a second item */
1664 obj = PyIter_Next(iter);
1665 if (obj == NULL) {
1666 if (PyErr_Occurred())
1667 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001668
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001669 /* Only one item to write */
1670 if (save(self, firstitem, 0) < 0)
1671 goto BatchFailed;
1672 if (self->write_func(self, &append, 1) < 0)
1673 goto BatchFailed;
1674 Py_CLEAR(firstitem);
1675 break;
1676 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001677
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001678 /* More than one item to write */
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001679
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001680 /* Pump out MARK, items, APPENDS. */
1681 if (self->write_func(self, &MARKv, 1) < 0)
1682 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001683
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001684 if (save(self, firstitem, 0) < 0)
1685 goto BatchFailed;
1686 Py_CLEAR(firstitem);
1687 n = 1;
Tim Peters1092d642003-02-11 21:06:20 +00001688
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001689 /* Fetch and save up to BATCHSIZE items */
1690 while (obj) {
1691 if (save(self, obj, 0) < 0)
1692 goto BatchFailed;
1693 Py_CLEAR(obj);
1694 n += 1;
Tim Peters1092d642003-02-11 21:06:20 +00001695
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001696 if (n == BATCHSIZE)
1697 break;
1698
1699 obj = PyIter_Next(iter);
1700 if (obj == NULL) {
1701 if (PyErr_Occurred())
1702 goto BatchFailed;
1703 break;
1704 }
1705 }
1706
1707 if (self->write_func(self, &appends, 1) < 0)
1708 goto BatchFailed;
1709
1710 } while (n == BATCHSIZE);
1711 return 0;
Tim Peters1092d642003-02-11 21:06:20 +00001712
1713BatchFailed:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001714 Py_XDECREF(firstitem);
1715 Py_XDECREF(obj);
1716 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001717}
1718
Guido van Rossum60456fd1997-04-09 17:36:32 +00001719static int
Tim Peterscba30e22003-02-01 06:24:36 +00001720save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001721{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001722 int res = -1;
1723 char s[3];
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001724 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001725 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001726
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001727 if (self->fast && !fast_save_enter(self, args))
1728 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001729
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001730 /* Create an empty list. */
1731 if (self->bin) {
1732 s[0] = EMPTY_LIST;
1733 len = 1;
1734 }
1735 else {
1736 s[0] = MARK;
1737 s[1] = LIST;
1738 len = 2;
1739 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001740
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001741 if (self->write_func(self, s, len) < 0)
1742 goto finally;
Tim Peters1092d642003-02-11 21:06:20 +00001743
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001744 /* Get list length, and bow out early if empty. */
1745 if ((len = PyList_Size(args)) < 0)
1746 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001747
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001748 /* Memoize. */
1749 if (len == 0) {
1750 if (put(self, args) >= 0)
1751 res = 0;
1752 goto finally;
1753 }
1754 if (put2(self, args) < 0)
1755 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001756
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001757 /* Materialize the list elements. */
1758 iter = PyObject_GetIter(args);
1759 if (iter == NULL)
1760 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001761
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001762 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1763 {
1764 res = batch_list(self, iter);
1765 Py_LeaveRecursiveCall();
1766 }
1767 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001768
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001769 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001770 if (self->fast && !fast_save_leave(self, args))
1771 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001772
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001773 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001774}
1775
1776
Tim Peters42f08ac2003-02-11 22:43:24 +00001777/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1778 * MARK key value ... key value SETITEMS
1779 * opcode sequences. Calling code should have arranged to first create an
1780 * empty dict, or dict-like object, for the SETITEMS to operate on.
1781 * Returns 0 on success, <0 on error.
1782 *
1783 * This is very much like batch_list(). The difference between saving
1784 * elements directly, and picking apart two-tuples, is so long-winded at
1785 * the C level, though, that attempts to combine these routines were too
1786 * ugly to bear.
1787 */
1788static int
1789batch_dict(Picklerobject *self, PyObject *iter)
1790{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001791 PyObject *p = NULL;
1792 PyObject *firstitem = NULL;
1793 int i, n;
Tim Peters42f08ac2003-02-11 22:43:24 +00001794
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001795 static char setitem = SETITEM;
1796 static char setitems = SETITEMS;
Tim Peters42f08ac2003-02-11 22:43:24 +00001797
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001798 assert(iter != NULL);
Tim Peters42f08ac2003-02-11 22:43:24 +00001799
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001800 if (self->proto == 0) {
1801 /* SETITEMS isn't available; do one at a time. */
1802 for (;;) {
1803 p = PyIter_Next(iter);
1804 if (p == NULL) {
1805 if (PyErr_Occurred())
1806 return -1;
1807 break;
1808 }
1809 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1810 PyErr_SetString(PyExc_TypeError, "dict items "
1811 "iterator must return 2-tuples");
1812 return -1;
1813 }
1814 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1815 if (i >= 0)
1816 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1817 Py_DECREF(p);
1818 if (i < 0)
1819 return -1;
1820 if (self->write_func(self, &setitem, 1) < 0)
1821 return -1;
1822 }
1823 return 0;
1824 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001825
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001826 /* proto > 0: write in batches of BATCHSIZE. */
1827 do {
1828 /* Get first item */
1829 firstitem = PyIter_Next(iter);
1830 if (firstitem == NULL) {
1831 if (PyErr_Occurred())
1832 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001833
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001834 /* nothing more to add */
1835 break;
1836 }
1837 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1838 PyErr_SetString(PyExc_TypeError, "dict items "
1839 "iterator must return 2-tuples");
1840 goto BatchFailed;
1841 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001842
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001843 /* Try to get a second item */
1844 p = PyIter_Next(iter);
1845 if (p == NULL) {
1846 if (PyErr_Occurred())
1847 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001848
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001849 /* Only one item to write */
1850 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1851 goto BatchFailed;
1852 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1853 goto BatchFailed;
1854 if (self->write_func(self, &setitem, 1) < 0)
1855 goto BatchFailed;
1856 Py_CLEAR(firstitem);
1857 break;
1858 }
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001859
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001860 /* More than one item to write */
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001861
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001862 /* Pump out MARK, items, SETITEMS. */
1863 if (self->write_func(self, &MARKv, 1) < 0)
1864 goto BatchFailed;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001865
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001866 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1867 goto BatchFailed;
1868 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1869 goto BatchFailed;
1870 Py_CLEAR(firstitem);
1871 n = 1;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001872
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001873 /* Fetch and save up to BATCHSIZE items */
1874 while (p) {
1875 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1876 PyErr_SetString(PyExc_TypeError, "dict items "
1877 "iterator must return 2-tuples");
1878 goto BatchFailed;
1879 }
1880 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1881 goto BatchFailed;
1882 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1883 goto BatchFailed;
1884 Py_CLEAR(p);
1885 n += 1;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001886
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001887 if (n == BATCHSIZE)
1888 break;
Amaury Forgeot d'Arc24cb3822008-09-11 20:56:13 +00001889
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001890 p = PyIter_Next(iter);
1891 if (p == NULL) {
1892 if (PyErr_Occurred())
1893 goto BatchFailed;
1894 break;
1895 }
1896 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001897
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001898 if (self->write_func(self, &setitems, 1) < 0)
1899 goto BatchFailed;
Tim Peters42f08ac2003-02-11 22:43:24 +00001900
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001901 } while (n == BATCHSIZE);
1902 return 0;
Tim Peters42f08ac2003-02-11 22:43:24 +00001903
1904BatchFailed:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001905 Py_XDECREF(firstitem);
1906 Py_XDECREF(p);
1907 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001908}
1909
Collin Winter179bf212009-05-25 04:34:39 +00001910/* This is a variant of batch_dict() above that specializes for dicts, with no
1911 * support for dict subclasses. Like batch_dict(), we batch up chunks of
1912 * MARK key value ... key value SETITEMS
1913 * opcode sequences. Calling code should have arranged to first create an
1914 * empty dict, or dict-like object, for the SETITEMS to operate on.
1915 * Returns 0 on success, -1 on error.
1916 *
1917 * Note that this currently doesn't work for protocol 0.
1918 */
1919static int
1920batch_dict_exact(Picklerobject *self, PyObject *obj)
1921{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001922 PyObject *key = NULL, *value = NULL;
1923 int i;
1924 Py_ssize_t dict_size, ppos = 0;
Collin Winter179bf212009-05-25 04:34:39 +00001925
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001926 static char setitem = SETITEM;
1927 static char setitems = SETITEMS;
Collin Winter179bf212009-05-25 04:34:39 +00001928
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001929 assert(obj != NULL);
1930 assert(self->proto > 0);
Collin Winter179bf212009-05-25 04:34:39 +00001931
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001932 dict_size = PyDict_Size(obj);
Collin Winter179bf212009-05-25 04:34:39 +00001933
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001934 /* Special-case len(d) == 1 to save space. */
1935 if (dict_size == 1) {
1936 PyDict_Next(obj, &ppos, &key, &value);
1937 if (save(self, key, 0) < 0)
1938 return -1;
1939 if (save(self, value, 0) < 0)
1940 return -1;
1941 if (self->write_func(self, &setitem, 1) < 0)
1942 return -1;
1943 return 0;
1944 }
Collin Winter179bf212009-05-25 04:34:39 +00001945
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001946 /* Write in batches of BATCHSIZE. */
1947 do {
1948 i = 0;
1949 if (self->write_func(self, &MARKv, 1) < 0)
1950 return -1;
1951 while (PyDict_Next(obj, &ppos, &key, &value)) {
1952 if (save(self, key, 0) < 0)
1953 return -1;
1954 if (save(self, value, 0) < 0)
1955 return -1;
1956 if (++i == BATCHSIZE)
1957 break;
1958 }
1959 if (self->write_func(self, &setitems, 1) < 0)
1960 return -1;
1961 if (PyDict_Size(obj) != dict_size) {
1962 PyErr_Format(
1963 PyExc_RuntimeError,
1964 "dictionary changed size during iteration");
1965 return -1;
1966 }
Collin Winter179bf212009-05-25 04:34:39 +00001967
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001968 } while (i == BATCHSIZE);
1969 return 0;
Collin Winter179bf212009-05-25 04:34:39 +00001970}
1971
Guido van Rossum60456fd1997-04-09 17:36:32 +00001972static int
Tim Peterscba30e22003-02-01 06:24:36 +00001973save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001974{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001975 int res = -1;
1976 char s[3];
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02001977 Py_ssize_t len;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001978
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001979 if (self->fast && !fast_save_enter(self, args))
1980 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001981
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001982 /* Create an empty dict. */
1983 if (self->bin) {
1984 s[0] = EMPTY_DICT;
1985 len = 1;
1986 }
1987 else {
1988 s[0] = MARK;
1989 s[1] = DICT;
1990 len = 2;
1991 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001992
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001993 if (self->write_func(self, s, len) < 0)
1994 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001995
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001996 /* Get dict size, and bow out early if empty. */
1997 if ((len = PyDict_Size(args)) < 0)
1998 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001999
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002000 if (len == 0) {
2001 if (put(self, args) >= 0)
2002 res = 0;
2003 goto finally;
2004 }
2005 if (put2(self, args) < 0)
2006 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002007
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002008 /* Materialize the dict items. */
2009 if (PyDict_CheckExact(args) && self->proto > 0) {
2010 /* We can take certain shortcuts if we know this is a dict and
2011 not a dict subclass. */
2012 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
2013 res = batch_dict_exact(self, args);
2014 Py_LeaveRecursiveCall();
2015 }
2016 } else {
2017 PyObject *iter = PyObject_CallMethod(args, "iteritems", "()");
2018 if (iter == NULL)
2019 goto finally;
2020 if (Py_EnterRecursiveCall(" while pickling an object") == 0) {
2021 res = batch_dict(self, iter);
2022 Py_LeaveRecursiveCall();
2023 }
2024 Py_DECREF(iter);
2025 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002026
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002027 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002028 if (self->fast && !fast_save_leave(self, args))
2029 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002031 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002032}
2033
2034
Tim Peters84e87f32001-03-17 04:50:51 +00002035static int
Tim Peterscba30e22003-02-01 06:24:36 +00002036save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002037{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002038 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
2039 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
2040 char *module_str, *name_str;
2041 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002042
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002043 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002044
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002045 if (self->fast && !fast_save_enter(self, args))
2046 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002047
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002048 if (self->write_func(self, &MARKv, 1) < 0)
2049 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002050
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002051 if (!( class = PyObject_GetAttr(args, __class___str)))
2052 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002053
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002054 if (self->bin) {
2055 if (save(self, class, 0) < 0)
2056 goto finally;
2057 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002058
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002059 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
2060 PyObject *element = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002061 Py_ssize_t i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002062
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002063 if (!( class_args =
2064 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
2065 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002066
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002067 if ((len = PyObject_Size(class_args)) < 0)
2068 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002069
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002070 for (i = 0; i < len; i++) {
2071 if (!( element = PySequence_GetItem(class_args, i)))
2072 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002073
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002074 if (save(self, element, 0) < 0) {
2075 Py_DECREF(element);
2076 goto finally;
2077 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002078
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002079 Py_DECREF(element);
2080 }
2081 }
2082 else {
2083 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2084 PyErr_Clear();
2085 else
2086 goto finally;
2087 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002088
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002089 if (!self->bin) {
2090 if (!( name = ((PyClassObject *)class)->cl_name )) {
2091 PyErr_SetString(PicklingError, "class has no name");
2092 goto finally;
2093 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002094
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002095 if (!( module = whichmodule(class, name)))
2096 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002097
Tim Peters84e87f32001-03-17 04:50:51 +00002098
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002099 if ((module_size = PyString_Size(module)) < 0 ||
2100 (name_size = PyString_Size(name)) < 0)
2101 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002102
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002103 module_str = PyString_AS_STRING((PyStringObject *)module);
2104 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002105
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002106 if (self->write_func(self, &inst, 1) < 0)
2107 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002108
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002109 if (self->write_func(self, module_str, module_size) < 0)
2110 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002111
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002112 if (self->write_func(self, "\n", 1) < 0)
2113 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002114
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002115 if (self->write_func(self, name_str, name_size) < 0)
2116 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002117
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002118 if (self->write_func(self, "\n", 1) < 0)
2119 goto finally;
2120 }
2121 else if (self->write_func(self, &obj, 1) < 0) {
2122 goto finally;
2123 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002124
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002125 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2126 state = PyObject_Call(getstate_func, empty_tuple, NULL);
2127 if (!state)
2128 goto finally;
2129 }
2130 else {
2131 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2132 PyErr_Clear();
2133 else
2134 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002135
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002136 if (!( state = PyObject_GetAttr(args, __dict___str))) {
2137 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2138 PyErr_Clear();
2139 else
2140 goto finally;
2141 res = 0;
2142 goto finally;
2143 }
2144 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002145
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002146 if (!PyDict_Check(state)) {
2147 if (put2(self, args) < 0)
2148 goto finally;
2149 }
2150 else {
2151 if (put(self, args) < 0)
2152 goto finally;
2153 }
Tim Peters84e87f32001-03-17 04:50:51 +00002154
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002155 if (save(self, state, 0) < 0)
2156 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002157
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002158 if (self->write_func(self, &build, 1) < 0)
2159 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002160
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002161 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002162
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002163 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002164 if (self->fast && !fast_save_leave(self, args))
2165 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002166
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002167 Py_XDECREF(module);
2168 Py_XDECREF(class);
2169 Py_XDECREF(state);
2170 Py_XDECREF(getinitargs_func);
2171 Py_XDECREF(getstate_func);
2172 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002173
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002174 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002175}
2176
2177
Guido van Rossum60456fd1997-04-09 17:36:32 +00002178static int
Tim Peterscba30e22003-02-01 06:24:36 +00002179save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002180{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002181 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
2182 char *name_str, *module_str;
2183 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002184
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002185 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002186
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002187 if (name) {
2188 global_name = name;
2189 Py_INCREF(global_name);
2190 }
2191 else {
2192 if (!( global_name = PyObject_GetAttr(args, __name___str)))
2193 goto finally;
2194 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002195
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002196 if (!( module = whichmodule(args, global_name)))
2197 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002198
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002199 if ((module_size = PyString_Size(module)) < 0 ||
2200 (name_size = PyString_Size(global_name)) < 0)
2201 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002202
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002203 module_str = PyString_AS_STRING((PyStringObject *)module);
2204 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002205
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002206 /* XXX This can be doing a relative import. Clearly it shouldn't,
2207 but I don't know how to stop it. :-( */
2208 mod = PyImport_ImportModule(module_str);
2209 if (mod == NULL) {
2210 cPickle_ErrFormat(PicklingError,
2211 "Can't pickle %s: import of module %s "
2212 "failed",
2213 "OS", args, module);
2214 goto finally;
2215 }
2216 klass = PyObject_GetAttrString(mod, name_str);
2217 if (klass == NULL) {
2218 cPickle_ErrFormat(PicklingError,
2219 "Can't pickle %s: attribute lookup %s.%s "
2220 "failed",
2221 "OSS", args, module, global_name);
2222 goto finally;
2223 }
2224 if (klass != args) {
2225 Py_DECREF(klass);
2226 cPickle_ErrFormat(PicklingError,
2227 "Can't pickle %s: it's not the same object "
2228 "as %s.%s",
2229 "OSS", args, module, global_name);
2230 goto finally;
2231 }
2232 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002233
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002234 if (self->proto >= 2) {
2235 /* See whether this is in the extension registry, and if
2236 * so generate an EXT opcode.
2237 */
2238 PyObject *py_code; /* extension code as Python object */
2239 long code; /* extension code as C value */
2240 char c_str[5];
2241 int n;
Tim Peters731098b2003-02-04 20:56:09 +00002242
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002243 PyTuple_SET_ITEM(two_tuple, 0, module);
2244 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2245 py_code = PyDict_GetItem(extension_registry, two_tuple);
2246 if (py_code == NULL)
2247 goto gen_global; /* not registered */
Tim Peters731098b2003-02-04 20:56:09 +00002248
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002249 /* Verify py_code has the right type and value. */
2250 if (!PyInt_Check(py_code)) {
2251 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2252 "extension code %s isn't an integer",
2253 "OO", args, py_code);
2254 goto finally;
2255 }
2256 code = PyInt_AS_LONG(py_code);
2257 if (code <= 0 || code > 0x7fffffffL) {
2258 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2259 "extension code %ld is out of range",
2260 "Ol", args, code);
2261 goto finally;
2262 }
Tim Peters731098b2003-02-04 20:56:09 +00002263
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002264 /* Generate an EXT opcode. */
2265 if (code <= 0xff) {
2266 c_str[0] = EXT1;
2267 c_str[1] = (char)code;
2268 n = 2;
2269 }
2270 else if (code <= 0xffff) {
2271 c_str[0] = EXT2;
2272 c_str[1] = (char)(code & 0xff);
2273 c_str[2] = (char)((code >> 8) & 0xff);
2274 n = 3;
2275 }
2276 else {
2277 c_str[0] = EXT4;
2278 c_str[1] = (char)(code & 0xff);
2279 c_str[2] = (char)((code >> 8) & 0xff);
2280 c_str[3] = (char)((code >> 16) & 0xff);
2281 c_str[4] = (char)((code >> 24) & 0xff);
2282 n = 5;
2283 }
Tim Peters731098b2003-02-04 20:56:09 +00002284
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002285 if (self->write_func(self, c_str, n) >= 0)
2286 res = 0;
2287 goto finally; /* and don't memoize */
2288 }
Tim Peters731098b2003-02-04 20:56:09 +00002289
2290 gen_global:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002291 if (self->write_func(self, &global, 1) < 0)
2292 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002293
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002294 if (self->write_func(self, module_str, module_size) < 0)
2295 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002296
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002297 if (self->write_func(self, "\n", 1) < 0)
2298 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002299
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002300 if (self->write_func(self, name_str, name_size) < 0)
2301 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002302
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002303 if (self->write_func(self, "\n", 1) < 0)
2304 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002305
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002306 if (put(self, args) < 0)
2307 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002308
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002309 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002310
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002311 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002312 Py_XDECREF(module);
2313 Py_XDECREF(global_name);
2314 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002315
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002316 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002317}
2318
Guido van Rossum60456fd1997-04-09 17:36:32 +00002319static int
Tim Peterscba30e22003-02-01 06:24:36 +00002320save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002321{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002322 PyObject *pid = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002323 Py_ssize_t size;
2324 int res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002325
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002326 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002327
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002328 Py_INCREF(args);
2329 ARG_TUP(self, args);
2330 if (self->arg) {
2331 pid = PyObject_Call(f, self->arg, NULL);
2332 FREE_ARG_TUP(self);
2333 }
2334 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002335
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002336 if (pid != Py_None) {
2337 if (!self->bin) {
2338 if (!PyString_Check(pid)) {
2339 PyErr_SetString(PicklingError,
2340 "persistent id must be string");
2341 goto finally;
2342 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002343
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002344 if (self->write_func(self, &persid, 1) < 0)
2345 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002346
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002347 if ((size = PyString_Size(pid)) < 0)
2348 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002349
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002350 if (self->write_func(self,
2351 PyString_AS_STRING(
2352 (PyStringObject *)pid),
2353 size) < 0)
2354 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002355
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002356 if (self->write_func(self, "\n", 1) < 0)
2357 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002358
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002359 res = 1;
2360 goto finally;
2361 }
2362 else if (save(self, pid, 1) >= 0) {
2363 if (self->write_func(self, &binpersid, 1) < 0)
2364 res = -1;
2365 else
2366 res = 1;
2367 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002368
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002369 goto finally;
2370 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002371
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002372 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002373
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002374 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002375 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002376
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002377 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002378}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002379
Tim Peters71fcda52003-02-14 23:05:28 +00002380/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2381 * appropriate __reduce__ method for ob.
2382 */
Tim Peters84e87f32001-03-17 04:50:51 +00002383static int
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002384save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002385{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002386 PyObject *callable;
2387 PyObject *argtup;
2388 PyObject *state = NULL;
2389 PyObject *listitems = Py_None;
2390 PyObject *dictitems = Py_None;
2391 Py_ssize_t size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002392
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002393 int use_newobj = self->proto >= 2;
Tim Peters71fcda52003-02-14 23:05:28 +00002394
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002395 static char reduce = REDUCE;
2396 static char build = BUILD;
2397 static char newobj = NEWOBJ;
Tim Peters71fcda52003-02-14 23:05:28 +00002398
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002399 size = PyTuple_Size(args);
2400 if (size < 2 || size > 5) {
2401 cPickle_ErrFormat(PicklingError, "tuple returned by "
2402 "%s must contain 2 through 5 elements",
2403 "O", fn);
2404 return -1;
2405 }
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002406
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002407 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2408 &callable,
2409 &argtup,
2410 &state,
2411 &listitems,
2412 &dictitems))
2413 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002414
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002415 if (!PyTuple_Check(argtup)) {
2416 cPickle_ErrFormat(PicklingError, "Second element of "
2417 "tuple returned by %s must be a tuple",
2418 "O", fn);
2419 return -1;
2420 }
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002421
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002422 if (state == Py_None)
2423 state = NULL;
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002424
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002425 if (listitems == Py_None)
2426 listitems = NULL;
2427 else if (!PyIter_Check(listitems)) {
2428 cPickle_ErrFormat(PicklingError, "Fourth element of "
2429 "tuple returned by %s must be an iterator, not %s",
2430 "Os", fn, Py_TYPE(listitems)->tp_name);
2431 return -1;
2432 }
Amaury Forgeot d'Arc69a9c5b2008-10-30 21:18:34 +00002433
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002434 if (dictitems == Py_None)
2435 dictitems = NULL;
2436 else if (!PyIter_Check(dictitems)) {
2437 cPickle_ErrFormat(PicklingError, "Fifth element of "
2438 "tuple returned by %s must be an iterator, not %s",
2439 "Os", fn, Py_TYPE(dictitems)->tp_name);
2440 return -1;
2441 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002442
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002443 /* Protocol 2 special case: if callable's name is __newobj__, use
2444 * NEWOBJ. This consumes a lot of code.
2445 */
2446 if (use_newobj) {
2447 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002448
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002449 if (temp == NULL) {
2450 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2451 PyErr_Clear();
2452 else
2453 return -1;
2454 use_newobj = 0;
2455 }
2456 else {
2457 use_newobj = PyString_Check(temp) &&
2458 strcmp(PyString_AS_STRING(temp),
2459 "__newobj__") == 0;
2460 Py_DECREF(temp);
2461 }
2462 }
2463 if (use_newobj) {
2464 PyObject *cls;
2465 PyObject *newargtup;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002466 Py_ssize_t n, i;
Tim Peters71fcda52003-02-14 23:05:28 +00002467
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002468 /* Sanity checks. */
2469 n = PyTuple_Size(argtup);
2470 if (n < 1) {
2471 PyErr_SetString(PicklingError, "__newobj__ arglist "
2472 "is empty");
2473 return -1;
2474 }
Tim Peters71fcda52003-02-14 23:05:28 +00002475
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002476 cls = PyTuple_GET_ITEM(argtup, 0);
2477 if (! PyObject_HasAttrString(cls, "__new__")) {
2478 PyErr_SetString(PicklingError, "args[0] from "
2479 "__newobj__ args has no __new__");
2480 return -1;
2481 }
Tim Peters71fcda52003-02-14 23:05:28 +00002482
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002483 /* XXX How could ob be NULL? */
2484 if (ob != NULL) {
2485 PyObject *ob_dot_class;
Tim Peters71fcda52003-02-14 23:05:28 +00002486
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002487 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2488 if (ob_dot_class == NULL) {
2489 if (PyErr_ExceptionMatches(
2490 PyExc_AttributeError))
2491 PyErr_Clear();
2492 else
2493 return -1;
2494 }
2495 i = ob_dot_class != cls; /* true iff a problem */
2496 Py_XDECREF(ob_dot_class);
2497 if (i) {
2498 PyErr_SetString(PicklingError, "args[0] from "
2499 "__newobj__ args has the wrong class");
2500 return -1;
2501 }
2502 }
Tim Peters71fcda52003-02-14 23:05:28 +00002503
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002504 /* Save the class and its __new__ arguments. */
2505 if (save(self, cls, 0) < 0)
2506 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002507
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002508 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2509 if (newargtup == NULL)
2510 return -1;
2511 for (i = 1; i < n; ++i) {
2512 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2513 Py_INCREF(temp);
2514 PyTuple_SET_ITEM(newargtup, i-1, temp);
2515 }
2516 i = save(self, newargtup, 0);
2517 Py_DECREF(newargtup);
2518 if (i < 0)
2519 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002520
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002521 /* Add NEWOBJ opcode. */
2522 if (self->write_func(self, &newobj, 1) < 0)
2523 return -1;
2524 }
2525 else {
2526 /* Not using NEWOBJ. */
2527 if (save(self, callable, 0) < 0 ||
2528 save(self, argtup, 0) < 0 ||
2529 self->write_func(self, &reduce, 1) < 0)
2530 return -1;
2531 }
Tim Peters71fcda52003-02-14 23:05:28 +00002532
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002533 /* Memoize. */
2534 /* XXX How can ob be NULL? */
2535 if (ob != NULL) {
2536 if (state && !PyDict_Check(state)) {
2537 if (put2(self, ob) < 0)
2538 return -1;
2539 }
2540 else if (put(self, ob) < 0)
2541 return -1;
2542 }
Tim Peters84e87f32001-03-17 04:50:51 +00002543
Guido van Rossum60456fd1997-04-09 17:36:32 +00002544
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002545 if (listitems && batch_list(self, listitems) < 0)
2546 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002547
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002548 if (dictitems && batch_dict(self, dictitems) < 0)
2549 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002550
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002551 if (state) {
2552 if (save(self, state, 0) < 0 ||
2553 self->write_func(self, &build, 1) < 0)
2554 return -1;
2555 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002556
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002557 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002558}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002559
Guido van Rossum60456fd1997-04-09 17:36:32 +00002560static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002561save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002562{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002563 PyTypeObject *type;
2564 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2565 int res = -1;
2566 int tmp;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002567
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002568 if (Py_EnterRecursiveCall(" while pickling an object"))
2569 return -1;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002570
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002571 if (!pers_save && self->pers_func) {
2572 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2573 res = tmp;
2574 goto finally;
2575 }
2576 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002577
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002578 if (args == Py_None) {
2579 res = save_none(self, args);
2580 goto finally;
2581 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002582
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002583 type = Py_TYPE(args);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002584
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002585 switch (type->tp_name[0]) {
2586 case 'b':
2587 if (args == Py_False || args == Py_True) {
2588 res = save_bool(self, args);
2589 goto finally;
2590 }
2591 break;
2592 case 'i':
2593 if (type == &PyInt_Type) {
2594 res = save_int(self, args);
2595 goto finally;
2596 }
2597 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002598
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002599 case 'l':
2600 if (type == &PyLong_Type) {
2601 res = save_long(self, args);
2602 goto finally;
2603 }
2604 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002605
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002606 case 'f':
2607 if (type == &PyFloat_Type) {
2608 res = save_float(self, args);
2609 goto finally;
2610 }
2611 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002612
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002613 case 't':
2614 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2615 res = save_tuple(self, args);
2616 goto finally;
2617 }
2618 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002619
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002620 case 's':
2621 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2622 res = save_string(self, args, 0);
2623 goto finally;
2624 }
2625 break;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002626
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002627#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002628 case 'u':
2629 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2630 res = save_unicode(self, args, 0);
2631 goto finally;
2632 }
2633 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002634#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002635 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002636
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002637 if (Py_REFCNT(args) > 1) {
2638 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2639 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002640
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002641 if (PyDict_GetItem(self->memo, py_ob_id)) {
2642 if (get(self, py_ob_id) < 0)
2643 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002644
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002645 res = 0;
2646 goto finally;
2647 }
2648 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002649
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002650 switch (type->tp_name[0]) {
2651 case 's':
2652 if (type == &PyString_Type) {
2653 res = save_string(self, args, 1);
2654 goto finally;
2655 }
2656 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002657
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002658#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002659 case 'u':
2660 if (type == &PyUnicode_Type) {
2661 res = save_unicode(self, args, 1);
2662 goto finally;
2663 }
2664 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002665#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002666
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002667 case 't':
2668 if (type == &PyTuple_Type) {
2669 res = save_tuple(self, args);
2670 goto finally;
2671 }
2672 if (type == &PyType_Type) {
Alexandre Vassalottidf9460f2013-11-30 17:43:42 -08002673 res = save_global(self, args, NULL);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002674 goto finally;
2675 }
2676 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002677
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002678 case 'l':
2679 if (type == &PyList_Type) {
2680 res = save_list(self, args);
2681 goto finally;
2682 }
2683 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002684
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002685 case 'd':
2686 if (type == &PyDict_Type) {
2687 res = save_dict(self, args);
2688 goto finally;
2689 }
2690 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002691
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002692 case 'i':
2693 if (type == &PyInstance_Type) {
2694 res = save_inst(self, args);
2695 goto finally;
2696 }
2697 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002698
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002699 case 'c':
2700 if (type == &PyClass_Type) {
2701 res = save_global(self, args, NULL);
2702 goto finally;
2703 }
2704 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002705
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002706 case 'f':
2707 if (type == &PyFunction_Type) {
2708 res = save_global(self, args, NULL);
2709 if (res && PyErr_ExceptionMatches(PickleError)) {
2710 /* fall back to reduce */
2711 PyErr_Clear();
2712 break;
2713 }
2714 goto finally;
2715 }
2716 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002717
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002718 case 'b':
2719 if (type == &PyCFunction_Type) {
2720 res = save_global(self, args, NULL);
2721 goto finally;
2722 }
2723 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002724
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002725 if (!pers_save && self->inst_pers_func) {
2726 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2727 res = tmp;
2728 goto finally;
2729 }
2730 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002731
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002732 /* Get a reduction callable, and call it. This may come from
2733 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2734 * or the object's __reduce__ method.
2735 */
2736 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2737 if (__reduce__ != NULL) {
2738 Py_INCREF(__reduce__);
2739 Py_INCREF(args);
2740 ARG_TUP(self, args);
2741 if (self->arg) {
2742 t = PyObject_Call(__reduce__, self->arg, NULL);
2743 FREE_ARG_TUP(self);
2744 }
2745 }
2746 else {
Antoine Pitrou561a8212011-10-04 09:34:48 +02002747 if (PyType_IsSubtype(type, &PyType_Type)) {
2748 res = save_global(self, args, NULL);
2749 goto finally;
2750 }
2751
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002752 /* Check for a __reduce_ex__ method. */
2753 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2754 if (__reduce__ != NULL) {
2755 t = PyInt_FromLong(self->proto);
2756 if (t != NULL) {
2757 ARG_TUP(self, t);
2758 t = NULL;
2759 if (self->arg) {
2760 t = PyObject_Call(__reduce__,
2761 self->arg, NULL);
2762 FREE_ARG_TUP(self);
2763 }
2764 }
2765 }
2766 else {
2767 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2768 PyErr_Clear();
2769 else
2770 goto finally;
2771 /* Check for a __reduce__ method. */
2772 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2773 if (__reduce__ != NULL) {
2774 t = PyObject_Call(__reduce__,
2775 empty_tuple, NULL);
2776 }
2777 else {
2778 PyErr_SetObject(UnpickleableError, args);
2779 goto finally;
2780 }
2781 }
2782 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002783
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002784 if (t == NULL)
2785 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002786
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002787 if (PyString_Check(t)) {
2788 res = save_global(self, args, t);
2789 goto finally;
2790 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002791
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002792 if (!PyTuple_Check(t)) {
2793 cPickle_ErrFormat(PicklingError, "Value returned by "
2794 "%s must be string or tuple",
2795 "O", __reduce__);
2796 goto finally;
2797 }
Tim Peters71fcda52003-02-14 23:05:28 +00002798
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002799 res = save_reduce(self, t, __reduce__, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002800
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002801 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002802 Py_LeaveRecursiveCall();
2803 Py_XDECREF(py_ob_id);
2804 Py_XDECREF(__reduce__);
2805 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002806
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002807 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002808}
2809
2810
2811static int
Tim Peterscba30e22003-02-01 06:24:36 +00002812dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002813{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002814 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002815
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002816 if (self->proto >= 2) {
2817 char bytes[2];
Tim Peters4190fb82003-02-02 16:09:05 +00002818
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002819 bytes[0] = PROTO;
2820 assert(self->proto >= 0 && self->proto < 256);
2821 bytes[1] = (char)self->proto;
2822 if (self->write_func(self, bytes, 2) < 0)
2823 return -1;
2824 }
Tim Peters4190fb82003-02-02 16:09:05 +00002825
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002826 if (save(self, args, 0) < 0)
2827 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002828
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002829 if (self->write_func(self, &stop, 1) < 0)
2830 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002831
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002832 if (self->write_func(self, NULL, 0) < 0)
2833 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002834
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002835 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002836}
2837
2838static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002839Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002840{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002841 if (self->memo)
2842 PyDict_Clear(self->memo);
2843 Py_INCREF(Py_None);
2844 return Py_None;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002845}
2846
2847static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002848Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002849{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02002850 Py_ssize_t l, i, rsize, ssize, clear=1, lm;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002851 long ik;
2852 PyObject *k, *r;
2853 char *s, *p, *have_get;
2854 Pdata *data;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002855
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002856 /* Can be called by Python code or C code */
2857 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2858 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002859
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002860 /* Check to make sure we are based on a list */
2861 if (! Pdata_Check(self->file)) {
2862 PyErr_SetString(PicklingError,
2863 "Attempt to getvalue() a non-list-based pickler");
2864 return NULL;
2865 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002866
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002867 /* flush write buffer */
2868 if (write_other(self, NULL, 0) < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002869
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002870 data=(Pdata*)self->file;
2871 l=data->length;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002872
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002873 /* set up an array to hold get/put status */
2874 lm = PyDict_Size(self->memo);
2875 if (lm < 0) return NULL;
2876 lm++;
2877 have_get = malloc(lm);
2878 if (have_get == NULL) return PyErr_NoMemory();
2879 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002880
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002881 /* Scan for gets. */
2882 for (rsize = 0, i = l; --i >= 0; ) {
2883 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002884
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002885 if (PyString_Check(k))
2886 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002887
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002888 else if (PyInt_Check(k)) { /* put */
2889 ik = PyInt_AS_LONG((PyIntObject*)k);
2890 if (ik >= lm || ik == 0) {
2891 PyErr_SetString(PicklingError,
2892 "Invalid get data");
2893 goto err;
2894 }
2895 if (have_get[ik]) /* with matching get */
2896 rsize += ik < 256 ? 2 : 5;
2897 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002898
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002899 else if (! (PyTuple_Check(k) &&
2900 PyTuple_GET_SIZE(k) == 2 &&
2901 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2902 ) {
2903 PyErr_SetString(PicklingError,
2904 "Unexpected data in internal list");
2905 goto err;
2906 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002907
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002908 else { /* put */
2909 ik = PyInt_AS_LONG((PyIntObject *)k);
2910 if (ik >= lm || ik == 0) {
2911 PyErr_SetString(PicklingError,
2912 "Invalid get data");
2913 return NULL;
2914 }
2915 have_get[ik] = 1;
2916 rsize += ik < 256 ? 2 : 5;
2917 }
2918 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002919
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002920 /* Now generate the result */
2921 r = PyString_FromStringAndSize(NULL, rsize);
2922 if (r == NULL) goto err;
2923 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002924
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002925 for (i = 0; i < l; i++) {
2926 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002927
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002928 if (PyString_Check(k)) {
2929 ssize = PyString_GET_SIZE(k);
2930 if (ssize) {
2931 p=PyString_AS_STRING((PyStringObject *)k);
2932 while (--ssize >= 0)
2933 *s++ = *p++;
2934 }
2935 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002936
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002937 else if (PyTuple_Check(k)) { /* get */
2938 ik = PyInt_AS_LONG((PyIntObject *)
2939 PyTuple_GET_ITEM(k, 0));
2940 if (ik < 256) {
2941 *s++ = BINGET;
2942 *s++ = (int)(ik & 0xff);
2943 }
2944 else {
2945 *s++ = LONG_BINGET;
2946 *s++ = (int)(ik & 0xff);
2947 *s++ = (int)((ik >> 8) & 0xff);
2948 *s++ = (int)((ik >> 16) & 0xff);
2949 *s++ = (int)((ik >> 24) & 0xff);
2950 }
2951 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002952
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002953 else { /* put */
2954 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002955
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002956 if (have_get[ik]) { /* with matching get */
2957 if (ik < 256) {
2958 *s++ = BINPUT;
2959 *s++ = (int)(ik & 0xff);
2960 }
2961 else {
2962 *s++ = LONG_BINPUT;
2963 *s++ = (int)(ik & 0xff);
2964 *s++ = (int)((ik >> 8) & 0xff);
2965 *s++ = (int)((ik >> 16) & 0xff);
2966 *s++ = (int)((ik >> 24) & 0xff);
2967 }
2968 }
2969 }
2970 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002971
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002972 if (clear) {
2973 PyDict_Clear(self->memo);
2974 Pdata_clear(data, 0);
2975 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002976
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002977 free(have_get);
2978 return r;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002979 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002980 free(have_get);
2981 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002982}
2983
2984static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002985Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002986{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002987 PyObject *ob;
2988 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002989
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002990 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
2991 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002992
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002993 if (dump(self, ob) < 0)
2994 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002995
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002996 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002997
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002998 /* XXX Why does dump() return self? */
2999 Py_INCREF(self);
3000 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003001}
3002
3003
Tim Peterscba30e22003-02-01 06:24:36 +00003004static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003005{
Neal Norwitzb0493252002-03-31 14:44:22 +00003006 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003007 PyDoc_STR("dump(object) -- "
3008 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00003009 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003010 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00003011 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00003012 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00003013 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003014};
3015
3016
3017static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00003018newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003019{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003020 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003021
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003022 if (proto < 0)
3023 proto = HIGHEST_PROTOCOL;
3024 if (proto > HIGHEST_PROTOCOL) {
3025 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
3026 "the highest available protocol is %d",
3027 proto, HIGHEST_PROTOCOL);
3028 return NULL;
3029 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003031 self = PyObject_GC_New(Picklerobject, &Picklertype);
3032 if (self == NULL)
3033 return NULL;
3034 self->proto = proto;
3035 self->bin = proto > 0;
3036 self->fp = NULL;
3037 self->write = NULL;
3038 self->memo = NULL;
3039 self->arg = NULL;
3040 self->pers_func = NULL;
3041 self->inst_pers_func = NULL;
3042 self->write_buf = NULL;
3043 self->fast = 0;
3044 self->fast_container = 0;
3045 self->fast_memo = NULL;
3046 self->buf_size = 0;
3047 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003048
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003049 self->file = NULL;
3050 if (file)
3051 Py_INCREF(file);
3052 else {
3053 file = Pdata_New();
3054 if (file == NULL)
3055 goto err;
3056 }
3057 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003058
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003059 if (!( self->memo = PyDict_New()))
3060 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003061
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003062 if (PyFile_Check(file)) {
3063 self->fp = PyFile_AsFile(file);
3064 if (self->fp == NULL) {
3065 PyErr_SetString(PyExc_ValueError,
3066 "I/O operation on closed file");
3067 goto err;
3068 }
3069 self->write_func = write_file;
3070 }
3071 else if (PycStringIO_OutputCheck(file)) {
3072 self->write_func = write_cStringIO;
3073 }
3074 else if (file == Py_None) {
3075 self->write_func = write_none;
3076 }
3077 else {
3078 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003079
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003080 if (! Pdata_Check(file)) {
3081 self->write = PyObject_GetAttr(file, write_str);
3082 if (!self->write) {
3083 PyErr_Clear();
3084 PyErr_SetString(PyExc_TypeError,
3085 "argument must have 'write' "
3086 "attribute");
3087 goto err;
3088 }
3089 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003090
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003091 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
3092 if (self->write_buf == NULL) {
3093 PyErr_NoMemory();
3094 goto err;
3095 }
3096 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003097
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003098 if (PyEval_GetRestricted()) {
3099 /* Restricted execution, get private tables */
3100 PyObject *m = PyImport_ImportModule("copy_reg");
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003101
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003102 if (m == NULL)
3103 goto err;
3104 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
3105 Py_DECREF(m);
3106 if (self->dispatch_table == NULL)
3107 goto err;
3108 }
3109 else {
3110 self->dispatch_table = dispatch_table;
3111 Py_INCREF(dispatch_table);
3112 }
3113 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003114
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003115 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003116
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003117 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003118 Py_DECREF(self);
3119 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003120}
3121
3122
3123static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00003124get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003125{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003126 static char *kwlist[] = {"file", "protocol", NULL};
3127 PyObject *file = NULL;
3128 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003129
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003130 /* XXX
3131 * The documented signature is Pickler(file, protocol=0), but this
3132 * accepts Pickler() and Pickler(integer) too. The meaning then
3133 * is clear as mud, undocumented, and not supported by pickle.py.
3134 * I'm told Zope uses this, but I haven't traced into this code
3135 * far enough to figure out what it means.
3136 */
3137 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
3138 PyErr_Clear();
3139 proto = 0;
3140 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
3141 kwlist, &file, &proto))
3142 return NULL;
3143 }
3144 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003145}
3146
3147
3148static void
Tim Peterscba30e22003-02-01 06:24:36 +00003149Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003150{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003151 PyObject_GC_UnTrack(self);
3152 Py_XDECREF(self->write);
3153 Py_XDECREF(self->memo);
3154 Py_XDECREF(self->fast_memo);
3155 Py_XDECREF(self->arg);
3156 Py_XDECREF(self->file);
3157 Py_XDECREF(self->pers_func);
3158 Py_XDECREF(self->inst_pers_func);
3159 Py_XDECREF(self->dispatch_table);
3160 PyMem_Free(self->write_buf);
3161 Py_TYPE(self)->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003162}
3163
3164static int
3165Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
3166{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003167 Py_VISIT(self->write);
3168 Py_VISIT(self->memo);
3169 Py_VISIT(self->fast_memo);
3170 Py_VISIT(self->arg);
3171 Py_VISIT(self->file);
3172 Py_VISIT(self->pers_func);
3173 Py_VISIT(self->inst_pers_func);
3174 Py_VISIT(self->dispatch_table);
3175 return 0;
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003176}
3177
3178static int
3179Pickler_clear(Picklerobject *self)
3180{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003181 Py_CLEAR(self->write);
3182 Py_CLEAR(self->memo);
3183 Py_CLEAR(self->fast_memo);
3184 Py_CLEAR(self->arg);
3185 Py_CLEAR(self->file);
3186 Py_CLEAR(self->pers_func);
3187 Py_CLEAR(self->inst_pers_func);
3188 Py_CLEAR(self->dispatch_table);
3189 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003190}
3191
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003192static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003193Pickler_get_pers_func(Picklerobject *p)
3194{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003195 if (p->pers_func == NULL)
3196 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3197 else
3198 Py_INCREF(p->pers_func);
3199 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003200}
3201
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003202static int
3203Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3204{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003205 if (v == NULL) {
3206 PyErr_SetString(PyExc_TypeError,
3207 "attribute deletion is not supported");
3208 return -1;
3209 }
3210 Py_XDECREF(p->pers_func);
3211 Py_INCREF(v);
3212 p->pers_func = v;
3213 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003214}
3215
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003216static int
3217Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3218{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003219 if (v == NULL) {
3220 PyErr_SetString(PyExc_TypeError,
3221 "attribute deletion is not supported");
3222 return -1;
3223 }
3224 Py_XDECREF(p->inst_pers_func);
3225 Py_INCREF(v);
3226 p->inst_pers_func = v;
3227 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003228}
3229
3230static PyObject *
3231Pickler_get_memo(Picklerobject *p)
3232{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003233 if (p->memo == NULL)
3234 PyErr_SetString(PyExc_AttributeError, "memo");
3235 else
3236 Py_INCREF(p->memo);
3237 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003238}
3239
3240static int
3241Pickler_set_memo(Picklerobject *p, PyObject *v)
3242{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003243 if (v == NULL) {
3244 PyErr_SetString(PyExc_TypeError,
3245 "attribute deletion is not supported");
3246 return -1;
3247 }
3248 if (!PyDict_Check(v)) {
3249 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3250 return -1;
3251 }
3252 Py_XDECREF(p->memo);
3253 Py_INCREF(v);
3254 p->memo = v;
3255 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003256}
3257
3258static PyObject *
3259Pickler_get_error(Picklerobject *p)
3260{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003261 /* why is this an attribute on the Pickler? */
3262 Py_INCREF(PicklingError);
3263 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003264}
3265
3266static PyMemberDef Pickler_members[] = {
3267 {"binary", T_INT, offsetof(Picklerobject, bin)},
3268 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003269 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003270};
3271
3272static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003273 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003274 (setter)Pickler_set_pers_func},
3275 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3276 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003277 {"PicklingError", (getter)Pickler_get_error, NULL},
3278 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003279};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003280
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003281PyDoc_STRVAR(Picklertype__doc__,
3282"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003283
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003284static PyTypeObject Picklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003285 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00003286 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003287 sizeof(Picklerobject), /*tp_basicsize*/
3288 0,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003289 (destructor)Pickler_dealloc, /* tp_dealloc */
3290 0, /* tp_print */
3291 0, /* tp_getattr */
3292 0, /* tp_setattr */
3293 0, /* tp_compare */
3294 0, /* tp_repr */
3295 0, /* tp_as_number */
3296 0, /* tp_as_sequence */
3297 0, /* tp_as_mapping */
3298 0, /* tp_hash */
3299 0, /* tp_call */
3300 0, /* tp_str */
3301 PyObject_GenericGetAttr, /* tp_getattro */
3302 PyObject_GenericSetAttr, /* tp_setattro */
3303 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003304 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003305 Picklertype__doc__, /* tp_doc */
3306 (traverseproc)Pickler_traverse, /* tp_traverse */
3307 (inquiry)Pickler_clear, /* tp_clear */
3308 0, /* tp_richcompare */
3309 0, /* tp_weaklistoffset */
3310 0, /* tp_iter */
3311 0, /* tp_iternext */
3312 Pickler_methods, /* tp_methods */
3313 Pickler_members, /* tp_members */
3314 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003315};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003316
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003317static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003318find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003319{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003320 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003321
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003322 if (fc) {
3323 if (fc==Py_None) {
3324 PyErr_SetString(UnpicklingError, "Global and instance "
3325 "pickles are not supported.");
3326 return NULL;
3327 }
3328 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3329 py_global_name, NULL);
3330 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003331
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003332 module = PySys_GetObject("modules");
3333 if (module == NULL)
3334 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003335
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003336 module = PyDict_GetItem(module, py_module_name);
3337 if (module == NULL) {
3338 module = PyImport_Import(py_module_name);
3339 if (!module)
3340 return NULL;
3341 global = PyObject_GetAttr(module, py_global_name);
3342 Py_DECREF(module);
3343 }
3344 else
3345 global = PyObject_GetAttr(module, py_global_name);
3346 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003347}
3348
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003349static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +00003350marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003351{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003352 if (self->num_marks < 1) {
3353 PyErr_SetString(UnpicklingError, "could not find MARK");
3354 return -1;
3355 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003356
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003357 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003358}
3359
Tim Peters84e87f32001-03-17 04:50:51 +00003360
Guido van Rossum60456fd1997-04-09 17:36:32 +00003361static int
Tim Peterscba30e22003-02-01 06:24:36 +00003362load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003363{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003364 PDATA_APPEND(self->stack, Py_None, -1);
3365 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003366}
3367
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003368static int
Tim Peterscba30e22003-02-01 06:24:36 +00003369bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003370{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003371 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3372 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003373}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003374
3375static int
Tim Peterscba30e22003-02-01 06:24:36 +00003376load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003377{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003378 PyObject *py_int = 0;
3379 char *endptr, *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003380 Py_ssize_t len;
3381 int res = -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003382 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003383
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003384 if ((len = self->readline_func(self, &s)) < 0) return -1;
3385 if (len < 2) return bad_readline();
3386 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003387
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003388 errno = 0;
3389 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003390
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003391 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3392 /* Hm, maybe we've got something long. Let's try reading
3393 it as a Python long object. */
3394 errno = 0;
3395 py_int = PyLong_FromString(s, NULL, 0);
3396 if (py_int == NULL) {
3397 PyErr_SetString(PyExc_ValueError,
3398 "could not convert string to int");
3399 goto finally;
3400 }
3401 }
3402 else {
3403 if (len == 3 && (l == 0 || l == 1)) {
3404 if (!( py_int = PyBool_FromLong(l))) goto finally;
3405 }
3406 else {
3407 if (!( py_int = PyInt_FromLong(l))) goto finally;
3408 }
3409 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003410
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003411 free(s);
3412 PDATA_PUSH(self->stack, py_int, -1);
3413 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003414
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003415 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003416 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003417
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003418 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003419}
3420
Tim Peters3c67d792003-02-02 17:59:11 +00003421static int
3422load_bool(Unpicklerobject *self, PyObject *boolean)
3423{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003424 assert(boolean == Py_True || boolean == Py_False);
3425 PDATA_APPEND(self->stack, boolean, -1);
3426 return 0;
Tim Peters3c67d792003-02-02 17:59:11 +00003427}
3428
Tim Petersee1a53c2003-02-02 02:57:53 +00003429/* s contains x bytes of a little-endian integer. Return its value as a
3430 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3431 * int, but when x is 4 it's a signed one. This is an historical source
3432 * of x-platform bugs.
3433 */
Tim Peters84e87f32001-03-17 04:50:51 +00003434static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003435calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003436{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003437 unsigned char c;
3438 int i;
3439 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003440
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003441 for (i = 0, l = 0L; i < x; i++) {
3442 c = (unsigned char)s[i];
3443 l |= (long)c << (i * 8);
3444 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003445#if SIZEOF_LONG > 4
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003446 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3447 * is signed, so on a box with longs bigger than 4 bytes we need
3448 * to extend a BININT's sign bit to the full width.
3449 */
3450 if (x == 4 && l & (1L << 31))
3451 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003452#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003453 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003454}
3455
3456
3457static int
Tim Peterscba30e22003-02-01 06:24:36 +00003458load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003459{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003460 PyObject *py_int = 0;
3461 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003462
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003463 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003464
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003465 if (!( py_int = PyInt_FromLong(l)))
3466 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003467
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003468 PDATA_PUSH(self->stack, py_int, -1);
3469 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003470}
3471
3472
3473static int
Tim Peterscba30e22003-02-01 06:24:36 +00003474load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003475{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003476 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003477
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003478 if (self->read_func(self, &s, 4) < 0)
3479 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003480
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003481 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003482}
3483
3484
3485static int
Tim Peterscba30e22003-02-01 06:24:36 +00003486load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003487{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003488 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003489
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003490 if (self->read_func(self, &s, 1) < 0)
3491 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003492
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003493 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003494}
3495
3496
3497static int
Tim Peterscba30e22003-02-01 06:24:36 +00003498load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003499{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003500 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003501
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003502 if (self->read_func(self, &s, 2) < 0)
3503 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003504
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003505 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003506}
Tim Peters84e87f32001-03-17 04:50:51 +00003507
Guido van Rossum60456fd1997-04-09 17:36:32 +00003508static int
Tim Peterscba30e22003-02-01 06:24:36 +00003509load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003510{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003511 PyObject *l = 0;
3512 char *end, *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003513 Py_ssize_t len;
3514 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003515
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003516 if ((len = self->readline_func(self, &s)) < 0) return -1;
3517 if (len < 2) return bad_readline();
3518 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003519
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003520 if (!( l = PyLong_FromString(s, &end, 0)))
3521 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003522
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003523 free(s);
3524 PDATA_PUSH(self->stack, l, -1);
3525 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003526
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003527 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003528 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003529
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003530 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003531}
3532
Tim Petersee1a53c2003-02-02 02:57:53 +00003533/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3534 * data following.
3535 */
3536static int
3537load_counted_long(Unpicklerobject *self, int size)
3538{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003539 Py_ssize_t i;
3540 char *nbytes;
3541 unsigned char *pdata;
3542 PyObject *along;
Tim Petersee1a53c2003-02-02 02:57:53 +00003543
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003544 assert(size == 1 || size == 4);
3545 i = self->read_func(self, &nbytes, size);
3546 if (i < 0) return -1;
Tim Petersee1a53c2003-02-02 02:57:53 +00003547
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003548 size = calc_binint(nbytes, size);
3549 if (size < 0) {
3550 /* Corrupt or hostile pickle -- we never write one like
3551 * this.
3552 */
3553 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3554 "byte count");
3555 return -1;
3556 }
Tim Petersee1a53c2003-02-02 02:57:53 +00003557
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003558 if (size == 0)
3559 along = PyLong_FromLong(0L);
3560 else {
3561 /* Read the raw little-endian bytes & convert. */
3562 i = self->read_func(self, (char **)&pdata, size);
3563 if (i < 0) return -1;
3564 along = _PyLong_FromByteArray(pdata, (size_t)size,
3565 1 /* little endian */, 1 /* signed */);
3566 }
3567 if (along == NULL)
3568 return -1;
3569 PDATA_PUSH(self->stack, along, -1);
3570 return 0;
Tim Petersee1a53c2003-02-02 02:57:53 +00003571}
Tim Peters84e87f32001-03-17 04:50:51 +00003572
Guido van Rossum60456fd1997-04-09 17:36:32 +00003573static int
Tim Peterscba30e22003-02-01 06:24:36 +00003574load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003575{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003576 PyObject *py_float = 0;
3577 char *endptr, *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003578 Py_ssize_t len;
3579 int res = -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003580 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003581
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003582 if ((len = self->readline_func(self, &s)) < 0) return -1;
3583 if (len < 2) return bad_readline();
3584 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003585
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003586 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003587
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003588 if (d == -1.0 && PyErr_Occurred()) {
3589 goto finally;
3590 } else if ((endptr[0] != '\n') || (endptr[1] != '\0')) {
3591 PyErr_SetString(PyExc_ValueError,
3592 "could not convert string to float");
3593 goto finally;
3594 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003595
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003596 if (!( py_float = PyFloat_FromDouble(d)))
3597 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003598
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003599 free(s);
3600 PDATA_PUSH(self->stack, py_float, -1);
3601 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003602
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003603 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003604 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003605
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003606 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003607}
3608
Guido van Rossum60456fd1997-04-09 17:36:32 +00003609static int
Tim Peterscba30e22003-02-01 06:24:36 +00003610load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003611{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003612 PyObject *py_float;
3613 double x;
3614 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003615
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003616 if (self->read_func(self, &p, 8) < 0)
3617 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003618
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003619 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3620 if (x == -1.0 && PyErr_Occurred())
3621 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003622
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003623 py_float = PyFloat_FromDouble(x);
3624 if (py_float == NULL)
3625 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003626
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003627 PDATA_PUSH(self->stack, py_float, -1);
3628 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003629}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003630
3631static int
Tim Peterscba30e22003-02-01 06:24:36 +00003632load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003633{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003634 PyObject *str = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003635 Py_ssize_t len;
3636 int res = -1;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003637 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003638
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003639 if ((len = self->readline_func(self, &s)) < 0) return -1;
3640 if (len < 2) return bad_readline();
3641 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003642
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003643
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003644 /* Strip outermost quotes */
Antoine Pitroube929712013-04-15 21:35:25 +02003645 while (len > 0 && s[len-1] <= ' ')
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003646 len--;
Antoine Pitroube929712013-04-15 21:35:25 +02003647 if (len > 1 && s[0]=='"' && s[len-1]=='"') {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003648 s[len-1] = '\0';
3649 p = s + 1 ;
3650 len -= 2;
Antoine Pitroube929712013-04-15 21:35:25 +02003651 }
3652 else if (len > 1 && s[0]=='\'' && s[len-1]=='\'') {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003653 s[len-1] = '\0';
3654 p = s + 1 ;
3655 len -= 2;
Antoine Pitroube929712013-04-15 21:35:25 +02003656 }
3657 else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003658 goto insecure;
3659 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003660
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003661 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3662 free(s);
3663 if (str) {
3664 PDATA_PUSH(self->stack, str, -1);
3665 res = 0;
3666 }
3667 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003669 insecure:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003670 free(s);
3671 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3672 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003673}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003674
3675
3676static int
Tim Peterscba30e22003-02-01 06:24:36 +00003677load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003678{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003679 PyObject *py_string = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003680 Py_ssize_t l;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003681 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003682
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003683 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003684
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003685 l = calc_binint(s, 4);
3686 if (l < 0) {
3687 /* Corrupt or hostile pickle -- we never write one like
3688 * this.
3689 */
3690 PyErr_SetString(UnpicklingError,
3691 "BINSTRING pickle has negative byte count");
3692 return -1;
3693 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003694
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003695 if (self->read_func(self, &s, l) < 0)
3696 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003697
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003698 if (!( py_string = PyString_FromStringAndSize(s, l)))
3699 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003700
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003701 PDATA_PUSH(self->stack, py_string, -1);
3702 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003703}
3704
3705
3706static int
Tim Peterscba30e22003-02-01 06:24:36 +00003707load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003708{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003709 PyObject *py_string = 0;
3710 unsigned char l;
3711 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003712
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003713 if (self->read_func(self, &s, 1) < 0)
3714 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003715
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003716 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003717
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003718 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003719
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003720 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003721
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003722 PDATA_PUSH(self->stack, py_string, -1);
3723 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003724}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003725
3726
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003727#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003728static int
Tim Peterscba30e22003-02-01 06:24:36 +00003729load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003730{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003731 PyObject *str = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003732 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003733 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003734
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003735 if ((len = self->readline_func(self, &s)) < 0) return -1;
3736 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003737
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003738 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003739 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003740
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003741 PDATA_PUSH(self->stack, str, -1);
3742 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003743}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003744#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003745
3746
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003747#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003748static int
Tim Peterscba30e22003-02-01 06:24:36 +00003749load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003750{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003751 PyObject *unicode;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003752 Py_ssize_t l;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003753 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003754
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003755 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003756
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003757 l = calc_binint(s, 4);
3758 if (l < 0) {
3759 /* Corrupt or hostile pickle -- we never write one like
3760 * this.
3761 */
3762 PyErr_SetString(UnpicklingError,
3763 "BINUNICODE pickle has negative byte count");
3764 return -1;
3765 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003766
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003767 if (self->read_func(self, &s, l) < 0)
3768 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003769
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003770 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3771 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003772
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003773 PDATA_PUSH(self->stack, unicode, -1);
3774 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003775}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003776#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003777
3778
3779static int
Tim Peterscba30e22003-02-01 06:24:36 +00003780load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003781{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003782 PyObject *tup;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003783 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003784
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003785 if ((i = marker(self)) < 0) return -1;
3786 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3787 PDATA_PUSH(self->stack, tup, -1);
3788 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003789}
3790
3791static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003792load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003793{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003794 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003795
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003796 if (tup == NULL)
3797 return -1;
Tim Peters1d63c9f2003-02-02 20:29:39 +00003798
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003799 while (--len >= 0) {
3800 PyObject *element;
Tim Peters1d63c9f2003-02-02 20:29:39 +00003801
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003802 PDATA_POP(self->stack, element);
3803 if (element == NULL)
3804 return -1;
3805 PyTuple_SET_ITEM(tup, len, element);
3806 }
3807 PDATA_PUSH(self->stack, tup, -1);
3808 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003809}
3810
3811static int
Tim Peterscba30e22003-02-01 06:24:36 +00003812load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003813{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003814 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003815
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003816 if (!( list=PyList_New(0))) return -1;
3817 PDATA_PUSH(self->stack, list, -1);
3818 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003819}
3820
3821static int
Tim Peterscba30e22003-02-01 06:24:36 +00003822load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003823{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003824 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003825
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003826 if (!( dict=PyDict_New())) return -1;
3827 PDATA_PUSH(self->stack, dict, -1);
3828 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003829}
3830
3831
3832static int
Tim Peterscba30e22003-02-01 06:24:36 +00003833load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003834{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003835 PyObject *list = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003836 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003837
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003838 if ((i = marker(self)) < 0) return -1;
3839 if (!( list=Pdata_popList(self->stack, i))) return -1;
3840 PDATA_PUSH(self->stack, list, -1);
3841 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003842}
3843
3844static int
Tim Peterscba30e22003-02-01 06:24:36 +00003845load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003846{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003847 PyObject *dict, *key, *value;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003848 Py_ssize_t i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003849
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003850 if ((i = marker(self)) < 0) return -1;
3851 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003852
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003853 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003854
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003855 for (k = i+1; k < j; k += 2) {
3856 key =self->stack->data[k-1];
3857 value=self->stack->data[k ];
3858 if (PyDict_SetItem(dict, key, value) < 0) {
3859 Py_DECREF(dict);
3860 return -1;
3861 }
3862 }
3863 Pdata_clear(self->stack, i);
3864 PDATA_PUSH(self->stack, dict, -1);
3865 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003866}
3867
3868static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003869Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003870{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003871 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003872
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003873 if (PyClass_Check(cls)) {
3874 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003875
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003876 if ((l=PyObject_Size(args)) < 0) goto err;
3877 if (!( l )) {
3878 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003879
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003880 __getinitargs__ = PyObject_GetAttr(cls,
3881 __getinitargs___str);
3882 if (!__getinitargs__) {
3883 /* We have a class with no __getinitargs__,
3884 so bypass usual construction */
3885 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003886
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003887 PyErr_Clear();
3888 if (!( inst=PyInstance_NewRaw(cls, NULL)))
3889 goto err;
3890 return inst;
3891 }
3892 Py_DECREF(__getinitargs__);
3893 }
Tim Peters84e87f32001-03-17 04:50:51 +00003894
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003895 if ((r=PyInstance_New(cls, args, NULL))) return r;
3896 else goto err;
3897 }
Tim Peters84e87f32001-03-17 04:50:51 +00003898
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003899 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003900
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003901 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003902 {
3903 PyObject *tp, *v, *tb, *tmp_value;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003904
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003905 PyErr_Fetch(&tp, &v, &tb);
3906 tmp_value = v;
3907 /* NULL occurs when there was a KeyboardInterrupt */
3908 if (tmp_value == NULL)
3909 tmp_value = Py_None;
3910 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3911 Py_XDECREF(v);
3912 v=r;
3913 }
3914 PyErr_Restore(tp,v,tb);
3915 }
3916 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003917}
Tim Peters84e87f32001-03-17 04:50:51 +00003918
Guido van Rossum60456fd1997-04-09 17:36:32 +00003919
3920static int
Tim Peterscba30e22003-02-01 06:24:36 +00003921load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003922{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003923 PyObject *class, *tup, *obj=0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003924 Py_ssize_t i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003925
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003926 if ((i = marker(self)) < 0) return -1;
3927 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3928 PDATA_POP(self->stack, class);
3929 if (class) {
3930 obj = Instance_New(class, tup);
3931 Py_DECREF(class);
3932 }
3933 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003934
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003935 if (! obj) return -1;
3936 PDATA_PUSH(self->stack, obj, -1);
3937 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003938}
3939
3940
3941static int
Tim Peterscba30e22003-02-01 06:24:36 +00003942load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003943{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003944 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02003945 Py_ssize_t i, len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003946 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003947
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003948 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003949
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003950 if ((len = self->readline_func(self, &s)) < 0) return -1;
3951 if (len < 2) return bad_readline();
3952 module_name = PyString_FromStringAndSize(s, len - 1);
3953 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003954
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003955 if ((len = self->readline_func(self, &s)) >= 0) {
3956 if (len < 2) return bad_readline();
3957 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3958 class = find_class(module_name, class_name,
3959 self->find_class);
3960 Py_DECREF(class_name);
3961 }
3962 }
3963 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003964
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003965 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003966
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003967 if ((tup=Pdata_popTuple(self->stack, i))) {
3968 obj = Instance_New(class, tup);
3969 Py_DECREF(tup);
3970 }
3971 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003972
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003973 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003974
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003975 PDATA_PUSH(self->stack, obj, -1);
3976 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003977}
3978
Tim Peterseab7db32003-02-13 18:24:14 +00003979static int
3980load_newobj(Unpicklerobject *self)
3981{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003982 PyObject *args = NULL;
3983 PyObject *clsraw = NULL;
3984 PyTypeObject *cls; /* clsraw cast to its true type */
3985 PyObject *obj;
Tim Peterseab7db32003-02-13 18:24:14 +00003986
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003987 /* Stack is ... cls argtuple, and we want to call
3988 * cls.__new__(cls, *argtuple).
3989 */
3990 PDATA_POP(self->stack, args);
3991 if (args == NULL) goto Fail;
3992 if (! PyTuple_Check(args)) {
3993 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3994 "tuple.");
3995 goto Fail;
3996 }
Tim Peterseab7db32003-02-13 18:24:14 +00003997
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003998 PDATA_POP(self->stack, clsraw);
3999 cls = (PyTypeObject *)clsraw;
4000 if (cls == NULL) goto Fail;
4001 if (! PyType_Check(cls)) {
4002 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
4003 "isn't a type object");
4004 goto Fail;
4005 }
4006 if (cls->tp_new == NULL) {
4007 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
4008 "has NULL tp_new");
4009 goto Fail;
4010 }
Tim Peterseab7db32003-02-13 18:24:14 +00004011
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004012 /* Call __new__. */
4013 obj = cls->tp_new(cls, args, NULL);
4014 if (obj == NULL) goto Fail;
Tim Peterseab7db32003-02-13 18:24:14 +00004015
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004016 Py_DECREF(args);
4017 Py_DECREF(clsraw);
4018 PDATA_PUSH(self->stack, obj, -1);
4019 return 0;
Tim Peterseab7db32003-02-13 18:24:14 +00004020
4021 Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004022 Py_XDECREF(args);
4023 Py_XDECREF(clsraw);
4024 return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004025}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004026
4027static int
Tim Peterscba30e22003-02-01 06:24:36 +00004028load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004029{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004030 PyObject *class = 0, *module_name = 0, *class_name = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004031 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004032 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004033
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004034 if ((len = self->readline_func(self, &s)) < 0) return -1;
4035 if (len < 2) return bad_readline();
4036 module_name = PyString_FromStringAndSize(s, len - 1);
4037 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004038
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004039 if ((len = self->readline_func(self, &s)) >= 0) {
4040 if (len < 2) {
4041 Py_DECREF(module_name);
4042 return bad_readline();
4043 }
4044 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
4045 class = find_class(module_name, class_name,
4046 self->find_class);
4047 Py_DECREF(class_name);
4048 }
4049 }
4050 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004051
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004052 if (! class) return -1;
4053 PDATA_PUSH(self->stack, class, -1);
4054 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004055}
4056
4057
4058static int
Tim Peterscba30e22003-02-01 06:24:36 +00004059load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004060{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004061 PyObject *pid = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004062 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004063 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004064
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004065 if (self->pers_func) {
4066 if ((len = self->readline_func(self, &s)) < 0) return -1;
4067 if (len < 2) return bad_readline();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004068
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004069 pid = PyString_FromStringAndSize(s, len - 1);
4070 if (!pid) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004071
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004072 if (PyList_Check(self->pers_func)) {
4073 if (PyList_Append(self->pers_func, pid) < 0) {
4074 Py_DECREF(pid);
4075 return -1;
4076 }
4077 }
4078 else {
4079 ARG_TUP(self, pid);
4080 if (self->arg) {
4081 pid = PyObject_Call(self->pers_func, self->arg,
4082 NULL);
4083 FREE_ARG_TUP(self);
4084 }
4085 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004086
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004087 if (! pid) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004088
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004089 PDATA_PUSH(self->stack, pid, -1);
4090 return 0;
4091 }
4092 else {
4093 PyErr_SetString(UnpicklingError,
4094 "A load persistent id instruction was encountered,\n"
4095 "but no persistent_load function was specified.");
4096 return -1;
4097 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004098}
4099
4100static int
Tim Peterscba30e22003-02-01 06:24:36 +00004101load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004102{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004103 PyObject *pid = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004104
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004105 if (self->pers_func) {
4106 PDATA_POP(self->stack, pid);
4107 if (! pid) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004108
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004109 if (PyList_Check(self->pers_func)) {
4110 if (PyList_Append(self->pers_func, pid) < 0) {
4111 Py_DECREF(pid);
4112 return -1;
4113 }
4114 }
4115 else {
4116 ARG_TUP(self, pid);
4117 if (self->arg) {
4118 pid = PyObject_Call(self->pers_func, self->arg,
4119 NULL);
4120 FREE_ARG_TUP(self);
4121 }
4122 if (! pid) return -1;
4123 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004124
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004125 PDATA_PUSH(self->stack, pid, -1);
4126 return 0;
4127 }
4128 else {
4129 PyErr_SetString(UnpicklingError,
4130 "A load persistent id instruction was encountered,\n"
4131 "but no persistent_load function was specified.");
4132 return -1;
4133 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004134}
4135
4136
4137static int
Tim Peterscba30e22003-02-01 06:24:36 +00004138load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004139{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004140 Py_ssize_t len = self->stack->length;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004141
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004142 /* Note that we split the (pickle.py) stack into two stacks,
4143 an object stack and a mark stack. We have to be clever and
4144 pop the right one. We do this by looking at the top of the
4145 mark stack first, and only signalling a stack underflow if
4146 the object stack is empty and the mark stack doesn't match
4147 our expectations.
4148 */
4149 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
4150 self->num_marks--;
4151 } else if (len > 0) {
4152 len--;
4153 Py_DECREF(self->stack->data[len]);
4154 self->stack->length = len;
4155 } else {
4156 return stackUnderflow();
4157 }
4158 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004159}
4160
4161
4162static int
Tim Peterscba30e22003-02-01 06:24:36 +00004163load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004164{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004165 Py_ssize_t i;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004166
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004167 if ((i = marker(self)) < 0)
4168 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004169
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004170 Pdata_clear(self->stack, i);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004171
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004172 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004173}
4174
4175
4176static int
Tim Peterscba30e22003-02-01 06:24:36 +00004177load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004178{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004179 PyObject *last;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004180 Py_ssize_t len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004181
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004182 if ((len = self->stack->length) <= 0) return stackUnderflow();
4183 last=self->stack->data[len-1];
4184 Py_INCREF(last);
4185 PDATA_PUSH(self->stack, last, -1);
4186 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004187}
4188
4189
4190static int
Tim Peterscba30e22003-02-01 06:24:36 +00004191load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004192{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004193 PyObject *py_str = 0, *value = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004194 Py_ssize_t len;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004195 char *s;
4196 int rc;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004197
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004198 if ((len = self->readline_func(self, &s)) < 0) return -1;
4199 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00004200
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004201 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004202
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004203 value = PyDict_GetItem(self->memo, py_str);
4204 if (! value) {
4205 PyErr_SetObject(BadPickleGet, py_str);
4206 rc = -1;
4207 }
4208 else {
4209 PDATA_APPEND(self->stack, value, -1);
4210 rc = 0;
4211 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004212
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004213 Py_DECREF(py_str);
4214 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004215}
4216
4217
4218static int
Tim Peterscba30e22003-02-01 06:24:36 +00004219load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004220{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004221 PyObject *py_key = 0, *value = 0;
4222 unsigned char key;
4223 char *s;
4224 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004225
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004226 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004227
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004228 key = (unsigned char)s[0];
4229 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00004230
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004231 value = PyDict_GetItem(self->memo, py_key);
4232 if (! value) {
4233 PyErr_SetObject(BadPickleGet, py_key);
4234 rc = -1;
4235 }
4236 else {
4237 PDATA_APPEND(self->stack, value, -1);
4238 rc = 0;
4239 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004240
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004241 Py_DECREF(py_key);
4242 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004243}
4244
4245
4246static int
Tim Peterscba30e22003-02-01 06:24:36 +00004247load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004248{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004249 PyObject *py_key = 0, *value = 0;
4250 unsigned char c;
4251 char *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004252 Py_ssize_t key;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004253 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004254
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004255 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004256
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004257 c = (unsigned char)s[0];
4258 key = (long)c;
4259 c = (unsigned char)s[1];
4260 key |= (long)c << 8;
4261 c = (unsigned char)s[2];
4262 key |= (long)c << 16;
4263 c = (unsigned char)s[3];
4264 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004265
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004266 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004267
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004268 value = PyDict_GetItem(self->memo, py_key);
4269 if (! value) {
4270 PyErr_SetObject(BadPickleGet, py_key);
4271 rc = -1;
4272 }
4273 else {
4274 PDATA_APPEND(self->stack, value, -1);
4275 rc = 0;
4276 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004277
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004278 Py_DECREF(py_key);
4279 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004280}
4281
Tim Peters2d629652003-02-04 05:06:17 +00004282/* Push an object from the extension registry (EXT[124]). nbytes is
4283 * the number of bytes following the opcode, holding the index (code) value.
4284 */
4285static int
4286load_extension(Unpicklerobject *self, int nbytes)
4287{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004288 char *codebytes; /* the nbytes bytes after the opcode */
4289 long code; /* calc_binint returns long */
4290 PyObject *py_code; /* code as a Python int */
4291 PyObject *obj; /* the object to push */
4292 PyObject *pair; /* (module_name, class_name) */
4293 PyObject *module_name, *class_name;
Tim Peters2d629652003-02-04 05:06:17 +00004294
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004295 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4296 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4297 code = calc_binint(codebytes, nbytes);
4298 if (code <= 0) { /* note that 0 is forbidden */
4299 /* Corrupt or hostile pickle. */
4300 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4301 return -1;
4302 }
Tim Peters2d629652003-02-04 05:06:17 +00004303
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004304 /* Look for the code in the cache. */
4305 py_code = PyInt_FromLong(code);
4306 if (py_code == NULL) return -1;
4307 obj = PyDict_GetItem(extension_cache, py_code);
4308 if (obj != NULL) {
4309 /* Bingo. */
4310 Py_DECREF(py_code);
4311 PDATA_APPEND(self->stack, obj, -1);
4312 return 0;
4313 }
Tim Peters2d629652003-02-04 05:06:17 +00004314
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004315 /* Look up the (module_name, class_name) pair. */
4316 pair = PyDict_GetItem(inverted_registry, py_code);
4317 if (pair == NULL) {
4318 Py_DECREF(py_code);
4319 PyErr_Format(PyExc_ValueError, "unregistered extension "
4320 "code %ld", code);
4321 return -1;
4322 }
4323 /* Since the extension registry is manipulable via Python code,
4324 * confirm that pair is really a 2-tuple of strings.
4325 */
4326 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4327 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4328 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4329 Py_DECREF(py_code);
4330 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4331 "isn't a 2-tuple of strings", code);
4332 return -1;
4333 }
4334 /* Load the object. */
4335 obj = find_class(module_name, class_name, self->find_class);
4336 if (obj == NULL) {
4337 Py_DECREF(py_code);
4338 return -1;
4339 }
4340 /* Cache code -> obj. */
4341 code = PyDict_SetItem(extension_cache, py_code, obj);
4342 Py_DECREF(py_code);
4343 if (code < 0) {
4344 Py_DECREF(obj);
4345 return -1;
4346 }
4347 PDATA_PUSH(self->stack, obj, -1);
4348 return 0;
Tim Peters2d629652003-02-04 05:06:17 +00004349}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004350
4351static int
Tim Peterscba30e22003-02-01 06:24:36 +00004352load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004353{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004354 PyObject *py_str = 0, *value = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004355 Py_ssize_t len, l;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004356 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004357
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004358 if ((l = self->readline_func(self, &s)) < 0) return -1;
4359 if (l < 2) return bad_readline();
4360 if (!( len=self->stack->length )) return stackUnderflow();
4361 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4362 value=self->stack->data[len-1];
4363 l=PyDict_SetItem(self->memo, py_str, value);
4364 Py_DECREF(py_str);
4365 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004366}
4367
4368
4369static int
Tim Peterscba30e22003-02-01 06:24:36 +00004370load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004371{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004372 PyObject *py_key = 0, *value = 0;
4373 unsigned char key;
4374 char *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004375 Py_ssize_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004376
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004377 if (self->read_func(self, &s, 1) < 0) return -1;
4378 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004379
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004380 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004381
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004382 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4383 value=self->stack->data[len-1];
4384 len=PyDict_SetItem(self->memo, py_key, value);
4385 Py_DECREF(py_key);
4386 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004387}
4388
4389
4390static int
Tim Peterscba30e22003-02-01 06:24:36 +00004391load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004392{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004393 PyObject *py_key = 0, *value = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004394 Py_ssize_t key;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004395 unsigned char c;
4396 char *s;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004397 Py_ssize_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004398
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004399 if (self->read_func(self, &s, 4) < 0) return -1;
4400 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004401
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004402 c = (unsigned char)s[0];
4403 key = (long)c;
4404 c = (unsigned char)s[1];
4405 key |= (long)c << 8;
4406 c = (unsigned char)s[2];
4407 key |= (long)c << 16;
4408 c = (unsigned char)s[3];
4409 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004410
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004411 if (!( py_key = PyInt_FromLong(key))) return -1;
4412 value=self->stack->data[len-1];
4413 len=PyDict_SetItem(self->memo, py_key, value);
4414 Py_DECREF(py_key);
4415 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004416}
4417
4418
4419static int
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004420do_append(Unpicklerobject *self, Py_ssize_t x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004421{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004422 PyObject *value = 0, *list = 0, *append_method = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004423 Py_ssize_t len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004424
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004425 len=self->stack->length;
4426 if (!( len >= x && x > 0 )) return stackUnderflow();
4427 /* nothing to do */
4428 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004429
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004430 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004431
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004432 if (PyList_Check(list)) {
4433 PyObject *slice;
4434 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004435
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004436 slice=Pdata_popList(self->stack, x);
4437 if (! slice) return -1;
4438 list_len = PyList_GET_SIZE(list);
4439 i=PyList_SetSlice(list, list_len, list_len, slice);
4440 Py_DECREF(slice);
4441 return i;
4442 }
4443 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004444
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004445 if (!( append_method = PyObject_GetAttr(list, append_str)))
4446 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004447
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004448 for (i = x; i < len; i++) {
4449 PyObject *junk;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004450
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004451 value=self->stack->data[i];
4452 junk=0;
4453 ARG_TUP(self, value);
4454 if (self->arg) {
4455 junk = PyObject_Call(append_method, self->arg,
4456 NULL);
4457 FREE_ARG_TUP(self);
4458 }
4459 if (! junk) {
4460 Pdata_clear(self->stack, i+1);
4461 self->stack->length=x;
4462 Py_DECREF(append_method);
4463 return -1;
4464 }
4465 Py_DECREF(junk);
4466 }
4467 self->stack->length=x;
4468 Py_DECREF(append_method);
4469 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004470
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004471 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004472}
4473
4474
4475static int
Tim Peterscba30e22003-02-01 06:24:36 +00004476load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004477{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004478 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004479}
4480
4481
4482static int
Tim Peterscba30e22003-02-01 06:24:36 +00004483load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004484{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004485 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004486}
4487
4488
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004489static Py_ssize_t
4490do_setitems(Unpicklerobject *self, Py_ssize_t x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004491{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004492 PyObject *value = 0, *key = 0, *dict = 0;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004493 Py_ssize_t len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004494
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004495 if (!( (len=self->stack->length) >= x
4496 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004497
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004498 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004499
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004500 for (i = x+1; i < len; i += 2) {
4501 key =self->stack->data[i-1];
4502 value=self->stack->data[i ];
4503 if (PyObject_SetItem(dict, key, value) < 0) {
4504 r=-1;
4505 break;
4506 }
4507 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004508
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004509 Pdata_clear(self->stack, x);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004510
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004511 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004512}
4513
4514
Tim Peters84e87f32001-03-17 04:50:51 +00004515static int
Tim Peterscba30e22003-02-01 06:24:36 +00004516load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004517{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004518 return do_setitems(self, self->stack->length - 2);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004519}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004520
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004521static int
Tim Peterscba30e22003-02-01 06:24:36 +00004522load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004523{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004524 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004525}
4526
Tim Peters84e87f32001-03-17 04:50:51 +00004527
Guido van Rossum60456fd1997-04-09 17:36:32 +00004528static int
Tim Peterscba30e22003-02-01 06:24:36 +00004529load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004530{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004531 PyObject *state, *inst, *slotstate;
4532 PyObject *__setstate__;
4533 PyObject *d_key, *d_value;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004534 int res = -1;
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004535 Py_ssize_t i;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004536
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004537 /* Stack is ... instance, state. We want to leave instance at
4538 * the stack top, possibly mutated via instance.__setstate__(state).
4539 */
4540 if (self->stack->length < 2)
4541 return stackUnderflow();
4542 PDATA_POP(self->stack, state);
4543 if (state == NULL)
4544 return -1;
4545 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004546
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004547 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4548 if (__setstate__ != NULL) {
4549 PyObject *junk = NULL;
Tim Peters080c88b2003-02-15 03:01:11 +00004550
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004551 /* The explicit __setstate__ is responsible for everything. */
4552 ARG_TUP(self, state);
4553 if (self->arg) {
4554 junk = PyObject_Call(__setstate__, self->arg, NULL);
4555 FREE_ARG_TUP(self);
4556 }
4557 Py_DECREF(__setstate__);
4558 if (junk == NULL)
4559 return -1;
4560 Py_DECREF(junk);
4561 return 0;
4562 }
4563 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4564 return -1;
4565 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004566
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004567 /* A default __setstate__. First see whether state embeds a
4568 * slot state dict too (a proto 2 addition).
4569 */
4570 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4571 PyObject *temp = state;
4572 state = PyTuple_GET_ITEM(temp, 0);
4573 slotstate = PyTuple_GET_ITEM(temp, 1);
4574 Py_INCREF(state);
4575 Py_INCREF(slotstate);
4576 Py_DECREF(temp);
4577 }
4578 else
4579 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004580
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004581 /* Set inst.__dict__ from the state dict (if any). */
4582 if (state != Py_None) {
4583 PyObject *dict;
4584 if (! PyDict_Check(state)) {
4585 PyErr_SetString(UnpicklingError, "state is not a "
4586 "dictionary");
4587 goto finally;
4588 }
4589 dict = PyObject_GetAttr(inst, __dict___str);
4590 if (dict == NULL)
4591 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004592
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004593 i = 0;
4594 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4595 /* normally the keys for instance attributes are
4596 interned. we should try to do that here. */
4597 Py_INCREF(d_key);
4598 if (PyString_CheckExact(d_key))
4599 PyString_InternInPlace(&d_key);
4600 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
4601 Py_DECREF(d_key);
4602 goto finally;
4603 }
4604 Py_DECREF(d_key);
4605 }
4606 Py_DECREF(dict);
4607 }
Tim Peters080c88b2003-02-15 03:01:11 +00004608
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004609 /* Also set instance attributes from the slotstate dict (if any). */
4610 if (slotstate != NULL) {
4611 if (! PyDict_Check(slotstate)) {
4612 PyErr_SetString(UnpicklingError, "slot state is not "
4613 "a dictionary");
4614 goto finally;
4615 }
4616 i = 0;
4617 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4618 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4619 goto finally;
4620 }
4621 }
4622 res = 0;
Tim Peters080c88b2003-02-15 03:01:11 +00004623
4624 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004625 Py_DECREF(state);
4626 Py_XDECREF(slotstate);
4627 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004628}
4629
4630
4631static int
Tim Peterscba30e22003-02-01 06:24:36 +00004632load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004633{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004634 Py_ssize_t s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004635
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004636 /* Note that we split the (pickle.py) stack into two stacks, an
4637 object stack and a mark stack. Here we push a mark onto the
4638 mark stack.
4639 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004640
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004641 if ((self->num_marks + 1) >= self->marks_size) {
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004642 Py_ssize_t *marks;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004643 s=self->marks_size+20;
4644 if (s <= self->num_marks) s=self->num_marks + 1;
4645 if (self->marks == NULL)
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004646 marks=(Py_ssize_t *)malloc(s * sizeof(Py_ssize_t));
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004647 else
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02004648 marks=(Py_ssize_t *)realloc(self->marks,
4649 s * sizeof(Py_ssize_t));
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004650 if (!marks) {
4651 PyErr_NoMemory();
4652 return -1;
4653 }
4654 self->marks = marks;
4655 self->marks_size = s;
4656 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004657
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004658 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004659
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004660 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004661}
4662
Guido van Rossum60456fd1997-04-09 17:36:32 +00004663static int
Tim Peterscba30e22003-02-01 06:24:36 +00004664load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004665{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004666 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004667
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004668 PDATA_POP(self->stack, arg_tup);
4669 if (! arg_tup) return -1;
4670 PDATA_POP(self->stack, callable);
4671 if (callable) {
4672 ob = Instance_New(callable, arg_tup);
4673 Py_DECREF(callable);
4674 }
4675 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004676
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004677 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004678
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004679 PDATA_PUSH(self->stack, ob, -1);
4680 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004681}
Tim Peters84e87f32001-03-17 04:50:51 +00004682
Tim Peters4190fb82003-02-02 16:09:05 +00004683/* Just raises an error if we don't know the protocol specified. PROTO
4684 * is the first opcode for protocols >= 2.
4685 */
4686static int
4687load_proto(Unpicklerobject *self)
4688{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004689 int i;
4690 char *protobyte;
Tim Peters4190fb82003-02-02 16:09:05 +00004691
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004692 i = self->read_func(self, &protobyte, 1);
4693 if (i < 0)
4694 return -1;
Tim Peters4190fb82003-02-02 16:09:05 +00004695
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004696 i = calc_binint(protobyte, 1);
4697 /* No point checking for < 0, since calc_binint returns an unsigned
4698 * int when chewing on 1 byte.
4699 */
4700 assert(i >= 0);
4701 if (i <= HIGHEST_PROTOCOL)
4702 return 0;
Tim Peters4190fb82003-02-02 16:09:05 +00004703
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004704 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4705 return -1;
Tim Peters4190fb82003-02-02 16:09:05 +00004706}
4707
Guido van Rossum60456fd1997-04-09 17:36:32 +00004708static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004709load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004710{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004711 PyObject *err = 0, *val = 0;
4712 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004713
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004714 self->num_marks = 0;
4715 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004716
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004717 while (1) {
4718 if (self->read_func(self, &s, 1) < 0)
4719 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004720
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004721 switch (s[0]) {
4722 case NONE:
4723 if (load_none(self) < 0)
4724 break;
4725 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004726
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004727 case BININT:
4728 if (load_binint(self) < 0)
4729 break;
4730 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004731
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004732 case BININT1:
4733 if (load_binint1(self) < 0)
4734 break;
4735 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004736
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004737 case BININT2:
4738 if (load_binint2(self) < 0)
4739 break;
4740 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004741
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004742 case INT:
4743 if (load_int(self) < 0)
4744 break;
4745 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004746
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004747 case LONG:
4748 if (load_long(self) < 0)
4749 break;
4750 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004751
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004752 case LONG1:
4753 if (load_counted_long(self, 1) < 0)
4754 break;
4755 continue;
Tim Petersee1a53c2003-02-02 02:57:53 +00004756
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004757 case LONG4:
4758 if (load_counted_long(self, 4) < 0)
4759 break;
4760 continue;
Tim Petersee1a53c2003-02-02 02:57:53 +00004761
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004762 case FLOAT:
4763 if (load_float(self) < 0)
4764 break;
4765 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004766
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004767 case BINFLOAT:
4768 if (load_binfloat(self) < 0)
4769 break;
4770 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004771
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004772 case BINSTRING:
4773 if (load_binstring(self) < 0)
4774 break;
4775 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004776
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004777 case SHORT_BINSTRING:
4778 if (load_short_binstring(self) < 0)
4779 break;
4780 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004781
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004782 case STRING:
4783 if (load_string(self) < 0)
4784 break;
4785 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004786
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004787#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004788 case UNICODE:
4789 if (load_unicode(self) < 0)
4790 break;
4791 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004792
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004793 case BINUNICODE:
4794 if (load_binunicode(self) < 0)
4795 break;
4796 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004797#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004798
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004799 case EMPTY_TUPLE:
4800 if (load_counted_tuple(self, 0) < 0)
4801 break;
4802 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00004803
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004804 case TUPLE1:
4805 if (load_counted_tuple(self, 1) < 0)
4806 break;
4807 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00004808
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004809 case TUPLE2:
4810 if (load_counted_tuple(self, 2) < 0)
4811 break;
4812 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00004813
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004814 case TUPLE3:
4815 if (load_counted_tuple(self, 3) < 0)
4816 break;
4817 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004818
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004819 case TUPLE:
4820 if (load_tuple(self) < 0)
4821 break;
4822 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004823
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004824 case EMPTY_LIST:
4825 if (load_empty_list(self) < 0)
4826 break;
4827 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004828
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004829 case LIST:
4830 if (load_list(self) < 0)
4831 break;
4832 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004833
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004834 case EMPTY_DICT:
4835 if (load_empty_dict(self) < 0)
4836 break;
4837 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004838
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004839 case DICT:
4840 if (load_dict(self) < 0)
4841 break;
4842 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004843
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004844 case OBJ:
4845 if (load_obj(self) < 0)
4846 break;
4847 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004848
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004849 case INST:
4850 if (load_inst(self) < 0)
4851 break;
4852 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004853
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004854 case NEWOBJ:
4855 if (load_newobj(self) < 0)
4856 break;
4857 continue;
Tim Peterseab7db32003-02-13 18:24:14 +00004858
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004859 case GLOBAL:
4860 if (load_global(self) < 0)
4861 break;
4862 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004863
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004864 case APPEND:
4865 if (load_append(self) < 0)
4866 break;
4867 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004868
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004869 case APPENDS:
4870 if (load_appends(self) < 0)
4871 break;
4872 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004873
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004874 case BUILD:
4875 if (load_build(self) < 0)
4876 break;
4877 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004878
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004879 case DUP:
4880 if (load_dup(self) < 0)
4881 break;
4882 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004883
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004884 case BINGET:
4885 if (load_binget(self) < 0)
4886 break;
4887 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004888
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004889 case LONG_BINGET:
4890 if (load_long_binget(self) < 0)
4891 break;
4892 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004893
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004894 case GET:
4895 if (load_get(self) < 0)
4896 break;
4897 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004898
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004899 case EXT1:
4900 if (load_extension(self, 1) < 0)
4901 break;
4902 continue;
Tim Peters2d629652003-02-04 05:06:17 +00004903
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004904 case EXT2:
4905 if (load_extension(self, 2) < 0)
4906 break;
4907 continue;
Tim Peters2d629652003-02-04 05:06:17 +00004908
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004909 case EXT4:
4910 if (load_extension(self, 4) < 0)
4911 break;
4912 continue;
4913 case MARK:
4914 if (load_mark(self) < 0)
4915 break;
4916 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004917
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004918 case BINPUT:
4919 if (load_binput(self) < 0)
4920 break;
4921 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004922
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004923 case LONG_BINPUT:
4924 if (load_long_binput(self) < 0)
4925 break;
4926 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004927
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004928 case PUT:
4929 if (load_put(self) < 0)
4930 break;
4931 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004932
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004933 case POP:
4934 if (load_pop(self) < 0)
4935 break;
4936 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004937
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004938 case POP_MARK:
4939 if (load_pop_mark(self) < 0)
4940 break;
4941 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004942
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004943 case SETITEM:
4944 if (load_setitem(self) < 0)
4945 break;
4946 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004947
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004948 case SETITEMS:
4949 if (load_setitems(self) < 0)
4950 break;
4951 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004952
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004953 case STOP:
4954 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004955
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004956 case PERSID:
4957 if (load_persid(self) < 0)
4958 break;
4959 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004960
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004961 case BINPERSID:
4962 if (load_binpersid(self) < 0)
4963 break;
4964 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004965
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004966 case REDUCE:
4967 if (load_reduce(self) < 0)
4968 break;
4969 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004970
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004971 case PROTO:
4972 if (load_proto(self) < 0)
4973 break;
4974 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00004975
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004976 case NEWTRUE:
4977 if (load_bool(self, Py_True) < 0)
4978 break;
4979 continue;
Tim Peters3c67d792003-02-02 17:59:11 +00004980
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004981 case NEWFALSE:
4982 if (load_bool(self, Py_False) < 0)
4983 break;
4984 continue;
Tim Peters3c67d792003-02-02 17:59:11 +00004985
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004986 case '\0':
4987 /* end of file */
4988 PyErr_SetNone(PyExc_EOFError);
4989 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004990
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004991 default:
4992 cPickle_ErrFormat(UnpicklingError,
4993 "invalid load key, '%s'.",
4994 "c", s[0]);
4995 return NULL;
4996 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004997
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004998 break;
4999 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005000
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005001 if ((err = PyErr_Occurred())) {
5002 if (err == PyExc_EOFError) {
5003 PyErr_SetNone(PyExc_EOFError);
5004 }
5005 return NULL;
5006 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005007
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005008 PDATA_POP(self->stack, val);
5009 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005010}
Tim Peters84e87f32001-03-17 04:50:51 +00005011
Guido van Rossum60456fd1997-04-09 17:36:32 +00005012
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005013/* No-load functions to support noload, which is used to
5014 find persistent references. */
5015
5016static int
Tim Peterscba30e22003-02-01 06:24:36 +00005017noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005018{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005019 Py_ssize_t i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005020
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005021 if ((i = marker(self)) < 0) return -1;
5022 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005023}
5024
5025
5026static int
Tim Peterscba30e22003-02-01 06:24:36 +00005027noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005028{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005029 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005030 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005031
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005032 if ((i = marker(self)) < 0) return -1;
5033 Pdata_clear(self->stack, i);
5034 if (self->readline_func(self, &s) < 0) return -1;
5035 if (self->readline_func(self, &s) < 0) return -1;
5036 PDATA_APPEND(self->stack, Py_None, -1);
5037 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005038}
5039
5040static int
Tim Peterseab7db32003-02-13 18:24:14 +00005041noload_newobj(Unpicklerobject *self)
5042{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005043 PyObject *obj;
Tim Peterseab7db32003-02-13 18:24:14 +00005044
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005045 PDATA_POP(self->stack, obj); /* pop argtuple */
5046 if (obj == NULL) return -1;
5047 Py_DECREF(obj);
Tim Peterseab7db32003-02-13 18:24:14 +00005048
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005049 PDATA_POP(self->stack, obj); /* pop cls */
5050 if (obj == NULL) return -1;
5051 Py_DECREF(obj);
Tim Peterseab7db32003-02-13 18:24:14 +00005052
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005053 PDATA_APPEND(self->stack, Py_None, -1);
5054 return 0;
Tim Peterseab7db32003-02-13 18:24:14 +00005055}
5056
5057static int
Tim Peterscba30e22003-02-01 06:24:36 +00005058noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005059{
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 (self->readline_func(self, &s) < 0) return -1;
5063 if (self->readline_func(self, &s) < 0) return -1;
5064 PDATA_APPEND(self->stack, Py_None,-1);
5065 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005066}
5067
5068static int
Tim Peterscba30e22003-02-01 06:24:36 +00005069noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005070{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005071
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005072 if (self->stack->length < 2) return stackUnderflow();
5073 Pdata_clear(self->stack, self->stack->length-2);
5074 PDATA_APPEND(self->stack, Py_None,-1);
5075 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005076}
5077
5078static int
5079noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005080
Guido van Rossum053b8df1998-11-25 16:18:00 +00005081 if (self->stack->length < 1) return stackUnderflow();
5082 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00005083 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005084}
5085
Tim Peters2d629652003-02-04 05:06:17 +00005086static int
5087noload_extension(Unpicklerobject *self, int nbytes)
5088{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005089 char *codebytes;
Tim Peters2d629652003-02-04 05:06:17 +00005090
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005091 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
5092 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
5093 PDATA_APPEND(self->stack, Py_None, -1);
5094 return 0;
Tim Peters2d629652003-02-04 05:06:17 +00005095}
5096
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005097static int
5098noload_append(Unpicklerobject *self)
5099{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005100 return Pdata_clear(self->stack, self->stack->length - 1);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005101}
5102
5103static int
5104noload_appends(Unpicklerobject *self)
5105{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005106 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005107 if ((i = marker(self)) < 0) return -1;
5108 return Pdata_clear(self->stack, i);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005109}
5110
5111static int
5112noload_setitem(Unpicklerobject *self)
5113{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005114 return Pdata_clear(self->stack, self->stack->length - 2);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005115}
5116
5117static int
5118noload_setitems(Unpicklerobject *self)
5119{
Serhiy Storchakacdc7a912013-02-12 21:36:47 +02005120 Py_ssize_t i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005121 if ((i = marker(self)) < 0) return -1;
5122 return Pdata_clear(self->stack, i);
Neil Schemenauer973f8b42009-10-14 19:33:31 +00005123}
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005124
5125static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005126noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005127{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005128 PyObject *err = 0, *val = 0;
5129 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005130
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005131 self->num_marks = 0;
5132 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005133
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005134 while (1) {
5135 if (self->read_func(self, &s, 1) < 0)
5136 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005137
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005138 switch (s[0]) {
5139 case NONE:
5140 if (load_none(self) < 0)
5141 break;
5142 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005143
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005144 case BININT:
5145 if (load_binint(self) < 0)
5146 break;
5147 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005148
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005149 case BININT1:
5150 if (load_binint1(self) < 0)
5151 break;
5152 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005153
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005154 case BININT2:
5155 if (load_binint2(self) < 0)
5156 break;
5157 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005158
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005159 case INT:
5160 if (load_int(self) < 0)
5161 break;
5162 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005163
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005164 case LONG:
5165 if (load_long(self) < 0)
5166 break;
5167 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005168
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005169 case LONG1:
5170 if (load_counted_long(self, 1) < 0)
5171 break;
5172 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005173
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005174 case LONG4:
5175 if (load_counted_long(self, 4) < 0)
5176 break;
5177 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005178
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005179 case FLOAT:
5180 if (load_float(self) < 0)
5181 break;
5182 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005183
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005184 case BINFLOAT:
5185 if (load_binfloat(self) < 0)
5186 break;
5187 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005188
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005189 case BINSTRING:
5190 if (load_binstring(self) < 0)
5191 break;
5192 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005193
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005194 case SHORT_BINSTRING:
5195 if (load_short_binstring(self) < 0)
5196 break;
5197 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005198
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005199 case STRING:
5200 if (load_string(self) < 0)
5201 break;
5202 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005203
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005204#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005205 case UNICODE:
5206 if (load_unicode(self) < 0)
5207 break;
5208 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005209
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005210 case BINUNICODE:
5211 if (load_binunicode(self) < 0)
5212 break;
5213 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00005214#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00005215
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005216 case EMPTY_TUPLE:
5217 if (load_counted_tuple(self, 0) < 0)
5218 break;
5219 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00005220
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005221 case TUPLE1:
5222 if (load_counted_tuple(self, 1) < 0)
5223 break;
5224 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00005225
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005226 case TUPLE2:
5227 if (load_counted_tuple(self, 2) < 0)
5228 break;
5229 continue;
Tim Peters1d63c9f2003-02-02 20:29:39 +00005230
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005231 case TUPLE3:
5232 if (load_counted_tuple(self, 3) < 0)
5233 break;
5234 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005235
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005236 case TUPLE:
5237 if (load_tuple(self) < 0)
5238 break;
5239 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005240
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005241 case EMPTY_LIST:
5242 if (load_empty_list(self) < 0)
5243 break;
5244 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005245
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005246 case LIST:
5247 if (load_list(self) < 0)
5248 break;
5249 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005250
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005251 case EMPTY_DICT:
5252 if (load_empty_dict(self) < 0)
5253 break;
5254 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005255
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005256 case DICT:
5257 if (load_dict(self) < 0)
5258 break;
5259 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005260
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005261 case OBJ:
5262 if (noload_obj(self) < 0)
5263 break;
5264 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005265
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005266 case INST:
5267 if (noload_inst(self) < 0)
5268 break;
5269 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005270
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005271 case NEWOBJ:
5272 if (noload_newobj(self) < 0)
5273 break;
5274 continue;
Tim Peterseab7db32003-02-13 18:24:14 +00005275
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005276 case GLOBAL:
5277 if (noload_global(self) < 0)
5278 break;
5279 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005280
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005281 case APPEND:
5282 if (noload_append(self) < 0)
5283 break;
5284 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005285
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005286 case APPENDS:
5287 if (noload_appends(self) < 0)
5288 break;
5289 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005290
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005291 case BUILD:
5292 if (noload_build(self) < 0)
5293 break;
5294 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005295
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005296 case DUP:
5297 if (load_dup(self) < 0)
5298 break;
5299 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005300
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005301 case BINGET:
5302 if (load_binget(self) < 0)
5303 break;
5304 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005305
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005306 case LONG_BINGET:
5307 if (load_long_binget(self) < 0)
5308 break;
5309 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005310
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005311 case GET:
5312 if (load_get(self) < 0)
5313 break;
5314 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005315
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005316 case EXT1:
5317 if (noload_extension(self, 1) < 0)
5318 break;
5319 continue;
Tim Peters2d629652003-02-04 05:06:17 +00005320
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005321 case EXT2:
5322 if (noload_extension(self, 2) < 0)
5323 break;
5324 continue;
Tim Peters2d629652003-02-04 05:06:17 +00005325
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005326 case EXT4:
5327 if (noload_extension(self, 4) < 0)
5328 break;
5329 continue;
Tim Peters2d629652003-02-04 05:06:17 +00005330
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005331 case MARK:
5332 if (load_mark(self) < 0)
5333 break;
5334 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005335
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005336 case BINPUT:
5337 if (load_binput(self) < 0)
5338 break;
5339 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005340
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005341 case LONG_BINPUT:
5342 if (load_long_binput(self) < 0)
5343 break;
5344 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005345
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005346 case PUT:
5347 if (load_put(self) < 0)
5348 break;
5349 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005350
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005351 case POP:
5352 if (load_pop(self) < 0)
5353 break;
5354 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005355
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005356 case POP_MARK:
5357 if (load_pop_mark(self) < 0)
5358 break;
5359 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005360
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005361 case SETITEM:
5362 if (noload_setitem(self) < 0)
5363 break;
5364 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005365
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005366 case SETITEMS:
5367 if (noload_setitems(self) < 0)
5368 break;
5369 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005370
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005371 case STOP:
5372 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005373
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005374 case PERSID:
5375 if (load_persid(self) < 0)
5376 break;
5377 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005378
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005379 case BINPERSID:
5380 if (load_binpersid(self) < 0)
5381 break;
5382 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005383
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005384 case REDUCE:
5385 if (noload_reduce(self) < 0)
5386 break;
5387 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005388
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005389 case PROTO:
5390 if (load_proto(self) < 0)
5391 break;
5392 continue;
Tim Peters4190fb82003-02-02 16:09:05 +00005393
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005394 case NEWTRUE:
5395 if (load_bool(self, Py_True) < 0)
5396 break;
5397 continue;
Tim Peters3c67d792003-02-02 17:59:11 +00005398
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005399 case NEWFALSE:
5400 if (load_bool(self, Py_False) < 0)
5401 break;
5402 continue;
5403 default:
5404 cPickle_ErrFormat(UnpicklingError,
5405 "invalid load key, '%s'.",
5406 "c", s[0]);
5407 return NULL;
5408 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005409
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005410 break;
5411 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005412
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005413 if ((err = PyErr_Occurred())) {
5414 if (err == PyExc_EOFError) {
5415 PyErr_SetNone(PyExc_EOFError);
5416 }
5417 return NULL;
5418 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005419
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005420 PDATA_POP(self->stack, val);
5421 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005422}
Tim Peters84e87f32001-03-17 04:50:51 +00005423
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005424
Guido van Rossum60456fd1997-04-09 17:36:32 +00005425static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005426Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005427{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005428 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005429}
5430
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005431static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005432Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005433{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005434 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005435}
5436
Guido van Rossum60456fd1997-04-09 17:36:32 +00005437
5438static struct PyMethodDef Unpickler_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +00005439 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005440 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005441 },
Georg Brandl96a8c392006-05-29 21:04:52 +00005442 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005443 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005444 "noload() -- not load a pickle, but go through most of the motions\n"
5445 "\n"
5446 "This function can be used to read past a pickle without instantiating\n"
5447 "any objects or importing any modules. It can also be used to find all\n"
5448 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005449 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005450 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005451 {NULL, NULL} /* sentinel */
5452};
5453
5454
5455static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005456newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005457{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005458 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005459
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005460 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5461 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005462
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005463 self->file = NULL;
5464 self->arg = NULL;
5465 self->stack = (Pdata*)Pdata_New();
5466 self->pers_func = NULL;
5467 self->last_string = NULL;
5468 self->marks = NULL;
5469 self->num_marks = 0;
5470 self->marks_size = 0;
5471 self->buf_size = 0;
5472 self->read = NULL;
5473 self->readline = NULL;
5474 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005475
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005476 if (!( self->memo = PyDict_New()))
5477 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005478
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005479 if (!self->stack)
5480 goto err;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00005481
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005482 Py_INCREF(f);
5483 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005484
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005485 /* Set read, readline based on type of f */
5486 if (PyFile_Check(f)) {
5487 self->fp = PyFile_AsFile(f);
5488 if (self->fp == NULL) {
5489 PyErr_SetString(PyExc_ValueError,
5490 "I/O operation on closed file");
5491 goto err;
5492 }
5493 self->read_func = read_file;
5494 self->readline_func = readline_file;
5495 }
5496 else if (PycStringIO_InputCheck(f)) {
5497 self->fp = NULL;
5498 self->read_func = read_cStringIO;
5499 self->readline_func = readline_cStringIO;
5500 }
5501 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005502
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005503 self->fp = NULL;
5504 self->read_func = read_other;
5505 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005506
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005507 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5508 (self->read = PyObject_GetAttr(f, read_str)))) {
5509 PyErr_Clear();
5510 PyErr_SetString( PyExc_TypeError,
5511 "argument must have 'read' and "
5512 "'readline' attributes" );
5513 goto err;
5514 }
5515 }
5516 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005517
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005518 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005519
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005520 err:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005521 Py_DECREF((PyObject *)self);
5522 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005523}
5524
5525
5526static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005527get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005528{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005529 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005530}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005531
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005532
Guido van Rossum60456fd1997-04-09 17:36:32 +00005533static void
Tim Peterscba30e22003-02-01 06:24:36 +00005534Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005535{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005536 PyObject_GC_UnTrack((PyObject *)self);
5537 Py_XDECREF(self->readline);
5538 Py_XDECREF(self->read);
5539 Py_XDECREF(self->file);
5540 Py_XDECREF(self->memo);
5541 Py_XDECREF(self->stack);
5542 Py_XDECREF(self->pers_func);
5543 Py_XDECREF(self->arg);
5544 Py_XDECREF(self->last_string);
5545 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005546
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005547 if (self->marks) {
5548 free(self->marks);
5549 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005550
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005551 if (self->buf_size) {
5552 free(self->buf);
5553 }
Tim Peters84e87f32001-03-17 04:50:51 +00005554
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005555 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005556}
5557
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005558static int
5559Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5560{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005561 Py_VISIT(self->readline);
5562 Py_VISIT(self->read);
5563 Py_VISIT(self->file);
5564 Py_VISIT(self->memo);
5565 Py_VISIT(self->stack);
5566 Py_VISIT(self->pers_func);
5567 Py_VISIT(self->arg);
5568 Py_VISIT(self->last_string);
5569 Py_VISIT(self->find_class);
5570 return 0;
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005571}
5572
5573static int
5574Unpickler_clear(Unpicklerobject *self)
5575{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005576 Py_CLEAR(self->readline);
5577 Py_CLEAR(self->read);
5578 Py_CLEAR(self->file);
5579 Py_CLEAR(self->memo);
5580 Py_CLEAR(self->stack);
5581 Py_CLEAR(self->pers_func);
5582 Py_CLEAR(self->arg);
5583 Py_CLEAR(self->last_string);
5584 Py_CLEAR(self->find_class);
5585 return 0;
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005586}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005587
5588static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005589Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005590{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005591 if (!strcmp(name, "persistent_load")) {
5592 if (!self->pers_func) {
5593 PyErr_SetString(PyExc_AttributeError, name);
5594 return NULL;
5595 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005596
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005597 Py_INCREF(self->pers_func);
5598 return self->pers_func;
5599 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005600
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005601 if (!strcmp(name, "find_global")) {
5602 if (!self->find_class) {
5603 PyErr_SetString(PyExc_AttributeError, name);
5604 return NULL;
5605 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005606
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005607 Py_INCREF(self->find_class);
5608 return self->find_class;
5609 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005610
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005611 if (!strcmp(name, "memo")) {
5612 if (!self->memo) {
5613 PyErr_SetString(PyExc_AttributeError, name);
5614 return NULL;
5615 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005616
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005617 Py_INCREF(self->memo);
5618 return self->memo;
5619 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005620
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005621 if (!strcmp(name, "UnpicklingError")) {
5622 Py_INCREF(UnpicklingError);
5623 return UnpicklingError;
5624 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005625
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005626 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005627}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005628
Guido van Rossum60456fd1997-04-09 17:36:32 +00005629
5630static int
Tim Peterscba30e22003-02-01 06:24:36 +00005631Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005632{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005633
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005634 if (!strcmp(name, "persistent_load")) {
5635 Py_XDECREF(self->pers_func);
5636 self->pers_func = value;
5637 Py_XINCREF(value);
5638 return 0;
5639 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005640
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005641 if (!strcmp(name, "find_global")) {
5642 Py_XDECREF(self->find_class);
5643 self->find_class = value;
5644 Py_XINCREF(value);
5645 return 0;
5646 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005647
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005648 if (! value) {
5649 PyErr_SetString(PyExc_TypeError,
5650 "attribute deletion is not supported");
5651 return -1;
5652 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005653
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005654 if (strcmp(name, "memo") == 0) {
5655 if (!PyDict_Check(value)) {
5656 PyErr_SetString(PyExc_TypeError,
5657 "memo must be a dictionary");
5658 return -1;
5659 }
5660 Py_XDECREF(self->memo);
5661 self->memo = value;
5662 Py_INCREF(value);
5663 return 0;
5664 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005665
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005666 PyErr_SetString(PyExc_AttributeError, name);
5667 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005668}
5669
Tim Peters5bd2a792003-02-01 16:45:06 +00005670/* ---------------------------------------------------------------------------
5671 * Module-level functions.
5672 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005673
Martin v. Löwis544f1192004-07-27 05:22:33 +00005674/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005675static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005676cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005677{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005678 static char *kwlist[] = {"obj", "file", "protocol", NULL};
5679 PyObject *ob, *file, *res = NULL;
5680 Picklerobject *pickler = 0;
5681 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005682
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005683 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5684 &ob, &file, &proto)))
5685 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005686
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005687 if (!( pickler = newPicklerobject(file, proto)))
5688 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005689
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005690 if (dump(pickler, ob) < 0)
5691 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005692
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005693 Py_INCREF(Py_None);
5694 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005696 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005697 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005698
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005699 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005700}
5701
5702
Martin v. Löwis544f1192004-07-27 05:22:33 +00005703/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005704static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005705cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005706{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005707 static char *kwlist[] = {"obj", "protocol", NULL};
5708 PyObject *ob, *file = 0, *res = NULL;
5709 Picklerobject *pickler = 0;
5710 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005711
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005712 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5713 &ob, &proto)))
5714 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005715
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005716 if (!( file = PycStringIO->NewOutput(128)))
5717 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005718
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005719 if (!( pickler = newPicklerobject(file, proto)))
5720 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005721
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005722 if (dump(pickler, ob) < 0)
5723 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005724
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005725 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005726
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005727 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005728 Py_XDECREF(pickler);
5729 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005730
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005731 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005732}
5733
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005734
Tim Peters5bd2a792003-02-01 16:45:06 +00005735/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005736static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005737cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005738{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005739 Unpicklerobject *unpickler = 0;
5740 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005741
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005742 if (!( unpickler = newUnpicklerobject(ob)))
5743 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005744
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005745 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005746
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005747 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005748 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005749
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005750 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005751}
5752
5753
Tim Peters5bd2a792003-02-01 16:45:06 +00005754/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005755static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005756cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005757{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005758 PyObject *ob, *file = 0, *res = NULL;
5759 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005760
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005761 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5762 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005763
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005764 if (!( file = PycStringIO->NewInput(ob)))
5765 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005766
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005767 if (!( unpickler = newUnpicklerobject(file)))
5768 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005769
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005770 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005771
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005772 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005773 Py_XDECREF(file);
5774 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005775
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005776 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005777}
5778
5779
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005780PyDoc_STRVAR(Unpicklertype__doc__,
5781"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005782
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005783static PyTypeObject Unpicklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00005784 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005785 "cPickle.Unpickler", /*tp_name*/
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005786 sizeof(Unpicklerobject), /*tp_basicsize*/
5787 0,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005788 (destructor)Unpickler_dealloc, /* tp_dealloc */
5789 0, /* tp_print */
5790 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5791 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5792 0, /* tp_compare */
5793 0, /* tp_repr */
5794 0, /* tp_as_number */
5795 0, /* tp_as_sequence */
5796 0, /* tp_as_mapping */
5797 0, /* tp_hash */
5798 0, /* tp_call */
5799 0, /* tp_str */
5800 0, /* tp_getattro */
5801 0, /* tp_setattro */
5802 0, /* tp_as_buffer */
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005803 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005804 Unpicklertype__doc__, /* tp_doc */
5805 (traverseproc)Unpickler_traverse, /* tp_traverse */
5806 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005807};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005808
Guido van Rossum60456fd1997-04-09 17:36:32 +00005809static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005810 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5811 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005812 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005813 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005814 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005815 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005816
Martin v. Löwis544f1192004-07-27 05:22:33 +00005817 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5818 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005819 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005820 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005821 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005822 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005823
Georg Brandl96a8c392006-05-29 21:04:52 +00005824 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005825 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005826
Neal Norwitzb0493252002-03-31 14:44:22 +00005827 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005828 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005829
Martin v. Löwis544f1192004-07-27 05:22:33 +00005830 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5831 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005832 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005833 "This takes a file-like object for writing a pickle data stream.\n"
5834 "The optional proto argument tells the pickler to use the given\n"
5835 "protocol; supported protocols are 0, 1, 2. The default\n"
5836 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5837 "only protocol that can be written to a file opened in text\n"
5838 "mode and read back successfully. When using a protocol higher\n"
5839 "than 0, make sure the file is opened in binary mode, both when\n"
5840 "pickling and unpickling.)\n"
5841 "\n"
5842 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5843 "more efficient than protocol 1.\n"
5844 "\n"
5845 "Specifying a negative protocol version selects the highest\n"
5846 "protocol version supported. The higher the protocol used, the\n"
5847 "more recent the version of Python needed to read the pickle\n"
5848 "produced.\n"
5849 "\n"
5850 "The file parameter must have a write() method that accepts a single\n"
5851 "string argument. It can thus be an open file object, a StringIO\n"
5852 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005853 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005854
Georg Brandl96a8c392006-05-29 21:04:52 +00005855 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005856 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5857
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005858 { NULL, NULL }
5859};
5860
Guido van Rossum60456fd1997-04-09 17:36:32 +00005861static int
Tim Peterscba30e22003-02-01 06:24:36 +00005862init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005863{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005864 PyObject *copyreg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005865
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005866#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005867
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005868 if (PyType_Ready(&Unpicklertype) < 0)
5869 return -1;
5870 if (PyType_Ready(&Picklertype) < 0)
5871 return -1;
Tim Peters3cfe7542003-05-21 21:29:48 +00005872
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005873 INIT_STR(__class__);
5874 INIT_STR(__getinitargs__);
5875 INIT_STR(__dict__);
5876 INIT_STR(__getstate__);
5877 INIT_STR(__setstate__);
5878 INIT_STR(__name__);
5879 INIT_STR(__main__);
5880 INIT_STR(__reduce__);
5881 INIT_STR(__reduce_ex__);
5882 INIT_STR(write);
5883 INIT_STR(append);
5884 INIT_STR(read);
5885 INIT_STR(readline);
5886 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005887
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005888 if (!( copyreg = PyImport_ImportModule("copy_reg")))
5889 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005890
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005891 /* This is special because we want to use a different
5892 one in restricted mode. */
5893 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
5894 if (!dispatch_table) return -1;
Tim Peters5b7da392003-02-04 00:21:07 +00005895
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005896 extension_registry = PyObject_GetAttrString(copyreg,
5897 "_extension_registry");
5898 if (!extension_registry) return -1;
Tim Peters5b7da392003-02-04 00:21:07 +00005899
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005900 inverted_registry = PyObject_GetAttrString(copyreg,
5901 "_inverted_registry");
5902 if (!inverted_registry) return -1;
Tim Peters5b7da392003-02-04 00:21:07 +00005903
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005904 extension_cache = PyObject_GetAttrString(copyreg,
5905 "_extension_cache");
5906 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005907
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005908 Py_DECREF(copyreg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005909
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005910 if (!(empty_tuple = PyTuple_New(0)))
5911 return -1;
Tim Peters731098b2003-02-04 20:56:09 +00005912
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005913 two_tuple = PyTuple_New(2);
5914 if (two_tuple == NULL)
5915 return -1;
5916 /* We use this temp container with no regard to refcounts, or to
5917 * keeping containees alive. Exempt from GC, because we don't
5918 * want anything looking at two_tuple() by magic.
5919 */
5920 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005921
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005922 /* Ugh */
5923 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5924 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5925 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005926
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005927 if (!( t=PyDict_New())) return -1;
5928 if (!( r=PyRun_String(
5929 "def __str__(self):\n"
5930 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5931 Py_file_input,
5932 module_dict, t) )) return -1;
5933 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005934
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005935 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5936 if (!PickleError)
5937 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005938
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005939 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005940
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005941 PicklingError = PyErr_NewException("cPickle.PicklingError",
5942 PickleError, NULL);
5943 if (!PicklingError)
5944 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005945
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005946 if (!( t=PyDict_New())) return -1;
5947 if (!( r=PyRun_String(
5948 "def __str__(self):\n"
5949 " a=self.args\n"
5950 " a=a and type(a[0]) or '(what)'\n"
5951 " return 'Cannot pickle %s objects' % a\n"
5952 , Py_file_input,
5953 module_dict, t) )) return -1;
5954 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005955
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005956 if (!( UnpickleableError = PyErr_NewException(
5957 "cPickle.UnpickleableError", PicklingError, t)))
5958 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005959
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005960 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005961
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005962 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5963 PickleError, NULL)))
5964 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005965
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005966 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5967 UnpicklingError, NULL)))
5968 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005969
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005970 if (PyDict_SetItemString(module_dict, "PickleError",
5971 PickleError) < 0)
5972 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005973
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005974 if (PyDict_SetItemString(module_dict, "PicklingError",
5975 PicklingError) < 0)
5976 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005977
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005978 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5979 UnpicklingError) < 0)
5980 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005981
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005982 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5983 UnpickleableError) < 0)
5984 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005985
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005986 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5987 BadPickleGet) < 0)
5988 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005989
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005990 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005991
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005992 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005993}
5994
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005995#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005996#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005997#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005998PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005999initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00006000{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006001 PyObject *m, *d, *di, *v, *k;
6002 Py_ssize_t i;
6003 char *rev = "1.71"; /* XXX when does this change? */
6004 PyObject *format_version;
6005 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006006
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006007 Py_TYPE(&Picklertype) = &PyType_Type;
6008 Py_TYPE(&Unpicklertype) = &PyType_Type;
6009 Py_TYPE(&PdataType) = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006010
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006011 /* Initialize some pieces. We need to do this before module creation,
6012 * so we're forced to use a temporary dictionary. :(
6013 */
6014 di = PyDict_New();
6015 if (!di) return;
6016 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00006017
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006018 /* Create the module and add the functions */
6019 m = Py_InitModule4("cPickle", cPickle_methods,
6020 cPickle_module_documentation,
6021 (PyObject*)NULL,PYTHON_API_VERSION);
6022 if (m == NULL)
6023 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006024
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006025 /* Add some symbolic constants to the module */
6026 d = PyModule_GetDict(m);
6027 v = PyString_FromString(rev);
6028 PyDict_SetItemString(d, "__version__", v);
6029 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00006030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006031 /* Copy data from di. Waaa. */
6032 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
6033 if (PyObject_SetItem(d, k, v) < 0) {
6034 Py_DECREF(di);
6035 return;
6036 }
6037 }
6038 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00006039
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006040 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
6041 if (i < 0)
6042 return;
Tim Peters8587b3c2003-02-13 15:44:41 +00006043
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006044 /* These are purely informational; no code uses them. */
6045 /* File format version we write. */
6046 format_version = PyString_FromString("2.0");
6047 /* Format versions we can read. */
6048 compatible_formats = Py_BuildValue("[sssss]",
6049 "1.0", /* Original protocol 0 */
6050 "1.1", /* Protocol 0 + INST */
6051 "1.2", /* Original protocol 1 */
6052 "1.3", /* Protocol 1 + BINFLOAT */
6053 "2.0"); /* Original protocol 2 */
6054 PyDict_SetItemString(d, "format_version", format_version);
6055 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
6056 Py_XDECREF(format_version);
6057 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00006058}