blob: b9b95c8421b9777553dac13f16de6c0831d3219f [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
Tim Peters5b7da392003-02-04 00:21:07 +0000108/* copy_reg.dispatch_table, {type_object: pickling_function} */
109static PyObject *dispatch_table;
110
111/* For EXT[124] opcodes. */
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000112/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000113static PyObject *extension_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000114/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000115static PyObject *inverted_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000116/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *extension_cache;
118
Tim Peters731098b2003-02-04 20:56:09 +0000119/* For looking up name pairs in copy_reg._extension_registry. */
120static 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,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000127 *copy_reg_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öwis5a395302002-08-04 08:20:23 +0000342 int nesting;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000343 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000344 char *write_buf;
345 int buf_size;
346 PyObject *dispatch_table;
347 int fast_container; /* count nested container dumps */
348 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000349} Picklerobject;
350
Barry Warsaw52acb492001-12-21 20:04:22 +0000351#ifndef PY_CPICKLE_FAST_LIMIT
352#define PY_CPICKLE_FAST_LIMIT 50
353#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000354
Jeremy Hylton938ace62002-07-17 16:30:39 +0000355static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000356
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000357typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000358 PyObject_HEAD
359 FILE *fp;
360 PyObject *file;
361 PyObject *readline;
362 PyObject *read;
363 PyObject *memo;
364 PyObject *arg;
365 Pdata *stack;
366 PyObject *mark;
367 PyObject *pers_func;
368 PyObject *last_string;
369 int *marks;
370 int num_marks;
371 int marks_size;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000372 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
373 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000374 int buf_size;
375 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000376 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000377} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000378
Jeremy Hylton938ace62002-07-17 16:30:39 +0000379static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000380
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000381/* Forward decls that need the above structs */
382static int save(Picklerobject *, PyObject *, int);
383static int put2(Picklerobject *, PyObject *);
384
Guido van Rossumd385d591997-04-09 17:47:47 +0000385static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000386PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000387cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
388{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000389 va_list va;
390 PyObject *args=0, *retval=0;
391 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000392
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000393 if (format) args = Py_VaBuildValue(format, va);
394 va_end(va);
395 if (format && ! args) return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000396 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000397 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000399 if (retval) {
400 if (args) {
401 PyObject *v;
402 v=PyString_Format(retval, args);
403 Py_DECREF(retval);
404 Py_DECREF(args);
405 if (! v) return NULL;
406 retval=v;
407 }
408 }
409 else
410 if (args) retval=args;
411 else {
412 PyErr_SetObject(ErrType,Py_None);
413 return NULL;
414 }
415 PyErr_SetObject(ErrType,retval);
416 Py_DECREF(retval);
417 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000418}
419
Tim Peters84e87f32001-03-17 04:50:51 +0000420static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000421write_file(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000422{
423 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000425 if (s == NULL) {
426 return 0;
427 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000428
Martin v. Löwis18e16552006-02-15 17:27:45 +0000429 if (n > INT_MAX) {
430 /* String too large */
431 return -1;
432 }
433
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000434 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000435 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000436 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000437 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000438 PyFile_DecUseCount((PyFileObject *)self->file);
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000439 if (nbyteswritten != (size_t)n) {
440 PyErr_SetFromErrno(PyExc_IOError);
441 return -1;
442 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000443
Martin v. Löwis18e16552006-02-15 17:27:45 +0000444 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000445}
446
Tim Peters84e87f32001-03-17 04:50:51 +0000447static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000448write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000449{
450 if (s == NULL) {
451 return 0;
452 }
Tim Peterscba30e22003-02-01 06:24:36 +0000453
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000454 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
455 return -1;
456 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000457
Martin v. Löwis18e16552006-02-15 17:27:45 +0000458 return (int)n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000459}
460
Tim Peters84e87f32001-03-17 04:50:51 +0000461static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000462write_none(Picklerobject *self, const char *s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000463{
464 if (s == NULL) return 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000465 if (n > INT_MAX) return -1;
466 return (int)n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000467}
468
Tim Peters84e87f32001-03-17 04:50:51 +0000469static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000470write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000471{
472 PyObject *py_str = 0, *junk = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000473 int n;
Tim Peterscba30e22003-02-01 06:24:36 +0000474
Martin v. Löwis18e16552006-02-15 17:27:45 +0000475 if (_n > INT_MAX)
476 return -1;
477 n = (int)_n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000478 if (s == NULL) {
479 if (!( self->buf_size )) return 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000480 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000481 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000482 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000483 return -1;
484 }
485 else {
486 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
487 if (write_other(self, NULL, 0) < 0)
488 return -1;
489 }
Tim Peterscba30e22003-02-01 06:24:36 +0000490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000491 if (n > WRITE_BUF_SIZE) {
492 if (!( py_str =
Tim Peterscba30e22003-02-01 06:24:36 +0000493 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000494 return -1;
495 }
496 else {
497 memcpy(self->write_buf + self->buf_size, s, n);
498 self->buf_size += n;
499 return n;
500 }
501 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000503 if (self->write) {
504 /* object with write method */
505 ARG_TUP(self, py_str);
506 if (self->arg) {
507 junk = PyObject_Call(self->write, self->arg, NULL);
508 FREE_ARG_TUP(self);
509 }
510 if (junk) Py_DECREF(junk);
511 else return -1;
512 }
513 else
514 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000515
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000516 self->buf_size = 0;
517 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000518}
519
520
Martin v. Löwis18e16552006-02-15 17:27:45 +0000521static Py_ssize_t
522read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000523{
524 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000525
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000526 if (self->buf_size == 0) {
527 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000528
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000529 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000530 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000531 PyErr_NoMemory();
532 return -1;
533 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000534
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000535 self->buf_size = size;
536 }
537 else if (n > self->buf_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000538 char *newbuf = (char *)realloc(self->buf, n);
539 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000540 PyErr_NoMemory();
541 return -1;
542 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000543 self->buf = newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000544 self->buf_size = n;
545 }
Tim Peters84e87f32001-03-17 04:50:51 +0000546
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000547 PyFile_IncUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000548 Py_BEGIN_ALLOW_THREADS
549 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
550 Py_END_ALLOW_THREADS
Gregory P. Smithc20adf82008-04-07 06:33:21 +0000551 PyFile_DecUseCount((PyFileObject *)self->file);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000552 if (nbytesread != (size_t)n) {
553 if (feof(self->fp)) {
554 PyErr_SetNone(PyExc_EOFError);
555 return -1;
556 }
Tim Peterscba30e22003-02-01 06:24:36 +0000557
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000558 PyErr_SetFromErrno(PyExc_IOError);
559 return -1;
560 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000561
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000562 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000563
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000564 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000565}
566
567
Martin v. Löwis18e16552006-02-15 17:27:45 +0000568static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000569readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000570{
571 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000572
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000573 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000574 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000575 PyErr_NoMemory();
576 return -1;
577 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000578 self->buf_size = 40;
579 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000581 i = 0;
582 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000583 int bigger;
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000584 char *newbuf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000585 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000586 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000587 (self->buf[i] = getc(self->fp)) == '\n') {
588 self->buf[i + 1] = '\0';
589 *s = self->buf;
590 return i + 1;
591 }
592 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000593 bigger = self->buf_size << 1;
594 if (bigger <= 0) { /* overflow */
595 PyErr_NoMemory();
596 return -1;
597 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000598 newbuf = (char *)realloc(self->buf, bigger);
599 if (!newbuf) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000600 PyErr_NoMemory();
601 return -1;
602 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +0000603 self->buf = newbuf;
Tim Petersee1a53c2003-02-02 02:57:53 +0000604 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000605 }
Tim Peters84e87f32001-03-17 04:50:51 +0000606}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000607
608
Martin v. Löwis18e16552006-02-15 17:27:45 +0000609static Py_ssize_t
610read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000611{
612 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000614 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
615 PyErr_SetNone(PyExc_EOFError);
616 return -1;
617 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000619 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000620
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000621 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000622}
623
624
Martin v. Löwis18e16552006-02-15 17:27:45 +0000625static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000626readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000627{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000628 Py_ssize_t n;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000629 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000630
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000631 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
632 return -1;
633 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000634
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000635 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000637 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000638}
639
640
Martin v. Löwis18e16552006-02-15 17:27:45 +0000641static Py_ssize_t
642read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000643{
644 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000645
Martin v. Löwis18e16552006-02-15 17:27:45 +0000646 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000648 ARG_TUP(self, bytes);
649 if (self->arg) {
650 str = PyObject_Call(self->read, self->arg, NULL);
651 FREE_ARG_TUP(self);
652 }
653 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000655 Py_XDECREF(self->last_string);
656 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000658 if (! (*s = PyString_AsString(str))) return -1;
659 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000660}
661
662
Martin v. Löwis18e16552006-02-15 17:27:45 +0000663static Py_ssize_t
Tim Peterscba30e22003-02-01 06:24:36 +0000664readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000665{
666 PyObject *str;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000667 Py_ssize_t str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000669 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
670 return -1;
671 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000672
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000673 if ((str_size = PyString_Size(str)) < 0)
674 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000675
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000676 Py_XDECREF(self->last_string);
677 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000678
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000679 if (! (*s = PyString_AsString(str)))
680 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000681
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000682 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000683}
684
Tim Petersee1a53c2003-02-02 02:57:53 +0000685/* Copy the first n bytes from s into newly malloc'ed memory, plus a
686 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
687 * The caller is responsible for free()'ing the return value.
688 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000689static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000690pystrndup(const char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000691{
Tim Petersee1a53c2003-02-02 02:57:53 +0000692 char *r = (char *)malloc(n+1);
693 if (r == NULL)
694 return (char*)PyErr_NoMemory();
695 memcpy(r, s, n);
696 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000697 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000698}
699
700
701static int
Tim Peterscba30e22003-02-01 06:24:36 +0000702get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000703{
704 PyObject *value, *mv;
705 long c_value;
706 char s[30];
707 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000709 if (!( mv = PyDict_GetItem(self->memo, id))) {
710 PyErr_SetObject(PyExc_KeyError, id);
711 return -1;
712 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000713
Tim Peterscba30e22003-02-01 06:24:36 +0000714 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000715 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000716
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000717 if (!( PyInt_Check(value))) {
718 PyErr_SetString(PicklingError, "no int where int expected in memo");
719 return -1;
720 }
721 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000722
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000723 if (!self->bin) {
724 s[0] = GET;
725 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
726 len = strlen(s);
727 }
728 else if (Pdata_Check(self->file)) {
729 if (write_other(self, NULL, 0) < 0) return -1;
730 PDATA_APPEND(self->file, mv, -1);
731 return 0;
732 }
733 else {
734 if (c_value < 256) {
735 s[0] = BINGET;
736 s[1] = (int)(c_value & 0xff);
737 len = 2;
738 }
739 else {
740 s[0] = LONG_BINGET;
741 s[1] = (int)(c_value & 0xff);
742 s[2] = (int)((c_value >> 8) & 0xff);
743 s[3] = (int)((c_value >> 16) & 0xff);
744 s[4] = (int)((c_value >> 24) & 0xff);
745 len = 5;
746 }
747 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000748
Tim Peters0bc93f52003-02-02 18:29:33 +0000749 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000750 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000751
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000752 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000753}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000754
Guido van Rossum60456fd1997-04-09 17:36:32 +0000755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000756static int
Tim Peterscba30e22003-02-01 06:24:36 +0000757put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000758{
Christian Heimese93237d2007-12-19 02:37:44 +0000759 if (Py_REFCNT(ob) < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000760 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000761
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000762 return put2(self, ob);
763}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000764
Guido van Rossum053b8df1998-11-25 16:18:00 +0000765
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000766static int
Tim Peterscba30e22003-02-01 06:24:36 +0000767put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000768{
769 char c_str[30];
770 int p;
771 size_t len;
772 int res = -1;
773 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000775 if (self->fast)
776 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000777
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000778 if ((p = PyDict_Size(self->memo)) < 0)
779 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000780
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000781 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000782 /* XXX Why?
783 * XXX And does "positive" really mean non-negative?
784 * XXX pickle.py starts with PUT index 0, not 1. This makes for
785 * XXX gratuitous differences between the pickling modules.
786 */
Tim Peterscba30e22003-02-01 06:24:36 +0000787 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000788
Tim Peterscba30e22003-02-01 06:24:36 +0000789 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000790 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000791
Tim Peterscba30e22003-02-01 06:24:36 +0000792 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000793 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000794
Tim Peterscba30e22003-02-01 06:24:36 +0000795 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000796 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000797
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000798 PyTuple_SET_ITEM(t, 0, memo_len);
799 Py_INCREF(memo_len);
800 PyTuple_SET_ITEM(t, 1, ob);
801 Py_INCREF(ob);
802
803 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
804 goto finally;
805
806 if (!self->bin) {
807 c_str[0] = PUT;
808 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
809 len = strlen(c_str);
810 }
811 else if (Pdata_Check(self->file)) {
812 if (write_other(self, NULL, 0) < 0) return -1;
813 PDATA_APPEND(self->file, memo_len, -1);
814 res=0; /* Job well done ;) */
815 goto finally;
816 }
817 else {
818 if (p >= 256) {
819 c_str[0] = LONG_BINPUT;
820 c_str[1] = (int)(p & 0xff);
821 c_str[2] = (int)((p >> 8) & 0xff);
822 c_str[3] = (int)((p >> 16) & 0xff);
823 c_str[4] = (int)((p >> 24) & 0xff);
824 len = 5;
825 }
826 else {
827 c_str[0] = BINPUT;
828 c_str[1] = p;
829 len = 2;
830 }
831 }
832
Tim Peters0bc93f52003-02-02 18:29:33 +0000833 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000834 goto finally;
835
836 res = 0;
837
838 finally:
839 Py_XDECREF(py_ob_id);
840 Py_XDECREF(memo_len);
841 Py_XDECREF(t);
842
843 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000844}
845
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000846static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000847whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000848{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000849 Py_ssize_t i, j;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000850 PyObject *module = 0, *modules_dict = 0,
851 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000852
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000853 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000854 if (module)
855 return module;
856 if (PyErr_ExceptionMatches(PyExc_AttributeError))
857 PyErr_Clear();
858 else
859 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000860
Tim Peterscba30e22003-02-01 06:24:36 +0000861 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000862 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000863
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000864 i = 0;
865 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000866
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000867 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000868
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000869 global_name_attr = PyObject_GetAttr(module, global_name);
870 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000871 if (PyErr_ExceptionMatches(PyExc_AttributeError))
872 PyErr_Clear();
873 else
874 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000875 continue;
876 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000877
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000878 if (global_name_attr != global) {
879 Py_DECREF(global_name_attr);
880 continue;
881 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000882
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000883 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000884
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000885 break;
886 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000887
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000888 /* The following implements the rule in pickle.py added in 1.5
889 that used __main__ if no module is found. I don't actually
890 like this rule. jlf
891 */
892 if (!j) {
893 j=1;
894 name=__main___str;
895 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000896
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000897 Py_INCREF(name);
898 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000899}
900
901
Guido van Rossum60456fd1997-04-09 17:36:32 +0000902static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000903fast_save_enter(Picklerobject *self, PyObject *obj)
904{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000905 /* if fast_container < 0, we're doing an error exit. */
906 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
907 PyObject *key = NULL;
908 if (self->fast_memo == NULL) {
909 self->fast_memo = PyDict_New();
910 if (self->fast_memo == NULL) {
911 self->fast_container = -1;
912 return 0;
913 }
914 }
915 key = PyLong_FromVoidPtr(obj);
916 if (key == NULL)
917 return 0;
918 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000919 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000920 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000921 "fast mode: can't pickle cyclic objects "
922 "including object type %s at %p",
Christian Heimese93237d2007-12-19 02:37:44 +0000923 Py_TYPE(obj)->tp_name, obj);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000924 self->fast_container = -1;
925 return 0;
926 }
927 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000928 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000929 self->fast_container = -1;
930 return 0;
931 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000932 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000933 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000934 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000935}
936
Tim Peterscba30e22003-02-01 06:24:36 +0000937int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000938fast_save_leave(Picklerobject *self, PyObject *obj)
939{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000940 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
941 PyObject *key = PyLong_FromVoidPtr(obj);
942 if (key == NULL)
943 return 0;
944 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000945 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000946 return 0;
947 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000948 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000949 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000950 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000951}
952
953static int
Tim Peterscba30e22003-02-01 06:24:36 +0000954save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000955{
956 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000957 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000958 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000959
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000960 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000961}
962
Guido van Rossum77f6a652002-04-03 22:41:51 +0000963static int
Tim Peterscba30e22003-02-01 06:24:36 +0000964save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000965{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000966 static const char *buf[2] = {FALSE, TRUE};
Guido van Rossume2763392002-04-05 19:30:08 +0000967 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000968 long l = PyInt_AS_LONG((PyIntObject *)args);
969
Tim Peters3c67d792003-02-02 17:59:11 +0000970 if (self->proto >= 2) {
971 char opcode = l ? NEWTRUE : NEWFALSE;
972 if (self->write_func(self, &opcode, 1) < 0)
973 return -1;
974 }
975 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000976 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000977 return 0;
978}
Tim Peters84e87f32001-03-17 04:50:51 +0000979
Guido van Rossum60456fd1997-04-09 17:36:32 +0000980static int
Tim Peterscba30e22003-02-01 06:24:36 +0000981save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000982{
983 char c_str[32];
984 long l = PyInt_AS_LONG((PyIntObject *)args);
985 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000986
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000987 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000988#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000989 || l > 0x7fffffffL
990 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000991#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000992 ) {
993 /* Text-mode pickle, or long too big to fit in the 4-byte
994 * signed BININT format: store as a string.
995 */
996 c_str[0] = INT;
997 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000998 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000999 return -1;
1000 }
1001 else {
1002 /* Binary pickle and l fits in a signed 4-byte int. */
1003 c_str[1] = (int)( l & 0xff);
1004 c_str[2] = (int)((l >> 8) & 0xff);
1005 c_str[3] = (int)((l >> 16) & 0xff);
1006 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001007
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001008 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1009 if (c_str[2] == 0) {
1010 c_str[0] = BININT1;
1011 len = 2;
1012 }
1013 else {
1014 c_str[0] = BININT2;
1015 len = 3;
1016 }
1017 }
1018 else {
1019 c_str[0] = BININT;
1020 len = 5;
1021 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001022
Tim Peters0bc93f52003-02-02 18:29:33 +00001023 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001024 return -1;
1025 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001026
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001027 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001028}
1029
1030
1031static int
Tim Peterscba30e22003-02-01 06:24:36 +00001032save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001033{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001034 Py_ssize_t size;
Tim Petersee1a53c2003-02-02 02:57:53 +00001035 int res = -1;
1036 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001037
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001038 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001039
Tim Petersee1a53c2003-02-02 02:57:53 +00001040 if (self->proto >= 2) {
1041 /* Linear-time pickling. */
1042 size_t nbits;
1043 size_t nbytes;
1044 unsigned char *pdata;
1045 char c_str[5];
1046 int i;
1047 int sign = _PyLong_Sign(args);
1048
1049 if (sign == 0) {
1050 /* It's 0 -- an empty bytestring. */
1051 c_str[0] = LONG1;
1052 c_str[1] = 0;
1053 i = self->write_func(self, c_str, 2);
1054 if (i < 0) goto finally;
1055 res = 0;
1056 goto finally;
1057 }
1058 nbits = _PyLong_NumBits(args);
1059 if (nbits == (size_t)-1 && PyErr_Occurred())
1060 goto finally;
1061 /* How many bytes do we need? There are nbits >> 3 full
1062 * bytes of data, and nbits & 7 leftover bits. If there
1063 * are any leftover bits, then we clearly need another
1064 * byte. Wnat's not so obvious is that we *probably*
1065 * need another byte even if there aren't any leftovers:
1066 * the most-significant bit of the most-significant byte
1067 * acts like a sign bit, and it's usually got a sense
1068 * opposite of the one we need. The exception is longs
1069 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1070 * its own 256's-complement, so has the right sign bit
1071 * even without the extra byte. That's a pain to check
1072 * for in advance, though, so we always grab an extra
1073 * byte at the start, and cut it back later if possible.
1074 */
1075 nbytes = (nbits >> 3) + 1;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001076 if (nbytes > INT_MAX) {
Tim Petersee1a53c2003-02-02 02:57:53 +00001077 PyErr_SetString(PyExc_OverflowError, "long too large "
1078 "to pickle");
1079 goto finally;
1080 }
1081 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1082 if (repr == NULL) goto finally;
1083 pdata = (unsigned char *)PyString_AS_STRING(repr);
1084 i = _PyLong_AsByteArray((PyLongObject *)args,
1085 pdata, nbytes,
1086 1 /* little endian */, 1 /* signed */);
1087 if (i < 0) goto finally;
1088 /* If the long is negative, this may be a byte more than
1089 * needed. This is so iff the MSB is all redundant sign
1090 * bits.
1091 */
1092 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1093 (pdata[nbytes - 2] & 0x80) != 0)
1094 --nbytes;
1095
1096 if (nbytes < 256) {
1097 c_str[0] = LONG1;
1098 c_str[1] = (char)nbytes;
1099 size = 2;
1100 }
1101 else {
1102 c_str[0] = LONG4;
1103 size = (int)nbytes;
1104 for (i = 1; i < 5; i++) {
1105 c_str[i] = (char)(size & 0xff);
1106 size >>= 8;
1107 }
1108 size = 5;
1109 }
1110 i = self->write_func(self, c_str, size);
1111 if (i < 0) goto finally;
1112 i = self->write_func(self, (char *)pdata, (int)nbytes);
1113 if (i < 0) goto finally;
1114 res = 0;
1115 goto finally;
1116 }
1117
1118 /* proto < 2: write the repr and newline. This is quadratic-time
1119 * (in the number of digits), in both directions.
1120 */
Tim Peterscba30e22003-02-01 06:24:36 +00001121 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001122 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001123
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001124 if ((size = PyString_Size(repr)) < 0)
1125 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001126
Tim Peters0bc93f52003-02-02 18:29:33 +00001127 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001128 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001129
Tim Peters0bc93f52003-02-02 18:29:33 +00001130 if (self->write_func(self,
1131 PyString_AS_STRING((PyStringObject *)repr),
1132 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001133 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001134
Tim Peters0bc93f52003-02-02 18:29:33 +00001135 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001136 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001138 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001139
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001140 finally:
1141 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001142 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001143}
1144
1145
1146static int
Tim Peterscba30e22003-02-01 06:24:36 +00001147save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001148{
1149 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001151 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001152 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001153 str[0] = BINFLOAT;
1154 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001155 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001156 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001157 return -1;
1158 }
1159 else {
1160 char c_str[250];
1161 c_str[0] = FLOAT;
Georg Brandlde9b6242006-04-30 11:13:56 +00001162 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1163 /* Extend the formatted string with a newline character */
1164 strcat(c_str, "\n");
Guido van Rossum60456fd1997-04-09 17:36:32 +00001165
Tim Peters0bc93f52003-02-02 18:29:33 +00001166 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001167 return -1;
1168 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001169
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001170 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001171}
1172
1173
1174static int
Tim Peterscba30e22003-02-01 06:24:36 +00001175save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001176{
1177 int size, len;
1178 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001179
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001180 if ((size = PyString_Size(args)) < 0)
1181 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001182
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001183 if (!self->bin) {
1184 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001186 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001187
Tim Peterscba30e22003-02-01 06:24:36 +00001188 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001189 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001190
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001191 if ((len = PyString_Size(repr)) < 0)
1192 goto err;
1193 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001194
Tim Peters0bc93f52003-02-02 18:29:33 +00001195 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001196 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001197
Tim Peters0bc93f52003-02-02 18:29:33 +00001198 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001199 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001200
Tim Peters0bc93f52003-02-02 18:29:33 +00001201 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001202 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001203
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001204 Py_XDECREF(repr);
1205 }
1206 else {
1207 int i;
1208 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001209
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001210 if ((size = PyString_Size(args)) < 0)
1211 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001212
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001213 if (size < 256) {
1214 c_str[0] = SHORT_BINSTRING;
1215 c_str[1] = size;
1216 len = 2;
1217 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001218 else if (size <= INT_MAX) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001219 c_str[0] = BINSTRING;
1220 for (i = 1; i < 5; i++)
1221 c_str[i] = (int)(size >> ((i - 1) * 8));
1222 len = 5;
1223 }
Armin Rigo7ccbca92006-10-04 12:17:45 +00001224 else
1225 return -1; /* string too large */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001226
Tim Peters0bc93f52003-02-02 18:29:33 +00001227 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001228 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001229
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001230 if (size > 128 && Pdata_Check(self->file)) {
1231 if (write_other(self, NULL, 0) < 0) return -1;
1232 PDATA_APPEND(self->file, args, -1);
1233 }
1234 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001235 if (self->write_func(self,
1236 PyString_AS_STRING(
1237 (PyStringObject *)args),
1238 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001239 return -1;
1240 }
1241 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001242
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001243 if (doput)
1244 if (put(self, args) < 0)
1245 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001246
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001247 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001248
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001249 err:
1250 Py_XDECREF(repr);
1251 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001252}
1253
1254
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001255#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001256/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1257 backslash and newline characters to \uXXXX escapes. */
1258static PyObject *
1259modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1260{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001261 PyObject *repr;
1262 char *p;
1263 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001264
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001265 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001266
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001267 repr = PyString_FromStringAndSize(NULL, 6 * size);
1268 if (repr == NULL)
1269 return NULL;
1270 if (size == 0)
1271 return repr;
1272
1273 p = q = PyString_AS_STRING(repr);
1274 while (size-- > 0) {
1275 Py_UNICODE ch = *s++;
1276 /* Map 16-bit characters to '\uxxxx' */
1277 if (ch >= 256 || ch == '\\' || ch == '\n') {
1278 *p++ = '\\';
1279 *p++ = 'u';
1280 *p++ = hexdigit[(ch >> 12) & 0xf];
1281 *p++ = hexdigit[(ch >> 8) & 0xf];
1282 *p++ = hexdigit[(ch >> 4) & 0xf];
1283 *p++ = hexdigit[ch & 15];
1284 }
1285 /* Copy everything else as-is */
1286 else
1287 *p++ = (char) ch;
1288 }
1289 *p = '\0';
Tim Peters5de98422002-04-27 18:44:32 +00001290 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001291 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001292}
1293
1294
Guido van Rossum60456fd1997-04-09 17:36:32 +00001295static int
Tim Peterscba30e22003-02-01 06:24:36 +00001296save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001297{
Armin Rigo7ccbca92006-10-04 12:17:45 +00001298 Py_ssize_t size, len;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001299 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001301 if (!PyUnicode_Check(args))
1302 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001303
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001304 if (!self->bin) {
1305 char *repr_str;
1306 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001307
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001308 repr = modified_EncodeRawUnicodeEscape(
1309 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001310 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001311 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001312
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001313 if ((len = PyString_Size(repr)) < 0)
1314 goto err;
1315 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001316
Tim Peters0bc93f52003-02-02 18:29:33 +00001317 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001318 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001319
Tim Peters0bc93f52003-02-02 18:29:33 +00001320 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001321 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001322
Tim Peters0bc93f52003-02-02 18:29:33 +00001323 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001324 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001326 Py_XDECREF(repr);
1327 }
1328 else {
1329 int i;
1330 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001331
Tim Peterscba30e22003-02-01 06:24:36 +00001332 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001333 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001334
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001335 if ((size = PyString_Size(repr)) < 0)
1336 goto err;
Armin Rigo7ccbca92006-10-04 12:17:45 +00001337 if (size > INT_MAX)
1338 return -1; /* string too large */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001339
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001340 c_str[0] = BINUNICODE;
1341 for (i = 1; i < 5; i++)
1342 c_str[i] = (int)(size >> ((i - 1) * 8));
1343 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001344
Tim Peters0bc93f52003-02-02 18:29:33 +00001345 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001346 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001347
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001348 if (size > 128 && Pdata_Check(self->file)) {
1349 if (write_other(self, NULL, 0) < 0)
1350 goto err;
1351 PDATA_APPEND(self->file, repr, -1);
1352 }
1353 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001354 if (self->write_func(self, PyString_AS_STRING(repr),
1355 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001356 goto err;
1357 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001358
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001359 Py_DECREF(repr);
1360 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001361
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001362 if (doput)
1363 if (put(self, args) < 0)
1364 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001365
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001366 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001367
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001368 err:
1369 Py_XDECREF(repr);
1370 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001371}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001372#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001373
Tim Peters1d63c9f2003-02-02 20:29:39 +00001374/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1375static int
Tim Peters67920142003-02-05 03:46:17 +00001376store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001377{
1378 int i;
1379 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001380
Tim Peters1d63c9f2003-02-02 20:29:39 +00001381 assert(PyTuple_Size(t) == len);
1382
1383 for (i = 0; i < len; i++) {
1384 PyObject *element = PyTuple_GET_ITEM(t, i);
1385
1386 if (element == NULL)
1387 goto finally;
1388 if (save(self, element, 0) < 0)
1389 goto finally;
1390 }
1391 res = 0;
1392
1393 finally:
1394 return res;
1395}
1396
1397/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1398 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001399 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001400 * (a tuple can be reached from itself), and that requires some subtle
1401 * magic so that it works in all cases. IOW, this is a long routine.
1402 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001403static int
Tim Peterscba30e22003-02-01 06:24:36 +00001404save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001405{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001406 PyObject *py_tuple_id = NULL;
1407 int len, i;
1408 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001409
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001410 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001411 static char pop = POP;
1412 static char pop_mark = POP_MARK;
1413 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001414
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001415 if ((len = PyTuple_Size(args)) < 0)
1416 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001417
Tim Peters1d63c9f2003-02-02 20:29:39 +00001418 if (len == 0) {
1419 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001420
Tim Peters1d63c9f2003-02-02 20:29:39 +00001421 if (self->proto) {
1422 c_str[0] = EMPTY_TUPLE;
1423 len = 1;
1424 }
1425 else {
1426 c_str[0] = MARK;
1427 c_str[1] = TUPLE;
1428 len = 2;
1429 }
1430 if (self->write_func(self, c_str, len) >= 0)
1431 res = 0;
1432 /* Don't memoize an empty tuple. */
1433 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001434 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001435
Tim Peters1d63c9f2003-02-02 20:29:39 +00001436 /* A non-empty tuple. */
1437
1438 /* id(tuple) isn't in the memo now. If it shows up there after
1439 * saving the tuple elements, the tuple must be recursive, in
1440 * which case we'll pop everything we put on the stack, and fetch
1441 * its value from the memo.
1442 */
1443 py_tuple_id = PyLong_FromVoidPtr(args);
1444 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001445 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001446
Tim Peters1d63c9f2003-02-02 20:29:39 +00001447 if (len <= 3 && self->proto >= 2) {
1448 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001449 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001450 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001451 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001452 /* pop the len elements */
1453 for (i = 0; i < len; ++i)
1454 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001455 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001456 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001457 if (get(self, py_tuple_id) < 0)
1458 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001459 res = 0;
1460 goto finally;
1461 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001462 /* Not recursive. */
1463 if (self->write_func(self, len2opcode + len, 1) < 0)
1464 goto finally;
1465 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001466 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001467
Tim Peters1d63c9f2003-02-02 20:29:39 +00001468 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1469 * Generate MARK elt1 elt2 ... TUPLE
1470 */
1471 if (self->write_func(self, &MARKv, 1) < 0)
1472 goto finally;
1473
Tim Peters67920142003-02-05 03:46:17 +00001474 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001475 goto finally;
1476
1477 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1478 /* pop the stack stuff we pushed */
1479 if (self->bin) {
1480 if (self->write_func(self, &pop_mark, 1) < 0)
1481 goto finally;
1482 }
1483 else {
1484 /* Note that we pop one more than len, to remove
1485 * the MARK too.
1486 */
1487 for (i = 0; i <= len; i++)
1488 if (self->write_func(self, &pop, 1) < 0)
1489 goto finally;
1490 }
1491 /* fetch from memo */
1492 if (get(self, py_tuple_id) >= 0)
1493 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001494 goto finally;
1495 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001496
Tim Peters1d63c9f2003-02-02 20:29:39 +00001497 /* Not recursive. */
1498 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001499 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001500
Tim Peters1d63c9f2003-02-02 20:29:39 +00001501 memoize:
1502 if (put(self, args) >= 0)
1503 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001504
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001505 finally:
1506 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001507 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001508}
1509
Tim Peters1092d642003-02-11 21:06:20 +00001510/* iter is an iterator giving items, and we batch up chunks of
1511 * MARK item item ... item APPENDS
1512 * opcode sequences. Calling code should have arranged to first create an
1513 * empty list, or list-like object, for the APPENDS to operate on.
1514 * Returns 0 on success, <0 on error.
1515 */
1516static int
1517batch_list(Picklerobject *self, PyObject *iter)
1518{
1519 PyObject *obj;
1520 PyObject *slice[BATCHSIZE];
1521 int i, n;
1522
1523 static char append = APPEND;
1524 static char appends = APPENDS;
1525
1526 assert(iter != NULL);
1527
1528 if (self->proto == 0) {
1529 /* APPENDS isn't available; do one at a time. */
1530 for (;;) {
1531 obj = PyIter_Next(iter);
1532 if (obj == NULL) {
1533 if (PyErr_Occurred())
1534 return -1;
1535 break;
1536 }
1537 i = save(self, obj, 0);
1538 Py_DECREF(obj);
1539 if (i < 0)
1540 return -1;
1541 if (self->write_func(self, &append, 1) < 0)
1542 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001543 }
1544 return 0;
1545 }
1546
1547 /* proto > 0: write in batches of BATCHSIZE. */
1548 do {
1549 /* Get next group of (no more than) BATCHSIZE elements. */
1550 for (n = 0; n < BATCHSIZE; ++n) {
1551 obj = PyIter_Next(iter);
1552 if (obj == NULL) {
1553 if (PyErr_Occurred())
1554 goto BatchFailed;
1555 break;
1556 }
1557 slice[n] = obj;
1558 }
1559
1560 if (n > 1) {
1561 /* Pump out MARK, slice[0:n], APPENDS. */
1562 if (self->write_func(self, &MARKv, 1) < 0)
1563 goto BatchFailed;
1564 for (i = 0; i < n; ++i) {
1565 if (save(self, slice[i], 0) < 0)
1566 goto BatchFailed;
1567 }
1568 if (self->write_func(self, &appends, 1) < 0)
1569 goto BatchFailed;
1570 }
1571 else if (n == 1) {
1572 if (save(self, slice[0], 0) < 0)
1573 goto BatchFailed;
1574 if (self->write_func(self, &append, 1) < 0)
1575 goto BatchFailed;
1576 }
1577
1578 for (i = 0; i < n; ++i) {
1579 Py_DECREF(slice[i]);
1580 }
Tim Peters90975f12003-02-12 05:28:58 +00001581 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001582 return 0;
1583
1584BatchFailed:
1585 while (--n >= 0) {
1586 Py_DECREF(slice[n]);
1587 }
1588 return -1;
1589}
1590
Guido van Rossum60456fd1997-04-09 17:36:32 +00001591static int
Tim Peterscba30e22003-02-01 06:24:36 +00001592save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001593{
Tim Peters1092d642003-02-11 21:06:20 +00001594 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001595 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001596 int len;
1597 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001598
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001599 if (self->fast && !fast_save_enter(self, args))
1600 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001601
Tim Peters1092d642003-02-11 21:06:20 +00001602 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001603 if (self->bin) {
1604 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001605 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001606 }
1607 else {
1608 s[0] = MARK;
1609 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001610 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001611 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001612
Tim Peters1092d642003-02-11 21:06:20 +00001613 if (self->write_func(self, s, len) < 0)
1614 goto finally;
1615
1616 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001617 if ((len = PyList_Size(args)) < 0)
1618 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001619
Tim Peters1092d642003-02-11 21:06:20 +00001620 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001621 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001622 if (put(self, args) >= 0)
1623 res = 0;
1624 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001625 }
Tim Peters90975f12003-02-12 05:28:58 +00001626 if (put2(self, args) < 0)
1627 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001628
Tim Peters1092d642003-02-11 21:06:20 +00001629 /* Materialize the list elements. */
1630 iter = PyObject_GetIter(args);
1631 if (iter == NULL)
1632 goto finally;
1633 res = batch_list(self, iter);
1634 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001635
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001636 finally:
1637 if (self->fast && !fast_save_leave(self, args))
1638 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001640 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001641}
1642
1643
Tim Peters42f08ac2003-02-11 22:43:24 +00001644/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1645 * MARK key value ... key value SETITEMS
1646 * opcode sequences. Calling code should have arranged to first create an
1647 * empty dict, or dict-like object, for the SETITEMS to operate on.
1648 * Returns 0 on success, <0 on error.
1649 *
1650 * This is very much like batch_list(). The difference between saving
1651 * elements directly, and picking apart two-tuples, is so long-winded at
1652 * the C level, though, that attempts to combine these routines were too
1653 * ugly to bear.
1654 */
1655static int
1656batch_dict(Picklerobject *self, PyObject *iter)
1657{
1658 PyObject *p;
1659 PyObject *slice[BATCHSIZE];
1660 int i, n;
1661
1662 static char setitem = SETITEM;
1663 static char setitems = SETITEMS;
1664
1665 assert(iter != NULL);
1666
1667 if (self->proto == 0) {
1668 /* SETITEMS isn't available; do one at a time. */
1669 for (;;) {
1670 p = PyIter_Next(iter);
1671 if (p == NULL) {
1672 if (PyErr_Occurred())
1673 return -1;
1674 break;
1675 }
1676 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1677 PyErr_SetString(PyExc_TypeError, "dict items "
1678 "iterator must return 2-tuples");
1679 return -1;
1680 }
1681 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1682 if (i >= 0)
1683 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1684 Py_DECREF(p);
1685 if (i < 0)
1686 return -1;
1687 if (self->write_func(self, &setitem, 1) < 0)
1688 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001689 }
1690 return 0;
1691 }
1692
1693 /* proto > 0: write in batches of BATCHSIZE. */
1694 do {
1695 /* Get next group of (no more than) BATCHSIZE elements. */
1696 for (n = 0; n < BATCHSIZE; ++n) {
1697 p = PyIter_Next(iter);
1698 if (p == NULL) {
1699 if (PyErr_Occurred())
1700 goto BatchFailed;
1701 break;
1702 }
1703 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1704 PyErr_SetString(PyExc_TypeError, "dict items "
1705 "iterator must return 2-tuples");
1706 goto BatchFailed;
1707 }
1708 slice[n] = p;
1709 }
1710
1711 if (n > 1) {
1712 /* Pump out MARK, slice[0:n], SETITEMS. */
1713 if (self->write_func(self, &MARKv, 1) < 0)
1714 goto BatchFailed;
1715 for (i = 0; i < n; ++i) {
1716 p = slice[i];
1717 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1718 goto BatchFailed;
1719 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1720 goto BatchFailed;
1721 }
1722 if (self->write_func(self, &setitems, 1) < 0)
1723 goto BatchFailed;
1724 }
1725 else if (n == 1) {
1726 p = slice[0];
1727 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1728 goto BatchFailed;
1729 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1730 goto BatchFailed;
1731 if (self->write_func(self, &setitem, 1) < 0)
1732 goto BatchFailed;
1733 }
1734
1735 for (i = 0; i < n; ++i) {
1736 Py_DECREF(slice[i]);
1737 }
Tim Peters90975f12003-02-12 05:28:58 +00001738 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001739 return 0;
1740
1741BatchFailed:
1742 while (--n >= 0) {
1743 Py_DECREF(slice[n]);
1744 }
1745 return -1;
1746}
1747
Guido van Rossum60456fd1997-04-09 17:36:32 +00001748static int
Tim Peterscba30e22003-02-01 06:24:36 +00001749save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001750{
Tim Peters42f08ac2003-02-11 22:43:24 +00001751 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001752 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001753 int len;
1754 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001756 if (self->fast && !fast_save_enter(self, args))
1757 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001758
Tim Peters42f08ac2003-02-11 22:43:24 +00001759 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001760 if (self->bin) {
1761 s[0] = EMPTY_DICT;
1762 len = 1;
1763 }
1764 else {
1765 s[0] = MARK;
1766 s[1] = DICT;
1767 len = 2;
1768 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001769
Tim Peters0bc93f52003-02-02 18:29:33 +00001770 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001771 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001772
Tim Peters42f08ac2003-02-11 22:43:24 +00001773 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001774 if ((len = PyDict_Size(args)) < 0)
1775 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001777 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001778 if (put(self, args) >= 0)
1779 res = 0;
1780 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001781 }
Tim Peters90975f12003-02-12 05:28:58 +00001782 if (put2(self, args) < 0)
1783 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001784
Tim Peters42f08ac2003-02-11 22:43:24 +00001785 /* Materialize the dict items. */
1786 iter = PyObject_CallMethod(args, "iteritems", "()");
1787 if (iter == NULL)
1788 goto finally;
1789 res = batch_dict(self, iter);
1790 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001791
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001792 finally:
1793 if (self->fast && !fast_save_leave(self, args))
1794 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001795
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001796 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001797}
1798
1799
Tim Peters84e87f32001-03-17 04:50:51 +00001800static int
Tim Peterscba30e22003-02-01 06:24:36 +00001801save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001802{
1803 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1804 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1805 char *module_str, *name_str;
1806 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001807
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001808 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001809
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001810 if (self->fast && !fast_save_enter(self, args))
1811 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001812
Tim Peters0bc93f52003-02-02 18:29:33 +00001813 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001814 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001815
Tim Peterscba30e22003-02-01 06:24:36 +00001816 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001817 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001818
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001819 if (self->bin) {
1820 if (save(self, class, 0) < 0)
1821 goto finally;
1822 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001823
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001824 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1825 PyObject *element = 0;
1826 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001827
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001828 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001829 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001830 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001831
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001832 if ((len = PyObject_Size(class_args)) < 0)
1833 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001834
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001835 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001836 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001837 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001838
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001839 if (save(self, element, 0) < 0) {
1840 Py_DECREF(element);
1841 goto finally;
1842 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001843
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001844 Py_DECREF(element);
1845 }
1846 }
1847 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001848 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1849 PyErr_Clear();
1850 else
1851 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001852 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001853
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001854 if (!self->bin) {
1855 if (!( name = ((PyClassObject *)class)->cl_name )) {
1856 PyErr_SetString(PicklingError, "class has no name");
1857 goto finally;
1858 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001859
Tim Peterscba30e22003-02-01 06:24:36 +00001860 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001861 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001862
Tim Peters84e87f32001-03-17 04:50:51 +00001863
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001864 if ((module_size = PyString_Size(module)) < 0 ||
1865 (name_size = PyString_Size(name)) < 0)
1866 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001867
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001868 module_str = PyString_AS_STRING((PyStringObject *)module);
1869 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001870
Tim Peters0bc93f52003-02-02 18:29:33 +00001871 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001872 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001873
Tim Peters0bc93f52003-02-02 18:29:33 +00001874 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001875 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001876
Tim Peters0bc93f52003-02-02 18:29:33 +00001877 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001878 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001879
Tim Peters0bc93f52003-02-02 18:29:33 +00001880 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001881 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001882
Tim Peters0bc93f52003-02-02 18:29:33 +00001883 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001884 goto finally;
1885 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001886 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001887 goto finally;
1888 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001889
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001890 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1891 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001892 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001893 goto finally;
1894 }
1895 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001896 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1897 PyErr_Clear();
1898 else
1899 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001900
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001901 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001902 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1903 PyErr_Clear();
1904 else
1905 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001906 res = 0;
1907 goto finally;
1908 }
1909 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001910
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001911 if (!PyDict_Check(state)) {
1912 if (put2(self, args) < 0)
1913 goto finally;
1914 }
1915 else {
1916 if (put(self, args) < 0)
1917 goto finally;
1918 }
Tim Peters84e87f32001-03-17 04:50:51 +00001919
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001920 if (save(self, state, 0) < 0)
1921 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001922
Tim Peters0bc93f52003-02-02 18:29:33 +00001923 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001924 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001925
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001926 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001927
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001928 finally:
1929 if (self->fast && !fast_save_leave(self, args))
1930 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001931
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001932 Py_XDECREF(module);
1933 Py_XDECREF(class);
1934 Py_XDECREF(state);
1935 Py_XDECREF(getinitargs_func);
1936 Py_XDECREF(getstate_func);
1937 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001938
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001939 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001940}
1941
1942
Guido van Rossum60456fd1997-04-09 17:36:32 +00001943static int
Tim Peterscba30e22003-02-01 06:24:36 +00001944save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001945{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001946 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001947 char *name_str, *module_str;
1948 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001949
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001950 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001951
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001952 if (name) {
1953 global_name = name;
1954 Py_INCREF(global_name);
1955 }
1956 else {
Tim Peterscba30e22003-02-01 06:24:36 +00001957 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001958 goto finally;
1959 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001960
Tim Peterscba30e22003-02-01 06:24:36 +00001961 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001962 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001963
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001964 if ((module_size = PyString_Size(module)) < 0 ||
1965 (name_size = PyString_Size(global_name)) < 0)
1966 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001967
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001968 module_str = PyString_AS_STRING((PyStringObject *)module);
1969 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001970
Guido van Rossum75bfd052002-12-24 18:10:07 +00001971 /* XXX This can be doing a relative import. Clearly it shouldn't,
1972 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001973 mod = PyImport_ImportModule(module_str);
1974 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001975 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001976 "Can't pickle %s: import of module %s "
1977 "failed",
1978 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001979 goto finally;
1980 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001981 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001982 if (klass == NULL) {
1983 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001984 "Can't pickle %s: attribute lookup %s.%s "
1985 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001986 "OSS", args, module, global_name);
1987 goto finally;
1988 }
1989 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001990 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001991 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00001992 "Can't pickle %s: it's not the same object "
1993 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001994 "OSS", args, module, global_name);
1995 goto finally;
1996 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001997 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001998
Tim Peters731098b2003-02-04 20:56:09 +00001999 if (self->proto >= 2) {
2000 /* See whether this is in the extension registry, and if
2001 * so generate an EXT opcode.
2002 */
2003 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002004 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002005 char c_str[5];
2006 int n;
2007
2008 PyTuple_SET_ITEM(two_tuple, 0, module);
2009 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2010 py_code = PyDict_GetItem(extension_registry, two_tuple);
2011 if (py_code == NULL)
2012 goto gen_global; /* not registered */
2013
2014 /* Verify py_code has the right type and value. */
2015 if (!PyInt_Check(py_code)) {
2016 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002017 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002018 "OO", args, py_code);
2019 goto finally;
2020 }
2021 code = PyInt_AS_LONG(py_code);
2022 if (code <= 0 || code > 0x7fffffffL) {
2023 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2024 "extension code %ld is out of range",
2025 "Ol", args, code);
2026 goto finally;
2027 }
2028
2029 /* Generate an EXT opcode. */
2030 if (code <= 0xff) {
2031 c_str[0] = EXT1;
2032 c_str[1] = (char)code;
2033 n = 2;
2034 }
2035 else if (code <= 0xffff) {
2036 c_str[0] = EXT2;
2037 c_str[1] = (char)(code & 0xff);
2038 c_str[2] = (char)((code >> 8) & 0xff);
2039 n = 3;
2040 }
2041 else {
2042 c_str[0] = EXT4;
2043 c_str[1] = (char)(code & 0xff);
2044 c_str[2] = (char)((code >> 8) & 0xff);
2045 c_str[3] = (char)((code >> 16) & 0xff);
2046 c_str[4] = (char)((code >> 24) & 0xff);
2047 n = 5;
2048 }
2049
2050 if (self->write_func(self, c_str, n) >= 0)
2051 res = 0;
2052 goto finally; /* and don't memoize */
2053 }
2054
2055 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002056 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002057 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002058
Tim Peters0bc93f52003-02-02 18:29:33 +00002059 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002060 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002061
Tim Peters0bc93f52003-02-02 18:29:33 +00002062 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002063 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002064
Tim Peters0bc93f52003-02-02 18:29:33 +00002065 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002066 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002067
Tim Peters0bc93f52003-02-02 18:29:33 +00002068 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002069 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002070
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002071 if (put(self, args) < 0)
2072 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002073
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002074 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002075
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002076 finally:
2077 Py_XDECREF(module);
2078 Py_XDECREF(global_name);
2079 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002080
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002081 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002082}
2083
Guido van Rossum60456fd1997-04-09 17:36:32 +00002084static int
Tim Peterscba30e22003-02-01 06:24:36 +00002085save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002086{
2087 PyObject *pid = 0;
2088 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002089
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002090 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002091
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002092 Py_INCREF(args);
2093 ARG_TUP(self, args);
2094 if (self->arg) {
2095 pid = PyObject_Call(f, self->arg, NULL);
2096 FREE_ARG_TUP(self);
2097 }
2098 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002099
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002100 if (pid != Py_None) {
2101 if (!self->bin) {
2102 if (!PyString_Check(pid)) {
2103 PyErr_SetString(PicklingError,
2104 "persistent id must be string");
2105 goto finally;
2106 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002107
Tim Peters0bc93f52003-02-02 18:29:33 +00002108 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002109 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002110
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002111 if ((size = PyString_Size(pid)) < 0)
2112 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002113
Tim Peters0bc93f52003-02-02 18:29:33 +00002114 if (self->write_func(self,
2115 PyString_AS_STRING(
2116 (PyStringObject *)pid),
2117 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002118 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002119
Tim Peters0bc93f52003-02-02 18:29:33 +00002120 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002121 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002122
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002123 res = 1;
2124 goto finally;
2125 }
2126 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002127 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002128 res = -1;
2129 else
2130 res = 1;
2131 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002132
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002133 goto finally;
2134 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002136 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002138 finally:
2139 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002140
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002141 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002142}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002143
Tim Peters71fcda52003-02-14 23:05:28 +00002144/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2145 * appropriate __reduce__ method for ob.
2146 */
Tim Peters84e87f32001-03-17 04:50:51 +00002147static int
Tim Peters71fcda52003-02-14 23:05:28 +00002148save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002149{
Tim Peters71fcda52003-02-14 23:05:28 +00002150 PyObject *callable;
2151 PyObject *argtup;
2152 PyObject *state = NULL;
2153 PyObject *listitems = NULL;
2154 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002155
Tim Peters71fcda52003-02-14 23:05:28 +00002156 int use_newobj = self->proto >= 2;
2157
2158 static char reduce = REDUCE;
2159 static char build = BUILD;
2160 static char newobj = NEWOBJ;
2161
2162 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2163 &callable,
2164 &argtup,
2165 &state,
2166 &listitems,
2167 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002168 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002169
Raymond Hettingera6b45cc2004-12-07 07:05:57 +00002170 if (!PyTuple_Check(argtup)) {
2171 PyErr_SetString(PicklingError,
2172 "args from reduce() should be a tuple");
2173 return -1;
2174 }
2175
Tim Peters71fcda52003-02-14 23:05:28 +00002176 if (state == Py_None)
2177 state = NULL;
2178 if (listitems == Py_None)
2179 listitems = NULL;
2180 if (dictitems == Py_None)
2181 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002182
Tim Peters71fcda52003-02-14 23:05:28 +00002183 /* Protocol 2 special case: if callable's name is __newobj__, use
2184 * NEWOBJ. This consumes a lot of code.
2185 */
2186 if (use_newobj) {
2187 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002188
Tim Peters71fcda52003-02-14 23:05:28 +00002189 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002190 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2191 PyErr_Clear();
2192 else
2193 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002194 use_newobj = 0;
2195 }
2196 else {
2197 use_newobj = PyString_Check(temp) &&
2198 strcmp(PyString_AS_STRING(temp),
2199 "__newobj__") == 0;
2200 Py_DECREF(temp);
2201 }
2202 }
2203 if (use_newobj) {
2204 PyObject *cls;
2205 PyObject *newargtup;
2206 int n, i;
2207
2208 /* Sanity checks. */
2209 n = PyTuple_Size(argtup);
2210 if (n < 1) {
2211 PyErr_SetString(PicklingError, "__newobj__ arglist "
2212 "is empty");
2213 return -1;
2214 }
2215
2216 cls = PyTuple_GET_ITEM(argtup, 0);
2217 if (! PyObject_HasAttrString(cls, "__new__")) {
2218 PyErr_SetString(PicklingError, "args[0] from "
2219 "__newobj__ args has no __new__");
2220 return -1;
2221 }
2222
2223 /* XXX How could ob be NULL? */
2224 if (ob != NULL) {
2225 PyObject *ob_dot_class;
2226
2227 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002228 if (ob_dot_class == NULL) {
2229 if (PyErr_ExceptionMatches(
2230 PyExc_AttributeError))
2231 PyErr_Clear();
2232 else
2233 return -1;
2234 }
Tim Peters71fcda52003-02-14 23:05:28 +00002235 i = ob_dot_class != cls; /* true iff a problem */
2236 Py_XDECREF(ob_dot_class);
2237 if (i) {
2238 PyErr_SetString(PicklingError, "args[0] from "
2239 "__newobj__ args has the wrong class");
2240 return -1;
2241 }
2242 }
2243
2244 /* Save the class and its __new__ arguments. */
2245 if (save(self, cls, 0) < 0)
2246 return -1;
2247
2248 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2249 if (newargtup == NULL)
2250 return -1;
2251 for (i = 1; i < n; ++i) {
2252 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2253 Py_INCREF(temp);
2254 PyTuple_SET_ITEM(newargtup, i-1, temp);
2255 }
Neal Norwitz5a29dd32007-10-05 05:01:38 +00002256 i = save(self, newargtup, 0);
Tim Peters71fcda52003-02-14 23:05:28 +00002257 Py_DECREF(newargtup);
2258 if (i < 0)
2259 return -1;
2260
2261 /* Add NEWOBJ opcode. */
2262 if (self->write_func(self, &newobj, 1) < 0)
2263 return -1;
2264 }
2265 else {
2266 /* Not using NEWOBJ. */
2267 if (save(self, callable, 0) < 0 ||
2268 save(self, argtup, 0) < 0 ||
2269 self->write_func(self, &reduce, 1) < 0)
2270 return -1;
2271 }
2272
2273 /* Memoize. */
2274 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002275 if (ob != NULL) {
2276 if (state && !PyDict_Check(state)) {
2277 if (put2(self, ob) < 0)
2278 return -1;
2279 }
Tim Peters71fcda52003-02-14 23:05:28 +00002280 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002281 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002282 }
Tim Peters84e87f32001-03-17 04:50:51 +00002283
Guido van Rossum60456fd1997-04-09 17:36:32 +00002284
Tim Peters71fcda52003-02-14 23:05:28 +00002285 if (listitems && batch_list(self, listitems) < 0)
2286 return -1;
2287
2288 if (dictitems && batch_dict(self, dictitems) < 0)
2289 return -1;
2290
2291 if (state) {
2292 if (save(self, state, 0) < 0 ||
2293 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002294 return -1;
2295 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002297 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002298}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002299
Guido van Rossum60456fd1997-04-09 17:36:32 +00002300static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002301save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002302{
2303 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002304 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2305 PyObject *arg_tup;
2306 int res = -1;
2307 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002308
Martin v. Löwis5a395302002-08-04 08:20:23 +00002309 if (self->nesting++ > Py_GetRecursionLimit()){
2310 PyErr_SetString(PyExc_RuntimeError,
2311 "maximum recursion depth exceeded");
2312 goto finally;
2313 }
2314
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002315 if (!pers_save && self->pers_func) {
2316 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2317 res = tmp;
2318 goto finally;
2319 }
2320 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002321
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002322 if (args == Py_None) {
2323 res = save_none(self, args);
2324 goto finally;
2325 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002326
Christian Heimese93237d2007-12-19 02:37:44 +00002327 type = Py_TYPE(args);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002328
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002329 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002330 case 'b':
2331 if (args == Py_False || args == Py_True) {
2332 res = save_bool(self, args);
2333 goto finally;
2334 }
2335 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002336 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002337 if (type == &PyInt_Type) {
2338 res = save_int(self, args);
2339 goto finally;
2340 }
2341 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002342
Guido van Rossum60456fd1997-04-09 17:36:32 +00002343 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002344 if (type == &PyLong_Type) {
2345 res = save_long(self, args);
2346 goto finally;
2347 }
2348 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002349
Guido van Rossum60456fd1997-04-09 17:36:32 +00002350 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002351 if (type == &PyFloat_Type) {
2352 res = save_float(self, args);
2353 goto finally;
2354 }
2355 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002356
Guido van Rossum60456fd1997-04-09 17:36:32 +00002357 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002358 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2359 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002360 goto finally;
2361 }
2362 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002363
Guido van Rossum60456fd1997-04-09 17:36:32 +00002364 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002365 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2366 res = save_string(self, args, 0);
2367 goto finally;
2368 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002369
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002370#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002371 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002372 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2373 res = save_unicode(self, args, 0);
2374 goto finally;
2375 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002376#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002377 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002378
Christian Heimese93237d2007-12-19 02:37:44 +00002379 if (Py_REFCNT(args) > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002380 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002381 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002382
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002383 if (PyDict_GetItem(self->memo, py_ob_id)) {
2384 if (get(self, py_ob_id) < 0)
2385 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002386
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002387 res = 0;
2388 goto finally;
2389 }
2390 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002392 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002393 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002394 if (type == &PyString_Type) {
2395 res = save_string(self, args, 1);
2396 goto finally;
2397 }
2398 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002399
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002400#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002401 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002402 if (type == &PyUnicode_Type) {
2403 res = save_unicode(self, args, 1);
2404 goto finally;
2405 }
2406 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002407#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002408
Guido van Rossum60456fd1997-04-09 17:36:32 +00002409 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002410 if (type == &PyTuple_Type) {
2411 res = save_tuple(self, args);
2412 goto finally;
2413 }
2414 if (type == &PyType_Type) {
2415 res = save_global(self, args, NULL);
2416 goto finally;
2417 }
2418 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002419
Guido van Rossum60456fd1997-04-09 17:36:32 +00002420 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002421 if (type == &PyList_Type) {
2422 res = save_list(self, args);
2423 goto finally;
2424 }
2425 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002426
2427 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002428 if (type == &PyDict_Type) {
2429 res = save_dict(self, args);
2430 goto finally;
2431 }
2432 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002433
2434 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002435 if (type == &PyInstance_Type) {
2436 res = save_inst(self, args);
2437 goto finally;
2438 }
2439 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002440
2441 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002442 if (type == &PyClass_Type) {
2443 res = save_global(self, args, NULL);
2444 goto finally;
2445 }
2446 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002447
2448 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002449 if (type == &PyFunction_Type) {
2450 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002451 if (res && PyErr_ExceptionMatches(PickleError)) {
2452 /* fall back to reduce */
2453 PyErr_Clear();
2454 break;
2455 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002456 goto finally;
2457 }
2458 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002459
2460 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002461 if (type == &PyCFunction_Type) {
2462 res = save_global(self, args, NULL);
2463 goto finally;
2464 }
2465 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002466
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002467 if (!pers_save && self->inst_pers_func) {
2468 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2469 res = tmp;
2470 goto finally;
2471 }
2472 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002473
Jeremy Hylton39c61162002-07-16 19:47:43 +00002474 if (PyType_IsSubtype(type, &PyType_Type)) {
2475 res = save_global(self, args, NULL);
2476 goto finally;
2477 }
2478
Guido van Rossumb289b872003-02-19 01:45:13 +00002479 /* Get a reduction callable, and call it. This may come from
2480 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2481 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002482 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002483 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2484 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002485 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002486 Py_INCREF(args);
2487 ARG_TUP(self, args);
2488 if (self->arg) {
2489 t = PyObject_Call(__reduce__, self->arg, NULL);
2490 FREE_ARG_TUP(self);
2491 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002492 }
2493 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002494 /* Check for a __reduce_ex__ method. */
2495 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2496 if (__reduce__ != NULL) {
2497 t = PyInt_FromLong(self->proto);
2498 if (t != NULL) {
2499 ARG_TUP(self, t);
2500 t = NULL;
2501 if (self->arg) {
2502 t = PyObject_Call(__reduce__,
2503 self->arg, NULL);
2504 FREE_ARG_TUP(self);
2505 }
2506 }
2507 }
2508 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002509 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2510 PyErr_Clear();
2511 else
2512 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002513 /* Check for a __reduce__ method. */
2514 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2515 if (__reduce__ != NULL) {
2516 t = PyObject_Call(__reduce__,
2517 empty_tuple, NULL);
2518 }
2519 else {
2520 PyErr_SetObject(UnpickleableError, args);
2521 goto finally;
2522 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002523 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002524 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002525
Tim Peters71fcda52003-02-14 23:05:28 +00002526 if (t == NULL)
2527 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002528
Tim Peters71fcda52003-02-14 23:05:28 +00002529 if (PyString_Check(t)) {
2530 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002531 goto finally;
2532 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002533
Tim Peters71fcda52003-02-14 23:05:28 +00002534 if (! PyTuple_Check(t)) {
2535 cPickle_ErrFormat(PicklingError, "Value returned by "
2536 "%s must be string or tuple",
2537 "O", __reduce__);
2538 goto finally;
2539 }
2540
2541 size = PyTuple_Size(t);
2542 if (size < 2 || size > 5) {
2543 cPickle_ErrFormat(PicklingError, "tuple returned by "
2544 "%s must contain 2 through 5 elements",
2545 "O", __reduce__);
2546 goto finally;
2547 }
2548
2549 arg_tup = PyTuple_GET_ITEM(t, 1);
2550 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2551 cPickle_ErrFormat(PicklingError, "Second element of "
2552 "tuple returned by %s must be a tuple",
2553 "O", __reduce__);
2554 goto finally;
2555 }
2556
2557 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002558
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002559 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002560 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002561 Py_XDECREF(py_ob_id);
2562 Py_XDECREF(__reduce__);
2563 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002564
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002565 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002566}
2567
2568
2569static int
Tim Peterscba30e22003-02-01 06:24:36 +00002570dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002571{
2572 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002573
Tim Peters4190fb82003-02-02 16:09:05 +00002574 if (self->proto >= 2) {
2575 char bytes[2];
2576
2577 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002578 assert(self->proto >= 0 && self->proto < 256);
2579 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002580 if (self->write_func(self, bytes, 2) < 0)
2581 return -1;
2582 }
2583
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002584 if (save(self, args, 0) < 0)
2585 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002586
Tim Peters4190fb82003-02-02 16:09:05 +00002587 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002588 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002589
Tim Peters4190fb82003-02-02 16:09:05 +00002590 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002591 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002592
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002593 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002594}
2595
2596static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002597Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002598{
Tim Peterscba30e22003-02-01 06:24:36 +00002599 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002600 PyDict_Clear(self->memo);
2601 Py_INCREF(Py_None);
2602 return Py_None;
2603}
2604
2605static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002606Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002607{
2608 int l, i, rsize, ssize, clear=1, lm;
2609 long ik;
2610 PyObject *k, *r;
2611 char *s, *p, *have_get;
2612 Pdata *data;
2613
2614 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002615 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002616 return NULL;
2617
2618 /* Check to make sure we are based on a list */
2619 if (! Pdata_Check(self->file)) {
2620 PyErr_SetString(PicklingError,
2621 "Attempt to getvalue() a non-list-based pickler");
2622 return NULL;
2623 }
2624
2625 /* flush write buffer */
2626 if (write_other(self, NULL, 0) < 0) return NULL;
2627
2628 data=(Pdata*)self->file;
2629 l=data->length;
2630
2631 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002632 lm = PyDict_Size(self->memo);
2633 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002634 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002635 have_get = malloc(lm);
2636 if (have_get == NULL) return PyErr_NoMemory();
2637 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002638
2639 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002640 for (rsize = 0, i = l; --i >= 0; ) {
2641 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002642
Tim Petersac5687a2003-02-02 18:08:34 +00002643 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002644 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002645
2646 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002647 ik = PyInt_AS_LONG((PyIntObject*)k);
2648 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002649 PyErr_SetString(PicklingError,
2650 "Invalid get data");
Neal Norwitz98a96002006-07-23 07:57:11 +00002651 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002652 }
Tim Petersac5687a2003-02-02 18:08:34 +00002653 if (have_get[ik]) /* with matching get */
2654 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002655 }
2656
2657 else if (! (PyTuple_Check(k) &&
2658 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002659 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002660 ) {
2661 PyErr_SetString(PicklingError,
2662 "Unexpected data in internal list");
Neal Norwitz98a96002006-07-23 07:57:11 +00002663 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002664 }
2665
2666 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002667 ik = PyInt_AS_LONG((PyIntObject *)k);
2668 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002669 PyErr_SetString(PicklingError,
2670 "Invalid get data");
2671 return NULL;
2672 }
Tim Petersac5687a2003-02-02 18:08:34 +00002673 have_get[ik] = 1;
2674 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002675 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002676 }
2677
2678 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002679 r = PyString_FromStringAndSize(NULL, rsize);
2680 if (r == NULL) goto err;
2681 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002682
Tim Petersac5687a2003-02-02 18:08:34 +00002683 for (i = 0; i < l; i++) {
2684 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002685
2686 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002687 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002688 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002689 p=PyString_AS_STRING((PyStringObject *)k);
2690 while (--ssize >= 0)
2691 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002692 }
2693 }
2694
2695 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002696 ik = PyInt_AS_LONG((PyIntObject *)
2697 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002698 if (ik < 256) {
2699 *s++ = BINGET;
2700 *s++ = (int)(ik & 0xff);
2701 }
2702 else {
2703 *s++ = LONG_BINGET;
2704 *s++ = (int)(ik & 0xff);
2705 *s++ = (int)((ik >> 8) & 0xff);
2706 *s++ = (int)((ik >> 16) & 0xff);
2707 *s++ = (int)((ik >> 24) & 0xff);
2708 }
2709 }
2710
2711 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002712 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002713
2714 if (have_get[ik]) { /* with matching get */
2715 if (ik < 256) {
2716 *s++ = BINPUT;
2717 *s++ = (int)(ik & 0xff);
2718 }
2719 else {
2720 *s++ = LONG_BINPUT;
2721 *s++ = (int)(ik & 0xff);
2722 *s++ = (int)((ik >> 8) & 0xff);
2723 *s++ = (int)((ik >> 16) & 0xff);
2724 *s++ = (int)((ik >> 24) & 0xff);
2725 }
2726 }
2727 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002728 }
2729
2730 if (clear) {
2731 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002732 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002733 }
2734
2735 free(have_get);
2736 return r;
2737 err:
2738 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002739 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002740}
2741
2742static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002743Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002744{
2745 PyObject *ob;
2746 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002747
Tim Peterscba30e22003-02-01 06:24:36 +00002748 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002749 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002750
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002751 if (dump(self, ob) < 0)
2752 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002753
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002754 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002756 /* XXX Why does dump() return self? */
2757 Py_INCREF(self);
2758 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002759}
2760
2761
Tim Peterscba30e22003-02-01 06:24:36 +00002762static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002763{
Neal Norwitzb0493252002-03-31 14:44:22 +00002764 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002765 PyDoc_STR("dump(object) -- "
2766 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002767 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002768 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002769 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002770 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002771 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002772};
2773
2774
2775static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002776newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002777{
2778 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002779
Tim Peters5bd2a792003-02-01 16:45:06 +00002780 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002781 proto = HIGHEST_PROTOCOL;
2782 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002783 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2784 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002785 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002786 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002787 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002788
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002789 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002790 if (self == NULL)
2791 return NULL;
2792 self->proto = proto;
2793 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002794 self->fp = NULL;
2795 self->write = NULL;
2796 self->memo = NULL;
2797 self->arg = NULL;
2798 self->pers_func = NULL;
2799 self->inst_pers_func = NULL;
2800 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002801 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002802 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002803 self->fast_container = 0;
2804 self->fast_memo = NULL;
2805 self->buf_size = 0;
2806 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002807
Tim Peters5bd2a792003-02-01 16:45:06 +00002808 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002809 if (file)
2810 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002811 else {
2812 file = Pdata_New();
2813 if (file == NULL)
2814 goto err;
2815 }
2816 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002817
Tim Peterscba30e22003-02-01 06:24:36 +00002818 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002819 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002820
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002821 if (PyFile_Check(file)) {
2822 self->fp = PyFile_AsFile(file);
2823 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002824 PyErr_SetString(PyExc_ValueError,
2825 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002826 goto err;
2827 }
2828 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002829 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002830 else if (PycStringIO_OutputCheck(file)) {
2831 self->write_func = write_cStringIO;
2832 }
2833 else if (file == Py_None) {
2834 self->write_func = write_none;
2835 }
2836 else {
2837 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002838
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002839 if (! Pdata_Check(file)) {
2840 self->write = PyObject_GetAttr(file, write_str);
2841 if (!self->write) {
2842 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002843 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002844 "argument must have 'write' "
2845 "attribute");
2846 goto err;
2847 }
2848 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002849
Tim Peters5bd2a792003-02-01 16:45:06 +00002850 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2851 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002852 PyErr_NoMemory();
2853 goto err;
2854 }
2855 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002856
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002857 if (PyEval_GetRestricted()) {
2858 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002859 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002860
Tim Peters5b7da392003-02-04 00:21:07 +00002861 if (m == NULL)
2862 goto err;
2863 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002864 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002865 if (self->dispatch_table == NULL)
2866 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002867 }
2868 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002869 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002870 Py_INCREF(dispatch_table);
2871 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002872 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002873
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002874 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002876 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002877 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002878 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002879}
2880
2881
2882static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00002883get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002884{
Martin v. Löwis15e62742006-02-27 16:46:16 +00002885 static char *kwlist[] = {"file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002886 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002887 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002888
Tim Peters92c8bb32003-02-13 23:00:26 +00002889 /* XXX
Martin v. Löwis544f1192004-07-27 05:22:33 +00002890 * The documented signature is Pickler(file, protocol=0), but this
Tim Peters92c8bb32003-02-13 23:00:26 +00002891 * accepts Pickler() and Pickler(integer) too. The meaning then
2892 * is clear as mud, undocumented, and not supported by pickle.py.
2893 * I'm told Zope uses this, but I haven't traced into this code
2894 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002895 */
2896 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002897 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002898 proto = 0;
Martin v. Löwis544f1192004-07-27 05:22:33 +00002899 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2900 kwlist, &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002901 return NULL;
2902 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002903 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002904}
2905
2906
2907static void
Tim Peterscba30e22003-02-01 06:24:36 +00002908Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002909{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002910 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002911 Py_XDECREF(self->write);
2912 Py_XDECREF(self->memo);
2913 Py_XDECREF(self->fast_memo);
2914 Py_XDECREF(self->arg);
2915 Py_XDECREF(self->file);
2916 Py_XDECREF(self->pers_func);
2917 Py_XDECREF(self->inst_pers_func);
2918 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002919 PyMem_Free(self->write_buf);
Christian Heimese93237d2007-12-19 02:37:44 +00002920 Py_TYPE(self)->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002921}
2922
2923static int
2924Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2925{
Thomas Woutersc6e55062006-04-15 21:47:09 +00002926 Py_VISIT(self->write);
2927 Py_VISIT(self->memo);
2928 Py_VISIT(self->fast_memo);
2929 Py_VISIT(self->arg);
2930 Py_VISIT(self->file);
2931 Py_VISIT(self->pers_func);
2932 Py_VISIT(self->inst_pers_func);
2933 Py_VISIT(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002934 return 0;
2935}
2936
2937static int
2938Pickler_clear(Picklerobject *self)
2939{
Thomas Woutersedf17d82006-04-15 17:28:34 +00002940 Py_CLEAR(self->write);
2941 Py_CLEAR(self->memo);
2942 Py_CLEAR(self->fast_memo);
2943 Py_CLEAR(self->arg);
2944 Py_CLEAR(self->file);
2945 Py_CLEAR(self->pers_func);
2946 Py_CLEAR(self->inst_pers_func);
2947 Py_CLEAR(self->dispatch_table);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002948 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002949}
2950
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002951static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002952Pickler_get_pers_func(Picklerobject *p)
2953{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002954 if (p->pers_func == NULL)
2955 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2956 else
2957 Py_INCREF(p->pers_func);
2958 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002959}
2960
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002961static int
2962Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2963{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002964 if (v == NULL) {
2965 PyErr_SetString(PyExc_TypeError,
2966 "attribute deletion is not supported");
2967 return -1;
2968 }
2969 Py_XDECREF(p->pers_func);
2970 Py_INCREF(v);
2971 p->pers_func = v;
2972 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002973}
2974
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002975static int
2976Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2977{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002978 if (v == NULL) {
2979 PyErr_SetString(PyExc_TypeError,
2980 "attribute deletion is not supported");
2981 return -1;
2982 }
2983 Py_XDECREF(p->inst_pers_func);
2984 Py_INCREF(v);
2985 p->inst_pers_func = v;
2986 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002987}
2988
2989static PyObject *
2990Pickler_get_memo(Picklerobject *p)
2991{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002992 if (p->memo == NULL)
2993 PyErr_SetString(PyExc_AttributeError, "memo");
2994 else
2995 Py_INCREF(p->memo);
2996 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002997}
2998
2999static int
3000Pickler_set_memo(Picklerobject *p, PyObject *v)
3001{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003002 if (v == NULL) {
3003 PyErr_SetString(PyExc_TypeError,
3004 "attribute deletion is not supported");
3005 return -1;
3006 }
3007 if (!PyDict_Check(v)) {
3008 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3009 return -1;
3010 }
3011 Py_XDECREF(p->memo);
3012 Py_INCREF(v);
3013 p->memo = v;
3014 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003015}
3016
3017static PyObject *
3018Pickler_get_error(Picklerobject *p)
3019{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003020 /* why is this an attribute on the Pickler? */
3021 Py_INCREF(PicklingError);
3022 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003023}
3024
3025static PyMemberDef Pickler_members[] = {
3026 {"binary", T_INT, offsetof(Picklerobject, bin)},
3027 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003028 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003029};
3030
3031static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003032 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003033 (setter)Pickler_set_pers_func},
3034 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3035 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003036 {"PicklingError", (getter)Pickler_get_error, NULL},
3037 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003038};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003039
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003040PyDoc_STRVAR(Picklertype__doc__,
3041"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003042
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003043static PyTypeObject Picklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00003044 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +00003045 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003046 sizeof(Picklerobject), /*tp_basicsize*/
3047 0,
3048 (destructor)Pickler_dealloc, /* tp_dealloc */
3049 0, /* tp_print */
3050 0, /* tp_getattr */
3051 0, /* tp_setattr */
3052 0, /* tp_compare */
3053 0, /* tp_repr */
3054 0, /* tp_as_number */
3055 0, /* tp_as_sequence */
3056 0, /* tp_as_mapping */
3057 0, /* tp_hash */
3058 0, /* tp_call */
3059 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003060 PyObject_GenericGetAttr, /* tp_getattro */
3061 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003062 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003063 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003064 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003065 (traverseproc)Pickler_traverse, /* tp_traverse */
3066 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003067 0, /* tp_richcompare */
3068 0, /* tp_weaklistoffset */
3069 0, /* tp_iter */
3070 0, /* tp_iternext */
3071 Pickler_methods, /* tp_methods */
3072 Pickler_members, /* tp_members */
3073 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003074};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003075
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003076static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003077find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003078{
3079 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003080
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003081 if (fc) {
3082 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003083 PyErr_SetString(UnpicklingError, "Global and instance "
3084 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003085 return NULL;
3086 }
Georg Brandl684fd0c2006-05-25 19:15:31 +00003087 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3088 py_global_name, NULL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003089 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003090
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003091 module = PySys_GetObject("modules");
3092 if (module == NULL)
3093 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003094
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003095 module = PyDict_GetItem(module, py_module_name);
3096 if (module == NULL) {
3097 module = PyImport_Import(py_module_name);
3098 if (!module)
3099 return NULL;
3100 global = PyObject_GetAttr(module, py_global_name);
3101 Py_DECREF(module);
3102 }
3103 else
3104 global = PyObject_GetAttr(module, py_global_name);
3105 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003106}
3107
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003108static int
Tim Peterscba30e22003-02-01 06:24:36 +00003109marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003110{
3111 if (self->num_marks < 1) {
3112 PyErr_SetString(UnpicklingError, "could not find MARK");
3113 return -1;
3114 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003115
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003116 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003117}
3118
Tim Peters84e87f32001-03-17 04:50:51 +00003119
Guido van Rossum60456fd1997-04-09 17:36:32 +00003120static int
Tim Peterscba30e22003-02-01 06:24:36 +00003121load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003122{
3123 PDATA_APPEND(self->stack, Py_None, -1);
3124 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003125}
3126
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003127static int
Tim Peterscba30e22003-02-01 06:24:36 +00003128bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003129{
3130 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3131 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003132}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003133
3134static int
Tim Peterscba30e22003-02-01 06:24:36 +00003135load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003136{
3137 PyObject *py_int = 0;
3138 char *endptr, *s;
3139 int len, res = -1;
3140 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003141
Tim Peters0bc93f52003-02-02 18:29:33 +00003142 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003143 if (len < 2) return bad_readline();
3144 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003145
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003146 errno = 0;
3147 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003148
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003149 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3150 /* Hm, maybe we've got something long. Let's try reading
3151 it as a Python long object. */
3152 errno = 0;
3153 py_int = PyLong_FromString(s, NULL, 0);
3154 if (py_int == NULL) {
3155 PyErr_SetString(PyExc_ValueError,
3156 "could not convert string to int");
3157 goto finally;
3158 }
3159 }
3160 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003161 if (len == 3 && (l == 0 || l == 1)) {
3162 if (!( py_int = PyBool_FromLong(l))) goto finally;
3163 }
3164 else {
3165 if (!( py_int = PyInt_FromLong(l))) goto finally;
3166 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003167 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003168
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003169 free(s);
3170 PDATA_PUSH(self->stack, py_int, -1);
3171 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003173 finally:
3174 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003175
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003176 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003177}
3178
Tim Peters3c67d792003-02-02 17:59:11 +00003179static int
3180load_bool(Unpicklerobject *self, PyObject *boolean)
3181{
3182 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003183 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003184 return 0;
3185}
3186
Tim Petersee1a53c2003-02-02 02:57:53 +00003187/* s contains x bytes of a little-endian integer. Return its value as a
3188 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3189 * int, but when x is 4 it's a signed one. This is an historical source
3190 * of x-platform bugs.
3191 */
Tim Peters84e87f32001-03-17 04:50:51 +00003192static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003193calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003194{
3195 unsigned char c;
3196 int i;
3197 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003198
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003199 for (i = 0, l = 0L; i < x; i++) {
3200 c = (unsigned char)s[i];
3201 l |= (long)c << (i * 8);
3202 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003203#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003204 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3205 * is signed, so on a box with longs bigger than 4 bytes we need
3206 * to extend a BININT's sign bit to the full width.
3207 */
3208 if (x == 4 && l & (1L << 31))
3209 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003210#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003211 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003212}
3213
3214
3215static int
Tim Peterscba30e22003-02-01 06:24:36 +00003216load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003217{
3218 PyObject *py_int = 0;
3219 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003220
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003221 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003222
Tim Peterscba30e22003-02-01 06:24:36 +00003223 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003224 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003225
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003226 PDATA_PUSH(self->stack, py_int, -1);
3227 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003228}
3229
3230
3231static int
Tim Peterscba30e22003-02-01 06:24:36 +00003232load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003233{
3234 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003235
Tim Peters0bc93f52003-02-02 18:29:33 +00003236 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003237 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003238
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003239 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240}
3241
3242
3243static int
Tim Peterscba30e22003-02-01 06:24:36 +00003244load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003245{
3246 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003247
Tim Peters0bc93f52003-02-02 18:29:33 +00003248 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003249 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003250
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003251 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003252}
3253
3254
3255static int
Tim Peterscba30e22003-02-01 06:24:36 +00003256load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003257{
3258 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003259
Tim Peters0bc93f52003-02-02 18:29:33 +00003260 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003261 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003262
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003263 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003264}
Tim Peters84e87f32001-03-17 04:50:51 +00003265
Guido van Rossum60456fd1997-04-09 17:36:32 +00003266static int
Tim Peterscba30e22003-02-01 06:24:36 +00003267load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003268{
3269 PyObject *l = 0;
3270 char *end, *s;
3271 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003272
Tim Peters0bc93f52003-02-02 18:29:33 +00003273 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003274 if (len < 2) return bad_readline();
3275 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003276
Tim Peterscba30e22003-02-01 06:24:36 +00003277 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003278 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003279
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003280 free(s);
3281 PDATA_PUSH(self->stack, l, -1);
3282 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003283
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003284 finally:
3285 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003286
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003287 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003288}
3289
Tim Petersee1a53c2003-02-02 02:57:53 +00003290/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3291 * data following.
3292 */
3293static int
3294load_counted_long(Unpicklerobject *self, int size)
3295{
Martin v. Löwis18e16552006-02-15 17:27:45 +00003296 Py_ssize_t i;
Tim Petersee1a53c2003-02-02 02:57:53 +00003297 char *nbytes;
3298 unsigned char *pdata;
3299 PyObject *along;
3300
3301 assert(size == 1 || size == 4);
3302 i = self->read_func(self, &nbytes, size);
3303 if (i < 0) return -1;
3304
3305 size = calc_binint(nbytes, size);
3306 if (size < 0) {
3307 /* Corrupt or hostile pickle -- we never write one like
3308 * this.
3309 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003310 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003311 "byte count");
3312 return -1;
3313 }
3314
3315 if (size == 0)
3316 along = PyLong_FromLong(0L);
3317 else {
3318 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003319 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003320 if (i < 0) return -1;
3321 along = _PyLong_FromByteArray(pdata, (size_t)size,
3322 1 /* little endian */, 1 /* signed */);
3323 }
3324 if (along == NULL)
3325 return -1;
3326 PDATA_PUSH(self->stack, along, -1);
3327 return 0;
3328}
Tim Peters84e87f32001-03-17 04:50:51 +00003329
Guido van Rossum60456fd1997-04-09 17:36:32 +00003330static int
Tim Peterscba30e22003-02-01 06:24:36 +00003331load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003332{
3333 PyObject *py_float = 0;
3334 char *endptr, *s;
3335 int len, res = -1;
3336 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003337
Tim Peters0bc93f52003-02-02 18:29:33 +00003338 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003339 if (len < 2) return bad_readline();
3340 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003341
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003342 errno = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +00003343 d = PyOS_ascii_strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003344
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003345 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3346 PyErr_SetString(PyExc_ValueError,
3347 "could not convert string to float");
3348 goto finally;
3349 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003350
Tim Peterscba30e22003-02-01 06:24:36 +00003351 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003352 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003353
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003354 free(s);
3355 PDATA_PUSH(self->stack, py_float, -1);
3356 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003358 finally:
3359 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003361 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003362}
3363
Guido van Rossum60456fd1997-04-09 17:36:32 +00003364static int
Tim Peterscba30e22003-02-01 06:24:36 +00003365load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003366{
Tim Peters9905b942003-03-20 20:53:32 +00003367 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003368 double x;
3369 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003370
Tim Peters0bc93f52003-02-02 18:29:33 +00003371 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003372 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003373
Tim Peters9905b942003-03-20 20:53:32 +00003374 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3375 if (x == -1.0 && PyErr_Occurred())
3376 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003377
Tim Peters9905b942003-03-20 20:53:32 +00003378 py_float = PyFloat_FromDouble(x);
3379 if (py_float == NULL)
3380 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003381
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003382 PDATA_PUSH(self->stack, py_float, -1);
3383 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003384}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003385
3386static int
Tim Peterscba30e22003-02-01 06:24:36 +00003387load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003388{
3389 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003390 int len, res = -1;
3391 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003392
Tim Peters0bc93f52003-02-02 18:29:33 +00003393 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003394 if (len < 2) return bad_readline();
3395 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003396
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003397
3398 /* Strip outermost quotes */
3399 while (s[len-1] <= ' ')
3400 len--;
3401 if(s[0]=='"' && s[len-1]=='"'){
3402 s[len-1] = '\0';
3403 p = s + 1 ;
3404 len -= 2;
3405 } else if(s[0]=='\'' && s[len-1]=='\''){
3406 s[len-1] = '\0';
3407 p = s + 1 ;
3408 len -= 2;
3409 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003410 goto insecure;
3411 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003412
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003413 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
Neal Norwitz99dfe3c2006-08-02 04:27:11 +00003414 free(s);
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003415 if (str) {
3416 PDATA_PUSH(self->stack, str, -1);
3417 res = 0;
3418 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003419 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003420
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003421 insecure:
3422 free(s);
3423 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3424 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003425}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003426
3427
3428static int
Tim Peterscba30e22003-02-01 06:24:36 +00003429load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003430{
3431 PyObject *py_string = 0;
3432 long l;
3433 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003434
Tim Peters0bc93f52003-02-02 18:29:33 +00003435 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003436
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003437 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003438
Tim Peters0bc93f52003-02-02 18:29:33 +00003439 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003440 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003441
Tim Peterscba30e22003-02-01 06:24:36 +00003442 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003443 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003444
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003445 PDATA_PUSH(self->stack, py_string, -1);
3446 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003447}
3448
3449
3450static int
Tim Peterscba30e22003-02-01 06:24:36 +00003451load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003452{
3453 PyObject *py_string = 0;
3454 unsigned char l;
3455 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003456
Tim Peters0bc93f52003-02-02 18:29:33 +00003457 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003458 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003459
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003460 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003461
Tim Peters0bc93f52003-02-02 18:29:33 +00003462 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003463
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003464 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003465
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003466 PDATA_PUSH(self->stack, py_string, -1);
3467 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003468}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003469
3470
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003471#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003472static int
Tim Peterscba30e22003-02-01 06:24:36 +00003473load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003474{
3475 PyObject *str = 0;
3476 int len, res = -1;
3477 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003478
Tim Peters0bc93f52003-02-02 18:29:33 +00003479 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003480 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003481
Tim Peterscba30e22003-02-01 06:24:36 +00003482 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003483 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003484
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003485 PDATA_PUSH(self->stack, str, -1);
3486 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003487
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003488 finally:
3489 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003490}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003491#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003492
3493
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003494#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003495static int
Tim Peterscba30e22003-02-01 06:24:36 +00003496load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003497{
3498 PyObject *unicode;
3499 long l;
3500 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003501
Tim Peters0bc93f52003-02-02 18:29:33 +00003502 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003503
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003504 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003505
Tim Peters0bc93f52003-02-02 18:29:33 +00003506 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003507 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003508
Tim Peterscba30e22003-02-01 06:24:36 +00003509 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003510 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003511
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003512 PDATA_PUSH(self->stack, unicode, -1);
3513 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003514}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003515#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003516
3517
3518static int
Tim Peterscba30e22003-02-01 06:24:36 +00003519load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003520{
3521 PyObject *tup;
3522 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003523
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003524 if ((i = marker(self)) < 0) return -1;
3525 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3526 PDATA_PUSH(self->stack, tup, -1);
3527 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003528}
3529
3530static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003531load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003532{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003533 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003534
Tim Peters1d63c9f2003-02-02 20:29:39 +00003535 if (tup == NULL)
3536 return -1;
3537
3538 while (--len >= 0) {
3539 PyObject *element;
3540
3541 PDATA_POP(self->stack, element);
3542 if (element == NULL)
3543 return -1;
3544 PyTuple_SET_ITEM(tup, len, element);
3545 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003546 PDATA_PUSH(self->stack, tup, -1);
3547 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003548}
3549
3550static int
Tim Peterscba30e22003-02-01 06:24:36 +00003551load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003552{
3553 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003554
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003555 if (!( list=PyList_New(0))) return -1;
3556 PDATA_PUSH(self->stack, list, -1);
3557 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003558}
3559
3560static int
Tim Peterscba30e22003-02-01 06:24:36 +00003561load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003562{
3563 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003564
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003565 if (!( dict=PyDict_New())) return -1;
3566 PDATA_PUSH(self->stack, dict, -1);
3567 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003568}
3569
3570
3571static int
Tim Peterscba30e22003-02-01 06:24:36 +00003572load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003573{
3574 PyObject *list = 0;
3575 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003576
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003577 if ((i = marker(self)) < 0) return -1;
3578 if (!( list=Pdata_popList(self->stack, i))) return -1;
3579 PDATA_PUSH(self->stack, list, -1);
3580 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003581}
3582
3583static int
Tim Peterscba30e22003-02-01 06:24:36 +00003584load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003585{
3586 PyObject *dict, *key, *value;
3587 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003588
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003589 if ((i = marker(self)) < 0) return -1;
3590 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003591
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003592 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003593
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003594 for (k = i+1; k < j; k += 2) {
3595 key =self->stack->data[k-1];
3596 value=self->stack->data[k ];
3597 if (PyDict_SetItem(dict, key, value) < 0) {
3598 Py_DECREF(dict);
3599 return -1;
3600 }
3601 }
3602 Pdata_clear(self->stack, i);
3603 PDATA_PUSH(self->stack, dict, -1);
3604 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003605}
3606
3607static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003608Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003609{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003610 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003611
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003612 if (PyClass_Check(cls)) {
3613 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003614
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003615 if ((l=PyObject_Size(args)) < 0) goto err;
3616 if (!( l )) {
3617 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003618
Tim Peterscba30e22003-02-01 06:24:36 +00003619 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003620 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003621 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003622 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003623 so bypass usual construction */
3624 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003626 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003627 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003628 goto err;
3629 return inst;
3630 }
3631 Py_DECREF(__getinitargs__);
3632 }
Tim Peters84e87f32001-03-17 04:50:51 +00003633
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003634 if ((r=PyInstance_New(cls, args, NULL))) return r;
3635 else goto err;
3636 }
Tim Peters84e87f32001-03-17 04:50:51 +00003637
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003638 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003640 err:
3641 {
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003642 PyObject *tp, *v, *tb, *tmp_value;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003643
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003644 PyErr_Fetch(&tp, &v, &tb);
Neal Norwitz0f8b31a2006-06-28 06:28:31 +00003645 tmp_value = v;
3646 /* NULL occurs when there was a KeyboardInterrupt */
3647 if (tmp_value == NULL)
3648 tmp_value = Py_None;
3649 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003650 Py_XDECREF(v);
3651 v=r;
3652 }
3653 PyErr_Restore(tp,v,tb);
3654 }
3655 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003656}
Tim Peters84e87f32001-03-17 04:50:51 +00003657
Guido van Rossum60456fd1997-04-09 17:36:32 +00003658
3659static int
Tim Peterscba30e22003-02-01 06:24:36 +00003660load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003661{
3662 PyObject *class, *tup, *obj=0;
3663 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003664
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003665 if ((i = marker(self)) < 0) return -1;
3666 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3667 PDATA_POP(self->stack, class);
3668 if (class) {
3669 obj = Instance_New(class, tup);
3670 Py_DECREF(class);
3671 }
3672 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003673
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003674 if (! obj) return -1;
3675 PDATA_PUSH(self->stack, obj, -1);
3676 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003677}
3678
3679
3680static int
Tim Peterscba30e22003-02-01 06:24:36 +00003681load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003682{
3683 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3684 int i, len;
3685 char *s;
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;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003688
Tim Peters0bc93f52003-02-02 18:29:33 +00003689 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003690 if (len < 2) return bad_readline();
3691 module_name = PyString_FromStringAndSize(s, len - 1);
3692 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003693
Tim Peters0bc93f52003-02-02 18:29:33 +00003694 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003695 if (len < 2) return bad_readline();
3696 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003697 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003698 self->find_class);
3699 Py_DECREF(class_name);
3700 }
3701 }
3702 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003703
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003704 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003706 if ((tup=Pdata_popTuple(self->stack, i))) {
3707 obj = Instance_New(class, tup);
3708 Py_DECREF(tup);
3709 }
3710 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003711
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003712 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003713
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003714 PDATA_PUSH(self->stack, obj, -1);
3715 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003716}
3717
Tim Peterseab7db32003-02-13 18:24:14 +00003718static int
3719load_newobj(Unpicklerobject *self)
3720{
3721 PyObject *args = NULL;
3722 PyObject *clsraw = NULL;
3723 PyTypeObject *cls; /* clsraw cast to its true type */
3724 PyObject *obj;
3725
3726 /* Stack is ... cls argtuple, and we want to call
3727 * cls.__new__(cls, *argtuple).
3728 */
3729 PDATA_POP(self->stack, args);
3730 if (args == NULL) goto Fail;
3731 if (! PyTuple_Check(args)) {
3732 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3733 "tuple.");
3734 goto Fail;
3735 }
3736
3737 PDATA_POP(self->stack, clsraw);
3738 cls = (PyTypeObject *)clsraw;
3739 if (cls == NULL) goto Fail;
3740 if (! PyType_Check(cls)) {
3741 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3742 "isn't a type object");
3743 goto Fail;
3744 }
3745 if (cls->tp_new == NULL) {
3746 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3747 "has NULL tp_new");
3748 goto Fail;
3749 }
3750
3751 /* Call __new__. */
3752 obj = cls->tp_new(cls, args, NULL);
3753 if (obj == NULL) goto Fail;
3754
3755 Py_DECREF(args);
3756 Py_DECREF(clsraw);
3757 PDATA_PUSH(self->stack, obj, -1);
3758 return 0;
3759
3760 Fail:
3761 Py_XDECREF(args);
3762 Py_XDECREF(clsraw);
3763 return -1;
3764}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003765
3766static int
Tim Peterscba30e22003-02-01 06:24:36 +00003767load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003768{
3769 PyObject *class = 0, *module_name = 0, *class_name = 0;
3770 int len;
3771 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003772
Tim Peters0bc93f52003-02-02 18:29:33 +00003773 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003774 if (len < 2) return bad_readline();
3775 module_name = PyString_FromStringAndSize(s, len - 1);
3776 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003777
Tim Peters0bc93f52003-02-02 18:29:33 +00003778 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003779 if (len < 2) {
3780 Py_DECREF(module_name);
3781 return bad_readline();
3782 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003783 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003784 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003785 self->find_class);
3786 Py_DECREF(class_name);
3787 }
3788 }
3789 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003790
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003791 if (! class) return -1;
3792 PDATA_PUSH(self->stack, class, -1);
3793 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003794}
3795
3796
3797static int
Tim Peterscba30e22003-02-01 06:24:36 +00003798load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003799{
3800 PyObject *pid = 0;
3801 int len;
3802 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003803
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003804 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003805 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003806 if (len < 2) return bad_readline();
3807
3808 pid = PyString_FromStringAndSize(s, len - 1);
3809 if (!pid) return -1;
3810
3811 if (PyList_Check(self->pers_func)) {
3812 if (PyList_Append(self->pers_func, pid) < 0) {
3813 Py_DECREF(pid);
3814 return -1;
3815 }
3816 }
3817 else {
3818 ARG_TUP(self, pid);
3819 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003820 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003821 NULL);
3822 FREE_ARG_TUP(self);
3823 }
3824 }
3825
3826 if (! pid) return -1;
3827
3828 PDATA_PUSH(self->stack, pid, -1);
3829 return 0;
3830 }
3831 else {
3832 PyErr_SetString(UnpicklingError,
3833 "A load persistent id instruction was encountered,\n"
3834 "but no persistent_load function was specified.");
3835 return -1;
3836 }
3837}
3838
3839static int
Tim Peterscba30e22003-02-01 06:24:36 +00003840load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003841{
3842 PyObject *pid = 0;
3843
3844 if (self->pers_func) {
3845 PDATA_POP(self->stack, pid);
3846 if (! pid) return -1;
3847
3848 if (PyList_Check(self->pers_func)) {
3849 if (PyList_Append(self->pers_func, pid) < 0) {
3850 Py_DECREF(pid);
3851 return -1;
3852 }
3853 }
3854 else {
3855 ARG_TUP(self, pid);
3856 if (self->arg) {
3857 pid = PyObject_Call(self->pers_func, self->arg,
3858 NULL);
3859 FREE_ARG_TUP(self);
3860 }
3861 if (! pid) return -1;
3862 }
3863
3864 PDATA_PUSH(self->stack, pid, -1);
3865 return 0;
3866 }
3867 else {
3868 PyErr_SetString(UnpicklingError,
3869 "A load persistent id instruction was encountered,\n"
3870 "but no persistent_load function was specified.");
3871 return -1;
3872 }
3873}
3874
3875
3876static int
Tim Peterscba30e22003-02-01 06:24:36 +00003877load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003878{
3879 int len;
3880
3881 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3882
3883 /* Note that we split the (pickle.py) stack into two stacks,
3884 an object stack and a mark stack. We have to be clever and
3885 pop the right one. We do this by looking at the top of the
3886 mark stack.
3887 */
3888
3889 if ((self->num_marks > 0) &&
3890 (self->marks[self->num_marks - 1] == len))
3891 self->num_marks--;
3892 else {
3893 len--;
3894 Py_DECREF(self->stack->data[len]);
3895 self->stack->length=len;
3896 }
3897
3898 return 0;
3899}
3900
3901
3902static int
Tim Peterscba30e22003-02-01 06:24:36 +00003903load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003904{
3905 int i;
3906
3907 if ((i = marker(self)) < 0)
3908 return -1;
3909
3910 Pdata_clear(self->stack, i);
3911
3912 return 0;
3913}
3914
3915
3916static int
Tim Peterscba30e22003-02-01 06:24:36 +00003917load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003918{
3919 PyObject *last;
3920 int len;
3921
3922 if ((len = self->stack->length) <= 0) return stackUnderflow();
3923 last=self->stack->data[len-1];
3924 Py_INCREF(last);
3925 PDATA_PUSH(self->stack, last, -1);
3926 return 0;
3927}
3928
3929
3930static int
Tim Peterscba30e22003-02-01 06:24:36 +00003931load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003932{
3933 PyObject *py_str = 0, *value = 0;
3934 int len;
3935 char *s;
3936 int rc;
3937
Tim Peters0bc93f52003-02-02 18:29:33 +00003938 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003939 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003940
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003941 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003942
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003943 value = PyDict_GetItem(self->memo, py_str);
3944 if (! value) {
3945 PyErr_SetObject(BadPickleGet, py_str);
3946 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003947 }
3948 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003949 PDATA_APPEND(self->stack, value, -1);
3950 rc = 0;
3951 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003952
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003953 Py_DECREF(py_str);
3954 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003955}
3956
3957
3958static int
Tim Peterscba30e22003-02-01 06:24:36 +00003959load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003960{
3961 PyObject *py_key = 0, *value = 0;
3962 unsigned char key;
3963 char *s;
3964 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003965
Tim Peters0bc93f52003-02-02 18:29:33 +00003966 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003967
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003968 key = (unsigned char)s[0];
3969 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003970
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003971 value = PyDict_GetItem(self->memo, py_key);
3972 if (! value) {
3973 PyErr_SetObject(BadPickleGet, py_key);
3974 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003975 }
3976 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003977 PDATA_APPEND(self->stack, value, -1);
3978 rc = 0;
3979 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003980
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003981 Py_DECREF(py_key);
3982 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003983}
3984
3985
3986static int
Tim Peterscba30e22003-02-01 06:24:36 +00003987load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003988{
3989 PyObject *py_key = 0, *value = 0;
3990 unsigned char c;
3991 char *s;
3992 long key;
3993 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003994
Tim Peters0bc93f52003-02-02 18:29:33 +00003995 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003996
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003997 c = (unsigned char)s[0];
3998 key = (long)c;
3999 c = (unsigned char)s[1];
4000 key |= (long)c << 8;
4001 c = (unsigned char)s[2];
4002 key |= (long)c << 16;
4003 c = (unsigned char)s[3];
4004 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004005
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004006 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4007
4008 value = PyDict_GetItem(self->memo, py_key);
4009 if (! value) {
4010 PyErr_SetObject(BadPickleGet, py_key);
4011 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004012 }
4013 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004014 PDATA_APPEND(self->stack, value, -1);
4015 rc = 0;
4016 }
4017
4018 Py_DECREF(py_key);
4019 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004020}
4021
Tim Peters2d629652003-02-04 05:06:17 +00004022/* Push an object from the extension registry (EXT[124]). nbytes is
4023 * the number of bytes following the opcode, holding the index (code) value.
4024 */
4025static int
4026load_extension(Unpicklerobject *self, int nbytes)
4027{
4028 char *codebytes; /* the nbytes bytes after the opcode */
4029 long code; /* calc_binint returns long */
4030 PyObject *py_code; /* code as a Python int */
4031 PyObject *obj; /* the object to push */
4032 PyObject *pair; /* (module_name, class_name) */
4033 PyObject *module_name, *class_name;
4034
4035 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4036 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4037 code = calc_binint(codebytes, nbytes);
4038 if (code <= 0) { /* note that 0 is forbidden */
4039 /* Corrupt or hostile pickle. */
4040 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4041 return -1;
4042 }
4043
4044 /* Look for the code in the cache. */
4045 py_code = PyInt_FromLong(code);
4046 if (py_code == NULL) return -1;
4047 obj = PyDict_GetItem(extension_cache, py_code);
4048 if (obj != NULL) {
4049 /* Bingo. */
4050 Py_DECREF(py_code);
4051 PDATA_APPEND(self->stack, obj, -1);
4052 return 0;
4053 }
4054
4055 /* Look up the (module_name, class_name) pair. */
4056 pair = PyDict_GetItem(inverted_registry, py_code);
4057 if (pair == NULL) {
4058 Py_DECREF(py_code);
4059 PyErr_Format(PyExc_ValueError, "unregistered extension "
4060 "code %ld", code);
4061 return -1;
4062 }
4063 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004064 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004065 */
4066 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4067 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4068 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4069 Py_DECREF(py_code);
4070 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4071 "isn't a 2-tuple of strings", code);
4072 return -1;
4073 }
4074 /* Load the object. */
4075 obj = find_class(module_name, class_name, self->find_class);
4076 if (obj == NULL) {
4077 Py_DECREF(py_code);
4078 return -1;
4079 }
4080 /* Cache code -> obj. */
4081 code = PyDict_SetItem(extension_cache, py_code, obj);
4082 Py_DECREF(py_code);
4083 if (code < 0) {
4084 Py_DECREF(obj);
4085 return -1;
4086 }
4087 PDATA_PUSH(self->stack, obj, -1);
4088 return 0;
4089}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004090
4091static int
Tim Peterscba30e22003-02-01 06:24:36 +00004092load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004093{
4094 PyObject *py_str = 0, *value = 0;
4095 int len, l;
4096 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004097
Tim Peters0bc93f52003-02-02 18:29:33 +00004098 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004099 if (l < 2) return bad_readline();
4100 if (!( len=self->stack->length )) return stackUnderflow();
4101 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4102 value=self->stack->data[len-1];
4103 l=PyDict_SetItem(self->memo, py_str, value);
4104 Py_DECREF(py_str);
4105 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004106}
4107
4108
4109static int
Tim Peterscba30e22003-02-01 06:24:36 +00004110load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004111{
4112 PyObject *py_key = 0, *value = 0;
4113 unsigned char key;
4114 char *s;
4115 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004116
Tim Peters0bc93f52003-02-02 18:29:33 +00004117 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004118 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004119
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004120 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004121
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004122 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4123 value=self->stack->data[len-1];
4124 len=PyDict_SetItem(self->memo, py_key, value);
4125 Py_DECREF(py_key);
4126 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004127}
4128
4129
4130static int
Tim Peterscba30e22003-02-01 06:24:36 +00004131load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004132{
4133 PyObject *py_key = 0, *value = 0;
4134 long key;
4135 unsigned char c;
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, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004140 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004141
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004142 c = (unsigned char)s[0];
4143 key = (long)c;
4144 c = (unsigned char)s[1];
4145 key |= (long)c << 8;
4146 c = (unsigned char)s[2];
4147 key |= (long)c << 16;
4148 c = (unsigned char)s[3];
4149 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004151 if (!( py_key = PyInt_FromLong(key))) return -1;
4152 value=self->stack->data[len-1];
4153 len=PyDict_SetItem(self->memo, py_key, value);
4154 Py_DECREF(py_key);
4155 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004156}
4157
4158
4159static int
Tim Peterscba30e22003-02-01 06:24:36 +00004160do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004161{
4162 PyObject *value = 0, *list = 0, *append_method = 0;
4163 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004164
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004165 len=self->stack->length;
4166 if (!( len >= x && x > 0 )) return stackUnderflow();
4167 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004168 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004169
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004170 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004171
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004172 if (PyList_Check(list)) {
4173 PyObject *slice;
4174 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004175
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004176 slice=Pdata_popList(self->stack, x);
Neal Norwitzb59d08c2006-07-22 16:20:49 +00004177 if (! slice) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004178 list_len = PyList_GET_SIZE(list);
4179 i=PyList_SetSlice(list, list_len, list_len, slice);
4180 Py_DECREF(slice);
4181 return i;
4182 }
4183 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004184
Tim Peterscba30e22003-02-01 06:24:36 +00004185 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004186 return -1;
4187
4188 for (i = x; i < len; i++) {
4189 PyObject *junk;
4190
4191 value=self->stack->data[i];
4192 junk=0;
4193 ARG_TUP(self, value);
4194 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004195 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004196 NULL);
4197 FREE_ARG_TUP(self);
4198 }
4199 if (! junk) {
4200 Pdata_clear(self->stack, i+1);
4201 self->stack->length=x;
4202 Py_DECREF(append_method);
4203 return -1;
4204 }
4205 Py_DECREF(junk);
4206 }
4207 self->stack->length=x;
4208 Py_DECREF(append_method);
4209 }
4210
4211 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004212}
4213
4214
4215static int
Tim Peterscba30e22003-02-01 06:24:36 +00004216load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004217{
4218 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004219}
4220
4221
4222static int
Tim Peterscba30e22003-02-01 06:24:36 +00004223load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004224{
4225 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004226}
4227
4228
4229static int
Tim Peterscba30e22003-02-01 06:24:36 +00004230do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004231{
4232 PyObject *value = 0, *key = 0, *dict = 0;
4233 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004234
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004235 if (!( (len=self->stack->length) >= x
4236 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004237
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004238 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004239
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004240 for (i = x+1; i < len; i += 2) {
4241 key =self->stack->data[i-1];
4242 value=self->stack->data[i ];
4243 if (PyObject_SetItem(dict, key, value) < 0) {
4244 r=-1;
4245 break;
4246 }
4247 }
4248
4249 Pdata_clear(self->stack, x);
4250
4251 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004252}
4253
4254
Tim Peters84e87f32001-03-17 04:50:51 +00004255static int
Tim Peterscba30e22003-02-01 06:24:36 +00004256load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004257{
4258 return do_setitems(self, self->stack->length - 2);
4259}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004260
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004261static int
Tim Peterscba30e22003-02-01 06:24:36 +00004262load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004263{
4264 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004265}
4266
Tim Peters84e87f32001-03-17 04:50:51 +00004267
Guido van Rossum60456fd1997-04-09 17:36:32 +00004268static int
Tim Peterscba30e22003-02-01 06:24:36 +00004269load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004270{
Tim Peters080c88b2003-02-15 03:01:11 +00004271 PyObject *state, *inst, *slotstate;
4272 PyObject *__setstate__;
4273 PyObject *d_key, *d_value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00004274 Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00004275 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004276
Tim Peters080c88b2003-02-15 03:01:11 +00004277 /* Stack is ... instance, state. We want to leave instance at
4278 * the stack top, possibly mutated via instance.__setstate__(state).
4279 */
4280 if (self->stack->length < 2)
4281 return stackUnderflow();
4282 PDATA_POP(self->stack, state);
4283 if (state == NULL)
4284 return -1;
4285 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004286
Tim Peters080c88b2003-02-15 03:01:11 +00004287 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4288 if (__setstate__ != NULL) {
4289 PyObject *junk = NULL;
4290
4291 /* The explicit __setstate__ is responsible for everything. */
4292 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004293 if (self->arg) {
4294 junk = PyObject_Call(__setstate__, self->arg, NULL);
4295 FREE_ARG_TUP(self);
4296 }
4297 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004298 if (junk == NULL)
4299 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004300 Py_DECREF(junk);
4301 return 0;
4302 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004303 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4304 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004305 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004306
4307 /* A default __setstate__. First see whether state embeds a
4308 * slot state dict too (a proto 2 addition).
4309 */
4310 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4311 PyObject *temp = state;
4312 state = PyTuple_GET_ITEM(temp, 0);
4313 slotstate = PyTuple_GET_ITEM(temp, 1);
4314 Py_INCREF(state);
4315 Py_INCREF(slotstate);
4316 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004317 }
Tim Peters080c88b2003-02-15 03:01:11 +00004318 else
4319 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004320
Tim Peters080c88b2003-02-15 03:01:11 +00004321 /* Set inst.__dict__ from the state dict (if any). */
4322 if (state != Py_None) {
4323 PyObject *dict;
4324 if (! PyDict_Check(state)) {
4325 PyErr_SetString(UnpicklingError, "state is not a "
4326 "dictionary");
4327 goto finally;
4328 }
4329 dict = PyObject_GetAttr(inst, __dict___str);
4330 if (dict == NULL)
4331 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004332
Tim Peters080c88b2003-02-15 03:01:11 +00004333 i = 0;
4334 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4335 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4336 goto finally;
4337 }
4338 Py_DECREF(dict);
4339 }
4340
4341 /* Also set instance attributes from the slotstate dict (if any). */
4342 if (slotstate != NULL) {
4343 if (! PyDict_Check(slotstate)) {
4344 PyErr_SetString(UnpicklingError, "slot state is not "
4345 "a dictionary");
4346 goto finally;
4347 }
4348 i = 0;
4349 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4350 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4351 goto finally;
4352 }
4353 }
4354 res = 0;
4355
4356 finally:
4357 Py_DECREF(state);
4358 Py_XDECREF(slotstate);
4359 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004360}
4361
4362
4363static int
Tim Peterscba30e22003-02-01 06:24:36 +00004364load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004365{
4366 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004367
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004368 /* Note that we split the (pickle.py) stack into two stacks, an
4369 object stack and a mark stack. Here we push a mark onto the
4370 mark stack.
4371 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004372
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004373 if ((self->num_marks + 1) >= self->marks_size) {
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004374 int *marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004375 s=self->marks_size+20;
4376 if (s <= self->num_marks) s=self->num_marks + 1;
4377 if (self->marks == NULL)
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004378 marks=(int *)malloc(s * sizeof(int));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004379 else
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004380 marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004381 s * sizeof(int));
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004382 if (!marks) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004383 PyErr_NoMemory();
4384 return -1;
4385 }
Kristján Valur Jónsson17b8e972007-04-25 00:10:50 +00004386 self->marks = marks;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004387 self->marks_size = s;
4388 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004389
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004390 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004392 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004393}
4394
Guido van Rossum60456fd1997-04-09 17:36:32 +00004395static int
Tim Peterscba30e22003-02-01 06:24:36 +00004396load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004397{
4398 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004399
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004400 PDATA_POP(self->stack, arg_tup);
4401 if (! arg_tup) return -1;
4402 PDATA_POP(self->stack, callable);
4403 if (callable) {
4404 ob = Instance_New(callable, arg_tup);
4405 Py_DECREF(callable);
4406 }
4407 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004408
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004409 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004410
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004411 PDATA_PUSH(self->stack, ob, -1);
4412 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004413}
Tim Peters84e87f32001-03-17 04:50:51 +00004414
Tim Peters4190fb82003-02-02 16:09:05 +00004415/* Just raises an error if we don't know the protocol specified. PROTO
4416 * is the first opcode for protocols >= 2.
4417 */
4418static int
4419load_proto(Unpicklerobject *self)
4420{
4421 int i;
4422 char *protobyte;
4423
4424 i = self->read_func(self, &protobyte, 1);
4425 if (i < 0)
4426 return -1;
4427
4428 i = calc_binint(protobyte, 1);
4429 /* No point checking for < 0, since calc_binint returns an unsigned
4430 * int when chewing on 1 byte.
4431 */
4432 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004433 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004434 return 0;
4435
4436 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4437 return -1;
4438}
4439
Guido van Rossum60456fd1997-04-09 17:36:32 +00004440static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004441load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004442{
4443 PyObject *err = 0, *val = 0;
4444 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004445
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004446 self->num_marks = 0;
4447 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004448
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004449 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004450 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004451 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004452
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004453 switch (s[0]) {
4454 case NONE:
4455 if (load_none(self) < 0)
4456 break;
4457 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004458
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004459 case BININT:
4460 if (load_binint(self) < 0)
4461 break;
4462 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004463
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004464 case BININT1:
4465 if (load_binint1(self) < 0)
4466 break;
4467 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004468
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004469 case BININT2:
4470 if (load_binint2(self) < 0)
4471 break;
4472 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004473
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004474 case INT:
4475 if (load_int(self) < 0)
4476 break;
4477 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004478
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004479 case LONG:
4480 if (load_long(self) < 0)
4481 break;
4482 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004483
Tim Petersee1a53c2003-02-02 02:57:53 +00004484 case LONG1:
4485 if (load_counted_long(self, 1) < 0)
4486 break;
4487 continue;
4488
4489 case LONG4:
4490 if (load_counted_long(self, 4) < 0)
4491 break;
4492 continue;
4493
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004494 case FLOAT:
4495 if (load_float(self) < 0)
4496 break;
4497 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004499 case BINFLOAT:
4500 if (load_binfloat(self) < 0)
4501 break;
4502 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004503
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004504 case BINSTRING:
4505 if (load_binstring(self) < 0)
4506 break;
4507 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004508
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004509 case SHORT_BINSTRING:
4510 if (load_short_binstring(self) < 0)
4511 break;
4512 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004513
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004514 case STRING:
4515 if (load_string(self) < 0)
4516 break;
4517 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004518
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004519#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004520 case UNICODE:
4521 if (load_unicode(self) < 0)
4522 break;
4523 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004524
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004525 case BINUNICODE:
4526 if (load_binunicode(self) < 0)
4527 break;
4528 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004529#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004530
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004531 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004532 if (load_counted_tuple(self, 0) < 0)
4533 break;
4534 continue;
4535
4536 case TUPLE1:
4537 if (load_counted_tuple(self, 1) < 0)
4538 break;
4539 continue;
4540
4541 case TUPLE2:
4542 if (load_counted_tuple(self, 2) < 0)
4543 break;
4544 continue;
4545
4546 case TUPLE3:
4547 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004548 break;
4549 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004550
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004551 case TUPLE:
4552 if (load_tuple(self) < 0)
4553 break;
4554 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004555
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004556 case EMPTY_LIST:
4557 if (load_empty_list(self) < 0)
4558 break;
4559 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004560
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004561 case LIST:
4562 if (load_list(self) < 0)
4563 break;
4564 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004565
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004566 case EMPTY_DICT:
4567 if (load_empty_dict(self) < 0)
4568 break;
4569 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004570
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004571 case DICT:
4572 if (load_dict(self) < 0)
4573 break;
4574 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004575
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004576 case OBJ:
4577 if (load_obj(self) < 0)
4578 break;
4579 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004581 case INST:
4582 if (load_inst(self) < 0)
4583 break;
4584 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004585
Tim Peterseab7db32003-02-13 18:24:14 +00004586 case NEWOBJ:
4587 if (load_newobj(self) < 0)
4588 break;
4589 continue;
4590
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004591 case GLOBAL:
4592 if (load_global(self) < 0)
4593 break;
4594 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004595
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004596 case APPEND:
4597 if (load_append(self) < 0)
4598 break;
4599 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004600
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004601 case APPENDS:
4602 if (load_appends(self) < 0)
4603 break;
4604 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004605
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004606 case BUILD:
4607 if (load_build(self) < 0)
4608 break;
4609 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004610
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004611 case DUP:
4612 if (load_dup(self) < 0)
4613 break;
4614 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004615
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004616 case BINGET:
4617 if (load_binget(self) < 0)
4618 break;
4619 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004620
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004621 case LONG_BINGET:
4622 if (load_long_binget(self) < 0)
4623 break;
4624 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004626 case GET:
4627 if (load_get(self) < 0)
4628 break;
4629 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004630
Tim Peters2d629652003-02-04 05:06:17 +00004631 case EXT1:
4632 if (load_extension(self, 1) < 0)
4633 break;
4634 continue;
4635
4636 case EXT2:
4637 if (load_extension(self, 2) < 0)
4638 break;
4639 continue;
4640
4641 case EXT4:
4642 if (load_extension(self, 4) < 0)
4643 break;
4644 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004645 case MARK:
4646 if (load_mark(self) < 0)
4647 break;
4648 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004649
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004650 case BINPUT:
4651 if (load_binput(self) < 0)
4652 break;
4653 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004655 case LONG_BINPUT:
4656 if (load_long_binput(self) < 0)
4657 break;
4658 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004660 case PUT:
4661 if (load_put(self) < 0)
4662 break;
4663 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004664
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004665 case POP:
4666 if (load_pop(self) < 0)
4667 break;
4668 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004669
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004670 case POP_MARK:
4671 if (load_pop_mark(self) < 0)
4672 break;
4673 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004674
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004675 case SETITEM:
4676 if (load_setitem(self) < 0)
4677 break;
4678 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004679
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004680 case SETITEMS:
4681 if (load_setitems(self) < 0)
4682 break;
4683 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004684
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004685 case STOP:
4686 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004687
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004688 case PERSID:
4689 if (load_persid(self) < 0)
4690 break;
4691 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004692
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004693 case BINPERSID:
4694 if (load_binpersid(self) < 0)
4695 break;
4696 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004697
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004698 case REDUCE:
4699 if (load_reduce(self) < 0)
4700 break;
4701 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004702
Tim Peters4190fb82003-02-02 16:09:05 +00004703 case PROTO:
4704 if (load_proto(self) < 0)
4705 break;
4706 continue;
4707
Tim Peters3c67d792003-02-02 17:59:11 +00004708 case NEWTRUE:
4709 if (load_bool(self, Py_True) < 0)
4710 break;
4711 continue;
4712
4713 case NEWFALSE:
4714 if (load_bool(self, Py_False) < 0)
4715 break;
4716 continue;
4717
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004718 case '\0':
4719 /* end of file */
4720 PyErr_SetNone(PyExc_EOFError);
4721 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004722
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004723 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004724 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004725 "invalid load key, '%s'.",
4726 "c", s[0]);
4727 return NULL;
4728 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004729
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004730 break;
4731 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004732
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004733 if ((err = PyErr_Occurred())) {
4734 if (err == PyExc_EOFError) {
4735 PyErr_SetNone(PyExc_EOFError);
4736 }
4737 return NULL;
4738 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004739
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004740 PDATA_POP(self->stack, val);
4741 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004742}
Tim Peters84e87f32001-03-17 04:50:51 +00004743
Guido van Rossum60456fd1997-04-09 17:36:32 +00004744
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004745/* No-load functions to support noload, which is used to
4746 find persistent references. */
4747
4748static int
Tim Peterscba30e22003-02-01 06:24:36 +00004749noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004750{
4751 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004752
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004753 if ((i = marker(self)) < 0) return -1;
4754 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004755}
4756
4757
4758static int
Tim Peterscba30e22003-02-01 06:24:36 +00004759noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004760{
4761 int i;
4762 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004763
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004764 if ((i = marker(self)) < 0) return -1;
4765 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004766 if (self->readline_func(self, &s) < 0) return -1;
4767 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004768 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004769 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004770}
4771
4772static int
Tim Peterseab7db32003-02-13 18:24:14 +00004773noload_newobj(Unpicklerobject *self)
4774{
4775 PyObject *obj;
4776
4777 PDATA_POP(self->stack, obj); /* pop argtuple */
4778 if (obj == NULL) return -1;
4779 Py_DECREF(obj);
4780
4781 PDATA_POP(self->stack, obj); /* pop cls */
4782 if (obj == NULL) return -1;
4783 Py_DECREF(obj);
4784
4785 PDATA_APPEND(self->stack, Py_None, -1);
4786 return 0;
4787}
4788
4789static int
Tim Peterscba30e22003-02-01 06:24:36 +00004790noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004791{
4792 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004793
Tim Peters0bc93f52003-02-02 18:29:33 +00004794 if (self->readline_func(self, &s) < 0) return -1;
4795 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004796 PDATA_APPEND(self->stack, Py_None,-1);
4797 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004798}
4799
4800static int
Tim Peterscba30e22003-02-01 06:24:36 +00004801noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004802{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004803
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004804 if (self->stack->length < 2) return stackUnderflow();
4805 Pdata_clear(self->stack, self->stack->length-2);
4806 PDATA_APPEND(self->stack, Py_None,-1);
4807 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004808}
4809
4810static int
4811noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004812
Guido van Rossum053b8df1998-11-25 16:18:00 +00004813 if (self->stack->length < 1) return stackUnderflow();
4814 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004815 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004816}
4817
Tim Peters2d629652003-02-04 05:06:17 +00004818static int
4819noload_extension(Unpicklerobject *self, int nbytes)
4820{
4821 char *codebytes;
4822
4823 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4824 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4825 PDATA_APPEND(self->stack, Py_None, -1);
4826 return 0;
4827}
4828
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004829
4830static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004831noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004832{
4833 PyObject *err = 0, *val = 0;
4834 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004835
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004836 self->num_marks = 0;
4837 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004838
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004839 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004840 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004841 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004842
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004843 switch (s[0]) {
4844 case NONE:
4845 if (load_none(self) < 0)
4846 break;
4847 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004848
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004849 case BININT:
4850 if (load_binint(self) < 0)
4851 break;
4852 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004853
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004854 case BININT1:
4855 if (load_binint1(self) < 0)
4856 break;
4857 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004858
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004859 case BININT2:
4860 if (load_binint2(self) < 0)
4861 break;
4862 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004863
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004864 case INT:
4865 if (load_int(self) < 0)
4866 break;
4867 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004868
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004869 case LONG:
4870 if (load_long(self) < 0)
4871 break;
4872 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004873
Tim Peters4190fb82003-02-02 16:09:05 +00004874 case LONG1:
4875 if (load_counted_long(self, 1) < 0)
4876 break;
4877 continue;
4878
4879 case LONG4:
4880 if (load_counted_long(self, 4) < 0)
4881 break;
4882 continue;
4883
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004884 case FLOAT:
4885 if (load_float(self) < 0)
4886 break;
4887 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004888
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004889 case BINFLOAT:
4890 if (load_binfloat(self) < 0)
4891 break;
4892 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004893
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004894 case BINSTRING:
4895 if (load_binstring(self) < 0)
4896 break;
4897 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004898
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004899 case SHORT_BINSTRING:
4900 if (load_short_binstring(self) < 0)
4901 break;
4902 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004903
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004904 case STRING:
4905 if (load_string(self) < 0)
4906 break;
4907 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004908
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004909#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004910 case UNICODE:
4911 if (load_unicode(self) < 0)
4912 break;
4913 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004914
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004915 case BINUNICODE:
4916 if (load_binunicode(self) < 0)
4917 break;
4918 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004919#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004920
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004921 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004922 if (load_counted_tuple(self, 0) < 0)
4923 break;
4924 continue;
4925
4926 case TUPLE1:
4927 if (load_counted_tuple(self, 1) < 0)
4928 break;
4929 continue;
4930
4931 case TUPLE2:
4932 if (load_counted_tuple(self, 2) < 0)
4933 break;
4934 continue;
4935
4936 case TUPLE3:
4937 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004938 break;
4939 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004940
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004941 case TUPLE:
4942 if (load_tuple(self) < 0)
4943 break;
4944 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004945
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004946 case EMPTY_LIST:
4947 if (load_empty_list(self) < 0)
4948 break;
4949 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004950
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004951 case LIST:
4952 if (load_list(self) < 0)
4953 break;
4954 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004955
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004956 case EMPTY_DICT:
4957 if (load_empty_dict(self) < 0)
4958 break;
4959 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004960
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004961 case DICT:
4962 if (load_dict(self) < 0)
4963 break;
4964 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004965
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004966 case OBJ:
4967 if (noload_obj(self) < 0)
4968 break;
4969 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004970
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004971 case INST:
4972 if (noload_inst(self) < 0)
4973 break;
4974 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004975
Tim Peterseab7db32003-02-13 18:24:14 +00004976 case NEWOBJ:
4977 if (noload_newobj(self) < 0)
4978 break;
4979 continue;
4980
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004981 case GLOBAL:
4982 if (noload_global(self) < 0)
4983 break;
4984 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004985
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004986 case APPEND:
4987 if (load_append(self) < 0)
4988 break;
4989 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004990
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004991 case APPENDS:
4992 if (load_appends(self) < 0)
4993 break;
4994 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004995
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004996 case BUILD:
4997 if (noload_build(self) < 0)
4998 break;
4999 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005000
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005001 case DUP:
5002 if (load_dup(self) < 0)
5003 break;
5004 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005005
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005006 case BINGET:
5007 if (load_binget(self) < 0)
5008 break;
5009 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005010
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005011 case LONG_BINGET:
5012 if (load_long_binget(self) < 0)
5013 break;
5014 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005015
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005016 case GET:
5017 if (load_get(self) < 0)
5018 break;
5019 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005020
Tim Peters2d629652003-02-04 05:06:17 +00005021 case EXT1:
5022 if (noload_extension(self, 1) < 0)
5023 break;
5024 continue;
5025
5026 case EXT2:
5027 if (noload_extension(self, 2) < 0)
5028 break;
5029 continue;
5030
5031 case EXT4:
5032 if (noload_extension(self, 4) < 0)
5033 break;
5034 continue;
5035
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005036 case MARK:
5037 if (load_mark(self) < 0)
5038 break;
5039 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005040
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005041 case BINPUT:
5042 if (load_binput(self) < 0)
5043 break;
5044 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005045
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005046 case LONG_BINPUT:
5047 if (load_long_binput(self) < 0)
5048 break;
5049 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005050
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005051 case PUT:
5052 if (load_put(self) < 0)
5053 break;
5054 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005055
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005056 case POP:
5057 if (load_pop(self) < 0)
5058 break;
5059 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005060
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005061 case POP_MARK:
5062 if (load_pop_mark(self) < 0)
5063 break;
5064 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005065
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005066 case SETITEM:
5067 if (load_setitem(self) < 0)
5068 break;
5069 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005070
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005071 case SETITEMS:
5072 if (load_setitems(self) < 0)
5073 break;
5074 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005075
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005076 case STOP:
5077 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005078
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005079 case PERSID:
5080 if (load_persid(self) < 0)
5081 break;
5082 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005083
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005084 case BINPERSID:
5085 if (load_binpersid(self) < 0)
5086 break;
5087 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005088
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005089 case REDUCE:
5090 if (noload_reduce(self) < 0)
5091 break;
5092 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005093
Tim Peters4190fb82003-02-02 16:09:05 +00005094 case PROTO:
5095 if (load_proto(self) < 0)
5096 break;
5097 continue;
5098
Tim Peters3c67d792003-02-02 17:59:11 +00005099 case NEWTRUE:
5100 if (load_bool(self, Py_True) < 0)
5101 break;
5102 continue;
5103
5104 case NEWFALSE:
5105 if (load_bool(self, Py_False) < 0)
5106 break;
5107 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005108 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005109 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005110 "invalid load key, '%s'.",
5111 "c", s[0]);
5112 return NULL;
5113 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005114
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005115 break;
5116 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005117
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005118 if ((err = PyErr_Occurred())) {
5119 if (err == PyExc_EOFError) {
5120 PyErr_SetNone(PyExc_EOFError);
5121 }
5122 return NULL;
5123 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005124
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005125 PDATA_POP(self->stack, val);
5126 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005127}
Tim Peters84e87f32001-03-17 04:50:51 +00005128
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005129
Guido van Rossum60456fd1997-04-09 17:36:32 +00005130static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005131Unpickler_load(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005132{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005133 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005134}
5135
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005136static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005137Unpickler_noload(Unpicklerobject *self, PyObject *unused)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005138{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005139 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005140}
5141
Guido van Rossum60456fd1997-04-09 17:36:32 +00005142
5143static struct PyMethodDef Unpickler_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +00005144 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005145 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005146 },
Georg Brandl96a8c392006-05-29 21:04:52 +00005147 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005148 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005149 "noload() -- not load a pickle, but go through most of the motions\n"
5150 "\n"
5151 "This function can be used to read past a pickle without instantiating\n"
5152 "any objects or importing any modules. It can also be used to find all\n"
5153 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005154 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005155 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005156 {NULL, NULL} /* sentinel */
5157};
5158
5159
5160static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005161newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005162{
5163 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005164
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005165 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005166 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005167
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005168 self->file = NULL;
5169 self->arg = NULL;
5170 self->stack = (Pdata*)Pdata_New();
5171 self->pers_func = NULL;
5172 self->last_string = NULL;
5173 self->marks = NULL;
5174 self->num_marks = 0;
5175 self->marks_size = 0;
5176 self->buf_size = 0;
5177 self->read = NULL;
5178 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005179 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005180
Tim Peterscba30e22003-02-01 06:24:36 +00005181 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005182 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005183
Neal Norwitzb59d08c2006-07-22 16:20:49 +00005184 if (!self->stack)
5185 goto err;
5186
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005187 Py_INCREF(f);
5188 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005189
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005190 /* Set read, readline based on type of f */
5191 if (PyFile_Check(f)) {
5192 self->fp = PyFile_AsFile(f);
5193 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005194 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005195 "I/O operation on closed file");
5196 goto err;
5197 }
5198 self->read_func = read_file;
5199 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005200 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005201 else if (PycStringIO_InputCheck(f)) {
5202 self->fp = NULL;
5203 self->read_func = read_cStringIO;
5204 self->readline_func = readline_cStringIO;
5205 }
5206 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005207
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005208 self->fp = NULL;
5209 self->read_func = read_other;
5210 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005211
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005212 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5213 (self->read = PyObject_GetAttr(f, read_str)))) {
5214 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005215 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005216 "argument must have 'read' and "
5217 "'readline' attributes" );
5218 goto err;
5219 }
5220 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005221 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005222
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005223 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005224
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005225 err:
5226 Py_DECREF((PyObject *)self);
5227 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005228}
5229
5230
5231static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005232get_Unpickler(PyObject *self, PyObject *file)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005233{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005234 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005235}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005236
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005237
Guido van Rossum60456fd1997-04-09 17:36:32 +00005238static void
Tim Peterscba30e22003-02-01 06:24:36 +00005239Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005240{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005241 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005242 Py_XDECREF(self->readline);
5243 Py_XDECREF(self->read);
5244 Py_XDECREF(self->file);
5245 Py_XDECREF(self->memo);
5246 Py_XDECREF(self->stack);
5247 Py_XDECREF(self->pers_func);
5248 Py_XDECREF(self->arg);
5249 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005250 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005251
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005252 if (self->marks) {
5253 free(self->marks);
5254 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005255
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005256 if (self->buf_size) {
5257 free(self->buf);
5258 }
Tim Peters84e87f32001-03-17 04:50:51 +00005259
Christian Heimese93237d2007-12-19 02:37:44 +00005260 Py_TYPE(self)->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005261}
5262
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005263static int
5264Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5265{
Thomas Woutersc6e55062006-04-15 21:47:09 +00005266 Py_VISIT(self->readline);
5267 Py_VISIT(self->read);
5268 Py_VISIT(self->file);
5269 Py_VISIT(self->memo);
5270 Py_VISIT(self->stack);
5271 Py_VISIT(self->pers_func);
5272 Py_VISIT(self->arg);
5273 Py_VISIT(self->last_string);
5274 Py_VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005275 return 0;
5276}
5277
5278static int
5279Unpickler_clear(Unpicklerobject *self)
5280{
Thomas Woutersedf17d82006-04-15 17:28:34 +00005281 Py_CLEAR(self->readline);
5282 Py_CLEAR(self->read);
5283 Py_CLEAR(self->file);
5284 Py_CLEAR(self->memo);
5285 Py_CLEAR(self->stack);
5286 Py_CLEAR(self->pers_func);
5287 Py_CLEAR(self->arg);
5288 Py_CLEAR(self->last_string);
5289 Py_CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005290 return 0;
5291}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005292
5293static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005294Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005295{
5296 if (!strcmp(name, "persistent_load")) {
5297 if (!self->pers_func) {
5298 PyErr_SetString(PyExc_AttributeError, name);
5299 return NULL;
5300 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005302 Py_INCREF(self->pers_func);
5303 return self->pers_func;
5304 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005305
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005306 if (!strcmp(name, "find_global")) {
5307 if (!self->find_class) {
5308 PyErr_SetString(PyExc_AttributeError, name);
5309 return NULL;
5310 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005311
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005312 Py_INCREF(self->find_class);
5313 return self->find_class;
5314 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005315
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005316 if (!strcmp(name, "memo")) {
5317 if (!self->memo) {
5318 PyErr_SetString(PyExc_AttributeError, name);
5319 return NULL;
5320 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005321
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005322 Py_INCREF(self->memo);
5323 return self->memo;
5324 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005326 if (!strcmp(name, "UnpicklingError")) {
5327 Py_INCREF(UnpicklingError);
5328 return UnpicklingError;
5329 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005330
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005331 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005332}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005333
Guido van Rossum60456fd1997-04-09 17:36:32 +00005334
5335static int
Tim Peterscba30e22003-02-01 06:24:36 +00005336Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005337{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005338
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005339 if (!strcmp(name, "persistent_load")) {
5340 Py_XDECREF(self->pers_func);
5341 self->pers_func = value;
5342 Py_XINCREF(value);
5343 return 0;
5344 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005345
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005346 if (!strcmp(name, "find_global")) {
5347 Py_XDECREF(self->find_class);
5348 self->find_class = value;
5349 Py_XINCREF(value);
5350 return 0;
5351 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005352
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005353 if (! value) {
5354 PyErr_SetString(PyExc_TypeError,
5355 "attribute deletion is not supported");
5356 return -1;
5357 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005358
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005359 if (strcmp(name, "memo") == 0) {
5360 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005361 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005362 "memo must be a dictionary");
5363 return -1;
5364 }
5365 Py_XDECREF(self->memo);
5366 self->memo = value;
5367 Py_INCREF(value);
5368 return 0;
5369 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005370
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005371 PyErr_SetString(PyExc_AttributeError, name);
5372 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005373}
5374
Tim Peters5bd2a792003-02-01 16:45:06 +00005375/* ---------------------------------------------------------------------------
5376 * Module-level functions.
5377 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005378
Martin v. Löwis544f1192004-07-27 05:22:33 +00005379/* dump(obj, file, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005380static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005381cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005382{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005383 static char *kwlist[] = {"obj", "file", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005384 PyObject *ob, *file, *res = NULL;
5385 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005386 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005387
Martin v. Löwis544f1192004-07-27 05:22:33 +00005388 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5389 &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005390 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005391
Tim Peters5bd2a792003-02-01 16:45:06 +00005392 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005393 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005394
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005395 if (dump(pickler, ob) < 0)
5396 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005397
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005398 Py_INCREF(Py_None);
5399 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005400
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005401 finally:
5402 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005403
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005404 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005405}
5406
5407
Martin v. Löwis544f1192004-07-27 05:22:33 +00005408/* dumps(obj, protocol=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005409static PyObject *
Martin v. Löwis544f1192004-07-27 05:22:33 +00005410cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005411{
Martin v. Löwis15e62742006-02-27 16:46:16 +00005412 static char *kwlist[] = {"obj", "protocol", NULL};
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005413 PyObject *ob, *file = 0, *res = NULL;
5414 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005415 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005416
Martin v. Löwis544f1192004-07-27 05:22:33 +00005417 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5418 &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005419 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005420
Tim Peterscba30e22003-02-01 06:24:36 +00005421 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005422 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005423
Tim Peters5bd2a792003-02-01 16:45:06 +00005424 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005425 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005426
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005427 if (dump(pickler, ob) < 0)
5428 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005429
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005430 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005431
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005432 finally:
5433 Py_XDECREF(pickler);
5434 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005435
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005436 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005437}
5438
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005439
Tim Peters5bd2a792003-02-01 16:45:06 +00005440/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005441static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +00005442cpm_load(PyObject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005443{
5444 Unpicklerobject *unpickler = 0;
Georg Brandl96a8c392006-05-29 21:04:52 +00005445 PyObject *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005446
Tim Peterscba30e22003-02-01 06:24:36 +00005447 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005448 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005449
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005450 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005451
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005452 finally:
5453 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005454
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005455 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005456}
5457
5458
Tim Peters5bd2a792003-02-01 16:45:06 +00005459/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005460static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005461cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005462{
5463 PyObject *ob, *file = 0, *res = NULL;
5464 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005465
Tim Peterscba30e22003-02-01 06:24:36 +00005466 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005467 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005468
Tim Peterscba30e22003-02-01 06:24:36 +00005469 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005470 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005471
Tim Peterscba30e22003-02-01 06:24:36 +00005472 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005473 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005474
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005475 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005477 finally:
5478 Py_XDECREF(file);
5479 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005480
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005481 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005482}
5483
5484
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005485PyDoc_STRVAR(Unpicklertype__doc__,
5486"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005487
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005488static PyTypeObject Unpicklertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00005489 PyVarObject_HEAD_INIT(NULL, 0)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005490 "cPickle.Unpickler", /*tp_name*/
5491 sizeof(Unpicklerobject), /*tp_basicsize*/
5492 0,
5493 (destructor)Unpickler_dealloc, /* tp_dealloc */
5494 0, /* tp_print */
5495 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5496 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5497 0, /* tp_compare */
5498 0, /* tp_repr */
5499 0, /* tp_as_number */
5500 0, /* tp_as_sequence */
5501 0, /* tp_as_mapping */
5502 0, /* tp_hash */
5503 0, /* tp_call */
5504 0, /* tp_str */
5505 0, /* tp_getattro */
5506 0, /* tp_setattro */
5507 0, /* tp_as_buffer */
5508 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5509 Unpicklertype__doc__, /* tp_doc */
5510 (traverseproc)Unpickler_traverse, /* tp_traverse */
5511 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005512};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005513
Guido van Rossum60456fd1997-04-09 17:36:32 +00005514static struct PyMethodDef cPickle_methods[] = {
Martin v. Löwis544f1192004-07-27 05:22:33 +00005515 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5516 PyDoc_STR("dump(obj, file, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005517 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005518 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005519 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005520 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005521
Martin v. Löwis544f1192004-07-27 05:22:33 +00005522 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5523 PyDoc_STR("dumps(obj, protocol=0) -- "
Tim Peters5bd2a792003-02-01 16:45:06 +00005524 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005525 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005526 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005527 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005528
Georg Brandl96a8c392006-05-29 21:04:52 +00005529 {"load", (PyCFunction)cpm_load, METH_O,
Neal Norwitz200788c2002-08-13 22:20:41 +00005530 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005531
Neal Norwitzb0493252002-03-31 14:44:22 +00005532 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005533 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005534
Martin v. Löwis544f1192004-07-27 05:22:33 +00005535 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5536 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005537 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005538 "This takes a file-like object for writing a pickle data stream.\n"
5539 "The optional proto argument tells the pickler to use the given\n"
5540 "protocol; supported protocols are 0, 1, 2. The default\n"
5541 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5542 "only protocol that can be written to a file opened in text\n"
5543 "mode and read back successfully. When using a protocol higher\n"
5544 "than 0, make sure the file is opened in binary mode, both when\n"
5545 "pickling and unpickling.)\n"
5546 "\n"
5547 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5548 "more efficient than protocol 1.\n"
5549 "\n"
5550 "Specifying a negative protocol version selects the highest\n"
5551 "protocol version supported. The higher the protocol used, the\n"
5552 "more recent the version of Python needed to read the pickle\n"
5553 "produced.\n"
5554 "\n"
5555 "The file parameter must have a write() method that accepts a single\n"
5556 "string argument. It can thus be an open file object, a StringIO\n"
5557 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005558 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005559
Georg Brandl96a8c392006-05-29 21:04:52 +00005560 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
Tim Peters5bd2a792003-02-01 16:45:06 +00005561 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5562
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005563 { NULL, NULL }
5564};
5565
Guido van Rossum60456fd1997-04-09 17:36:32 +00005566static int
Tim Peterscba30e22003-02-01 06:24:36 +00005567init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005568{
5569 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005570
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005571#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005572
Tim Peters3cfe7542003-05-21 21:29:48 +00005573 if (PyType_Ready(&Unpicklertype) < 0)
5574 return -1;
5575 if (PyType_Ready(&Picklertype) < 0)
5576 return -1;
5577
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005578 INIT_STR(__class__);
5579 INIT_STR(__getinitargs__);
5580 INIT_STR(__dict__);
5581 INIT_STR(__getstate__);
5582 INIT_STR(__setstate__);
5583 INIT_STR(__name__);
5584 INIT_STR(__main__);
5585 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005586 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005587 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005588 INIT_STR(append);
5589 INIT_STR(read);
5590 INIT_STR(readline);
5591 INIT_STR(copy_reg);
5592 INIT_STR(dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005593
Tim Peterscba30e22003-02-01 06:24:36 +00005594 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005595 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005596
Tim Peters1f1b2d22003-02-01 02:16:37 +00005597 /* This is special because we want to use a different
5598 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005599 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005600 if (!dispatch_table) return -1;
5601
5602 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005603 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005604 if (!extension_registry) return -1;
5605
5606 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005607 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005608 if (!inverted_registry) return -1;
5609
5610 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005611 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005612 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005614 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005615
Tim Peters731098b2003-02-04 20:56:09 +00005616 if (!(empty_tuple = PyTuple_New(0)))
5617 return -1;
5618
5619 two_tuple = PyTuple_New(2);
5620 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005621 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005622 /* We use this temp container with no regard to refcounts, or to
5623 * keeping containees alive. Exempt from GC, because we don't
5624 * want anything looking at two_tuple() by magic.
5625 */
5626 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005628 /* Ugh */
5629 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5630 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5631 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005632
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005633 if (!( t=PyDict_New())) return -1;
5634 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005635 "def __str__(self):\n"
5636 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5637 Py_file_input,
5638 module_dict, t) )) return -1;
5639 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005640
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005641 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005642 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005643 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005644
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005645 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005646
Tim Peterscba30e22003-02-01 06:24:36 +00005647 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005648 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005649 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005650 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005651
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005652 if (!( t=PyDict_New())) return -1;
5653 if (!( r=PyRun_String(
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005654 "def __str__(self):\n"
5655 " a=self.args\n"
5656 " a=a and type(a[0]) or '(what)'\n"
5657 " return 'Cannot pickle %s objects' % a\n"
5658 , Py_file_input,
5659 module_dict, t) )) return -1;
5660 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005661
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005662 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005663 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005664 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005665
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005666 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005668 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005669 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005670 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005671
Martin v. Löwis658009a2002-09-16 17:26:24 +00005672 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5673 UnpicklingError, NULL)))
5674 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005675
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005676 if (PyDict_SetItemString(module_dict, "PickleError",
5677 PickleError) < 0)
5678 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005679
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005680 if (PyDict_SetItemString(module_dict, "PicklingError",
5681 PicklingError) < 0)
5682 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005683
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005684 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5685 UnpicklingError) < 0)
5686 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005687
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005688 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5689 UnpickleableError) < 0)
5690 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005691
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005692 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5693 BadPickleGet) < 0)
5694 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005696 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005697
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005698 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005699}
5700
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005701#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5702#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005703#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005704PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005705initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005706{
5707 PyObject *m, *d, *di, *v, *k;
Martin v. Löwis18e16552006-02-15 17:27:45 +00005708 Py_ssize_t i;
Tim Peters5b7da392003-02-04 00:21:07 +00005709 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005710 PyObject *format_version;
5711 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005712
Christian Heimese93237d2007-12-19 02:37:44 +00005713 Py_TYPE(&Picklertype) = &PyType_Type;
5714 Py_TYPE(&Unpicklertype) = &PyType_Type;
5715 Py_TYPE(&PdataType) = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005716
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005717 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005718 * so we're forced to use a temporary dictionary. :(
5719 */
5720 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005721 if (!di) return;
5722 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005723
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005724 /* Create the module and add the functions */
5725 m = Py_InitModule4("cPickle", cPickle_methods,
5726 cPickle_module_documentation,
5727 (PyObject*)NULL,PYTHON_API_VERSION);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00005728 if (m == NULL)
5729 return;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005730
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005731 /* Add some symbolic constants to the module */
5732 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005733 v = PyString_FromString(rev);
5734 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005735 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005736
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005737 /* Copy data from di. Waaa. */
5738 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5739 if (PyObject_SetItem(d, k, v) < 0) {
5740 Py_DECREF(di);
5741 return;
5742 }
5743 }
5744 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005745
Tim Peters8587b3c2003-02-13 15:44:41 +00005746 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5747 if (i < 0)
5748 return;
5749
Tim Peters5b7da392003-02-04 00:21:07 +00005750 /* These are purely informational; no code uses them. */
5751 /* File format version we write. */
5752 format_version = PyString_FromString("2.0");
5753 /* Format versions we can read. */
5754 compatible_formats = Py_BuildValue("[sssss]",
5755 "1.0", /* Original protocol 0 */
5756 "1.1", /* Protocol 0 + INST */
5757 "1.2", /* Original protocol 1 */
5758 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005759 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005760 PyDict_SetItemString(d, "format_version", format_version);
5761 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5762 Py_XDECREF(format_version);
5763 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005764}