blob: 3f3d82f1ff5672f7eda3b9ef0e843892b8c72a92 [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
Tim Peters71fcda52003-02-14 23:05:28 +0000122/* object.__reduce__, the default reduce callable. */
123PyObject *object_reduce;
124
125/* copy_reg._better_reduce, the protocol 2 reduction function. */
126PyObject *better_reduce;
127
Guido van Rossum60456fd1997-04-09 17:36:32 +0000128static PyObject *__class___str, *__getinitargs___str, *__dict___str,
129 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000130 *write_str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000131 *read_str, *readline_str, *__main___str, *__basicnew___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000132 *copy_reg_str, *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000133
Guido van Rossum053b8df1998-11-25 16:18:00 +0000134/*************************************************************************
135 Internal Data type for pickle data. */
136
137typedef struct {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000138 PyObject_HEAD
Tim Peters1d63c9f2003-02-02 20:29:39 +0000139 int length; /* number of initial slots in data currently used */
140 int size; /* number of slots in data allocated */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000141 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000142} Pdata;
143
Tim Peters84e87f32001-03-17 04:50:51 +0000144static void
Tim Peterscba30e22003-02-01 06:24:36 +0000145Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000146{
147 int i;
148 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000149
Tim Peters1d63c9f2003-02-02 20:29:39 +0000150 for (i = self->length, p = self->data; --i >= 0; p++) {
151 Py_DECREF(*p);
152 }
153 if (self->data)
154 free(self->data);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000155 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000156}
157
158static PyTypeObject PdataType = {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000159 PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0,
160 (destructor)Pdata_dealloc,
161 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000162};
163
164#define Pdata_Check(O) ((O)->ob_type == &PdataType)
165
166static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000167Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000168{
169 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000170
Tim Peters1d63c9f2003-02-02 20:29:39 +0000171 if (!(self = PyObject_New(Pdata, &PdataType)))
172 return NULL;
173 self->size = 8;
174 self->length = 0;
175 self->data = malloc(self->size * sizeof(PyObject*));
176 if (self->data)
177 return (PyObject*)self;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000178 Py_DECREF(self);
179 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000180}
181
Tim Peters84e87f32001-03-17 04:50:51 +0000182static int
Tim Peterscba30e22003-02-01 06:24:36 +0000183stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000184{
185 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
186 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000187}
188
Tim Peters1d63c9f2003-02-02 20:29:39 +0000189/* Retain only the initial clearto items. If clearto >= the current
190 * number of items, this is a (non-erroneous) NOP.
191 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000192static int
Tim Peterscba30e22003-02-01 06:24:36 +0000193Pdata_clear(Pdata *self, int clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000194{
195 int i;
196 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000197
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000198 if (clearto < 0) return stackUnderflow();
199 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000200
Tim Peters1d63c9f2003-02-02 20:29:39 +0000201 for (i = self->length, p = self->data + clearto;
202 --i >= clearto;
203 p++) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000204 Py_DECREF(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000205 }
206 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000207
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000208 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000209}
210
Tim Peters84e87f32001-03-17 04:50:51 +0000211static int
Tim Peterscba30e22003-02-01 06:24:36 +0000212Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000213{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000214 int bigger;
215 size_t nbytes;
216
Tim Peters1d63c9f2003-02-02 20:29:39 +0000217 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000218 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000219 goto nomemory;
220 if ((int)(size_t)bigger != bigger)
221 goto nomemory;
222 nbytes = (size_t)bigger * sizeof(PyObject *);
223 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
224 goto nomemory;
225 self->data = realloc(self->data, nbytes);
226 if (self->data == NULL)
227 goto nomemory;
228 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000229 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000230
231 nomemory:
232 self->size = 0;
233 PyErr_NoMemory();
234 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000235}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000236
Tim Peterse0a39072003-02-03 15:45:56 +0000237/* D is a Pdata*. Pop the topmost element and store it into V, which
238 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000239 * is raised and V is set to NULL. D and V may be evaluated several times.
240 */
241#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000242 if ((D)->length) \
243 (V) = (D)->data[--((D)->length)]; \
244 else { \
245 PyErr_SetString(UnpicklingError, "bad pickle data"); \
246 (V) = NULL; \
247 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000248}
249
Tim Peterse0a39072003-02-03 15:45:56 +0000250/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
251 * D. If the Pdata stack can't be grown to hold the new value, both
252 * raise MemoryError and execute "return ER". The difference is in ownership
253 * of O after: _PUSH transfers ownership of O from the caller to the stack
254 * (no incref of O is done, and in case of error O is decrefed), while
255 * _APPEND pushes a new reference.
256 */
257
258/* Push O on stack D, giving ownership of O to the stack. */
259#define PDATA_PUSH(D, O, ER) { \
260 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
261 Pdata_grow((Pdata*)(D)) < 0) { \
262 Py_DECREF(O); \
263 return ER; \
264 } \
265 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
266}
267
268/* Push O on stack D, pushing a new reference. */
269#define PDATA_APPEND(D, O, ER) { \
270 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
271 Pdata_grow((Pdata*)(D)) < 0) \
272 return ER; \
273 Py_INCREF(O); \
274 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
275}
276
277
Guido van Rossum053b8df1998-11-25 16:18:00 +0000278static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000279Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000280{
281 PyObject *r;
282 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000283
Tim Peters1d63c9f2003-02-02 20:29:39 +0000284 l = self->length-start;
285 r = PyTuple_New(l);
286 if (r == NULL)
287 return NULL;
288 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000289 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000290
Tim Peters1d63c9f2003-02-02 20:29:39 +0000291 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000292 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000293}
294
295static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000296Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000297{
298 PyObject *r;
299 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000301 l=self->length-start;
302 if (!( r=PyList_New(l))) return NULL;
303 for (i=start, j=0 ; j < l; i++, j++)
304 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000305
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000306 self->length=start;
307 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000308}
309
Guido van Rossum053b8df1998-11-25 16:18:00 +0000310/*************************************************************************/
311
312#define ARG_TUP(self, o) { \
313 if (self->arg || (self->arg=PyTuple_New(1))) { \
314 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
315 PyTuple_SET_ITEM(self->arg,0,o); \
316 } \
317 else { \
318 Py_DECREF(o); \
319 } \
320}
321
322#define FREE_ARG_TUP(self) { \
323 if (self->arg->ob_refcnt > 1) { \
324 Py_DECREF(self->arg); \
325 self->arg=NULL; \
326 } \
327 }
328
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000329typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000330 PyObject_HEAD
331 FILE *fp;
332 PyObject *write;
333 PyObject *file;
334 PyObject *memo;
335 PyObject *arg;
336 PyObject *pers_func;
337 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000338
339 /* pickle protocol number, >= 0 */
340 int proto;
341
342 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000343 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000344
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000345 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis5a395302002-08-04 08:20:23 +0000346 int nesting;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000347 int (*write_func)(struct Picklerobject *, char *, int);
348 char *write_buf;
349 int buf_size;
350 PyObject *dispatch_table;
351 int fast_container; /* count nested container dumps */
352 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000353} Picklerobject;
354
Barry Warsaw52acb492001-12-21 20:04:22 +0000355#ifndef PY_CPICKLE_FAST_LIMIT
356#define PY_CPICKLE_FAST_LIMIT 50
357#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000358
Jeremy Hylton938ace62002-07-17 16:30:39 +0000359static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000360
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000361typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000362 PyObject_HEAD
363 FILE *fp;
364 PyObject *file;
365 PyObject *readline;
366 PyObject *read;
367 PyObject *memo;
368 PyObject *arg;
369 Pdata *stack;
370 PyObject *mark;
371 PyObject *pers_func;
372 PyObject *last_string;
373 int *marks;
374 int num_marks;
375 int marks_size;
376 int (*read_func)(struct Unpicklerobject *, char **, int);
377 int (*readline_func)(struct Unpicklerobject *, char **);
378 int buf_size;
379 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000380 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000381} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000382
Jeremy Hylton938ace62002-07-17 16:30:39 +0000383static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000384
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000385/* Forward decls that need the above structs */
386static int save(Picklerobject *, PyObject *, int);
387static int put2(Picklerobject *, PyObject *);
388
Guido van Rossumd385d591997-04-09 17:47:47 +0000389static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000390PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000391cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
392{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000393 va_list va;
394 PyObject *args=0, *retval=0;
395 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000396
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000397 if (format) args = Py_VaBuildValue(format, va);
398 va_end(va);
399 if (format && ! args) return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000400 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000401 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000402
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000403 if (retval) {
404 if (args) {
405 PyObject *v;
406 v=PyString_Format(retval, args);
407 Py_DECREF(retval);
408 Py_DECREF(args);
409 if (! v) return NULL;
410 retval=v;
411 }
412 }
413 else
414 if (args) retval=args;
415 else {
416 PyErr_SetObject(ErrType,Py_None);
417 return NULL;
418 }
419 PyErr_SetObject(ErrType,retval);
420 Py_DECREF(retval);
421 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000422}
423
Tim Peters84e87f32001-03-17 04:50:51 +0000424static int
Tim Peterscba30e22003-02-01 06:24:36 +0000425write_file(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000426{
427 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000428
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000429 if (s == NULL) {
430 return 0;
431 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000432
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000433 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000434 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000435 Py_END_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000436 if (nbyteswritten != (size_t)n) {
437 PyErr_SetFromErrno(PyExc_IOError);
438 return -1;
439 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000440
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000441 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000442}
443
Tim Peters84e87f32001-03-17 04:50:51 +0000444static int
Tim Peterscba30e22003-02-01 06:24:36 +0000445write_cStringIO(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000446{
447 if (s == NULL) {
448 return 0;
449 }
Tim Peterscba30e22003-02-01 06:24:36 +0000450
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000451 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
452 return -1;
453 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000454
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000455 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000456}
457
Tim Peters84e87f32001-03-17 04:50:51 +0000458static int
Tim Peterscba30e22003-02-01 06:24:36 +0000459write_none(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000460{
461 if (s == NULL) return 0;
462 return n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000463}
464
Tim Peters84e87f32001-03-17 04:50:51 +0000465static int
Tim Peterscba30e22003-02-01 06:24:36 +0000466write_other(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000467{
468 PyObject *py_str = 0, *junk = 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000469
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000470 if (s == NULL) {
471 if (!( self->buf_size )) return 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000472 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000473 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000474 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000475 return -1;
476 }
477 else {
478 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
479 if (write_other(self, NULL, 0) < 0)
480 return -1;
481 }
Tim Peterscba30e22003-02-01 06:24:36 +0000482
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000483 if (n > WRITE_BUF_SIZE) {
484 if (!( py_str =
Tim Peterscba30e22003-02-01 06:24:36 +0000485 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000486 return -1;
487 }
488 else {
489 memcpy(self->write_buf + self->buf_size, s, n);
490 self->buf_size += n;
491 return n;
492 }
493 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000494
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000495 if (self->write) {
496 /* object with write method */
497 ARG_TUP(self, py_str);
498 if (self->arg) {
499 junk = PyObject_Call(self->write, self->arg, NULL);
500 FREE_ARG_TUP(self);
501 }
502 if (junk) Py_DECREF(junk);
503 else return -1;
504 }
505 else
506 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000507
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000508 self->buf_size = 0;
509 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000510}
511
512
Tim Peters84e87f32001-03-17 04:50:51 +0000513static int
Tim Petersee1a53c2003-02-02 02:57:53 +0000514read_file(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000515{
516 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000517
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000518 if (self->buf_size == 0) {
519 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000520
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000521 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000522 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000523 PyErr_NoMemory();
524 return -1;
525 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000526
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000527 self->buf_size = size;
528 }
529 else if (n > self->buf_size) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000530 self->buf = (char *)realloc(self->buf, n);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000531 if (!self->buf) {
532 PyErr_NoMemory();
533 return -1;
534 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000535 self->buf_size = n;
536 }
Tim Peters84e87f32001-03-17 04:50:51 +0000537
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000538 Py_BEGIN_ALLOW_THREADS
539 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
540 Py_END_ALLOW_THREADS
541 if (nbytesread != (size_t)n) {
542 if (feof(self->fp)) {
543 PyErr_SetNone(PyExc_EOFError);
544 return -1;
545 }
Tim Peterscba30e22003-02-01 06:24:36 +0000546
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000547 PyErr_SetFromErrno(PyExc_IOError);
548 return -1;
549 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000550
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000551 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000552
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000553 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000554}
555
556
Tim Peters84e87f32001-03-17 04:50:51 +0000557static int
Tim Peterscba30e22003-02-01 06:24:36 +0000558readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000559{
560 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000561
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000562 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000563 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000564 PyErr_NoMemory();
565 return -1;
566 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000567 self->buf_size = 40;
568 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000569
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000570 i = 0;
571 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000572 int bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000573 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000574 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000575 (self->buf[i] = getc(self->fp)) == '\n') {
576 self->buf[i + 1] = '\0';
577 *s = self->buf;
578 return i + 1;
579 }
580 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000581 bigger = self->buf_size << 1;
582 if (bigger <= 0) { /* overflow */
583 PyErr_NoMemory();
584 return -1;
585 }
586 self->buf = (char *)realloc(self->buf, bigger);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000587 if (!self->buf) {
588 PyErr_NoMemory();
589 return -1;
590 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000591 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000592 }
Tim Peters84e87f32001-03-17 04:50:51 +0000593}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000594
595
Tim Peters84e87f32001-03-17 04:50:51 +0000596static int
Tim Peterscba30e22003-02-01 06:24:36 +0000597read_cStringIO(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000598{
599 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000600
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000601 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
602 PyErr_SetNone(PyExc_EOFError);
603 return -1;
604 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000605
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000606 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000607
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000608 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000609}
610
611
Tim Peters84e87f32001-03-17 04:50:51 +0000612static int
Tim Peterscba30e22003-02-01 06:24:36 +0000613readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000614{
615 int n;
616 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000617
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000618 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
619 return -1;
620 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000621
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000622 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000624 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000625}
626
627
Tim Peters84e87f32001-03-17 04:50:51 +0000628static int
Tim Peterscba30e22003-02-01 06:24:36 +0000629read_other(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000630{
631 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000632
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000633 if (!( bytes = PyInt_FromLong(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000634
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000635 ARG_TUP(self, bytes);
636 if (self->arg) {
637 str = PyObject_Call(self->read, self->arg, NULL);
638 FREE_ARG_TUP(self);
639 }
640 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000641
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000642 Py_XDECREF(self->last_string);
643 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000644
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000645 if (! (*s = PyString_AsString(str))) return -1;
646 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000647}
648
649
Tim Peters84e87f32001-03-17 04:50:51 +0000650static int
Tim Peterscba30e22003-02-01 06:24:36 +0000651readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000652{
653 PyObject *str;
654 int str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000655
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000656 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
657 return -1;
658 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000660 if ((str_size = PyString_Size(str)) < 0)
661 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000662
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000663 Py_XDECREF(self->last_string);
664 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000665
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000666 if (! (*s = PyString_AsString(str)))
667 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000669 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000670}
671
Tim Petersee1a53c2003-02-02 02:57:53 +0000672/* Copy the first n bytes from s into newly malloc'ed memory, plus a
673 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
674 * The caller is responsible for free()'ing the return value.
675 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000676static char *
Tim Petersee1a53c2003-02-02 02:57:53 +0000677pystrndup(char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000678{
Tim Petersee1a53c2003-02-02 02:57:53 +0000679 char *r = (char *)malloc(n+1);
680 if (r == NULL)
681 return (char*)PyErr_NoMemory();
682 memcpy(r, s, n);
683 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000684 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000685}
686
687
688static int
Tim Peterscba30e22003-02-01 06:24:36 +0000689get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000690{
691 PyObject *value, *mv;
692 long c_value;
693 char s[30];
694 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000696 if (!( mv = PyDict_GetItem(self->memo, id))) {
697 PyErr_SetObject(PyExc_KeyError, id);
698 return -1;
699 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000700
Tim Peterscba30e22003-02-01 06:24:36 +0000701 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000702 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000703
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000704 if (!( PyInt_Check(value))) {
705 PyErr_SetString(PicklingError, "no int where int expected in memo");
706 return -1;
707 }
708 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000709
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000710 if (!self->bin) {
711 s[0] = GET;
712 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
713 len = strlen(s);
714 }
715 else if (Pdata_Check(self->file)) {
716 if (write_other(self, NULL, 0) < 0) return -1;
717 PDATA_APPEND(self->file, mv, -1);
718 return 0;
719 }
720 else {
721 if (c_value < 256) {
722 s[0] = BINGET;
723 s[1] = (int)(c_value & 0xff);
724 len = 2;
725 }
726 else {
727 s[0] = LONG_BINGET;
728 s[1] = (int)(c_value & 0xff);
729 s[2] = (int)((c_value >> 8) & 0xff);
730 s[3] = (int)((c_value >> 16) & 0xff);
731 s[4] = (int)((c_value >> 24) & 0xff);
732 len = 5;
733 }
734 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000735
Tim Peters0bc93f52003-02-02 18:29:33 +0000736 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000737 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000738
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000739 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000740}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000741
Guido van Rossum60456fd1997-04-09 17:36:32 +0000742
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000743static int
Tim Peterscba30e22003-02-01 06:24:36 +0000744put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000745{
Tim Peterscba30e22003-02-01 06:24:36 +0000746 if (ob->ob_refcnt < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000747 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000748
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000749 return put2(self, ob);
750}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000751
Guido van Rossum053b8df1998-11-25 16:18:00 +0000752
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000753static int
Tim Peterscba30e22003-02-01 06:24:36 +0000754put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000755{
756 char c_str[30];
757 int p;
758 size_t len;
759 int res = -1;
760 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000761
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000762 if (self->fast)
763 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000764
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000765 if ((p = PyDict_Size(self->memo)) < 0)
766 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000767
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000768 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000769 /* XXX Why?
770 * XXX And does "positive" really mean non-negative?
771 * XXX pickle.py starts with PUT index 0, not 1. This makes for
772 * XXX gratuitous differences between the pickling modules.
773 */
Tim Peterscba30e22003-02-01 06:24:36 +0000774 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000775
Tim Peterscba30e22003-02-01 06:24:36 +0000776 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000777 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000778
Tim Peterscba30e22003-02-01 06:24:36 +0000779 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000780 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000781
Tim Peterscba30e22003-02-01 06:24:36 +0000782 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000783 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000784
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000785 PyTuple_SET_ITEM(t, 0, memo_len);
786 Py_INCREF(memo_len);
787 PyTuple_SET_ITEM(t, 1, ob);
788 Py_INCREF(ob);
789
790 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
791 goto finally;
792
793 if (!self->bin) {
794 c_str[0] = PUT;
795 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
796 len = strlen(c_str);
797 }
798 else if (Pdata_Check(self->file)) {
799 if (write_other(self, NULL, 0) < 0) return -1;
800 PDATA_APPEND(self->file, memo_len, -1);
801 res=0; /* Job well done ;) */
802 goto finally;
803 }
804 else {
805 if (p >= 256) {
806 c_str[0] = LONG_BINPUT;
807 c_str[1] = (int)(p & 0xff);
808 c_str[2] = (int)((p >> 8) & 0xff);
809 c_str[3] = (int)((p >> 16) & 0xff);
810 c_str[4] = (int)((p >> 24) & 0xff);
811 len = 5;
812 }
813 else {
814 c_str[0] = BINPUT;
815 c_str[1] = p;
816 len = 2;
817 }
818 }
819
Tim Peters0bc93f52003-02-02 18:29:33 +0000820 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000821 goto finally;
822
823 res = 0;
824
825 finally:
826 Py_XDECREF(py_ob_id);
827 Py_XDECREF(memo_len);
828 Py_XDECREF(t);
829
830 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000831}
832
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000833static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000834whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000835{
836 int i, j;
837 PyObject *module = 0, *modules_dict = 0,
838 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000839
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000840 module = PyObject_GetAttrString(global, "__module__");
841 if (module) return module;
842 PyErr_Clear();
Guido van Rossum45188231997-09-28 05:38:51 +0000843
Tim Peterscba30e22003-02-01 06:24:36 +0000844 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000845 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000846
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000847 i = 0;
848 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000849
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000850 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000851
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000852 global_name_attr = PyObject_GetAttr(module, global_name);
853 if (!global_name_attr) {
854 PyErr_Clear();
855 continue;
856 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000857
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000858 if (global_name_attr != global) {
859 Py_DECREF(global_name_attr);
860 continue;
861 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000862
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000863 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000864
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000865 break;
866 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000867
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000868 /* The following implements the rule in pickle.py added in 1.5
869 that used __main__ if no module is found. I don't actually
870 like this rule. jlf
871 */
872 if (!j) {
873 j=1;
874 name=__main___str;
875 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000877 Py_INCREF(name);
878 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000879}
880
881
Guido van Rossum60456fd1997-04-09 17:36:32 +0000882static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000883fast_save_enter(Picklerobject *self, PyObject *obj)
884{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000885 /* if fast_container < 0, we're doing an error exit. */
886 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
887 PyObject *key = NULL;
888 if (self->fast_memo == NULL) {
889 self->fast_memo = PyDict_New();
890 if (self->fast_memo == NULL) {
891 self->fast_container = -1;
892 return 0;
893 }
894 }
895 key = PyLong_FromVoidPtr(obj);
896 if (key == NULL)
897 return 0;
898 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000899 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000900 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000901 "fast mode: can't pickle cyclic objects "
902 "including object type %s at %p",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000903 obj->ob_type->tp_name, obj);
904 self->fast_container = -1;
905 return 0;
906 }
907 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000908 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000909 self->fast_container = -1;
910 return 0;
911 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000912 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000913 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000914 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000915}
916
Tim Peterscba30e22003-02-01 06:24:36 +0000917int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000918fast_save_leave(Picklerobject *self, PyObject *obj)
919{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000920 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
921 PyObject *key = PyLong_FromVoidPtr(obj);
922 if (key == NULL)
923 return 0;
924 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000925 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000926 return 0;
927 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000928 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000929 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000930 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000931}
932
933static int
Tim Peterscba30e22003-02-01 06:24:36 +0000934save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000935{
936 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000937 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000938 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000939
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000940 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000941}
942
Guido van Rossum77f6a652002-04-03 22:41:51 +0000943static int
Tim Peterscba30e22003-02-01 06:24:36 +0000944save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000945{
Guido van Rossume2763392002-04-05 19:30:08 +0000946 static char *buf[2] = {FALSE, TRUE};
947 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000948 long l = PyInt_AS_LONG((PyIntObject *)args);
949
Tim Peters3c67d792003-02-02 17:59:11 +0000950 if (self->proto >= 2) {
951 char opcode = l ? NEWTRUE : NEWFALSE;
952 if (self->write_func(self, &opcode, 1) < 0)
953 return -1;
954 }
955 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000956 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000957 return 0;
958}
Tim Peters84e87f32001-03-17 04:50:51 +0000959
Guido van Rossum60456fd1997-04-09 17:36:32 +0000960static int
Tim Peterscba30e22003-02-01 06:24:36 +0000961save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000962{
963 char c_str[32];
964 long l = PyInt_AS_LONG((PyIntObject *)args);
965 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000966
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000967 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000968#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000969 || l > 0x7fffffffL
970 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000971#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000972 ) {
973 /* Text-mode pickle, or long too big to fit in the 4-byte
974 * signed BININT format: store as a string.
975 */
976 c_str[0] = INT;
977 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000978 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000979 return -1;
980 }
981 else {
982 /* Binary pickle and l fits in a signed 4-byte int. */
983 c_str[1] = (int)( l & 0xff);
984 c_str[2] = (int)((l >> 8) & 0xff);
985 c_str[3] = (int)((l >> 16) & 0xff);
986 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000988 if ((c_str[4] == 0) && (c_str[3] == 0)) {
989 if (c_str[2] == 0) {
990 c_str[0] = BININT1;
991 len = 2;
992 }
993 else {
994 c_str[0] = BININT2;
995 len = 3;
996 }
997 }
998 else {
999 c_str[0] = BININT;
1000 len = 5;
1001 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001002
Tim Peters0bc93f52003-02-02 18:29:33 +00001003 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001004 return -1;
1005 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001006
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001007 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001008}
1009
1010
1011static int
Tim Peterscba30e22003-02-01 06:24:36 +00001012save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001013{
Tim Petersee1a53c2003-02-02 02:57:53 +00001014 int size;
1015 int res = -1;
1016 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001017
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001018 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001019
Tim Petersee1a53c2003-02-02 02:57:53 +00001020 if (self->proto >= 2) {
1021 /* Linear-time pickling. */
1022 size_t nbits;
1023 size_t nbytes;
1024 unsigned char *pdata;
1025 char c_str[5];
1026 int i;
1027 int sign = _PyLong_Sign(args);
1028
1029 if (sign == 0) {
1030 /* It's 0 -- an empty bytestring. */
1031 c_str[0] = LONG1;
1032 c_str[1] = 0;
1033 i = self->write_func(self, c_str, 2);
1034 if (i < 0) goto finally;
1035 res = 0;
1036 goto finally;
1037 }
1038 nbits = _PyLong_NumBits(args);
1039 if (nbits == (size_t)-1 && PyErr_Occurred())
1040 goto finally;
1041 /* How many bytes do we need? There are nbits >> 3 full
1042 * bytes of data, and nbits & 7 leftover bits. If there
1043 * are any leftover bits, then we clearly need another
1044 * byte. Wnat's not so obvious is that we *probably*
1045 * need another byte even if there aren't any leftovers:
1046 * the most-significant bit of the most-significant byte
1047 * acts like a sign bit, and it's usually got a sense
1048 * opposite of the one we need. The exception is longs
1049 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1050 * its own 256's-complement, so has the right sign bit
1051 * even without the extra byte. That's a pain to check
1052 * for in advance, though, so we always grab an extra
1053 * byte at the start, and cut it back later if possible.
1054 */
1055 nbytes = (nbits >> 3) + 1;
1056 if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) {
1057 PyErr_SetString(PyExc_OverflowError, "long too large "
1058 "to pickle");
1059 goto finally;
1060 }
1061 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1062 if (repr == NULL) goto finally;
1063 pdata = (unsigned char *)PyString_AS_STRING(repr);
1064 i = _PyLong_AsByteArray((PyLongObject *)args,
1065 pdata, nbytes,
1066 1 /* little endian */, 1 /* signed */);
1067 if (i < 0) goto finally;
1068 /* If the long is negative, this may be a byte more than
1069 * needed. This is so iff the MSB is all redundant sign
1070 * bits.
1071 */
1072 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1073 (pdata[nbytes - 2] & 0x80) != 0)
1074 --nbytes;
1075
1076 if (nbytes < 256) {
1077 c_str[0] = LONG1;
1078 c_str[1] = (char)nbytes;
1079 size = 2;
1080 }
1081 else {
1082 c_str[0] = LONG4;
1083 size = (int)nbytes;
1084 for (i = 1; i < 5; i++) {
1085 c_str[i] = (char)(size & 0xff);
1086 size >>= 8;
1087 }
1088 size = 5;
1089 }
1090 i = self->write_func(self, c_str, size);
1091 if (i < 0) goto finally;
1092 i = self->write_func(self, (char *)pdata, (int)nbytes);
1093 if (i < 0) goto finally;
1094 res = 0;
1095 goto finally;
1096 }
1097
1098 /* proto < 2: write the repr and newline. This is quadratic-time
1099 * (in the number of digits), in both directions.
1100 */
Tim Peterscba30e22003-02-01 06:24:36 +00001101 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001102 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001103
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001104 if ((size = PyString_Size(repr)) < 0)
1105 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001106
Tim Peters0bc93f52003-02-02 18:29:33 +00001107 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001108 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001109
Tim Peters0bc93f52003-02-02 18:29:33 +00001110 if (self->write_func(self,
1111 PyString_AS_STRING((PyStringObject *)repr),
1112 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001113 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001114
Tim Peters0bc93f52003-02-02 18:29:33 +00001115 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001116 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001117
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001118 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001119
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001120 finally:
1121 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001122 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001123}
1124
1125
1126static int
Tim Peterscba30e22003-02-01 06:24:36 +00001127save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001128{
1129 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001130
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001131 if (self->bin) {
1132 int s, e;
1133 double f;
1134 long fhi, flo;
1135 char str[9];
1136 unsigned char *p = (unsigned char *)str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001138 *p = BINFLOAT;
1139 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001140
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001141 if (x < 0) {
1142 s = 1;
1143 x = -x;
1144 }
1145 else
1146 s = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001147
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001148 f = frexp(x, &e);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001149
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001150 /* Normalize f to be in the range [1.0, 2.0) */
1151 if (0.5 <= f && f < 1.0) {
1152 f *= 2.0;
1153 e--;
1154 }
1155 else if (f == 0.0) {
1156 e = 0;
1157 }
1158 else {
1159 PyErr_SetString(PyExc_SystemError,
1160 "frexp() result out of range");
1161 return -1;
1162 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001163
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001164 if (e >= 1024) {
1165 /* XXX 1024 itself is reserved for Inf/NaN */
1166 PyErr_SetString(PyExc_OverflowError,
1167 "float too large to pack with d format");
1168 return -1;
1169 }
1170 else if (e < -1022) {
1171 /* Gradual underflow */
1172 f = ldexp(f, 1022 + e);
1173 e = 0;
1174 }
1175 else if (!(e == 0 && f == 0.0)) {
1176 e += 1023;
1177 f -= 1.0; /* Get rid of leading 1 */
1178 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001179
Tim Peterscba30e22003-02-01 06:24:36 +00001180 /* fhi receives the high 28 bits;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001181 flo the low 24 bits (== 52 bits) */
1182 f *= 268435456.0; /* 2**28 */
1183 fhi = (long) floor(f); /* Truncate */
1184 f -= (double)fhi;
1185 f *= 16777216.0; /* 2**24 */
1186 flo = (long) floor(f + 0.5); /* Round */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001187
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001188 /* First byte */
1189 *p = (s<<7) | (e>>4);
1190 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001191
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001192 /* Second byte */
1193 *p = (unsigned char) (((e&0xF)<<4) | (fhi>>24));
1194 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001195
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001196 /* Third byte */
1197 *p = (unsigned char) ((fhi>>16) & 0xFF);
1198 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001200 /* Fourth byte */
1201 *p = (unsigned char) ((fhi>>8) & 0xFF);
1202 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001203
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001204 /* Fifth byte */
1205 *p = (unsigned char) (fhi & 0xFF);
1206 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001207
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001208 /* Sixth byte */
1209 *p = (unsigned char) ((flo>>16) & 0xFF);
1210 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001211
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001212 /* Seventh byte */
1213 *p = (unsigned char) ((flo>>8) & 0xFF);
1214 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001215
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001216 /* Eighth byte */
1217 *p = (unsigned char) (flo & 0xFF);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001218
Tim Peters0bc93f52003-02-02 18:29:33 +00001219 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001220 return -1;
1221 }
1222 else {
1223 char c_str[250];
1224 c_str[0] = FLOAT;
1225 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001226
Tim Peters0bc93f52003-02-02 18:29:33 +00001227 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001228 return -1;
1229 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001230
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001231 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001232}
1233
1234
1235static int
Tim Peterscba30e22003-02-01 06:24:36 +00001236save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001237{
1238 int size, len;
1239 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001240
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001241 if ((size = PyString_Size(args)) < 0)
1242 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001243
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001244 if (!self->bin) {
1245 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001246
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001247 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001248
Tim Peterscba30e22003-02-01 06:24:36 +00001249 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001250 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001251
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001252 if ((len = PyString_Size(repr)) < 0)
1253 goto err;
1254 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001255
Tim Peters0bc93f52003-02-02 18:29:33 +00001256 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001257 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001258
Tim Peters0bc93f52003-02-02 18:29:33 +00001259 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001260 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001261
Tim Peters0bc93f52003-02-02 18:29:33 +00001262 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001263 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001264
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001265 Py_XDECREF(repr);
1266 }
1267 else {
1268 int i;
1269 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001270
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001271 if ((size = PyString_Size(args)) < 0)
1272 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001273
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001274 if (size < 256) {
1275 c_str[0] = SHORT_BINSTRING;
1276 c_str[1] = size;
1277 len = 2;
1278 }
1279 else {
1280 c_str[0] = BINSTRING;
1281 for (i = 1; i < 5; i++)
1282 c_str[i] = (int)(size >> ((i - 1) * 8));
1283 len = 5;
1284 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001285
Tim Peters0bc93f52003-02-02 18:29:33 +00001286 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001287 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001288
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001289 if (size > 128 && Pdata_Check(self->file)) {
1290 if (write_other(self, NULL, 0) < 0) return -1;
1291 PDATA_APPEND(self->file, args, -1);
1292 }
1293 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001294 if (self->write_func(self,
1295 PyString_AS_STRING(
1296 (PyStringObject *)args),
1297 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001298 return -1;
1299 }
1300 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001302 if (doput)
1303 if (put(self, args) < 0)
1304 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001305
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001306 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001307
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001308 err:
1309 Py_XDECREF(repr);
1310 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001311}
1312
1313
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001314#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001315/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1316 backslash and newline characters to \uXXXX escapes. */
1317static PyObject *
1318modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1319{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001320 PyObject *repr;
1321 char *p;
1322 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001323
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001324 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001326 repr = PyString_FromStringAndSize(NULL, 6 * size);
1327 if (repr == NULL)
1328 return NULL;
1329 if (size == 0)
1330 return repr;
1331
1332 p = q = PyString_AS_STRING(repr);
1333 while (size-- > 0) {
1334 Py_UNICODE ch = *s++;
1335 /* Map 16-bit characters to '\uxxxx' */
1336 if (ch >= 256 || ch == '\\' || ch == '\n') {
1337 *p++ = '\\';
1338 *p++ = 'u';
1339 *p++ = hexdigit[(ch >> 12) & 0xf];
1340 *p++ = hexdigit[(ch >> 8) & 0xf];
1341 *p++ = hexdigit[(ch >> 4) & 0xf];
1342 *p++ = hexdigit[ch & 15];
1343 }
1344 /* Copy everything else as-is */
1345 else
1346 *p++ = (char) ch;
1347 }
1348 *p = '\0';
Tim Peters5de98422002-04-27 18:44:32 +00001349 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001350 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001351}
1352
1353
Guido van Rossum60456fd1997-04-09 17:36:32 +00001354static int
Tim Peterscba30e22003-02-01 06:24:36 +00001355save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001356{
1357 int size, len;
1358 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001359
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001360 if (!PyUnicode_Check(args))
1361 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001362
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001363 if (!self->bin) {
1364 char *repr_str;
1365 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001366
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001367 repr = modified_EncodeRawUnicodeEscape(
1368 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001369 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001370 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001371
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001372 if ((len = PyString_Size(repr)) < 0)
1373 goto err;
1374 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001375
Tim Peters0bc93f52003-02-02 18:29:33 +00001376 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001377 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001378
Tim Peters0bc93f52003-02-02 18:29:33 +00001379 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001380 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001381
Tim Peters0bc93f52003-02-02 18:29:33 +00001382 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001383 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001384
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001385 Py_XDECREF(repr);
1386 }
1387 else {
1388 int i;
1389 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001390
Tim Peterscba30e22003-02-01 06:24:36 +00001391 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001392 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001393
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001394 if ((size = PyString_Size(repr)) < 0)
1395 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001396
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001397 c_str[0] = BINUNICODE;
1398 for (i = 1; i < 5; i++)
1399 c_str[i] = (int)(size >> ((i - 1) * 8));
1400 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001401
Tim Peters0bc93f52003-02-02 18:29:33 +00001402 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001403 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001404
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001405 if (size > 128 && Pdata_Check(self->file)) {
1406 if (write_other(self, NULL, 0) < 0)
1407 goto err;
1408 PDATA_APPEND(self->file, repr, -1);
1409 }
1410 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001411 if (self->write_func(self, PyString_AS_STRING(repr),
1412 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001413 goto err;
1414 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001416 Py_DECREF(repr);
1417 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001418
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001419 if (doput)
1420 if (put(self, args) < 0)
1421 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001422
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001423 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001425 err:
1426 Py_XDECREF(repr);
1427 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001428}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001429#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001430
Tim Peters1d63c9f2003-02-02 20:29:39 +00001431/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1432static int
Tim Peters67920142003-02-05 03:46:17 +00001433store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001434{
1435 int i;
1436 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001437
Tim Peters1d63c9f2003-02-02 20:29:39 +00001438 assert(PyTuple_Size(t) == len);
1439
1440 for (i = 0; i < len; i++) {
1441 PyObject *element = PyTuple_GET_ITEM(t, i);
1442
1443 if (element == NULL)
1444 goto finally;
1445 if (save(self, element, 0) < 0)
1446 goto finally;
1447 }
1448 res = 0;
1449
1450 finally:
1451 return res;
1452}
1453
1454/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1455 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001456 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001457 * (a tuple can be reached from itself), and that requires some subtle
1458 * magic so that it works in all cases. IOW, this is a long routine.
1459 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001460static int
Tim Peterscba30e22003-02-01 06:24:36 +00001461save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001462{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001463 PyObject *py_tuple_id = NULL;
1464 int len, i;
1465 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001466
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001467 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001468 static char pop = POP;
1469 static char pop_mark = POP_MARK;
1470 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001471
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001472 if ((len = PyTuple_Size(args)) < 0)
1473 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001474
Tim Peters1d63c9f2003-02-02 20:29:39 +00001475 if (len == 0) {
1476 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001477
Tim Peters1d63c9f2003-02-02 20:29:39 +00001478 if (self->proto) {
1479 c_str[0] = EMPTY_TUPLE;
1480 len = 1;
1481 }
1482 else {
1483 c_str[0] = MARK;
1484 c_str[1] = TUPLE;
1485 len = 2;
1486 }
1487 if (self->write_func(self, c_str, len) >= 0)
1488 res = 0;
1489 /* Don't memoize an empty tuple. */
1490 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001491 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001492
Tim Peters1d63c9f2003-02-02 20:29:39 +00001493 /* A non-empty tuple. */
1494
1495 /* id(tuple) isn't in the memo now. If it shows up there after
1496 * saving the tuple elements, the tuple must be recursive, in
1497 * which case we'll pop everything we put on the stack, and fetch
1498 * its value from the memo.
1499 */
1500 py_tuple_id = PyLong_FromVoidPtr(args);
1501 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001502 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001503
Tim Peters1d63c9f2003-02-02 20:29:39 +00001504 if (len <= 3 && self->proto >= 2) {
1505 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001506 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001507 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001508 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001509 /* pop the len elements */
1510 for (i = 0; i < len; ++i)
1511 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001512 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001513 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001514 if (get(self, py_tuple_id) < 0)
1515 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001516 res = 0;
1517 goto finally;
1518 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001519 /* Not recursive. */
1520 if (self->write_func(self, len2opcode + len, 1) < 0)
1521 goto finally;
1522 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001523 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001524
Tim Peters1d63c9f2003-02-02 20:29:39 +00001525 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1526 * Generate MARK elt1 elt2 ... TUPLE
1527 */
1528 if (self->write_func(self, &MARKv, 1) < 0)
1529 goto finally;
1530
Tim Peters67920142003-02-05 03:46:17 +00001531 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001532 goto finally;
1533
1534 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1535 /* pop the stack stuff we pushed */
1536 if (self->bin) {
1537 if (self->write_func(self, &pop_mark, 1) < 0)
1538 goto finally;
1539 }
1540 else {
1541 /* Note that we pop one more than len, to remove
1542 * the MARK too.
1543 */
1544 for (i = 0; i <= len; i++)
1545 if (self->write_func(self, &pop, 1) < 0)
1546 goto finally;
1547 }
1548 /* fetch from memo */
1549 if (get(self, py_tuple_id) >= 0)
1550 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001551 goto finally;
1552 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001553
Tim Peters1d63c9f2003-02-02 20:29:39 +00001554 /* Not recursive. */
1555 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001556 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001557
Tim Peters1d63c9f2003-02-02 20:29:39 +00001558 memoize:
1559 if (put(self, args) >= 0)
1560 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001561
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001562 finally:
1563 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001564 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001565}
1566
Tim Peters1092d642003-02-11 21:06:20 +00001567/* iter is an iterator giving items, and we batch up chunks of
1568 * MARK item item ... item APPENDS
1569 * opcode sequences. Calling code should have arranged to first create an
1570 * empty list, or list-like object, for the APPENDS to operate on.
1571 * Returns 0 on success, <0 on error.
1572 */
1573static int
1574batch_list(Picklerobject *self, PyObject *iter)
1575{
1576 PyObject *obj;
1577 PyObject *slice[BATCHSIZE];
1578 int i, n;
1579
1580 static char append = APPEND;
1581 static char appends = APPENDS;
1582
1583 assert(iter != NULL);
1584
1585 if (self->proto == 0) {
1586 /* APPENDS isn't available; do one at a time. */
1587 for (;;) {
1588 obj = PyIter_Next(iter);
1589 if (obj == NULL) {
1590 if (PyErr_Occurred())
1591 return -1;
1592 break;
1593 }
1594 i = save(self, obj, 0);
1595 Py_DECREF(obj);
1596 if (i < 0)
1597 return -1;
1598 if (self->write_func(self, &append, 1) < 0)
1599 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001600 }
1601 return 0;
1602 }
1603
1604 /* proto > 0: write in batches of BATCHSIZE. */
1605 do {
1606 /* Get next group of (no more than) BATCHSIZE elements. */
1607 for (n = 0; n < BATCHSIZE; ++n) {
1608 obj = PyIter_Next(iter);
1609 if (obj == NULL) {
1610 if (PyErr_Occurred())
1611 goto BatchFailed;
1612 break;
1613 }
1614 slice[n] = obj;
1615 }
1616
1617 if (n > 1) {
1618 /* Pump out MARK, slice[0:n], APPENDS. */
1619 if (self->write_func(self, &MARKv, 1) < 0)
1620 goto BatchFailed;
1621 for (i = 0; i < n; ++i) {
1622 if (save(self, slice[i], 0) < 0)
1623 goto BatchFailed;
1624 }
1625 if (self->write_func(self, &appends, 1) < 0)
1626 goto BatchFailed;
1627 }
1628 else if (n == 1) {
1629 if (save(self, slice[0], 0) < 0)
1630 goto BatchFailed;
1631 if (self->write_func(self, &append, 1) < 0)
1632 goto BatchFailed;
1633 }
1634
1635 for (i = 0; i < n; ++i) {
1636 Py_DECREF(slice[i]);
1637 }
Tim Peters90975f12003-02-12 05:28:58 +00001638 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001639 return 0;
1640
1641BatchFailed:
1642 while (--n >= 0) {
1643 Py_DECREF(slice[n]);
1644 }
1645 return -1;
1646}
1647
Guido van Rossum60456fd1997-04-09 17:36:32 +00001648static int
Tim Peterscba30e22003-02-01 06:24:36 +00001649save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001650{
Tim Peters1092d642003-02-11 21:06:20 +00001651 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001652 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001653 int len;
1654 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001655
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001656 if (self->fast && !fast_save_enter(self, args))
1657 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001658
Tim Peters1092d642003-02-11 21:06:20 +00001659 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001660 if (self->bin) {
1661 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001662 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001663 }
1664 else {
1665 s[0] = MARK;
1666 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001667 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001668 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001669
Tim Peters1092d642003-02-11 21:06:20 +00001670 if (self->write_func(self, s, len) < 0)
1671 goto finally;
1672
1673 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001674 if ((len = PyList_Size(args)) < 0)
1675 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001676
Tim Peters1092d642003-02-11 21:06:20 +00001677 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001678 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001679 if (put(self, args) >= 0)
1680 res = 0;
1681 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001682 }
Tim Peters90975f12003-02-12 05:28:58 +00001683 if (put2(self, args) < 0)
1684 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001685
Tim Peters1092d642003-02-11 21:06:20 +00001686 /* Materialize the list elements. */
1687 iter = PyObject_GetIter(args);
1688 if (iter == NULL)
1689 goto finally;
1690 res = batch_list(self, iter);
1691 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001692
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001693 finally:
1694 if (self->fast && !fast_save_leave(self, args))
1695 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001696
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001697 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001698}
1699
1700
Tim Peters42f08ac2003-02-11 22:43:24 +00001701/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1702 * MARK key value ... key value SETITEMS
1703 * opcode sequences. Calling code should have arranged to first create an
1704 * empty dict, or dict-like object, for the SETITEMS to operate on.
1705 * Returns 0 on success, <0 on error.
1706 *
1707 * This is very much like batch_list(). The difference between saving
1708 * elements directly, and picking apart two-tuples, is so long-winded at
1709 * the C level, though, that attempts to combine these routines were too
1710 * ugly to bear.
1711 */
1712static int
1713batch_dict(Picklerobject *self, PyObject *iter)
1714{
1715 PyObject *p;
1716 PyObject *slice[BATCHSIZE];
1717 int i, n;
1718
1719 static char setitem = SETITEM;
1720 static char setitems = SETITEMS;
1721
1722 assert(iter != NULL);
1723
1724 if (self->proto == 0) {
1725 /* SETITEMS isn't available; do one at a time. */
1726 for (;;) {
1727 p = PyIter_Next(iter);
1728 if (p == NULL) {
1729 if (PyErr_Occurred())
1730 return -1;
1731 break;
1732 }
1733 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1734 PyErr_SetString(PyExc_TypeError, "dict items "
1735 "iterator must return 2-tuples");
1736 return -1;
1737 }
1738 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1739 if (i >= 0)
1740 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1741 Py_DECREF(p);
1742 if (i < 0)
1743 return -1;
1744 if (self->write_func(self, &setitem, 1) < 0)
1745 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001746 }
1747 return 0;
1748 }
1749
1750 /* proto > 0: write in batches of BATCHSIZE. */
1751 do {
1752 /* Get next group of (no more than) BATCHSIZE elements. */
1753 for (n = 0; n < BATCHSIZE; ++n) {
1754 p = PyIter_Next(iter);
1755 if (p == NULL) {
1756 if (PyErr_Occurred())
1757 goto BatchFailed;
1758 break;
1759 }
1760 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1761 PyErr_SetString(PyExc_TypeError, "dict items "
1762 "iterator must return 2-tuples");
1763 goto BatchFailed;
1764 }
1765 slice[n] = p;
1766 }
1767
1768 if (n > 1) {
1769 /* Pump out MARK, slice[0:n], SETITEMS. */
1770 if (self->write_func(self, &MARKv, 1) < 0)
1771 goto BatchFailed;
1772 for (i = 0; i < n; ++i) {
1773 p = slice[i];
1774 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1775 goto BatchFailed;
1776 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1777 goto BatchFailed;
1778 }
1779 if (self->write_func(self, &setitems, 1) < 0)
1780 goto BatchFailed;
1781 }
1782 else if (n == 1) {
1783 p = slice[0];
1784 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1785 goto BatchFailed;
1786 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1787 goto BatchFailed;
1788 if (self->write_func(self, &setitem, 1) < 0)
1789 goto BatchFailed;
1790 }
1791
1792 for (i = 0; i < n; ++i) {
1793 Py_DECREF(slice[i]);
1794 }
Tim Peters90975f12003-02-12 05:28:58 +00001795 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001796 return 0;
1797
1798BatchFailed:
1799 while (--n >= 0) {
1800 Py_DECREF(slice[n]);
1801 }
1802 return -1;
1803}
1804
Guido van Rossum60456fd1997-04-09 17:36:32 +00001805static int
Tim Peterscba30e22003-02-01 06:24:36 +00001806save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001807{
Tim Peters42f08ac2003-02-11 22:43:24 +00001808 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001809 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001810 int len;
1811 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001812
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001813 if (self->fast && !fast_save_enter(self, args))
1814 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001815
Tim Peters42f08ac2003-02-11 22:43:24 +00001816 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001817 if (self->bin) {
1818 s[0] = EMPTY_DICT;
1819 len = 1;
1820 }
1821 else {
1822 s[0] = MARK;
1823 s[1] = DICT;
1824 len = 2;
1825 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001826
Tim Peters0bc93f52003-02-02 18:29:33 +00001827 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001828 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001829
Tim Peters42f08ac2003-02-11 22:43:24 +00001830 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001831 if ((len = PyDict_Size(args)) < 0)
1832 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001833
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001834 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001835 if (put(self, args) >= 0)
1836 res = 0;
1837 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001838 }
Tim Peters90975f12003-02-12 05:28:58 +00001839 if (put2(self, args) < 0)
1840 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001841
Tim Peters42f08ac2003-02-11 22:43:24 +00001842 /* Materialize the dict items. */
1843 iter = PyObject_CallMethod(args, "iteritems", "()");
1844 if (iter == NULL)
1845 goto finally;
1846 res = batch_dict(self, iter);
1847 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001848
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001849 finally:
1850 if (self->fast && !fast_save_leave(self, args))
1851 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001852
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001853 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001854}
1855
1856
Tim Peters84e87f32001-03-17 04:50:51 +00001857static int
Tim Peterscba30e22003-02-01 06:24:36 +00001858save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001859{
1860 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1861 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1862 char *module_str, *name_str;
1863 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001864
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001865 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001866
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001867 if (self->fast && !fast_save_enter(self, args))
1868 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001869
Tim Peters0bc93f52003-02-02 18:29:33 +00001870 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001871 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001872
Tim Peterscba30e22003-02-01 06:24:36 +00001873 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001874 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001876 if (self->bin) {
1877 if (save(self, class, 0) < 0)
1878 goto finally;
1879 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001880
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001881 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1882 PyObject *element = 0;
1883 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001884
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001885 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001886 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001887 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001888
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001889 if ((len = PyObject_Size(class_args)) < 0)
1890 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001891
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001892 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001893 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001894 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001895
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001896 if (save(self, element, 0) < 0) {
1897 Py_DECREF(element);
1898 goto finally;
1899 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001900
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001901 Py_DECREF(element);
1902 }
1903 }
1904 else {
1905 PyErr_Clear();
1906 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001907
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001908 if (!self->bin) {
1909 if (!( name = ((PyClassObject *)class)->cl_name )) {
1910 PyErr_SetString(PicklingError, "class has no name");
1911 goto finally;
1912 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001913
Tim Peterscba30e22003-02-01 06:24:36 +00001914 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001915 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001916
Tim Peters84e87f32001-03-17 04:50:51 +00001917
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001918 if ((module_size = PyString_Size(module)) < 0 ||
1919 (name_size = PyString_Size(name)) < 0)
1920 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001921
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001922 module_str = PyString_AS_STRING((PyStringObject *)module);
1923 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001924
Tim Peters0bc93f52003-02-02 18:29:33 +00001925 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001926 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001927
Tim Peters0bc93f52003-02-02 18:29:33 +00001928 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001929 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001930
Tim Peters0bc93f52003-02-02 18:29:33 +00001931 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001932 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001933
Tim Peters0bc93f52003-02-02 18:29:33 +00001934 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001935 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001936
Tim Peters0bc93f52003-02-02 18:29:33 +00001937 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001938 goto finally;
1939 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001940 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001941 goto finally;
1942 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001943
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001944 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1945 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001946 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001947 goto finally;
1948 }
1949 else {
1950 PyErr_Clear();
Guido van Rossum60456fd1997-04-09 17:36:32 +00001951
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001952 if (!( state = PyObject_GetAttr(args, __dict___str))) {
1953 PyErr_Clear();
1954 res = 0;
1955 goto finally;
1956 }
1957 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001958
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001959 if (!PyDict_Check(state)) {
1960 if (put2(self, args) < 0)
1961 goto finally;
1962 }
1963 else {
1964 if (put(self, args) < 0)
1965 goto finally;
1966 }
Tim Peters84e87f32001-03-17 04:50:51 +00001967
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001968 if (save(self, state, 0) < 0)
1969 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001970
Tim Peters0bc93f52003-02-02 18:29:33 +00001971 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001972 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001973
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001974 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001975
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001976 finally:
1977 if (self->fast && !fast_save_leave(self, args))
1978 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001979
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001980 Py_XDECREF(module);
1981 Py_XDECREF(class);
1982 Py_XDECREF(state);
1983 Py_XDECREF(getinitargs_func);
1984 Py_XDECREF(getstate_func);
1985 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001986
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001987 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001988}
1989
1990
Guido van Rossum60456fd1997-04-09 17:36:32 +00001991static int
Tim Peterscba30e22003-02-01 06:24:36 +00001992save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001993{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001994 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001995 char *name_str, *module_str;
1996 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001997
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001998 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001999
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002000 if (name) {
2001 global_name = name;
2002 Py_INCREF(global_name);
2003 }
2004 else {
Tim Peterscba30e22003-02-01 06:24:36 +00002005 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002006 goto finally;
2007 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002008
Tim Peterscba30e22003-02-01 06:24:36 +00002009 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002010 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002011
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002012 if ((module_size = PyString_Size(module)) < 0 ||
2013 (name_size = PyString_Size(global_name)) < 0)
2014 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002015
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002016 module_str = PyString_AS_STRING((PyStringObject *)module);
2017 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002018
Guido van Rossum75bfd052002-12-24 18:10:07 +00002019 /* XXX This can be doing a relative import. Clearly it shouldn't,
2020 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002021 mod = PyImport_ImportModule(module_str);
2022 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002023 cPickle_ErrFormat(PicklingError,
2024 "Can't pickle %s: it's not found as %s.%s",
2025 "OSS", args, module, global_name);
2026 goto finally;
2027 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002028 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002029 if (klass == NULL) {
2030 cPickle_ErrFormat(PicklingError,
2031 "Can't pickle %s: it's not found as %s.%s",
2032 "OSS", args, module, global_name);
2033 goto finally;
2034 }
2035 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002036 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002037 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00002038 "Can't pickle %s: it's not the same object "
2039 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002040 "OSS", args, module, global_name);
2041 goto finally;
2042 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002043 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002044
Tim Peters731098b2003-02-04 20:56:09 +00002045 if (self->proto >= 2) {
2046 /* See whether this is in the extension registry, and if
2047 * so generate an EXT opcode.
2048 */
2049 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002050 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002051 char c_str[5];
2052 int n;
2053
2054 PyTuple_SET_ITEM(two_tuple, 0, module);
2055 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2056 py_code = PyDict_GetItem(extension_registry, two_tuple);
2057 if (py_code == NULL)
2058 goto gen_global; /* not registered */
2059
2060 /* Verify py_code has the right type and value. */
2061 if (!PyInt_Check(py_code)) {
2062 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002063 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002064 "OO", args, py_code);
2065 goto finally;
2066 }
2067 code = PyInt_AS_LONG(py_code);
2068 if (code <= 0 || code > 0x7fffffffL) {
2069 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2070 "extension code %ld is out of range",
2071 "Ol", args, code);
2072 goto finally;
2073 }
2074
2075 /* Generate an EXT opcode. */
2076 if (code <= 0xff) {
2077 c_str[0] = EXT1;
2078 c_str[1] = (char)code;
2079 n = 2;
2080 }
2081 else if (code <= 0xffff) {
2082 c_str[0] = EXT2;
2083 c_str[1] = (char)(code & 0xff);
2084 c_str[2] = (char)((code >> 8) & 0xff);
2085 n = 3;
2086 }
2087 else {
2088 c_str[0] = EXT4;
2089 c_str[1] = (char)(code & 0xff);
2090 c_str[2] = (char)((code >> 8) & 0xff);
2091 c_str[3] = (char)((code >> 16) & 0xff);
2092 c_str[4] = (char)((code >> 24) & 0xff);
2093 n = 5;
2094 }
2095
2096 if (self->write_func(self, c_str, n) >= 0)
2097 res = 0;
2098 goto finally; /* and don't memoize */
2099 }
2100
2101 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002102 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002103 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002104
Tim Peters0bc93f52003-02-02 18:29:33 +00002105 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002106 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002107
Tim Peters0bc93f52003-02-02 18:29:33 +00002108 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002109 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002110
Tim Peters0bc93f52003-02-02 18:29:33 +00002111 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002112 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002113
Tim Peters0bc93f52003-02-02 18:29:33 +00002114 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002115 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002116
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002117 if (put(self, args) < 0)
2118 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002119
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002120 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002121
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002122 finally:
2123 Py_XDECREF(module);
2124 Py_XDECREF(global_name);
2125 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002126
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002127 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002128}
2129
Guido van Rossum60456fd1997-04-09 17:36:32 +00002130static int
Tim Peterscba30e22003-02-01 06:24:36 +00002131save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002132{
2133 PyObject *pid = 0;
2134 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002135
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002136 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002138 Py_INCREF(args);
2139 ARG_TUP(self, args);
2140 if (self->arg) {
2141 pid = PyObject_Call(f, self->arg, NULL);
2142 FREE_ARG_TUP(self);
2143 }
2144 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002145
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002146 if (pid != Py_None) {
2147 if (!self->bin) {
2148 if (!PyString_Check(pid)) {
2149 PyErr_SetString(PicklingError,
2150 "persistent id must be string");
2151 goto finally;
2152 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002153
Tim Peters0bc93f52003-02-02 18:29:33 +00002154 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002155 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002156
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002157 if ((size = PyString_Size(pid)) < 0)
2158 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002159
Tim Peters0bc93f52003-02-02 18:29:33 +00002160 if (self->write_func(self,
2161 PyString_AS_STRING(
2162 (PyStringObject *)pid),
2163 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002164 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002165
Tim Peters0bc93f52003-02-02 18:29:33 +00002166 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002167 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002168
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002169 res = 1;
2170 goto finally;
2171 }
2172 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002173 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002174 res = -1;
2175 else
2176 res = 1;
2177 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002178
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002179 goto finally;
2180 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002181
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002182 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002184 finally:
2185 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002186
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002187 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002188}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002189
Tim Peters71fcda52003-02-14 23:05:28 +00002190/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2191 * appropriate __reduce__ method for ob.
2192 */
Tim Peters84e87f32001-03-17 04:50:51 +00002193static int
Tim Peters71fcda52003-02-14 23:05:28 +00002194save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002195{
Tim Peters71fcda52003-02-14 23:05:28 +00002196 PyObject *callable;
2197 PyObject *argtup;
2198 PyObject *state = NULL;
2199 PyObject *listitems = NULL;
2200 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002201
Tim Peters71fcda52003-02-14 23:05:28 +00002202 int use_newobj = self->proto >= 2;
2203
2204 static char reduce = REDUCE;
2205 static char build = BUILD;
2206 static char newobj = NEWOBJ;
2207
2208 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2209 &callable,
2210 &argtup,
2211 &state,
2212 &listitems,
2213 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002214 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002215
Tim Peters71fcda52003-02-14 23:05:28 +00002216 if (state == Py_None)
2217 state = NULL;
2218 if (listitems == Py_None)
2219 listitems = NULL;
2220 if (dictitems == Py_None)
2221 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002222
Tim Peters71fcda52003-02-14 23:05:28 +00002223 /* Protocol 2 special case: if callable's name is __newobj__, use
2224 * NEWOBJ. This consumes a lot of code.
2225 */
2226 if (use_newobj) {
2227 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002228
Tim Peters71fcda52003-02-14 23:05:28 +00002229 if (temp == NULL) {
2230 PyErr_Clear();
2231 use_newobj = 0;
2232 }
2233 else {
2234 use_newobj = PyString_Check(temp) &&
2235 strcmp(PyString_AS_STRING(temp),
2236 "__newobj__") == 0;
2237 Py_DECREF(temp);
2238 }
2239 }
2240 if (use_newobj) {
2241 PyObject *cls;
2242 PyObject *newargtup;
2243 int n, i;
2244
2245 /* Sanity checks. */
2246 n = PyTuple_Size(argtup);
2247 if (n < 1) {
2248 PyErr_SetString(PicklingError, "__newobj__ arglist "
2249 "is empty");
2250 return -1;
2251 }
2252
2253 cls = PyTuple_GET_ITEM(argtup, 0);
2254 if (! PyObject_HasAttrString(cls, "__new__")) {
2255 PyErr_SetString(PicklingError, "args[0] from "
2256 "__newobj__ args has no __new__");
2257 return -1;
2258 }
2259
2260 /* XXX How could ob be NULL? */
2261 if (ob != NULL) {
2262 PyObject *ob_dot_class;
2263
2264 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2265 if (ob_dot_class == NULL)
2266 PyErr_Clear();
2267 i = ob_dot_class != cls; /* true iff a problem */
2268 Py_XDECREF(ob_dot_class);
2269 if (i) {
2270 PyErr_SetString(PicklingError, "args[0] from "
2271 "__newobj__ args has the wrong class");
2272 return -1;
2273 }
2274 }
2275
2276 /* Save the class and its __new__ arguments. */
2277 if (save(self, cls, 0) < 0)
2278 return -1;
2279
2280 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2281 if (newargtup == NULL)
2282 return -1;
2283 for (i = 1; i < n; ++i) {
2284 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2285 Py_INCREF(temp);
2286 PyTuple_SET_ITEM(newargtup, i-1, temp);
2287 }
2288 i = save(self, newargtup, 0) < 0;
2289 Py_DECREF(newargtup);
2290 if (i < 0)
2291 return -1;
2292
2293 /* Add NEWOBJ opcode. */
2294 if (self->write_func(self, &newobj, 1) < 0)
2295 return -1;
2296 }
2297 else {
2298 /* Not using NEWOBJ. */
2299 if (save(self, callable, 0) < 0 ||
2300 save(self, argtup, 0) < 0 ||
2301 self->write_func(self, &reduce, 1) < 0)
2302 return -1;
2303 }
2304
2305 /* Memoize. */
2306 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002307 if (ob != NULL) {
2308 if (state && !PyDict_Check(state)) {
2309 if (put2(self, ob) < 0)
2310 return -1;
2311 }
Tim Peters71fcda52003-02-14 23:05:28 +00002312 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002313 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002314 }
Tim Peters84e87f32001-03-17 04:50:51 +00002315
Guido van Rossum60456fd1997-04-09 17:36:32 +00002316
Tim Peters71fcda52003-02-14 23:05:28 +00002317 if (listitems && batch_list(self, listitems) < 0)
2318 return -1;
2319
2320 if (dictitems && batch_dict(self, dictitems) < 0)
2321 return -1;
2322
2323 if (state) {
2324 if (save(self, state, 0) < 0 ||
2325 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002326 return -1;
2327 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002328
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002329 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002330}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002331
Guido van Rossum60456fd1997-04-09 17:36:32 +00002332static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002333save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002334{
2335 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002336 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2337 PyObject *arg_tup;
2338 int res = -1;
2339 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002340
Martin v. Löwis5a395302002-08-04 08:20:23 +00002341 if (self->nesting++ > Py_GetRecursionLimit()){
2342 PyErr_SetString(PyExc_RuntimeError,
2343 "maximum recursion depth exceeded");
2344 goto finally;
2345 }
2346
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002347 if (!pers_save && self->pers_func) {
2348 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2349 res = tmp;
2350 goto finally;
2351 }
2352 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002353
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002354 if (args == Py_None) {
2355 res = save_none(self, args);
2356 goto finally;
2357 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002358
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002359 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002361 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002362 case 'b':
2363 if (args == Py_False || args == Py_True) {
2364 res = save_bool(self, args);
2365 goto finally;
2366 }
2367 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002368 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002369 if (type == &PyInt_Type) {
2370 res = save_int(self, args);
2371 goto finally;
2372 }
2373 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002374
Guido van Rossum60456fd1997-04-09 17:36:32 +00002375 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002376 if (type == &PyLong_Type) {
2377 res = save_long(self, args);
2378 goto finally;
2379 }
2380 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002381
Guido van Rossum60456fd1997-04-09 17:36:32 +00002382 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002383 if (type == &PyFloat_Type) {
2384 res = save_float(self, args);
2385 goto finally;
2386 }
2387 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002388
Guido van Rossum60456fd1997-04-09 17:36:32 +00002389 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002390 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2391 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002392 goto finally;
2393 }
2394 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002395
Guido van Rossum60456fd1997-04-09 17:36:32 +00002396 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002397 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2398 res = save_string(self, args, 0);
2399 goto finally;
2400 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002401
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002402#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002403 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002404 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2405 res = save_unicode(self, args, 0);
2406 goto finally;
2407 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002408#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002409 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002410
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002411 if (args->ob_refcnt > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002412 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002413 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002414
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002415 if (PyDict_GetItem(self->memo, py_ob_id)) {
2416 if (get(self, py_ob_id) < 0)
2417 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002418
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002419 res = 0;
2420 goto finally;
2421 }
2422 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002424 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002425 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002426 if (type == &PyString_Type) {
2427 res = save_string(self, args, 1);
2428 goto finally;
2429 }
2430 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002431
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002432#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002433 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002434 if (type == &PyUnicode_Type) {
2435 res = save_unicode(self, args, 1);
2436 goto finally;
2437 }
2438 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002439#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002440
Guido van Rossum60456fd1997-04-09 17:36:32 +00002441 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002442 if (type == &PyTuple_Type) {
2443 res = save_tuple(self, args);
2444 goto finally;
2445 }
2446 if (type == &PyType_Type) {
2447 res = save_global(self, args, NULL);
2448 goto finally;
2449 }
2450 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002451
Guido van Rossum60456fd1997-04-09 17:36:32 +00002452 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002453 if (type == &PyList_Type) {
2454 res = save_list(self, args);
2455 goto finally;
2456 }
2457 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002458
2459 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002460 if (type == &PyDict_Type) {
2461 res = save_dict(self, args);
2462 goto finally;
2463 }
2464 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002465
2466 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002467 if (type == &PyInstance_Type) {
2468 res = save_inst(self, args);
2469 goto finally;
2470 }
2471 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002472
2473 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002474 if (type == &PyClass_Type) {
2475 res = save_global(self, args, NULL);
2476 goto finally;
2477 }
2478 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002479
2480 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002481 if (type == &PyFunction_Type) {
2482 res = save_global(self, args, NULL);
2483 goto finally;
2484 }
2485 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002486
2487 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002488 if (type == &PyCFunction_Type) {
2489 res = save_global(self, args, NULL);
2490 goto finally;
2491 }
2492 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002493
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002494 if (!pers_save && self->inst_pers_func) {
2495 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2496 res = tmp;
2497 goto finally;
2498 }
2499 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002500
Jeremy Hylton39c61162002-07-16 19:47:43 +00002501 if (PyType_IsSubtype(type, &PyType_Type)) {
2502 res = save_global(self, args, NULL);
2503 goto finally;
2504 }
2505
Tim Peters71fcda52003-02-14 23:05:28 +00002506 /* Get a reduction callable. This may come from
2507 * copy_reg.dispatch_table, the object's __reduce__ method,
2508 * the default object.__reduce__, or copy_reg._better_reduce.
2509 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002510 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2511 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002512 Py_INCREF(__reduce__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002513 }
2514 else {
Tim Peters71fcda52003-02-14 23:05:28 +00002515 /* Check for a __reduce__ method.
2516 * Subtle: get the unbound method from the class, so that
2517 * protocol 2 can override the default __reduce__ that all
2518 * classes inherit from object.
2519 * XXX object.__reduce__ should really be rewritten so that
2520 * XXX we don't need to call back into Python code here
2521 * XXX (better_reduce), but no time to do that.
2522 */
2523 __reduce__ = PyObject_GetAttr((PyObject *)type,
2524 __reduce___str);
2525 if (__reduce__ == NULL) {
Tim Peters5aa3da62003-02-13 21:03:57 +00002526 PyErr_Clear();
Tim Peters71fcda52003-02-14 23:05:28 +00002527 PyErr_SetObject(UnpickleableError, args);
2528 goto finally;
2529 }
2530
2531 if (self->proto >= 2 && __reduce__ == object_reduce) {
2532 /* Proto 2 can do better than the default. */
2533 Py_DECREF(__reduce__);
2534 Py_INCREF(better_reduce);
2535 __reduce__ = better_reduce;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002536 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002537 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002538
Tim Peters71fcda52003-02-14 23:05:28 +00002539 /* Call the reduction callable, setting t to the result. */
2540 assert(__reduce__ != NULL);
2541 assert(t == NULL);
2542 Py_INCREF(args);
2543 ARG_TUP(self, args);
2544 if (self->arg) {
2545 t = PyObject_Call(__reduce__, self->arg, NULL);
2546 FREE_ARG_TUP(self);
2547 }
2548 if (t == NULL)
2549 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002550
Tim Peters71fcda52003-02-14 23:05:28 +00002551 if (PyString_Check(t)) {
2552 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002553 goto finally;
2554 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002555
Tim Peters71fcda52003-02-14 23:05:28 +00002556 if (! PyTuple_Check(t)) {
2557 cPickle_ErrFormat(PicklingError, "Value returned by "
2558 "%s must be string or tuple",
2559 "O", __reduce__);
2560 goto finally;
2561 }
2562
2563 size = PyTuple_Size(t);
2564 if (size < 2 || size > 5) {
2565 cPickle_ErrFormat(PicklingError, "tuple returned by "
2566 "%s must contain 2 through 5 elements",
2567 "O", __reduce__);
2568 goto finally;
2569 }
2570
2571 arg_tup = PyTuple_GET_ITEM(t, 1);
2572 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2573 cPickle_ErrFormat(PicklingError, "Second element of "
2574 "tuple returned by %s must be a tuple",
2575 "O", __reduce__);
2576 goto finally;
2577 }
2578
2579 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002581 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002582 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002583 Py_XDECREF(py_ob_id);
2584 Py_XDECREF(__reduce__);
2585 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002586
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002587 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002588}
2589
2590
2591static int
Tim Peterscba30e22003-02-01 06:24:36 +00002592dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002593{
2594 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002595
Tim Peters4190fb82003-02-02 16:09:05 +00002596 if (self->proto >= 2) {
2597 char bytes[2];
2598
2599 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002600 assert(self->proto >= 0 && self->proto < 256);
2601 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002602 if (self->write_func(self, bytes, 2) < 0)
2603 return -1;
2604 }
2605
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002606 if (save(self, args, 0) < 0)
2607 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002608
Tim Peters4190fb82003-02-02 16:09:05 +00002609 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002610 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002611
Tim Peters4190fb82003-02-02 16:09:05 +00002612 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002613 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002614
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002615 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002616}
2617
2618static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002619Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002620{
Tim Peterscba30e22003-02-01 06:24:36 +00002621 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002622 PyDict_Clear(self->memo);
2623 Py_INCREF(Py_None);
2624 return Py_None;
2625}
2626
2627static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002628Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002629{
2630 int l, i, rsize, ssize, clear=1, lm;
2631 long ik;
2632 PyObject *k, *r;
2633 char *s, *p, *have_get;
2634 Pdata *data;
2635
2636 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002637 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002638 return NULL;
2639
2640 /* Check to make sure we are based on a list */
2641 if (! Pdata_Check(self->file)) {
2642 PyErr_SetString(PicklingError,
2643 "Attempt to getvalue() a non-list-based pickler");
2644 return NULL;
2645 }
2646
2647 /* flush write buffer */
2648 if (write_other(self, NULL, 0) < 0) return NULL;
2649
2650 data=(Pdata*)self->file;
2651 l=data->length;
2652
2653 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002654 lm = PyDict_Size(self->memo);
2655 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002656 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002657 have_get = malloc(lm);
2658 if (have_get == NULL) return PyErr_NoMemory();
2659 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002660
2661 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002662 for (rsize = 0, i = l; --i >= 0; ) {
2663 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002664
Tim Petersac5687a2003-02-02 18:08:34 +00002665 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002666 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002667
2668 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002669 ik = PyInt_AS_LONG((PyIntObject*)k);
2670 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002671 PyErr_SetString(PicklingError,
2672 "Invalid get data");
2673 return NULL;
2674 }
Tim Petersac5687a2003-02-02 18:08:34 +00002675 if (have_get[ik]) /* with matching get */
2676 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002677 }
2678
2679 else if (! (PyTuple_Check(k) &&
2680 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002681 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002682 ) {
2683 PyErr_SetString(PicklingError,
2684 "Unexpected data in internal list");
2685 return NULL;
2686 }
2687
2688 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002689 ik = PyInt_AS_LONG((PyIntObject *)k);
2690 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002691 PyErr_SetString(PicklingError,
2692 "Invalid get data");
2693 return NULL;
2694 }
Tim Petersac5687a2003-02-02 18:08:34 +00002695 have_get[ik] = 1;
2696 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002697 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002698 }
2699
2700 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002701 r = PyString_FromStringAndSize(NULL, rsize);
2702 if (r == NULL) goto err;
2703 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002704
Tim Petersac5687a2003-02-02 18:08:34 +00002705 for (i = 0; i < l; i++) {
2706 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002707
2708 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002709 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002710 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002711 p=PyString_AS_STRING((PyStringObject *)k);
2712 while (--ssize >= 0)
2713 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002714 }
2715 }
2716
2717 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002718 ik = PyInt_AS_LONG((PyIntObject *)
2719 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002720 if (ik < 256) {
2721 *s++ = BINGET;
2722 *s++ = (int)(ik & 0xff);
2723 }
2724 else {
2725 *s++ = LONG_BINGET;
2726 *s++ = (int)(ik & 0xff);
2727 *s++ = (int)((ik >> 8) & 0xff);
2728 *s++ = (int)((ik >> 16) & 0xff);
2729 *s++ = (int)((ik >> 24) & 0xff);
2730 }
2731 }
2732
2733 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002734 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002735
2736 if (have_get[ik]) { /* with matching get */
2737 if (ik < 256) {
2738 *s++ = BINPUT;
2739 *s++ = (int)(ik & 0xff);
2740 }
2741 else {
2742 *s++ = LONG_BINPUT;
2743 *s++ = (int)(ik & 0xff);
2744 *s++ = (int)((ik >> 8) & 0xff);
2745 *s++ = (int)((ik >> 16) & 0xff);
2746 *s++ = (int)((ik >> 24) & 0xff);
2747 }
2748 }
2749 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002750 }
2751
2752 if (clear) {
2753 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002754 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002755 }
2756
2757 free(have_get);
2758 return r;
2759 err:
2760 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002761 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002762}
2763
2764static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002765Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002766{
2767 PyObject *ob;
2768 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002769
Tim Peterscba30e22003-02-01 06:24:36 +00002770 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002771 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002772
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002773 if (dump(self, ob) < 0)
2774 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002775
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002776 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002777
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002778 /* XXX Why does dump() return self? */
2779 Py_INCREF(self);
2780 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002781}
2782
2783
Tim Peterscba30e22003-02-01 06:24:36 +00002784static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002785{
Neal Norwitzb0493252002-03-31 14:44:22 +00002786 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002787 PyDoc_STR("dump(object) -- "
2788 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002789 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002790 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002791 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002792 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002793 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002794};
2795
2796
2797static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002798newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002799{
2800 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002801
Tim Peters5bd2a792003-02-01 16:45:06 +00002802 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002803 proto = HIGHEST_PROTOCOL;
2804 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002805 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2806 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002807 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002808 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002809 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002810
Tim Peters5bd2a792003-02-01 16:45:06 +00002811 self = PyObject_New(Picklerobject, &Picklertype);
2812 if (self == NULL)
2813 return NULL;
2814 self->proto = proto;
2815 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002816 self->fp = NULL;
2817 self->write = NULL;
2818 self->memo = NULL;
2819 self->arg = NULL;
2820 self->pers_func = NULL;
2821 self->inst_pers_func = NULL;
2822 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002823 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002824 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002825 self->fast_container = 0;
2826 self->fast_memo = NULL;
2827 self->buf_size = 0;
2828 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002829
Tim Peters5bd2a792003-02-01 16:45:06 +00002830 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002831 if (file)
2832 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002833 else {
2834 file = Pdata_New();
2835 if (file == NULL)
2836 goto err;
2837 }
2838 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002839
Tim Peterscba30e22003-02-01 06:24:36 +00002840 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002841 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002842
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002843 if (PyFile_Check(file)) {
2844 self->fp = PyFile_AsFile(file);
2845 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002846 PyErr_SetString(PyExc_ValueError,
2847 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002848 goto err;
2849 }
2850 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002851 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002852 else if (PycStringIO_OutputCheck(file)) {
2853 self->write_func = write_cStringIO;
2854 }
2855 else if (file == Py_None) {
2856 self->write_func = write_none;
2857 }
2858 else {
2859 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002860
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002861 if (! Pdata_Check(file)) {
2862 self->write = PyObject_GetAttr(file, write_str);
2863 if (!self->write) {
2864 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002865 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002866 "argument must have 'write' "
2867 "attribute");
2868 goto err;
2869 }
2870 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002871
Tim Peters5bd2a792003-02-01 16:45:06 +00002872 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2873 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002874 PyErr_NoMemory();
2875 goto err;
2876 }
2877 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002878
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002879 if (PyEval_GetRestricted()) {
2880 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002881 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002882
Tim Peters5b7da392003-02-04 00:21:07 +00002883 if (m == NULL)
2884 goto err;
2885 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002886 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002887 if (self->dispatch_table == NULL)
2888 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002889 }
2890 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002891 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002892 Py_INCREF(dispatch_table);
2893 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002894
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002895 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002896
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002897 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002898 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002899 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002900}
2901
2902
2903static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002904get_Pickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002905{
2906 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002907 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002908
Tim Peters92c8bb32003-02-13 23:00:26 +00002909 /* XXX
2910 * The documented signature is Pickler(file, proto=0), but this
2911 * accepts Pickler() and Pickler(integer) too. The meaning then
2912 * is clear as mud, undocumented, and not supported by pickle.py.
2913 * I'm told Zope uses this, but I haven't traced into this code
2914 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002915 */
2916 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002917 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002918 proto = 0;
2919 if (!PyArg_ParseTuple(args, "O|i:Pickler", &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002920 return NULL;
2921 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002922 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002923}
2924
2925
2926static void
Tim Peterscba30e22003-02-01 06:24:36 +00002927Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002928{
2929 Py_XDECREF(self->write);
2930 Py_XDECREF(self->memo);
2931 Py_XDECREF(self->fast_memo);
2932 Py_XDECREF(self->arg);
2933 Py_XDECREF(self->file);
2934 Py_XDECREF(self->pers_func);
2935 Py_XDECREF(self->inst_pers_func);
2936 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002937 PyMem_Free(self->write_buf);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002938 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002939}
2940
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002941static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002942Pickler_get_pers_func(Picklerobject *p)
2943{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002944 if (p->pers_func == NULL)
2945 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2946 else
2947 Py_INCREF(p->pers_func);
2948 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002949}
2950
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002951static int
2952Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2953{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002954 if (v == NULL) {
2955 PyErr_SetString(PyExc_TypeError,
2956 "attribute deletion is not supported");
2957 return -1;
2958 }
2959 Py_XDECREF(p->pers_func);
2960 Py_INCREF(v);
2961 p->pers_func = v;
2962 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002963}
2964
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002965static int
2966Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2967{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002968 if (v == NULL) {
2969 PyErr_SetString(PyExc_TypeError,
2970 "attribute deletion is not supported");
2971 return -1;
2972 }
2973 Py_XDECREF(p->inst_pers_func);
2974 Py_INCREF(v);
2975 p->inst_pers_func = v;
2976 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002977}
2978
2979static PyObject *
2980Pickler_get_memo(Picklerobject *p)
2981{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002982 if (p->memo == NULL)
2983 PyErr_SetString(PyExc_AttributeError, "memo");
2984 else
2985 Py_INCREF(p->memo);
2986 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002987}
2988
2989static int
2990Pickler_set_memo(Picklerobject *p, PyObject *v)
2991{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002992 if (v == NULL) {
2993 PyErr_SetString(PyExc_TypeError,
2994 "attribute deletion is not supported");
2995 return -1;
2996 }
2997 if (!PyDict_Check(v)) {
2998 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2999 return -1;
3000 }
3001 Py_XDECREF(p->memo);
3002 Py_INCREF(v);
3003 p->memo = v;
3004 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003005}
3006
3007static PyObject *
3008Pickler_get_error(Picklerobject *p)
3009{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003010 /* why is this an attribute on the Pickler? */
3011 Py_INCREF(PicklingError);
3012 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003013}
3014
3015static PyMemberDef Pickler_members[] = {
3016 {"binary", T_INT, offsetof(Picklerobject, bin)},
3017 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003018 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003019};
3020
3021static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003022 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003023 (setter)Pickler_set_pers_func},
3024 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3025 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003026 {"PicklingError", (getter)Pickler_get_error, NULL},
3027 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003028};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003029
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003030PyDoc_STRVAR(Picklertype__doc__,
3031"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003032
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003033static PyTypeObject Picklertype = {
3034 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00003035 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00003036 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003037 sizeof(Picklerobject), /*tp_basicsize*/
3038 0,
3039 (destructor)Pickler_dealloc, /* tp_dealloc */
3040 0, /* tp_print */
3041 0, /* tp_getattr */
3042 0, /* tp_setattr */
3043 0, /* tp_compare */
3044 0, /* tp_repr */
3045 0, /* tp_as_number */
3046 0, /* tp_as_sequence */
3047 0, /* tp_as_mapping */
3048 0, /* tp_hash */
3049 0, /* tp_call */
3050 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003051 PyObject_GenericGetAttr, /* tp_getattro */
3052 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003053 0, /* tp_as_buffer */
3054 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3055 Picklertype__doc__, /* tp_doc */
3056 0, /* tp_traverse */
3057 0, /* tp_clear */
3058 0, /* tp_richcompare */
3059 0, /* tp_weaklistoffset */
3060 0, /* tp_iter */
3061 0, /* tp_iternext */
3062 Pickler_methods, /* tp_methods */
3063 Pickler_members, /* tp_members */
3064 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003065};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003066
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003067static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003068find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003069{
3070 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003071
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003072 if (fc) {
3073 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003074 PyErr_SetString(UnpicklingError, "Global and instance "
3075 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003076 return NULL;
3077 }
Tim Peterscba30e22003-02-01 06:24:36 +00003078 return PyObject_CallFunction(fc, "OO", py_module_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003079 py_global_name);
3080 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003081
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003082 module = PySys_GetObject("modules");
3083 if (module == NULL)
3084 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003085
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003086 module = PyDict_GetItem(module, py_module_name);
3087 if (module == NULL) {
3088 module = PyImport_Import(py_module_name);
3089 if (!module)
3090 return NULL;
3091 global = PyObject_GetAttr(module, py_global_name);
3092 Py_DECREF(module);
3093 }
3094 else
3095 global = PyObject_GetAttr(module, py_global_name);
3096 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003097}
3098
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003099static int
Tim Peterscba30e22003-02-01 06:24:36 +00003100marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003101{
3102 if (self->num_marks < 1) {
3103 PyErr_SetString(UnpicklingError, "could not find MARK");
3104 return -1;
3105 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003106
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003107 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003108}
3109
Tim Peters84e87f32001-03-17 04:50:51 +00003110
Guido van Rossum60456fd1997-04-09 17:36:32 +00003111static int
Tim Peterscba30e22003-02-01 06:24:36 +00003112load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003113{
3114 PDATA_APPEND(self->stack, Py_None, -1);
3115 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003116}
3117
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003118static int
Tim Peterscba30e22003-02-01 06:24:36 +00003119bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003120{
3121 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3122 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003123}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003124
3125static int
Tim Peterscba30e22003-02-01 06:24:36 +00003126load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003127{
3128 PyObject *py_int = 0;
3129 char *endptr, *s;
3130 int len, res = -1;
3131 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003132
Tim Peters0bc93f52003-02-02 18:29:33 +00003133 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003134 if (len < 2) return bad_readline();
3135 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003136
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003137 errno = 0;
3138 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003139
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003140 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3141 /* Hm, maybe we've got something long. Let's try reading
3142 it as a Python long object. */
3143 errno = 0;
3144 py_int = PyLong_FromString(s, NULL, 0);
3145 if (py_int == NULL) {
3146 PyErr_SetString(PyExc_ValueError,
3147 "could not convert string to int");
3148 goto finally;
3149 }
3150 }
3151 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003152 if (len == 3 && (l == 0 || l == 1)) {
3153 if (!( py_int = PyBool_FromLong(l))) goto finally;
3154 }
3155 else {
3156 if (!( py_int = PyInt_FromLong(l))) goto finally;
3157 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003158 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003159
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003160 free(s);
3161 PDATA_PUSH(self->stack, py_int, -1);
3162 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003163
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003164 finally:
3165 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003166
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003167 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003168}
3169
Tim Peters3c67d792003-02-02 17:59:11 +00003170static int
3171load_bool(Unpicklerobject *self, PyObject *boolean)
3172{
3173 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003174 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003175 return 0;
3176}
3177
Tim Petersee1a53c2003-02-02 02:57:53 +00003178/* s contains x bytes of a little-endian integer. Return its value as a
3179 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3180 * int, but when x is 4 it's a signed one. This is an historical source
3181 * of x-platform bugs.
3182 */
Tim Peters84e87f32001-03-17 04:50:51 +00003183static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003184calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003185{
3186 unsigned char c;
3187 int i;
3188 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003189
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003190 for (i = 0, l = 0L; i < x; i++) {
3191 c = (unsigned char)s[i];
3192 l |= (long)c << (i * 8);
3193 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003194#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003195 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3196 * is signed, so on a box with longs bigger than 4 bytes we need
3197 * to extend a BININT's sign bit to the full width.
3198 */
3199 if (x == 4 && l & (1L << 31))
3200 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003201#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003202 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003203}
3204
3205
3206static int
Tim Peterscba30e22003-02-01 06:24:36 +00003207load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003208{
3209 PyObject *py_int = 0;
3210 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003211
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003212 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003213
Tim Peterscba30e22003-02-01 06:24:36 +00003214 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003215 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003216
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003217 PDATA_PUSH(self->stack, py_int, -1);
3218 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003219}
3220
3221
3222static int
Tim Peterscba30e22003-02-01 06:24:36 +00003223load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003224{
3225 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003226
Tim Peters0bc93f52003-02-02 18:29:33 +00003227 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003228 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003229
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003230 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003231}
3232
3233
3234static int
Tim Peterscba30e22003-02-01 06:24:36 +00003235load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003236{
3237 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003238
Tim Peters0bc93f52003-02-02 18:29:33 +00003239 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003240 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003241
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003242 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003243}
3244
3245
3246static int
Tim Peterscba30e22003-02-01 06:24:36 +00003247load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003248{
3249 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003250
Tim Peters0bc93f52003-02-02 18:29:33 +00003251 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003252 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003253
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003254 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003255}
Tim Peters84e87f32001-03-17 04:50:51 +00003256
Guido van Rossum60456fd1997-04-09 17:36:32 +00003257static int
Tim Peterscba30e22003-02-01 06:24:36 +00003258load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003259{
3260 PyObject *l = 0;
3261 char *end, *s;
3262 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003263
Tim Peters0bc93f52003-02-02 18:29:33 +00003264 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003265 if (len < 2) return bad_readline();
3266 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003267
Tim Peterscba30e22003-02-01 06:24:36 +00003268 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003269 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003270
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003271 free(s);
3272 PDATA_PUSH(self->stack, l, -1);
3273 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003274
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003275 finally:
3276 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003277
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003278 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003279}
3280
Tim Petersee1a53c2003-02-02 02:57:53 +00003281/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3282 * data following.
3283 */
3284static int
3285load_counted_long(Unpicklerobject *self, int size)
3286{
3287 int i;
3288 char *nbytes;
3289 unsigned char *pdata;
3290 PyObject *along;
3291
3292 assert(size == 1 || size == 4);
3293 i = self->read_func(self, &nbytes, size);
3294 if (i < 0) return -1;
3295
3296 size = calc_binint(nbytes, size);
3297 if (size < 0) {
3298 /* Corrupt or hostile pickle -- we never write one like
3299 * this.
3300 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003301 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003302 "byte count");
3303 return -1;
3304 }
3305
3306 if (size == 0)
3307 along = PyLong_FromLong(0L);
3308 else {
3309 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003310 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003311 if (i < 0) return -1;
3312 along = _PyLong_FromByteArray(pdata, (size_t)size,
3313 1 /* little endian */, 1 /* signed */);
3314 }
3315 if (along == NULL)
3316 return -1;
3317 PDATA_PUSH(self->stack, along, -1);
3318 return 0;
3319}
Tim Peters84e87f32001-03-17 04:50:51 +00003320
Guido van Rossum60456fd1997-04-09 17:36:32 +00003321static int
Tim Peterscba30e22003-02-01 06:24:36 +00003322load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003323{
3324 PyObject *py_float = 0;
3325 char *endptr, *s;
3326 int len, res = -1;
3327 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003328
Tim Peters0bc93f52003-02-02 18:29:33 +00003329 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003330 if (len < 2) return bad_readline();
3331 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003332
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003333 errno = 0;
3334 d = strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003335
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003336 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3337 PyErr_SetString(PyExc_ValueError,
3338 "could not convert string to float");
3339 goto finally;
3340 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003341
Tim Peterscba30e22003-02-01 06:24:36 +00003342 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003343 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003344
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003345 free(s);
3346 PDATA_PUSH(self->stack, py_float, -1);
3347 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003348
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003349 finally:
3350 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003351
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003352 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003353}
3354
Guido van Rossum60456fd1997-04-09 17:36:32 +00003355static int
Tim Peterscba30e22003-02-01 06:24:36 +00003356load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003357{
3358 PyObject *py_float = 0;
3359 int s, e;
3360 long fhi, flo;
3361 double x;
3362 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003363
Tim Peters0bc93f52003-02-02 18:29:33 +00003364 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003365 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003366
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003367 /* First byte */
3368 s = (*p>>7) & 1;
3369 e = (*p & 0x7F) << 4;
3370 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003371
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003372 /* Second byte */
3373 e |= (*p>>4) & 0xF;
3374 fhi = (*p & 0xF) << 24;
3375 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003376
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003377 /* Third byte */
3378 fhi |= (*p & 0xFF) << 16;
3379 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003380
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003381 /* Fourth byte */
3382 fhi |= (*p & 0xFF) << 8;
3383 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003384
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003385 /* Fifth byte */
3386 fhi |= *p & 0xFF;
3387 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003388
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003389 /* Sixth byte */
3390 flo = (*p & 0xFF) << 16;
3391 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003392
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003393 /* Seventh byte */
3394 flo |= (*p & 0xFF) << 8;
3395 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003396
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003397 /* Eighth byte */
3398 flo |= *p & 0xFF;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003399
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003400 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
3401 x /= 268435456.0; /* 2**28 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003402
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003403 /* XXX This sadly ignores Inf/NaN */
3404 if (e == 0)
3405 e = -1022;
3406 else {
3407 x += 1.0;
3408 e -= 1023;
3409 }
3410 x = ldexp(x, e);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003411
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003412 if (s)
3413 x = -x;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003414
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003415 if (!( py_float = PyFloat_FromDouble(x))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003416
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003417 PDATA_PUSH(self->stack, py_float, -1);
3418 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003419}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003420
3421static int
Tim Peterscba30e22003-02-01 06:24:36 +00003422load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003423{
3424 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003425 int len, res = -1;
3426 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003427
Tim Peters0bc93f52003-02-02 18:29:33 +00003428 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003429 if (len < 2) return bad_readline();
3430 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003431
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003432
3433 /* Strip outermost quotes */
3434 while (s[len-1] <= ' ')
3435 len--;
3436 if(s[0]=='"' && s[len-1]=='"'){
3437 s[len-1] = '\0';
3438 p = s + 1 ;
3439 len -= 2;
3440 } else if(s[0]=='\'' && s[len-1]=='\''){
3441 s[len-1] = '\0';
3442 p = s + 1 ;
3443 len -= 2;
3444 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003445 goto insecure;
3446 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003447
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003448 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3449 if (str) {
3450 PDATA_PUSH(self->stack, str, -1);
3451 res = 0;
3452 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003453 free(s);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003454 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003455
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003456 insecure:
3457 free(s);
3458 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3459 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003460}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003461
3462
3463static int
Tim Peterscba30e22003-02-01 06:24:36 +00003464load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003465{
3466 PyObject *py_string = 0;
3467 long l;
3468 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003469
Tim Peters0bc93f52003-02-02 18:29:33 +00003470 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003471
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003472 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003473
Tim Peters0bc93f52003-02-02 18:29:33 +00003474 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003475 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003476
Tim Peterscba30e22003-02-01 06:24:36 +00003477 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003478 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003479
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003480 PDATA_PUSH(self->stack, py_string, -1);
3481 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003482}
3483
3484
3485static int
Tim Peterscba30e22003-02-01 06:24:36 +00003486load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003487{
3488 PyObject *py_string = 0;
3489 unsigned char l;
3490 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003491
Tim Peters0bc93f52003-02-02 18:29:33 +00003492 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003493 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003494
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003495 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003496
Tim Peters0bc93f52003-02-02 18:29:33 +00003497 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003498
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003499 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003500
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003501 PDATA_PUSH(self->stack, py_string, -1);
3502 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003503}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003504
3505
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003506#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003507static int
Tim Peterscba30e22003-02-01 06:24:36 +00003508load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003509{
3510 PyObject *str = 0;
3511 int len, res = -1;
3512 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003513
Tim Peters0bc93f52003-02-02 18:29:33 +00003514 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003515 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003516
Tim Peterscba30e22003-02-01 06:24:36 +00003517 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003518 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003519
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003520 PDATA_PUSH(self->stack, str, -1);
3521 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003522
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003523 finally:
3524 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003525}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003526#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003527
3528
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003529#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003530static int
Tim Peterscba30e22003-02-01 06:24:36 +00003531load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003532{
3533 PyObject *unicode;
3534 long l;
3535 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003536
Tim Peters0bc93f52003-02-02 18:29:33 +00003537 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003538
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003539 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003540
Tim Peters0bc93f52003-02-02 18:29:33 +00003541 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003542 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003543
Tim Peterscba30e22003-02-01 06:24:36 +00003544 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003545 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003546
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003547 PDATA_PUSH(self->stack, unicode, -1);
3548 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003549}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003550#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003551
3552
3553static int
Tim Peterscba30e22003-02-01 06:24:36 +00003554load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003555{
3556 PyObject *tup;
3557 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003558
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003559 if ((i = marker(self)) < 0) return -1;
3560 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3561 PDATA_PUSH(self->stack, tup, -1);
3562 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003563}
3564
3565static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003566load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003567{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003568 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003569
Tim Peters1d63c9f2003-02-02 20:29:39 +00003570 if (tup == NULL)
3571 return -1;
3572
3573 while (--len >= 0) {
3574 PyObject *element;
3575
3576 PDATA_POP(self->stack, element);
3577 if (element == NULL)
3578 return -1;
3579 PyTuple_SET_ITEM(tup, len, element);
3580 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003581 PDATA_PUSH(self->stack, tup, -1);
3582 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003583}
3584
3585static int
Tim Peterscba30e22003-02-01 06:24:36 +00003586load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003587{
3588 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003589
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003590 if (!( list=PyList_New(0))) return -1;
3591 PDATA_PUSH(self->stack, list, -1);
3592 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003593}
3594
3595static int
Tim Peterscba30e22003-02-01 06:24:36 +00003596load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003597{
3598 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003599
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003600 if (!( dict=PyDict_New())) return -1;
3601 PDATA_PUSH(self->stack, dict, -1);
3602 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003603}
3604
3605
3606static int
Tim Peterscba30e22003-02-01 06:24:36 +00003607load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003608{
3609 PyObject *list = 0;
3610 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003611
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003612 if ((i = marker(self)) < 0) return -1;
3613 if (!( list=Pdata_popList(self->stack, i))) return -1;
3614 PDATA_PUSH(self->stack, list, -1);
3615 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003616}
3617
3618static int
Tim Peterscba30e22003-02-01 06:24:36 +00003619load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003620{
3621 PyObject *dict, *key, *value;
3622 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003624 if ((i = marker(self)) < 0) return -1;
3625 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003626
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003627 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003628
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003629 for (k = i+1; k < j; k += 2) {
3630 key =self->stack->data[k-1];
3631 value=self->stack->data[k ];
3632 if (PyDict_SetItem(dict, key, value) < 0) {
3633 Py_DECREF(dict);
3634 return -1;
3635 }
3636 }
3637 Pdata_clear(self->stack, i);
3638 PDATA_PUSH(self->stack, dict, -1);
3639 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003640}
3641
3642static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003643Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003644{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003645 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003646
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003647 if (PyClass_Check(cls)) {
3648 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003649
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003650 if ((l=PyObject_Size(args)) < 0) goto err;
3651 if (!( l )) {
3652 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003653
Tim Peterscba30e22003-02-01 06:24:36 +00003654 __getinitargs__ = PyObject_GetAttr(cls,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003655 __getinitargs___str);
3656 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003657 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003658 so bypass usual construction */
3659 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003660
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003661 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003662 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003663 goto err;
3664 return inst;
3665 }
3666 Py_DECREF(__getinitargs__);
3667 }
Tim Peters84e87f32001-03-17 04:50:51 +00003668
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003669 if ((r=PyInstance_New(cls, args, NULL))) return r;
3670 else goto err;
3671 }
Tim Peters84e87f32001-03-17 04:50:51 +00003672
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003673 if (args==Py_None) {
3674 /* Special case, call cls.__basicnew__() */
3675 PyObject *basicnew;
Tim Peters84e87f32001-03-17 04:50:51 +00003676
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003677 basicnew = PyObject_GetAttr(cls, __basicnew___str);
3678 if (!basicnew) return NULL;
3679 r=PyObject_CallObject(basicnew, NULL);
3680 Py_DECREF(basicnew);
3681 if (r) return r;
3682 }
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003683
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003684 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003685
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003686 err:
3687 {
3688 PyObject *tp, *v, *tb;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003689
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003690 PyErr_Fetch(&tp, &v, &tb);
3691 if ((r=Py_BuildValue("OOO",v,cls,args))) {
3692 Py_XDECREF(v);
3693 v=r;
3694 }
3695 PyErr_Restore(tp,v,tb);
3696 }
3697 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003698}
Tim Peters84e87f32001-03-17 04:50:51 +00003699
Guido van Rossum60456fd1997-04-09 17:36:32 +00003700
3701static int
Tim Peterscba30e22003-02-01 06:24:36 +00003702load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003703{
3704 PyObject *class, *tup, *obj=0;
3705 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003706
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003707 if ((i = marker(self)) < 0) return -1;
3708 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3709 PDATA_POP(self->stack, class);
3710 if (class) {
3711 obj = Instance_New(class, tup);
3712 Py_DECREF(class);
3713 }
3714 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003716 if (! obj) return -1;
3717 PDATA_PUSH(self->stack, obj, -1);
3718 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003719}
3720
3721
3722static int
Tim Peterscba30e22003-02-01 06:24:36 +00003723load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003724{
3725 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3726 int i, len;
3727 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003728
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003729 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003730
Tim Peters0bc93f52003-02-02 18:29:33 +00003731 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003732 if (len < 2) return bad_readline();
3733 module_name = PyString_FromStringAndSize(s, len - 1);
3734 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003735
Tim Peters0bc93f52003-02-02 18:29:33 +00003736 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003737 if (len < 2) return bad_readline();
3738 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003739 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003740 self->find_class);
3741 Py_DECREF(class_name);
3742 }
3743 }
3744 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003745
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003746 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003747
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003748 if ((tup=Pdata_popTuple(self->stack, i))) {
3749 obj = Instance_New(class, tup);
3750 Py_DECREF(tup);
3751 }
3752 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003753
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003754 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003756 PDATA_PUSH(self->stack, obj, -1);
3757 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003758}
3759
Tim Peterseab7db32003-02-13 18:24:14 +00003760static int
3761load_newobj(Unpicklerobject *self)
3762{
3763 PyObject *args = NULL;
3764 PyObject *clsraw = NULL;
3765 PyTypeObject *cls; /* clsraw cast to its true type */
3766 PyObject *obj;
3767
3768 /* Stack is ... cls argtuple, and we want to call
3769 * cls.__new__(cls, *argtuple).
3770 */
3771 PDATA_POP(self->stack, args);
3772 if (args == NULL) goto Fail;
3773 if (! PyTuple_Check(args)) {
3774 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3775 "tuple.");
3776 goto Fail;
3777 }
3778
3779 PDATA_POP(self->stack, clsraw);
3780 cls = (PyTypeObject *)clsraw;
3781 if (cls == NULL) goto Fail;
3782 if (! PyType_Check(cls)) {
3783 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3784 "isn't a type object");
3785 goto Fail;
3786 }
3787 if (cls->tp_new == NULL) {
3788 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3789 "has NULL tp_new");
3790 goto Fail;
3791 }
3792
3793 /* Call __new__. */
3794 obj = cls->tp_new(cls, args, NULL);
3795 if (obj == NULL) goto Fail;
3796
3797 Py_DECREF(args);
3798 Py_DECREF(clsraw);
3799 PDATA_PUSH(self->stack, obj, -1);
3800 return 0;
3801
3802 Fail:
3803 Py_XDECREF(args);
3804 Py_XDECREF(clsraw);
3805 return -1;
3806}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003807
3808static int
Tim Peterscba30e22003-02-01 06:24:36 +00003809load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003810{
3811 PyObject *class = 0, *module_name = 0, *class_name = 0;
3812 int len;
3813 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003814
Tim Peters0bc93f52003-02-02 18:29:33 +00003815 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003816 if (len < 2) return bad_readline();
3817 module_name = PyString_FromStringAndSize(s, len - 1);
3818 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003819
Tim Peters0bc93f52003-02-02 18:29:33 +00003820 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003821 if (len < 2) {
3822 Py_DECREF(module_name);
3823 return bad_readline();
3824 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003825 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003826 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003827 self->find_class);
3828 Py_DECREF(class_name);
3829 }
3830 }
3831 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003832
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003833 if (! class) return -1;
3834 PDATA_PUSH(self->stack, class, -1);
3835 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003836}
3837
3838
3839static int
Tim Peterscba30e22003-02-01 06:24:36 +00003840load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003841{
3842 PyObject *pid = 0;
3843 int len;
3844 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003845
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003846 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003847 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003848 if (len < 2) return bad_readline();
3849
3850 pid = PyString_FromStringAndSize(s, len - 1);
3851 if (!pid) return -1;
3852
3853 if (PyList_Check(self->pers_func)) {
3854 if (PyList_Append(self->pers_func, pid) < 0) {
3855 Py_DECREF(pid);
3856 return -1;
3857 }
3858 }
3859 else {
3860 ARG_TUP(self, pid);
3861 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003862 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003863 NULL);
3864 FREE_ARG_TUP(self);
3865 }
3866 }
3867
3868 if (! pid) return -1;
3869
3870 PDATA_PUSH(self->stack, pid, -1);
3871 return 0;
3872 }
3873 else {
3874 PyErr_SetString(UnpicklingError,
3875 "A load persistent id instruction was encountered,\n"
3876 "but no persistent_load function was specified.");
3877 return -1;
3878 }
3879}
3880
3881static int
Tim Peterscba30e22003-02-01 06:24:36 +00003882load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003883{
3884 PyObject *pid = 0;
3885
3886 if (self->pers_func) {
3887 PDATA_POP(self->stack, pid);
3888 if (! pid) return -1;
3889
3890 if (PyList_Check(self->pers_func)) {
3891 if (PyList_Append(self->pers_func, pid) < 0) {
3892 Py_DECREF(pid);
3893 return -1;
3894 }
3895 }
3896 else {
3897 ARG_TUP(self, pid);
3898 if (self->arg) {
3899 pid = PyObject_Call(self->pers_func, self->arg,
3900 NULL);
3901 FREE_ARG_TUP(self);
3902 }
3903 if (! pid) return -1;
3904 }
3905
3906 PDATA_PUSH(self->stack, pid, -1);
3907 return 0;
3908 }
3909 else {
3910 PyErr_SetString(UnpicklingError,
3911 "A load persistent id instruction was encountered,\n"
3912 "but no persistent_load function was specified.");
3913 return -1;
3914 }
3915}
3916
3917
3918static int
Tim Peterscba30e22003-02-01 06:24:36 +00003919load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003920{
3921 int len;
3922
3923 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3924
3925 /* Note that we split the (pickle.py) stack into two stacks,
3926 an object stack and a mark stack. We have to be clever and
3927 pop the right one. We do this by looking at the top of the
3928 mark stack.
3929 */
3930
3931 if ((self->num_marks > 0) &&
3932 (self->marks[self->num_marks - 1] == len))
3933 self->num_marks--;
3934 else {
3935 len--;
3936 Py_DECREF(self->stack->data[len]);
3937 self->stack->length=len;
3938 }
3939
3940 return 0;
3941}
3942
3943
3944static int
Tim Peterscba30e22003-02-01 06:24:36 +00003945load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003946{
3947 int i;
3948
3949 if ((i = marker(self)) < 0)
3950 return -1;
3951
3952 Pdata_clear(self->stack, i);
3953
3954 return 0;
3955}
3956
3957
3958static int
Tim Peterscba30e22003-02-01 06:24:36 +00003959load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003960{
3961 PyObject *last;
3962 int len;
3963
3964 if ((len = self->stack->length) <= 0) return stackUnderflow();
3965 last=self->stack->data[len-1];
3966 Py_INCREF(last);
3967 PDATA_PUSH(self->stack, last, -1);
3968 return 0;
3969}
3970
3971
3972static int
Tim Peterscba30e22003-02-01 06:24:36 +00003973load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003974{
3975 PyObject *py_str = 0, *value = 0;
3976 int len;
3977 char *s;
3978 int rc;
3979
Tim Peters0bc93f52003-02-02 18:29:33 +00003980 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003981 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003982
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003983 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003984
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003985 value = PyDict_GetItem(self->memo, py_str);
3986 if (! value) {
3987 PyErr_SetObject(BadPickleGet, py_str);
3988 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003989 }
3990 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003991 PDATA_APPEND(self->stack, value, -1);
3992 rc = 0;
3993 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003994
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003995 Py_DECREF(py_str);
3996 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003997}
3998
3999
4000static int
Tim Peterscba30e22003-02-01 06:24:36 +00004001load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004002{
4003 PyObject *py_key = 0, *value = 0;
4004 unsigned char key;
4005 char *s;
4006 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004007
Tim Peters0bc93f52003-02-02 18:29:33 +00004008 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004009
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004010 key = (unsigned char)s[0];
4011 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00004012
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004013 value = PyDict_GetItem(self->memo, py_key);
4014 if (! value) {
4015 PyErr_SetObject(BadPickleGet, py_key);
4016 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004017 }
4018 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004019 PDATA_APPEND(self->stack, value, -1);
4020 rc = 0;
4021 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004022
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004023 Py_DECREF(py_key);
4024 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004025}
4026
4027
4028static int
Tim Peterscba30e22003-02-01 06:24:36 +00004029load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004030{
4031 PyObject *py_key = 0, *value = 0;
4032 unsigned char c;
4033 char *s;
4034 long key;
4035 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004036
Tim Peters0bc93f52003-02-02 18:29:33 +00004037 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004038
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004039 c = (unsigned char)s[0];
4040 key = (long)c;
4041 c = (unsigned char)s[1];
4042 key |= (long)c << 8;
4043 c = (unsigned char)s[2];
4044 key |= (long)c << 16;
4045 c = (unsigned char)s[3];
4046 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004047
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004048 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4049
4050 value = PyDict_GetItem(self->memo, py_key);
4051 if (! value) {
4052 PyErr_SetObject(BadPickleGet, py_key);
4053 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004054 }
4055 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004056 PDATA_APPEND(self->stack, value, -1);
4057 rc = 0;
4058 }
4059
4060 Py_DECREF(py_key);
4061 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004062}
4063
Tim Peters2d629652003-02-04 05:06:17 +00004064/* Push an object from the extension registry (EXT[124]). nbytes is
4065 * the number of bytes following the opcode, holding the index (code) value.
4066 */
4067static int
4068load_extension(Unpicklerobject *self, int nbytes)
4069{
4070 char *codebytes; /* the nbytes bytes after the opcode */
4071 long code; /* calc_binint returns long */
4072 PyObject *py_code; /* code as a Python int */
4073 PyObject *obj; /* the object to push */
4074 PyObject *pair; /* (module_name, class_name) */
4075 PyObject *module_name, *class_name;
4076
4077 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4078 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4079 code = calc_binint(codebytes, nbytes);
4080 if (code <= 0) { /* note that 0 is forbidden */
4081 /* Corrupt or hostile pickle. */
4082 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4083 return -1;
4084 }
4085
4086 /* Look for the code in the cache. */
4087 py_code = PyInt_FromLong(code);
4088 if (py_code == NULL) return -1;
4089 obj = PyDict_GetItem(extension_cache, py_code);
4090 if (obj != NULL) {
4091 /* Bingo. */
4092 Py_DECREF(py_code);
4093 PDATA_APPEND(self->stack, obj, -1);
4094 return 0;
4095 }
4096
4097 /* Look up the (module_name, class_name) pair. */
4098 pair = PyDict_GetItem(inverted_registry, py_code);
4099 if (pair == NULL) {
4100 Py_DECREF(py_code);
4101 PyErr_Format(PyExc_ValueError, "unregistered extension "
4102 "code %ld", code);
4103 return -1;
4104 }
4105 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004106 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004107 */
4108 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4109 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4110 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4111 Py_DECREF(py_code);
4112 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4113 "isn't a 2-tuple of strings", code);
4114 return -1;
4115 }
4116 /* Load the object. */
4117 obj = find_class(module_name, class_name, self->find_class);
4118 if (obj == NULL) {
4119 Py_DECREF(py_code);
4120 return -1;
4121 }
4122 /* Cache code -> obj. */
4123 code = PyDict_SetItem(extension_cache, py_code, obj);
4124 Py_DECREF(py_code);
4125 if (code < 0) {
4126 Py_DECREF(obj);
4127 return -1;
4128 }
4129 PDATA_PUSH(self->stack, obj, -1);
4130 return 0;
4131}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004132
4133static int
Tim Peterscba30e22003-02-01 06:24:36 +00004134load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004135{
4136 PyObject *py_str = 0, *value = 0;
4137 int len, l;
4138 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004139
Tim Peters0bc93f52003-02-02 18:29:33 +00004140 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004141 if (l < 2) return bad_readline();
4142 if (!( len=self->stack->length )) return stackUnderflow();
4143 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4144 value=self->stack->data[len-1];
4145 l=PyDict_SetItem(self->memo, py_str, value);
4146 Py_DECREF(py_str);
4147 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004148}
4149
4150
4151static int
Tim Peterscba30e22003-02-01 06:24:36 +00004152load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004153{
4154 PyObject *py_key = 0, *value = 0;
4155 unsigned char key;
4156 char *s;
4157 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004158
Tim Peters0bc93f52003-02-02 18:29:33 +00004159 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004160 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004161
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004162 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004163
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004164 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4165 value=self->stack->data[len-1];
4166 len=PyDict_SetItem(self->memo, py_key, value);
4167 Py_DECREF(py_key);
4168 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004169}
4170
4171
4172static int
Tim Peterscba30e22003-02-01 06:24:36 +00004173load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004174{
4175 PyObject *py_key = 0, *value = 0;
4176 long key;
4177 unsigned char c;
4178 char *s;
4179 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004180
Tim Peters0bc93f52003-02-02 18:29:33 +00004181 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004182 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004184 c = (unsigned char)s[0];
4185 key = (long)c;
4186 c = (unsigned char)s[1];
4187 key |= (long)c << 8;
4188 c = (unsigned char)s[2];
4189 key |= (long)c << 16;
4190 c = (unsigned char)s[3];
4191 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004193 if (!( py_key = PyInt_FromLong(key))) return -1;
4194 value=self->stack->data[len-1];
4195 len=PyDict_SetItem(self->memo, py_key, value);
4196 Py_DECREF(py_key);
4197 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004198}
4199
4200
4201static int
Tim Peterscba30e22003-02-01 06:24:36 +00004202do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004203{
4204 PyObject *value = 0, *list = 0, *append_method = 0;
4205 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004206
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004207 len=self->stack->length;
4208 if (!( len >= x && x > 0 )) return stackUnderflow();
4209 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004210 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004211
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004212 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004213
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004214 if (PyList_Check(list)) {
4215 PyObject *slice;
4216 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004217
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004218 slice=Pdata_popList(self->stack, x);
4219 list_len = PyList_GET_SIZE(list);
4220 i=PyList_SetSlice(list, list_len, list_len, slice);
4221 Py_DECREF(slice);
4222 return i;
4223 }
4224 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004225
Tim Peterscba30e22003-02-01 06:24:36 +00004226 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004227 return -1;
4228
4229 for (i = x; i < len; i++) {
4230 PyObject *junk;
4231
4232 value=self->stack->data[i];
4233 junk=0;
4234 ARG_TUP(self, value);
4235 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004236 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004237 NULL);
4238 FREE_ARG_TUP(self);
4239 }
4240 if (! junk) {
4241 Pdata_clear(self->stack, i+1);
4242 self->stack->length=x;
4243 Py_DECREF(append_method);
4244 return -1;
4245 }
4246 Py_DECREF(junk);
4247 }
4248 self->stack->length=x;
4249 Py_DECREF(append_method);
4250 }
4251
4252 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004253}
4254
4255
4256static int
Tim Peterscba30e22003-02-01 06:24:36 +00004257load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004258{
4259 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004260}
4261
4262
4263static int
Tim Peterscba30e22003-02-01 06:24:36 +00004264load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004265{
4266 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004267}
4268
4269
4270static int
Tim Peterscba30e22003-02-01 06:24:36 +00004271do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004272{
4273 PyObject *value = 0, *key = 0, *dict = 0;
4274 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004275
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004276 if (!( (len=self->stack->length) >= x
4277 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004278
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004279 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004280
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004281 for (i = x+1; i < len; i += 2) {
4282 key =self->stack->data[i-1];
4283 value=self->stack->data[i ];
4284 if (PyObject_SetItem(dict, key, value) < 0) {
4285 r=-1;
4286 break;
4287 }
4288 }
4289
4290 Pdata_clear(self->stack, x);
4291
4292 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004293}
4294
4295
Tim Peters84e87f32001-03-17 04:50:51 +00004296static int
Tim Peterscba30e22003-02-01 06:24:36 +00004297load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004298{
4299 return do_setitems(self, self->stack->length - 2);
4300}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004301
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004302static int
Tim Peterscba30e22003-02-01 06:24:36 +00004303load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004304{
4305 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004306}
4307
Tim Peters84e87f32001-03-17 04:50:51 +00004308
Guido van Rossum60456fd1997-04-09 17:36:32 +00004309static int
Tim Peterscba30e22003-02-01 06:24:36 +00004310load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004311{
Tim Peters080c88b2003-02-15 03:01:11 +00004312 PyObject *state, *inst, *slotstate;
4313 PyObject *__setstate__;
4314 PyObject *d_key, *d_value;
4315 int i;
4316 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004317
Tim Peters080c88b2003-02-15 03:01:11 +00004318 /* Stack is ... instance, state. We want to leave instance at
4319 * the stack top, possibly mutated via instance.__setstate__(state).
4320 */
4321 if (self->stack->length < 2)
4322 return stackUnderflow();
4323 PDATA_POP(self->stack, state);
4324 if (state == NULL)
4325 return -1;
4326 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004327
Tim Peters080c88b2003-02-15 03:01:11 +00004328 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4329 if (__setstate__ != NULL) {
4330 PyObject *junk = NULL;
4331
4332 /* The explicit __setstate__ is responsible for everything. */
4333 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004334 if (self->arg) {
4335 junk = PyObject_Call(__setstate__, self->arg, NULL);
4336 FREE_ARG_TUP(self);
4337 }
4338 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004339 if (junk == NULL)
4340 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004341 Py_DECREF(junk);
4342 return 0;
4343 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004344 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004345
4346 /* A default __setstate__. First see whether state embeds a
4347 * slot state dict too (a proto 2 addition).
4348 */
4349 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4350 PyObject *temp = state;
4351 state = PyTuple_GET_ITEM(temp, 0);
4352 slotstate = PyTuple_GET_ITEM(temp, 1);
4353 Py_INCREF(state);
4354 Py_INCREF(slotstate);
4355 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004356 }
Tim Peters080c88b2003-02-15 03:01:11 +00004357 else
4358 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004359
Tim Peters080c88b2003-02-15 03:01:11 +00004360 /* Set inst.__dict__ from the state dict (if any). */
4361 if (state != Py_None) {
4362 PyObject *dict;
4363 if (! PyDict_Check(state)) {
4364 PyErr_SetString(UnpicklingError, "state is not a "
4365 "dictionary");
4366 goto finally;
4367 }
4368 dict = PyObject_GetAttr(inst, __dict___str);
4369 if (dict == NULL)
4370 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004371
Tim Peters080c88b2003-02-15 03:01:11 +00004372 i = 0;
4373 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4374 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4375 goto finally;
4376 }
4377 Py_DECREF(dict);
4378 }
4379
4380 /* Also set instance attributes from the slotstate dict (if any). */
4381 if (slotstate != NULL) {
4382 if (! PyDict_Check(slotstate)) {
4383 PyErr_SetString(UnpicklingError, "slot state is not "
4384 "a dictionary");
4385 goto finally;
4386 }
4387 i = 0;
4388 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4389 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4390 goto finally;
4391 }
4392 }
4393 res = 0;
4394
4395 finally:
4396 Py_DECREF(state);
4397 Py_XDECREF(slotstate);
4398 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004399}
4400
4401
4402static int
Tim Peterscba30e22003-02-01 06:24:36 +00004403load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004404{
4405 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004406
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004407 /* Note that we split the (pickle.py) stack into two stacks, an
4408 object stack and a mark stack. Here we push a mark onto the
4409 mark stack.
4410 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004411
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004412 if ((self->num_marks + 1) >= self->marks_size) {
4413 s=self->marks_size+20;
4414 if (s <= self->num_marks) s=self->num_marks + 1;
4415 if (self->marks == NULL)
4416 self->marks=(int *)malloc(s * sizeof(int));
4417 else
Tim Peterscba30e22003-02-01 06:24:36 +00004418 self->marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004419 s * sizeof(int));
4420 if (! self->marks) {
4421 PyErr_NoMemory();
4422 return -1;
4423 }
4424 self->marks_size = s;
4425 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004426
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004427 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004428
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004429 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004430}
4431
Guido van Rossum60456fd1997-04-09 17:36:32 +00004432static int
Tim Peterscba30e22003-02-01 06:24:36 +00004433load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004434{
4435 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004436
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004437 PDATA_POP(self->stack, arg_tup);
4438 if (! arg_tup) return -1;
4439 PDATA_POP(self->stack, callable);
4440 if (callable) {
4441 ob = Instance_New(callable, arg_tup);
4442 Py_DECREF(callable);
4443 }
4444 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004445
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004446 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004447
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004448 PDATA_PUSH(self->stack, ob, -1);
4449 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004450}
Tim Peters84e87f32001-03-17 04:50:51 +00004451
Tim Peters4190fb82003-02-02 16:09:05 +00004452/* Just raises an error if we don't know the protocol specified. PROTO
4453 * is the first opcode for protocols >= 2.
4454 */
4455static int
4456load_proto(Unpicklerobject *self)
4457{
4458 int i;
4459 char *protobyte;
4460
4461 i = self->read_func(self, &protobyte, 1);
4462 if (i < 0)
4463 return -1;
4464
4465 i = calc_binint(protobyte, 1);
4466 /* No point checking for < 0, since calc_binint returns an unsigned
4467 * int when chewing on 1 byte.
4468 */
4469 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004470 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004471 return 0;
4472
4473 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4474 return -1;
4475}
4476
Guido van Rossum60456fd1997-04-09 17:36:32 +00004477static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004478load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004479{
4480 PyObject *err = 0, *val = 0;
4481 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004482
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004483 self->num_marks = 0;
4484 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004485
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004486 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004487 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004488 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004489
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004490 switch (s[0]) {
4491 case NONE:
4492 if (load_none(self) < 0)
4493 break;
4494 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004495
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004496 case BININT:
4497 if (load_binint(self) < 0)
4498 break;
4499 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004500
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004501 case BININT1:
4502 if (load_binint1(self) < 0)
4503 break;
4504 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004505
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004506 case BININT2:
4507 if (load_binint2(self) < 0)
4508 break;
4509 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004510
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004511 case INT:
4512 if (load_int(self) < 0)
4513 break;
4514 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004515
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004516 case LONG:
4517 if (load_long(self) < 0)
4518 break;
4519 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004520
Tim Petersee1a53c2003-02-02 02:57:53 +00004521 case LONG1:
4522 if (load_counted_long(self, 1) < 0)
4523 break;
4524 continue;
4525
4526 case LONG4:
4527 if (load_counted_long(self, 4) < 0)
4528 break;
4529 continue;
4530
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004531 case FLOAT:
4532 if (load_float(self) < 0)
4533 break;
4534 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004535
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004536 case BINFLOAT:
4537 if (load_binfloat(self) < 0)
4538 break;
4539 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004540
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004541 case BINSTRING:
4542 if (load_binstring(self) < 0)
4543 break;
4544 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004546 case SHORT_BINSTRING:
4547 if (load_short_binstring(self) < 0)
4548 break;
4549 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004550
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004551 case STRING:
4552 if (load_string(self) < 0)
4553 break;
4554 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004555
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004556#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004557 case UNICODE:
4558 if (load_unicode(self) < 0)
4559 break;
4560 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004561
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004562 case BINUNICODE:
4563 if (load_binunicode(self) < 0)
4564 break;
4565 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004566#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004567
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004568 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004569 if (load_counted_tuple(self, 0) < 0)
4570 break;
4571 continue;
4572
4573 case TUPLE1:
4574 if (load_counted_tuple(self, 1) < 0)
4575 break;
4576 continue;
4577
4578 case TUPLE2:
4579 if (load_counted_tuple(self, 2) < 0)
4580 break;
4581 continue;
4582
4583 case TUPLE3:
4584 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004585 break;
4586 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004587
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004588 case TUPLE:
4589 if (load_tuple(self) < 0)
4590 break;
4591 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004592
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004593 case EMPTY_LIST:
4594 if (load_empty_list(self) < 0)
4595 break;
4596 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004597
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004598 case LIST:
4599 if (load_list(self) < 0)
4600 break;
4601 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004602
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004603 case EMPTY_DICT:
4604 if (load_empty_dict(self) < 0)
4605 break;
4606 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004607
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004608 case DICT:
4609 if (load_dict(self) < 0)
4610 break;
4611 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004613 case OBJ:
4614 if (load_obj(self) < 0)
4615 break;
4616 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004617
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004618 case INST:
4619 if (load_inst(self) < 0)
4620 break;
4621 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004622
Tim Peterseab7db32003-02-13 18:24:14 +00004623 case NEWOBJ:
4624 if (load_newobj(self) < 0)
4625 break;
4626 continue;
4627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004628 case GLOBAL:
4629 if (load_global(self) < 0)
4630 break;
4631 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004632
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004633 case APPEND:
4634 if (load_append(self) < 0)
4635 break;
4636 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004637
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004638 case APPENDS:
4639 if (load_appends(self) < 0)
4640 break;
4641 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004642
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004643 case BUILD:
4644 if (load_build(self) < 0)
4645 break;
4646 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004648 case DUP:
4649 if (load_dup(self) < 0)
4650 break;
4651 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004652
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004653 case BINGET:
4654 if (load_binget(self) < 0)
4655 break;
4656 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004658 case LONG_BINGET:
4659 if (load_long_binget(self) < 0)
4660 break;
4661 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004662
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004663 case GET:
4664 if (load_get(self) < 0)
4665 break;
4666 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004667
Tim Peters2d629652003-02-04 05:06:17 +00004668 case EXT1:
4669 if (load_extension(self, 1) < 0)
4670 break;
4671 continue;
4672
4673 case EXT2:
4674 if (load_extension(self, 2) < 0)
4675 break;
4676 continue;
4677
4678 case EXT4:
4679 if (load_extension(self, 4) < 0)
4680 break;
4681 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004682 case MARK:
4683 if (load_mark(self) < 0)
4684 break;
4685 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004686
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004687 case BINPUT:
4688 if (load_binput(self) < 0)
4689 break;
4690 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004691
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004692 case LONG_BINPUT:
4693 if (load_long_binput(self) < 0)
4694 break;
4695 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004696
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004697 case PUT:
4698 if (load_put(self) < 0)
4699 break;
4700 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004701
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004702 case POP:
4703 if (load_pop(self) < 0)
4704 break;
4705 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004706
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004707 case POP_MARK:
4708 if (load_pop_mark(self) < 0)
4709 break;
4710 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004711
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004712 case SETITEM:
4713 if (load_setitem(self) < 0)
4714 break;
4715 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004716
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004717 case SETITEMS:
4718 if (load_setitems(self) < 0)
4719 break;
4720 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004721
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004722 case STOP:
4723 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004724
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004725 case PERSID:
4726 if (load_persid(self) < 0)
4727 break;
4728 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004729
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004730 case BINPERSID:
4731 if (load_binpersid(self) < 0)
4732 break;
4733 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004734
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004735 case REDUCE:
4736 if (load_reduce(self) < 0)
4737 break;
4738 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004739
Tim Peters4190fb82003-02-02 16:09:05 +00004740 case PROTO:
4741 if (load_proto(self) < 0)
4742 break;
4743 continue;
4744
Tim Peters3c67d792003-02-02 17:59:11 +00004745 case NEWTRUE:
4746 if (load_bool(self, Py_True) < 0)
4747 break;
4748 continue;
4749
4750 case NEWFALSE:
4751 if (load_bool(self, Py_False) < 0)
4752 break;
4753 continue;
4754
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004755 case '\0':
4756 /* end of file */
4757 PyErr_SetNone(PyExc_EOFError);
4758 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004759
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004760 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004761 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004762 "invalid load key, '%s'.",
4763 "c", s[0]);
4764 return NULL;
4765 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004766
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004767 break;
4768 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004769
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004770 if ((err = PyErr_Occurred())) {
4771 if (err == PyExc_EOFError) {
4772 PyErr_SetNone(PyExc_EOFError);
4773 }
4774 return NULL;
4775 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004777 PDATA_POP(self->stack, val);
4778 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004779}
Tim Peters84e87f32001-03-17 04:50:51 +00004780
Guido van Rossum60456fd1997-04-09 17:36:32 +00004781
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004782/* No-load functions to support noload, which is used to
4783 find persistent references. */
4784
4785static int
Tim Peterscba30e22003-02-01 06:24:36 +00004786noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004787{
4788 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004789
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004790 if ((i = marker(self)) < 0) return -1;
4791 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004792}
4793
4794
4795static int
Tim Peterscba30e22003-02-01 06:24:36 +00004796noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004797{
4798 int i;
4799 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004800
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004801 if ((i = marker(self)) < 0) return -1;
4802 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004803 if (self->readline_func(self, &s) < 0) return -1;
4804 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004805 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004806 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004807}
4808
4809static int
Tim Peterseab7db32003-02-13 18:24:14 +00004810noload_newobj(Unpicklerobject *self)
4811{
4812 PyObject *obj;
4813
4814 PDATA_POP(self->stack, obj); /* pop argtuple */
4815 if (obj == NULL) return -1;
4816 Py_DECREF(obj);
4817
4818 PDATA_POP(self->stack, obj); /* pop cls */
4819 if (obj == NULL) return -1;
4820 Py_DECREF(obj);
4821
4822 PDATA_APPEND(self->stack, Py_None, -1);
4823 return 0;
4824}
4825
4826static int
Tim Peterscba30e22003-02-01 06:24:36 +00004827noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004828{
4829 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004830
Tim Peters0bc93f52003-02-02 18:29:33 +00004831 if (self->readline_func(self, &s) < 0) return -1;
4832 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004833 PDATA_APPEND(self->stack, Py_None,-1);
4834 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004835}
4836
4837static int
Tim Peterscba30e22003-02-01 06:24:36 +00004838noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004839{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004840
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004841 if (self->stack->length < 2) return stackUnderflow();
4842 Pdata_clear(self->stack, self->stack->length-2);
4843 PDATA_APPEND(self->stack, Py_None,-1);
4844 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004845}
4846
4847static int
4848noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004849
Guido van Rossum053b8df1998-11-25 16:18:00 +00004850 if (self->stack->length < 1) return stackUnderflow();
4851 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004852 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004853}
4854
Tim Peters2d629652003-02-04 05:06:17 +00004855static int
4856noload_extension(Unpicklerobject *self, int nbytes)
4857{
4858 char *codebytes;
4859
4860 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4861 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4862 PDATA_APPEND(self->stack, Py_None, -1);
4863 return 0;
4864}
4865
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004866
4867static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004868noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004869{
4870 PyObject *err = 0, *val = 0;
4871 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004872
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004873 self->num_marks = 0;
4874 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004875
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004876 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004877 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004878 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004879
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004880 switch (s[0]) {
4881 case NONE:
4882 if (load_none(self) < 0)
4883 break;
4884 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004885
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004886 case BININT:
4887 if (load_binint(self) < 0)
4888 break;
4889 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004890
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004891 case BININT1:
4892 if (load_binint1(self) < 0)
4893 break;
4894 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004895
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004896 case BININT2:
4897 if (load_binint2(self) < 0)
4898 break;
4899 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004900
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004901 case INT:
4902 if (load_int(self) < 0)
4903 break;
4904 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004905
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004906 case LONG:
4907 if (load_long(self) < 0)
4908 break;
4909 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004910
Tim Peters4190fb82003-02-02 16:09:05 +00004911 case LONG1:
4912 if (load_counted_long(self, 1) < 0)
4913 break;
4914 continue;
4915
4916 case LONG4:
4917 if (load_counted_long(self, 4) < 0)
4918 break;
4919 continue;
4920
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004921 case FLOAT:
4922 if (load_float(self) < 0)
4923 break;
4924 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004925
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004926 case BINFLOAT:
4927 if (load_binfloat(self) < 0)
4928 break;
4929 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004930
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004931 case BINSTRING:
4932 if (load_binstring(self) < 0)
4933 break;
4934 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004935
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004936 case SHORT_BINSTRING:
4937 if (load_short_binstring(self) < 0)
4938 break;
4939 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004940
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004941 case STRING:
4942 if (load_string(self) < 0)
4943 break;
4944 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004945
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004946#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004947 case UNICODE:
4948 if (load_unicode(self) < 0)
4949 break;
4950 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004951
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004952 case BINUNICODE:
4953 if (load_binunicode(self) < 0)
4954 break;
4955 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004956#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004957
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004958 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004959 if (load_counted_tuple(self, 0) < 0)
4960 break;
4961 continue;
4962
4963 case TUPLE1:
4964 if (load_counted_tuple(self, 1) < 0)
4965 break;
4966 continue;
4967
4968 case TUPLE2:
4969 if (load_counted_tuple(self, 2) < 0)
4970 break;
4971 continue;
4972
4973 case TUPLE3:
4974 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004975 break;
4976 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004977
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004978 case TUPLE:
4979 if (load_tuple(self) < 0)
4980 break;
4981 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004982
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004983 case EMPTY_LIST:
4984 if (load_empty_list(self) < 0)
4985 break;
4986 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004988 case LIST:
4989 if (load_list(self) < 0)
4990 break;
4991 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004992
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004993 case EMPTY_DICT:
4994 if (load_empty_dict(self) < 0)
4995 break;
4996 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004997
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004998 case DICT:
4999 if (load_dict(self) < 0)
5000 break;
5001 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005002
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005003 case OBJ:
5004 if (noload_obj(self) < 0)
5005 break;
5006 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005007
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005008 case INST:
5009 if (noload_inst(self) < 0)
5010 break;
5011 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005012
Tim Peterseab7db32003-02-13 18:24:14 +00005013 case NEWOBJ:
5014 if (noload_newobj(self) < 0)
5015 break;
5016 continue;
5017
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005018 case GLOBAL:
5019 if (noload_global(self) < 0)
5020 break;
5021 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005022
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005023 case APPEND:
5024 if (load_append(self) < 0)
5025 break;
5026 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005027
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005028 case APPENDS:
5029 if (load_appends(self) < 0)
5030 break;
5031 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005032
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005033 case BUILD:
5034 if (noload_build(self) < 0)
5035 break;
5036 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005037
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005038 case DUP:
5039 if (load_dup(self) < 0)
5040 break;
5041 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005042
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005043 case BINGET:
5044 if (load_binget(self) < 0)
5045 break;
5046 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005047
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005048 case LONG_BINGET:
5049 if (load_long_binget(self) < 0)
5050 break;
5051 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005052
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005053 case GET:
5054 if (load_get(self) < 0)
5055 break;
5056 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005057
Tim Peters2d629652003-02-04 05:06:17 +00005058 case EXT1:
5059 if (noload_extension(self, 1) < 0)
5060 break;
5061 continue;
5062
5063 case EXT2:
5064 if (noload_extension(self, 2) < 0)
5065 break;
5066 continue;
5067
5068 case EXT4:
5069 if (noload_extension(self, 4) < 0)
5070 break;
5071 continue;
5072
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005073 case MARK:
5074 if (load_mark(self) < 0)
5075 break;
5076 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005077
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005078 case BINPUT:
5079 if (load_binput(self) < 0)
5080 break;
5081 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005082
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005083 case LONG_BINPUT:
5084 if (load_long_binput(self) < 0)
5085 break;
5086 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005087
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005088 case PUT:
5089 if (load_put(self) < 0)
5090 break;
5091 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005092
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005093 case POP:
5094 if (load_pop(self) < 0)
5095 break;
5096 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005097
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005098 case POP_MARK:
5099 if (load_pop_mark(self) < 0)
5100 break;
5101 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005102
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005103 case SETITEM:
5104 if (load_setitem(self) < 0)
5105 break;
5106 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005107
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005108 case SETITEMS:
5109 if (load_setitems(self) < 0)
5110 break;
5111 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005112
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005113 case STOP:
5114 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005115
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005116 case PERSID:
5117 if (load_persid(self) < 0)
5118 break;
5119 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005120
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005121 case BINPERSID:
5122 if (load_binpersid(self) < 0)
5123 break;
5124 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005125
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005126 case REDUCE:
5127 if (noload_reduce(self) < 0)
5128 break;
5129 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005130
Tim Peters4190fb82003-02-02 16:09:05 +00005131 case PROTO:
5132 if (load_proto(self) < 0)
5133 break;
5134 continue;
5135
Tim Peters3c67d792003-02-02 17:59:11 +00005136 case NEWTRUE:
5137 if (load_bool(self, Py_True) < 0)
5138 break;
5139 continue;
5140
5141 case NEWFALSE:
5142 if (load_bool(self, Py_False) < 0)
5143 break;
5144 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005145 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005146 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005147 "invalid load key, '%s'.",
5148 "c", s[0]);
5149 return NULL;
5150 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005151
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005152 break;
5153 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005154
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005155 if ((err = PyErr_Occurred())) {
5156 if (err == PyExc_EOFError) {
5157 PyErr_SetNone(PyExc_EOFError);
5158 }
5159 return NULL;
5160 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005161
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005162 PDATA_POP(self->stack, val);
5163 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005164}
Tim Peters84e87f32001-03-17 04:50:51 +00005165
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005166
Guido van Rossum60456fd1997-04-09 17:36:32 +00005167static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005168Unpickler_load(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005169{
Tim Peterscba30e22003-02-01 06:24:36 +00005170 if (!( PyArg_ParseTuple(args, ":load")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005171 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005173 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005174}
5175
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005176static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005177Unpickler_noload(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005178{
Tim Peterscba30e22003-02-01 06:24:36 +00005179 if (!( PyArg_ParseTuple(args, ":noload")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005180 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005181
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005182 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005183}
5184
Guido van Rossum60456fd1997-04-09 17:36:32 +00005185
5186static struct PyMethodDef Unpickler_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005187 {"load", (PyCFunction)Unpickler_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005188 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005189 },
Neal Norwitzb0493252002-03-31 14:44:22 +00005190 {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005191 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005192 "noload() -- not load a pickle, but go through most of the motions\n"
5193 "\n"
5194 "This function can be used to read past a pickle without instantiating\n"
5195 "any objects or importing any modules. It can also be used to find all\n"
5196 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005197 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005198 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005199 {NULL, NULL} /* sentinel */
5200};
5201
5202
5203static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005204newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005205{
5206 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005207
Tim Peterscba30e22003-02-01 06:24:36 +00005208 if (!( self = PyObject_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005209 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005210
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005211 self->file = NULL;
5212 self->arg = NULL;
5213 self->stack = (Pdata*)Pdata_New();
5214 self->pers_func = NULL;
5215 self->last_string = NULL;
5216 self->marks = NULL;
5217 self->num_marks = 0;
5218 self->marks_size = 0;
5219 self->buf_size = 0;
5220 self->read = NULL;
5221 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005222 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005223
Tim Peterscba30e22003-02-01 06:24:36 +00005224 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005225 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005226
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005227 Py_INCREF(f);
5228 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005229
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005230 /* Set read, readline based on type of f */
5231 if (PyFile_Check(f)) {
5232 self->fp = PyFile_AsFile(f);
5233 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005234 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005235 "I/O operation on closed file");
5236 goto err;
5237 }
5238 self->read_func = read_file;
5239 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005240 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005241 else if (PycStringIO_InputCheck(f)) {
5242 self->fp = NULL;
5243 self->read_func = read_cStringIO;
5244 self->readline_func = readline_cStringIO;
5245 }
5246 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005247
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005248 self->fp = NULL;
5249 self->read_func = read_other;
5250 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005251
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005252 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5253 (self->read = PyObject_GetAttr(f, read_str)))) {
5254 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005255 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005256 "argument must have 'read' and "
5257 "'readline' attributes" );
5258 goto err;
5259 }
5260 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005261
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005262 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005263
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005264 err:
5265 Py_DECREF((PyObject *)self);
5266 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005267}
5268
5269
5270static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005271get_Unpickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005272{
5273 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00005274
Tim Peterscba30e22003-02-01 06:24:36 +00005275 if (!( PyArg_ParseTuple(args, "O:Unpickler", &file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005276 return NULL;
5277 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005278}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005279
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005280
Guido van Rossum60456fd1997-04-09 17:36:32 +00005281static void
Tim Peterscba30e22003-02-01 06:24:36 +00005282Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005283{
5284 Py_XDECREF(self->readline);
5285 Py_XDECREF(self->read);
5286 Py_XDECREF(self->file);
5287 Py_XDECREF(self->memo);
5288 Py_XDECREF(self->stack);
5289 Py_XDECREF(self->pers_func);
5290 Py_XDECREF(self->arg);
5291 Py_XDECREF(self->last_string);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005292
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005293 if (self->marks) {
5294 free(self->marks);
5295 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005297 if (self->buf_size) {
5298 free(self->buf);
5299 }
Tim Peters84e87f32001-03-17 04:50:51 +00005300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005301 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005302}
5303
5304
5305static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005306Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005307{
5308 if (!strcmp(name, "persistent_load")) {
5309 if (!self->pers_func) {
5310 PyErr_SetString(PyExc_AttributeError, name);
5311 return NULL;
5312 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005313
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005314 Py_INCREF(self->pers_func);
5315 return self->pers_func;
5316 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005317
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005318 if (!strcmp(name, "find_global")) {
5319 if (!self->find_class) {
5320 PyErr_SetString(PyExc_AttributeError, name);
5321 return NULL;
5322 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005323
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005324 Py_INCREF(self->find_class);
5325 return self->find_class;
5326 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005327
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005328 if (!strcmp(name, "memo")) {
5329 if (!self->memo) {
5330 PyErr_SetString(PyExc_AttributeError, name);
5331 return NULL;
5332 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005333
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005334 Py_INCREF(self->memo);
5335 return self->memo;
5336 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005337
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005338 if (!strcmp(name, "UnpicklingError")) {
5339 Py_INCREF(UnpicklingError);
5340 return UnpicklingError;
5341 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005342
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005343 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005344}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005345
Guido van Rossum60456fd1997-04-09 17:36:32 +00005346
5347static int
Tim Peterscba30e22003-02-01 06:24:36 +00005348Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005349{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005350
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005351 if (!strcmp(name, "persistent_load")) {
5352 Py_XDECREF(self->pers_func);
5353 self->pers_func = value;
5354 Py_XINCREF(value);
5355 return 0;
5356 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005358 if (!strcmp(name, "find_global")) {
5359 Py_XDECREF(self->find_class);
5360 self->find_class = value;
5361 Py_XINCREF(value);
5362 return 0;
5363 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005364
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005365 if (! value) {
5366 PyErr_SetString(PyExc_TypeError,
5367 "attribute deletion is not supported");
5368 return -1;
5369 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005370
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005371 if (strcmp(name, "memo") == 0) {
5372 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005373 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005374 "memo must be a dictionary");
5375 return -1;
5376 }
5377 Py_XDECREF(self->memo);
5378 self->memo = value;
5379 Py_INCREF(value);
5380 return 0;
5381 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005382
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005383 PyErr_SetString(PyExc_AttributeError, name);
5384 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005385}
5386
Tim Peters5bd2a792003-02-01 16:45:06 +00005387/* ---------------------------------------------------------------------------
5388 * Module-level functions.
5389 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005390
Tim Peters5bd2a792003-02-01 16:45:06 +00005391/* dump(obj, file, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005392static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005393cpm_dump(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005394{
5395 PyObject *ob, *file, *res = NULL;
5396 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005397 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005398
Tim Peters5bd2a792003-02-01 16:45:06 +00005399 if (!( PyArg_ParseTuple(args, "OO|i", &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005400 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005401
Tim Peters5bd2a792003-02-01 16:45:06 +00005402 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005403 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005404
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005405 if (dump(pickler, ob) < 0)
5406 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005407
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005408 Py_INCREF(Py_None);
5409 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005410
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005411 finally:
5412 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005413
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005414 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005415}
5416
5417
Tim Peters5bd2a792003-02-01 16:45:06 +00005418/* dumps(obj, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005419static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005420cpm_dumps(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005421{
5422 PyObject *ob, *file = 0, *res = NULL;
5423 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005424 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005425
Tim Peters5bd2a792003-02-01 16:45:06 +00005426 if (!( PyArg_ParseTuple(args, "O|i:dumps", &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005427 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005428
Tim Peterscba30e22003-02-01 06:24:36 +00005429 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005430 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005431
Tim Peters5bd2a792003-02-01 16:45:06 +00005432 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005433 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005434
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005435 if (dump(pickler, ob) < 0)
5436 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005437
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005438 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005439
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005440 finally:
5441 Py_XDECREF(pickler);
5442 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005443
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005444 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005445}
5446
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005447
Tim Peters5bd2a792003-02-01 16:45:06 +00005448/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005449static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005450cpm_load(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005451{
5452 Unpicklerobject *unpickler = 0;
5453 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005454
Tim Peterscba30e22003-02-01 06:24:36 +00005455 if (!( PyArg_ParseTuple(args, "O:load", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005456 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005457
Tim Peterscba30e22003-02-01 06:24:36 +00005458 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005459 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005460
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005461 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005462
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005463 finally:
5464 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005465
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005466 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005467}
5468
5469
Tim Peters5bd2a792003-02-01 16:45:06 +00005470/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005471static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005472cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005473{
5474 PyObject *ob, *file = 0, *res = NULL;
5475 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005476
Tim Peterscba30e22003-02-01 06:24:36 +00005477 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005478 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005479
Tim Peterscba30e22003-02-01 06:24:36 +00005480 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005481 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005482
Tim Peterscba30e22003-02-01 06:24:36 +00005483 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005484 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005485
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005486 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005487
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005488 finally:
5489 Py_XDECREF(file);
5490 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005491
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005492 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005493}
5494
5495
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005496PyDoc_STRVAR(Unpicklertype__doc__,
5497"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005498
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005499static PyTypeObject Unpicklertype = {
5500 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00005501 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00005502 "cPickle.Unpickler", /*tp_name*/
Guido van Rossum60456fd1997-04-09 17:36:32 +00005503 sizeof(Unpicklerobject), /*tp_basicsize*/
5504 0, /*tp_itemsize*/
5505 /* methods */
5506 (destructor)Unpickler_dealloc, /*tp_dealloc*/
5507 (printfunc)0, /*tp_print*/
5508 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
5509 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
5510 (cmpfunc)0, /*tp_compare*/
5511 (reprfunc)0, /*tp_repr*/
5512 0, /*tp_as_number*/
5513 0, /*tp_as_sequence*/
5514 0, /*tp_as_mapping*/
5515 (hashfunc)0, /*tp_hash*/
5516 (ternaryfunc)0, /*tp_call*/
5517 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005518
Guido van Rossum60456fd1997-04-09 17:36:32 +00005519 /* Space for future expansion */
5520 0L,0L,0L,0L,
5521 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005522};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005523
Guido van Rossum60456fd1997-04-09 17:36:32 +00005524static struct PyMethodDef cPickle_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005525 {"dump", (PyCFunction)cpm_dump, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005526 PyDoc_STR("dump(object, file, proto=0) -- "
5527 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005528 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005529 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005530 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005531
Neal Norwitzb0493252002-03-31 14:44:22 +00005532 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005533 PyDoc_STR("dumps(object, proto=0) -- "
5534 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005535 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005536 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005537 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005538
Neal Norwitzb0493252002-03-31 14:44:22 +00005539 {"load", (PyCFunction)cpm_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005540 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005541
Neal Norwitzb0493252002-03-31 14:44:22 +00005542 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005543 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005544
Neal Norwitzb0493252002-03-31 14:44:22 +00005545 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005546 PyDoc_STR("Pickler(file, proto=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005547 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005548 "This takes a file-like object for writing a pickle data stream.\n"
5549 "The optional proto argument tells the pickler to use the given\n"
5550 "protocol; supported protocols are 0, 1, 2. The default\n"
5551 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5552 "only protocol that can be written to a file opened in text\n"
5553 "mode and read back successfully. When using a protocol higher\n"
5554 "than 0, make sure the file is opened in binary mode, both when\n"
5555 "pickling and unpickling.)\n"
5556 "\n"
5557 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5558 "more efficient than protocol 1.\n"
5559 "\n"
5560 "Specifying a negative protocol version selects the highest\n"
5561 "protocol version supported. The higher the protocol used, the\n"
5562 "more recent the version of Python needed to read the pickle\n"
5563 "produced.\n"
5564 "\n"
5565 "The file parameter must have a write() method that accepts a single\n"
5566 "string argument. It can thus be an open file object, a StringIO\n"
5567 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005568 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005569
Neal Norwitzb0493252002-03-31 14:44:22 +00005570 {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005571 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5572
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005573 { NULL, NULL }
5574};
5575
Guido van Rossum60456fd1997-04-09 17:36:32 +00005576static int
Tim Peterscba30e22003-02-01 06:24:36 +00005577init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005578{
5579 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005580
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005581#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005582
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005583 INIT_STR(__class__);
5584 INIT_STR(__getinitargs__);
5585 INIT_STR(__dict__);
5586 INIT_STR(__getstate__);
5587 INIT_STR(__setstate__);
5588 INIT_STR(__name__);
5589 INIT_STR(__main__);
5590 INIT_STR(__reduce__);
5591 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005592 INIT_STR(append);
5593 INIT_STR(read);
5594 INIT_STR(readline);
5595 INIT_STR(copy_reg);
5596 INIT_STR(dispatch_table);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005597 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005598
Tim Peterscba30e22003-02-01 06:24:36 +00005599 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005600 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005601
Tim Peters1f1b2d22003-02-01 02:16:37 +00005602 /* This is special because we want to use a different
5603 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005604 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005605 if (!dispatch_table) return -1;
5606
5607 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005608 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005609 if (!extension_registry) return -1;
5610
5611 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005612 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005613 if (!inverted_registry) return -1;
5614
5615 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005616 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005617 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005618
Tim Peters71fcda52003-02-14 23:05:28 +00005619 better_reduce = PyObject_GetAttrString(copy_reg, "_better_reduce");
5620 if (!better_reduce) return -1;
5621
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005622 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005623
Tim Peters71fcda52003-02-14 23:05:28 +00005624 object_reduce = PyObject_GetAttrString((PyObject *)&PyBaseObject_Type,
5625 "__reduce__");
5626 if (object_reduce == NULL) return -1;
5627
Tim Peters731098b2003-02-04 20:56:09 +00005628 if (!(empty_tuple = PyTuple_New(0)))
5629 return -1;
5630
5631 two_tuple = PyTuple_New(2);
5632 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005633 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005634 /* We use this temp container with no regard to refcounts, or to
5635 * keeping containees alive. Exempt from GC, because we don't
5636 * want anything looking at two_tuple() by magic.
5637 */
5638 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005640 /* Ugh */
5641 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5642 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5643 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005644
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005645 if (!( t=PyDict_New())) return -1;
5646 if (!( r=PyRun_String(
5647 "def __init__(self, *args): self.args=args\n\n"
5648 "def __str__(self):\n"
5649 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5650 Py_file_input,
5651 module_dict, t) )) return -1;
5652 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005653
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005654 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005655 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005656 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005658 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005659
Tim Peterscba30e22003-02-01 06:24:36 +00005660 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005661 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005662 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005663 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005664
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005665 if (!( t=PyDict_New())) return -1;
5666 if (!( r=PyRun_String(
5667 "def __init__(self, *args): self.args=args\n\n"
5668 "def __str__(self):\n"
5669 " a=self.args\n"
5670 " a=a and type(a[0]) or '(what)'\n"
5671 " return 'Cannot pickle %s objects' % a\n"
5672 , Py_file_input,
5673 module_dict, t) )) return -1;
5674 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005675
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005676 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005677 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005678 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005679
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005680 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005681
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005682 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005683 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005684 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005685
Martin v. Löwis658009a2002-09-16 17:26:24 +00005686 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5687 UnpicklingError, NULL)))
5688 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005689
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005690 if (PyDict_SetItemString(module_dict, "PickleError",
5691 PickleError) < 0)
5692 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005693
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005694 if (PyDict_SetItemString(module_dict, "PicklingError",
5695 PicklingError) < 0)
5696 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005697
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005698 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5699 UnpicklingError) < 0)
5700 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005701
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005702 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5703 UnpickleableError) < 0)
5704 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005706 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5707 BadPickleGet) < 0)
5708 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005709
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005710 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005711
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005712 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005713}
5714
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005715#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5716#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005717#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005718PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005719initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005720{
5721 PyObject *m, *d, *di, *v, *k;
5722 int i;
Tim Peters5b7da392003-02-04 00:21:07 +00005723 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005724 PyObject *format_version;
5725 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005726
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005727 Picklertype.ob_type = &PyType_Type;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005728 Unpicklertype.ob_type = &PyType_Type;
5729 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005730
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005731 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005732 * so we're forced to use a temporary dictionary. :(
5733 */
5734 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005735 if (!di) return;
5736 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005737
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005738 /* Create the module and add the functions */
5739 m = Py_InitModule4("cPickle", cPickle_methods,
5740 cPickle_module_documentation,
5741 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005742
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005743 /* Add some symbolic constants to the module */
5744 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005745 v = PyString_FromString(rev);
5746 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005747 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005748
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005749 /* Copy data from di. Waaa. */
5750 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5751 if (PyObject_SetItem(d, k, v) < 0) {
5752 Py_DECREF(di);
5753 return;
5754 }
5755 }
5756 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005757
Tim Peters8587b3c2003-02-13 15:44:41 +00005758 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5759 if (i < 0)
5760 return;
5761
Tim Peters5b7da392003-02-04 00:21:07 +00005762 /* These are purely informational; no code uses them. */
5763 /* File format version we write. */
5764 format_version = PyString_FromString("2.0");
5765 /* Format versions we can read. */
5766 compatible_formats = Py_BuildValue("[sssss]",
5767 "1.0", /* Original protocol 0 */
5768 "1.1", /* Protocol 0 + INST */
5769 "1.2", /* Original protocol 1 */
5770 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005771 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005772 PyDict_SetItemString(d, "format_version", format_version);
5773 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5774 Py_XDECREF(format_version);
5775 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005776}