blob: 7f4ad7eb870d0ebe4e1611402c75efb32d4defcb [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/*
21 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
22 * docs are in pickletools.py.
23 */
Guido van Rossum60456fd1997-04-09 17:36:32 +000024#define MARK '('
25#define STOP '.'
26#define POP '0'
27#define POP_MARK '1'
28#define DUP '2'
29#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000030#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000031#define INT 'I'
32#define BININT 'J'
33#define BININT1 'K'
34#define LONG 'L'
35#define BININT2 'M'
36#define NONE 'N'
37#define PERSID 'P'
38#define BINPERSID 'Q'
39#define REDUCE 'R'
40#define STRING 'S'
41#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000042#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000043#define UNICODE 'V'
44#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000045#define APPEND 'a'
46#define BUILD 'b'
47#define GLOBAL 'c'
48#define DICT 'd'
49#define EMPTY_DICT '}'
50#define APPENDS 'e'
51#define GET 'g'
52#define BINGET 'h'
53#define INST 'i'
54#define LONG_BINGET 'j'
55#define LIST 'l'
56#define EMPTY_LIST ']'
57#define OBJ 'o'
58#define PUT 'p'
59#define BINPUT 'q'
60#define LONG_BINPUT 'r'
61#define SETITEM 's'
62#define TUPLE 't'
63#define EMPTY_TUPLE ')'
64#define SETITEMS 'u'
Tim Peters797ec242003-02-01 06:22:36 +000065
66/* Protocol 2. */
67#define PROTO '\x80' /* identify pickle protocol */
68#define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
69#define EXT1 '\x82' /* push object from extension registry; 1-byte index */
70#define EXT2 '\x83' /* ditto, but 2-byte index */
71#define EXT4 '\x84' /* ditto, but 4-byte index */
72#define TUPLE1 '\x85' /* build 1-tuple from stack top */
73#define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
74#define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
75#define NEWTRUE '\x88' /* push True */
76#define NEWFALSE '\x89' /* push False */
77#define LONG1 '\x8a' /* push long from < 256 bytes */
78#define LONG4 '\x8b' /* push really big long */
79
80/* There aren't opcodes -- they're ways to pickle bools before protocol 2,
81 * so that unpicklers written before bools were introduced unpickle them
82 * as ints, but unpicklers after can recognize that bools were intended.
83 * Note that protocol 2 added direct ways to pickle bools.
84 */
Jack Jansen3a967022002-06-26 20:40:42 +000085#undef TRUE
Guido van Rossume2763392002-04-05 19:30:08 +000086#define TRUE "I01\n"
Jack Jansen3a967022002-06-26 20:40:42 +000087#undef FALSE
Guido van Rossume2763392002-04-05 19:30:08 +000088#define FALSE "I00\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +000089
Tim Peters1092d642003-02-11 21:06:20 +000090/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
Tim Peters42f08ac2003-02-11 22:43:24 +000091 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
92 * break if this gets out of synch with pickle.py, but it's unclear that
93 * would help anything either.
Tim Peters1092d642003-02-11 21:06:20 +000094 */
95#define BATCHSIZE 1000
96
Guido van Rossum60456fd1997-04-09 17:36:32 +000097static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +000098
Guido van Rossumc03158b1999-06-09 15:23:31 +000099static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000100static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000101static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000102static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000103static PyObject *BadPickleGet;
104
Tim Peters5b7da392003-02-04 00:21:07 +0000105/* As the name says, an empty tuple. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000106static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000107
Georg Brandldffbf5f2008-05-20 07:49:57 +0000108/* copy_reg.dispatch_table, {type_object: pickling_function} */
Tim Peters5b7da392003-02-04 00:21:07 +0000109static PyObject *dispatch_table;
110
111/* For EXT[124] opcodes. */
Georg Brandldffbf5f2008-05-20 07:49:57 +0000112/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000113static PyObject *extension_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000114/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000115static PyObject *inverted_registry;
Georg Brandldffbf5f2008-05-20 07:49:57 +0000116/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *extension_cache;
118
Georg Brandldffbf5f2008-05-20 07:49:57 +0000119/* For looking up name pairs in copy_reg._extension_registry. */
Tim Peters731098b2003-02-04 20:56:09 +0000120static PyObject *two_tuple;
121
Guido van Rossum60456fd1997-04-09 17:36:32 +0000122static PyObject *__class___str, *__getinitargs___str, *__dict___str,
123 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Guido van Rossumb289b872003-02-19 01:45:13 +0000124 *__reduce_ex___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000125 *write_str, *append_str,
Neal Norwitzb183a252006-04-10 01:03:32 +0000126 *read_str, *readline_str, *__main___str,
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +0000127 *copyreg_str, *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000128
Guido van Rossum053b8df1998-11-25 16:18:00 +0000129/*************************************************************************
130 Internal Data type for pickle data. */
131
132typedef struct {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000133 PyObject_HEAD
Tim Peters1d63c9f2003-02-02 20:29:39 +0000134 int length; /* number of initial slots in data currently used */
135 int size; /* number of slots in data allocated */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000136 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000137} Pdata;
138
Tim Peters84e87f32001-03-17 04:50:51 +0000139static void
Tim Peterscba30e22003-02-01 06:24:36 +0000140Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000141{
142 int i;
143 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000144
Tim Peters1d63c9f2003-02-02 20:29:39 +0000145 for (i = self->length, p = self->data; --i >= 0; p++) {
146 Py_DECREF(*p);
147 }
148 if (self->data)
149 free(self->data);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000150 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000151}
152
153static PyTypeObject PdataType = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000154 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000155 (destructor)Pdata_dealloc,
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000157};
158
Christian Heimese93237d2007-12-19 02:37:44 +0000159#define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000160
161static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000162Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000163{
164 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000165
Tim Peters1d63c9f2003-02-02 20:29:39 +0000166 if (!(self = PyObject_New(Pdata, &PdataType)))
167 return NULL;
168 self->size = 8;
169 self->length = 0;
170 self->data = malloc(self->size * sizeof(PyObject*));
171 if (self->data)
172 return (PyObject*)self;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000173 Py_DECREF(self);
174 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000175}
176
Tim Peters84e87f32001-03-17 04:50:51 +0000177static int
Tim Peterscba30e22003-02-01 06:24:36 +0000178stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000179{
180 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
181 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000182}
183
Tim Peters1d63c9f2003-02-02 20:29:39 +0000184/* Retain only the initial clearto items. If clearto >= the current
185 * number of items, this is a (non-erroneous) NOP.
186 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000187static int
Tim Peterscba30e22003-02-01 06:24:36 +0000188Pdata_clear(Pdata *self, int clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000189{
190 int i;
191 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000193 if (clearto < 0) return stackUnderflow();
194 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000195
Tim Peters1d63c9f2003-02-02 20:29:39 +0000196 for (i = self->length, p = self->data + clearto;
197 --i >= clearto;
198 p++) {
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000199 Py_CLEAR(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000200 }
201 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000203 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000204}
205
Tim Peters84e87f32001-03-17 04:50:51 +0000206static int
Tim Peterscba30e22003-02-01 06:24:36 +0000207Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000208{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000209 int bigger;
210 size_t nbytes;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000211 PyObject **tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000212
Tim Peters1d63c9f2003-02-02 20:29:39 +0000213 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000214 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000215 goto nomemory;
216 if ((int)(size_t)bigger != bigger)
217 goto nomemory;
218 nbytes = (size_t)bigger * sizeof(PyObject *);
219 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
220 goto nomemory;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000221 tmp = realloc(self->data, nbytes);
222 if (tmp == NULL)
Tim Peters1d63c9f2003-02-02 20:29:39 +0000223 goto nomemory;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000224 self->data = tmp;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000225 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000226 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000227
228 nomemory:
Tim Peters1d63c9f2003-02-02 20:29:39 +0000229 PyErr_NoMemory();
230 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000231}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000232
Tim Peterse0a39072003-02-03 15:45:56 +0000233/* D is a Pdata*. Pop the topmost element and store it into V, which
234 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000235 * is raised and V is set to NULL. D and V may be evaluated several times.
236 */
237#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000238 if ((D)->length) \
239 (V) = (D)->data[--((D)->length)]; \
240 else { \
241 PyErr_SetString(UnpicklingError, "bad pickle data"); \
242 (V) = NULL; \
243 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000244}
245
Tim Peterse0a39072003-02-03 15:45:56 +0000246/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
247 * D. If the Pdata stack can't be grown to hold the new value, both
248 * raise MemoryError and execute "return ER". The difference is in ownership
249 * of O after: _PUSH transfers ownership of O from the caller to the stack
250 * (no incref of O is done, and in case of error O is decrefed), while
251 * _APPEND pushes a new reference.
252 */
253
254/* Push O on stack D, giving ownership of O to the stack. */
255#define PDATA_PUSH(D, O, ER) { \
256 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
257 Pdata_grow((Pdata*)(D)) < 0) { \
258 Py_DECREF(O); \
259 return ER; \
260 } \
261 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
262}
263
264/* Push O on stack D, pushing a new reference. */
265#define PDATA_APPEND(D, O, ER) { \
266 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
267 Pdata_grow((Pdata*)(D)) < 0) \
268 return ER; \
269 Py_INCREF(O); \
270 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
271}
272
273
Guido van Rossum053b8df1998-11-25 16:18:00 +0000274static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000275Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000276{
277 PyObject *r;
278 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000279
Tim Peters1d63c9f2003-02-02 20:29:39 +0000280 l = self->length-start;
281 r = PyTuple_New(l);
282 if (r == NULL)
283 return NULL;
284 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000285 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000286
Tim Peters1d63c9f2003-02-02 20:29:39 +0000287 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000288 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000289}
290
291static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000292Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000293{
294 PyObject *r;
295 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000297 l=self->length-start;
298 if (!( r=PyList_New(l))) return NULL;
299 for (i=start, j=0 ; j < l; i++, j++)
300 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000302 self->length=start;
303 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000304}
305
Guido van Rossum053b8df1998-11-25 16:18:00 +0000306/*************************************************************************/
307
308#define ARG_TUP(self, o) { \
309 if (self->arg || (self->arg=PyTuple_New(1))) { \
310 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
311 PyTuple_SET_ITEM(self->arg,0,o); \
312 } \
313 else { \
314 Py_DECREF(o); \
315 } \
316}
317
318#define FREE_ARG_TUP(self) { \
Christian Heimese93237d2007-12-19 02:37:44 +0000319 if (Py_REFCNT(self->arg) > 1) { \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000320 Py_DECREF(self->arg); \
321 self->arg=NULL; \
322 } \
323 }
324
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000325typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000326 PyObject_HEAD
327 FILE *fp;
328 PyObject *write;
329 PyObject *file;
330 PyObject *memo;
331 PyObject *arg;
332 PyObject *pers_func;
333 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000334
335 /* pickle protocol number, >= 0 */
336 int proto;
337
338 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000339 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000340
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000341 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000342 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000343 char *write_buf;
344 int buf_size;
345 PyObject *dispatch_table;
346 int fast_container; /* count nested container dumps */
347 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000348} Picklerobject;
349
Barry Warsaw52acb492001-12-21 20:04:22 +0000350#ifndef PY_CPICKLE_FAST_LIMIT
351#define PY_CPICKLE_FAST_LIMIT 50
352#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000353
Jeremy Hylton938ace62002-07-17 16:30:39 +0000354static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000355
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000356typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000357 PyObject_HEAD
358 FILE *fp;
359 PyObject *file;
360 PyObject *readline;
361 PyObject *read;
362 PyObject *memo;
363 PyObject *arg;
364 Pdata *stack;
365 PyObject *mark;
366 PyObject *pers_func;
367 PyObject *last_string;
368 int *marks;
369 int num_marks;
370 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000371 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
372 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000373 int buf_size;
374 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000375 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000376} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000377
Jeremy Hylton938ace62002-07-17 16:30:39 +0000378static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000379
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000380/* Forward decls that need the above structs */
381static int save(Picklerobject *, PyObject *, int);
382static int put2(Picklerobject *, PyObject *);
383
Guido van Rossumd385d591997-04-09 17:47:47 +0000384static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000385PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000386cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
387{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000388 va_list va;
389 PyObject *args=0, *retval=0;
390 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000392 if (format) args = Py_VaBuildValue(format, va);
393 va_end(va);
394 if (format && ! args) return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000395 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000396 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000397
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000398 if (retval) {
399 if (args) {
400 PyObject *v;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000401 v=PyString_Format(retval, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000402 Py_DECREF(retval);
403 Py_DECREF(args);
404 if (! v) return NULL;
405 retval=v;
406 }
407 }
408 else
409 if (args) retval=args;
410 else {
411 PyErr_SetObject(ErrType,Py_None);
412 return NULL;
413 }
414 PyErr_SetObject(ErrType,retval);
415 Py_DECREF(retval);
416 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000417}
418
Tim Peters84e87f32001-03-17 04:50:51 +0000419static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000420write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000421{
422 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000424 if (s == NULL) {
425 return 0;
426 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000427
Martin v. Löwis18e16552006-02-15 17:27:45 +0000428 if (n > INT_MAX) {
429 /* String too large */
430 return -1;
431 }
432
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000433 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000434 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000435 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000436 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000437 PyFile_DecUseCount((PyFileObject *)self->file);
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000438 if (nbyteswritten != (size_t)n) {
439 PyErr_SetFromErrno(PyExc_IOError);
440 return -1;
441 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000442
Martin v. Löwis18e16552006-02-15 17:27:45 +0000443 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000444}
445
Tim Peters84e87f32001-03-17 04:50:51 +0000446static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000447write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000448{
449 if (s == NULL) {
450 return 0;
451 }
Tim Peterscba30e22003-02-01 06:24:36 +0000452
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000453 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
454 return -1;
455 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000456
Martin v. Löwis18e16552006-02-15 17:27:45 +0000457 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000458}
459
Tim Peters84e87f32001-03-17 04:50:51 +0000460static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000461write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000462{
463 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000464 if (n > INT_MAX) return -1;
465 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000466}
467
Tim Peters84e87f32001-03-17 04:50:51 +0000468static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000469write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000470{
471 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000472 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000473
Martin v. Löwis18e16552006-02-15 17:27:45 +0000474 if (_n > INT_MAX)
475 return -1;
476 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000477 if (s == NULL) {
478 if (!( self->buf_size )) return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000479 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000480 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000481 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000482 return -1;
483 }
484 else {
485 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
486 if (write_other(self, NULL, 0) < 0)
487 return -1;
488 }
Tim Peterscba30e22003-02-01 06:24:36 +0000489
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000490 if (n > WRITE_BUF_SIZE) {
491 if (!( py_str =
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000492 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000493 return -1;
494 }
495 else {
496 memcpy(self->write_buf + self->buf_size, s, n);
497 self->buf_size += n;
498 return n;
499 }
500 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000501
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000502 if (self->write) {
503 /* object with write method */
504 ARG_TUP(self, py_str);
505 if (self->arg) {
506 junk = PyObject_Call(self->write, self->arg, NULL);
507 FREE_ARG_TUP(self);
508 }
509 if (junk) Py_DECREF(junk);
510 else return -1;
511 }
512 else
513 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000514
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000515 self->buf_size = 0;
516 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000517}
518
519
Martin v. Löwis18e16552006-02-15 17:27:45 +0000520static Py_ssize_t
521read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000522{
523 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000524
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000525 if (self->buf_size == 0) {
526 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000527
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000528 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000529 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000530 PyErr_NoMemory();
531 return -1;
532 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000533
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000534 self->buf_size = size;
535 }
536 else if (n > self->buf_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000537 char *newbuf = (char *)realloc(self->buf, n);
538 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000539 PyErr_NoMemory();
540 return -1;
541 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000542 self->buf = newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000543 self->buf_size = n;
544 }
Tim Peters84e87f32001-03-17 04:50:51 +0000545
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000546 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000547 Py_BEGIN_ALLOW_THREADS
548 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
549 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000550 PyFile_DecUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000551 if (nbytesread != (size_t)n) {
552 if (feof(self->fp)) {
553 PyErr_SetNone(PyExc_EOFError);
554 return -1;
555 }
Tim Peterscba30e22003-02-01 06:24:36 +0000556
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000557 PyErr_SetFromErrno(PyExc_IOError);
558 return -1;
559 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000560
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000561 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000562
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000563 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000564}
565
566
Martin v. Löwis18e16552006-02-15 17:27:45 +0000567static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000568readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000569{
570 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000571
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000572 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000573 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000574 PyErr_NoMemory();
575 return -1;
576 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000577 self->buf_size = 40;
578 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000579
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000580 i = 0;
581 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000582 int bigger;
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000583 char *newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000584 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000585 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000586 (self->buf[i] = getc(self->fp)) == '\n') {
587 self->buf[i + 1] = '\0';
588 *s = self->buf;
589 return i + 1;
590 }
591 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000592 bigger = self->buf_size << 1;
593 if (bigger <= 0) { /* overflow */
594 PyErr_NoMemory();
595 return -1;
596 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000597 newbuf = (char *)realloc(self->buf, bigger);
598 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000599 PyErr_NoMemory();
600 return -1;
601 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000602 self->buf = newbuf;
Tim Petersee1a53c2003-02-02 02:57:53 +0000603 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000604 }
Tim Peters84e87f32001-03-17 04:50:51 +0000605}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000606
607
Martin v. Löwis18e16552006-02-15 17:27:45 +0000608static Py_ssize_t
609read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000610{
611 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000613 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
614 PyErr_SetNone(PyExc_EOFError);
615 return -1;
616 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000617
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000618 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000619
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000620 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000621}
622
623
Martin v. Löwis18e16552006-02-15 17:27:45 +0000624static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000625readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000626{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000627 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000628 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000630 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
631 return -1;
632 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000633
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000634 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000635
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000636 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000637}
638
639
Martin v. Löwis18e16552006-02-15 17:27:45 +0000640static Py_ssize_t
641read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000642{
643 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000644
Martin v. Löwis18e16552006-02-15 17:27:45 +0000645 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000646
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000647 ARG_TUP(self, bytes);
648 if (self->arg) {
649 str = PyObject_Call(self->read, self->arg, NULL);
650 FREE_ARG_TUP(self);
651 }
652 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000653
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000654 Py_XDECREF(self->last_string);
655 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000656
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000657 if (! (*s = PyString_AsString(str))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000658 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000659}
660
661
Martin v. Löwis18e16552006-02-15 17:27:45 +0000662static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000663readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000664{
665 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000666 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000668 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
669 return -1;
670 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000672 if ((str_size = PyString_Size(str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000673 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000674
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000675 Py_XDECREF(self->last_string);
676 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000677
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000678 if (! (*s = PyString_AsString(str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000679 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000680
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000681 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000682}
683
Tim Petersee1a53c2003-02-02 02:57:53 +0000684/* Copy the first n bytes from s into newly malloc'ed memory, plus a
685 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
686 * The caller is responsible for free()'ing the return value.
687 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000688static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000689pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000690{
Tim Petersee1a53c2003-02-02 02:57:53 +0000691 char *r = (char *)malloc(n+1);
692 if (r == NULL)
693 return (char*)PyErr_NoMemory();
694 memcpy(r, s, n);
695 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000696 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000697}
698
699
700static int
Tim Peterscba30e22003-02-01 06:24:36 +0000701get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000702{
703 PyObject *value, *mv;
704 long c_value;
705 char s[30];
706 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000707
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000708 if (!( mv = PyDict_GetItem(self->memo, id))) {
709 PyErr_SetObject(PyExc_KeyError, id);
710 return -1;
711 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000712
Tim Peterscba30e22003-02-01 06:24:36 +0000713 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000714 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000716 if (!( PyInt_Check(value))) {
717 PyErr_SetString(PicklingError, "no int where int expected in memo");
718 return -1;
719 }
720 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000721
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000722 if (!self->bin) {
723 s[0] = GET;
724 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
725 len = strlen(s);
726 }
727 else if (Pdata_Check(self->file)) {
728 if (write_other(self, NULL, 0) < 0) return -1;
729 PDATA_APPEND(self->file, mv, -1);
730 return 0;
731 }
732 else {
733 if (c_value < 256) {
734 s[0] = BINGET;
735 s[1] = (int)(c_value & 0xff);
736 len = 2;
737 }
738 else {
739 s[0] = LONG_BINGET;
740 s[1] = (int)(c_value & 0xff);
741 s[2] = (int)((c_value >> 8) & 0xff);
742 s[3] = (int)((c_value >> 16) & 0xff);
743 s[4] = (int)((c_value >> 24) & 0xff);
744 len = 5;
745 }
746 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000747
Tim Peters0bc93f52003-02-02 18:29:33 +0000748 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000749 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000750
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000751 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000752}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000753
Guido van Rossum60456fd1997-04-09 17:36:32 +0000754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000755static int
Tim Peterscba30e22003-02-01 06:24:36 +0000756put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000757{
Christian Heimese93237d2007-12-19 02:37:44 +0000758 if (Py_REFCNT(ob) < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000759 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000760
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000761 return put2(self, ob);
762}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000763
Guido van Rossum053b8df1998-11-25 16:18:00 +0000764
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000765static int
Tim Peterscba30e22003-02-01 06:24:36 +0000766put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000767{
768 char c_str[30];
769 int p;
770 size_t len;
771 int res = -1;
772 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000773
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000774 if (self->fast)
775 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000777 if ((p = PyDict_Size(self->memo)) < 0)
778 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000780 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000781 /* XXX Why?
782 * XXX And does "positive" really mean non-negative?
783 * XXX pickle.py starts with PUT index 0, not 1. This makes for
784 * XXX gratuitous differences between the pickling modules.
785 */
Tim Peterscba30e22003-02-01 06:24:36 +0000786 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000787
Tim Peterscba30e22003-02-01 06:24:36 +0000788 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000789 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000790
Tim Peterscba30e22003-02-01 06:24:36 +0000791 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000792 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000793
Tim Peterscba30e22003-02-01 06:24:36 +0000794 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000795 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000796
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000797 PyTuple_SET_ITEM(t, 0, memo_len);
798 Py_INCREF(memo_len);
799 PyTuple_SET_ITEM(t, 1, ob);
800 Py_INCREF(ob);
801
802 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
803 goto finally;
804
805 if (!self->bin) {
806 c_str[0] = PUT;
807 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
808 len = strlen(c_str);
809 }
810 else if (Pdata_Check(self->file)) {
811 if (write_other(self, NULL, 0) < 0) return -1;
812 PDATA_APPEND(self->file, memo_len, -1);
813 res=0; /* Job well done ;) */
814 goto finally;
815 }
816 else {
817 if (p >= 256) {
818 c_str[0] = LONG_BINPUT;
819 c_str[1] = (int)(p & 0xff);
820 c_str[2] = (int)((p >> 8) & 0xff);
821 c_str[3] = (int)((p >> 16) & 0xff);
822 c_str[4] = (int)((p >> 24) & 0xff);
823 len = 5;
824 }
825 else {
826 c_str[0] = BINPUT;
827 c_str[1] = p;
828 len = 2;
829 }
830 }
831
Tim Peters0bc93f52003-02-02 18:29:33 +0000832 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000833 goto finally;
834
835 res = 0;
836
837 finally:
838 Py_XDECREF(py_ob_id);
839 Py_XDECREF(memo_len);
840 Py_XDECREF(t);
841
842 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000843}
844
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000845static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000846whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000847{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000848 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000849 PyObject *module = 0, *modules_dict = 0,
850 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000851
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000852 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000853 if (module)
854 return module;
855 if (PyErr_ExceptionMatches(PyExc_AttributeError))
856 PyErr_Clear();
857 else
858 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000859
Tim Peterscba30e22003-02-01 06:24:36 +0000860 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000861 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000862
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000863 i = 0;
864 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000865
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000866 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000867
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000868 global_name_attr = PyObject_GetAttr(module, global_name);
869 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000870 if (PyErr_ExceptionMatches(PyExc_AttributeError))
871 PyErr_Clear();
872 else
873 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000874 continue;
875 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000877 if (global_name_attr != global) {
878 Py_DECREF(global_name_attr);
879 continue;
880 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000881
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000882 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000883
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000884 break;
885 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000886
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000887 /* The following implements the rule in pickle.py added in 1.5
888 that used __main__ if no module is found. I don't actually
889 like this rule. jlf
890 */
891 if (!j) {
892 j=1;
893 name=__main___str;
894 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000895
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000896 Py_INCREF(name);
897 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000898}
899
900
Guido van Rossum60456fd1997-04-09 17:36:32 +0000901static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000902fast_save_enter(Picklerobject *self, PyObject *obj)
903{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000904 /* if fast_container < 0, we're doing an error exit. */
905 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
906 PyObject *key = NULL;
907 if (self->fast_memo == NULL) {
908 self->fast_memo = PyDict_New();
909 if (self->fast_memo == NULL) {
910 self->fast_container = -1;
911 return 0;
912 }
913 }
914 key = PyLong_FromVoidPtr(obj);
915 if (key == NULL)
916 return 0;
917 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000918 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000919 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000920 "fast mode: can't pickle cyclic objects "
921 "including object type %s at %p",
Christian Heimese93237d2007-12-19 02:37:44 +0000922 Py_TYPE(obj)->tp_name, obj);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000923 self->fast_container = -1;
924 return 0;
925 }
926 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000927 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000928 self->fast_container = -1;
929 return 0;
930 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000931 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000932 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000933 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000934}
935
Tim Peterscba30e22003-02-01 06:24:36 +0000936int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000937fast_save_leave(Picklerobject *self, PyObject *obj)
938{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000939 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
940 PyObject *key = PyLong_FromVoidPtr(obj);
941 if (key == NULL)
942 return 0;
943 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000944 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000945 return 0;
946 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000947 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000948 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000949 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000950}
951
952static int
Tim Peterscba30e22003-02-01 06:24:36 +0000953save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000954{
955 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000956 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000957 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000958
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000959 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000960}
961
Guido van Rossum77f6a652002-04-03 22:41:51 +0000962static int
Tim Peterscba30e22003-02-01 06:24:36 +0000963save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000964{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000965 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000966 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000967 long l = PyInt_AS_LONG((PyIntObject *)args);
968
Tim Peters3c67d792003-02-02 17:59:11 +0000969 if (self->proto >= 2) {
970 char opcode = l ? NEWTRUE : NEWFALSE;
971 if (self->write_func(self, &opcode, 1) < 0)
972 return -1;
973 }
974 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000975 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000976 return 0;
977}
Tim Peters84e87f32001-03-17 04:50:51 +0000978
Guido van Rossum60456fd1997-04-09 17:36:32 +0000979static int
Tim Peterscba30e22003-02-01 06:24:36 +0000980save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000981{
982 char c_str[32];
983 long l = PyInt_AS_LONG((PyIntObject *)args);
984 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000985
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000986 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000987#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000988 || l > 0x7fffffffL
989 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000990#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000991 ) {
992 /* Text-mode pickle, or long too big to fit in the 4-byte
993 * signed BININT format: store as a string.
994 */
995 c_str[0] = INT;
996 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000997 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000998 return -1;
999 }
1000 else {
1001 /* Binary pickle and l fits in a signed 4-byte int. */
1002 c_str[1] = (int)( l & 0xff);
1003 c_str[2] = (int)((l >> 8) & 0xff);
1004 c_str[3] = (int)((l >> 16) & 0xff);
1005 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001006
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001007 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1008 if (c_str[2] == 0) {
1009 c_str[0] = BININT1;
1010 len = 2;
1011 }
1012 else {
1013 c_str[0] = BININT2;
1014 len = 3;
1015 }
1016 }
1017 else {
1018 c_str[0] = BININT;
1019 len = 5;
1020 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001021
Tim Peters0bc93f52003-02-02 18:29:33 +00001022 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001023 return -1;
1024 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001025
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001026 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001027}
1028
1029
1030static int
Tim Peterscba30e22003-02-01 06:24:36 +00001031save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001032{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001033 Py_ssize_t size;
Tim Petersee1a53c2003-02-02 02:57:53 +00001034 int res = -1;
1035 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001036
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001037 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001038
Tim Petersee1a53c2003-02-02 02:57:53 +00001039 if (self->proto >= 2) {
1040 /* Linear-time pickling. */
1041 size_t nbits;
1042 size_t nbytes;
1043 unsigned char *pdata;
1044 char c_str[5];
1045 int i;
1046 int sign = _PyLong_Sign(args);
1047
1048 if (sign == 0) {
1049 /* It's 0 -- an empty bytestring. */
1050 c_str[0] = LONG1;
1051 c_str[1] = 0;
1052 i = self->write_func(self, c_str, 2);
1053 if (i < 0) goto finally;
1054 res = 0;
1055 goto finally;
1056 }
1057 nbits = _PyLong_NumBits(args);
1058 if (nbits == (size_t)-1 && PyErr_Occurred())
1059 goto finally;
1060 /* How many bytes do we need? There are nbits >> 3 full
1061 * bytes of data, and nbits & 7 leftover bits. If there
1062 * are any leftover bits, then we clearly need another
1063 * byte. Wnat's not so obvious is that we *probably*
1064 * need another byte even if there aren't any leftovers:
1065 * the most-significant bit of the most-significant byte
1066 * acts like a sign bit, and it's usually got a sense
1067 * opposite of the one we need. The exception is longs
1068 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1069 * its own 256's-complement, so has the right sign bit
1070 * even without the extra byte. That's a pain to check
1071 * for in advance, though, so we always grab an extra
1072 * byte at the start, and cut it back later if possible.
1073 */
1074 nbytes = (nbits >> 3) + 1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001075 if (nbytes > INT_MAX) {
Tim Petersee1a53c2003-02-02 02:57:53 +00001076 PyErr_SetString(PyExc_OverflowError, "long too large "
1077 "to pickle");
1078 goto finally;
1079 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001080 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
Tim Petersee1a53c2003-02-02 02:57:53 +00001081 if (repr == NULL) goto finally;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001082 pdata = (unsigned char *)PyString_AS_STRING(repr);
Tim Petersee1a53c2003-02-02 02:57:53 +00001083 i = _PyLong_AsByteArray((PyLongObject *)args,
1084 pdata, nbytes,
1085 1 /* little endian */, 1 /* signed */);
1086 if (i < 0) goto finally;
1087 /* If the long is negative, this may be a byte more than
1088 * needed. This is so iff the MSB is all redundant sign
1089 * bits.
1090 */
1091 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1092 (pdata[nbytes - 2] & 0x80) != 0)
1093 --nbytes;
1094
1095 if (nbytes < 256) {
1096 c_str[0] = LONG1;
1097 c_str[1] = (char)nbytes;
1098 size = 2;
1099 }
1100 else {
1101 c_str[0] = LONG4;
1102 size = (int)nbytes;
1103 for (i = 1; i < 5; i++) {
1104 c_str[i] = (char)(size & 0xff);
1105 size >>= 8;
1106 }
1107 size = 5;
1108 }
1109 i = self->write_func(self, c_str, size);
1110 if (i < 0) goto finally;
1111 i = self->write_func(self, (char *)pdata, (int)nbytes);
1112 if (i < 0) goto finally;
1113 res = 0;
1114 goto finally;
1115 }
1116
1117 /* proto < 2: write the repr and newline. This is quadratic-time
1118 * (in the number of digits), in both directions.
1119 */
Tim Peterscba30e22003-02-01 06:24:36 +00001120 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001121 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001122
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001123 if ((size = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001124 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001125
Tim Peters0bc93f52003-02-02 18:29:33 +00001126 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001127 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001128
Tim Peters0bc93f52003-02-02 18:29:33 +00001129 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001130 PyString_AS_STRING((PyStringObject *)repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001131 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001132 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001133
Tim Peters0bc93f52003-02-02 18:29:33 +00001134 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001135 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001136
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001137 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001138
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001139 finally:
1140 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001141 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001142}
1143
1144
1145static int
Tim Peterscba30e22003-02-01 06:24:36 +00001146save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001147{
1148 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001149
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001150 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001151 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001152 str[0] = BINFLOAT;
1153 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001154 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001155 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001156 return -1;
1157 }
1158 else {
1159 char c_str[250];
1160 c_str[0] = FLOAT;
Georg Brandlde9b6242006-04-30 11:13:56 +00001161 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1162 /* Extend the formatted string with a newline character */
1163 strcat(c_str, "\n");
Guido van Rossum60456fd1997-04-09 17:36:32 +00001164
Tim Peters0bc93f52003-02-02 18:29:33 +00001165 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001166 return -1;
1167 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001168
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001169 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001170}
1171
1172
1173static int
Tim Peterscba30e22003-02-01 06:24:36 +00001174save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001175{
1176 int size, len;
1177 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001178
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001179 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001180 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001181
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001182 if (!self->bin) {
1183 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001184
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001185 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001186
Tim Peterscba30e22003-02-01 06:24:36 +00001187 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001188 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001189
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001190 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001191 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001192 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001193
Tim Peters0bc93f52003-02-02 18:29:33 +00001194 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001195 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001196
Tim Peters0bc93f52003-02-02 18:29:33 +00001197 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001198 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001199
Tim Peters0bc93f52003-02-02 18:29:33 +00001200 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001201 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001203 Py_XDECREF(repr);
1204 }
1205 else {
1206 int i;
1207 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001208
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001209 if ((size = PyString_Size(args)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001210 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001211
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001212 if (size < 256) {
1213 c_str[0] = SHORT_BINSTRING;
1214 c_str[1] = size;
1215 len = 2;
1216 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001217 else if (size <= INT_MAX) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001218 c_str[0] = BINSTRING;
1219 for (i = 1; i < 5; i++)
1220 c_str[i] = (int)(size >> ((i - 1) * 8));
1221 len = 5;
1222 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001223 else
1224 return -1; /* string too large */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001225
Tim Peters0bc93f52003-02-02 18:29:33 +00001226 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001227 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001228
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001229 if (size > 128 && Pdata_Check(self->file)) {
1230 if (write_other(self, NULL, 0) < 0) return -1;
1231 PDATA_APPEND(self->file, args, -1);
1232 }
1233 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001234 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001235 PyString_AS_STRING(
1236 (PyStringObject *)args),
Tim Peters0bc93f52003-02-02 18:29:33 +00001237 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001238 return -1;
1239 }
1240 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001241
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001242 if (doput)
1243 if (put(self, args) < 0)
1244 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001245
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001246 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001247
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001248 err:
1249 Py_XDECREF(repr);
1250 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001251}
1252
1253
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001254#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001255/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1256 backslash and newline characters to \uXXXX escapes. */
1257static PyObject *
1258modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1259{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001260 PyObject *repr;
1261 char *p;
1262 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001263
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001264 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001265
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001266 repr = PyString_FromStringAndSize(NULL, 6 * size);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001267 if (repr == NULL)
1268 return NULL;
1269 if (size == 0)
1270 return repr;
1271
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001272 p = q = PyString_AS_STRING(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001273 while (size-- > 0) {
1274 Py_UNICODE ch = *s++;
1275 /* Map 16-bit characters to '\uxxxx' */
1276 if (ch >= 256 || ch == '\\' || ch == '\n') {
1277 *p++ = '\\';
1278 *p++ = 'u';
1279 *p++ = hexdigit[(ch >> 12) & 0xf];
1280 *p++ = hexdigit[(ch >> 8) & 0xf];
1281 *p++ = hexdigit[(ch >> 4) & 0xf];
1282 *p++ = hexdigit[ch & 15];
1283 }
1284 /* Copy everything else as-is */
1285 else
1286 *p++ = (char) ch;
1287 }
1288 *p = '\0';
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001289 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001290 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001291}
1292
1293
Guido van Rossum60456fd1997-04-09 17:36:32 +00001294static int
Tim Peterscba30e22003-02-01 06:24:36 +00001295save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001296{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001297 Py_ssize_t size, len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001298 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001299
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001300 if (!PyUnicode_Check(args))
1301 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001302
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001303 if (!self->bin) {
1304 char *repr_str;
1305 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001306
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001307 repr = modified_EncodeRawUnicodeEscape(
1308 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001309 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001310 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001311
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001312 if ((len = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001313 goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001314 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001315
Tim Peters0bc93f52003-02-02 18:29:33 +00001316 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001317 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001318
Tim Peters0bc93f52003-02-02 18:29:33 +00001319 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001320 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001321
Tim Peters0bc93f52003-02-02 18:29:33 +00001322 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001323 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001324
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001325 Py_XDECREF(repr);
1326 }
1327 else {
1328 int i;
1329 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001330
Tim Peterscba30e22003-02-01 06:24:36 +00001331 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001332 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001333
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001334 if ((size = PyString_Size(repr)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001335 goto err;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001336 if (size > INT_MAX)
1337 return -1; /* string too large */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001338
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001339 c_str[0] = BINUNICODE;
1340 for (i = 1; i < 5; i++)
1341 c_str[i] = (int)(size >> ((i - 1) * 8));
1342 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001343
Tim Peters0bc93f52003-02-02 18:29:33 +00001344 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001345 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001346
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001347 if (size > 128 && Pdata_Check(self->file)) {
1348 if (write_other(self, NULL, 0) < 0)
1349 goto err;
1350 PDATA_APPEND(self->file, repr, -1);
1351 }
1352 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001353 if (self->write_func(self, PyString_AS_STRING(repr),
Tim Peters0bc93f52003-02-02 18:29:33 +00001354 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001355 goto err;
1356 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001358 Py_DECREF(repr);
1359 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001361 if (doput)
1362 if (put(self, args) < 0)
1363 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001364
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001365 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001366
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001367 err:
1368 Py_XDECREF(repr);
1369 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001370}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001371#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001372
Tim Peters1d63c9f2003-02-02 20:29:39 +00001373/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1374static int
Tim Peters67920142003-02-05 03:46:17 +00001375store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001376{
1377 int i;
1378 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001379
Tim Peters1d63c9f2003-02-02 20:29:39 +00001380 assert(PyTuple_Size(t) == len);
1381
1382 for (i = 0; i < len; i++) {
1383 PyObject *element = PyTuple_GET_ITEM(t, i);
1384
1385 if (element == NULL)
1386 goto finally;
1387 if (save(self, element, 0) < 0)
1388 goto finally;
1389 }
1390 res = 0;
1391
1392 finally:
1393 return res;
1394}
1395
1396/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1397 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001398 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001399 * (a tuple can be reached from itself), and that requires some subtle
1400 * magic so that it works in all cases. IOW, this is a long routine.
1401 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001402static int
Tim Peterscba30e22003-02-01 06:24:36 +00001403save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001404{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001405 PyObject *py_tuple_id = NULL;
1406 int len, i;
1407 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001408
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001409 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001410 static char pop = POP;
1411 static char pop_mark = POP_MARK;
1412 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001413
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001414 if ((len = PyTuple_Size(args)) < 0)
1415 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001416
Tim Peters1d63c9f2003-02-02 20:29:39 +00001417 if (len == 0) {
1418 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001419
Tim Peters1d63c9f2003-02-02 20:29:39 +00001420 if (self->proto) {
1421 c_str[0] = EMPTY_TUPLE;
1422 len = 1;
1423 }
1424 else {
1425 c_str[0] = MARK;
1426 c_str[1] = TUPLE;
1427 len = 2;
1428 }
1429 if (self->write_func(self, c_str, len) >= 0)
1430 res = 0;
1431 /* Don't memoize an empty tuple. */
1432 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001433 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001434
Tim Peters1d63c9f2003-02-02 20:29:39 +00001435 /* A non-empty tuple. */
1436
1437 /* id(tuple) isn't in the memo now. If it shows up there after
1438 * saving the tuple elements, the tuple must be recursive, in
1439 * which case we'll pop everything we put on the stack, and fetch
1440 * its value from the memo.
1441 */
1442 py_tuple_id = PyLong_FromVoidPtr(args);
1443 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001444 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001445
Tim Peters1d63c9f2003-02-02 20:29:39 +00001446 if (len <= 3 && self->proto >= 2) {
1447 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001448 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001449 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001450 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001451 /* pop the len elements */
1452 for (i = 0; i < len; ++i)
1453 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001454 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001455 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001456 if (get(self, py_tuple_id) < 0)
1457 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001458 res = 0;
1459 goto finally;
1460 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001461 /* Not recursive. */
1462 if (self->write_func(self, len2opcode + len, 1) < 0)
1463 goto finally;
1464 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001465 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001466
Tim Peters1d63c9f2003-02-02 20:29:39 +00001467 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1468 * Generate MARK elt1 elt2 ... TUPLE
1469 */
1470 if (self->write_func(self, &MARKv, 1) < 0)
1471 goto finally;
1472
Tim Peters67920142003-02-05 03:46:17 +00001473 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001474 goto finally;
1475
1476 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1477 /* pop the stack stuff we pushed */
1478 if (self->bin) {
1479 if (self->write_func(self, &pop_mark, 1) < 0)
1480 goto finally;
1481 }
1482 else {
1483 /* Note that we pop one more than len, to remove
1484 * the MARK too.
1485 */
1486 for (i = 0; i <= len; i++)
1487 if (self->write_func(self, &pop, 1) < 0)
1488 goto finally;
1489 }
1490 /* fetch from memo */
1491 if (get(self, py_tuple_id) >= 0)
1492 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001493 goto finally;
1494 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001495
Tim Peters1d63c9f2003-02-02 20:29:39 +00001496 /* Not recursive. */
1497 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001498 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001499
Tim Peters1d63c9f2003-02-02 20:29:39 +00001500 memoize:
1501 if (put(self, args) >= 0)
1502 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001503
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001504 finally:
1505 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001506 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001507}
1508
Tim Peters1092d642003-02-11 21:06:20 +00001509/* iter is an iterator giving items, and we batch up chunks of
1510 * MARK item item ... item APPENDS
1511 * opcode sequences. Calling code should have arranged to first create an
1512 * empty list, or list-like object, for the APPENDS to operate on.
1513 * Returns 0 on success, <0 on error.
1514 */
1515static int
1516batch_list(Picklerobject *self, PyObject *iter)
1517{
1518 PyObject *obj;
1519 PyObject *slice[BATCHSIZE];
1520 int i, n;
1521
1522 static char append = APPEND;
1523 static char appends = APPENDS;
1524
1525 assert(iter != NULL);
1526
1527 if (self->proto == 0) {
1528 /* APPENDS isn't available; do one at a time. */
1529 for (;;) {
1530 obj = PyIter_Next(iter);
1531 if (obj == NULL) {
1532 if (PyErr_Occurred())
1533 return -1;
1534 break;
1535 }
1536 i = save(self, obj, 0);
1537 Py_DECREF(obj);
1538 if (i < 0)
1539 return -1;
1540 if (self->write_func(self, &append, 1) < 0)
1541 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001542 }
1543 return 0;
1544 }
1545
1546 /* proto > 0: write in batches of BATCHSIZE. */
1547 do {
1548 /* Get next group of (no more than) BATCHSIZE elements. */
1549 for (n = 0; n < BATCHSIZE; ++n) {
1550 obj = PyIter_Next(iter);
1551 if (obj == NULL) {
1552 if (PyErr_Occurred())
1553 goto BatchFailed;
1554 break;
1555 }
1556 slice[n] = obj;
1557 }
1558
1559 if (n > 1) {
1560 /* Pump out MARK, slice[0:n], APPENDS. */
1561 if (self->write_func(self, &MARKv, 1) < 0)
1562 goto BatchFailed;
1563 for (i = 0; i < n; ++i) {
1564 if (save(self, slice[i], 0) < 0)
1565 goto BatchFailed;
1566 }
1567 if (self->write_func(self, &appends, 1) < 0)
1568 goto BatchFailed;
1569 }
1570 else if (n == 1) {
1571 if (save(self, slice[0], 0) < 0)
1572 goto BatchFailed;
1573 if (self->write_func(self, &append, 1) < 0)
1574 goto BatchFailed;
1575 }
1576
1577 for (i = 0; i < n; ++i) {
1578 Py_DECREF(slice[i]);
1579 }
Tim Peters90975f12003-02-12 05:28:58 +00001580 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001581 return 0;
1582
1583BatchFailed:
1584 while (--n >= 0) {
1585 Py_DECREF(slice[n]);
1586 }
1587 return -1;
1588}
1589
Guido van Rossum60456fd1997-04-09 17:36:32 +00001590static int
Tim Peterscba30e22003-02-01 06:24:36 +00001591save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001592{
Tim Peters1092d642003-02-11 21:06:20 +00001593 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001594 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001595 int len;
1596 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001597
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001598 if (self->fast && !fast_save_enter(self, args))
1599 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001600
Tim Peters1092d642003-02-11 21:06:20 +00001601 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001602 if (self->bin) {
1603 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001604 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001605 }
1606 else {
1607 s[0] = MARK;
1608 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001609 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001610 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001611
Tim Peters1092d642003-02-11 21:06:20 +00001612 if (self->write_func(self, s, len) < 0)
1613 goto finally;
1614
1615 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001616 if ((len = PyList_Size(args)) < 0)
1617 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001618
Tim Peters1092d642003-02-11 21:06:20 +00001619 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001620 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001621 if (put(self, args) >= 0)
1622 res = 0;
1623 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001624 }
Tim Peters90975f12003-02-12 05:28:58 +00001625 if (put2(self, args) < 0)
1626 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001627
Tim Peters1092d642003-02-11 21:06:20 +00001628 /* Materialize the list elements. */
1629 iter = PyObject_GetIter(args);
1630 if (iter == NULL)
1631 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001632
1633 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1634 {
1635 res = batch_list(self, iter);
1636 Py_LeaveRecursiveCall();
1637 }
Tim Peters1092d642003-02-11 21:06:20 +00001638 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001640 finally:
1641 if (self->fast && !fast_save_leave(self, args))
1642 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001643
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001644 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001645}
1646
1647
Tim Peters42f08ac2003-02-11 22:43:24 +00001648/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1649 * MARK key value ... key value SETITEMS
1650 * opcode sequences. Calling code should have arranged to first create an
1651 * empty dict, or dict-like object, for the SETITEMS to operate on.
1652 * Returns 0 on success, <0 on error.
1653 *
1654 * This is very much like batch_list(). The difference between saving
1655 * elements directly, and picking apart two-tuples, is so long-winded at
1656 * the C level, though, that attempts to combine these routines were too
1657 * ugly to bear.
1658 */
1659static int
1660batch_dict(Picklerobject *self, PyObject *iter)
1661{
1662 PyObject *p;
1663 PyObject *slice[BATCHSIZE];
1664 int i, n;
1665
1666 static char setitem = SETITEM;
1667 static char setitems = SETITEMS;
1668
1669 assert(iter != NULL);
1670
1671 if (self->proto == 0) {
1672 /* SETITEMS isn't available; do one at a time. */
1673 for (;;) {
1674 p = PyIter_Next(iter);
1675 if (p == NULL) {
1676 if (PyErr_Occurred())
1677 return -1;
1678 break;
1679 }
1680 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1681 PyErr_SetString(PyExc_TypeError, "dict items "
1682 "iterator must return 2-tuples");
1683 return -1;
1684 }
1685 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1686 if (i >= 0)
1687 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1688 Py_DECREF(p);
1689 if (i < 0)
1690 return -1;
1691 if (self->write_func(self, &setitem, 1) < 0)
1692 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001693 }
1694 return 0;
1695 }
1696
1697 /* proto > 0: write in batches of BATCHSIZE. */
1698 do {
1699 /* Get next group of (no more than) BATCHSIZE elements. */
1700 for (n = 0; n < BATCHSIZE; ++n) {
1701 p = PyIter_Next(iter);
1702 if (p == NULL) {
1703 if (PyErr_Occurred())
1704 goto BatchFailed;
1705 break;
1706 }
1707 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1708 PyErr_SetString(PyExc_TypeError, "dict items "
1709 "iterator must return 2-tuples");
1710 goto BatchFailed;
1711 }
1712 slice[n] = p;
1713 }
1714
1715 if (n > 1) {
1716 /* Pump out MARK, slice[0:n], SETITEMS. */
1717 if (self->write_func(self, &MARKv, 1) < 0)
1718 goto BatchFailed;
1719 for (i = 0; i < n; ++i) {
1720 p = slice[i];
1721 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1722 goto BatchFailed;
1723 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1724 goto BatchFailed;
1725 }
1726 if (self->write_func(self, &setitems, 1) < 0)
1727 goto BatchFailed;
1728 }
1729 else if (n == 1) {
1730 p = slice[0];
1731 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1732 goto BatchFailed;
1733 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1734 goto BatchFailed;
1735 if (self->write_func(self, &setitem, 1) < 0)
1736 goto BatchFailed;
1737 }
1738
1739 for (i = 0; i < n; ++i) {
1740 Py_DECREF(slice[i]);
1741 }
Tim Peters90975f12003-02-12 05:28:58 +00001742 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001743 return 0;
1744
1745BatchFailed:
1746 while (--n >= 0) {
1747 Py_DECREF(slice[n]);
1748 }
1749 return -1;
1750}
1751
Guido van Rossum60456fd1997-04-09 17:36:32 +00001752static int
Tim Peterscba30e22003-02-01 06:24:36 +00001753save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001754{
Tim Peters42f08ac2003-02-11 22:43:24 +00001755 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001756 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001757 int len;
1758 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001759
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001760 if (self->fast && !fast_save_enter(self, args))
1761 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001762
Tim Peters42f08ac2003-02-11 22:43:24 +00001763 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001764 if (self->bin) {
1765 s[0] = EMPTY_DICT;
1766 len = 1;
1767 }
1768 else {
1769 s[0] = MARK;
1770 s[1] = DICT;
1771 len = 2;
1772 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001773
Tim Peters0bc93f52003-02-02 18:29:33 +00001774 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001775 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001776
Tim Peters42f08ac2003-02-11 22:43:24 +00001777 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001778 if ((len = PyDict_Size(args)) < 0)
1779 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001780
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001781 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001782 if (put(self, args) >= 0)
1783 res = 0;
1784 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001785 }
Tim Peters90975f12003-02-12 05:28:58 +00001786 if (put2(self, args) < 0)
1787 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001788
Tim Peters42f08ac2003-02-11 22:43:24 +00001789 /* Materialize the dict items. */
1790 iter = PyObject_CallMethod(args, "iteritems", "()");
1791 if (iter == NULL)
1792 goto finally;
Facundo Batista763d3092008-06-30 01:10:55 +00001793 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1794 {
1795 res = batch_dict(self, iter);
1796 Py_LeaveRecursiveCall();
1797 }
Tim Peters42f08ac2003-02-11 22:43:24 +00001798 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001799
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001800 finally:
1801 if (self->fast && !fast_save_leave(self, args))
1802 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001803
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001804 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001805}
1806
1807
Tim Peters84e87f32001-03-17 04:50:51 +00001808static int
Tim Peterscba30e22003-02-01 06:24:36 +00001809save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001810{
1811 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1812 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1813 char *module_str, *name_str;
1814 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001815
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001816 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001817
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001818 if (self->fast && !fast_save_enter(self, args))
1819 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001820
Tim Peters0bc93f52003-02-02 18:29:33 +00001821 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001822 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001823
Tim Peterscba30e22003-02-01 06:24:36 +00001824 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001825 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001826
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001827 if (self->bin) {
1828 if (save(self, class, 0) < 0)
1829 goto finally;
1830 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001831
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001832 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1833 PyObject *element = 0;
1834 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001835
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001836 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001837 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001838 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001839
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001840 if ((len = PyObject_Size(class_args)) < 0)
1841 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001842
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001843 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001844 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001845 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001846
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001847 if (save(self, element, 0) < 0) {
1848 Py_DECREF(element);
1849 goto finally;
1850 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001851
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001852 Py_DECREF(element);
1853 }
1854 }
1855 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001856 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1857 PyErr_Clear();
1858 else
1859 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001860 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001861
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001862 if (!self->bin) {
1863 if (!( name = ((PyClassObject *)class)->cl_name )) {
1864 PyErr_SetString(PicklingError, "class has no name");
1865 goto finally;
1866 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001867
Tim Peterscba30e22003-02-01 06:24:36 +00001868 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001869 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001870
Tim Peters84e87f32001-03-17 04:50:51 +00001871
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001872 if ((module_size = PyString_Size(module)) < 0 ||
1873 (name_size = PyString_Size(name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001874 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001875
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001876 module_str = PyString_AS_STRING((PyStringObject *)module);
1877 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001878
Tim Peters0bc93f52003-02-02 18:29:33 +00001879 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001880 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001881
Tim Peters0bc93f52003-02-02 18:29:33 +00001882 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001883 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001884
Tim Peters0bc93f52003-02-02 18:29:33 +00001885 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001886 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001887
Tim Peters0bc93f52003-02-02 18:29:33 +00001888 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001889 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001890
Tim Peters0bc93f52003-02-02 18:29:33 +00001891 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001892 goto finally;
1893 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001894 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001895 goto finally;
1896 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001897
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001898 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1899 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001900 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001901 goto finally;
1902 }
1903 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001904 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1905 PyErr_Clear();
1906 else
1907 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001908
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001909 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001910 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1911 PyErr_Clear();
1912 else
1913 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001914 res = 0;
1915 goto finally;
1916 }
1917 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001918
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001919 if (!PyDict_Check(state)) {
1920 if (put2(self, args) < 0)
1921 goto finally;
1922 }
1923 else {
1924 if (put(self, args) < 0)
1925 goto finally;
1926 }
Tim Peters84e87f32001-03-17 04:50:51 +00001927
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001928 if (save(self, state, 0) < 0)
1929 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001930
Tim Peters0bc93f52003-02-02 18:29:33 +00001931 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001932 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001933
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001934 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001935
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001936 finally:
1937 if (self->fast && !fast_save_leave(self, args))
1938 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001939
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001940 Py_XDECREF(module);
1941 Py_XDECREF(class);
1942 Py_XDECREF(state);
1943 Py_XDECREF(getinitargs_func);
1944 Py_XDECREF(getstate_func);
1945 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001946
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001947 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001948}
1949
1950
Guido van Rossum60456fd1997-04-09 17:36:32 +00001951static int
Tim Peterscba30e22003-02-01 06:24:36 +00001952save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001953{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001954 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001955 char *name_str, *module_str;
1956 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001957
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001958 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001959
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001960 if (name) {
1961 global_name = name;
1962 Py_INCREF(global_name);
1963 }
1964 else {
Tim Peterscba30e22003-02-01 06:24:36 +00001965 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001966 goto finally;
1967 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001968
Tim Peterscba30e22003-02-01 06:24:36 +00001969 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001970 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001971
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001972 if ((module_size = PyString_Size(module)) < 0 ||
1973 (name_size = PyString_Size(global_name)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001974 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001975
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001976 module_str = PyString_AS_STRING((PyStringObject *)module);
1977 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001978
Guido van Rossum75bfd052002-12-24 18:10:07 +00001979 /* XXX This can be doing a relative import. Clearly it shouldn't,
1980 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001981 mod = PyImport_ImportModule(module_str);
1982 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001983 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001984 "Can't pickle %s: import of module %s "
1985 "failed",
1986 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001987 goto finally;
1988 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001989 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001990 if (klass == NULL) {
1991 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001992 "Can't pickle %s: attribute lookup %s.%s "
1993 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001994 "OSS", args, module, global_name);
1995 goto finally;
1996 }
1997 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001998 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001999 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00002000 "Can't pickle %s: it's not the same object "
2001 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002002 "OSS", args, module, global_name);
2003 goto finally;
2004 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002005 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002006
Tim Peters731098b2003-02-04 20:56:09 +00002007 if (self->proto >= 2) {
2008 /* See whether this is in the extension registry, and if
2009 * so generate an EXT opcode.
2010 */
2011 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002012 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002013 char c_str[5];
2014 int n;
2015
2016 PyTuple_SET_ITEM(two_tuple, 0, module);
2017 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2018 py_code = PyDict_GetItem(extension_registry, two_tuple);
2019 if (py_code == NULL)
2020 goto gen_global; /* not registered */
2021
2022 /* Verify py_code has the right type and value. */
2023 if (!PyInt_Check(py_code)) {
2024 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002025 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002026 "OO", args, py_code);
2027 goto finally;
2028 }
2029 code = PyInt_AS_LONG(py_code);
2030 if (code <= 0 || code > 0x7fffffffL) {
2031 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2032 "extension code %ld is out of range",
2033 "Ol", args, code);
2034 goto finally;
2035 }
2036
2037 /* Generate an EXT opcode. */
2038 if (code <= 0xff) {
2039 c_str[0] = EXT1;
2040 c_str[1] = (char)code;
2041 n = 2;
2042 }
2043 else if (code <= 0xffff) {
2044 c_str[0] = EXT2;
2045 c_str[1] = (char)(code & 0xff);
2046 c_str[2] = (char)((code >> 8) & 0xff);
2047 n = 3;
2048 }
2049 else {
2050 c_str[0] = EXT4;
2051 c_str[1] = (char)(code & 0xff);
2052 c_str[2] = (char)((code >> 8) & 0xff);
2053 c_str[3] = (char)((code >> 16) & 0xff);
2054 c_str[4] = (char)((code >> 24) & 0xff);
2055 n = 5;
2056 }
2057
2058 if (self->write_func(self, c_str, n) >= 0)
2059 res = 0;
2060 goto finally; /* and don't memoize */
2061 }
2062
2063 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002064 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002065 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002066
Tim Peters0bc93f52003-02-02 18:29:33 +00002067 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002068 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002069
Tim Peters0bc93f52003-02-02 18:29:33 +00002070 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002071 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002072
Tim Peters0bc93f52003-02-02 18:29:33 +00002073 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002074 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002075
Tim Peters0bc93f52003-02-02 18:29:33 +00002076 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002077 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002078
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002079 if (put(self, args) < 0)
2080 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002081
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002082 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002083
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002084 finally:
2085 Py_XDECREF(module);
2086 Py_XDECREF(global_name);
2087 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002088
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002089 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002090}
2091
Guido van Rossum60456fd1997-04-09 17:36:32 +00002092static int
Tim Peterscba30e22003-02-01 06:24:36 +00002093save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002094{
2095 PyObject *pid = 0;
2096 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002097
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002098 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002099
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002100 Py_INCREF(args);
2101 ARG_TUP(self, args);
2102 if (self->arg) {
2103 pid = PyObject_Call(f, self->arg, NULL);
2104 FREE_ARG_TUP(self);
2105 }
2106 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002107
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002108 if (pid != Py_None) {
2109 if (!self->bin) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002110 if (!PyString_Check(pid)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002111 PyErr_SetString(PicklingError,
2112 "persistent id must be string");
2113 goto finally;
2114 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002115
Tim Peters0bc93f52003-02-02 18:29:33 +00002116 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002117 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002118
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002119 if ((size = PyString_Size(pid)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002120 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002121
Tim Peters0bc93f52003-02-02 18:29:33 +00002122 if (self->write_func(self,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002123 PyString_AS_STRING(
2124 (PyStringObject *)pid),
Tim Peters0bc93f52003-02-02 18:29:33 +00002125 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002126 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002127
Tim Peters0bc93f52003-02-02 18:29:33 +00002128 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002129 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002130
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002131 res = 1;
2132 goto finally;
2133 }
2134 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002135 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002136 res = -1;
2137 else
2138 res = 1;
2139 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002140
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002141 goto finally;
2142 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002143
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002144 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002145
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002146 finally:
2147 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002148
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002149 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002150}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002151
Tim Peters71fcda52003-02-14 23:05:28 +00002152/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2153 * appropriate __reduce__ method for ob.
2154 */
Tim Peters84e87f32001-03-17 04:50:51 +00002155static int
Tim Peters71fcda52003-02-14 23:05:28 +00002156save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002157{
Tim Peters71fcda52003-02-14 23:05:28 +00002158 PyObject *callable;
2159 PyObject *argtup;
2160 PyObject *state = NULL;
2161 PyObject *listitems = NULL;
2162 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002163
Tim Peters71fcda52003-02-14 23:05:28 +00002164 int use_newobj = self->proto >= 2;
2165
2166 static char reduce = REDUCE;
2167 static char build = BUILD;
2168 static char newobj = NEWOBJ;
2169
2170 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2171 &callable,
2172 &argtup,
2173 &state,
2174 &listitems,
2175 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002176 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002177
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002178 if (!PyTuple_Check(argtup)) {
2179 PyErr_SetString(PicklingError,
2180 "args from reduce() should be a tuple");
2181 return -1;
2182 }
2183
Tim Peters71fcda52003-02-14 23:05:28 +00002184 if (state == Py_None)
2185 state = NULL;
2186 if (listitems == Py_None)
2187 listitems = NULL;
2188 if (dictitems == Py_None)
2189 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002190
Tim Peters71fcda52003-02-14 23:05:28 +00002191 /* Protocol 2 special case: if callable's name is __newobj__, use
2192 * NEWOBJ. This consumes a lot of code.
2193 */
2194 if (use_newobj) {
2195 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002196
Tim Peters71fcda52003-02-14 23:05:28 +00002197 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002198 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2199 PyErr_Clear();
2200 else
2201 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002202 use_newobj = 0;
2203 }
2204 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002205 use_newobj = PyString_Check(temp) &&
2206 strcmp(PyString_AS_STRING(temp),
Tim Peters71fcda52003-02-14 23:05:28 +00002207 "__newobj__") == 0;
2208 Py_DECREF(temp);
2209 }
2210 }
2211 if (use_newobj) {
2212 PyObject *cls;
2213 PyObject *newargtup;
2214 int n, i;
2215
2216 /* Sanity checks. */
2217 n = PyTuple_Size(argtup);
2218 if (n < 1) {
2219 PyErr_SetString(PicklingError, "__newobj__ arglist "
2220 "is empty");
2221 return -1;
2222 }
2223
2224 cls = PyTuple_GET_ITEM(argtup, 0);
2225 if (! PyObject_HasAttrString(cls, "__new__")) {
2226 PyErr_SetString(PicklingError, "args[0] from "
2227 "__newobj__ args has no __new__");
2228 return -1;
2229 }
2230
2231 /* XXX How could ob be NULL? */
2232 if (ob != NULL) {
2233 PyObject *ob_dot_class;
2234
2235 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002236 if (ob_dot_class == NULL) {
2237 if (PyErr_ExceptionMatches(
2238 PyExc_AttributeError))
2239 PyErr_Clear();
2240 else
2241 return -1;
2242 }
Tim Peters71fcda52003-02-14 23:05:28 +00002243 i = ob_dot_class != cls; /* true iff a problem */
2244 Py_XDECREF(ob_dot_class);
2245 if (i) {
2246 PyErr_SetString(PicklingError, "args[0] from "
2247 "__newobj__ args has the wrong class");
2248 return -1;
2249 }
2250 }
2251
2252 /* Save the class and its __new__ arguments. */
2253 if (save(self, cls, 0) < 0)
2254 return -1;
2255
2256 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2257 if (newargtup == NULL)
2258 return -1;
2259 for (i = 1; i < n; ++i) {
2260 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2261 Py_INCREF(temp);
2262 PyTuple_SET_ITEM(newargtup, i-1, temp);
2263 }
Neal Norwitz5a29dd32007-10-05 05:01:38 +00002264 i = save(self, newargtup, 0);
Tim Peters71fcda52003-02-14 23:05:28 +00002265 Py_DECREF(newargtup);
2266 if (i < 0)
2267 return -1;
2268
2269 /* Add NEWOBJ opcode. */
2270 if (self->write_func(self, &newobj, 1) < 0)
2271 return -1;
2272 }
2273 else {
2274 /* Not using NEWOBJ. */
2275 if (save(self, callable, 0) < 0 ||
2276 save(self, argtup, 0) < 0 ||
2277 self->write_func(self, &reduce, 1) < 0)
2278 return -1;
2279 }
2280
2281 /* Memoize. */
2282 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002283 if (ob != NULL) {
2284 if (state && !PyDict_Check(state)) {
2285 if (put2(self, ob) < 0)
2286 return -1;
2287 }
Tim Peters71fcda52003-02-14 23:05:28 +00002288 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002289 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002290 }
Tim Peters84e87f32001-03-17 04:50:51 +00002291
Guido van Rossum60456fd1997-04-09 17:36:32 +00002292
Tim Peters71fcda52003-02-14 23:05:28 +00002293 if (listitems && batch_list(self, listitems) < 0)
2294 return -1;
2295
2296 if (dictitems && batch_dict(self, dictitems) < 0)
2297 return -1;
2298
2299 if (state) {
2300 if (save(self, state, 0) < 0 ||
2301 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002302 return -1;
2303 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002304
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002305 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002306}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002307
Guido van Rossum60456fd1997-04-09 17:36:32 +00002308static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002309save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002310{
2311 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002312 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2313 PyObject *arg_tup;
2314 int res = -1;
2315 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002316
Facundo Batista763d3092008-06-30 01:10:55 +00002317 if (Py_EnterRecursiveCall(" while pickling an object"))
2318 return -1;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002319
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002320 if (!pers_save && self->pers_func) {
2321 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2322 res = tmp;
2323 goto finally;
2324 }
2325 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002326
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002327 if (args == Py_None) {
2328 res = save_none(self, args);
2329 goto finally;
2330 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002331
Christian Heimese93237d2007-12-19 02:37:44 +00002332 type = Py_TYPE(args);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002333
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002334 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002335 case 'b':
2336 if (args == Py_False || args == Py_True) {
2337 res = save_bool(self, args);
2338 goto finally;
2339 }
2340 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002341 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002342 if (type == &PyInt_Type) {
2343 res = save_int(self, args);
2344 goto finally;
2345 }
2346 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002347
Guido van Rossum60456fd1997-04-09 17:36:32 +00002348 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002349 if (type == &PyLong_Type) {
2350 res = save_long(self, args);
2351 goto finally;
2352 }
2353 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002354
Guido van Rossum60456fd1997-04-09 17:36:32 +00002355 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002356 if (type == &PyFloat_Type) {
2357 res = save_float(self, args);
2358 goto finally;
2359 }
2360 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002361
Guido van Rossum60456fd1997-04-09 17:36:32 +00002362 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002363 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2364 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002365 goto finally;
2366 }
2367 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002368
Guido van Rossum60456fd1997-04-09 17:36:32 +00002369 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002370 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002371 res = save_string(self, args, 0);
2372 goto finally;
2373 }
Facundo Batista14618862008-06-22 15:27:10 +00002374 break;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002375
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002376#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002377 case 'u':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002378 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002379 res = save_unicode(self, args, 0);
2380 goto finally;
2381 }
Facundo Batista14618862008-06-22 15:27:10 +00002382 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002383#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002384 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002385
Christian Heimese93237d2007-12-19 02:37:44 +00002386 if (Py_REFCNT(args) > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002387 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002388 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002389
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002390 if (PyDict_GetItem(self->memo, py_ob_id)) {
2391 if (get(self, py_ob_id) < 0)
2392 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002393
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002394 res = 0;
2395 goto finally;
2396 }
2397 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002399 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002400 case 's':
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002401 if (type == &PyString_Type) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002402 res = save_string(self, args, 1);
2403 goto finally;
2404 }
2405 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002406
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002407#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002408 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002409 if (type == &PyUnicode_Type) {
2410 res = save_unicode(self, args, 1);
2411 goto finally;
2412 }
2413 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002414#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002415
Guido van Rossum60456fd1997-04-09 17:36:32 +00002416 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002417 if (type == &PyTuple_Type) {
2418 res = save_tuple(self, args);
2419 goto finally;
2420 }
2421 if (type == &PyType_Type) {
2422 res = save_global(self, args, NULL);
2423 goto finally;
2424 }
2425 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002426
Guido van Rossum60456fd1997-04-09 17:36:32 +00002427 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002428 if (type == &PyList_Type) {
2429 res = save_list(self, args);
2430 goto finally;
2431 }
2432 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002433
2434 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002435 if (type == &PyDict_Type) {
2436 res = save_dict(self, args);
2437 goto finally;
2438 }
2439 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002440
2441 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002442 if (type == &PyInstance_Type) {
2443 res = save_inst(self, args);
2444 goto finally;
2445 }
2446 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002447
2448 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002449 if (type == &PyClass_Type) {
2450 res = save_global(self, args, NULL);
2451 goto finally;
2452 }
2453 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002454
2455 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002456 if (type == &PyFunction_Type) {
2457 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002458 if (res && PyErr_ExceptionMatches(PickleError)) {
2459 /* fall back to reduce */
2460 PyErr_Clear();
2461 break;
2462 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002463 goto finally;
2464 }
2465 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002466
2467 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002468 if (type == &PyCFunction_Type) {
2469 res = save_global(self, args, NULL);
2470 goto finally;
2471 }
2472 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002473
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002474 if (!pers_save && self->inst_pers_func) {
2475 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2476 res = tmp;
2477 goto finally;
2478 }
2479 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002480
Jeremy Hylton39c61162002-07-16 19:47:43 +00002481 if (PyType_IsSubtype(type, &PyType_Type)) {
2482 res = save_global(self, args, NULL);
2483 goto finally;
2484 }
2485
Guido van Rossumb289b872003-02-19 01:45:13 +00002486 /* Get a reduction callable, and call it. This may come from
Georg Brandldffbf5f2008-05-20 07:49:57 +00002487 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
Guido van Rossumb289b872003-02-19 01:45:13 +00002488 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002489 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002490 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2491 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002492 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002493 Py_INCREF(args);
2494 ARG_TUP(self, args);
2495 if (self->arg) {
2496 t = PyObject_Call(__reduce__, self->arg, NULL);
2497 FREE_ARG_TUP(self);
2498 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002499 }
2500 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002501 /* Check for a __reduce_ex__ method. */
2502 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2503 if (__reduce__ != NULL) {
2504 t = PyInt_FromLong(self->proto);
2505 if (t != NULL) {
2506 ARG_TUP(self, t);
2507 t = NULL;
2508 if (self->arg) {
2509 t = PyObject_Call(__reduce__,
2510 self->arg, NULL);
2511 FREE_ARG_TUP(self);
2512 }
2513 }
2514 }
2515 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002516 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2517 PyErr_Clear();
2518 else
2519 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002520 /* Check for a __reduce__ method. */
2521 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2522 if (__reduce__ != NULL) {
2523 t = PyObject_Call(__reduce__,
2524 empty_tuple, NULL);
2525 }
2526 else {
2527 PyErr_SetObject(UnpickleableError, args);
2528 goto finally;
2529 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002530 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002531 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002532
Tim Peters71fcda52003-02-14 23:05:28 +00002533 if (t == NULL)
2534 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002535
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002536 if (PyString_Check(t)) {
Tim Peters71fcda52003-02-14 23:05:28 +00002537 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002538 goto finally;
2539 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002540
Tim Peters71fcda52003-02-14 23:05:28 +00002541 if (! PyTuple_Check(t)) {
2542 cPickle_ErrFormat(PicklingError, "Value returned by "
2543 "%s must be string or tuple",
2544 "O", __reduce__);
2545 goto finally;
2546 }
2547
2548 size = PyTuple_Size(t);
2549 if (size < 2 || size > 5) {
2550 cPickle_ErrFormat(PicklingError, "tuple returned by "
2551 "%s must contain 2 through 5 elements",
2552 "O", __reduce__);
2553 goto finally;
2554 }
2555
2556 arg_tup = PyTuple_GET_ITEM(t, 1);
2557 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2558 cPickle_ErrFormat(PicklingError, "Second element of "
2559 "tuple returned by %s must be a tuple",
2560 "O", __reduce__);
2561 goto finally;
2562 }
2563
2564 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002565
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002566 finally:
Facundo Batista763d3092008-06-30 01:10:55 +00002567 Py_LeaveRecursiveCall();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002568 Py_XDECREF(py_ob_id);
2569 Py_XDECREF(__reduce__);
2570 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002571
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002572 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002573}
2574
2575
2576static int
Tim Peterscba30e22003-02-01 06:24:36 +00002577dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002578{
2579 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002580
Tim Peters4190fb82003-02-02 16:09:05 +00002581 if (self->proto >= 2) {
2582 char bytes[2];
2583
2584 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002585 assert(self->proto >= 0 && self->proto < 256);
2586 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002587 if (self->write_func(self, bytes, 2) < 0)
2588 return -1;
2589 }
2590
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002591 if (save(self, args, 0) < 0)
2592 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002593
Tim Peters4190fb82003-02-02 16:09:05 +00002594 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002595 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002596
Tim Peters4190fb82003-02-02 16:09:05 +00002597 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002598 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002599
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002600 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002601}
2602
2603static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002604Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002605{
Tim Peterscba30e22003-02-01 06:24:36 +00002606 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002607 PyDict_Clear(self->memo);
2608 Py_INCREF(Py_None);
2609 return Py_None;
2610}
2611
2612static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002613Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002614{
2615 int l, i, rsize, ssize, clear=1, lm;
2616 long ik;
2617 PyObject *k, *r;
2618 char *s, *p, *have_get;
2619 Pdata *data;
2620
2621 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002622 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002623 return NULL;
2624
2625 /* Check to make sure we are based on a list */
2626 if (! Pdata_Check(self->file)) {
2627 PyErr_SetString(PicklingError,
2628 "Attempt to getvalue() a non-list-based pickler");
2629 return NULL;
2630 }
2631
2632 /* flush write buffer */
2633 if (write_other(self, NULL, 0) < 0) return NULL;
2634
2635 data=(Pdata*)self->file;
2636 l=data->length;
2637
2638 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002639 lm = PyDict_Size(self->memo);
2640 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002641 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002642 have_get = malloc(lm);
2643 if (have_get == NULL) return PyErr_NoMemory();
2644 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002645
2646 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002647 for (rsize = 0, i = l; --i >= 0; ) {
2648 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002649
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002650 if (PyString_Check(k))
2651 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002652
2653 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002654 ik = PyInt_AS_LONG((PyIntObject*)k);
2655 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002656 PyErr_SetString(PicklingError,
2657 "Invalid get data");
Neal Norwitz98a96002006-07-23 07:57:11 +00002658 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002659 }
Tim Petersac5687a2003-02-02 18:08:34 +00002660 if (have_get[ik]) /* with matching get */
2661 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002662 }
2663
2664 else if (! (PyTuple_Check(k) &&
2665 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002666 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002667 ) {
2668 PyErr_SetString(PicklingError,
2669 "Unexpected data in internal list");
Neal Norwitz98a96002006-07-23 07:57:11 +00002670 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002671 }
2672
2673 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002674 ik = PyInt_AS_LONG((PyIntObject *)k);
2675 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002676 PyErr_SetString(PicklingError,
2677 "Invalid get data");
2678 return NULL;
2679 }
Tim Petersac5687a2003-02-02 18:08:34 +00002680 have_get[ik] = 1;
2681 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002682 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002683 }
2684
2685 /* Now generate the result */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002686 r = PyString_FromStringAndSize(NULL, rsize);
Tim Petersac5687a2003-02-02 18:08:34 +00002687 if (r == NULL) goto err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002688 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002689
Tim Petersac5687a2003-02-02 18:08:34 +00002690 for (i = 0; i < l; i++) {
2691 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002692
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002693 if (PyString_Check(k)) {
2694 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002695 if (ssize) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002696 p=PyString_AS_STRING((PyStringObject *)k);
Tim Petersac5687a2003-02-02 18:08:34 +00002697 while (--ssize >= 0)
2698 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002699 }
2700 }
2701
2702 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002703 ik = PyInt_AS_LONG((PyIntObject *)
2704 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002705 if (ik < 256) {
2706 *s++ = BINGET;
2707 *s++ = (int)(ik & 0xff);
2708 }
2709 else {
2710 *s++ = LONG_BINGET;
2711 *s++ = (int)(ik & 0xff);
2712 *s++ = (int)((ik >> 8) & 0xff);
2713 *s++ = (int)((ik >> 16) & 0xff);
2714 *s++ = (int)((ik >> 24) & 0xff);
2715 }
2716 }
2717
2718 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002719 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002720
2721 if (have_get[ik]) { /* with matching get */
2722 if (ik < 256) {
2723 *s++ = BINPUT;
2724 *s++ = (int)(ik & 0xff);
2725 }
2726 else {
2727 *s++ = LONG_BINPUT;
2728 *s++ = (int)(ik & 0xff);
2729 *s++ = (int)((ik >> 8) & 0xff);
2730 *s++ = (int)((ik >> 16) & 0xff);
2731 *s++ = (int)((ik >> 24) & 0xff);
2732 }
2733 }
2734 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002735 }
2736
2737 if (clear) {
2738 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002739 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002740 }
2741
2742 free(have_get);
2743 return r;
2744 err:
2745 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002746 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002747}
2748
2749static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002750Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002751{
2752 PyObject *ob;
2753 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002754
Tim Peterscba30e22003-02-01 06:24:36 +00002755 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002756 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002757
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002758 if (dump(self, ob) < 0)
2759 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002760
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002761 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002762
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002763 /* XXX Why does dump() return self? */
2764 Py_INCREF(self);
2765 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002766}
2767
2768
Tim Peterscba30e22003-02-01 06:24:36 +00002769static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002770{
Neal Norwitzb0493252002-03-31 14:44:22 +00002771 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002772 PyDoc_STR("dump(object) -- "
2773 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002774 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002775 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002776 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002777 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002778 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002779};
2780
2781
2782static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002783newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002784{
2785 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002786
Tim Peters5bd2a792003-02-01 16:45:06 +00002787 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002788 proto = HIGHEST_PROTOCOL;
2789 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002790 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2791 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002792 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002793 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002794 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002795
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002796 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002797 if (self == NULL)
2798 return NULL;
2799 self->proto = proto;
2800 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002801 self->fp = NULL;
2802 self->write = NULL;
2803 self->memo = NULL;
2804 self->arg = NULL;
2805 self->pers_func = NULL;
2806 self->inst_pers_func = NULL;
2807 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002808 self->fast = 0;
2809 self->fast_container = 0;
2810 self->fast_memo = NULL;
2811 self->buf_size = 0;
2812 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002813
Tim Peters5bd2a792003-02-01 16:45:06 +00002814 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002815 if (file)
2816 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002817 else {
2818 file = Pdata_New();
2819 if (file == NULL)
2820 goto err;
2821 }
2822 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002823
Tim Peterscba30e22003-02-01 06:24:36 +00002824 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002825 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002826
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002827 if (PyFile_Check(file)) {
2828 self->fp = PyFile_AsFile(file);
2829 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002830 PyErr_SetString(PyExc_ValueError,
2831 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002832 goto err;
2833 }
2834 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002835 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002836 else if (PycStringIO_OutputCheck(file)) {
2837 self->write_func = write_cStringIO;
2838 }
2839 else if (file == Py_None) {
2840 self->write_func = write_none;
2841 }
2842 else {
2843 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002845 if (! Pdata_Check(file)) {
2846 self->write = PyObject_GetAttr(file, write_str);
2847 if (!self->write) {
2848 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002849 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002850 "argument must have 'write' "
2851 "attribute");
2852 goto err;
2853 }
2854 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002855
Tim Peters5bd2a792003-02-01 16:45:06 +00002856 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2857 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002858 PyErr_NoMemory();
2859 goto err;
2860 }
2861 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002862
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002863 if (PyEval_GetRestricted()) {
2864 /* Restricted execution, get private tables */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00002865 PyObject *m = PyImport_Import(copyreg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002866
Tim Peters5b7da392003-02-04 00:21:07 +00002867 if (m == NULL)
2868 goto err;
2869 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002870 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002871 if (self->dispatch_table == NULL)
2872 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002873 }
2874 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002875 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002876 Py_INCREF(dispatch_table);
2877 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002878 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002880 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002881
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002882 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002883 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002884 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002885}
2886
2887
2888static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00002889get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002890{
Martin v. Löwis15e62742006-02-27 16:46:16 +00002891 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002892 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002893 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002894
Tim Peters92c8bb32003-02-13 23:00:26 +00002895 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00002896 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00002897 * accepts Pickler() and Pickler(integer) too. The meaning then
2898 * is clear as mud, undocumented, and not supported by pickle.py.
2899 * I'm told Zope uses this, but I haven't traced into this code
2900 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002901 */
2902 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002903 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002904 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00002905 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2906 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002907 return NULL;
2908 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002909 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002910}
2911
2912
2913static void
Tim Peterscba30e22003-02-01 06:24:36 +00002914Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002915{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002916 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002917 Py_XDECREF(self->write);
2918 Py_XDECREF(self->memo);
2919 Py_XDECREF(self->fast_memo);
2920 Py_XDECREF(self->arg);
2921 Py_XDECREF(self->file);
2922 Py_XDECREF(self->pers_func);
2923 Py_XDECREF(self->inst_pers_func);
2924 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002925 PyMem_Free(self->write_buf);
Christian Heimese93237d2007-12-19 02:37:44 +00002926 Py_TYPE(self)->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002927}
2928
2929static int
2930Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2931{
Thomas Woutersc6e55062006-04-15 21:47:09 +00002932 Py_VISIT(self->write);
2933 Py_VISIT(self->memo);
2934 Py_VISIT(self->fast_memo);
2935 Py_VISIT(self->arg);
2936 Py_VISIT(self->file);
2937 Py_VISIT(self->pers_func);
2938 Py_VISIT(self->inst_pers_func);
2939 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002940 return 0;
2941}
2942
2943static int
2944Pickler_clear(Picklerobject *self)
2945{
Thomas Woutersedf17d82006-04-15 17:28:34 +00002946 Py_CLEAR(self->write);
2947 Py_CLEAR(self->memo);
2948 Py_CLEAR(self->fast_memo);
2949 Py_CLEAR(self->arg);
2950 Py_CLEAR(self->file);
2951 Py_CLEAR(self->pers_func);
2952 Py_CLEAR(self->inst_pers_func);
2953 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002954 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002955}
2956
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002957static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002958Pickler_get_pers_func(Picklerobject *p)
2959{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002960 if (p->pers_func == NULL)
2961 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2962 else
2963 Py_INCREF(p->pers_func);
2964 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002965}
2966
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002967static int
2968Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2969{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002970 if (v == NULL) {
2971 PyErr_SetString(PyExc_TypeError,
2972 "attribute deletion is not supported");
2973 return -1;
2974 }
2975 Py_XDECREF(p->pers_func);
2976 Py_INCREF(v);
2977 p->pers_func = v;
2978 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002979}
2980
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002981static int
2982Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2983{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002984 if (v == NULL) {
2985 PyErr_SetString(PyExc_TypeError,
2986 "attribute deletion is not supported");
2987 return -1;
2988 }
2989 Py_XDECREF(p->inst_pers_func);
2990 Py_INCREF(v);
2991 p->inst_pers_func = v;
2992 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002993}
2994
2995static PyObject *
2996Pickler_get_memo(Picklerobject *p)
2997{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002998 if (p->memo == NULL)
2999 PyErr_SetString(PyExc_AttributeError, "memo");
3000 else
3001 Py_INCREF(p->memo);
3002 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003003}
3004
3005static int
3006Pickler_set_memo(Picklerobject *p, PyObject *v)
3007{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003008 if (v == NULL) {
3009 PyErr_SetString(PyExc_TypeError,
3010 "attribute deletion is not supported");
3011 return -1;
3012 }
3013 if (!PyDict_Check(v)) {
3014 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3015 return -1;
3016 }
3017 Py_XDECREF(p->memo);
3018 Py_INCREF(v);
3019 p->memo = v;
3020 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003021}
3022
3023static PyObject *
3024Pickler_get_error(Picklerobject *p)
3025{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003026 /* why is this an attribute on the Pickler? */
3027 Py_INCREF(PicklingError);
3028 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003029}
3030
3031static PyMemberDef Pickler_members[] = {
3032 {"binary", T_INT, offsetof(Picklerobject, bin)},
3033 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003034 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003035};
3036
3037static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003038 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003039 (setter)Pickler_set_pers_func},
3040 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3041 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003042 {"PicklingError", (getter)Pickler_get_error, NULL},
3043 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003044};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003045
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003046PyDoc_STRVAR(Picklertype__doc__,
3047"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003048
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003049static PyTypeObject Picklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003050 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00003051 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003052 sizeof(Picklerobject), /*tp_basicsize*/
3053 0,
3054 (destructor)Pickler_dealloc, /* tp_dealloc */
3055 0, /* tp_print */
3056 0, /* tp_getattr */
3057 0, /* tp_setattr */
3058 0, /* tp_compare */
3059 0, /* tp_repr */
3060 0, /* tp_as_number */
3061 0, /* tp_as_sequence */
3062 0, /* tp_as_mapping */
3063 0, /* tp_hash */
3064 0, /* tp_call */
3065 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003066 PyObject_GenericGetAttr, /* tp_getattro */
3067 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003068 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003069 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003070 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003071 (traverseproc)Pickler_traverse, /* tp_traverse */
3072 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003073 0, /* tp_richcompare */
3074 0, /* tp_weaklistoffset */
3075 0, /* tp_iter */
3076 0, /* tp_iternext */
3077 Pickler_methods, /* tp_methods */
3078 Pickler_members, /* tp_members */
3079 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003080};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003081
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003082static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003083find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003084{
3085 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003086
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003087 if (fc) {
3088 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003089 PyErr_SetString(UnpicklingError, "Global and instance "
3090 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003091 return NULL;
3092 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00003093 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3094 py_global_name, NULL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003095 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003096
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003097 module = PySys_GetObject("modules");
3098 if (module == NULL)
3099 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003100
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003101 module = PyDict_GetItem(module, py_module_name);
3102 if (module == NULL) {
3103 module = PyImport_Import(py_module_name);
3104 if (!module)
3105 return NULL;
3106 global = PyObject_GetAttr(module, py_global_name);
3107 Py_DECREF(module);
3108 }
3109 else
3110 global = PyObject_GetAttr(module, py_global_name);
3111 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003112}
3113
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003114static int
Tim Peterscba30e22003-02-01 06:24:36 +00003115marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003116{
3117 if (self->num_marks < 1) {
3118 PyErr_SetString(UnpicklingError, "could not find MARK");
3119 return -1;
3120 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003121
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003122 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003123}
3124
Tim Peters84e87f32001-03-17 04:50:51 +00003125
Guido van Rossum60456fd1997-04-09 17:36:32 +00003126static int
Tim Peterscba30e22003-02-01 06:24:36 +00003127load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003128{
3129 PDATA_APPEND(self->stack, Py_None, -1);
3130 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003131}
3132
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003133static int
Tim Peterscba30e22003-02-01 06:24:36 +00003134bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003135{
3136 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3137 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003138}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003139
3140static int
Tim Peterscba30e22003-02-01 06:24:36 +00003141load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003142{
3143 PyObject *py_int = 0;
3144 char *endptr, *s;
3145 int len, res = -1;
3146 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003147
Tim Peters0bc93f52003-02-02 18:29:33 +00003148 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003149 if (len < 2) return bad_readline();
3150 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003151
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003152 errno = 0;
3153 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003154
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003155 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3156 /* Hm, maybe we've got something long. Let's try reading
3157 it as a Python long object. */
3158 errno = 0;
3159 py_int = PyLong_FromString(s, NULL, 0);
3160 if (py_int == NULL) {
3161 PyErr_SetString(PyExc_ValueError,
3162 "could not convert string to int");
3163 goto finally;
3164 }
3165 }
3166 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003167 if (len == 3 && (l == 0 || l == 1)) {
3168 if (!( py_int = PyBool_FromLong(l))) goto finally;
3169 }
3170 else {
3171 if (!( py_int = PyInt_FromLong(l))) goto finally;
3172 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003173 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003174
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003175 free(s);
3176 PDATA_PUSH(self->stack, py_int, -1);
3177 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003178
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003179 finally:
3180 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003181
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003182 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003183}
3184
Tim Peters3c67d792003-02-02 17:59:11 +00003185static int
3186load_bool(Unpicklerobject *self, PyObject *boolean)
3187{
3188 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003189 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003190 return 0;
3191}
3192
Tim Petersee1a53c2003-02-02 02:57:53 +00003193/* s contains x bytes of a little-endian integer. Return its value as a
3194 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3195 * int, but when x is 4 it's a signed one. This is an historical source
3196 * of x-platform bugs.
3197 */
Tim Peters84e87f32001-03-17 04:50:51 +00003198static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003199calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003200{
3201 unsigned char c;
3202 int i;
3203 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003204
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003205 for (i = 0, l = 0L; i < x; i++) {
3206 c = (unsigned char)s[i];
3207 l |= (long)c << (i * 8);
3208 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003209#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003210 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3211 * is signed, so on a box with longs bigger than 4 bytes we need
3212 * to extend a BININT's sign bit to the full width.
3213 */
3214 if (x == 4 && l & (1L << 31))
3215 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003216#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003217 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003218}
3219
3220
3221static int
Tim Peterscba30e22003-02-01 06:24:36 +00003222load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003223{
3224 PyObject *py_int = 0;
3225 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003226
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003227 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003228
Tim Peterscba30e22003-02-01 06:24:36 +00003229 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003230 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003231
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003232 PDATA_PUSH(self->stack, py_int, -1);
3233 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003234}
3235
3236
3237static int
Tim Peterscba30e22003-02-01 06:24:36 +00003238load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003239{
3240 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003241
Tim Peters0bc93f52003-02-02 18:29:33 +00003242 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003243 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003244
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003245 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003246}
3247
3248
3249static int
Tim Peterscba30e22003-02-01 06:24:36 +00003250load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003251{
3252 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003253
Tim Peters0bc93f52003-02-02 18:29:33 +00003254 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003255 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003256
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003257 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003258}
3259
3260
3261static int
Tim Peterscba30e22003-02-01 06:24:36 +00003262load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003263{
3264 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003265
Tim Peters0bc93f52003-02-02 18:29:33 +00003266 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003267 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003268
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003269 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003270}
Tim Peters84e87f32001-03-17 04:50:51 +00003271
Guido van Rossum60456fd1997-04-09 17:36:32 +00003272static int
Tim Peterscba30e22003-02-01 06:24:36 +00003273load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003274{
3275 PyObject *l = 0;
3276 char *end, *s;
3277 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003278
Tim Peters0bc93f52003-02-02 18:29:33 +00003279 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003280 if (len < 2) return bad_readline();
3281 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003282
Tim Peterscba30e22003-02-01 06:24:36 +00003283 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003284 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003285
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003286 free(s);
3287 PDATA_PUSH(self->stack, l, -1);
3288 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003289
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003290 finally:
3291 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003292
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003293 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003294}
3295
Tim Petersee1a53c2003-02-02 02:57:53 +00003296/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3297 * data following.
3298 */
3299static int
3300load_counted_long(Unpicklerobject *self, int size)
3301{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003302 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003303 char *nbytes;
3304 unsigned char *pdata;
3305 PyObject *along;
3306
3307 assert(size == 1 || size == 4);
3308 i = self->read_func(self, &nbytes, size);
3309 if (i < 0) return -1;
3310
3311 size = calc_binint(nbytes, size);
3312 if (size < 0) {
3313 /* Corrupt or hostile pickle -- we never write one like
3314 * this.
3315 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003316 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003317 "byte count");
3318 return -1;
3319 }
3320
3321 if (size == 0)
3322 along = PyLong_FromLong(0L);
3323 else {
3324 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003325 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003326 if (i < 0) return -1;
3327 along = _PyLong_FromByteArray(pdata, (size_t)size,
3328 1 /* little endian */, 1 /* signed */);
3329 }
3330 if (along == NULL)
3331 return -1;
3332 PDATA_PUSH(self->stack, along, -1);
3333 return 0;
3334}
Tim Peters84e87f32001-03-17 04:50:51 +00003335
Guido van Rossum60456fd1997-04-09 17:36:32 +00003336static int
Tim Peterscba30e22003-02-01 06:24:36 +00003337load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003338{
3339 PyObject *py_float = 0;
3340 char *endptr, *s;
3341 int len, res = -1;
3342 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003343
Tim Peters0bc93f52003-02-02 18:29:33 +00003344 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003345 if (len < 2) return bad_readline();
3346 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003348 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003349 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003350
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003351 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3352 PyErr_SetString(PyExc_ValueError,
3353 "could not convert string to float");
3354 goto finally;
3355 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003356
Tim Peterscba30e22003-02-01 06:24:36 +00003357 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003358 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003359
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003360 free(s);
3361 PDATA_PUSH(self->stack, py_float, -1);
3362 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003363
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003364 finally:
3365 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003366
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003367 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003368}
3369
Guido van Rossum60456fd1997-04-09 17:36:32 +00003370static int
Tim Peterscba30e22003-02-01 06:24:36 +00003371load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003372{
Tim Peters9905b942003-03-20 20:53:32 +00003373 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003374 double x;
3375 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003376
Tim Peters0bc93f52003-02-02 18:29:33 +00003377 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003378 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003379
Tim Peters9905b942003-03-20 20:53:32 +00003380 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3381 if (x == -1.0 && PyErr_Occurred())
3382 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003383
Tim Peters9905b942003-03-20 20:53:32 +00003384 py_float = PyFloat_FromDouble(x);
3385 if (py_float == NULL)
3386 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003387
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003388 PDATA_PUSH(self->stack, py_float, -1);
3389 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003390}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003391
3392static int
Tim Peterscba30e22003-02-01 06:24:36 +00003393load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003394{
3395 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003396 int len, res = -1;
3397 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003398
Tim Peters0bc93f52003-02-02 18:29:33 +00003399 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003400 if (len < 2) return bad_readline();
3401 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003402
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003403
3404 /* Strip outermost quotes */
3405 while (s[len-1] <= ' ')
3406 len--;
3407 if(s[0]=='"' && s[len-1]=='"'){
3408 s[len-1] = '\0';
3409 p = s + 1 ;
3410 len -= 2;
3411 } else if(s[0]=='\'' && s[len-1]=='\''){
3412 s[len-1] = '\0';
3413 p = s + 1 ;
3414 len -= 2;
3415 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003416 goto insecure;
3417 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003418
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003419 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
Neal Norwitz99dfe3c2006-08-02 04:27:11 +00003420 free(s);
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003421 if (str) {
3422 PDATA_PUSH(self->stack, str, -1);
3423 res = 0;
3424 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003425 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003426
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003427 insecure:
3428 free(s);
3429 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3430 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003431}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003432
3433
3434static int
Tim Peterscba30e22003-02-01 06:24:36 +00003435load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003436{
3437 PyObject *py_string = 0;
3438 long l;
3439 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003440
Tim Peters0bc93f52003-02-02 18:29:33 +00003441 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003442
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003443 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003444 if (l < 0) {
3445 /* Corrupt or hostile pickle -- we never write one like
3446 * this.
3447 */
3448 PyErr_SetString(UnpicklingError,
3449 "BINSTRING pickle has negative byte count");
3450 return -1;
3451 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003452
Tim Peters0bc93f52003-02-02 18:29:33 +00003453 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003454 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003455
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003456 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003457 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003458
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003459 PDATA_PUSH(self->stack, py_string, -1);
3460 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003461}
3462
3463
3464static int
Tim Peterscba30e22003-02-01 06:24:36 +00003465load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003466{
3467 PyObject *py_string = 0;
3468 unsigned char l;
3469 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003470
Tim Peters0bc93f52003-02-02 18:29:33 +00003471 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003472 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003473
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003474 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003475
Tim Peters0bc93f52003-02-02 18:29:33 +00003476 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003477
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003478 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003479
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003480 PDATA_PUSH(self->stack, py_string, -1);
3481 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003482}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003483
3484
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003485#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003486static int
Tim Peterscba30e22003-02-01 06:24:36 +00003487load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003488{
3489 PyObject *str = 0;
3490 int len, res = -1;
3491 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003492
Tim Peters0bc93f52003-02-02 18:29:33 +00003493 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003494 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003495
Tim Peterscba30e22003-02-01 06:24:36 +00003496 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003497 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003499 PDATA_PUSH(self->stack, str, -1);
3500 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003501
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003502 finally:
3503 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003504}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003505#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003506
3507
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003508#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003509static int
Tim Peterscba30e22003-02-01 06:24:36 +00003510load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003511{
3512 PyObject *unicode;
3513 long l;
3514 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003515
Tim Peters0bc93f52003-02-02 18:29:33 +00003516 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003517
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003518 l = calc_binint(s, 4);
Gregory P. Smith9d534572008-06-11 07:41:16 +00003519 if (l < 0) {
3520 /* Corrupt or hostile pickle -- we never write one like
3521 * this.
3522 */
3523 PyErr_SetString(UnpicklingError,
3524 "BINUNICODE pickle has negative byte count");
3525 return -1;
3526 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003527
Tim Peters0bc93f52003-02-02 18:29:33 +00003528 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003529 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003530
Tim Peterscba30e22003-02-01 06:24:36 +00003531 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003532 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003533
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003534 PDATA_PUSH(self->stack, unicode, -1);
3535 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003536}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003537#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003538
3539
3540static int
Tim Peterscba30e22003-02-01 06:24:36 +00003541load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003542{
3543 PyObject *tup;
3544 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003546 if ((i = marker(self)) < 0) return -1;
3547 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3548 PDATA_PUSH(self->stack, tup, -1);
3549 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003550}
3551
3552static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003553load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003554{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003555 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003556
Tim Peters1d63c9f2003-02-02 20:29:39 +00003557 if (tup == NULL)
3558 return -1;
3559
3560 while (--len >= 0) {
3561 PyObject *element;
3562
3563 PDATA_POP(self->stack, element);
3564 if (element == NULL)
3565 return -1;
3566 PyTuple_SET_ITEM(tup, len, element);
3567 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003568 PDATA_PUSH(self->stack, tup, -1);
3569 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003570}
3571
3572static int
Tim Peterscba30e22003-02-01 06:24:36 +00003573load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003574{
3575 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003576
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003577 if (!( list=PyList_New(0))) return -1;
3578 PDATA_PUSH(self->stack, list, -1);
3579 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003580}
3581
3582static int
Tim Peterscba30e22003-02-01 06:24:36 +00003583load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003584{
3585 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003586
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003587 if (!( dict=PyDict_New())) return -1;
3588 PDATA_PUSH(self->stack, dict, -1);
3589 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003590}
3591
3592
3593static int
Tim Peterscba30e22003-02-01 06:24:36 +00003594load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003595{
3596 PyObject *list = 0;
3597 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003598
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003599 if ((i = marker(self)) < 0) return -1;
3600 if (!( list=Pdata_popList(self->stack, i))) return -1;
3601 PDATA_PUSH(self->stack, list, -1);
3602 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003603}
3604
3605static int
Tim Peterscba30e22003-02-01 06:24:36 +00003606load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003607{
3608 PyObject *dict, *key, *value;
3609 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003610
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003611 if ((i = marker(self)) < 0) return -1;
3612 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003614 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003615
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003616 for (k = i+1; k < j; k += 2) {
3617 key =self->stack->data[k-1];
3618 value=self->stack->data[k ];
3619 if (PyDict_SetItem(dict, key, value) < 0) {
3620 Py_DECREF(dict);
3621 return -1;
3622 }
3623 }
3624 Pdata_clear(self->stack, i);
3625 PDATA_PUSH(self->stack, dict, -1);
3626 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003627}
3628
3629static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003630Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003631{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003632 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003633
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003634 if (PyClass_Check(cls)) {
3635 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003637 if ((l=PyObject_Size(args)) < 0) goto err;
3638 if (!( l )) {
3639 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003640
Tim Peterscba30e22003-02-01 06:24:36 +00003641 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003642 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003643 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003644 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003645 so bypass usual construction */
3646 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003648 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003649 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003650 goto err;
3651 return inst;
3652 }
3653 Py_DECREF(__getinitargs__);
3654 }
Tim Peters84e87f32001-03-17 04:50:51 +00003655
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003656 if ((r=PyInstance_New(cls, args, NULL))) return r;
3657 else goto err;
3658 }
Tim Peters84e87f32001-03-17 04:50:51 +00003659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003660 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003661
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003662 err:
3663 {
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003664 PyObject *tp, *v, *tb, *tmp_value;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003665
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003666 PyErr_Fetch(&tp, &v, &tb);
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003667 tmp_value = v;
3668 /* NULL occurs when there was a KeyboardInterrupt */
3669 if (tmp_value == NULL)
3670 tmp_value = Py_None;
3671 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003672 Py_XDECREF(v);
3673 v=r;
3674 }
3675 PyErr_Restore(tp,v,tb);
3676 }
3677 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003678}
Tim Peters84e87f32001-03-17 04:50:51 +00003679
Guido van Rossum60456fd1997-04-09 17:36:32 +00003680
3681static int
Tim Peterscba30e22003-02-01 06:24:36 +00003682load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003683{
3684 PyObject *class, *tup, *obj=0;
3685 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003686
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003687 if ((i = marker(self)) < 0) return -1;
3688 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3689 PDATA_POP(self->stack, class);
3690 if (class) {
3691 obj = Instance_New(class, tup);
3692 Py_DECREF(class);
3693 }
3694 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003696 if (! obj) return -1;
3697 PDATA_PUSH(self->stack, obj, -1);
3698 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003699}
3700
3701
3702static int
Tim Peterscba30e22003-02-01 06:24:36 +00003703load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003704{
3705 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3706 int i, len;
3707 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003709 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003710
Tim Peters0bc93f52003-02-02 18:29:33 +00003711 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003712 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003713 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003714 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003715
Tim Peters0bc93f52003-02-02 18:29:33 +00003716 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003717 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003718 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003719 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003720 self->find_class);
3721 Py_DECREF(class_name);
3722 }
3723 }
3724 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003725
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003726 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003727
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003728 if ((tup=Pdata_popTuple(self->stack, i))) {
3729 obj = Instance_New(class, tup);
3730 Py_DECREF(tup);
3731 }
3732 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003733
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003734 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003735
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003736 PDATA_PUSH(self->stack, obj, -1);
3737 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003738}
3739
Tim Peterseab7db32003-02-13 18:24:14 +00003740static int
3741load_newobj(Unpicklerobject *self)
3742{
3743 PyObject *args = NULL;
3744 PyObject *clsraw = NULL;
3745 PyTypeObject *cls; /* clsraw cast to its true type */
3746 PyObject *obj;
3747
3748 /* Stack is ... cls argtuple, and we want to call
3749 * cls.__new__(cls, *argtuple).
3750 */
3751 PDATA_POP(self->stack, args);
3752 if (args == NULL) goto Fail;
3753 if (! PyTuple_Check(args)) {
3754 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3755 "tuple.");
3756 goto Fail;
3757 }
3758
3759 PDATA_POP(self->stack, clsraw);
3760 cls = (PyTypeObject *)clsraw;
3761 if (cls == NULL) goto Fail;
3762 if (! PyType_Check(cls)) {
3763 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3764 "isn't a type object");
3765 goto Fail;
3766 }
3767 if (cls->tp_new == NULL) {
3768 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3769 "has NULL tp_new");
3770 goto Fail;
3771 }
3772
3773 /* Call __new__. */
3774 obj = cls->tp_new(cls, args, NULL);
3775 if (obj == NULL) goto Fail;
3776
3777 Py_DECREF(args);
3778 Py_DECREF(clsraw);
3779 PDATA_PUSH(self->stack, obj, -1);
3780 return 0;
3781
3782 Fail:
3783 Py_XDECREF(args);
3784 Py_XDECREF(clsraw);
3785 return -1;
3786}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003787
3788static int
Tim Peterscba30e22003-02-01 06:24:36 +00003789load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003790{
3791 PyObject *class = 0, *module_name = 0, *class_name = 0;
3792 int len;
3793 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003794
Tim Peters0bc93f52003-02-02 18:29:33 +00003795 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003796 if (len < 2) return bad_readline();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003797 module_name = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003798 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003799
Tim Peters0bc93f52003-02-02 18:29:33 +00003800 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003801 if (len < 2) {
3802 Py_DECREF(module_name);
3803 return bad_readline();
3804 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003805 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003806 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003807 self->find_class);
3808 Py_DECREF(class_name);
3809 }
3810 }
3811 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003812
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003813 if (! class) return -1;
3814 PDATA_PUSH(self->stack, class, -1);
3815 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003816}
3817
3818
3819static int
Tim Peterscba30e22003-02-01 06:24:36 +00003820load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003821{
3822 PyObject *pid = 0;
3823 int len;
3824 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003825
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003826 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003827 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003828 if (len < 2) return bad_readline();
3829
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003830 pid = PyString_FromStringAndSize(s, len - 1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003831 if (!pid) return -1;
3832
3833 if (PyList_Check(self->pers_func)) {
3834 if (PyList_Append(self->pers_func, pid) < 0) {
3835 Py_DECREF(pid);
3836 return -1;
3837 }
3838 }
3839 else {
3840 ARG_TUP(self, pid);
3841 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003842 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003843 NULL);
3844 FREE_ARG_TUP(self);
3845 }
3846 }
3847
3848 if (! pid) return -1;
3849
3850 PDATA_PUSH(self->stack, pid, -1);
3851 return 0;
3852 }
3853 else {
3854 PyErr_SetString(UnpicklingError,
3855 "A load persistent id instruction was encountered,\n"
3856 "but no persistent_load function was specified.");
3857 return -1;
3858 }
3859}
3860
3861static int
Tim Peterscba30e22003-02-01 06:24:36 +00003862load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003863{
3864 PyObject *pid = 0;
3865
3866 if (self->pers_func) {
3867 PDATA_POP(self->stack, pid);
3868 if (! pid) return -1;
3869
3870 if (PyList_Check(self->pers_func)) {
3871 if (PyList_Append(self->pers_func, pid) < 0) {
3872 Py_DECREF(pid);
3873 return -1;
3874 }
3875 }
3876 else {
3877 ARG_TUP(self, pid);
3878 if (self->arg) {
3879 pid = PyObject_Call(self->pers_func, self->arg,
3880 NULL);
3881 FREE_ARG_TUP(self);
3882 }
3883 if (! pid) return -1;
3884 }
3885
3886 PDATA_PUSH(self->stack, pid, -1);
3887 return 0;
3888 }
3889 else {
3890 PyErr_SetString(UnpicklingError,
3891 "A load persistent id instruction was encountered,\n"
3892 "but no persistent_load function was specified.");
3893 return -1;
3894 }
3895}
3896
3897
3898static int
Tim Peterscba30e22003-02-01 06:24:36 +00003899load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003900{
3901 int len;
3902
3903 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3904
3905 /* Note that we split the (pickle.py) stack into two stacks,
3906 an object stack and a mark stack. We have to be clever and
3907 pop the right one. We do this by looking at the top of the
3908 mark stack.
3909 */
3910
3911 if ((self->num_marks > 0) &&
3912 (self->marks[self->num_marks - 1] == len))
3913 self->num_marks--;
3914 else {
3915 len--;
3916 Py_DECREF(self->stack->data[len]);
3917 self->stack->length=len;
3918 }
3919
3920 return 0;
3921}
3922
3923
3924static int
Tim Peterscba30e22003-02-01 06:24:36 +00003925load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003926{
3927 int i;
3928
3929 if ((i = marker(self)) < 0)
3930 return -1;
3931
3932 Pdata_clear(self->stack, i);
3933
3934 return 0;
3935}
3936
3937
3938static int
Tim Peterscba30e22003-02-01 06:24:36 +00003939load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003940{
3941 PyObject *last;
3942 int len;
3943
3944 if ((len = self->stack->length) <= 0) return stackUnderflow();
3945 last=self->stack->data[len-1];
3946 Py_INCREF(last);
3947 PDATA_PUSH(self->stack, last, -1);
3948 return 0;
3949}
3950
3951
3952static int
Tim Peterscba30e22003-02-01 06:24:36 +00003953load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003954{
3955 PyObject *py_str = 0, *value = 0;
3956 int len;
3957 char *s;
3958 int rc;
3959
Tim Peters0bc93f52003-02-02 18:29:33 +00003960 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003961 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003962
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003963 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003964
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003965 value = PyDict_GetItem(self->memo, py_str);
3966 if (! value) {
3967 PyErr_SetObject(BadPickleGet, py_str);
3968 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003969 }
3970 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003971 PDATA_APPEND(self->stack, value, -1);
3972 rc = 0;
3973 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003974
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003975 Py_DECREF(py_str);
3976 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003977}
3978
3979
3980static int
Tim Peterscba30e22003-02-01 06:24:36 +00003981load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003982{
3983 PyObject *py_key = 0, *value = 0;
3984 unsigned char key;
3985 char *s;
3986 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003987
Tim Peters0bc93f52003-02-02 18:29:33 +00003988 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003989
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003990 key = (unsigned char)s[0];
3991 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003992
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003993 value = PyDict_GetItem(self->memo, py_key);
3994 if (! value) {
3995 PyErr_SetObject(BadPickleGet, py_key);
3996 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003997 }
3998 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003999 PDATA_APPEND(self->stack, value, -1);
4000 rc = 0;
4001 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004002
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004003 Py_DECREF(py_key);
4004 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004005}
4006
4007
4008static int
Tim Peterscba30e22003-02-01 06:24:36 +00004009load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004010{
4011 PyObject *py_key = 0, *value = 0;
4012 unsigned char c;
4013 char *s;
4014 long key;
4015 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004016
Tim Peters0bc93f52003-02-02 18:29:33 +00004017 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004018
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004019 c = (unsigned char)s[0];
4020 key = (long)c;
4021 c = (unsigned char)s[1];
4022 key |= (long)c << 8;
4023 c = (unsigned char)s[2];
4024 key |= (long)c << 16;
4025 c = (unsigned char)s[3];
4026 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004027
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004028 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4029
4030 value = PyDict_GetItem(self->memo, py_key);
4031 if (! value) {
4032 PyErr_SetObject(BadPickleGet, py_key);
4033 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004034 }
4035 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004036 PDATA_APPEND(self->stack, value, -1);
4037 rc = 0;
4038 }
4039
4040 Py_DECREF(py_key);
4041 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004042}
4043
Tim Peters2d629652003-02-04 05:06:17 +00004044/* Push an object from the extension registry (EXT[124]). nbytes is
4045 * the number of bytes following the opcode, holding the index (code) value.
4046 */
4047static int
4048load_extension(Unpicklerobject *self, int nbytes)
4049{
4050 char *codebytes; /* the nbytes bytes after the opcode */
4051 long code; /* calc_binint returns long */
4052 PyObject *py_code; /* code as a Python int */
4053 PyObject *obj; /* the object to push */
4054 PyObject *pair; /* (module_name, class_name) */
4055 PyObject *module_name, *class_name;
4056
4057 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4058 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4059 code = calc_binint(codebytes, nbytes);
4060 if (code <= 0) { /* note that 0 is forbidden */
4061 /* Corrupt or hostile pickle. */
4062 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4063 return -1;
4064 }
4065
4066 /* Look for the code in the cache. */
4067 py_code = PyInt_FromLong(code);
4068 if (py_code == NULL) return -1;
4069 obj = PyDict_GetItem(extension_cache, py_code);
4070 if (obj != NULL) {
4071 /* Bingo. */
4072 Py_DECREF(py_code);
4073 PDATA_APPEND(self->stack, obj, -1);
4074 return 0;
4075 }
4076
4077 /* Look up the (module_name, class_name) pair. */
4078 pair = PyDict_GetItem(inverted_registry, py_code);
4079 if (pair == NULL) {
4080 Py_DECREF(py_code);
4081 PyErr_Format(PyExc_ValueError, "unregistered extension "
4082 "code %ld", code);
4083 return -1;
4084 }
4085 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004086 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004087 */
4088 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004089 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4090 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
Tim Peters2d629652003-02-04 05:06:17 +00004091 Py_DECREF(py_code);
4092 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4093 "isn't a 2-tuple of strings", code);
4094 return -1;
4095 }
4096 /* Load the object. */
4097 obj = find_class(module_name, class_name, self->find_class);
4098 if (obj == NULL) {
4099 Py_DECREF(py_code);
4100 return -1;
4101 }
4102 /* Cache code -> obj. */
4103 code = PyDict_SetItem(extension_cache, py_code, obj);
4104 Py_DECREF(py_code);
4105 if (code < 0) {
4106 Py_DECREF(obj);
4107 return -1;
4108 }
4109 PDATA_PUSH(self->stack, obj, -1);
4110 return 0;
4111}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004112
4113static int
Tim Peterscba30e22003-02-01 06:24:36 +00004114load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004115{
4116 PyObject *py_str = 0, *value = 0;
4117 int len, l;
4118 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004119
Tim Peters0bc93f52003-02-02 18:29:33 +00004120 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004121 if (l < 2) return bad_readline();
4122 if (!( len=self->stack->length )) return stackUnderflow();
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004123 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004124 value=self->stack->data[len-1];
4125 l=PyDict_SetItem(self->memo, py_str, value);
4126 Py_DECREF(py_str);
4127 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004128}
4129
4130
4131static int
Tim Peterscba30e22003-02-01 06:24:36 +00004132load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004133{
4134 PyObject *py_key = 0, *value = 0;
4135 unsigned char key;
4136 char *s;
4137 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004138
Tim Peters0bc93f52003-02-02 18:29:33 +00004139 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004140 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004141
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004142 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004143
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004144 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4145 value=self->stack->data[len-1];
4146 len=PyDict_SetItem(self->memo, py_key, value);
4147 Py_DECREF(py_key);
4148 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004149}
4150
4151
4152static int
Tim Peterscba30e22003-02-01 06:24:36 +00004153load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004154{
4155 PyObject *py_key = 0, *value = 0;
4156 long key;
4157 unsigned char c;
4158 char *s;
4159 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004160
Tim Peters0bc93f52003-02-02 18:29:33 +00004161 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004162 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004163
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004164 c = (unsigned char)s[0];
4165 key = (long)c;
4166 c = (unsigned char)s[1];
4167 key |= (long)c << 8;
4168 c = (unsigned char)s[2];
4169 key |= (long)c << 16;
4170 c = (unsigned char)s[3];
4171 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004173 if (!( py_key = PyInt_FromLong(key))) return -1;
4174 value=self->stack->data[len-1];
4175 len=PyDict_SetItem(self->memo, py_key, value);
4176 Py_DECREF(py_key);
4177 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004178}
4179
4180
4181static int
Tim Peterscba30e22003-02-01 06:24:36 +00004182do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004183{
4184 PyObject *value = 0, *list = 0, *append_method = 0;
4185 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004186
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004187 len=self->stack->length;
4188 if (!( len >= x && x > 0 )) return stackUnderflow();
4189 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004190 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004191
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004192 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004193
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004194 if (PyList_Check(list)) {
4195 PyObject *slice;
4196 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004197
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004198 slice=Pdata_popList(self->stack, x);
Neal Norwitzb59d08c2006-07-22 16:20:49 +00004199 if (! slice) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004200 list_len = PyList_GET_SIZE(list);
4201 i=PyList_SetSlice(list, list_len, list_len, slice);
4202 Py_DECREF(slice);
4203 return i;
4204 }
4205 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004206
Tim Peterscba30e22003-02-01 06:24:36 +00004207 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004208 return -1;
4209
4210 for (i = x; i < len; i++) {
4211 PyObject *junk;
4212
4213 value=self->stack->data[i];
4214 junk=0;
4215 ARG_TUP(self, value);
4216 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004217 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004218 NULL);
4219 FREE_ARG_TUP(self);
4220 }
4221 if (! junk) {
4222 Pdata_clear(self->stack, i+1);
4223 self->stack->length=x;
4224 Py_DECREF(append_method);
4225 return -1;
4226 }
4227 Py_DECREF(junk);
4228 }
4229 self->stack->length=x;
4230 Py_DECREF(append_method);
4231 }
4232
4233 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004234}
4235
4236
4237static int
Tim Peterscba30e22003-02-01 06:24:36 +00004238load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004239{
4240 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004241}
4242
4243
4244static int
Tim Peterscba30e22003-02-01 06:24:36 +00004245load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004246{
4247 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004248}
4249
4250
4251static int
Tim Peterscba30e22003-02-01 06:24:36 +00004252do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004253{
4254 PyObject *value = 0, *key = 0, *dict = 0;
4255 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004256
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004257 if (!( (len=self->stack->length) >= x
4258 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004259
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004260 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004261
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004262 for (i = x+1; i < len; i += 2) {
4263 key =self->stack->data[i-1];
4264 value=self->stack->data[i ];
4265 if (PyObject_SetItem(dict, key, value) < 0) {
4266 r=-1;
4267 break;
4268 }
4269 }
4270
4271 Pdata_clear(self->stack, x);
4272
4273 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004274}
4275
4276
Tim Peters84e87f32001-03-17 04:50:51 +00004277static int
Tim Peterscba30e22003-02-01 06:24:36 +00004278load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004279{
4280 return do_setitems(self, self->stack->length - 2);
4281}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004282
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004283static int
Tim Peterscba30e22003-02-01 06:24:36 +00004284load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004285{
4286 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004287}
4288
Tim Peters84e87f32001-03-17 04:50:51 +00004289
Guido van Rossum60456fd1997-04-09 17:36:32 +00004290static int
Tim Peterscba30e22003-02-01 06:24:36 +00004291load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004292{
Tim Peters080c88b2003-02-15 03:01:11 +00004293 PyObject *state, *inst, *slotstate;
4294 PyObject *__setstate__;
4295 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004296 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004297 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004298
Tim Peters080c88b2003-02-15 03:01:11 +00004299 /* Stack is ... instance, state. We want to leave instance at
4300 * the stack top, possibly mutated via instance.__setstate__(state).
4301 */
4302 if (self->stack->length < 2)
4303 return stackUnderflow();
4304 PDATA_POP(self->stack, state);
4305 if (state == NULL)
4306 return -1;
4307 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004308
Tim Peters080c88b2003-02-15 03:01:11 +00004309 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4310 if (__setstate__ != NULL) {
4311 PyObject *junk = NULL;
4312
4313 /* The explicit __setstate__ is responsible for everything. */
4314 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004315 if (self->arg) {
4316 junk = PyObject_Call(__setstate__, self->arg, NULL);
4317 FREE_ARG_TUP(self);
4318 }
4319 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004320 if (junk == NULL)
4321 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004322 Py_DECREF(junk);
4323 return 0;
4324 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004325 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4326 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004327 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004328
4329 /* A default __setstate__. First see whether state embeds a
4330 * slot state dict too (a proto 2 addition).
4331 */
4332 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4333 PyObject *temp = state;
4334 state = PyTuple_GET_ITEM(temp, 0);
4335 slotstate = PyTuple_GET_ITEM(temp, 1);
4336 Py_INCREF(state);
4337 Py_INCREF(slotstate);
4338 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004339 }
Tim Peters080c88b2003-02-15 03:01:11 +00004340 else
4341 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004342
Tim Peters080c88b2003-02-15 03:01:11 +00004343 /* Set inst.__dict__ from the state dict (if any). */
4344 if (state != Py_None) {
4345 PyObject *dict;
4346 if (! PyDict_Check(state)) {
4347 PyErr_SetString(UnpicklingError, "state is not a "
4348 "dictionary");
4349 goto finally;
4350 }
4351 dict = PyObject_GetAttr(inst, __dict___str);
4352 if (dict == NULL)
4353 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004354
Tim Peters080c88b2003-02-15 03:01:11 +00004355 i = 0;
4356 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4357 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4358 goto finally;
4359 }
4360 Py_DECREF(dict);
4361 }
4362
4363 /* Also set instance attributes from the slotstate dict (if any). */
4364 if (slotstate != NULL) {
4365 if (! PyDict_Check(slotstate)) {
4366 PyErr_SetString(UnpicklingError, "slot state is not "
4367 "a dictionary");
4368 goto finally;
4369 }
4370 i = 0;
4371 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4372 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4373 goto finally;
4374 }
4375 }
4376 res = 0;
4377
4378 finally:
4379 Py_DECREF(state);
4380 Py_XDECREF(slotstate);
4381 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004382}
4383
4384
4385static int
Tim Peterscba30e22003-02-01 06:24:36 +00004386load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004387{
4388 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004389
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004390 /* Note that we split the (pickle.py) stack into two stacks, an
4391 object stack and a mark stack. Here we push a mark onto the
4392 mark stack.
4393 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004394
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004395 if ((self->num_marks + 1) >= self->marks_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004396 int *marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004397 s=self->marks_size+20;
4398 if (s <= self->num_marks) s=self->num_marks + 1;
4399 if (self->marks == NULL)
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004400 marks=(int *)malloc(s * sizeof(int));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004401 else
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004402 marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004403 s * sizeof(int));
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004404 if (!marks) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004405 PyErr_NoMemory();
4406 return -1;
4407 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004408 self->marks = marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004409 self->marks_size = s;
4410 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004411
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004412 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004413
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004414 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004415}
4416
Guido van Rossum60456fd1997-04-09 17:36:32 +00004417static int
Tim Peterscba30e22003-02-01 06:24:36 +00004418load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004419{
4420 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004421
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004422 PDATA_POP(self->stack, arg_tup);
4423 if (! arg_tup) return -1;
4424 PDATA_POP(self->stack, callable);
4425 if (callable) {
4426 ob = Instance_New(callable, arg_tup);
4427 Py_DECREF(callable);
4428 }
4429 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004430
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004431 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004432
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004433 PDATA_PUSH(self->stack, ob, -1);
4434 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004435}
Tim Peters84e87f32001-03-17 04:50:51 +00004436
Tim Peters4190fb82003-02-02 16:09:05 +00004437/* Just raises an error if we don't know the protocol specified. PROTO
4438 * is the first opcode for protocols >= 2.
4439 */
4440static int
4441load_proto(Unpicklerobject *self)
4442{
4443 int i;
4444 char *protobyte;
4445
4446 i = self->read_func(self, &protobyte, 1);
4447 if (i < 0)
4448 return -1;
4449
4450 i = calc_binint(protobyte, 1);
4451 /* No point checking for < 0, since calc_binint returns an unsigned
4452 * int when chewing on 1 byte.
4453 */
4454 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004455 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004456 return 0;
4457
4458 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4459 return -1;
4460}
4461
Guido van Rossum60456fd1997-04-09 17:36:32 +00004462static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004463load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004464{
4465 PyObject *err = 0, *val = 0;
4466 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004467
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004468 self->num_marks = 0;
4469 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004470
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004471 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004472 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004473 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004474
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004475 switch (s[0]) {
4476 case NONE:
4477 if (load_none(self) < 0)
4478 break;
4479 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004480
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004481 case BININT:
4482 if (load_binint(self) < 0)
4483 break;
4484 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004485
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004486 case BININT1:
4487 if (load_binint1(self) < 0)
4488 break;
4489 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004491 case BININT2:
4492 if (load_binint2(self) < 0)
4493 break;
4494 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004495
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004496 case INT:
4497 if (load_int(self) < 0)
4498 break;
4499 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004500
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004501 case LONG:
4502 if (load_long(self) < 0)
4503 break;
4504 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004505
Tim Petersee1a53c2003-02-02 02:57:53 +00004506 case LONG1:
4507 if (load_counted_long(self, 1) < 0)
4508 break;
4509 continue;
4510
4511 case LONG4:
4512 if (load_counted_long(self, 4) < 0)
4513 break;
4514 continue;
4515
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004516 case FLOAT:
4517 if (load_float(self) < 0)
4518 break;
4519 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004520
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004521 case BINFLOAT:
4522 if (load_binfloat(self) < 0)
4523 break;
4524 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004525
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004526 case BINSTRING:
4527 if (load_binstring(self) < 0)
4528 break;
4529 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004530
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004531 case SHORT_BINSTRING:
4532 if (load_short_binstring(self) < 0)
4533 break;
4534 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004535
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004536 case STRING:
4537 if (load_string(self) < 0)
4538 break;
4539 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004540
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004541#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004542 case UNICODE:
4543 if (load_unicode(self) < 0)
4544 break;
4545 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004546
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004547 case BINUNICODE:
4548 if (load_binunicode(self) < 0)
4549 break;
4550 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004551#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004552
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004553 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004554 if (load_counted_tuple(self, 0) < 0)
4555 break;
4556 continue;
4557
4558 case TUPLE1:
4559 if (load_counted_tuple(self, 1) < 0)
4560 break;
4561 continue;
4562
4563 case TUPLE2:
4564 if (load_counted_tuple(self, 2) < 0)
4565 break;
4566 continue;
4567
4568 case TUPLE3:
4569 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004570 break;
4571 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004572
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004573 case TUPLE:
4574 if (load_tuple(self) < 0)
4575 break;
4576 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004577
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004578 case EMPTY_LIST:
4579 if (load_empty_list(self) < 0)
4580 break;
4581 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004582
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004583 case LIST:
4584 if (load_list(self) < 0)
4585 break;
4586 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004587
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004588 case EMPTY_DICT:
4589 if (load_empty_dict(self) < 0)
4590 break;
4591 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004592
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004593 case DICT:
4594 if (load_dict(self) < 0)
4595 break;
4596 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004597
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004598 case OBJ:
4599 if (load_obj(self) < 0)
4600 break;
4601 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004602
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004603 case INST:
4604 if (load_inst(self) < 0)
4605 break;
4606 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004607
Tim Peterseab7db32003-02-13 18:24:14 +00004608 case NEWOBJ:
4609 if (load_newobj(self) < 0)
4610 break;
4611 continue;
4612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004613 case GLOBAL:
4614 if (load_global(self) < 0)
4615 break;
4616 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004617
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004618 case APPEND:
4619 if (load_append(self) < 0)
4620 break;
4621 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004622
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004623 case APPENDS:
4624 if (load_appends(self) < 0)
4625 break;
4626 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004628 case BUILD:
4629 if (load_build(self) < 0)
4630 break;
4631 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004632
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004633 case DUP:
4634 if (load_dup(self) < 0)
4635 break;
4636 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004637
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004638 case BINGET:
4639 if (load_binget(self) < 0)
4640 break;
4641 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004642
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004643 case LONG_BINGET:
4644 if (load_long_binget(self) < 0)
4645 break;
4646 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004648 case GET:
4649 if (load_get(self) < 0)
4650 break;
4651 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004652
Tim Peters2d629652003-02-04 05:06:17 +00004653 case EXT1:
4654 if (load_extension(self, 1) < 0)
4655 break;
4656 continue;
4657
4658 case EXT2:
4659 if (load_extension(self, 2) < 0)
4660 break;
4661 continue;
4662
4663 case EXT4:
4664 if (load_extension(self, 4) < 0)
4665 break;
4666 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004667 case MARK:
4668 if (load_mark(self) < 0)
4669 break;
4670 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004671
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004672 case BINPUT:
4673 if (load_binput(self) < 0)
4674 break;
4675 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004676
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004677 case LONG_BINPUT:
4678 if (load_long_binput(self) < 0)
4679 break;
4680 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004681
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004682 case PUT:
4683 if (load_put(self) < 0)
4684 break;
4685 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004686
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004687 case POP:
4688 if (load_pop(self) < 0)
4689 break;
4690 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004691
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004692 case POP_MARK:
4693 if (load_pop_mark(self) < 0)
4694 break;
4695 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004696
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004697 case SETITEM:
4698 if (load_setitem(self) < 0)
4699 break;
4700 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004701
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004702 case SETITEMS:
4703 if (load_setitems(self) < 0)
4704 break;
4705 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004706
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004707 case STOP:
4708 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004709
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004710 case PERSID:
4711 if (load_persid(self) < 0)
4712 break;
4713 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004714
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004715 case BINPERSID:
4716 if (load_binpersid(self) < 0)
4717 break;
4718 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004719
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004720 case REDUCE:
4721 if (load_reduce(self) < 0)
4722 break;
4723 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004724
Tim Peters4190fb82003-02-02 16:09:05 +00004725 case PROTO:
4726 if (load_proto(self) < 0)
4727 break;
4728 continue;
4729
Tim Peters3c67d792003-02-02 17:59:11 +00004730 case NEWTRUE:
4731 if (load_bool(self, Py_True) < 0)
4732 break;
4733 continue;
4734
4735 case NEWFALSE:
4736 if (load_bool(self, Py_False) < 0)
4737 break;
4738 continue;
4739
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004740 case '\0':
4741 /* end of file */
4742 PyErr_SetNone(PyExc_EOFError);
4743 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004744
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004745 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004746 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004747 "invalid load key, '%s'.",
4748 "c", s[0]);
4749 return NULL;
4750 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004751
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004752 break;
4753 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004755 if ((err = PyErr_Occurred())) {
4756 if (err == PyExc_EOFError) {
4757 PyErr_SetNone(PyExc_EOFError);
4758 }
4759 return NULL;
4760 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004761
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004762 PDATA_POP(self->stack, val);
4763 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004764}
Tim Peters84e87f32001-03-17 04:50:51 +00004765
Guido van Rossum60456fd1997-04-09 17:36:32 +00004766
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004767/* No-load functions to support noload, which is used to
4768 find persistent references. */
4769
4770static int
Tim Peterscba30e22003-02-01 06:24:36 +00004771noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004772{
4773 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004775 if ((i = marker(self)) < 0) return -1;
4776 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004777}
4778
4779
4780static int
Tim Peterscba30e22003-02-01 06:24:36 +00004781noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004782{
4783 int i;
4784 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004785
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004786 if ((i = marker(self)) < 0) return -1;
4787 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004788 if (self->readline_func(self, &s) < 0) return -1;
4789 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004790 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004791 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004792}
4793
4794static int
Tim Peterseab7db32003-02-13 18:24:14 +00004795noload_newobj(Unpicklerobject *self)
4796{
4797 PyObject *obj;
4798
4799 PDATA_POP(self->stack, obj); /* pop argtuple */
4800 if (obj == NULL) return -1;
4801 Py_DECREF(obj);
4802
4803 PDATA_POP(self->stack, obj); /* pop cls */
4804 if (obj == NULL) return -1;
4805 Py_DECREF(obj);
4806
4807 PDATA_APPEND(self->stack, Py_None, -1);
4808 return 0;
4809}
4810
4811static int
Tim Peterscba30e22003-02-01 06:24:36 +00004812noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004813{
4814 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004815
Tim Peters0bc93f52003-02-02 18:29:33 +00004816 if (self->readline_func(self, &s) < 0) return -1;
4817 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004818 PDATA_APPEND(self->stack, Py_None,-1);
4819 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004820}
4821
4822static int
Tim Peterscba30e22003-02-01 06:24:36 +00004823noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004824{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004825
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004826 if (self->stack->length < 2) return stackUnderflow();
4827 Pdata_clear(self->stack, self->stack->length-2);
4828 PDATA_APPEND(self->stack, Py_None,-1);
4829 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004830}
4831
4832static int
4833noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004834
Guido van Rossum053b8df1998-11-25 16:18:00 +00004835 if (self->stack->length < 1) return stackUnderflow();
4836 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004837 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004838}
4839
Tim Peters2d629652003-02-04 05:06:17 +00004840static int
4841noload_extension(Unpicklerobject *self, int nbytes)
4842{
4843 char *codebytes;
4844
4845 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4846 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4847 PDATA_APPEND(self->stack, Py_None, -1);
4848 return 0;
4849}
4850
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004851
4852static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004853noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004854{
4855 PyObject *err = 0, *val = 0;
4856 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004857
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004858 self->num_marks = 0;
4859 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004860
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004861 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004862 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004863 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004864
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004865 switch (s[0]) {
4866 case NONE:
4867 if (load_none(self) < 0)
4868 break;
4869 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004870
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004871 case BININT:
4872 if (load_binint(self) < 0)
4873 break;
4874 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004876 case BININT1:
4877 if (load_binint1(self) < 0)
4878 break;
4879 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004880
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004881 case BININT2:
4882 if (load_binint2(self) < 0)
4883 break;
4884 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004885
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004886 case INT:
4887 if (load_int(self) < 0)
4888 break;
4889 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004890
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004891 case LONG:
4892 if (load_long(self) < 0)
4893 break;
4894 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004895
Tim Peters4190fb82003-02-02 16:09:05 +00004896 case LONG1:
4897 if (load_counted_long(self, 1) < 0)
4898 break;
4899 continue;
4900
4901 case LONG4:
4902 if (load_counted_long(self, 4) < 0)
4903 break;
4904 continue;
4905
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004906 case FLOAT:
4907 if (load_float(self) < 0)
4908 break;
4909 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004910
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004911 case BINFLOAT:
4912 if (load_binfloat(self) < 0)
4913 break;
4914 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004915
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004916 case BINSTRING:
4917 if (load_binstring(self) < 0)
4918 break;
4919 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004920
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004921 case SHORT_BINSTRING:
4922 if (load_short_binstring(self) < 0)
4923 break;
4924 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004925
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004926 case STRING:
4927 if (load_string(self) < 0)
4928 break;
4929 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004930
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004931#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004932 case UNICODE:
4933 if (load_unicode(self) < 0)
4934 break;
4935 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004936
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004937 case BINUNICODE:
4938 if (load_binunicode(self) < 0)
4939 break;
4940 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004941#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004942
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004943 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004944 if (load_counted_tuple(self, 0) < 0)
4945 break;
4946 continue;
4947
4948 case TUPLE1:
4949 if (load_counted_tuple(self, 1) < 0)
4950 break;
4951 continue;
4952
4953 case TUPLE2:
4954 if (load_counted_tuple(self, 2) < 0)
4955 break;
4956 continue;
4957
4958 case TUPLE3:
4959 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004960 break;
4961 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004962
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004963 case TUPLE:
4964 if (load_tuple(self) < 0)
4965 break;
4966 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004967
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004968 case EMPTY_LIST:
4969 if (load_empty_list(self) < 0)
4970 break;
4971 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004972
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004973 case LIST:
4974 if (load_list(self) < 0)
4975 break;
4976 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004977
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004978 case EMPTY_DICT:
4979 if (load_empty_dict(self) < 0)
4980 break;
4981 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004982
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004983 case DICT:
4984 if (load_dict(self) < 0)
4985 break;
4986 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004988 case OBJ:
4989 if (noload_obj(self) < 0)
4990 break;
4991 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004992
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004993 case INST:
4994 if (noload_inst(self) < 0)
4995 break;
4996 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004997
Tim Peterseab7db32003-02-13 18:24:14 +00004998 case NEWOBJ:
4999 if (noload_newobj(self) < 0)
5000 break;
5001 continue;
5002
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005003 case GLOBAL:
5004 if (noload_global(self) < 0)
5005 break;
5006 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005007
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005008 case APPEND:
5009 if (load_append(self) < 0)
5010 break;
5011 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005012
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005013 case APPENDS:
5014 if (load_appends(self) < 0)
5015 break;
5016 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005017
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005018 case BUILD:
5019 if (noload_build(self) < 0)
5020 break;
5021 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005022
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005023 case DUP:
5024 if (load_dup(self) < 0)
5025 break;
5026 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005027
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005028 case BINGET:
5029 if (load_binget(self) < 0)
5030 break;
5031 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005032
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005033 case LONG_BINGET:
5034 if (load_long_binget(self) < 0)
5035 break;
5036 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005037
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005038 case GET:
5039 if (load_get(self) < 0)
5040 break;
5041 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005042
Tim Peters2d629652003-02-04 05:06:17 +00005043 case EXT1:
5044 if (noload_extension(self, 1) < 0)
5045 break;
5046 continue;
5047
5048 case EXT2:
5049 if (noload_extension(self, 2) < 0)
5050 break;
5051 continue;
5052
5053 case EXT4:
5054 if (noload_extension(self, 4) < 0)
5055 break;
5056 continue;
5057
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005058 case MARK:
5059 if (load_mark(self) < 0)
5060 break;
5061 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005062
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005063 case BINPUT:
5064 if (load_binput(self) < 0)
5065 break;
5066 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005067
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005068 case LONG_BINPUT:
5069 if (load_long_binput(self) < 0)
5070 break;
5071 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005072
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005073 case PUT:
5074 if (load_put(self) < 0)
5075 break;
5076 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005077
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005078 case POP:
5079 if (load_pop(self) < 0)
5080 break;
5081 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005082
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005083 case POP_MARK:
5084 if (load_pop_mark(self) < 0)
5085 break;
5086 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005087
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005088 case SETITEM:
5089 if (load_setitem(self) < 0)
5090 break;
5091 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005092
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005093 case SETITEMS:
5094 if (load_setitems(self) < 0)
5095 break;
5096 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005097
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005098 case STOP:
5099 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005100
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005101 case PERSID:
5102 if (load_persid(self) < 0)
5103 break;
5104 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005105
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005106 case BINPERSID:
5107 if (load_binpersid(self) < 0)
5108 break;
5109 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005110
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005111 case REDUCE:
5112 if (noload_reduce(self) < 0)
5113 break;
5114 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005115
Tim Peters4190fb82003-02-02 16:09:05 +00005116 case PROTO:
5117 if (load_proto(self) < 0)
5118 break;
5119 continue;
5120
Tim Peters3c67d792003-02-02 17:59:11 +00005121 case NEWTRUE:
5122 if (load_bool(self, Py_True) < 0)
5123 break;
5124 continue;
5125
5126 case NEWFALSE:
5127 if (load_bool(self, Py_False) < 0)
5128 break;
5129 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005130 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005131 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005132 "invalid load key, '%s'.",
5133 "c", s[0]);
5134 return NULL;
5135 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005136
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005137 break;
5138 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005139
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005140 if ((err = PyErr_Occurred())) {
5141 if (err == PyExc_EOFError) {
5142 PyErr_SetNone(PyExc_EOFError);
5143 }
5144 return NULL;
5145 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005146
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005147 PDATA_POP(self->stack, val);
5148 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005149}
Tim Peters84e87f32001-03-17 04:50:51 +00005150
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005151
Guido van Rossum60456fd1997-04-09 17:36:32 +00005152static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005153Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005154{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005155 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005156}
5157
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005158static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005159Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005160{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005161 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005162}
5163
Guido van Rossum60456fd1997-04-09 17:36:32 +00005164
5165static struct PyMethodDef Unpickler_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +00005166 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005167 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005168 },
Georg Brandl96a8c392006-05-29 21:04:52 +00005169 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005170 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005171 "noload() -- not load a pickle, but go through most of the motions\n"
5172 "\n"
5173 "This function can be used to read past a pickle without instantiating\n"
5174 "any objects or importing any modules. It can also be used to find all\n"
5175 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005176 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005177 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005178 {NULL, NULL} /* sentinel */
5179};
5180
5181
5182static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005183newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005184{
5185 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005186
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005187 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005188 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005189
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005190 self->file = NULL;
5191 self->arg = NULL;
5192 self->stack = (Pdata*)Pdata_New();
5193 self->pers_func = NULL;
5194 self->last_string = NULL;
5195 self->marks = NULL;
5196 self->num_marks = 0;
5197 self->marks_size = 0;
5198 self->buf_size = 0;
5199 self->read = NULL;
5200 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005201 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005202
Tim Peterscba30e22003-02-01 06:24:36 +00005203 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005204 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005205
Neal Norwitzb59d08c2006-07-22 16:20:49 +00005206 if (!self->stack)
5207 goto err;
5208
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005209 Py_INCREF(f);
5210 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005211
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005212 /* Set read, readline based on type of f */
5213 if (PyFile_Check(f)) {
5214 self->fp = PyFile_AsFile(f);
5215 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005216 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005217 "I/O operation on closed file");
5218 goto err;
5219 }
5220 self->read_func = read_file;
5221 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005222 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005223 else if (PycStringIO_InputCheck(f)) {
5224 self->fp = NULL;
5225 self->read_func = read_cStringIO;
5226 self->readline_func = readline_cStringIO;
5227 }
5228 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005229
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005230 self->fp = NULL;
5231 self->read_func = read_other;
5232 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005233
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005234 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5235 (self->read = PyObject_GetAttr(f, read_str)))) {
5236 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005237 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005238 "argument must have 'read' and "
5239 "'readline' attributes" );
5240 goto err;
5241 }
5242 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005243 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005244
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005245 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005246
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005247 err:
5248 Py_DECREF((PyObject *)self);
5249 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005250}
5251
5252
5253static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005254get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005255{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005256 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005257}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005258
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005259
Guido van Rossum60456fd1997-04-09 17:36:32 +00005260static void
Tim Peterscba30e22003-02-01 06:24:36 +00005261Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005262{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005263 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005264 Py_XDECREF(self->readline);
5265 Py_XDECREF(self->read);
5266 Py_XDECREF(self->file);
5267 Py_XDECREF(self->memo);
5268 Py_XDECREF(self->stack);
5269 Py_XDECREF(self->pers_func);
5270 Py_XDECREF(self->arg);
5271 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005272 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005273
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005274 if (self->marks) {
5275 free(self->marks);
5276 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005277
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005278 if (self->buf_size) {
5279 free(self->buf);
5280 }
Tim Peters84e87f32001-03-17 04:50:51 +00005281
Christian Heimese93237d2007-12-19 02:37:44 +00005282 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005283}
5284
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005285static int
5286Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5287{
Thomas Woutersc6e55062006-04-15 21:47:09 +00005288 Py_VISIT(self->readline);
5289 Py_VISIT(self->read);
5290 Py_VISIT(self->file);
5291 Py_VISIT(self->memo);
5292 Py_VISIT(self->stack);
5293 Py_VISIT(self->pers_func);
5294 Py_VISIT(self->arg);
5295 Py_VISIT(self->last_string);
5296 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005297 return 0;
5298}
5299
5300static int
5301Unpickler_clear(Unpicklerobject *self)
5302{
Thomas Woutersedf17d82006-04-15 17:28:34 +00005303 Py_CLEAR(self->readline);
5304 Py_CLEAR(self->read);
5305 Py_CLEAR(self->file);
5306 Py_CLEAR(self->memo);
5307 Py_CLEAR(self->stack);
5308 Py_CLEAR(self->pers_func);
5309 Py_CLEAR(self->arg);
5310 Py_CLEAR(self->last_string);
5311 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005312 return 0;
5313}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005314
5315static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005316Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005317{
5318 if (!strcmp(name, "persistent_load")) {
5319 if (!self->pers_func) {
5320 PyErr_SetString(PyExc_AttributeError, name);
5321 return NULL;
5322 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005323
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005324 Py_INCREF(self->pers_func);
5325 return self->pers_func;
5326 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005327
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005328 if (!strcmp(name, "find_global")) {
5329 if (!self->find_class) {
5330 PyErr_SetString(PyExc_AttributeError, name);
5331 return NULL;
5332 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005333
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005334 Py_INCREF(self->find_class);
5335 return self->find_class;
5336 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005337
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005338 if (!strcmp(name, "memo")) {
5339 if (!self->memo) {
5340 PyErr_SetString(PyExc_AttributeError, name);
5341 return NULL;
5342 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005343
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005344 Py_INCREF(self->memo);
5345 return self->memo;
5346 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005348 if (!strcmp(name, "UnpicklingError")) {
5349 Py_INCREF(UnpicklingError);
5350 return UnpicklingError;
5351 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005352
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005353 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005354}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005355
Guido van Rossum60456fd1997-04-09 17:36:32 +00005356
5357static int
Tim Peterscba30e22003-02-01 06:24:36 +00005358Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005359{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005361 if (!strcmp(name, "persistent_load")) {
5362 Py_XDECREF(self->pers_func);
5363 self->pers_func = value;
5364 Py_XINCREF(value);
5365 return 0;
5366 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005367
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005368 if (!strcmp(name, "find_global")) {
5369 Py_XDECREF(self->find_class);
5370 self->find_class = value;
5371 Py_XINCREF(value);
5372 return 0;
5373 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005374
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005375 if (! value) {
5376 PyErr_SetString(PyExc_TypeError,
5377 "attribute deletion is not supported");
5378 return -1;
5379 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005380
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005381 if (strcmp(name, "memo") == 0) {
5382 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005383 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005384 "memo must be a dictionary");
5385 return -1;
5386 }
5387 Py_XDECREF(self->memo);
5388 self->memo = value;
5389 Py_INCREF(value);
5390 return 0;
5391 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005392
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005393 PyErr_SetString(PyExc_AttributeError, name);
5394 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005395}
5396
Tim Peters5bd2a792003-02-01 16:45:06 +00005397/* ---------------------------------------------------------------------------
5398 * Module-level functions.
5399 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005400
Martin v. Löwis544f1192004-07-27 05:22:33 +00005401/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005402static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005403cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005404{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005405 static char *kwlist[] = {"obj", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005406 PyObject *ob, *file, *res = NULL;
5407 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005408 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005409
Martin v. Löwis544f1192004-07-27 05:22:33 +00005410 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5411 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005412 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005413
Tim Peters5bd2a792003-02-01 16:45:06 +00005414 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005415 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005416
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005417 if (dump(pickler, ob) < 0)
5418 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005419
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005420 Py_INCREF(Py_None);
5421 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005422
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005423 finally:
5424 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005425
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005426 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005427}
5428
5429
Martin v. Löwis544f1192004-07-27 05:22:33 +00005430/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005431static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005432cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005433{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005434 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005435 PyObject *ob, *file = 0, *res = NULL;
5436 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005437 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005438
Martin v. Löwis544f1192004-07-27 05:22:33 +00005439 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5440 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005441 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005442
Tim Peterscba30e22003-02-01 06:24:36 +00005443 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005444 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005445
Tim Peters5bd2a792003-02-01 16:45:06 +00005446 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005447 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005448
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005449 if (dump(pickler, ob) < 0)
5450 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005451
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005452 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005453
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005454 finally:
5455 Py_XDECREF(pickler);
5456 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005457
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005458 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005459}
5460
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005461
Tim Peters5bd2a792003-02-01 16:45:06 +00005462/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005463static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005464cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005465{
5466 Unpicklerobject *unpickler = 0;
Georg Brandl96a8c392006-05-29 21:04:52 +00005467 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005468
Tim Peterscba30e22003-02-01 06:24:36 +00005469 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005470 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005471
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005472 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005473
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005474 finally:
5475 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005477 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005478}
5479
5480
Tim Peters5bd2a792003-02-01 16:45:06 +00005481/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005482static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005483cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005484{
5485 PyObject *ob, *file = 0, *res = NULL;
5486 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005487
Tim Peterscba30e22003-02-01 06:24:36 +00005488 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005489 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005490
Tim Peterscba30e22003-02-01 06:24:36 +00005491 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005492 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005493
Tim Peterscba30e22003-02-01 06:24:36 +00005494 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005495 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005496
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005497 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005499 finally:
5500 Py_XDECREF(file);
5501 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005503 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005504}
5505
5506
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005507PyDoc_STRVAR(Unpicklertype__doc__,
5508"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005509
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005510static PyTypeObject Unpicklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00005511 PyVarObject_HEAD_INIT(NULL, 0)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005512 "cPickle.Unpickler", /*tp_name*/
5513 sizeof(Unpicklerobject), /*tp_basicsize*/
5514 0,
5515 (destructor)Unpickler_dealloc, /* tp_dealloc */
5516 0, /* tp_print */
5517 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5518 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5519 0, /* tp_compare */
5520 0, /* tp_repr */
5521 0, /* tp_as_number */
5522 0, /* tp_as_sequence */
5523 0, /* tp_as_mapping */
5524 0, /* tp_hash */
5525 0, /* tp_call */
5526 0, /* tp_str */
5527 0, /* tp_getattro */
5528 0, /* tp_setattro */
5529 0, /* tp_as_buffer */
5530 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5531 Unpicklertype__doc__, /* tp_doc */
5532 (traverseproc)Unpickler_traverse, /* tp_traverse */
5533 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005534};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005535
Guido van Rossum60456fd1997-04-09 17:36:32 +00005536static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005537 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5538 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005539 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005540 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005541 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005542 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005543
Martin v. Löwis544f1192004-07-27 05:22:33 +00005544 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5545 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005546 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005547 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005548 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005549 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005550
Georg Brandl96a8c392006-05-29 21:04:52 +00005551 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005552 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005553
Neal Norwitzb0493252002-03-31 14:44:22 +00005554 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005555 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005556
Martin v. Löwis544f1192004-07-27 05:22:33 +00005557 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5558 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005559 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005560 "This takes a file-like object for writing a pickle data stream.\n"
5561 "The optional proto argument tells the pickler to use the given\n"
5562 "protocol; supported protocols are 0, 1, 2. The default\n"
5563 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5564 "only protocol that can be written to a file opened in text\n"
5565 "mode and read back successfully. When using a protocol higher\n"
5566 "than 0, make sure the file is opened in binary mode, both when\n"
5567 "pickling and unpickling.)\n"
5568 "\n"
5569 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5570 "more efficient than protocol 1.\n"
5571 "\n"
5572 "Specifying a negative protocol version selects the highest\n"
5573 "protocol version supported. The higher the protocol used, the\n"
5574 "more recent the version of Python needed to read the pickle\n"
5575 "produced.\n"
5576 "\n"
5577 "The file parameter must have a write() method that accepts a single\n"
5578 "string argument. It can thus be an open file object, a StringIO\n"
5579 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005580 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005581
Georg Brandl96a8c392006-05-29 21:04:52 +00005582 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005583 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5584
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005585 { NULL, NULL }
5586};
5587
Guido van Rossum60456fd1997-04-09 17:36:32 +00005588static int
Tim Peterscba30e22003-02-01 06:24:36 +00005589init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005590{
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005591 PyObject *copyreg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005592
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005593#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005594
Tim Peters3cfe7542003-05-21 21:29:48 +00005595 if (PyType_Ready(&Unpicklertype) < 0)
5596 return -1;
5597 if (PyType_Ready(&Picklertype) < 0)
5598 return -1;
5599
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005600 INIT_STR(__class__);
5601 INIT_STR(__getinitargs__);
5602 INIT_STR(__dict__);
5603 INIT_STR(__getstate__);
5604 INIT_STR(__setstate__);
5605 INIT_STR(__name__);
5606 INIT_STR(__main__);
5607 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005608 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005609 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005610 INIT_STR(append);
5611 INIT_STR(read);
5612 INIT_STR(readline);
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005613 INIT_STR(copyreg);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005614 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005615
Georg Brandldffbf5f2008-05-20 07:49:57 +00005616 if (!( copyreg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005617 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005618
Tim Peters1f1b2d22003-02-01 02:16:37 +00005619 /* This is special because we want to use a different
5620 one in restricted mode. */
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005621 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005622 if (!dispatch_table) return -1;
5623
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005624 extension_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005625 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005626 if (!extension_registry) return -1;
5627
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005628 inverted_registry = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005629 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005630 if (!inverted_registry) return -1;
5631
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005632 extension_cache = PyObject_GetAttrString(copyreg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005633 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005634 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005635
Alexandre Vassalotti9510e4a2008-05-11 08:25:28 +00005636 Py_DECREF(copyreg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005637
Tim Peters731098b2003-02-04 20:56:09 +00005638 if (!(empty_tuple = PyTuple_New(0)))
5639 return -1;
5640
5641 two_tuple = PyTuple_New(2);
5642 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005643 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005644 /* We use this temp container with no regard to refcounts, or to
5645 * keeping containees alive. Exempt from GC, because we don't
5646 * want anything looking at two_tuple() by magic.
5647 */
5648 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005649
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005650 /* Ugh */
5651 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5652 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5653 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005655 if (!( t=PyDict_New())) return -1;
5656 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005657 "def __str__(self):\n"
5658 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5659 Py_file_input,
5660 module_dict, t) )) return -1;
5661 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005662
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005663 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005664 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005665 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005666
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005667 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005668
Tim Peterscba30e22003-02-01 06:24:36 +00005669 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005670 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005671 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005672 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005673
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005674 if (!( t=PyDict_New())) return -1;
5675 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005676 "def __str__(self):\n"
5677 " a=self.args\n"
5678 " a=a and type(a[0]) or '(what)'\n"
5679 " return 'Cannot pickle %s objects' % a\n"
5680 , Py_file_input,
5681 module_dict, t) )) return -1;
5682 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005683
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005684 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005685 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005686 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005687
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005688 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005689
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005690 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005691 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005692 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005693
Martin v. Löwis658009a2002-09-16 17:26:24 +00005694 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5695 UnpicklingError, NULL)))
5696 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005697
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005698 if (PyDict_SetItemString(module_dict, "PickleError",
5699 PickleError) < 0)
5700 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005701
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005702 if (PyDict_SetItemString(module_dict, "PicklingError",
5703 PicklingError) < 0)
5704 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005706 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5707 UnpicklingError) < 0)
5708 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005709
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005710 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5711 UnpickleableError) < 0)
5712 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005713
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005714 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5715 BadPickleGet) < 0)
5716 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005717
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005718 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005719
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005720 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005721}
5722
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005723#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5724#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005725#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005726PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005727initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005728{
5729 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005730 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005731 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005732 PyObject *format_version;
5733 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005734
Alexandre Vassalotti3c4971c2008-05-16 19:14:31 +00005735 /* XXX: Should mention that the pickle module will include the C
5736 XXX: optimized implementation automatically. */
5737 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5738 "Python 3.0", 2) < 0)
5739 return;
5740
Christian Heimese93237d2007-12-19 02:37:44 +00005741 Py_TYPE(&Picklertype) = &PyType_Type;
5742 Py_TYPE(&Unpicklertype) = &PyType_Type;
5743 Py_TYPE(&PdataType) = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005744
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005745 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005746 * so we're forced to use a temporary dictionary. :(
5747 */
5748 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005749 if (!di) return;
5750 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005751
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005752 /* Create the module and add the functions */
5753 m = Py_InitModule4("cPickle", cPickle_methods,
5754 cPickle_module_documentation,
5755 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005756 if (m == NULL)
5757 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005758
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005759 /* Add some symbolic constants to the module */
5760 d = PyModule_GetDict(m);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005761 v = PyString_FromString(rev);
Tim Peters5b7da392003-02-04 00:21:07 +00005762 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005763 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005764
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005765 /* Copy data from di. Waaa. */
5766 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5767 if (PyObject_SetItem(d, k, v) < 0) {
5768 Py_DECREF(di);
5769 return;
5770 }
5771 }
5772 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005773
Tim Peters8587b3c2003-02-13 15:44:41 +00005774 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5775 if (i < 0)
5776 return;
5777
Tim Peters5b7da392003-02-04 00:21:07 +00005778 /* These are purely informational; no code uses them. */
5779 /* File format version we write. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005780 format_version = PyString_FromString("2.0");
Tim Peters5b7da392003-02-04 00:21:07 +00005781 /* Format versions we can read. */
5782 compatible_formats = Py_BuildValue("[sssss]",
5783 "1.0", /* Original protocol 0 */
5784 "1.1", /* Protocol 0 + INST */
5785 "1.2", /* Original protocol 1 */
5786 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005787 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005788 PyDict_SetItemString(d, "format_version", format_version);
5789 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5790 Py_XDECREF(format_version);
5791 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005792}