blob: 0d78e49f8f3b47b377eab932d3051f14af85767b [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. */
Neal Norwitz0ae4c4a82003-02-15 15:07:17 +0000123static PyObject *object_reduce;
Tim Peters71fcda52003-02-14 23:05:28 +0000124
125/* copy_reg._better_reduce, the protocol 2 reduction function. */
Neal Norwitz0ae4c4a82003-02-15 15:07:17 +0000126static PyObject *better_reduce;
Tim Peters71fcda52003-02-14 23:05:28 +0000127
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,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002024 "Can't pickle %s: import of module %s "
2025 "failed",
2026 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002027 goto finally;
2028 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002029 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002030 if (klass == NULL) {
2031 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00002032 "Can't pickle %s: attribute lookup %s.%s "
2033 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002034 "OSS", args, module, global_name);
2035 goto finally;
2036 }
2037 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002038 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002039 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00002040 "Can't pickle %s: it's not the same object "
2041 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002042 "OSS", args, module, global_name);
2043 goto finally;
2044 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002045 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002046
Tim Peters731098b2003-02-04 20:56:09 +00002047 if (self->proto >= 2) {
2048 /* See whether this is in the extension registry, and if
2049 * so generate an EXT opcode.
2050 */
2051 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002052 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002053 char c_str[5];
2054 int n;
2055
2056 PyTuple_SET_ITEM(two_tuple, 0, module);
2057 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2058 py_code = PyDict_GetItem(extension_registry, two_tuple);
2059 if (py_code == NULL)
2060 goto gen_global; /* not registered */
2061
2062 /* Verify py_code has the right type and value. */
2063 if (!PyInt_Check(py_code)) {
2064 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002065 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002066 "OO", args, py_code);
2067 goto finally;
2068 }
2069 code = PyInt_AS_LONG(py_code);
2070 if (code <= 0 || code > 0x7fffffffL) {
2071 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2072 "extension code %ld is out of range",
2073 "Ol", args, code);
2074 goto finally;
2075 }
2076
2077 /* Generate an EXT opcode. */
2078 if (code <= 0xff) {
2079 c_str[0] = EXT1;
2080 c_str[1] = (char)code;
2081 n = 2;
2082 }
2083 else if (code <= 0xffff) {
2084 c_str[0] = EXT2;
2085 c_str[1] = (char)(code & 0xff);
2086 c_str[2] = (char)((code >> 8) & 0xff);
2087 n = 3;
2088 }
2089 else {
2090 c_str[0] = EXT4;
2091 c_str[1] = (char)(code & 0xff);
2092 c_str[2] = (char)((code >> 8) & 0xff);
2093 c_str[3] = (char)((code >> 16) & 0xff);
2094 c_str[4] = (char)((code >> 24) & 0xff);
2095 n = 5;
2096 }
2097
2098 if (self->write_func(self, c_str, n) >= 0)
2099 res = 0;
2100 goto finally; /* and don't memoize */
2101 }
2102
2103 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002104 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002105 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002106
Tim Peters0bc93f52003-02-02 18:29:33 +00002107 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002108 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002109
Tim Peters0bc93f52003-02-02 18:29:33 +00002110 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002111 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002112
Tim Peters0bc93f52003-02-02 18:29:33 +00002113 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002114 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002115
Tim Peters0bc93f52003-02-02 18:29:33 +00002116 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002117 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002118
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002119 if (put(self, args) < 0)
2120 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002121
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002122 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002123
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002124 finally:
2125 Py_XDECREF(module);
2126 Py_XDECREF(global_name);
2127 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002128
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002129 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002130}
2131
Guido van Rossum60456fd1997-04-09 17:36:32 +00002132static int
Tim Peterscba30e22003-02-01 06:24:36 +00002133save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002134{
2135 PyObject *pid = 0;
2136 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002137
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002138 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002139
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002140 Py_INCREF(args);
2141 ARG_TUP(self, args);
2142 if (self->arg) {
2143 pid = PyObject_Call(f, self->arg, NULL);
2144 FREE_ARG_TUP(self);
2145 }
2146 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002147
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002148 if (pid != Py_None) {
2149 if (!self->bin) {
2150 if (!PyString_Check(pid)) {
2151 PyErr_SetString(PicklingError,
2152 "persistent id must be string");
2153 goto finally;
2154 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002155
Tim Peters0bc93f52003-02-02 18:29:33 +00002156 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002157 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002158
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002159 if ((size = PyString_Size(pid)) < 0)
2160 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002161
Tim Peters0bc93f52003-02-02 18:29:33 +00002162 if (self->write_func(self,
2163 PyString_AS_STRING(
2164 (PyStringObject *)pid),
2165 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002166 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002167
Tim Peters0bc93f52003-02-02 18:29:33 +00002168 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002169 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002170
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002171 res = 1;
2172 goto finally;
2173 }
2174 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002175 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002176 res = -1;
2177 else
2178 res = 1;
2179 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002180
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002181 goto finally;
2182 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002184 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002186 finally:
2187 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002188
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002189 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002190}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002191
Tim Peters71fcda52003-02-14 23:05:28 +00002192/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2193 * appropriate __reduce__ method for ob.
2194 */
Tim Peters84e87f32001-03-17 04:50:51 +00002195static int
Tim Peters71fcda52003-02-14 23:05:28 +00002196save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002197{
Tim Peters71fcda52003-02-14 23:05:28 +00002198 PyObject *callable;
2199 PyObject *argtup;
2200 PyObject *state = NULL;
2201 PyObject *listitems = NULL;
2202 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002203
Tim Peters71fcda52003-02-14 23:05:28 +00002204 int use_newobj = self->proto >= 2;
2205
2206 static char reduce = REDUCE;
2207 static char build = BUILD;
2208 static char newobj = NEWOBJ;
2209
2210 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2211 &callable,
2212 &argtup,
2213 &state,
2214 &listitems,
2215 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002216 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002217
Tim Peters71fcda52003-02-14 23:05:28 +00002218 if (state == Py_None)
2219 state = NULL;
2220 if (listitems == Py_None)
2221 listitems = NULL;
2222 if (dictitems == Py_None)
2223 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002224
Tim Peters71fcda52003-02-14 23:05:28 +00002225 /* Protocol 2 special case: if callable's name is __newobj__, use
2226 * NEWOBJ. This consumes a lot of code.
2227 */
2228 if (use_newobj) {
2229 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002230
Tim Peters71fcda52003-02-14 23:05:28 +00002231 if (temp == NULL) {
2232 PyErr_Clear();
2233 use_newobj = 0;
2234 }
2235 else {
2236 use_newobj = PyString_Check(temp) &&
2237 strcmp(PyString_AS_STRING(temp),
2238 "__newobj__") == 0;
2239 Py_DECREF(temp);
2240 }
2241 }
2242 if (use_newobj) {
2243 PyObject *cls;
2244 PyObject *newargtup;
2245 int n, i;
2246
2247 /* Sanity checks. */
2248 n = PyTuple_Size(argtup);
2249 if (n < 1) {
2250 PyErr_SetString(PicklingError, "__newobj__ arglist "
2251 "is empty");
2252 return -1;
2253 }
2254
2255 cls = PyTuple_GET_ITEM(argtup, 0);
2256 if (! PyObject_HasAttrString(cls, "__new__")) {
2257 PyErr_SetString(PicklingError, "args[0] from "
2258 "__newobj__ args has no __new__");
2259 return -1;
2260 }
2261
2262 /* XXX How could ob be NULL? */
2263 if (ob != NULL) {
2264 PyObject *ob_dot_class;
2265
2266 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2267 if (ob_dot_class == NULL)
2268 PyErr_Clear();
2269 i = ob_dot_class != cls; /* true iff a problem */
2270 Py_XDECREF(ob_dot_class);
2271 if (i) {
2272 PyErr_SetString(PicklingError, "args[0] from "
2273 "__newobj__ args has the wrong class");
2274 return -1;
2275 }
2276 }
2277
2278 /* Save the class and its __new__ arguments. */
2279 if (save(self, cls, 0) < 0)
2280 return -1;
2281
2282 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2283 if (newargtup == NULL)
2284 return -1;
2285 for (i = 1; i < n; ++i) {
2286 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2287 Py_INCREF(temp);
2288 PyTuple_SET_ITEM(newargtup, i-1, temp);
2289 }
2290 i = save(self, newargtup, 0) < 0;
2291 Py_DECREF(newargtup);
2292 if (i < 0)
2293 return -1;
2294
2295 /* Add NEWOBJ opcode. */
2296 if (self->write_func(self, &newobj, 1) < 0)
2297 return -1;
2298 }
2299 else {
2300 /* Not using NEWOBJ. */
2301 if (save(self, callable, 0) < 0 ||
2302 save(self, argtup, 0) < 0 ||
2303 self->write_func(self, &reduce, 1) < 0)
2304 return -1;
2305 }
2306
2307 /* Memoize. */
2308 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002309 if (ob != NULL) {
2310 if (state && !PyDict_Check(state)) {
2311 if (put2(self, ob) < 0)
2312 return -1;
2313 }
Tim Peters71fcda52003-02-14 23:05:28 +00002314 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002315 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002316 }
Tim Peters84e87f32001-03-17 04:50:51 +00002317
Guido van Rossum60456fd1997-04-09 17:36:32 +00002318
Tim Peters71fcda52003-02-14 23:05:28 +00002319 if (listitems && batch_list(self, listitems) < 0)
2320 return -1;
2321
2322 if (dictitems && batch_dict(self, dictitems) < 0)
2323 return -1;
2324
2325 if (state) {
2326 if (save(self, state, 0) < 0 ||
2327 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002328 return -1;
2329 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002330
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002331 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002332}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002333
Guido van Rossum60456fd1997-04-09 17:36:32 +00002334static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002335save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002336{
2337 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002338 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2339 PyObject *arg_tup;
2340 int res = -1;
2341 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002342
Martin v. Löwis5a395302002-08-04 08:20:23 +00002343 if (self->nesting++ > Py_GetRecursionLimit()){
2344 PyErr_SetString(PyExc_RuntimeError,
2345 "maximum recursion depth exceeded");
2346 goto finally;
2347 }
2348
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002349 if (!pers_save && self->pers_func) {
2350 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2351 res = tmp;
2352 goto finally;
2353 }
2354 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002355
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002356 if (args == Py_None) {
2357 res = save_none(self, args);
2358 goto finally;
2359 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002361 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002362
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002363 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002364 case 'b':
2365 if (args == Py_False || args == Py_True) {
2366 res = save_bool(self, args);
2367 goto finally;
2368 }
2369 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002370 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002371 if (type == &PyInt_Type) {
2372 res = save_int(self, args);
2373 goto finally;
2374 }
2375 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002376
Guido van Rossum60456fd1997-04-09 17:36:32 +00002377 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002378 if (type == &PyLong_Type) {
2379 res = save_long(self, args);
2380 goto finally;
2381 }
2382 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002383
Guido van Rossum60456fd1997-04-09 17:36:32 +00002384 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002385 if (type == &PyFloat_Type) {
2386 res = save_float(self, args);
2387 goto finally;
2388 }
2389 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002390
Guido van Rossum60456fd1997-04-09 17:36:32 +00002391 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002392 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2393 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002394 goto finally;
2395 }
2396 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002397
Guido van Rossum60456fd1997-04-09 17:36:32 +00002398 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002399 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2400 res = save_string(self, args, 0);
2401 goto finally;
2402 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002403
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002404#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002405 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002406 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2407 res = save_unicode(self, args, 0);
2408 goto finally;
2409 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002410#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002411 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002412
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002413 if (args->ob_refcnt > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002414 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002415 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002416
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002417 if (PyDict_GetItem(self->memo, py_ob_id)) {
2418 if (get(self, py_ob_id) < 0)
2419 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002420
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002421 res = 0;
2422 goto finally;
2423 }
2424 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002425
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002426 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002427 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002428 if (type == &PyString_Type) {
2429 res = save_string(self, args, 1);
2430 goto finally;
2431 }
2432 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002433
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002434#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002435 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002436 if (type == &PyUnicode_Type) {
2437 res = save_unicode(self, args, 1);
2438 goto finally;
2439 }
2440 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002441#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002442
Guido van Rossum60456fd1997-04-09 17:36:32 +00002443 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002444 if (type == &PyTuple_Type) {
2445 res = save_tuple(self, args);
2446 goto finally;
2447 }
2448 if (type == &PyType_Type) {
2449 res = save_global(self, args, NULL);
2450 goto finally;
2451 }
2452 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002453
Guido van Rossum60456fd1997-04-09 17:36:32 +00002454 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002455 if (type == &PyList_Type) {
2456 res = save_list(self, args);
2457 goto finally;
2458 }
2459 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002460
2461 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002462 if (type == &PyDict_Type) {
2463 res = save_dict(self, args);
2464 goto finally;
2465 }
2466 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002467
2468 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002469 if (type == &PyInstance_Type) {
2470 res = save_inst(self, args);
2471 goto finally;
2472 }
2473 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002474
2475 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002476 if (type == &PyClass_Type) {
2477 res = save_global(self, args, NULL);
2478 goto finally;
2479 }
2480 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002481
2482 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002483 if (type == &PyFunction_Type) {
2484 res = save_global(self, args, NULL);
2485 goto finally;
2486 }
2487 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002488
2489 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002490 if (type == &PyCFunction_Type) {
2491 res = save_global(self, args, NULL);
2492 goto finally;
2493 }
2494 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002495
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002496 if (!pers_save && self->inst_pers_func) {
2497 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2498 res = tmp;
2499 goto finally;
2500 }
2501 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002502
Jeremy Hylton39c61162002-07-16 19:47:43 +00002503 if (PyType_IsSubtype(type, &PyType_Type)) {
2504 res = save_global(self, args, NULL);
2505 goto finally;
2506 }
2507
Tim Peters71fcda52003-02-14 23:05:28 +00002508 /* Get a reduction callable. This may come from
2509 * copy_reg.dispatch_table, the object's __reduce__ method,
2510 * the default object.__reduce__, or copy_reg._better_reduce.
2511 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002512 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2513 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002514 Py_INCREF(__reduce__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002515 }
2516 else {
Tim Peters71fcda52003-02-14 23:05:28 +00002517 /* Check for a __reduce__ method.
2518 * Subtle: get the unbound method from the class, so that
2519 * protocol 2 can override the default __reduce__ that all
2520 * classes inherit from object.
2521 * XXX object.__reduce__ should really be rewritten so that
2522 * XXX we don't need to call back into Python code here
2523 * XXX (better_reduce), but no time to do that.
2524 */
2525 __reduce__ = PyObject_GetAttr((PyObject *)type,
2526 __reduce___str);
2527 if (__reduce__ == NULL) {
Tim Peters5aa3da62003-02-13 21:03:57 +00002528 PyErr_Clear();
Tim Peters71fcda52003-02-14 23:05:28 +00002529 PyErr_SetObject(UnpickleableError, args);
2530 goto finally;
2531 }
2532
2533 if (self->proto >= 2 && __reduce__ == object_reduce) {
2534 /* Proto 2 can do better than the default. */
2535 Py_DECREF(__reduce__);
2536 Py_INCREF(better_reduce);
2537 __reduce__ = better_reduce;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002538 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002539 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002540
Tim Peters71fcda52003-02-14 23:05:28 +00002541 /* Call the reduction callable, setting t to the result. */
2542 assert(__reduce__ != NULL);
2543 assert(t == NULL);
2544 Py_INCREF(args);
2545 ARG_TUP(self, args);
2546 if (self->arg) {
2547 t = PyObject_Call(__reduce__, self->arg, NULL);
2548 FREE_ARG_TUP(self);
2549 }
2550 if (t == NULL)
2551 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002552
Tim Peters71fcda52003-02-14 23:05:28 +00002553 if (PyString_Check(t)) {
2554 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002555 goto finally;
2556 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002557
Tim Peters71fcda52003-02-14 23:05:28 +00002558 if (! PyTuple_Check(t)) {
2559 cPickle_ErrFormat(PicklingError, "Value returned by "
2560 "%s must be string or tuple",
2561 "O", __reduce__);
2562 goto finally;
2563 }
2564
2565 size = PyTuple_Size(t);
2566 if (size < 2 || size > 5) {
2567 cPickle_ErrFormat(PicklingError, "tuple returned by "
2568 "%s must contain 2 through 5 elements",
2569 "O", __reduce__);
2570 goto finally;
2571 }
2572
2573 arg_tup = PyTuple_GET_ITEM(t, 1);
2574 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2575 cPickle_ErrFormat(PicklingError, "Second element of "
2576 "tuple returned by %s must be a tuple",
2577 "O", __reduce__);
2578 goto finally;
2579 }
2580
2581 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002582
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002583 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002584 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002585 Py_XDECREF(py_ob_id);
2586 Py_XDECREF(__reduce__);
2587 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002588
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002589 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002590}
2591
2592
2593static int
Tim Peterscba30e22003-02-01 06:24:36 +00002594dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002595{
2596 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002597
Tim Peters4190fb82003-02-02 16:09:05 +00002598 if (self->proto >= 2) {
2599 char bytes[2];
2600
2601 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002602 assert(self->proto >= 0 && self->proto < 256);
2603 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002604 if (self->write_func(self, bytes, 2) < 0)
2605 return -1;
2606 }
2607
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002608 if (save(self, args, 0) < 0)
2609 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002610
Tim Peters4190fb82003-02-02 16:09:05 +00002611 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002612 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002613
Tim Peters4190fb82003-02-02 16:09:05 +00002614 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002615 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002616
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002617 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002618}
2619
2620static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002621Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002622{
Tim Peterscba30e22003-02-01 06:24:36 +00002623 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002624 PyDict_Clear(self->memo);
2625 Py_INCREF(Py_None);
2626 return Py_None;
2627}
2628
2629static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002630Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002631{
2632 int l, i, rsize, ssize, clear=1, lm;
2633 long ik;
2634 PyObject *k, *r;
2635 char *s, *p, *have_get;
2636 Pdata *data;
2637
2638 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002639 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002640 return NULL;
2641
2642 /* Check to make sure we are based on a list */
2643 if (! Pdata_Check(self->file)) {
2644 PyErr_SetString(PicklingError,
2645 "Attempt to getvalue() a non-list-based pickler");
2646 return NULL;
2647 }
2648
2649 /* flush write buffer */
2650 if (write_other(self, NULL, 0) < 0) return NULL;
2651
2652 data=(Pdata*)self->file;
2653 l=data->length;
2654
2655 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002656 lm = PyDict_Size(self->memo);
2657 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002658 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002659 have_get = malloc(lm);
2660 if (have_get == NULL) return PyErr_NoMemory();
2661 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002662
2663 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002664 for (rsize = 0, i = l; --i >= 0; ) {
2665 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002666
Tim Petersac5687a2003-02-02 18:08:34 +00002667 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002668 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002669
2670 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002671 ik = PyInt_AS_LONG((PyIntObject*)k);
2672 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002673 PyErr_SetString(PicklingError,
2674 "Invalid get data");
2675 return NULL;
2676 }
Tim Petersac5687a2003-02-02 18:08:34 +00002677 if (have_get[ik]) /* with matching get */
2678 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002679 }
2680
2681 else if (! (PyTuple_Check(k) &&
2682 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002683 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002684 ) {
2685 PyErr_SetString(PicklingError,
2686 "Unexpected data in internal list");
2687 return NULL;
2688 }
2689
2690 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002691 ik = PyInt_AS_LONG((PyIntObject *)k);
2692 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002693 PyErr_SetString(PicklingError,
2694 "Invalid get data");
2695 return NULL;
2696 }
Tim Petersac5687a2003-02-02 18:08:34 +00002697 have_get[ik] = 1;
2698 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002699 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002700 }
2701
2702 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002703 r = PyString_FromStringAndSize(NULL, rsize);
2704 if (r == NULL) goto err;
2705 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002706
Tim Petersac5687a2003-02-02 18:08:34 +00002707 for (i = 0; i < l; i++) {
2708 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002709
2710 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002711 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002712 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002713 p=PyString_AS_STRING((PyStringObject *)k);
2714 while (--ssize >= 0)
2715 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002716 }
2717 }
2718
2719 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002720 ik = PyInt_AS_LONG((PyIntObject *)
2721 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002722 if (ik < 256) {
2723 *s++ = BINGET;
2724 *s++ = (int)(ik & 0xff);
2725 }
2726 else {
2727 *s++ = LONG_BINGET;
2728 *s++ = (int)(ik & 0xff);
2729 *s++ = (int)((ik >> 8) & 0xff);
2730 *s++ = (int)((ik >> 16) & 0xff);
2731 *s++ = (int)((ik >> 24) & 0xff);
2732 }
2733 }
2734
2735 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002736 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002737
2738 if (have_get[ik]) { /* with matching get */
2739 if (ik < 256) {
2740 *s++ = BINPUT;
2741 *s++ = (int)(ik & 0xff);
2742 }
2743 else {
2744 *s++ = LONG_BINPUT;
2745 *s++ = (int)(ik & 0xff);
2746 *s++ = (int)((ik >> 8) & 0xff);
2747 *s++ = (int)((ik >> 16) & 0xff);
2748 *s++ = (int)((ik >> 24) & 0xff);
2749 }
2750 }
2751 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002752 }
2753
2754 if (clear) {
2755 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002756 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002757 }
2758
2759 free(have_get);
2760 return r;
2761 err:
2762 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002763 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002764}
2765
2766static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002767Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002768{
2769 PyObject *ob;
2770 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002771
Tim Peterscba30e22003-02-01 06:24:36 +00002772 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002773 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002775 if (dump(self, ob) < 0)
2776 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002777
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002778 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002780 /* XXX Why does dump() return self? */
2781 Py_INCREF(self);
2782 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002783}
2784
2785
Tim Peterscba30e22003-02-01 06:24:36 +00002786static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002787{
Neal Norwitzb0493252002-03-31 14:44:22 +00002788 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002789 PyDoc_STR("dump(object) -- "
2790 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002791 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002792 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002793 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002794 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002795 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002796};
2797
2798
2799static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002800newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002801{
2802 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002803
Tim Peters5bd2a792003-02-01 16:45:06 +00002804 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002805 proto = HIGHEST_PROTOCOL;
2806 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002807 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2808 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002809 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002810 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002811 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002812
Tim Peters5bd2a792003-02-01 16:45:06 +00002813 self = PyObject_New(Picklerobject, &Picklertype);
2814 if (self == NULL)
2815 return NULL;
2816 self->proto = proto;
2817 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002818 self->fp = NULL;
2819 self->write = NULL;
2820 self->memo = NULL;
2821 self->arg = NULL;
2822 self->pers_func = NULL;
2823 self->inst_pers_func = NULL;
2824 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002825 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002826 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002827 self->fast_container = 0;
2828 self->fast_memo = NULL;
2829 self->buf_size = 0;
2830 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002831
Tim Peters5bd2a792003-02-01 16:45:06 +00002832 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002833 if (file)
2834 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002835 else {
2836 file = Pdata_New();
2837 if (file == NULL)
2838 goto err;
2839 }
2840 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002841
Tim Peterscba30e22003-02-01 06:24:36 +00002842 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002843 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002845 if (PyFile_Check(file)) {
2846 self->fp = PyFile_AsFile(file);
2847 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002848 PyErr_SetString(PyExc_ValueError,
2849 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002850 goto err;
2851 }
2852 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002853 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002854 else if (PycStringIO_OutputCheck(file)) {
2855 self->write_func = write_cStringIO;
2856 }
2857 else if (file == Py_None) {
2858 self->write_func = write_none;
2859 }
2860 else {
2861 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002862
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002863 if (! Pdata_Check(file)) {
2864 self->write = PyObject_GetAttr(file, write_str);
2865 if (!self->write) {
2866 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002867 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002868 "argument must have 'write' "
2869 "attribute");
2870 goto err;
2871 }
2872 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002873
Tim Peters5bd2a792003-02-01 16:45:06 +00002874 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2875 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002876 PyErr_NoMemory();
2877 goto err;
2878 }
2879 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002880
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002881 if (PyEval_GetRestricted()) {
2882 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002883 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002884
Tim Peters5b7da392003-02-04 00:21:07 +00002885 if (m == NULL)
2886 goto err;
2887 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002888 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002889 if (self->dispatch_table == NULL)
2890 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002891 }
2892 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002893 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002894 Py_INCREF(dispatch_table);
2895 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002896
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002897 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002898
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002899 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002900 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002901 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002902}
2903
2904
2905static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002906get_Pickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002907{
2908 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002909 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002910
Tim Peters92c8bb32003-02-13 23:00:26 +00002911 /* XXX
2912 * The documented signature is Pickler(file, proto=0), but this
2913 * accepts Pickler() and Pickler(integer) too. The meaning then
2914 * is clear as mud, undocumented, and not supported by pickle.py.
2915 * I'm told Zope uses this, but I haven't traced into this code
2916 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002917 */
2918 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002919 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002920 proto = 0;
2921 if (!PyArg_ParseTuple(args, "O|i:Pickler", &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002922 return NULL;
2923 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002924 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002925}
2926
2927
2928static void
Tim Peterscba30e22003-02-01 06:24:36 +00002929Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002930{
2931 Py_XDECREF(self->write);
2932 Py_XDECREF(self->memo);
2933 Py_XDECREF(self->fast_memo);
2934 Py_XDECREF(self->arg);
2935 Py_XDECREF(self->file);
2936 Py_XDECREF(self->pers_func);
2937 Py_XDECREF(self->inst_pers_func);
2938 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002939 PyMem_Free(self->write_buf);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002940 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002941}
2942
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002943static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002944Pickler_get_pers_func(Picklerobject *p)
2945{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002946 if (p->pers_func == NULL)
2947 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2948 else
2949 Py_INCREF(p->pers_func);
2950 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002951}
2952
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002953static int
2954Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2955{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002956 if (v == NULL) {
2957 PyErr_SetString(PyExc_TypeError,
2958 "attribute deletion is not supported");
2959 return -1;
2960 }
2961 Py_XDECREF(p->pers_func);
2962 Py_INCREF(v);
2963 p->pers_func = v;
2964 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002965}
2966
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002967static int
2968Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2969{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002970 if (v == NULL) {
2971 PyErr_SetString(PyExc_TypeError,
2972 "attribute deletion is not supported");
2973 return -1;
2974 }
2975 Py_XDECREF(p->inst_pers_func);
2976 Py_INCREF(v);
2977 p->inst_pers_func = v;
2978 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002979}
2980
2981static PyObject *
2982Pickler_get_memo(Picklerobject *p)
2983{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002984 if (p->memo == NULL)
2985 PyErr_SetString(PyExc_AttributeError, "memo");
2986 else
2987 Py_INCREF(p->memo);
2988 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002989}
2990
2991static int
2992Pickler_set_memo(Picklerobject *p, PyObject *v)
2993{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002994 if (v == NULL) {
2995 PyErr_SetString(PyExc_TypeError,
2996 "attribute deletion is not supported");
2997 return -1;
2998 }
2999 if (!PyDict_Check(v)) {
3000 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3001 return -1;
3002 }
3003 Py_XDECREF(p->memo);
3004 Py_INCREF(v);
3005 p->memo = v;
3006 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003007}
3008
3009static PyObject *
3010Pickler_get_error(Picklerobject *p)
3011{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003012 /* why is this an attribute on the Pickler? */
3013 Py_INCREF(PicklingError);
3014 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003015}
3016
3017static PyMemberDef Pickler_members[] = {
3018 {"binary", T_INT, offsetof(Picklerobject, bin)},
3019 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003020 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003021};
3022
3023static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003024 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003025 (setter)Pickler_set_pers_func},
3026 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3027 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003028 {"PicklingError", (getter)Pickler_get_error, NULL},
3029 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003030};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003031
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003032PyDoc_STRVAR(Picklertype__doc__,
3033"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003034
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003035static PyTypeObject Picklertype = {
3036 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00003037 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00003038 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003039 sizeof(Picklerobject), /*tp_basicsize*/
3040 0,
3041 (destructor)Pickler_dealloc, /* tp_dealloc */
3042 0, /* tp_print */
3043 0, /* tp_getattr */
3044 0, /* tp_setattr */
3045 0, /* tp_compare */
3046 0, /* tp_repr */
3047 0, /* tp_as_number */
3048 0, /* tp_as_sequence */
3049 0, /* tp_as_mapping */
3050 0, /* tp_hash */
3051 0, /* tp_call */
3052 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003053 PyObject_GenericGetAttr, /* tp_getattro */
3054 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003055 0, /* tp_as_buffer */
3056 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3057 Picklertype__doc__, /* tp_doc */
3058 0, /* tp_traverse */
3059 0, /* tp_clear */
3060 0, /* tp_richcompare */
3061 0, /* tp_weaklistoffset */
3062 0, /* tp_iter */
3063 0, /* tp_iternext */
3064 Pickler_methods, /* tp_methods */
3065 Pickler_members, /* tp_members */
3066 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003067};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003068
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003069static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003070find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003071{
3072 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003073
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003074 if (fc) {
3075 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003076 PyErr_SetString(UnpicklingError, "Global and instance "
3077 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003078 return NULL;
3079 }
Tim Peterscba30e22003-02-01 06:24:36 +00003080 return PyObject_CallFunction(fc, "OO", py_module_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003081 py_global_name);
3082 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003083
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003084 module = PySys_GetObject("modules");
3085 if (module == NULL)
3086 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003087
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003088 module = PyDict_GetItem(module, py_module_name);
3089 if (module == NULL) {
3090 module = PyImport_Import(py_module_name);
3091 if (!module)
3092 return NULL;
3093 global = PyObject_GetAttr(module, py_global_name);
3094 Py_DECREF(module);
3095 }
3096 else
3097 global = PyObject_GetAttr(module, py_global_name);
3098 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003099}
3100
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003101static int
Tim Peterscba30e22003-02-01 06:24:36 +00003102marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003103{
3104 if (self->num_marks < 1) {
3105 PyErr_SetString(UnpicklingError, "could not find MARK");
3106 return -1;
3107 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003108
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003109 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003110}
3111
Tim Peters84e87f32001-03-17 04:50:51 +00003112
Guido van Rossum60456fd1997-04-09 17:36:32 +00003113static int
Tim Peterscba30e22003-02-01 06:24:36 +00003114load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003115{
3116 PDATA_APPEND(self->stack, Py_None, -1);
3117 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003118}
3119
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003120static int
Tim Peterscba30e22003-02-01 06:24:36 +00003121bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003122{
3123 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3124 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003125}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003126
3127static int
Tim Peterscba30e22003-02-01 06:24:36 +00003128load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003129{
3130 PyObject *py_int = 0;
3131 char *endptr, *s;
3132 int len, res = -1;
3133 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003134
Tim Peters0bc93f52003-02-02 18:29:33 +00003135 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003136 if (len < 2) return bad_readline();
3137 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003138
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003139 errno = 0;
3140 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003141
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003142 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3143 /* Hm, maybe we've got something long. Let's try reading
3144 it as a Python long object. */
3145 errno = 0;
3146 py_int = PyLong_FromString(s, NULL, 0);
3147 if (py_int == NULL) {
3148 PyErr_SetString(PyExc_ValueError,
3149 "could not convert string to int");
3150 goto finally;
3151 }
3152 }
3153 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003154 if (len == 3 && (l == 0 || l == 1)) {
3155 if (!( py_int = PyBool_FromLong(l))) goto finally;
3156 }
3157 else {
3158 if (!( py_int = PyInt_FromLong(l))) goto finally;
3159 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003160 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003161
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003162 free(s);
3163 PDATA_PUSH(self->stack, py_int, -1);
3164 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003165
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003166 finally:
3167 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003168
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003169 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003170}
3171
Tim Peters3c67d792003-02-02 17:59:11 +00003172static int
3173load_bool(Unpicklerobject *self, PyObject *boolean)
3174{
3175 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003176 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003177 return 0;
3178}
3179
Tim Petersee1a53c2003-02-02 02:57:53 +00003180/* s contains x bytes of a little-endian integer. Return its value as a
3181 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3182 * int, but when x is 4 it's a signed one. This is an historical source
3183 * of x-platform bugs.
3184 */
Tim Peters84e87f32001-03-17 04:50:51 +00003185static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003186calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003187{
3188 unsigned char c;
3189 int i;
3190 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003191
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003192 for (i = 0, l = 0L; i < x; i++) {
3193 c = (unsigned char)s[i];
3194 l |= (long)c << (i * 8);
3195 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003196#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003197 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3198 * is signed, so on a box with longs bigger than 4 bytes we need
3199 * to extend a BININT's sign bit to the full width.
3200 */
3201 if (x == 4 && l & (1L << 31))
3202 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003203#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003204 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003205}
3206
3207
3208static int
Tim Peterscba30e22003-02-01 06:24:36 +00003209load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003210{
3211 PyObject *py_int = 0;
3212 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003213
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003214 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003215
Tim Peterscba30e22003-02-01 06:24:36 +00003216 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003217 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003218
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003219 PDATA_PUSH(self->stack, py_int, -1);
3220 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003221}
3222
3223
3224static int
Tim Peterscba30e22003-02-01 06:24:36 +00003225load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003226{
3227 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003228
Tim Peters0bc93f52003-02-02 18:29:33 +00003229 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003230 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003231
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003232 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003233}
3234
3235
3236static int
Tim Peterscba30e22003-02-01 06:24:36 +00003237load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003238{
3239 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240
Tim Peters0bc93f52003-02-02 18:29:33 +00003241 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003242 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003243
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003244 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003245}
3246
3247
3248static int
Tim Peterscba30e22003-02-01 06:24:36 +00003249load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003250{
3251 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003252
Tim Peters0bc93f52003-02-02 18:29:33 +00003253 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003254 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003255
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003256 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003257}
Tim Peters84e87f32001-03-17 04:50:51 +00003258
Guido van Rossum60456fd1997-04-09 17:36:32 +00003259static int
Tim Peterscba30e22003-02-01 06:24:36 +00003260load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003261{
3262 PyObject *l = 0;
3263 char *end, *s;
3264 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003265
Tim Peters0bc93f52003-02-02 18:29:33 +00003266 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003267 if (len < 2) return bad_readline();
3268 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003269
Tim Peterscba30e22003-02-01 06:24:36 +00003270 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003271 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003272
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003273 free(s);
3274 PDATA_PUSH(self->stack, l, -1);
3275 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003276
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003277 finally:
3278 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003279
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003280 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003281}
3282
Tim Petersee1a53c2003-02-02 02:57:53 +00003283/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3284 * data following.
3285 */
3286static int
3287load_counted_long(Unpicklerobject *self, int size)
3288{
3289 int i;
3290 char *nbytes;
3291 unsigned char *pdata;
3292 PyObject *along;
3293
3294 assert(size == 1 || size == 4);
3295 i = self->read_func(self, &nbytes, size);
3296 if (i < 0) return -1;
3297
3298 size = calc_binint(nbytes, size);
3299 if (size < 0) {
3300 /* Corrupt or hostile pickle -- we never write one like
3301 * this.
3302 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003303 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003304 "byte count");
3305 return -1;
3306 }
3307
3308 if (size == 0)
3309 along = PyLong_FromLong(0L);
3310 else {
3311 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003312 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003313 if (i < 0) return -1;
3314 along = _PyLong_FromByteArray(pdata, (size_t)size,
3315 1 /* little endian */, 1 /* signed */);
3316 }
3317 if (along == NULL)
3318 return -1;
3319 PDATA_PUSH(self->stack, along, -1);
3320 return 0;
3321}
Tim Peters84e87f32001-03-17 04:50:51 +00003322
Guido van Rossum60456fd1997-04-09 17:36:32 +00003323static int
Tim Peterscba30e22003-02-01 06:24:36 +00003324load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003325{
3326 PyObject *py_float = 0;
3327 char *endptr, *s;
3328 int len, res = -1;
3329 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003330
Tim Peters0bc93f52003-02-02 18:29:33 +00003331 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003332 if (len < 2) return bad_readline();
3333 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003334
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003335 errno = 0;
3336 d = strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003337
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003338 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3339 PyErr_SetString(PyExc_ValueError,
3340 "could not convert string to float");
3341 goto finally;
3342 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003343
Tim Peterscba30e22003-02-01 06:24:36 +00003344 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003345 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003346
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003347 free(s);
3348 PDATA_PUSH(self->stack, py_float, -1);
3349 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003350
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003351 finally:
3352 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003353
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003354 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003355}
3356
Guido van Rossum60456fd1997-04-09 17:36:32 +00003357static int
Tim Peterscba30e22003-02-01 06:24:36 +00003358load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003359{
3360 PyObject *py_float = 0;
3361 int s, e;
3362 long fhi, flo;
3363 double x;
3364 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003365
Tim Peters0bc93f52003-02-02 18:29:33 +00003366 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003367 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003368
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003369 /* First byte */
3370 s = (*p>>7) & 1;
3371 e = (*p & 0x7F) << 4;
3372 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003373
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003374 /* Second byte */
3375 e |= (*p>>4) & 0xF;
3376 fhi = (*p & 0xF) << 24;
3377 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003378
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003379 /* Third byte */
3380 fhi |= (*p & 0xFF) << 16;
3381 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003382
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003383 /* Fourth byte */
3384 fhi |= (*p & 0xFF) << 8;
3385 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003386
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003387 /* Fifth byte */
3388 fhi |= *p & 0xFF;
3389 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003390
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003391 /* Sixth byte */
3392 flo = (*p & 0xFF) << 16;
3393 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003394
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003395 /* Seventh byte */
3396 flo |= (*p & 0xFF) << 8;
3397 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003398
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003399 /* Eighth byte */
3400 flo |= *p & 0xFF;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003401
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003402 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
3403 x /= 268435456.0; /* 2**28 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003404
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003405 /* XXX This sadly ignores Inf/NaN */
3406 if (e == 0)
3407 e = -1022;
3408 else {
3409 x += 1.0;
3410 e -= 1023;
3411 }
3412 x = ldexp(x, e);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003413
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003414 if (s)
3415 x = -x;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003416
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003417 if (!( py_float = PyFloat_FromDouble(x))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003418
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003419 PDATA_PUSH(self->stack, py_float, -1);
3420 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003421}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003422
3423static int
Tim Peterscba30e22003-02-01 06:24:36 +00003424load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003425{
3426 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003427 int len, res = -1;
3428 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003429
Tim Peters0bc93f52003-02-02 18:29:33 +00003430 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003431 if (len < 2) return bad_readline();
3432 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003433
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003434
3435 /* Strip outermost quotes */
3436 while (s[len-1] <= ' ')
3437 len--;
3438 if(s[0]=='"' && s[len-1]=='"'){
3439 s[len-1] = '\0';
3440 p = s + 1 ;
3441 len -= 2;
3442 } else if(s[0]=='\'' && s[len-1]=='\''){
3443 s[len-1] = '\0';
3444 p = s + 1 ;
3445 len -= 2;
3446 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003447 goto insecure;
3448 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003449
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003450 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3451 if (str) {
3452 PDATA_PUSH(self->stack, str, -1);
3453 res = 0;
3454 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003455 free(s);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003456 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003457
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003458 insecure:
3459 free(s);
3460 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3461 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003462}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003463
3464
3465static int
Tim Peterscba30e22003-02-01 06:24:36 +00003466load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003467{
3468 PyObject *py_string = 0;
3469 long l;
3470 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003471
Tim Peters0bc93f52003-02-02 18:29:33 +00003472 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003473
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003474 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003475
Tim Peters0bc93f52003-02-02 18:29:33 +00003476 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003477 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003478
Tim Peterscba30e22003-02-01 06:24:36 +00003479 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003480 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003481
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003482 PDATA_PUSH(self->stack, py_string, -1);
3483 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003484}
3485
3486
3487static int
Tim Peterscba30e22003-02-01 06:24:36 +00003488load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003489{
3490 PyObject *py_string = 0;
3491 unsigned char l;
3492 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003493
Tim Peters0bc93f52003-02-02 18:29:33 +00003494 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003495 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003496
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003497 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003498
Tim Peters0bc93f52003-02-02 18:29:33 +00003499 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003500
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003501 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003503 PDATA_PUSH(self->stack, py_string, -1);
3504 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003505}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003506
3507
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003508#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003509static int
Tim Peterscba30e22003-02-01 06:24:36 +00003510load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003511{
3512 PyObject *str = 0;
3513 int len, res = -1;
3514 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003515
Tim Peters0bc93f52003-02-02 18:29:33 +00003516 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003517 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003518
Tim Peterscba30e22003-02-01 06:24:36 +00003519 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003520 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003521
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003522 PDATA_PUSH(self->stack, str, -1);
3523 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003524
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003525 finally:
3526 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003527}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003528#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003529
3530
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003531#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003532static int
Tim Peterscba30e22003-02-01 06:24:36 +00003533load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003534{
3535 PyObject *unicode;
3536 long l;
3537 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003538
Tim Peters0bc93f52003-02-02 18:29:33 +00003539 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003540
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003541 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003542
Tim Peters0bc93f52003-02-02 18:29:33 +00003543 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003544 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003545
Tim Peterscba30e22003-02-01 06:24:36 +00003546 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003547 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003548
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003549 PDATA_PUSH(self->stack, unicode, -1);
3550 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003551}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003552#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003553
3554
3555static int
Tim Peterscba30e22003-02-01 06:24:36 +00003556load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003557{
3558 PyObject *tup;
3559 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003560
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003561 if ((i = marker(self)) < 0) return -1;
3562 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3563 PDATA_PUSH(self->stack, tup, -1);
3564 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003565}
3566
3567static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003568load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003569{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003570 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003571
Tim Peters1d63c9f2003-02-02 20:29:39 +00003572 if (tup == NULL)
3573 return -1;
3574
3575 while (--len >= 0) {
3576 PyObject *element;
3577
3578 PDATA_POP(self->stack, element);
3579 if (element == NULL)
3580 return -1;
3581 PyTuple_SET_ITEM(tup, len, element);
3582 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003583 PDATA_PUSH(self->stack, tup, -1);
3584 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003585}
3586
3587static int
Tim Peterscba30e22003-02-01 06:24:36 +00003588load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003589{
3590 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003591
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003592 if (!( list=PyList_New(0))) return -1;
3593 PDATA_PUSH(self->stack, list, -1);
3594 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003595}
3596
3597static int
Tim Peterscba30e22003-02-01 06:24:36 +00003598load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003599{
3600 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003601
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003602 if (!( dict=PyDict_New())) return -1;
3603 PDATA_PUSH(self->stack, dict, -1);
3604 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003605}
3606
3607
3608static int
Tim Peterscba30e22003-02-01 06:24:36 +00003609load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003610{
3611 PyObject *list = 0;
3612 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003614 if ((i = marker(self)) < 0) return -1;
3615 if (!( list=Pdata_popList(self->stack, i))) return -1;
3616 PDATA_PUSH(self->stack, list, -1);
3617 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003618}
3619
3620static int
Tim Peterscba30e22003-02-01 06:24:36 +00003621load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003622{
3623 PyObject *dict, *key, *value;
3624 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003626 if ((i = marker(self)) < 0) return -1;
3627 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003628
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003629 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003630
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003631 for (k = i+1; k < j; k += 2) {
3632 key =self->stack->data[k-1];
3633 value=self->stack->data[k ];
3634 if (PyDict_SetItem(dict, key, value) < 0) {
3635 Py_DECREF(dict);
3636 return -1;
3637 }
3638 }
3639 Pdata_clear(self->stack, i);
3640 PDATA_PUSH(self->stack, dict, -1);
3641 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003642}
3643
3644static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003645Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003646{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003647 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003648
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003649 if (PyClass_Check(cls)) {
3650 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003651
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003652 if ((l=PyObject_Size(args)) < 0) goto err;
3653 if (!( l )) {
3654 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003655
Tim Peterscba30e22003-02-01 06:24:36 +00003656 __getinitargs__ = PyObject_GetAttr(cls,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003657 __getinitargs___str);
3658 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003659 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003660 so bypass usual construction */
3661 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003662
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003663 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003664 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003665 goto err;
3666 return inst;
3667 }
3668 Py_DECREF(__getinitargs__);
3669 }
Tim Peters84e87f32001-03-17 04:50:51 +00003670
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003671 if ((r=PyInstance_New(cls, args, NULL))) return r;
3672 else goto err;
3673 }
Tim Peters84e87f32001-03-17 04:50:51 +00003674
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003675 if (args==Py_None) {
3676 /* Special case, call cls.__basicnew__() */
3677 PyObject *basicnew;
Tim Peters84e87f32001-03-17 04:50:51 +00003678
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003679 basicnew = PyObject_GetAttr(cls, __basicnew___str);
3680 if (!basicnew) return NULL;
3681 r=PyObject_CallObject(basicnew, NULL);
3682 Py_DECREF(basicnew);
3683 if (r) return r;
3684 }
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003685
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003686 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003687
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003688 err:
3689 {
3690 PyObject *tp, *v, *tb;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003691
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003692 PyErr_Fetch(&tp, &v, &tb);
3693 if ((r=Py_BuildValue("OOO",v,cls,args))) {
3694 Py_XDECREF(v);
3695 v=r;
3696 }
3697 PyErr_Restore(tp,v,tb);
3698 }
3699 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003700}
Tim Peters84e87f32001-03-17 04:50:51 +00003701
Guido van Rossum60456fd1997-04-09 17:36:32 +00003702
3703static int
Tim Peterscba30e22003-02-01 06:24:36 +00003704load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003705{
3706 PyObject *class, *tup, *obj=0;
3707 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003709 if ((i = marker(self)) < 0) return -1;
3710 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3711 PDATA_POP(self->stack, class);
3712 if (class) {
3713 obj = Instance_New(class, tup);
3714 Py_DECREF(class);
3715 }
3716 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003717
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003718 if (! obj) return -1;
3719 PDATA_PUSH(self->stack, obj, -1);
3720 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003721}
3722
3723
3724static int
Tim Peterscba30e22003-02-01 06:24:36 +00003725load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003726{
3727 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3728 int i, len;
3729 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003730
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003731 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003732
Tim Peters0bc93f52003-02-02 18:29:33 +00003733 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003734 if (len < 2) return bad_readline();
3735 module_name = PyString_FromStringAndSize(s, len - 1);
3736 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003737
Tim Peters0bc93f52003-02-02 18:29:33 +00003738 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003739 if (len < 2) return bad_readline();
3740 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003741 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003742 self->find_class);
3743 Py_DECREF(class_name);
3744 }
3745 }
3746 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003747
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003748 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003749
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003750 if ((tup=Pdata_popTuple(self->stack, i))) {
3751 obj = Instance_New(class, tup);
3752 Py_DECREF(tup);
3753 }
3754 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003756 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003757
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003758 PDATA_PUSH(self->stack, obj, -1);
3759 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003760}
3761
Tim Peterseab7db32003-02-13 18:24:14 +00003762static int
3763load_newobj(Unpicklerobject *self)
3764{
3765 PyObject *args = NULL;
3766 PyObject *clsraw = NULL;
3767 PyTypeObject *cls; /* clsraw cast to its true type */
3768 PyObject *obj;
3769
3770 /* Stack is ... cls argtuple, and we want to call
3771 * cls.__new__(cls, *argtuple).
3772 */
3773 PDATA_POP(self->stack, args);
3774 if (args == NULL) goto Fail;
3775 if (! PyTuple_Check(args)) {
3776 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3777 "tuple.");
3778 goto Fail;
3779 }
3780
3781 PDATA_POP(self->stack, clsraw);
3782 cls = (PyTypeObject *)clsraw;
3783 if (cls == NULL) goto Fail;
3784 if (! PyType_Check(cls)) {
3785 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3786 "isn't a type object");
3787 goto Fail;
3788 }
3789 if (cls->tp_new == NULL) {
3790 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3791 "has NULL tp_new");
3792 goto Fail;
3793 }
3794
3795 /* Call __new__. */
3796 obj = cls->tp_new(cls, args, NULL);
3797 if (obj == NULL) goto Fail;
3798
3799 Py_DECREF(args);
3800 Py_DECREF(clsraw);
3801 PDATA_PUSH(self->stack, obj, -1);
3802 return 0;
3803
3804 Fail:
3805 Py_XDECREF(args);
3806 Py_XDECREF(clsraw);
3807 return -1;
3808}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003809
3810static int
Tim Peterscba30e22003-02-01 06:24:36 +00003811load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003812{
3813 PyObject *class = 0, *module_name = 0, *class_name = 0;
3814 int len;
3815 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003816
Tim Peters0bc93f52003-02-02 18:29:33 +00003817 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003818 if (len < 2) return bad_readline();
3819 module_name = PyString_FromStringAndSize(s, len - 1);
3820 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003821
Tim Peters0bc93f52003-02-02 18:29:33 +00003822 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003823 if (len < 2) {
3824 Py_DECREF(module_name);
3825 return bad_readline();
3826 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003827 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003828 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003829 self->find_class);
3830 Py_DECREF(class_name);
3831 }
3832 }
3833 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003834
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003835 if (! class) return -1;
3836 PDATA_PUSH(self->stack, class, -1);
3837 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003838}
3839
3840
3841static int
Tim Peterscba30e22003-02-01 06:24:36 +00003842load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003843{
3844 PyObject *pid = 0;
3845 int len;
3846 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003847
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003848 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003849 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003850 if (len < 2) return bad_readline();
3851
3852 pid = PyString_FromStringAndSize(s, len - 1);
3853 if (!pid) return -1;
3854
3855 if (PyList_Check(self->pers_func)) {
3856 if (PyList_Append(self->pers_func, pid) < 0) {
3857 Py_DECREF(pid);
3858 return -1;
3859 }
3860 }
3861 else {
3862 ARG_TUP(self, pid);
3863 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003864 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003865 NULL);
3866 FREE_ARG_TUP(self);
3867 }
3868 }
3869
3870 if (! pid) return -1;
3871
3872 PDATA_PUSH(self->stack, pid, -1);
3873 return 0;
3874 }
3875 else {
3876 PyErr_SetString(UnpicklingError,
3877 "A load persistent id instruction was encountered,\n"
3878 "but no persistent_load function was specified.");
3879 return -1;
3880 }
3881}
3882
3883static int
Tim Peterscba30e22003-02-01 06:24:36 +00003884load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003885{
3886 PyObject *pid = 0;
3887
3888 if (self->pers_func) {
3889 PDATA_POP(self->stack, pid);
3890 if (! pid) return -1;
3891
3892 if (PyList_Check(self->pers_func)) {
3893 if (PyList_Append(self->pers_func, pid) < 0) {
3894 Py_DECREF(pid);
3895 return -1;
3896 }
3897 }
3898 else {
3899 ARG_TUP(self, pid);
3900 if (self->arg) {
3901 pid = PyObject_Call(self->pers_func, self->arg,
3902 NULL);
3903 FREE_ARG_TUP(self);
3904 }
3905 if (! pid) return -1;
3906 }
3907
3908 PDATA_PUSH(self->stack, pid, -1);
3909 return 0;
3910 }
3911 else {
3912 PyErr_SetString(UnpicklingError,
3913 "A load persistent id instruction was encountered,\n"
3914 "but no persistent_load function was specified.");
3915 return -1;
3916 }
3917}
3918
3919
3920static int
Tim Peterscba30e22003-02-01 06:24:36 +00003921load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003922{
3923 int len;
3924
3925 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3926
3927 /* Note that we split the (pickle.py) stack into two stacks,
3928 an object stack and a mark stack. We have to be clever and
3929 pop the right one. We do this by looking at the top of the
3930 mark stack.
3931 */
3932
3933 if ((self->num_marks > 0) &&
3934 (self->marks[self->num_marks - 1] == len))
3935 self->num_marks--;
3936 else {
3937 len--;
3938 Py_DECREF(self->stack->data[len]);
3939 self->stack->length=len;
3940 }
3941
3942 return 0;
3943}
3944
3945
3946static int
Tim Peterscba30e22003-02-01 06:24:36 +00003947load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003948{
3949 int i;
3950
3951 if ((i = marker(self)) < 0)
3952 return -1;
3953
3954 Pdata_clear(self->stack, i);
3955
3956 return 0;
3957}
3958
3959
3960static int
Tim Peterscba30e22003-02-01 06:24:36 +00003961load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003962{
3963 PyObject *last;
3964 int len;
3965
3966 if ((len = self->stack->length) <= 0) return stackUnderflow();
3967 last=self->stack->data[len-1];
3968 Py_INCREF(last);
3969 PDATA_PUSH(self->stack, last, -1);
3970 return 0;
3971}
3972
3973
3974static int
Tim Peterscba30e22003-02-01 06:24:36 +00003975load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003976{
3977 PyObject *py_str = 0, *value = 0;
3978 int len;
3979 char *s;
3980 int rc;
3981
Tim Peters0bc93f52003-02-02 18:29:33 +00003982 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003983 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003984
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003985 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003986
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003987 value = PyDict_GetItem(self->memo, py_str);
3988 if (! value) {
3989 PyErr_SetObject(BadPickleGet, py_str);
3990 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003991 }
3992 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003993 PDATA_APPEND(self->stack, value, -1);
3994 rc = 0;
3995 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003996
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003997 Py_DECREF(py_str);
3998 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003999}
4000
4001
4002static int
Tim Peterscba30e22003-02-01 06:24:36 +00004003load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004004{
4005 PyObject *py_key = 0, *value = 0;
4006 unsigned char key;
4007 char *s;
4008 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004009
Tim Peters0bc93f52003-02-02 18:29:33 +00004010 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004011
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004012 key = (unsigned char)s[0];
4013 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00004014
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004015 value = PyDict_GetItem(self->memo, py_key);
4016 if (! value) {
4017 PyErr_SetObject(BadPickleGet, py_key);
4018 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004019 }
4020 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004021 PDATA_APPEND(self->stack, value, -1);
4022 rc = 0;
4023 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004024
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004025 Py_DECREF(py_key);
4026 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004027}
4028
4029
4030static int
Tim Peterscba30e22003-02-01 06:24:36 +00004031load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004032{
4033 PyObject *py_key = 0, *value = 0;
4034 unsigned char c;
4035 char *s;
4036 long key;
4037 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004038
Tim Peters0bc93f52003-02-02 18:29:33 +00004039 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004040
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004041 c = (unsigned char)s[0];
4042 key = (long)c;
4043 c = (unsigned char)s[1];
4044 key |= (long)c << 8;
4045 c = (unsigned char)s[2];
4046 key |= (long)c << 16;
4047 c = (unsigned char)s[3];
4048 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004049
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004050 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4051
4052 value = PyDict_GetItem(self->memo, py_key);
4053 if (! value) {
4054 PyErr_SetObject(BadPickleGet, py_key);
4055 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00004056 }
4057 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004058 PDATA_APPEND(self->stack, value, -1);
4059 rc = 0;
4060 }
4061
4062 Py_DECREF(py_key);
4063 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004064}
4065
Tim Peters2d629652003-02-04 05:06:17 +00004066/* Push an object from the extension registry (EXT[124]). nbytes is
4067 * the number of bytes following the opcode, holding the index (code) value.
4068 */
4069static int
4070load_extension(Unpicklerobject *self, int nbytes)
4071{
4072 char *codebytes; /* the nbytes bytes after the opcode */
4073 long code; /* calc_binint returns long */
4074 PyObject *py_code; /* code as a Python int */
4075 PyObject *obj; /* the object to push */
4076 PyObject *pair; /* (module_name, class_name) */
4077 PyObject *module_name, *class_name;
4078
4079 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4080 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4081 code = calc_binint(codebytes, nbytes);
4082 if (code <= 0) { /* note that 0 is forbidden */
4083 /* Corrupt or hostile pickle. */
4084 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4085 return -1;
4086 }
4087
4088 /* Look for the code in the cache. */
4089 py_code = PyInt_FromLong(code);
4090 if (py_code == NULL) return -1;
4091 obj = PyDict_GetItem(extension_cache, py_code);
4092 if (obj != NULL) {
4093 /* Bingo. */
4094 Py_DECREF(py_code);
4095 PDATA_APPEND(self->stack, obj, -1);
4096 return 0;
4097 }
4098
4099 /* Look up the (module_name, class_name) pair. */
4100 pair = PyDict_GetItem(inverted_registry, py_code);
4101 if (pair == NULL) {
4102 Py_DECREF(py_code);
4103 PyErr_Format(PyExc_ValueError, "unregistered extension "
4104 "code %ld", code);
4105 return -1;
4106 }
4107 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004108 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004109 */
4110 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4111 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4112 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4113 Py_DECREF(py_code);
4114 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4115 "isn't a 2-tuple of strings", code);
4116 return -1;
4117 }
4118 /* Load the object. */
4119 obj = find_class(module_name, class_name, self->find_class);
4120 if (obj == NULL) {
4121 Py_DECREF(py_code);
4122 return -1;
4123 }
4124 /* Cache code -> obj. */
4125 code = PyDict_SetItem(extension_cache, py_code, obj);
4126 Py_DECREF(py_code);
4127 if (code < 0) {
4128 Py_DECREF(obj);
4129 return -1;
4130 }
4131 PDATA_PUSH(self->stack, obj, -1);
4132 return 0;
4133}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004134
4135static int
Tim Peterscba30e22003-02-01 06:24:36 +00004136load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004137{
4138 PyObject *py_str = 0, *value = 0;
4139 int len, l;
4140 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004141
Tim Peters0bc93f52003-02-02 18:29:33 +00004142 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004143 if (l < 2) return bad_readline();
4144 if (!( len=self->stack->length )) return stackUnderflow();
4145 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4146 value=self->stack->data[len-1];
4147 l=PyDict_SetItem(self->memo, py_str, value);
4148 Py_DECREF(py_str);
4149 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004150}
4151
4152
4153static int
Tim Peterscba30e22003-02-01 06:24:36 +00004154load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004155{
4156 PyObject *py_key = 0, *value = 0;
4157 unsigned char key;
4158 char *s;
4159 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004160
Tim Peters0bc93f52003-02-02 18:29:33 +00004161 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004162 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004163
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004164 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004165
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004166 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4167 value=self->stack->data[len-1];
4168 len=PyDict_SetItem(self->memo, py_key, value);
4169 Py_DECREF(py_key);
4170 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004171}
4172
4173
4174static int
Tim Peterscba30e22003-02-01 06:24:36 +00004175load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004176{
4177 PyObject *py_key = 0, *value = 0;
4178 long key;
4179 unsigned char c;
4180 char *s;
4181 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004182
Tim Peters0bc93f52003-02-02 18:29:33 +00004183 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004184 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004186 c = (unsigned char)s[0];
4187 key = (long)c;
4188 c = (unsigned char)s[1];
4189 key |= (long)c << 8;
4190 c = (unsigned char)s[2];
4191 key |= (long)c << 16;
4192 c = (unsigned char)s[3];
4193 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004194
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004195 if (!( py_key = PyInt_FromLong(key))) return -1;
4196 value=self->stack->data[len-1];
4197 len=PyDict_SetItem(self->memo, py_key, value);
4198 Py_DECREF(py_key);
4199 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004200}
4201
4202
4203static int
Tim Peterscba30e22003-02-01 06:24:36 +00004204do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004205{
4206 PyObject *value = 0, *list = 0, *append_method = 0;
4207 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004208
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004209 len=self->stack->length;
4210 if (!( len >= x && x > 0 )) return stackUnderflow();
4211 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004212 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004213
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004214 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004215
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004216 if (PyList_Check(list)) {
4217 PyObject *slice;
4218 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004219
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004220 slice=Pdata_popList(self->stack, x);
4221 list_len = PyList_GET_SIZE(list);
4222 i=PyList_SetSlice(list, list_len, list_len, slice);
4223 Py_DECREF(slice);
4224 return i;
4225 }
4226 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004227
Tim Peterscba30e22003-02-01 06:24:36 +00004228 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004229 return -1;
4230
4231 for (i = x; i < len; i++) {
4232 PyObject *junk;
4233
4234 value=self->stack->data[i];
4235 junk=0;
4236 ARG_TUP(self, value);
4237 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004238 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004239 NULL);
4240 FREE_ARG_TUP(self);
4241 }
4242 if (! junk) {
4243 Pdata_clear(self->stack, i+1);
4244 self->stack->length=x;
4245 Py_DECREF(append_method);
4246 return -1;
4247 }
4248 Py_DECREF(junk);
4249 }
4250 self->stack->length=x;
4251 Py_DECREF(append_method);
4252 }
4253
4254 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004255}
4256
4257
4258static int
Tim Peterscba30e22003-02-01 06:24:36 +00004259load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004260{
4261 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004262}
4263
4264
4265static int
Tim Peterscba30e22003-02-01 06:24:36 +00004266load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004267{
4268 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004269}
4270
4271
4272static int
Tim Peterscba30e22003-02-01 06:24:36 +00004273do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004274{
4275 PyObject *value = 0, *key = 0, *dict = 0;
4276 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004277
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004278 if (!( (len=self->stack->length) >= x
4279 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004280
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004281 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004282
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004283 for (i = x+1; i < len; i += 2) {
4284 key =self->stack->data[i-1];
4285 value=self->stack->data[i ];
4286 if (PyObject_SetItem(dict, key, value) < 0) {
4287 r=-1;
4288 break;
4289 }
4290 }
4291
4292 Pdata_clear(self->stack, x);
4293
4294 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004295}
4296
4297
Tim Peters84e87f32001-03-17 04:50:51 +00004298static int
Tim Peterscba30e22003-02-01 06:24:36 +00004299load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004300{
4301 return do_setitems(self, self->stack->length - 2);
4302}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004303
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004304static int
Tim Peterscba30e22003-02-01 06:24:36 +00004305load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004306{
4307 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004308}
4309
Tim Peters84e87f32001-03-17 04:50:51 +00004310
Guido van Rossum60456fd1997-04-09 17:36:32 +00004311static int
Tim Peterscba30e22003-02-01 06:24:36 +00004312load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004313{
Tim Peters080c88b2003-02-15 03:01:11 +00004314 PyObject *state, *inst, *slotstate;
4315 PyObject *__setstate__;
4316 PyObject *d_key, *d_value;
4317 int i;
4318 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004319
Tim Peters080c88b2003-02-15 03:01:11 +00004320 /* Stack is ... instance, state. We want to leave instance at
4321 * the stack top, possibly mutated via instance.__setstate__(state).
4322 */
4323 if (self->stack->length < 2)
4324 return stackUnderflow();
4325 PDATA_POP(self->stack, state);
4326 if (state == NULL)
4327 return -1;
4328 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004329
Tim Peters080c88b2003-02-15 03:01:11 +00004330 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4331 if (__setstate__ != NULL) {
4332 PyObject *junk = NULL;
4333
4334 /* The explicit __setstate__ is responsible for everything. */
4335 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004336 if (self->arg) {
4337 junk = PyObject_Call(__setstate__, self->arg, NULL);
4338 FREE_ARG_TUP(self);
4339 }
4340 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004341 if (junk == NULL)
4342 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004343 Py_DECREF(junk);
4344 return 0;
4345 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004346 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004347
4348 /* A default __setstate__. First see whether state embeds a
4349 * slot state dict too (a proto 2 addition).
4350 */
4351 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4352 PyObject *temp = state;
4353 state = PyTuple_GET_ITEM(temp, 0);
4354 slotstate = PyTuple_GET_ITEM(temp, 1);
4355 Py_INCREF(state);
4356 Py_INCREF(slotstate);
4357 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004358 }
Tim Peters080c88b2003-02-15 03:01:11 +00004359 else
4360 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004361
Tim Peters080c88b2003-02-15 03:01:11 +00004362 /* Set inst.__dict__ from the state dict (if any). */
4363 if (state != Py_None) {
4364 PyObject *dict;
4365 if (! PyDict_Check(state)) {
4366 PyErr_SetString(UnpicklingError, "state is not a "
4367 "dictionary");
4368 goto finally;
4369 }
4370 dict = PyObject_GetAttr(inst, __dict___str);
4371 if (dict == NULL)
4372 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004373
Tim Peters080c88b2003-02-15 03:01:11 +00004374 i = 0;
4375 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4376 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4377 goto finally;
4378 }
4379 Py_DECREF(dict);
4380 }
4381
4382 /* Also set instance attributes from the slotstate dict (if any). */
4383 if (slotstate != NULL) {
4384 if (! PyDict_Check(slotstate)) {
4385 PyErr_SetString(UnpicklingError, "slot state is not "
4386 "a dictionary");
4387 goto finally;
4388 }
4389 i = 0;
4390 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4391 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4392 goto finally;
4393 }
4394 }
4395 res = 0;
4396
4397 finally:
4398 Py_DECREF(state);
4399 Py_XDECREF(slotstate);
4400 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004401}
4402
4403
4404static int
Tim Peterscba30e22003-02-01 06:24:36 +00004405load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004406{
4407 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004408
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004409 /* Note that we split the (pickle.py) stack into two stacks, an
4410 object stack and a mark stack. Here we push a mark onto the
4411 mark stack.
4412 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004413
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004414 if ((self->num_marks + 1) >= self->marks_size) {
4415 s=self->marks_size+20;
4416 if (s <= self->num_marks) s=self->num_marks + 1;
4417 if (self->marks == NULL)
4418 self->marks=(int *)malloc(s * sizeof(int));
4419 else
Tim Peterscba30e22003-02-01 06:24:36 +00004420 self->marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004421 s * sizeof(int));
4422 if (! self->marks) {
4423 PyErr_NoMemory();
4424 return -1;
4425 }
4426 self->marks_size = s;
4427 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004428
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004429 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004430
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004431 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004432}
4433
Guido van Rossum60456fd1997-04-09 17:36:32 +00004434static int
Tim Peterscba30e22003-02-01 06:24:36 +00004435load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004436{
4437 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004438
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004439 PDATA_POP(self->stack, arg_tup);
4440 if (! arg_tup) return -1;
4441 PDATA_POP(self->stack, callable);
4442 if (callable) {
4443 ob = Instance_New(callable, arg_tup);
4444 Py_DECREF(callable);
4445 }
4446 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004447
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004448 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004449
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004450 PDATA_PUSH(self->stack, ob, -1);
4451 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004452}
Tim Peters84e87f32001-03-17 04:50:51 +00004453
Tim Peters4190fb82003-02-02 16:09:05 +00004454/* Just raises an error if we don't know the protocol specified. PROTO
4455 * is the first opcode for protocols >= 2.
4456 */
4457static int
4458load_proto(Unpicklerobject *self)
4459{
4460 int i;
4461 char *protobyte;
4462
4463 i = self->read_func(self, &protobyte, 1);
4464 if (i < 0)
4465 return -1;
4466
4467 i = calc_binint(protobyte, 1);
4468 /* No point checking for < 0, since calc_binint returns an unsigned
4469 * int when chewing on 1 byte.
4470 */
4471 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004472 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004473 return 0;
4474
4475 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4476 return -1;
4477}
4478
Guido van Rossum60456fd1997-04-09 17:36:32 +00004479static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004480load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004481{
4482 PyObject *err = 0, *val = 0;
4483 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004484
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004485 self->num_marks = 0;
4486 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004487
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004488 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004489 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004490 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004491
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004492 switch (s[0]) {
4493 case NONE:
4494 if (load_none(self) < 0)
4495 break;
4496 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004497
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004498 case BININT:
4499 if (load_binint(self) < 0)
4500 break;
4501 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004503 case BININT1:
4504 if (load_binint1(self) < 0)
4505 break;
4506 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004507
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004508 case BININT2:
4509 if (load_binint2(self) < 0)
4510 break;
4511 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004513 case INT:
4514 if (load_int(self) < 0)
4515 break;
4516 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004517
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004518 case LONG:
4519 if (load_long(self) < 0)
4520 break;
4521 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004522
Tim Petersee1a53c2003-02-02 02:57:53 +00004523 case LONG1:
4524 if (load_counted_long(self, 1) < 0)
4525 break;
4526 continue;
4527
4528 case LONG4:
4529 if (load_counted_long(self, 4) < 0)
4530 break;
4531 continue;
4532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004533 case FLOAT:
4534 if (load_float(self) < 0)
4535 break;
4536 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004537
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004538 case BINFLOAT:
4539 if (load_binfloat(self) < 0)
4540 break;
4541 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004542
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004543 case BINSTRING:
4544 if (load_binstring(self) < 0)
4545 break;
4546 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004547
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004548 case SHORT_BINSTRING:
4549 if (load_short_binstring(self) < 0)
4550 break;
4551 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004552
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004553 case STRING:
4554 if (load_string(self) < 0)
4555 break;
4556 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004557
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004558#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004559 case UNICODE:
4560 if (load_unicode(self) < 0)
4561 break;
4562 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004563
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004564 case BINUNICODE:
4565 if (load_binunicode(self) < 0)
4566 break;
4567 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004568#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004569
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004570 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004571 if (load_counted_tuple(self, 0) < 0)
4572 break;
4573 continue;
4574
4575 case TUPLE1:
4576 if (load_counted_tuple(self, 1) < 0)
4577 break;
4578 continue;
4579
4580 case TUPLE2:
4581 if (load_counted_tuple(self, 2) < 0)
4582 break;
4583 continue;
4584
4585 case TUPLE3:
4586 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004587 break;
4588 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004589
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004590 case TUPLE:
4591 if (load_tuple(self) < 0)
4592 break;
4593 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004594
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004595 case EMPTY_LIST:
4596 if (load_empty_list(self) < 0)
4597 break;
4598 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004599
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004600 case LIST:
4601 if (load_list(self) < 0)
4602 break;
4603 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004604
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004605 case EMPTY_DICT:
4606 if (load_empty_dict(self) < 0)
4607 break;
4608 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004609
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004610 case DICT:
4611 if (load_dict(self) < 0)
4612 break;
4613 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004614
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004615 case OBJ:
4616 if (load_obj(self) < 0)
4617 break;
4618 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004619
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004620 case INST:
4621 if (load_inst(self) < 0)
4622 break;
4623 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004624
Tim Peterseab7db32003-02-13 18:24:14 +00004625 case NEWOBJ:
4626 if (load_newobj(self) < 0)
4627 break;
4628 continue;
4629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004630 case GLOBAL:
4631 if (load_global(self) < 0)
4632 break;
4633 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004634
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004635 case APPEND:
4636 if (load_append(self) < 0)
4637 break;
4638 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004640 case APPENDS:
4641 if (load_appends(self) < 0)
4642 break;
4643 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004644
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004645 case BUILD:
4646 if (load_build(self) < 0)
4647 break;
4648 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004649
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004650 case DUP:
4651 if (load_dup(self) < 0)
4652 break;
4653 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004655 case BINGET:
4656 if (load_binget(self) < 0)
4657 break;
4658 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004660 case LONG_BINGET:
4661 if (load_long_binget(self) < 0)
4662 break;
4663 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004664
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004665 case GET:
4666 if (load_get(self) < 0)
4667 break;
4668 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004669
Tim Peters2d629652003-02-04 05:06:17 +00004670 case EXT1:
4671 if (load_extension(self, 1) < 0)
4672 break;
4673 continue;
4674
4675 case EXT2:
4676 if (load_extension(self, 2) < 0)
4677 break;
4678 continue;
4679
4680 case EXT4:
4681 if (load_extension(self, 4) < 0)
4682 break;
4683 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004684 case MARK:
4685 if (load_mark(self) < 0)
4686 break;
4687 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004688
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004689 case BINPUT:
4690 if (load_binput(self) < 0)
4691 break;
4692 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004693
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004694 case LONG_BINPUT:
4695 if (load_long_binput(self) < 0)
4696 break;
4697 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004698
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004699 case PUT:
4700 if (load_put(self) < 0)
4701 break;
4702 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004703
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004704 case POP:
4705 if (load_pop(self) < 0)
4706 break;
4707 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004708
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004709 case POP_MARK:
4710 if (load_pop_mark(self) < 0)
4711 break;
4712 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004713
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004714 case SETITEM:
4715 if (load_setitem(self) < 0)
4716 break;
4717 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004718
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004719 case SETITEMS:
4720 if (load_setitems(self) < 0)
4721 break;
4722 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004723
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004724 case STOP:
4725 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004726
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004727 case PERSID:
4728 if (load_persid(self) < 0)
4729 break;
4730 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004731
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004732 case BINPERSID:
4733 if (load_binpersid(self) < 0)
4734 break;
4735 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004736
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004737 case REDUCE:
4738 if (load_reduce(self) < 0)
4739 break;
4740 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004741
Tim Peters4190fb82003-02-02 16:09:05 +00004742 case PROTO:
4743 if (load_proto(self) < 0)
4744 break;
4745 continue;
4746
Tim Peters3c67d792003-02-02 17:59:11 +00004747 case NEWTRUE:
4748 if (load_bool(self, Py_True) < 0)
4749 break;
4750 continue;
4751
4752 case NEWFALSE:
4753 if (load_bool(self, Py_False) < 0)
4754 break;
4755 continue;
4756
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004757 case '\0':
4758 /* end of file */
4759 PyErr_SetNone(PyExc_EOFError);
4760 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004761
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004762 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004763 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004764 "invalid load key, '%s'.",
4765 "c", s[0]);
4766 return NULL;
4767 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004768
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004769 break;
4770 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004771
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004772 if ((err = PyErr_Occurred())) {
4773 if (err == PyExc_EOFError) {
4774 PyErr_SetNone(PyExc_EOFError);
4775 }
4776 return NULL;
4777 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004778
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004779 PDATA_POP(self->stack, val);
4780 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004781}
Tim Peters84e87f32001-03-17 04:50:51 +00004782
Guido van Rossum60456fd1997-04-09 17:36:32 +00004783
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004784/* No-load functions to support noload, which is used to
4785 find persistent references. */
4786
4787static int
Tim Peterscba30e22003-02-01 06:24:36 +00004788noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004789{
4790 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004791
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004792 if ((i = marker(self)) < 0) return -1;
4793 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004794}
4795
4796
4797static int
Tim Peterscba30e22003-02-01 06:24:36 +00004798noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004799{
4800 int i;
4801 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004802
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004803 if ((i = marker(self)) < 0) return -1;
4804 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004805 if (self->readline_func(self, &s) < 0) return -1;
4806 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004807 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004808 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004809}
4810
4811static int
Tim Peterseab7db32003-02-13 18:24:14 +00004812noload_newobj(Unpicklerobject *self)
4813{
4814 PyObject *obj;
4815
4816 PDATA_POP(self->stack, obj); /* pop argtuple */
4817 if (obj == NULL) return -1;
4818 Py_DECREF(obj);
4819
4820 PDATA_POP(self->stack, obj); /* pop cls */
4821 if (obj == NULL) return -1;
4822 Py_DECREF(obj);
4823
4824 PDATA_APPEND(self->stack, Py_None, -1);
4825 return 0;
4826}
4827
4828static int
Tim Peterscba30e22003-02-01 06:24:36 +00004829noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004830{
4831 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004832
Tim Peters0bc93f52003-02-02 18:29:33 +00004833 if (self->readline_func(self, &s) < 0) return -1;
4834 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004835 PDATA_APPEND(self->stack, Py_None,-1);
4836 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004837}
4838
4839static int
Tim Peterscba30e22003-02-01 06:24:36 +00004840noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004841{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004842
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004843 if (self->stack->length < 2) return stackUnderflow();
4844 Pdata_clear(self->stack, self->stack->length-2);
4845 PDATA_APPEND(self->stack, Py_None,-1);
4846 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004847}
4848
4849static int
4850noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004851
Guido van Rossum053b8df1998-11-25 16:18:00 +00004852 if (self->stack->length < 1) return stackUnderflow();
4853 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004854 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004855}
4856
Tim Peters2d629652003-02-04 05:06:17 +00004857static int
4858noload_extension(Unpicklerobject *self, int nbytes)
4859{
4860 char *codebytes;
4861
4862 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4863 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4864 PDATA_APPEND(self->stack, Py_None, -1);
4865 return 0;
4866}
4867
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004868
4869static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004870noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004871{
4872 PyObject *err = 0, *val = 0;
4873 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004874
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004875 self->num_marks = 0;
4876 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004877
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004878 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004879 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004880 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004881
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004882 switch (s[0]) {
4883 case NONE:
4884 if (load_none(self) < 0)
4885 break;
4886 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004887
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004888 case BININT:
4889 if (load_binint(self) < 0)
4890 break;
4891 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004892
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004893 case BININT1:
4894 if (load_binint1(self) < 0)
4895 break;
4896 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004897
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004898 case BININT2:
4899 if (load_binint2(self) < 0)
4900 break;
4901 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004902
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004903 case INT:
4904 if (load_int(self) < 0)
4905 break;
4906 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004907
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004908 case LONG:
4909 if (load_long(self) < 0)
4910 break;
4911 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004912
Tim Peters4190fb82003-02-02 16:09:05 +00004913 case LONG1:
4914 if (load_counted_long(self, 1) < 0)
4915 break;
4916 continue;
4917
4918 case LONG4:
4919 if (load_counted_long(self, 4) < 0)
4920 break;
4921 continue;
4922
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004923 case FLOAT:
4924 if (load_float(self) < 0)
4925 break;
4926 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004927
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004928 case BINFLOAT:
4929 if (load_binfloat(self) < 0)
4930 break;
4931 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004932
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004933 case BINSTRING:
4934 if (load_binstring(self) < 0)
4935 break;
4936 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004937
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004938 case SHORT_BINSTRING:
4939 if (load_short_binstring(self) < 0)
4940 break;
4941 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004942
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004943 case STRING:
4944 if (load_string(self) < 0)
4945 break;
4946 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004947
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004948#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004949 case UNICODE:
4950 if (load_unicode(self) < 0)
4951 break;
4952 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004953
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004954 case BINUNICODE:
4955 if (load_binunicode(self) < 0)
4956 break;
4957 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004958#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004959
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004960 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004961 if (load_counted_tuple(self, 0) < 0)
4962 break;
4963 continue;
4964
4965 case TUPLE1:
4966 if (load_counted_tuple(self, 1) < 0)
4967 break;
4968 continue;
4969
4970 case TUPLE2:
4971 if (load_counted_tuple(self, 2) < 0)
4972 break;
4973 continue;
4974
4975 case TUPLE3:
4976 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004977 break;
4978 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004979
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004980 case TUPLE:
4981 if (load_tuple(self) < 0)
4982 break;
4983 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004984
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004985 case EMPTY_LIST:
4986 if (load_empty_list(self) < 0)
4987 break;
4988 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004989
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004990 case LIST:
4991 if (load_list(self) < 0)
4992 break;
4993 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004994
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004995 case EMPTY_DICT:
4996 if (load_empty_dict(self) < 0)
4997 break;
4998 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004999
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005000 case DICT:
5001 if (load_dict(self) < 0)
5002 break;
5003 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005004
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005005 case OBJ:
5006 if (noload_obj(self) < 0)
5007 break;
5008 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005009
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005010 case INST:
5011 if (noload_inst(self) < 0)
5012 break;
5013 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005014
Tim Peterseab7db32003-02-13 18:24:14 +00005015 case NEWOBJ:
5016 if (noload_newobj(self) < 0)
5017 break;
5018 continue;
5019
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005020 case GLOBAL:
5021 if (noload_global(self) < 0)
5022 break;
5023 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005024
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005025 case APPEND:
5026 if (load_append(self) < 0)
5027 break;
5028 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005029
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005030 case APPENDS:
5031 if (load_appends(self) < 0)
5032 break;
5033 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005034
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005035 case BUILD:
5036 if (noload_build(self) < 0)
5037 break;
5038 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005039
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005040 case DUP:
5041 if (load_dup(self) < 0)
5042 break;
5043 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005044
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005045 case BINGET:
5046 if (load_binget(self) < 0)
5047 break;
5048 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005049
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005050 case LONG_BINGET:
5051 if (load_long_binget(self) < 0)
5052 break;
5053 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005054
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005055 case GET:
5056 if (load_get(self) < 0)
5057 break;
5058 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005059
Tim Peters2d629652003-02-04 05:06:17 +00005060 case EXT1:
5061 if (noload_extension(self, 1) < 0)
5062 break;
5063 continue;
5064
5065 case EXT2:
5066 if (noload_extension(self, 2) < 0)
5067 break;
5068 continue;
5069
5070 case EXT4:
5071 if (noload_extension(self, 4) < 0)
5072 break;
5073 continue;
5074
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005075 case MARK:
5076 if (load_mark(self) < 0)
5077 break;
5078 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005079
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005080 case BINPUT:
5081 if (load_binput(self) < 0)
5082 break;
5083 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005084
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005085 case LONG_BINPUT:
5086 if (load_long_binput(self) < 0)
5087 break;
5088 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005089
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005090 case PUT:
5091 if (load_put(self) < 0)
5092 break;
5093 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005094
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005095 case POP:
5096 if (load_pop(self) < 0)
5097 break;
5098 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005099
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005100 case POP_MARK:
5101 if (load_pop_mark(self) < 0)
5102 break;
5103 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005104
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005105 case SETITEM:
5106 if (load_setitem(self) < 0)
5107 break;
5108 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005109
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005110 case SETITEMS:
5111 if (load_setitems(self) < 0)
5112 break;
5113 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005114
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005115 case STOP:
5116 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005117
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005118 case PERSID:
5119 if (load_persid(self) < 0)
5120 break;
5121 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005122
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005123 case BINPERSID:
5124 if (load_binpersid(self) < 0)
5125 break;
5126 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005127
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005128 case REDUCE:
5129 if (noload_reduce(self) < 0)
5130 break;
5131 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005132
Tim Peters4190fb82003-02-02 16:09:05 +00005133 case PROTO:
5134 if (load_proto(self) < 0)
5135 break;
5136 continue;
5137
Tim Peters3c67d792003-02-02 17:59:11 +00005138 case NEWTRUE:
5139 if (load_bool(self, Py_True) < 0)
5140 break;
5141 continue;
5142
5143 case NEWFALSE:
5144 if (load_bool(self, Py_False) < 0)
5145 break;
5146 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005147 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005148 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005149 "invalid load key, '%s'.",
5150 "c", s[0]);
5151 return NULL;
5152 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005153
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005154 break;
5155 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005156
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005157 if ((err = PyErr_Occurred())) {
5158 if (err == PyExc_EOFError) {
5159 PyErr_SetNone(PyExc_EOFError);
5160 }
5161 return NULL;
5162 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005163
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005164 PDATA_POP(self->stack, val);
5165 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005166}
Tim Peters84e87f32001-03-17 04:50:51 +00005167
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005168
Guido van Rossum60456fd1997-04-09 17:36:32 +00005169static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005170Unpickler_load(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005171{
Tim Peterscba30e22003-02-01 06:24:36 +00005172 if (!( PyArg_ParseTuple(args, ":load")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005173 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005174
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005175 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005176}
5177
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005178static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005179Unpickler_noload(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005180{
Tim Peterscba30e22003-02-01 06:24:36 +00005181 if (!( PyArg_ParseTuple(args, ":noload")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005182 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005184 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005185}
5186
Guido van Rossum60456fd1997-04-09 17:36:32 +00005187
5188static struct PyMethodDef Unpickler_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005189 {"load", (PyCFunction)Unpickler_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005190 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005191 },
Neal Norwitzb0493252002-03-31 14:44:22 +00005192 {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005193 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005194 "noload() -- not load a pickle, but go through most of the motions\n"
5195 "\n"
5196 "This function can be used to read past a pickle without instantiating\n"
5197 "any objects or importing any modules. It can also be used to find all\n"
5198 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005199 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005200 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005201 {NULL, NULL} /* sentinel */
5202};
5203
5204
5205static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005206newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005207{
5208 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005209
Tim Peterscba30e22003-02-01 06:24:36 +00005210 if (!( self = PyObject_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005211 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005212
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005213 self->file = NULL;
5214 self->arg = NULL;
5215 self->stack = (Pdata*)Pdata_New();
5216 self->pers_func = NULL;
5217 self->last_string = NULL;
5218 self->marks = NULL;
5219 self->num_marks = 0;
5220 self->marks_size = 0;
5221 self->buf_size = 0;
5222 self->read = NULL;
5223 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005224 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005225
Tim Peterscba30e22003-02-01 06:24:36 +00005226 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005227 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005228
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005229 Py_INCREF(f);
5230 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005231
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005232 /* Set read, readline based on type of f */
5233 if (PyFile_Check(f)) {
5234 self->fp = PyFile_AsFile(f);
5235 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005236 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005237 "I/O operation on closed file");
5238 goto err;
5239 }
5240 self->read_func = read_file;
5241 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005242 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005243 else if (PycStringIO_InputCheck(f)) {
5244 self->fp = NULL;
5245 self->read_func = read_cStringIO;
5246 self->readline_func = readline_cStringIO;
5247 }
5248 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005249
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005250 self->fp = NULL;
5251 self->read_func = read_other;
5252 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005253
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005254 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5255 (self->read = PyObject_GetAttr(f, read_str)))) {
5256 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005257 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005258 "argument must have 'read' and "
5259 "'readline' attributes" );
5260 goto err;
5261 }
5262 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005263
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005264 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005265
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005266 err:
5267 Py_DECREF((PyObject *)self);
5268 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005269}
5270
5271
5272static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005273get_Unpickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005274{
5275 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00005276
Tim Peterscba30e22003-02-01 06:24:36 +00005277 if (!( PyArg_ParseTuple(args, "O:Unpickler", &file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005278 return NULL;
5279 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005280}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005281
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005282
Guido van Rossum60456fd1997-04-09 17:36:32 +00005283static void
Tim Peterscba30e22003-02-01 06:24:36 +00005284Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005285{
5286 Py_XDECREF(self->readline);
5287 Py_XDECREF(self->read);
5288 Py_XDECREF(self->file);
5289 Py_XDECREF(self->memo);
5290 Py_XDECREF(self->stack);
5291 Py_XDECREF(self->pers_func);
5292 Py_XDECREF(self->arg);
5293 Py_XDECREF(self->last_string);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005294
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005295 if (self->marks) {
5296 free(self->marks);
5297 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005298
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005299 if (self->buf_size) {
5300 free(self->buf);
5301 }
Tim Peters84e87f32001-03-17 04:50:51 +00005302
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005303 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005304}
5305
5306
5307static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005308Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005309{
5310 if (!strcmp(name, "persistent_load")) {
5311 if (!self->pers_func) {
5312 PyErr_SetString(PyExc_AttributeError, name);
5313 return NULL;
5314 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005315
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005316 Py_INCREF(self->pers_func);
5317 return self->pers_func;
5318 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005319
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005320 if (!strcmp(name, "find_global")) {
5321 if (!self->find_class) {
5322 PyErr_SetString(PyExc_AttributeError, name);
5323 return NULL;
5324 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005326 Py_INCREF(self->find_class);
5327 return self->find_class;
5328 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005329
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005330 if (!strcmp(name, "memo")) {
5331 if (!self->memo) {
5332 PyErr_SetString(PyExc_AttributeError, name);
5333 return NULL;
5334 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005335
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005336 Py_INCREF(self->memo);
5337 return self->memo;
5338 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005339
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005340 if (!strcmp(name, "UnpicklingError")) {
5341 Py_INCREF(UnpicklingError);
5342 return UnpicklingError;
5343 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005344
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005345 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005346}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005347
Guido van Rossum60456fd1997-04-09 17:36:32 +00005348
5349static int
Tim Peterscba30e22003-02-01 06:24:36 +00005350Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005351{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005352
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005353 if (!strcmp(name, "persistent_load")) {
5354 Py_XDECREF(self->pers_func);
5355 self->pers_func = value;
5356 Py_XINCREF(value);
5357 return 0;
5358 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005359
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005360 if (!strcmp(name, "find_global")) {
5361 Py_XDECREF(self->find_class);
5362 self->find_class = value;
5363 Py_XINCREF(value);
5364 return 0;
5365 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005366
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005367 if (! value) {
5368 PyErr_SetString(PyExc_TypeError,
5369 "attribute deletion is not supported");
5370 return -1;
5371 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005372
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005373 if (strcmp(name, "memo") == 0) {
5374 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005375 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005376 "memo must be a dictionary");
5377 return -1;
5378 }
5379 Py_XDECREF(self->memo);
5380 self->memo = value;
5381 Py_INCREF(value);
5382 return 0;
5383 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005384
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005385 PyErr_SetString(PyExc_AttributeError, name);
5386 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005387}
5388
Tim Peters5bd2a792003-02-01 16:45:06 +00005389/* ---------------------------------------------------------------------------
5390 * Module-level functions.
5391 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005392
Tim Peters5bd2a792003-02-01 16:45:06 +00005393/* dump(obj, file, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005394static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005395cpm_dump(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005396{
5397 PyObject *ob, *file, *res = NULL;
5398 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005399 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005400
Tim Peters5bd2a792003-02-01 16:45:06 +00005401 if (!( PyArg_ParseTuple(args, "OO|i", &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005402 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005403
Tim Peters5bd2a792003-02-01 16:45:06 +00005404 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005405 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005406
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005407 if (dump(pickler, ob) < 0)
5408 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005409
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005410 Py_INCREF(Py_None);
5411 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005412
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005413 finally:
5414 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005416 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005417}
5418
5419
Tim Peters5bd2a792003-02-01 16:45:06 +00005420/* dumps(obj, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005421static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005422cpm_dumps(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005423{
5424 PyObject *ob, *file = 0, *res = NULL;
5425 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005426 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005427
Tim Peters5bd2a792003-02-01 16:45:06 +00005428 if (!( PyArg_ParseTuple(args, "O|i:dumps", &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005429 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005430
Tim Peterscba30e22003-02-01 06:24:36 +00005431 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005432 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005433
Tim Peters5bd2a792003-02-01 16:45:06 +00005434 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005435 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005436
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005437 if (dump(pickler, ob) < 0)
5438 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005439
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005440 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005441
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005442 finally:
5443 Py_XDECREF(pickler);
5444 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005445
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005446 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005447}
5448
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005449
Tim Peters5bd2a792003-02-01 16:45:06 +00005450/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005451static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005452cpm_load(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005453{
5454 Unpicklerobject *unpickler = 0;
5455 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005456
Tim Peterscba30e22003-02-01 06:24:36 +00005457 if (!( PyArg_ParseTuple(args, "O:load", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005458 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005459
Tim Peterscba30e22003-02-01 06:24:36 +00005460 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005461 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005462
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005463 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005465 finally:
5466 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005467
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005468 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005469}
5470
5471
Tim Peters5bd2a792003-02-01 16:45:06 +00005472/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005473static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005474cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005475{
5476 PyObject *ob, *file = 0, *res = NULL;
5477 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005478
Tim Peterscba30e22003-02-01 06:24:36 +00005479 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005480 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005481
Tim Peterscba30e22003-02-01 06:24:36 +00005482 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005483 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005484
Tim Peterscba30e22003-02-01 06:24:36 +00005485 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005486 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005487
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005488 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005489
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005490 finally:
5491 Py_XDECREF(file);
5492 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005493
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005494 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005495}
5496
5497
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005498PyDoc_STRVAR(Unpicklertype__doc__,
5499"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005500
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005501static PyTypeObject Unpicklertype = {
5502 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00005503 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00005504 "cPickle.Unpickler", /*tp_name*/
Guido van Rossum60456fd1997-04-09 17:36:32 +00005505 sizeof(Unpicklerobject), /*tp_basicsize*/
5506 0, /*tp_itemsize*/
5507 /* methods */
5508 (destructor)Unpickler_dealloc, /*tp_dealloc*/
5509 (printfunc)0, /*tp_print*/
5510 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
5511 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
5512 (cmpfunc)0, /*tp_compare*/
5513 (reprfunc)0, /*tp_repr*/
5514 0, /*tp_as_number*/
5515 0, /*tp_as_sequence*/
5516 0, /*tp_as_mapping*/
5517 (hashfunc)0, /*tp_hash*/
5518 (ternaryfunc)0, /*tp_call*/
5519 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005520
Guido van Rossum60456fd1997-04-09 17:36:32 +00005521 /* Space for future expansion */
5522 0L,0L,0L,0L,
5523 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005524};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005525
Guido van Rossum60456fd1997-04-09 17:36:32 +00005526static struct PyMethodDef cPickle_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005527 {"dump", (PyCFunction)cpm_dump, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005528 PyDoc_STR("dump(object, file, proto=0) -- "
5529 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005530 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005531 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005532 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005533
Neal Norwitzb0493252002-03-31 14:44:22 +00005534 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005535 PyDoc_STR("dumps(object, proto=0) -- "
5536 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005537 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005538 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005539 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005540
Neal Norwitzb0493252002-03-31 14:44:22 +00005541 {"load", (PyCFunction)cpm_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005542 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005543
Neal Norwitzb0493252002-03-31 14:44:22 +00005544 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005545 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005546
Neal Norwitzb0493252002-03-31 14:44:22 +00005547 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005548 PyDoc_STR("Pickler(file, proto=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005549 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005550 "This takes a file-like object for writing a pickle data stream.\n"
5551 "The optional proto argument tells the pickler to use the given\n"
5552 "protocol; supported protocols are 0, 1, 2. The default\n"
5553 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5554 "only protocol that can be written to a file opened in text\n"
5555 "mode and read back successfully. When using a protocol higher\n"
5556 "than 0, make sure the file is opened in binary mode, both when\n"
5557 "pickling and unpickling.)\n"
5558 "\n"
5559 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5560 "more efficient than protocol 1.\n"
5561 "\n"
5562 "Specifying a negative protocol version selects the highest\n"
5563 "protocol version supported. The higher the protocol used, the\n"
5564 "more recent the version of Python needed to read the pickle\n"
5565 "produced.\n"
5566 "\n"
5567 "The file parameter must have a write() method that accepts a single\n"
5568 "string argument. It can thus be an open file object, a StringIO\n"
5569 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005570 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005571
Neal Norwitzb0493252002-03-31 14:44:22 +00005572 {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005573 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5574
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005575 { NULL, NULL }
5576};
5577
Guido van Rossum60456fd1997-04-09 17:36:32 +00005578static int
Tim Peterscba30e22003-02-01 06:24:36 +00005579init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005580{
5581 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005582
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005583#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005584
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005585 INIT_STR(__class__);
5586 INIT_STR(__getinitargs__);
5587 INIT_STR(__dict__);
5588 INIT_STR(__getstate__);
5589 INIT_STR(__setstate__);
5590 INIT_STR(__name__);
5591 INIT_STR(__main__);
5592 INIT_STR(__reduce__);
5593 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005594 INIT_STR(append);
5595 INIT_STR(read);
5596 INIT_STR(readline);
5597 INIT_STR(copy_reg);
5598 INIT_STR(dispatch_table);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005599 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005600
Tim Peterscba30e22003-02-01 06:24:36 +00005601 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005602 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005603
Tim Peters1f1b2d22003-02-01 02:16:37 +00005604 /* This is special because we want to use a different
5605 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005606 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005607 if (!dispatch_table) return -1;
5608
5609 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005610 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005611 if (!extension_registry) return -1;
5612
5613 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005614 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005615 if (!inverted_registry) return -1;
5616
5617 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005618 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005619 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005620
Tim Peters71fcda52003-02-14 23:05:28 +00005621 better_reduce = PyObject_GetAttrString(copy_reg, "_better_reduce");
5622 if (!better_reduce) return -1;
5623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005624 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005625
Tim Peters71fcda52003-02-14 23:05:28 +00005626 object_reduce = PyObject_GetAttrString((PyObject *)&PyBaseObject_Type,
5627 "__reduce__");
5628 if (object_reduce == NULL) return -1;
5629
Tim Peters731098b2003-02-04 20:56:09 +00005630 if (!(empty_tuple = PyTuple_New(0)))
5631 return -1;
5632
5633 two_tuple = PyTuple_New(2);
5634 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005635 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005636 /* We use this temp container with no regard to refcounts, or to
5637 * keeping containees alive. Exempt from GC, because we don't
5638 * want anything looking at two_tuple() by magic.
5639 */
5640 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005641
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005642 /* Ugh */
5643 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5644 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5645 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005646
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005647 if (!( t=PyDict_New())) return -1;
5648 if (!( r=PyRun_String(
5649 "def __init__(self, *args): self.args=args\n\n"
5650 "def __str__(self):\n"
5651 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5652 Py_file_input,
5653 module_dict, t) )) return -1;
5654 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005655
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005656 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005657 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005658 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005660 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005661
Tim Peterscba30e22003-02-01 06:24:36 +00005662 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005663 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005664 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005665 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005666
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005667 if (!( t=PyDict_New())) return -1;
5668 if (!( r=PyRun_String(
5669 "def __init__(self, *args): self.args=args\n\n"
5670 "def __str__(self):\n"
5671 " a=self.args\n"
5672 " a=a and type(a[0]) or '(what)'\n"
5673 " return 'Cannot pickle %s objects' % a\n"
5674 , Py_file_input,
5675 module_dict, t) )) return -1;
5676 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005677
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005678 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005679 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005680 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005681
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005682 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005683
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005684 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005685 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005686 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005687
Martin v. Löwis658009a2002-09-16 17:26:24 +00005688 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5689 UnpicklingError, NULL)))
5690 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005691
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005692 if (PyDict_SetItemString(module_dict, "PickleError",
5693 PickleError) < 0)
5694 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005696 if (PyDict_SetItemString(module_dict, "PicklingError",
5697 PicklingError) < 0)
5698 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005699
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005700 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5701 UnpicklingError) < 0)
5702 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005703
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005704 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5705 UnpickleableError) < 0)
5706 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005707
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005708 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5709 BadPickleGet) < 0)
5710 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005711
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005712 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005713
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005714 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005715}
5716
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005717#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5718#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005719#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005720PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005721initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005722{
5723 PyObject *m, *d, *di, *v, *k;
5724 int i;
Tim Peters5b7da392003-02-04 00:21:07 +00005725 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005726 PyObject *format_version;
5727 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005728
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005729 Picklertype.ob_type = &PyType_Type;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005730 Unpicklertype.ob_type = &PyType_Type;
5731 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005732
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005733 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005734 * so we're forced to use a temporary dictionary. :(
5735 */
5736 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005737 if (!di) return;
5738 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005739
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005740 /* Create the module and add the functions */
5741 m = Py_InitModule4("cPickle", cPickle_methods,
5742 cPickle_module_documentation,
5743 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005744
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005745 /* Add some symbolic constants to the module */
5746 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005747 v = PyString_FromString(rev);
5748 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005749 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005750
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005751 /* Copy data from di. Waaa. */
5752 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5753 if (PyObject_SetItem(d, k, v) < 0) {
5754 Py_DECREF(di);
5755 return;
5756 }
5757 }
5758 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005759
Tim Peters8587b3c2003-02-13 15:44:41 +00005760 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5761 if (i < 0)
5762 return;
5763
Tim Peters5b7da392003-02-04 00:21:07 +00005764 /* These are purely informational; no code uses them. */
5765 /* File format version we write. */
5766 format_version = PyString_FromString("2.0");
5767 /* Format versions we can read. */
5768 compatible_formats = Py_BuildValue("[sssss]",
5769 "1.0", /* Original protocol 0 */
5770 "1.1", /* Protocol 0 + INST */
5771 "1.2", /* Original protocol 1 */
5772 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005773 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005774 PyDict_SetItemString(d, "format_version", format_version);
5775 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5776 Py_XDECREF(format_version);
5777 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005778}