blob: 105a2e9a03504579b8aff318d172aeea833adeb7 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001#include "Python.h"
2#include "cStringIO.h"
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003#include "structmember.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005PyDoc_STRVAR(cPickle_module_documentation,
Tim Peters64c04d12003-02-01 06:27:59 +00006"C implementation and optimization of the Python pickle module.");
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007
Guido van Rossum142eeb81997-08-13 03:14:41 +00008#ifndef Py_eval_input
9#include <graminit.h>
10#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000011#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000012
Guido van Rossum60456fd1997-04-09 17:36:32 +000013#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000014
Guido van Rossum60456fd1997-04-09 17:36:32 +000015#define WRITE_BUF_SIZE 256
16
Tim Peters5bd2a792003-02-01 16:45:06 +000017/* Bump this when new opcodes are added to the pickle protocol. */
Tim Peters8587b3c2003-02-13 15:44:41 +000018#define HIGHEST_PROTOCOL 2
Tim Peters5bd2a792003-02-01 16:45:06 +000019
Tim Peters797ec242003-02-01 06:22:36 +000020/*
21 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
22 * docs are in pickletools.py.
23 */
Guido van Rossum60456fd1997-04-09 17:36:32 +000024#define MARK '('
25#define STOP '.'
26#define POP '0'
27#define POP_MARK '1'
28#define DUP '2'
29#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000030#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000031#define INT 'I'
32#define BININT 'J'
33#define BININT1 'K'
34#define LONG 'L'
35#define BININT2 'M'
36#define NONE 'N'
37#define PERSID 'P'
38#define BINPERSID 'Q'
39#define REDUCE 'R'
40#define STRING 'S'
41#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000042#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000043#define UNICODE 'V'
44#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000045#define APPEND 'a'
46#define BUILD 'b'
47#define GLOBAL 'c'
48#define DICT 'd'
49#define EMPTY_DICT '}'
50#define APPENDS 'e'
51#define GET 'g'
52#define BINGET 'h'
53#define INST 'i'
54#define LONG_BINGET 'j'
55#define LIST 'l'
56#define EMPTY_LIST ']'
57#define OBJ 'o'
58#define PUT 'p'
59#define BINPUT 'q'
60#define LONG_BINPUT 'r'
61#define SETITEM 's'
62#define TUPLE 't'
63#define EMPTY_TUPLE ')'
64#define SETITEMS 'u'
Tim Peters797ec242003-02-01 06:22:36 +000065
66/* Protocol 2. */
67#define PROTO '\x80' /* identify pickle protocol */
68#define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
69#define EXT1 '\x82' /* push object from extension registry; 1-byte index */
70#define EXT2 '\x83' /* ditto, but 2-byte index */
71#define EXT4 '\x84' /* ditto, but 4-byte index */
72#define TUPLE1 '\x85' /* build 1-tuple from stack top */
73#define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
74#define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
75#define NEWTRUE '\x88' /* push True */
76#define NEWFALSE '\x89' /* push False */
77#define LONG1 '\x8a' /* push long from < 256 bytes */
78#define LONG4 '\x8b' /* push really big long */
79
80/* There aren't opcodes -- they're ways to pickle bools before protocol 2,
81 * so that unpicklers written before bools were introduced unpickle them
82 * as ints, but unpicklers after can recognize that bools were intended.
83 * Note that protocol 2 added direct ways to pickle bools.
84 */
Jack Jansen3a967022002-06-26 20:40:42 +000085#undef TRUE
Guido van Rossume2763392002-04-05 19:30:08 +000086#define TRUE "I01\n"
Jack Jansen3a967022002-06-26 20:40:42 +000087#undef FALSE
Guido van Rossume2763392002-04-05 19:30:08 +000088#define FALSE "I00\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +000089
Tim Peters1092d642003-02-11 21:06:20 +000090/* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
Tim Peters42f08ac2003-02-11 22:43:24 +000091 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
92 * break if this gets out of synch with pickle.py, but it's unclear that
93 * would help anything either.
Tim Peters1092d642003-02-11 21:06:20 +000094 */
95#define BATCHSIZE 1000
96
Guido van Rossum60456fd1997-04-09 17:36:32 +000097static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +000098
Guido van Rossumc03158b1999-06-09 15:23:31 +000099static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000100static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000101static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000102static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000103static PyObject *BadPickleGet;
104
Tim Peters5b7da392003-02-04 00:21:07 +0000105/* As the name says, an empty tuple. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000106static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000107
Tim Peters5b7da392003-02-04 00:21:07 +0000108/* copy_reg.dispatch_table, {type_object: pickling_function} */
109static PyObject *dispatch_table;
110
111/* For EXT[124] opcodes. */
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000112/* copy_reg._extension_registry, {(module_name, function_name): code} */
Tim Peters5b7da392003-02-04 00:21:07 +0000113static PyObject *extension_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000114/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
Tim Peters5b7da392003-02-04 00:21:07 +0000115static PyObject *inverted_registry;
Guido van Rossumd4b920c2003-02-04 01:54:49 +0000116/* copy_reg._extension_cache, {code: object} */
Tim Peters5b7da392003-02-04 00:21:07 +0000117static PyObject *extension_cache;
118
Tim Peters731098b2003-02-04 20:56:09 +0000119/* For looking up name pairs in copy_reg._extension_registry. */
120static PyObject *two_tuple;
121
Guido van Rossum60456fd1997-04-09 17:36:32 +0000122static PyObject *__class___str, *__getinitargs___str, *__dict___str,
123 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
Guido van Rossumb289b872003-02-19 01:45:13 +0000124 *__reduce_ex___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000125 *write_str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000126 *read_str, *readline_str, *__main___str, *__basicnew___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000127 *copy_reg_str, *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000128
Guido van Rossum053b8df1998-11-25 16:18:00 +0000129/*************************************************************************
130 Internal Data type for pickle data. */
131
132typedef struct {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000133 PyObject_HEAD
Tim Peters1d63c9f2003-02-02 20:29:39 +0000134 int length; /* number of initial slots in data currently used */
135 int size; /* number of slots in data allocated */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000136 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000137} Pdata;
138
Tim Peters84e87f32001-03-17 04:50:51 +0000139static void
Tim Peterscba30e22003-02-01 06:24:36 +0000140Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000141{
142 int i;
143 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000144
Tim Peters1d63c9f2003-02-02 20:29:39 +0000145 for (i = self->length, p = self->data; --i >= 0; p++) {
146 Py_DECREF(*p);
147 }
148 if (self->data)
149 free(self->data);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000150 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000151}
152
153static PyTypeObject PdataType = {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000154 PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0,
155 (destructor)Pdata_dealloc,
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000157};
158
159#define Pdata_Check(O) ((O)->ob_type == &PdataType)
160
161static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000162Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000163{
164 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000165
Tim Peters1d63c9f2003-02-02 20:29:39 +0000166 if (!(self = PyObject_New(Pdata, &PdataType)))
167 return NULL;
168 self->size = 8;
169 self->length = 0;
170 self->data = malloc(self->size * sizeof(PyObject*));
171 if (self->data)
172 return (PyObject*)self;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000173 Py_DECREF(self);
174 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000175}
176
Tim Peters84e87f32001-03-17 04:50:51 +0000177static int
Tim Peterscba30e22003-02-01 06:24:36 +0000178stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000179{
180 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
181 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000182}
183
Tim Peters1d63c9f2003-02-02 20:29:39 +0000184/* Retain only the initial clearto items. If clearto >= the current
185 * number of items, this is a (non-erroneous) NOP.
186 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000187static int
Tim Peterscba30e22003-02-01 06:24:36 +0000188Pdata_clear(Pdata *self, int clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000189{
190 int i;
191 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000193 if (clearto < 0) return stackUnderflow();
194 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000195
Tim Peters1d63c9f2003-02-02 20:29:39 +0000196 for (i = self->length, p = self->data + clearto;
197 --i >= clearto;
198 p++) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000199 Py_DECREF(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000200 }
201 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000203 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000204}
205
Tim Peters84e87f32001-03-17 04:50:51 +0000206static int
Tim Peterscba30e22003-02-01 06:24:36 +0000207Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000208{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000209 int bigger;
210 size_t nbytes;
211
Tim Peters1d63c9f2003-02-02 20:29:39 +0000212 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000213 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000214 goto nomemory;
215 if ((int)(size_t)bigger != bigger)
216 goto nomemory;
217 nbytes = (size_t)bigger * sizeof(PyObject *);
218 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
219 goto nomemory;
220 self->data = realloc(self->data, nbytes);
221 if (self->data == NULL)
222 goto nomemory;
223 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000224 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000225
226 nomemory:
227 self->size = 0;
228 PyErr_NoMemory();
229 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000230}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000231
Tim Peterse0a39072003-02-03 15:45:56 +0000232/* D is a Pdata*. Pop the topmost element and store it into V, which
233 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000234 * is raised and V is set to NULL. D and V may be evaluated several times.
235 */
236#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000237 if ((D)->length) \
238 (V) = (D)->data[--((D)->length)]; \
239 else { \
240 PyErr_SetString(UnpicklingError, "bad pickle data"); \
241 (V) = NULL; \
242 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000243}
244
Tim Peterse0a39072003-02-03 15:45:56 +0000245/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
246 * D. If the Pdata stack can't be grown to hold the new value, both
247 * raise MemoryError and execute "return ER". The difference is in ownership
248 * of O after: _PUSH transfers ownership of O from the caller to the stack
249 * (no incref of O is done, and in case of error O is decrefed), while
250 * _APPEND pushes a new reference.
251 */
252
253/* Push O on stack D, giving ownership of O to the stack. */
254#define PDATA_PUSH(D, O, ER) { \
255 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
256 Pdata_grow((Pdata*)(D)) < 0) { \
257 Py_DECREF(O); \
258 return ER; \
259 } \
260 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
261}
262
263/* Push O on stack D, pushing a new reference. */
264#define PDATA_APPEND(D, O, ER) { \
265 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
266 Pdata_grow((Pdata*)(D)) < 0) \
267 return ER; \
268 Py_INCREF(O); \
269 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
270}
271
272
Guido van Rossum053b8df1998-11-25 16:18:00 +0000273static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000274Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000275{
276 PyObject *r;
277 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000278
Tim Peters1d63c9f2003-02-02 20:29:39 +0000279 l = self->length-start;
280 r = PyTuple_New(l);
281 if (r == NULL)
282 return NULL;
283 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000284 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000285
Tim Peters1d63c9f2003-02-02 20:29:39 +0000286 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000287 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000288}
289
290static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000291Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000292{
293 PyObject *r;
294 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000295
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000296 l=self->length-start;
297 if (!( r=PyList_New(l))) return NULL;
298 for (i=start, j=0 ; j < l; i++, j++)
299 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000301 self->length=start;
302 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000303}
304
Guido van Rossum053b8df1998-11-25 16:18:00 +0000305/*************************************************************************/
306
307#define ARG_TUP(self, o) { \
308 if (self->arg || (self->arg=PyTuple_New(1))) { \
309 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
310 PyTuple_SET_ITEM(self->arg,0,o); \
311 } \
312 else { \
313 Py_DECREF(o); \
314 } \
315}
316
317#define FREE_ARG_TUP(self) { \
318 if (self->arg->ob_refcnt > 1) { \
319 Py_DECREF(self->arg); \
320 self->arg=NULL; \
321 } \
322 }
323
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000324typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000325 PyObject_HEAD
326 FILE *fp;
327 PyObject *write;
328 PyObject *file;
329 PyObject *memo;
330 PyObject *arg;
331 PyObject *pers_func;
332 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000333
334 /* pickle protocol number, >= 0 */
335 int proto;
336
337 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000338 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000339
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000340 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis5a395302002-08-04 08:20:23 +0000341 int nesting;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000342 int (*write_func)(struct Picklerobject *, char *, int);
343 char *write_buf;
344 int buf_size;
345 PyObject *dispatch_table;
346 int fast_container; /* count nested container dumps */
347 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000348} Picklerobject;
349
Barry Warsaw52acb492001-12-21 20:04:22 +0000350#ifndef PY_CPICKLE_FAST_LIMIT
351#define PY_CPICKLE_FAST_LIMIT 50
352#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000353
Jeremy Hylton938ace62002-07-17 16:30:39 +0000354static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000355
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000356typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000357 PyObject_HEAD
358 FILE *fp;
359 PyObject *file;
360 PyObject *readline;
361 PyObject *read;
362 PyObject *memo;
363 PyObject *arg;
364 Pdata *stack;
365 PyObject *mark;
366 PyObject *pers_func;
367 PyObject *last_string;
368 int *marks;
369 int num_marks;
370 int marks_size;
371 int (*read_func)(struct Unpicklerobject *, char **, int);
372 int (*readline_func)(struct Unpicklerobject *, char **);
373 int buf_size;
374 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000375 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000376} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000377
Jeremy Hylton938ace62002-07-17 16:30:39 +0000378static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000379
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000380/* Forward decls that need the above structs */
381static int save(Picklerobject *, PyObject *, int);
382static int put2(Picklerobject *, PyObject *);
383
Guido van Rossumd385d591997-04-09 17:47:47 +0000384static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000385PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000386cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
387{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000388 va_list va;
389 PyObject *args=0, *retval=0;
390 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000392 if (format) args = Py_VaBuildValue(format, va);
393 va_end(va);
394 if (format && ! args) return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000395 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000396 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000397
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000398 if (retval) {
399 if (args) {
400 PyObject *v;
401 v=PyString_Format(retval, args);
402 Py_DECREF(retval);
403 Py_DECREF(args);
404 if (! v) return NULL;
405 retval=v;
406 }
407 }
408 else
409 if (args) retval=args;
410 else {
411 PyErr_SetObject(ErrType,Py_None);
412 return NULL;
413 }
414 PyErr_SetObject(ErrType,retval);
415 Py_DECREF(retval);
416 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000417}
418
Tim Peters84e87f32001-03-17 04:50:51 +0000419static int
Tim Peterscba30e22003-02-01 06:24:36 +0000420write_file(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000421{
422 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000424 if (s == NULL) {
425 return 0;
426 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000427
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000428 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000429 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000430 Py_END_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000431 if (nbyteswritten != (size_t)n) {
432 PyErr_SetFromErrno(PyExc_IOError);
433 return -1;
434 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000435
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000436 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000437}
438
Tim Peters84e87f32001-03-17 04:50:51 +0000439static int
Tim Peterscba30e22003-02-01 06:24:36 +0000440write_cStringIO(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000441{
442 if (s == NULL) {
443 return 0;
444 }
Tim Peterscba30e22003-02-01 06:24:36 +0000445
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000446 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
447 return -1;
448 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000449
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000450 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000451}
452
Tim Peters84e87f32001-03-17 04:50:51 +0000453static int
Tim Peterscba30e22003-02-01 06:24:36 +0000454write_none(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000455{
456 if (s == NULL) return 0;
457 return n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000458}
459
Tim Peters84e87f32001-03-17 04:50:51 +0000460static int
Tim Peterscba30e22003-02-01 06:24:36 +0000461write_other(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000462{
463 PyObject *py_str = 0, *junk = 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000465 if (s == NULL) {
466 if (!( self->buf_size )) return 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000467 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000468 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000469 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000470 return -1;
471 }
472 else {
473 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
474 if (write_other(self, NULL, 0) < 0)
475 return -1;
476 }
Tim Peterscba30e22003-02-01 06:24:36 +0000477
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000478 if (n > WRITE_BUF_SIZE) {
479 if (!( py_str =
Tim Peterscba30e22003-02-01 06:24:36 +0000480 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000481 return -1;
482 }
483 else {
484 memcpy(self->write_buf + self->buf_size, s, n);
485 self->buf_size += n;
486 return n;
487 }
488 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000489
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000490 if (self->write) {
491 /* object with write method */
492 ARG_TUP(self, py_str);
493 if (self->arg) {
494 junk = PyObject_Call(self->write, self->arg, NULL);
495 FREE_ARG_TUP(self);
496 }
497 if (junk) Py_DECREF(junk);
498 else return -1;
499 }
500 else
501 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000503 self->buf_size = 0;
504 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000505}
506
507
Tim Peters84e87f32001-03-17 04:50:51 +0000508static int
Tim Petersee1a53c2003-02-02 02:57:53 +0000509read_file(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000510{
511 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000512
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000513 if (self->buf_size == 0) {
514 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000515
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000516 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000517 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000518 PyErr_NoMemory();
519 return -1;
520 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000521
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000522 self->buf_size = size;
523 }
524 else if (n > self->buf_size) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000525 self->buf = (char *)realloc(self->buf, n);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000526 if (!self->buf) {
527 PyErr_NoMemory();
528 return -1;
529 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000530 self->buf_size = n;
531 }
Tim Peters84e87f32001-03-17 04:50:51 +0000532
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000533 Py_BEGIN_ALLOW_THREADS
534 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
535 Py_END_ALLOW_THREADS
536 if (nbytesread != (size_t)n) {
537 if (feof(self->fp)) {
538 PyErr_SetNone(PyExc_EOFError);
539 return -1;
540 }
Tim Peterscba30e22003-02-01 06:24:36 +0000541
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000542 PyErr_SetFromErrno(PyExc_IOError);
543 return -1;
544 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000545
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000546 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000547
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000548 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000549}
550
551
Tim Peters84e87f32001-03-17 04:50:51 +0000552static int
Tim Peterscba30e22003-02-01 06:24:36 +0000553readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000554{
555 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000556
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000557 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000558 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000559 PyErr_NoMemory();
560 return -1;
561 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000562 self->buf_size = 40;
563 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000564
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000565 i = 0;
566 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000567 int bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000568 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000569 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000570 (self->buf[i] = getc(self->fp)) == '\n') {
571 self->buf[i + 1] = '\0';
572 *s = self->buf;
573 return i + 1;
574 }
575 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000576 bigger = self->buf_size << 1;
577 if (bigger <= 0) { /* overflow */
578 PyErr_NoMemory();
579 return -1;
580 }
581 self->buf = (char *)realloc(self->buf, bigger);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000582 if (!self->buf) {
583 PyErr_NoMemory();
584 return -1;
585 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000586 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000587 }
Tim Peters84e87f32001-03-17 04:50:51 +0000588}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000589
590
Tim Peters84e87f32001-03-17 04:50:51 +0000591static int
Tim Peterscba30e22003-02-01 06:24:36 +0000592read_cStringIO(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000593{
594 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000595
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000596 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
597 PyErr_SetNone(PyExc_EOFError);
598 return -1;
599 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000600
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000601 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000602
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000603 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000604}
605
606
Tim Peters84e87f32001-03-17 04:50:51 +0000607static int
Tim Peterscba30e22003-02-01 06:24:36 +0000608readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000609{
610 int n;
611 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000613 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
614 return -1;
615 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000616
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000617 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000619 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000620}
621
622
Tim Peters84e87f32001-03-17 04:50:51 +0000623static int
Tim Peterscba30e22003-02-01 06:24:36 +0000624read_other(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000625{
626 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000628 if (!( bytes = PyInt_FromLong(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000630 ARG_TUP(self, bytes);
631 if (self->arg) {
632 str = PyObject_Call(self->read, self->arg, NULL);
633 FREE_ARG_TUP(self);
634 }
635 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000636
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000637 Py_XDECREF(self->last_string);
638 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000639
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000640 if (! (*s = PyString_AsString(str))) return -1;
641 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000642}
643
644
Tim Peters84e87f32001-03-17 04:50:51 +0000645static int
Tim Peterscba30e22003-02-01 06:24:36 +0000646readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000647{
648 PyObject *str;
649 int str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000650
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000651 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
652 return -1;
653 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000655 if ((str_size = PyString_Size(str)) < 0)
656 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000658 Py_XDECREF(self->last_string);
659 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000660
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000661 if (! (*s = PyString_AsString(str)))
662 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000663
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000664 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000665}
666
Tim Petersee1a53c2003-02-02 02:57:53 +0000667/* Copy the first n bytes from s into newly malloc'ed memory, plus a
668 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
669 * The caller is responsible for free()'ing the return value.
670 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671static char *
Tim Petersee1a53c2003-02-02 02:57:53 +0000672pystrndup(char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000673{
Tim Petersee1a53c2003-02-02 02:57:53 +0000674 char *r = (char *)malloc(n+1);
675 if (r == NULL)
676 return (char*)PyErr_NoMemory();
677 memcpy(r, s, n);
678 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000679 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000680}
681
682
683static int
Tim Peterscba30e22003-02-01 06:24:36 +0000684get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000685{
686 PyObject *value, *mv;
687 long c_value;
688 char s[30];
689 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000690
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000691 if (!( mv = PyDict_GetItem(self->memo, id))) {
692 PyErr_SetObject(PyExc_KeyError, id);
693 return -1;
694 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000695
Tim Peterscba30e22003-02-01 06:24:36 +0000696 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000697 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000698
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000699 if (!( PyInt_Check(value))) {
700 PyErr_SetString(PicklingError, "no int where int expected in memo");
701 return -1;
702 }
703 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000704
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000705 if (!self->bin) {
706 s[0] = GET;
707 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
708 len = strlen(s);
709 }
710 else if (Pdata_Check(self->file)) {
711 if (write_other(self, NULL, 0) < 0) return -1;
712 PDATA_APPEND(self->file, mv, -1);
713 return 0;
714 }
715 else {
716 if (c_value < 256) {
717 s[0] = BINGET;
718 s[1] = (int)(c_value & 0xff);
719 len = 2;
720 }
721 else {
722 s[0] = LONG_BINGET;
723 s[1] = (int)(c_value & 0xff);
724 s[2] = (int)((c_value >> 8) & 0xff);
725 s[3] = (int)((c_value >> 16) & 0xff);
726 s[4] = (int)((c_value >> 24) & 0xff);
727 len = 5;
728 }
729 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000730
Tim Peters0bc93f52003-02-02 18:29:33 +0000731 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000732 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000733
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000734 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000735}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000736
Guido van Rossum60456fd1997-04-09 17:36:32 +0000737
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000738static int
Tim Peterscba30e22003-02-01 06:24:36 +0000739put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000740{
Tim Peterscba30e22003-02-01 06:24:36 +0000741 if (ob->ob_refcnt < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000742 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000743
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000744 return put2(self, ob);
745}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000746
Guido van Rossum053b8df1998-11-25 16:18:00 +0000747
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000748static int
Tim Peterscba30e22003-02-01 06:24:36 +0000749put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000750{
751 char c_str[30];
752 int p;
753 size_t len;
754 int res = -1;
755 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000756
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000757 if (self->fast)
758 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000759
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000760 if ((p = PyDict_Size(self->memo)) < 0)
761 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000762
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000763 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000764 /* XXX Why?
765 * XXX And does "positive" really mean non-negative?
766 * XXX pickle.py starts with PUT index 0, not 1. This makes for
767 * XXX gratuitous differences between the pickling modules.
768 */
Tim Peterscba30e22003-02-01 06:24:36 +0000769 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000770
Tim Peterscba30e22003-02-01 06:24:36 +0000771 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000772 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000773
Tim Peterscba30e22003-02-01 06:24:36 +0000774 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000775 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000776
Tim Peterscba30e22003-02-01 06:24:36 +0000777 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000778 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000780 PyTuple_SET_ITEM(t, 0, memo_len);
781 Py_INCREF(memo_len);
782 PyTuple_SET_ITEM(t, 1, ob);
783 Py_INCREF(ob);
784
785 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
786 goto finally;
787
788 if (!self->bin) {
789 c_str[0] = PUT;
790 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
791 len = strlen(c_str);
792 }
793 else if (Pdata_Check(self->file)) {
794 if (write_other(self, NULL, 0) < 0) return -1;
795 PDATA_APPEND(self->file, memo_len, -1);
796 res=0; /* Job well done ;) */
797 goto finally;
798 }
799 else {
800 if (p >= 256) {
801 c_str[0] = LONG_BINPUT;
802 c_str[1] = (int)(p & 0xff);
803 c_str[2] = (int)((p >> 8) & 0xff);
804 c_str[3] = (int)((p >> 16) & 0xff);
805 c_str[4] = (int)((p >> 24) & 0xff);
806 len = 5;
807 }
808 else {
809 c_str[0] = BINPUT;
810 c_str[1] = p;
811 len = 2;
812 }
813 }
814
Tim Peters0bc93f52003-02-02 18:29:33 +0000815 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000816 goto finally;
817
818 res = 0;
819
820 finally:
821 Py_XDECREF(py_ob_id);
822 Py_XDECREF(memo_len);
823 Py_XDECREF(t);
824
825 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000826}
827
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000828static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000829whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000830{
831 int i, j;
832 PyObject *module = 0, *modules_dict = 0,
833 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000834
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000835 module = PyObject_GetAttrString(global, "__module__");
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000836 if (module)
837 return module;
838 if (PyErr_ExceptionMatches(PyExc_AttributeError))
839 PyErr_Clear();
840 else
841 return NULL;
Guido van Rossum45188231997-09-28 05:38:51 +0000842
Tim Peterscba30e22003-02-01 06:24:36 +0000843 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000844 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000845
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000846 i = 0;
847 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000848
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000849 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000850
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000851 global_name_attr = PyObject_GetAttr(module, global_name);
852 if (!global_name_attr) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +0000853 if (PyErr_ExceptionMatches(PyExc_AttributeError))
854 PyErr_Clear();
855 else
856 return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000857 continue;
858 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000859
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000860 if (global_name_attr != global) {
861 Py_DECREF(global_name_attr);
862 continue;
863 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000864
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000865 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000866
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000867 break;
868 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000869
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000870 /* The following implements the rule in pickle.py added in 1.5
871 that used __main__ if no module is found. I don't actually
872 like this rule. jlf
873 */
874 if (!j) {
875 j=1;
876 name=__main___str;
877 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000878
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000879 Py_INCREF(name);
880 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000881}
882
883
Guido van Rossum60456fd1997-04-09 17:36:32 +0000884static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000885fast_save_enter(Picklerobject *self, PyObject *obj)
886{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000887 /* if fast_container < 0, we're doing an error exit. */
888 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
889 PyObject *key = NULL;
890 if (self->fast_memo == NULL) {
891 self->fast_memo = PyDict_New();
892 if (self->fast_memo == NULL) {
893 self->fast_container = -1;
894 return 0;
895 }
896 }
897 key = PyLong_FromVoidPtr(obj);
898 if (key == NULL)
899 return 0;
900 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000901 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000902 PyErr_Format(PyExc_ValueError,
Tim Peters92c8bb32003-02-13 23:00:26 +0000903 "fast mode: can't pickle cyclic objects "
904 "including object type %s at %p",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000905 obj->ob_type->tp_name, obj);
906 self->fast_container = -1;
907 return 0;
908 }
909 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000910 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000911 self->fast_container = -1;
912 return 0;
913 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000914 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000915 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000916 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000917}
918
Tim Peterscba30e22003-02-01 06:24:36 +0000919int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000920fast_save_leave(Picklerobject *self, PyObject *obj)
921{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000922 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
923 PyObject *key = PyLong_FromVoidPtr(obj);
924 if (key == NULL)
925 return 0;
926 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000927 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000928 return 0;
929 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000930 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000931 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000932 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000933}
934
935static int
Tim Peterscba30e22003-02-01 06:24:36 +0000936save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000937{
938 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000939 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000940 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000941
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000942 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000943}
944
Guido van Rossum77f6a652002-04-03 22:41:51 +0000945static int
Tim Peterscba30e22003-02-01 06:24:36 +0000946save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000947{
Guido van Rossume2763392002-04-05 19:30:08 +0000948 static char *buf[2] = {FALSE, TRUE};
949 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +0000950 long l = PyInt_AS_LONG((PyIntObject *)args);
951
Tim Peters3c67d792003-02-02 17:59:11 +0000952 if (self->proto >= 2) {
953 char opcode = l ? NEWTRUE : NEWFALSE;
954 if (self->write_func(self, &opcode, 1) < 0)
955 return -1;
956 }
957 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +0000958 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +0000959 return 0;
960}
Tim Peters84e87f32001-03-17 04:50:51 +0000961
Guido van Rossum60456fd1997-04-09 17:36:32 +0000962static int
Tim Peterscba30e22003-02-01 06:24:36 +0000963save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000964{
965 char c_str[32];
966 long l = PyInt_AS_LONG((PyIntObject *)args);
967 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000968
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000969 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000970#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000971 || l > 0x7fffffffL
972 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000973#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000974 ) {
975 /* Text-mode pickle, or long too big to fit in the 4-byte
976 * signed BININT format: store as a string.
977 */
978 c_str[0] = INT;
979 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +0000980 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000981 return -1;
982 }
983 else {
984 /* Binary pickle and l fits in a signed 4-byte int. */
985 c_str[1] = (int)( l & 0xff);
986 c_str[2] = (int)((l >> 8) & 0xff);
987 c_str[3] = (int)((l >> 16) & 0xff);
988 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000989
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000990 if ((c_str[4] == 0) && (c_str[3] == 0)) {
991 if (c_str[2] == 0) {
992 c_str[0] = BININT1;
993 len = 2;
994 }
995 else {
996 c_str[0] = BININT2;
997 len = 3;
998 }
999 }
1000 else {
1001 c_str[0] = BININT;
1002 len = 5;
1003 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001004
Tim Peters0bc93f52003-02-02 18:29:33 +00001005 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001006 return -1;
1007 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001008
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001009 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001010}
1011
1012
1013static int
Tim Peterscba30e22003-02-01 06:24:36 +00001014save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001015{
Tim Petersee1a53c2003-02-02 02:57:53 +00001016 int size;
1017 int res = -1;
1018 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001019
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001020 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001021
Tim Petersee1a53c2003-02-02 02:57:53 +00001022 if (self->proto >= 2) {
1023 /* Linear-time pickling. */
1024 size_t nbits;
1025 size_t nbytes;
1026 unsigned char *pdata;
1027 char c_str[5];
1028 int i;
1029 int sign = _PyLong_Sign(args);
1030
1031 if (sign == 0) {
1032 /* It's 0 -- an empty bytestring. */
1033 c_str[0] = LONG1;
1034 c_str[1] = 0;
1035 i = self->write_func(self, c_str, 2);
1036 if (i < 0) goto finally;
1037 res = 0;
1038 goto finally;
1039 }
1040 nbits = _PyLong_NumBits(args);
1041 if (nbits == (size_t)-1 && PyErr_Occurred())
1042 goto finally;
1043 /* How many bytes do we need? There are nbits >> 3 full
1044 * bytes of data, and nbits & 7 leftover bits. If there
1045 * are any leftover bits, then we clearly need another
1046 * byte. Wnat's not so obvious is that we *probably*
1047 * need another byte even if there aren't any leftovers:
1048 * the most-significant bit of the most-significant byte
1049 * acts like a sign bit, and it's usually got a sense
1050 * opposite of the one we need. The exception is longs
1051 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1052 * its own 256's-complement, so has the right sign bit
1053 * even without the extra byte. That's a pain to check
1054 * for in advance, though, so we always grab an extra
1055 * byte at the start, and cut it back later if possible.
1056 */
1057 nbytes = (nbits >> 3) + 1;
1058 if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) {
1059 PyErr_SetString(PyExc_OverflowError, "long too large "
1060 "to pickle");
1061 goto finally;
1062 }
1063 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1064 if (repr == NULL) goto finally;
1065 pdata = (unsigned char *)PyString_AS_STRING(repr);
1066 i = _PyLong_AsByteArray((PyLongObject *)args,
1067 pdata, nbytes,
1068 1 /* little endian */, 1 /* signed */);
1069 if (i < 0) goto finally;
1070 /* If the long is negative, this may be a byte more than
1071 * needed. This is so iff the MSB is all redundant sign
1072 * bits.
1073 */
1074 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1075 (pdata[nbytes - 2] & 0x80) != 0)
1076 --nbytes;
1077
1078 if (nbytes < 256) {
1079 c_str[0] = LONG1;
1080 c_str[1] = (char)nbytes;
1081 size = 2;
1082 }
1083 else {
1084 c_str[0] = LONG4;
1085 size = (int)nbytes;
1086 for (i = 1; i < 5; i++) {
1087 c_str[i] = (char)(size & 0xff);
1088 size >>= 8;
1089 }
1090 size = 5;
1091 }
1092 i = self->write_func(self, c_str, size);
1093 if (i < 0) goto finally;
1094 i = self->write_func(self, (char *)pdata, (int)nbytes);
1095 if (i < 0) goto finally;
1096 res = 0;
1097 goto finally;
1098 }
1099
1100 /* proto < 2: write the repr and newline. This is quadratic-time
1101 * (in the number of digits), in both directions.
1102 */
Tim Peterscba30e22003-02-01 06:24:36 +00001103 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001104 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001105
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001106 if ((size = PyString_Size(repr)) < 0)
1107 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001108
Tim Peters0bc93f52003-02-02 18:29:33 +00001109 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001110 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001111
Tim Peters0bc93f52003-02-02 18:29:33 +00001112 if (self->write_func(self,
1113 PyString_AS_STRING((PyStringObject *)repr),
1114 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001115 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001116
Tim Peters0bc93f52003-02-02 18:29:33 +00001117 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001118 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001119
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001120 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001121
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001122 finally:
1123 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001124 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001125}
1126
1127
1128static int
Tim Peterscba30e22003-02-01 06:24:36 +00001129save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001130{
1131 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001132
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001133 if (self->bin) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001134 char str[9];
Tim Peters9905b942003-03-20 20:53:32 +00001135 str[0] = BINFLOAT;
1136 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001137 return -1;
Tim Peters0bc93f52003-02-02 18:29:33 +00001138 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001139 return -1;
1140 }
1141 else {
1142 char c_str[250];
1143 c_str[0] = FLOAT;
1144 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001145
Tim Peters0bc93f52003-02-02 18:29:33 +00001146 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001147 return -1;
1148 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001149
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001150 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001151}
1152
1153
1154static int
Tim Peterscba30e22003-02-01 06:24:36 +00001155save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001156{
1157 int size, len;
1158 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001159
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001160 if ((size = PyString_Size(args)) < 0)
1161 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001162
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001163 if (!self->bin) {
1164 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001165
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001166 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001167
Tim Peterscba30e22003-02-01 06:24:36 +00001168 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001169 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001170
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001171 if ((len = PyString_Size(repr)) < 0)
1172 goto err;
1173 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001174
Tim Peters0bc93f52003-02-02 18:29:33 +00001175 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001176 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001177
Tim Peters0bc93f52003-02-02 18:29:33 +00001178 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001179 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001180
Tim Peters0bc93f52003-02-02 18:29:33 +00001181 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001182 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001184 Py_XDECREF(repr);
1185 }
1186 else {
1187 int i;
1188 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001189
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001190 if ((size = PyString_Size(args)) < 0)
1191 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001193 if (size < 256) {
1194 c_str[0] = SHORT_BINSTRING;
1195 c_str[1] = size;
1196 len = 2;
1197 }
1198 else {
1199 c_str[0] = BINSTRING;
1200 for (i = 1; i < 5; i++)
1201 c_str[i] = (int)(size >> ((i - 1) * 8));
1202 len = 5;
1203 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001204
Tim Peters0bc93f52003-02-02 18:29:33 +00001205 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001206 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001207
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001208 if (size > 128 && Pdata_Check(self->file)) {
1209 if (write_other(self, NULL, 0) < 0) return -1;
1210 PDATA_APPEND(self->file, args, -1);
1211 }
1212 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001213 if (self->write_func(self,
1214 PyString_AS_STRING(
1215 (PyStringObject *)args),
1216 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001217 return -1;
1218 }
1219 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001220
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001221 if (doput)
1222 if (put(self, args) < 0)
1223 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001224
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001225 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001226
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001227 err:
1228 Py_XDECREF(repr);
1229 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001230}
1231
1232
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001233#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001234/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1235 backslash and newline characters to \uXXXX escapes. */
1236static PyObject *
1237modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1238{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001239 PyObject *repr;
1240 char *p;
1241 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001242
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001243 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001244
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001245 repr = PyString_FromStringAndSize(NULL, 6 * size);
1246 if (repr == NULL)
1247 return NULL;
1248 if (size == 0)
1249 return repr;
1250
1251 p = q = PyString_AS_STRING(repr);
1252 while (size-- > 0) {
1253 Py_UNICODE ch = *s++;
1254 /* Map 16-bit characters to '\uxxxx' */
1255 if (ch >= 256 || ch == '\\' || ch == '\n') {
1256 *p++ = '\\';
1257 *p++ = 'u';
1258 *p++ = hexdigit[(ch >> 12) & 0xf];
1259 *p++ = hexdigit[(ch >> 8) & 0xf];
1260 *p++ = hexdigit[(ch >> 4) & 0xf];
1261 *p++ = hexdigit[ch & 15];
1262 }
1263 /* Copy everything else as-is */
1264 else
1265 *p++ = (char) ch;
1266 }
1267 *p = '\0';
Tim Peters5de98422002-04-27 18:44:32 +00001268 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001269 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001270}
1271
1272
Guido van Rossum60456fd1997-04-09 17:36:32 +00001273static int
Tim Peterscba30e22003-02-01 06:24:36 +00001274save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001275{
1276 int size, len;
1277 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001278
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001279 if (!PyUnicode_Check(args))
1280 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001281
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001282 if (!self->bin) {
1283 char *repr_str;
1284 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001285
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001286 repr = modified_EncodeRawUnicodeEscape(
1287 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001288 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001289 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001290
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001291 if ((len = PyString_Size(repr)) < 0)
1292 goto err;
1293 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001294
Tim Peters0bc93f52003-02-02 18:29:33 +00001295 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001296 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001297
Tim Peters0bc93f52003-02-02 18:29:33 +00001298 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001299 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001300
Tim Peters0bc93f52003-02-02 18:29:33 +00001301 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001302 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001303
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001304 Py_XDECREF(repr);
1305 }
1306 else {
1307 int i;
1308 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001309
Tim Peterscba30e22003-02-01 06:24:36 +00001310 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001311 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001312
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001313 if ((size = PyString_Size(repr)) < 0)
1314 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001315
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001316 c_str[0] = BINUNICODE;
1317 for (i = 1; i < 5; i++)
1318 c_str[i] = (int)(size >> ((i - 1) * 8));
1319 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001320
Tim Peters0bc93f52003-02-02 18:29:33 +00001321 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001322 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001323
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001324 if (size > 128 && Pdata_Check(self->file)) {
1325 if (write_other(self, NULL, 0) < 0)
1326 goto err;
1327 PDATA_APPEND(self->file, repr, -1);
1328 }
1329 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001330 if (self->write_func(self, PyString_AS_STRING(repr),
1331 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001332 goto err;
1333 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001334
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001335 Py_DECREF(repr);
1336 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001337
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001338 if (doput)
1339 if (put(self, args) < 0)
1340 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001341
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001342 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001343
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001344 err:
1345 Py_XDECREF(repr);
1346 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001347}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001348#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001349
Tim Peters1d63c9f2003-02-02 20:29:39 +00001350/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1351static int
Tim Peters67920142003-02-05 03:46:17 +00001352store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001353{
1354 int i;
1355 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001356
Tim Peters1d63c9f2003-02-02 20:29:39 +00001357 assert(PyTuple_Size(t) == len);
1358
1359 for (i = 0; i < len; i++) {
1360 PyObject *element = PyTuple_GET_ITEM(t, i);
1361
1362 if (element == NULL)
1363 goto finally;
1364 if (save(self, element, 0) < 0)
1365 goto finally;
1366 }
1367 res = 0;
1368
1369 finally:
1370 return res;
1371}
1372
1373/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1374 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001375 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001376 * (a tuple can be reached from itself), and that requires some subtle
1377 * magic so that it works in all cases. IOW, this is a long routine.
1378 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001379static int
Tim Peterscba30e22003-02-01 06:24:36 +00001380save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001381{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001382 PyObject *py_tuple_id = NULL;
1383 int len, i;
1384 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001385
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001386 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001387 static char pop = POP;
1388 static char pop_mark = POP_MARK;
1389 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001390
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001391 if ((len = PyTuple_Size(args)) < 0)
1392 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001393
Tim Peters1d63c9f2003-02-02 20:29:39 +00001394 if (len == 0) {
1395 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001396
Tim Peters1d63c9f2003-02-02 20:29:39 +00001397 if (self->proto) {
1398 c_str[0] = EMPTY_TUPLE;
1399 len = 1;
1400 }
1401 else {
1402 c_str[0] = MARK;
1403 c_str[1] = TUPLE;
1404 len = 2;
1405 }
1406 if (self->write_func(self, c_str, len) >= 0)
1407 res = 0;
1408 /* Don't memoize an empty tuple. */
1409 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001410 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001411
Tim Peters1d63c9f2003-02-02 20:29:39 +00001412 /* A non-empty tuple. */
1413
1414 /* id(tuple) isn't in the memo now. If it shows up there after
1415 * saving the tuple elements, the tuple must be recursive, in
1416 * which case we'll pop everything we put on the stack, and fetch
1417 * its value from the memo.
1418 */
1419 py_tuple_id = PyLong_FromVoidPtr(args);
1420 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001421 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001422
Tim Peters1d63c9f2003-02-02 20:29:39 +00001423 if (len <= 3 && self->proto >= 2) {
1424 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001425 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001426 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001427 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001428 /* pop the len elements */
1429 for (i = 0; i < len; ++i)
1430 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001431 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001432 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001433 if (get(self, py_tuple_id) < 0)
1434 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001435 res = 0;
1436 goto finally;
1437 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001438 /* Not recursive. */
1439 if (self->write_func(self, len2opcode + len, 1) < 0)
1440 goto finally;
1441 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001442 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001443
Tim Peters1d63c9f2003-02-02 20:29:39 +00001444 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1445 * Generate MARK elt1 elt2 ... TUPLE
1446 */
1447 if (self->write_func(self, &MARKv, 1) < 0)
1448 goto finally;
1449
Tim Peters67920142003-02-05 03:46:17 +00001450 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001451 goto finally;
1452
1453 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1454 /* pop the stack stuff we pushed */
1455 if (self->bin) {
1456 if (self->write_func(self, &pop_mark, 1) < 0)
1457 goto finally;
1458 }
1459 else {
1460 /* Note that we pop one more than len, to remove
1461 * the MARK too.
1462 */
1463 for (i = 0; i <= len; i++)
1464 if (self->write_func(self, &pop, 1) < 0)
1465 goto finally;
1466 }
1467 /* fetch from memo */
1468 if (get(self, py_tuple_id) >= 0)
1469 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001470 goto finally;
1471 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001472
Tim Peters1d63c9f2003-02-02 20:29:39 +00001473 /* Not recursive. */
1474 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001475 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001476
Tim Peters1d63c9f2003-02-02 20:29:39 +00001477 memoize:
1478 if (put(self, args) >= 0)
1479 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001480
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001481 finally:
1482 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001483 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001484}
1485
Tim Peters1092d642003-02-11 21:06:20 +00001486/* iter is an iterator giving items, and we batch up chunks of
1487 * MARK item item ... item APPENDS
1488 * opcode sequences. Calling code should have arranged to first create an
1489 * empty list, or list-like object, for the APPENDS to operate on.
1490 * Returns 0 on success, <0 on error.
1491 */
1492static int
1493batch_list(Picklerobject *self, PyObject *iter)
1494{
1495 PyObject *obj;
1496 PyObject *slice[BATCHSIZE];
1497 int i, n;
1498
1499 static char append = APPEND;
1500 static char appends = APPENDS;
1501
1502 assert(iter != NULL);
1503
1504 if (self->proto == 0) {
1505 /* APPENDS isn't available; do one at a time. */
1506 for (;;) {
1507 obj = PyIter_Next(iter);
1508 if (obj == NULL) {
1509 if (PyErr_Occurred())
1510 return -1;
1511 break;
1512 }
1513 i = save(self, obj, 0);
1514 Py_DECREF(obj);
1515 if (i < 0)
1516 return -1;
1517 if (self->write_func(self, &append, 1) < 0)
1518 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001519 }
1520 return 0;
1521 }
1522
1523 /* proto > 0: write in batches of BATCHSIZE. */
1524 do {
1525 /* Get next group of (no more than) BATCHSIZE elements. */
1526 for (n = 0; n < BATCHSIZE; ++n) {
1527 obj = PyIter_Next(iter);
1528 if (obj == NULL) {
1529 if (PyErr_Occurred())
1530 goto BatchFailed;
1531 break;
1532 }
1533 slice[n] = obj;
1534 }
1535
1536 if (n > 1) {
1537 /* Pump out MARK, slice[0:n], APPENDS. */
1538 if (self->write_func(self, &MARKv, 1) < 0)
1539 goto BatchFailed;
1540 for (i = 0; i < n; ++i) {
1541 if (save(self, slice[i], 0) < 0)
1542 goto BatchFailed;
1543 }
1544 if (self->write_func(self, &appends, 1) < 0)
1545 goto BatchFailed;
1546 }
1547 else if (n == 1) {
1548 if (save(self, slice[0], 0) < 0)
1549 goto BatchFailed;
1550 if (self->write_func(self, &append, 1) < 0)
1551 goto BatchFailed;
1552 }
1553
1554 for (i = 0; i < n; ++i) {
1555 Py_DECREF(slice[i]);
1556 }
Tim Peters90975f12003-02-12 05:28:58 +00001557 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001558 return 0;
1559
1560BatchFailed:
1561 while (--n >= 0) {
1562 Py_DECREF(slice[n]);
1563 }
1564 return -1;
1565}
1566
Guido van Rossum60456fd1997-04-09 17:36:32 +00001567static int
Tim Peterscba30e22003-02-01 06:24:36 +00001568save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001569{
Tim Peters1092d642003-02-11 21:06:20 +00001570 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001571 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001572 int len;
1573 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001574
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001575 if (self->fast && !fast_save_enter(self, args))
1576 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001577
Tim Peters1092d642003-02-11 21:06:20 +00001578 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001579 if (self->bin) {
1580 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001581 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001582 }
1583 else {
1584 s[0] = MARK;
1585 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001586 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001587 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001588
Tim Peters1092d642003-02-11 21:06:20 +00001589 if (self->write_func(self, s, len) < 0)
1590 goto finally;
1591
1592 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001593 if ((len = PyList_Size(args)) < 0)
1594 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001595
Tim Peters1092d642003-02-11 21:06:20 +00001596 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001597 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001598 if (put(self, args) >= 0)
1599 res = 0;
1600 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001601 }
Tim Peters90975f12003-02-12 05:28:58 +00001602 if (put2(self, args) < 0)
1603 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001604
Tim Peters1092d642003-02-11 21:06:20 +00001605 /* Materialize the list elements. */
1606 iter = PyObject_GetIter(args);
1607 if (iter == NULL)
1608 goto finally;
1609 res = batch_list(self, iter);
1610 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001611
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001612 finally:
1613 if (self->fast && !fast_save_leave(self, args))
1614 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001615
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001616 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001617}
1618
1619
Tim Peters42f08ac2003-02-11 22:43:24 +00001620/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1621 * MARK key value ... key value SETITEMS
1622 * opcode sequences. Calling code should have arranged to first create an
1623 * empty dict, or dict-like object, for the SETITEMS to operate on.
1624 * Returns 0 on success, <0 on error.
1625 *
1626 * This is very much like batch_list(). The difference between saving
1627 * elements directly, and picking apart two-tuples, is so long-winded at
1628 * the C level, though, that attempts to combine these routines were too
1629 * ugly to bear.
1630 */
1631static int
1632batch_dict(Picklerobject *self, PyObject *iter)
1633{
1634 PyObject *p;
1635 PyObject *slice[BATCHSIZE];
1636 int i, n;
1637
1638 static char setitem = SETITEM;
1639 static char setitems = SETITEMS;
1640
1641 assert(iter != NULL);
1642
1643 if (self->proto == 0) {
1644 /* SETITEMS isn't available; do one at a time. */
1645 for (;;) {
1646 p = PyIter_Next(iter);
1647 if (p == NULL) {
1648 if (PyErr_Occurred())
1649 return -1;
1650 break;
1651 }
1652 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1653 PyErr_SetString(PyExc_TypeError, "dict items "
1654 "iterator must return 2-tuples");
1655 return -1;
1656 }
1657 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1658 if (i >= 0)
1659 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1660 Py_DECREF(p);
1661 if (i < 0)
1662 return -1;
1663 if (self->write_func(self, &setitem, 1) < 0)
1664 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001665 }
1666 return 0;
1667 }
1668
1669 /* proto > 0: write in batches of BATCHSIZE. */
1670 do {
1671 /* Get next group of (no more than) BATCHSIZE elements. */
1672 for (n = 0; n < BATCHSIZE; ++n) {
1673 p = PyIter_Next(iter);
1674 if (p == NULL) {
1675 if (PyErr_Occurred())
1676 goto BatchFailed;
1677 break;
1678 }
1679 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1680 PyErr_SetString(PyExc_TypeError, "dict items "
1681 "iterator must return 2-tuples");
1682 goto BatchFailed;
1683 }
1684 slice[n] = p;
1685 }
1686
1687 if (n > 1) {
1688 /* Pump out MARK, slice[0:n], SETITEMS. */
1689 if (self->write_func(self, &MARKv, 1) < 0)
1690 goto BatchFailed;
1691 for (i = 0; i < n; ++i) {
1692 p = slice[i];
1693 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1694 goto BatchFailed;
1695 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1696 goto BatchFailed;
1697 }
1698 if (self->write_func(self, &setitems, 1) < 0)
1699 goto BatchFailed;
1700 }
1701 else if (n == 1) {
1702 p = slice[0];
1703 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1704 goto BatchFailed;
1705 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1706 goto BatchFailed;
1707 if (self->write_func(self, &setitem, 1) < 0)
1708 goto BatchFailed;
1709 }
1710
1711 for (i = 0; i < n; ++i) {
1712 Py_DECREF(slice[i]);
1713 }
Tim Peters90975f12003-02-12 05:28:58 +00001714 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001715 return 0;
1716
1717BatchFailed:
1718 while (--n >= 0) {
1719 Py_DECREF(slice[n]);
1720 }
1721 return -1;
1722}
1723
Guido van Rossum60456fd1997-04-09 17:36:32 +00001724static int
Tim Peterscba30e22003-02-01 06:24:36 +00001725save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001726{
Tim Peters42f08ac2003-02-11 22:43:24 +00001727 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001728 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001729 int len;
1730 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001731
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001732 if (self->fast && !fast_save_enter(self, args))
1733 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001734
Tim Peters42f08ac2003-02-11 22:43:24 +00001735 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001736 if (self->bin) {
1737 s[0] = EMPTY_DICT;
1738 len = 1;
1739 }
1740 else {
1741 s[0] = MARK;
1742 s[1] = DICT;
1743 len = 2;
1744 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001745
Tim Peters0bc93f52003-02-02 18:29:33 +00001746 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001747 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001748
Tim Peters42f08ac2003-02-11 22:43:24 +00001749 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001750 if ((len = PyDict_Size(args)) < 0)
1751 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001752
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001753 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001754 if (put(self, args) >= 0)
1755 res = 0;
1756 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001757 }
Tim Peters90975f12003-02-12 05:28:58 +00001758 if (put2(self, args) < 0)
1759 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001760
Tim Peters42f08ac2003-02-11 22:43:24 +00001761 /* Materialize the dict items. */
1762 iter = PyObject_CallMethod(args, "iteritems", "()");
1763 if (iter == NULL)
1764 goto finally;
1765 res = batch_dict(self, iter);
1766 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001767
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001768 finally:
1769 if (self->fast && !fast_save_leave(self, args))
1770 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001771
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001772 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001773}
1774
1775
Tim Peters84e87f32001-03-17 04:50:51 +00001776static int
Tim Peterscba30e22003-02-01 06:24:36 +00001777save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001778{
1779 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1780 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1781 char *module_str, *name_str;
1782 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001783
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001784 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001785
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001786 if (self->fast && !fast_save_enter(self, args))
1787 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001788
Tim Peters0bc93f52003-02-02 18:29:33 +00001789 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001790 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001791
Tim Peterscba30e22003-02-01 06:24:36 +00001792 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001793 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001794
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001795 if (self->bin) {
1796 if (save(self, class, 0) < 0)
1797 goto finally;
1798 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001799
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001800 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1801 PyObject *element = 0;
1802 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001803
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001804 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001805 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001806 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001807
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001808 if ((len = PyObject_Size(class_args)) < 0)
1809 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001810
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001811 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001812 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001813 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001814
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001815 if (save(self, element, 0) < 0) {
1816 Py_DECREF(element);
1817 goto finally;
1818 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001819
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001820 Py_DECREF(element);
1821 }
1822 }
1823 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001824 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1825 PyErr_Clear();
1826 else
1827 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001828 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001829
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001830 if (!self->bin) {
1831 if (!( name = ((PyClassObject *)class)->cl_name )) {
1832 PyErr_SetString(PicklingError, "class has no name");
1833 goto finally;
1834 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001835
Tim Peterscba30e22003-02-01 06:24:36 +00001836 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001837 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001838
Tim Peters84e87f32001-03-17 04:50:51 +00001839
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001840 if ((module_size = PyString_Size(module)) < 0 ||
1841 (name_size = PyString_Size(name)) < 0)
1842 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001843
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001844 module_str = PyString_AS_STRING((PyStringObject *)module);
1845 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001846
Tim Peters0bc93f52003-02-02 18:29:33 +00001847 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001848 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001849
Tim Peters0bc93f52003-02-02 18:29:33 +00001850 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001851 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001852
Tim Peters0bc93f52003-02-02 18:29:33 +00001853 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001854 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001855
Tim Peters0bc93f52003-02-02 18:29:33 +00001856 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001857 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001858
Tim Peters0bc93f52003-02-02 18:29:33 +00001859 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001860 goto finally;
1861 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001862 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001863 goto finally;
1864 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001865
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001866 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1867 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00001868 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001869 goto finally;
1870 }
1871 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001872 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1873 PyErr_Clear();
1874 else
1875 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001877 if (!( state = PyObject_GetAttr(args, __dict___str))) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00001878 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1879 PyErr_Clear();
1880 else
1881 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001882 res = 0;
1883 goto finally;
1884 }
1885 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001886
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001887 if (!PyDict_Check(state)) {
1888 if (put2(self, args) < 0)
1889 goto finally;
1890 }
1891 else {
1892 if (put(self, args) < 0)
1893 goto finally;
1894 }
Tim Peters84e87f32001-03-17 04:50:51 +00001895
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001896 if (save(self, state, 0) < 0)
1897 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001898
Tim Peters0bc93f52003-02-02 18:29:33 +00001899 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001900 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001901
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001902 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001903
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001904 finally:
1905 if (self->fast && !fast_save_leave(self, args))
1906 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001907
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001908 Py_XDECREF(module);
1909 Py_XDECREF(class);
1910 Py_XDECREF(state);
1911 Py_XDECREF(getinitargs_func);
1912 Py_XDECREF(getstate_func);
1913 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001914
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001915 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001916}
1917
1918
Guido van Rossum60456fd1997-04-09 17:36:32 +00001919static int
Tim Peterscba30e22003-02-01 06:24:36 +00001920save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001921{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00001922 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001923 char *name_str, *module_str;
1924 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001925
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001926 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001927
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001928 if (name) {
1929 global_name = name;
1930 Py_INCREF(global_name);
1931 }
1932 else {
Tim Peterscba30e22003-02-01 06:24:36 +00001933 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001934 goto finally;
1935 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001936
Tim Peterscba30e22003-02-01 06:24:36 +00001937 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001938 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001939
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001940 if ((module_size = PyString_Size(module)) < 0 ||
1941 (name_size = PyString_Size(global_name)) < 0)
1942 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001943
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001944 module_str = PyString_AS_STRING((PyStringObject *)module);
1945 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001946
Guido van Rossum75bfd052002-12-24 18:10:07 +00001947 /* XXX This can be doing a relative import. Clearly it shouldn't,
1948 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001949 mod = PyImport_ImportModule(module_str);
1950 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001951 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001952 "Can't pickle %s: import of module %s "
1953 "failed",
1954 "OS", args, module);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001955 goto finally;
1956 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001957 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001958 if (klass == NULL) {
1959 cPickle_ErrFormat(PicklingError,
Tim Petersb9ce7cd2003-02-18 20:50:45 +00001960 "Can't pickle %s: attribute lookup %s.%s "
1961 "failed",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001962 "OSS", args, module, global_name);
1963 goto finally;
1964 }
1965 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001966 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001967 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00001968 "Can't pickle %s: it's not the same object "
1969 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001970 "OSS", args, module, global_name);
1971 goto finally;
1972 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00001973 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00001974
Tim Peters731098b2003-02-04 20:56:09 +00001975 if (self->proto >= 2) {
1976 /* See whether this is in the extension registry, and if
1977 * so generate an EXT opcode.
1978 */
1979 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00001980 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00001981 char c_str[5];
1982 int n;
1983
1984 PyTuple_SET_ITEM(two_tuple, 0, module);
1985 PyTuple_SET_ITEM(two_tuple, 1, global_name);
1986 py_code = PyDict_GetItem(extension_registry, two_tuple);
1987 if (py_code == NULL)
1988 goto gen_global; /* not registered */
1989
1990 /* Verify py_code has the right type and value. */
1991 if (!PyInt_Check(py_code)) {
1992 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00001993 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00001994 "OO", args, py_code);
1995 goto finally;
1996 }
1997 code = PyInt_AS_LONG(py_code);
1998 if (code <= 0 || code > 0x7fffffffL) {
1999 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2000 "extension code %ld is out of range",
2001 "Ol", args, code);
2002 goto finally;
2003 }
2004
2005 /* Generate an EXT opcode. */
2006 if (code <= 0xff) {
2007 c_str[0] = EXT1;
2008 c_str[1] = (char)code;
2009 n = 2;
2010 }
2011 else if (code <= 0xffff) {
2012 c_str[0] = EXT2;
2013 c_str[1] = (char)(code & 0xff);
2014 c_str[2] = (char)((code >> 8) & 0xff);
2015 n = 3;
2016 }
2017 else {
2018 c_str[0] = EXT4;
2019 c_str[1] = (char)(code & 0xff);
2020 c_str[2] = (char)((code >> 8) & 0xff);
2021 c_str[3] = (char)((code >> 16) & 0xff);
2022 c_str[4] = (char)((code >> 24) & 0xff);
2023 n = 5;
2024 }
2025
2026 if (self->write_func(self, c_str, n) >= 0)
2027 res = 0;
2028 goto finally; /* and don't memoize */
2029 }
2030
2031 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002032 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002033 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002034
Tim Peters0bc93f52003-02-02 18:29:33 +00002035 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002036 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002037
Tim Peters0bc93f52003-02-02 18:29:33 +00002038 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002039 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002040
Tim Peters0bc93f52003-02-02 18:29:33 +00002041 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002042 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002043
Tim Peters0bc93f52003-02-02 18:29:33 +00002044 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002045 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002046
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002047 if (put(self, args) < 0)
2048 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002049
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002050 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002051
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002052 finally:
2053 Py_XDECREF(module);
2054 Py_XDECREF(global_name);
2055 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002056
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002057 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002058}
2059
Guido van Rossum60456fd1997-04-09 17:36:32 +00002060static int
Tim Peterscba30e22003-02-01 06:24:36 +00002061save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002062{
2063 PyObject *pid = 0;
2064 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002065
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002066 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002067
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002068 Py_INCREF(args);
2069 ARG_TUP(self, args);
2070 if (self->arg) {
2071 pid = PyObject_Call(f, self->arg, NULL);
2072 FREE_ARG_TUP(self);
2073 }
2074 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002075
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002076 if (pid != Py_None) {
2077 if (!self->bin) {
2078 if (!PyString_Check(pid)) {
2079 PyErr_SetString(PicklingError,
2080 "persistent id must be string");
2081 goto finally;
2082 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002083
Tim Peters0bc93f52003-02-02 18:29:33 +00002084 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002085 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002086
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002087 if ((size = PyString_Size(pid)) < 0)
2088 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002089
Tim Peters0bc93f52003-02-02 18:29:33 +00002090 if (self->write_func(self,
2091 PyString_AS_STRING(
2092 (PyStringObject *)pid),
2093 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002094 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002095
Tim Peters0bc93f52003-02-02 18:29:33 +00002096 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002097 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002098
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002099 res = 1;
2100 goto finally;
2101 }
2102 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002103 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002104 res = -1;
2105 else
2106 res = 1;
2107 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002108
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002109 goto finally;
2110 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002111
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002112 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002113
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002114 finally:
2115 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002116
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002117 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002118}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002119
Tim Peters71fcda52003-02-14 23:05:28 +00002120/* We're saving ob, and args is the 2-thru-5 tuple returned by the
2121 * appropriate __reduce__ method for ob.
2122 */
Tim Peters84e87f32001-03-17 04:50:51 +00002123static int
Tim Peters71fcda52003-02-14 23:05:28 +00002124save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002125{
Tim Peters71fcda52003-02-14 23:05:28 +00002126 PyObject *callable;
2127 PyObject *argtup;
2128 PyObject *state = NULL;
2129 PyObject *listitems = NULL;
2130 PyObject *dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002131
Tim Peters71fcda52003-02-14 23:05:28 +00002132 int use_newobj = self->proto >= 2;
2133
2134 static char reduce = REDUCE;
2135 static char build = BUILD;
2136 static char newobj = NEWOBJ;
2137
2138 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2139 &callable,
2140 &argtup,
2141 &state,
2142 &listitems,
2143 &dictitems))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002144 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002145
Tim Peters71fcda52003-02-14 23:05:28 +00002146 if (state == Py_None)
2147 state = NULL;
2148 if (listitems == Py_None)
2149 listitems = NULL;
2150 if (dictitems == Py_None)
2151 dictitems = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002152
Tim Peters71fcda52003-02-14 23:05:28 +00002153 /* Protocol 2 special case: if callable's name is __newobj__, use
2154 * NEWOBJ. This consumes a lot of code.
2155 */
2156 if (use_newobj) {
2157 PyObject *temp = PyObject_GetAttr(callable, __name___str);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002158
Tim Peters71fcda52003-02-14 23:05:28 +00002159 if (temp == NULL) {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002160 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2161 PyErr_Clear();
2162 else
2163 return -1;
Tim Peters71fcda52003-02-14 23:05:28 +00002164 use_newobj = 0;
2165 }
2166 else {
2167 use_newobj = PyString_Check(temp) &&
2168 strcmp(PyString_AS_STRING(temp),
2169 "__newobj__") == 0;
2170 Py_DECREF(temp);
2171 }
2172 }
2173 if (use_newobj) {
2174 PyObject *cls;
2175 PyObject *newargtup;
2176 int n, i;
2177
2178 /* Sanity checks. */
2179 n = PyTuple_Size(argtup);
2180 if (n < 1) {
2181 PyErr_SetString(PicklingError, "__newobj__ arglist "
2182 "is empty");
2183 return -1;
2184 }
2185
2186 cls = PyTuple_GET_ITEM(argtup, 0);
2187 if (! PyObject_HasAttrString(cls, "__new__")) {
2188 PyErr_SetString(PicklingError, "args[0] from "
2189 "__newobj__ args has no __new__");
2190 return -1;
2191 }
2192
2193 /* XXX How could ob be NULL? */
2194 if (ob != NULL) {
2195 PyObject *ob_dot_class;
2196
2197 ob_dot_class = PyObject_GetAttr(ob, __class___str);
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002198 if (ob_dot_class == NULL) {
2199 if (PyErr_ExceptionMatches(
2200 PyExc_AttributeError))
2201 PyErr_Clear();
2202 else
2203 return -1;
2204 }
Tim Peters71fcda52003-02-14 23:05:28 +00002205 i = ob_dot_class != cls; /* true iff a problem */
2206 Py_XDECREF(ob_dot_class);
2207 if (i) {
2208 PyErr_SetString(PicklingError, "args[0] from "
2209 "__newobj__ args has the wrong class");
2210 return -1;
2211 }
2212 }
2213
2214 /* Save the class and its __new__ arguments. */
2215 if (save(self, cls, 0) < 0)
2216 return -1;
2217
2218 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2219 if (newargtup == NULL)
2220 return -1;
2221 for (i = 1; i < n; ++i) {
2222 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2223 Py_INCREF(temp);
2224 PyTuple_SET_ITEM(newargtup, i-1, temp);
2225 }
2226 i = save(self, newargtup, 0) < 0;
2227 Py_DECREF(newargtup);
2228 if (i < 0)
2229 return -1;
2230
2231 /* Add NEWOBJ opcode. */
2232 if (self->write_func(self, &newobj, 1) < 0)
2233 return -1;
2234 }
2235 else {
2236 /* Not using NEWOBJ. */
2237 if (save(self, callable, 0) < 0 ||
2238 save(self, argtup, 0) < 0 ||
2239 self->write_func(self, &reduce, 1) < 0)
2240 return -1;
2241 }
2242
2243 /* Memoize. */
2244 /* XXX How can ob be NULL? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002245 if (ob != NULL) {
2246 if (state && !PyDict_Check(state)) {
2247 if (put2(self, ob) < 0)
2248 return -1;
2249 }
Tim Peters71fcda52003-02-14 23:05:28 +00002250 else if (put(self, ob) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002251 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002252 }
Tim Peters84e87f32001-03-17 04:50:51 +00002253
Guido van Rossum60456fd1997-04-09 17:36:32 +00002254
Tim Peters71fcda52003-02-14 23:05:28 +00002255 if (listitems && batch_list(self, listitems) < 0)
2256 return -1;
2257
2258 if (dictitems && batch_dict(self, dictitems) < 0)
2259 return -1;
2260
2261 if (state) {
2262 if (save(self, state, 0) < 0 ||
2263 self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002264 return -1;
2265 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002266
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002267 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002268}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002269
Guido van Rossum60456fd1997-04-09 17:36:32 +00002270static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002271save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002272{
2273 PyTypeObject *type;
Tim Peters71fcda52003-02-14 23:05:28 +00002274 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2275 PyObject *arg_tup;
2276 int res = -1;
2277 int tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002278
Martin v. Löwis5a395302002-08-04 08:20:23 +00002279 if (self->nesting++ > Py_GetRecursionLimit()){
2280 PyErr_SetString(PyExc_RuntimeError,
2281 "maximum recursion depth exceeded");
2282 goto finally;
2283 }
2284
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002285 if (!pers_save && self->pers_func) {
2286 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2287 res = tmp;
2288 goto finally;
2289 }
2290 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002291
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002292 if (args == Py_None) {
2293 res = save_none(self, args);
2294 goto finally;
2295 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002297 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002298
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002299 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002300 case 'b':
2301 if (args == Py_False || args == Py_True) {
2302 res = save_bool(self, args);
2303 goto finally;
2304 }
2305 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002306 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002307 if (type == &PyInt_Type) {
2308 res = save_int(self, args);
2309 goto finally;
2310 }
2311 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002312
Guido van Rossum60456fd1997-04-09 17:36:32 +00002313 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002314 if (type == &PyLong_Type) {
2315 res = save_long(self, args);
2316 goto finally;
2317 }
2318 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002319
Guido van Rossum60456fd1997-04-09 17:36:32 +00002320 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002321 if (type == &PyFloat_Type) {
2322 res = save_float(self, args);
2323 goto finally;
2324 }
2325 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002326
Guido van Rossum60456fd1997-04-09 17:36:32 +00002327 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002328 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2329 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002330 goto finally;
2331 }
2332 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002333
Guido van Rossum60456fd1997-04-09 17:36:32 +00002334 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002335 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2336 res = save_string(self, args, 0);
2337 goto finally;
2338 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002339
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002340#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002341 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002342 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2343 res = save_unicode(self, args, 0);
2344 goto finally;
2345 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002346#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002347 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002348
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002349 if (args->ob_refcnt > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002350 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002351 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002352
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002353 if (PyDict_GetItem(self->memo, py_ob_id)) {
2354 if (get(self, py_ob_id) < 0)
2355 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002356
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002357 res = 0;
2358 goto finally;
2359 }
2360 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002361
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002362 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002363 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002364 if (type == &PyString_Type) {
2365 res = save_string(self, args, 1);
2366 goto finally;
2367 }
2368 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002369
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002370#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002371 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002372 if (type == &PyUnicode_Type) {
2373 res = save_unicode(self, args, 1);
2374 goto finally;
2375 }
2376 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002377#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002378
Guido van Rossum60456fd1997-04-09 17:36:32 +00002379 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002380 if (type == &PyTuple_Type) {
2381 res = save_tuple(self, args);
2382 goto finally;
2383 }
2384 if (type == &PyType_Type) {
2385 res = save_global(self, args, NULL);
2386 goto finally;
2387 }
2388 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002389
Guido van Rossum60456fd1997-04-09 17:36:32 +00002390 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002391 if (type == &PyList_Type) {
2392 res = save_list(self, args);
2393 goto finally;
2394 }
2395 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002396
2397 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002398 if (type == &PyDict_Type) {
2399 res = save_dict(self, args);
2400 goto finally;
2401 }
2402 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002403
2404 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002405 if (type == &PyInstance_Type) {
2406 res = save_inst(self, args);
2407 goto finally;
2408 }
2409 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002410
2411 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002412 if (type == &PyClass_Type) {
2413 res = save_global(self, args, NULL);
2414 goto finally;
2415 }
2416 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002417
2418 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002419 if (type == &PyFunction_Type) {
2420 res = save_global(self, args, NULL);
Christian Tismer2460c622004-02-26 16:21:45 +00002421 if (res && PyErr_ExceptionMatches(PickleError)) {
2422 /* fall back to reduce */
2423 PyErr_Clear();
2424 break;
2425 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002426 goto finally;
2427 }
2428 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002429
2430 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002431 if (type == &PyCFunction_Type) {
2432 res = save_global(self, args, NULL);
2433 goto finally;
2434 }
2435 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002436
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002437 if (!pers_save && self->inst_pers_func) {
2438 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2439 res = tmp;
2440 goto finally;
2441 }
2442 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002443
Jeremy Hylton39c61162002-07-16 19:47:43 +00002444 if (PyType_IsSubtype(type, &PyType_Type)) {
2445 res = save_global(self, args, NULL);
2446 goto finally;
2447 }
2448
Guido van Rossumb289b872003-02-19 01:45:13 +00002449 /* Get a reduction callable, and call it. This may come from
2450 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2451 * or the object's __reduce__ method.
Tim Peters71fcda52003-02-14 23:05:28 +00002452 */
Tim Peters5aa3da62003-02-13 21:03:57 +00002453 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2454 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002455 Py_INCREF(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00002456 Py_INCREF(args);
2457 ARG_TUP(self, args);
2458 if (self->arg) {
2459 t = PyObject_Call(__reduce__, self->arg, NULL);
2460 FREE_ARG_TUP(self);
2461 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002462 }
2463 else {
Guido van Rossumb289b872003-02-19 01:45:13 +00002464 /* Check for a __reduce_ex__ method. */
2465 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2466 if (__reduce__ != NULL) {
2467 t = PyInt_FromLong(self->proto);
2468 if (t != NULL) {
2469 ARG_TUP(self, t);
2470 t = NULL;
2471 if (self->arg) {
2472 t = PyObject_Call(__reduce__,
2473 self->arg, NULL);
2474 FREE_ARG_TUP(self);
2475 }
2476 }
2477 }
2478 else {
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00002479 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2480 PyErr_Clear();
2481 else
2482 goto finally;
Guido van Rossumb289b872003-02-19 01:45:13 +00002483 /* Check for a __reduce__ method. */
2484 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2485 if (__reduce__ != NULL) {
2486 t = PyObject_Call(__reduce__,
2487 empty_tuple, NULL);
2488 }
2489 else {
2490 PyErr_SetObject(UnpickleableError, args);
2491 goto finally;
2492 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002493 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002494 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002495
Tim Peters71fcda52003-02-14 23:05:28 +00002496 if (t == NULL)
2497 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002498
Tim Peters71fcda52003-02-14 23:05:28 +00002499 if (PyString_Check(t)) {
2500 res = save_global(self, args, t);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002501 goto finally;
2502 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002503
Tim Peters71fcda52003-02-14 23:05:28 +00002504 if (! PyTuple_Check(t)) {
2505 cPickle_ErrFormat(PicklingError, "Value returned by "
2506 "%s must be string or tuple",
2507 "O", __reduce__);
2508 goto finally;
2509 }
2510
2511 size = PyTuple_Size(t);
2512 if (size < 2 || size > 5) {
2513 cPickle_ErrFormat(PicklingError, "tuple returned by "
2514 "%s must contain 2 through 5 elements",
2515 "O", __reduce__);
2516 goto finally;
2517 }
2518
2519 arg_tup = PyTuple_GET_ITEM(t, 1);
2520 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2521 cPickle_ErrFormat(PicklingError, "Second element of "
2522 "tuple returned by %s must be a tuple",
2523 "O", __reduce__);
2524 goto finally;
2525 }
2526
2527 res = save_reduce(self, t, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002528
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002529 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002530 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002531 Py_XDECREF(py_ob_id);
2532 Py_XDECREF(__reduce__);
2533 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002534
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002535 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002536}
2537
2538
2539static int
Tim Peterscba30e22003-02-01 06:24:36 +00002540dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002541{
2542 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002543
Tim Peters4190fb82003-02-02 16:09:05 +00002544 if (self->proto >= 2) {
2545 char bytes[2];
2546
2547 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002548 assert(self->proto >= 0 && self->proto < 256);
2549 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002550 if (self->write_func(self, bytes, 2) < 0)
2551 return -1;
2552 }
2553
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002554 if (save(self, args, 0) < 0)
2555 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002556
Tim Peters4190fb82003-02-02 16:09:05 +00002557 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002558 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002559
Tim Peters4190fb82003-02-02 16:09:05 +00002560 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002561 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002562
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002563 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002564}
2565
2566static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002567Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002568{
Tim Peterscba30e22003-02-01 06:24:36 +00002569 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002570 PyDict_Clear(self->memo);
2571 Py_INCREF(Py_None);
2572 return Py_None;
2573}
2574
2575static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002576Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002577{
2578 int l, i, rsize, ssize, clear=1, lm;
2579 long ik;
2580 PyObject *k, *r;
2581 char *s, *p, *have_get;
2582 Pdata *data;
2583
2584 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002585 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002586 return NULL;
2587
2588 /* Check to make sure we are based on a list */
2589 if (! Pdata_Check(self->file)) {
2590 PyErr_SetString(PicklingError,
2591 "Attempt to getvalue() a non-list-based pickler");
2592 return NULL;
2593 }
2594
2595 /* flush write buffer */
2596 if (write_other(self, NULL, 0) < 0) return NULL;
2597
2598 data=(Pdata*)self->file;
2599 l=data->length;
2600
2601 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002602 lm = PyDict_Size(self->memo);
2603 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002604 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002605 have_get = malloc(lm);
2606 if (have_get == NULL) return PyErr_NoMemory();
2607 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002608
2609 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002610 for (rsize = 0, i = l; --i >= 0; ) {
2611 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002612
Tim Petersac5687a2003-02-02 18:08:34 +00002613 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002614 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002615
2616 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002617 ik = PyInt_AS_LONG((PyIntObject*)k);
2618 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002619 PyErr_SetString(PicklingError,
2620 "Invalid get data");
2621 return NULL;
2622 }
Tim Petersac5687a2003-02-02 18:08:34 +00002623 if (have_get[ik]) /* with matching get */
2624 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002625 }
2626
2627 else if (! (PyTuple_Check(k) &&
2628 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002629 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002630 ) {
2631 PyErr_SetString(PicklingError,
2632 "Unexpected data in internal list");
2633 return NULL;
2634 }
2635
2636 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002637 ik = PyInt_AS_LONG((PyIntObject *)k);
2638 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002639 PyErr_SetString(PicklingError,
2640 "Invalid get data");
2641 return NULL;
2642 }
Tim Petersac5687a2003-02-02 18:08:34 +00002643 have_get[ik] = 1;
2644 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002645 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002646 }
2647
2648 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002649 r = PyString_FromStringAndSize(NULL, rsize);
2650 if (r == NULL) goto err;
2651 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002652
Tim Petersac5687a2003-02-02 18:08:34 +00002653 for (i = 0; i < l; i++) {
2654 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002655
2656 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002657 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002658 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002659 p=PyString_AS_STRING((PyStringObject *)k);
2660 while (--ssize >= 0)
2661 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002662 }
2663 }
2664
2665 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002666 ik = PyInt_AS_LONG((PyIntObject *)
2667 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002668 if (ik < 256) {
2669 *s++ = BINGET;
2670 *s++ = (int)(ik & 0xff);
2671 }
2672 else {
2673 *s++ = LONG_BINGET;
2674 *s++ = (int)(ik & 0xff);
2675 *s++ = (int)((ik >> 8) & 0xff);
2676 *s++ = (int)((ik >> 16) & 0xff);
2677 *s++ = (int)((ik >> 24) & 0xff);
2678 }
2679 }
2680
2681 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002682 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002683
2684 if (have_get[ik]) { /* with matching get */
2685 if (ik < 256) {
2686 *s++ = BINPUT;
2687 *s++ = (int)(ik & 0xff);
2688 }
2689 else {
2690 *s++ = LONG_BINPUT;
2691 *s++ = (int)(ik & 0xff);
2692 *s++ = (int)((ik >> 8) & 0xff);
2693 *s++ = (int)((ik >> 16) & 0xff);
2694 *s++ = (int)((ik >> 24) & 0xff);
2695 }
2696 }
2697 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002698 }
2699
2700 if (clear) {
2701 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002702 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002703 }
2704
2705 free(have_get);
2706 return r;
2707 err:
2708 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002709 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002710}
2711
2712static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002713Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002714{
2715 PyObject *ob;
2716 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002717
Tim Peterscba30e22003-02-01 06:24:36 +00002718 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002719 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002720
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002721 if (dump(self, ob) < 0)
2722 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002723
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002724 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002725
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002726 /* XXX Why does dump() return self? */
2727 Py_INCREF(self);
2728 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002729}
2730
2731
Tim Peterscba30e22003-02-01 06:24:36 +00002732static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002733{
Neal Norwitzb0493252002-03-31 14:44:22 +00002734 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002735 PyDoc_STR("dump(object) -- "
2736 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002737 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002738 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002739 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002740 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002741 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002742};
2743
2744
2745static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002746newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002747{
2748 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002749
Tim Peters5bd2a792003-02-01 16:45:06 +00002750 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002751 proto = HIGHEST_PROTOCOL;
2752 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002753 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2754 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002755 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002756 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002757 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002758
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002759 self = PyObject_GC_New(Picklerobject, &Picklertype);
Tim Peters5bd2a792003-02-01 16:45:06 +00002760 if (self == NULL)
2761 return NULL;
2762 self->proto = proto;
2763 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002764 self->fp = NULL;
2765 self->write = NULL;
2766 self->memo = NULL;
2767 self->arg = NULL;
2768 self->pers_func = NULL;
2769 self->inst_pers_func = NULL;
2770 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002771 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002772 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002773 self->fast_container = 0;
2774 self->fast_memo = NULL;
2775 self->buf_size = 0;
2776 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002777
Tim Peters5bd2a792003-02-01 16:45:06 +00002778 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002779 if (file)
2780 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002781 else {
2782 file = Pdata_New();
2783 if (file == NULL)
2784 goto err;
2785 }
2786 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002787
Tim Peterscba30e22003-02-01 06:24:36 +00002788 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002789 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002790
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002791 if (PyFile_Check(file)) {
2792 self->fp = PyFile_AsFile(file);
2793 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002794 PyErr_SetString(PyExc_ValueError,
2795 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002796 goto err;
2797 }
2798 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002799 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002800 else if (PycStringIO_OutputCheck(file)) {
2801 self->write_func = write_cStringIO;
2802 }
2803 else if (file == Py_None) {
2804 self->write_func = write_none;
2805 }
2806 else {
2807 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002808
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002809 if (! Pdata_Check(file)) {
2810 self->write = PyObject_GetAttr(file, write_str);
2811 if (!self->write) {
2812 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002813 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002814 "argument must have 'write' "
2815 "attribute");
2816 goto err;
2817 }
2818 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002819
Tim Peters5bd2a792003-02-01 16:45:06 +00002820 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2821 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002822 PyErr_NoMemory();
2823 goto err;
2824 }
2825 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002826
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002827 if (PyEval_GetRestricted()) {
2828 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002829 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002830
Tim Peters5b7da392003-02-04 00:21:07 +00002831 if (m == NULL)
2832 goto err;
2833 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002834 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002835 if (self->dispatch_table == NULL)
2836 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002837 }
2838 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002839 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002840 Py_INCREF(dispatch_table);
2841 }
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002842 PyObject_GC_Track(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002843
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002844 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002845
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002846 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002847 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002848 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002849}
2850
2851
2852static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002853get_Pickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002854{
2855 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002856 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002857
Tim Peters92c8bb32003-02-13 23:00:26 +00002858 /* XXX
2859 * The documented signature is Pickler(file, proto=0), but this
2860 * accepts Pickler() and Pickler(integer) too. The meaning then
2861 * is clear as mud, undocumented, and not supported by pickle.py.
2862 * I'm told Zope uses this, but I haven't traced into this code
2863 * far enough to figure out what it means.
Tim Peters5bd2a792003-02-01 16:45:06 +00002864 */
2865 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002866 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002867 proto = 0;
2868 if (!PyArg_ParseTuple(args, "O|i:Pickler", &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002869 return NULL;
2870 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002871 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002872}
2873
2874
2875static void
Tim Peterscba30e22003-02-01 06:24:36 +00002876Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002877{
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002878 PyObject_GC_UnTrack(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002879 Py_XDECREF(self->write);
2880 Py_XDECREF(self->memo);
2881 Py_XDECREF(self->fast_memo);
2882 Py_XDECREF(self->arg);
2883 Py_XDECREF(self->file);
2884 Py_XDECREF(self->pers_func);
2885 Py_XDECREF(self->inst_pers_func);
2886 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002887 PyMem_Free(self->write_buf);
Tim Peters3cfe7542003-05-21 21:29:48 +00002888 self->ob_type->tp_free((PyObject *)self);
Jeremy Hylton4cf63192003-04-09 21:05:12 +00002889}
2890
2891static int
2892Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2893{
2894 int err;
2895#define VISIT(SLOT) \
2896 if (SLOT) { \
2897 err = visit((PyObject *)(SLOT), arg); \
2898 if (err) \
2899 return err; \
2900 }
2901 VISIT(self->write);
2902 VISIT(self->memo);
2903 VISIT(self->fast_memo);
2904 VISIT(self->arg);
2905 VISIT(self->file);
2906 VISIT(self->pers_func);
2907 VISIT(self->inst_pers_func);
2908 VISIT(self->dispatch_table);
2909#undef VISIT
2910 return 0;
2911}
2912
2913static int
2914Pickler_clear(Picklerobject *self)
2915{
2916#define CLEAR(SLOT) Py_XDECREF(SLOT); SLOT = NULL;
2917 CLEAR(self->write);
2918 CLEAR(self->memo);
2919 CLEAR(self->fast_memo);
2920 CLEAR(self->arg);
2921 CLEAR(self->file);
2922 CLEAR(self->pers_func);
2923 CLEAR(self->inst_pers_func);
2924 CLEAR(self->dispatch_table);
2925#undef CLEAR
2926 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002927}
2928
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002929static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002930Pickler_get_pers_func(Picklerobject *p)
2931{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002932 if (p->pers_func == NULL)
2933 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2934 else
2935 Py_INCREF(p->pers_func);
2936 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002937}
2938
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002939static int
2940Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2941{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002942 if (v == NULL) {
2943 PyErr_SetString(PyExc_TypeError,
2944 "attribute deletion is not supported");
2945 return -1;
2946 }
2947 Py_XDECREF(p->pers_func);
2948 Py_INCREF(v);
2949 p->pers_func = v;
2950 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002951}
2952
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002953static int
2954Pickler_set_inst_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->inst_pers_func);
2962 Py_INCREF(v);
2963 p->inst_pers_func = v;
2964 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002965}
2966
2967static PyObject *
2968Pickler_get_memo(Picklerobject *p)
2969{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002970 if (p->memo == NULL)
2971 PyErr_SetString(PyExc_AttributeError, "memo");
2972 else
2973 Py_INCREF(p->memo);
2974 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002975}
2976
2977static int
2978Pickler_set_memo(Picklerobject *p, PyObject *v)
2979{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002980 if (v == NULL) {
2981 PyErr_SetString(PyExc_TypeError,
2982 "attribute deletion is not supported");
2983 return -1;
2984 }
2985 if (!PyDict_Check(v)) {
2986 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2987 return -1;
2988 }
2989 Py_XDECREF(p->memo);
2990 Py_INCREF(v);
2991 p->memo = v;
2992 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002993}
2994
2995static PyObject *
2996Pickler_get_error(Picklerobject *p)
2997{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002998 /* why is this an attribute on the Pickler? */
2999 Py_INCREF(PicklingError);
3000 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003001}
3002
3003static PyMemberDef Pickler_members[] = {
3004 {"binary", T_INT, offsetof(Picklerobject, bin)},
3005 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003006 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003007};
3008
3009static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00003010 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003011 (setter)Pickler_set_pers_func},
3012 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3013 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00003014 {"PicklingError", (getter)Pickler_get_error, NULL},
3015 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003016};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003017
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003018PyDoc_STRVAR(Picklertype__doc__,
3019"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003020
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003021static PyTypeObject Picklertype = {
3022 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00003023 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00003024 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003025 sizeof(Picklerobject), /*tp_basicsize*/
3026 0,
3027 (destructor)Pickler_dealloc, /* tp_dealloc */
3028 0, /* tp_print */
3029 0, /* tp_getattr */
3030 0, /* tp_setattr */
3031 0, /* tp_compare */
3032 0, /* tp_repr */
3033 0, /* tp_as_number */
3034 0, /* tp_as_sequence */
3035 0, /* tp_as_mapping */
3036 0, /* tp_hash */
3037 0, /* tp_call */
3038 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00003039 PyObject_GenericGetAttr, /* tp_getattro */
3040 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003041 0, /* tp_as_buffer */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003042 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003043 Picklertype__doc__, /* tp_doc */
Jeremy Hylton4cf63192003-04-09 21:05:12 +00003044 (traverseproc)Pickler_traverse, /* tp_traverse */
3045 (inquiry)Pickler_clear, /* tp_clear */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00003046 0, /* tp_richcompare */
3047 0, /* tp_weaklistoffset */
3048 0, /* tp_iter */
3049 0, /* tp_iternext */
3050 Pickler_methods, /* tp_methods */
3051 Pickler_members, /* tp_members */
3052 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003053};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003054
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003055static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003056find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003057{
3058 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003059
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003060 if (fc) {
3061 if (fc==Py_None) {
Tim Peters92c8bb32003-02-13 23:00:26 +00003062 PyErr_SetString(UnpicklingError, "Global and instance "
3063 "pickles are not supported.");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003064 return NULL;
3065 }
Tim Peterscba30e22003-02-01 06:24:36 +00003066 return PyObject_CallFunction(fc, "OO", py_module_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003067 py_global_name);
3068 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003069
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003070 module = PySys_GetObject("modules");
3071 if (module == NULL)
3072 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003073
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003074 module = PyDict_GetItem(module, py_module_name);
3075 if (module == NULL) {
3076 module = PyImport_Import(py_module_name);
3077 if (!module)
3078 return NULL;
3079 global = PyObject_GetAttr(module, py_global_name);
3080 Py_DECREF(module);
3081 }
3082 else
3083 global = PyObject_GetAttr(module, py_global_name);
3084 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003085}
3086
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003087static int
Tim Peterscba30e22003-02-01 06:24:36 +00003088marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003089{
3090 if (self->num_marks < 1) {
3091 PyErr_SetString(UnpicklingError, "could not find MARK");
3092 return -1;
3093 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003094
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003095 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003096}
3097
Tim Peters84e87f32001-03-17 04:50:51 +00003098
Guido van Rossum60456fd1997-04-09 17:36:32 +00003099static int
Tim Peterscba30e22003-02-01 06:24:36 +00003100load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003101{
3102 PDATA_APPEND(self->stack, Py_None, -1);
3103 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003104}
3105
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003106static int
Tim Peterscba30e22003-02-01 06:24:36 +00003107bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003108{
3109 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3110 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003111}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003112
3113static int
Tim Peterscba30e22003-02-01 06:24:36 +00003114load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003115{
3116 PyObject *py_int = 0;
3117 char *endptr, *s;
3118 int len, res = -1;
3119 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003120
Tim Peters0bc93f52003-02-02 18:29:33 +00003121 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003122 if (len < 2) return bad_readline();
3123 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003124
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003125 errno = 0;
3126 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003127
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003128 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3129 /* Hm, maybe we've got something long. Let's try reading
3130 it as a Python long object. */
3131 errno = 0;
3132 py_int = PyLong_FromString(s, NULL, 0);
3133 if (py_int == NULL) {
3134 PyErr_SetString(PyExc_ValueError,
3135 "could not convert string to int");
3136 goto finally;
3137 }
3138 }
3139 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003140 if (len == 3 && (l == 0 || l == 1)) {
3141 if (!( py_int = PyBool_FromLong(l))) goto finally;
3142 }
3143 else {
3144 if (!( py_int = PyInt_FromLong(l))) goto finally;
3145 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003146 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003147
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003148 free(s);
3149 PDATA_PUSH(self->stack, py_int, -1);
3150 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003151
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003152 finally:
3153 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003154
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003155 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003156}
3157
Tim Peters3c67d792003-02-02 17:59:11 +00003158static int
3159load_bool(Unpicklerobject *self, PyObject *boolean)
3160{
3161 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003162 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003163 return 0;
3164}
3165
Tim Petersee1a53c2003-02-02 02:57:53 +00003166/* s contains x bytes of a little-endian integer. Return its value as a
3167 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3168 * int, but when x is 4 it's a signed one. This is an historical source
3169 * of x-platform bugs.
3170 */
Tim Peters84e87f32001-03-17 04:50:51 +00003171static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003172calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003173{
3174 unsigned char c;
3175 int i;
3176 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003177
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003178 for (i = 0, l = 0L; i < x; i++) {
3179 c = (unsigned char)s[i];
3180 l |= (long)c << (i * 8);
3181 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003182#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003183 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3184 * is signed, so on a box with longs bigger than 4 bytes we need
3185 * to extend a BININT's sign bit to the full width.
3186 */
3187 if (x == 4 && l & (1L << 31))
3188 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003189#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003190 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003191}
3192
3193
3194static int
Tim Peterscba30e22003-02-01 06:24:36 +00003195load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003196{
3197 PyObject *py_int = 0;
3198 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003199
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003200 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003201
Tim Peterscba30e22003-02-01 06:24:36 +00003202 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003203 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003204
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003205 PDATA_PUSH(self->stack, py_int, -1);
3206 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003207}
3208
3209
3210static int
Tim Peterscba30e22003-02-01 06:24:36 +00003211load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003212{
3213 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003214
Tim Peters0bc93f52003-02-02 18:29:33 +00003215 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003216 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003217
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003218 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003219}
3220
3221
3222static int
Tim Peterscba30e22003-02-01 06:24:36 +00003223load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003224{
3225 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003226
Tim Peters0bc93f52003-02-02 18:29:33 +00003227 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003228 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003229
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003230 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003231}
3232
3233
3234static int
Tim Peterscba30e22003-02-01 06:24:36 +00003235load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003236{
3237 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003238
Tim Peters0bc93f52003-02-02 18:29:33 +00003239 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003240 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003241
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003242 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003243}
Tim Peters84e87f32001-03-17 04:50:51 +00003244
Guido van Rossum60456fd1997-04-09 17:36:32 +00003245static int
Tim Peterscba30e22003-02-01 06:24:36 +00003246load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003247{
3248 PyObject *l = 0;
3249 char *end, *s;
3250 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003251
Tim Peters0bc93f52003-02-02 18:29:33 +00003252 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003253 if (len < 2) return bad_readline();
3254 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003255
Tim Peterscba30e22003-02-01 06:24:36 +00003256 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003257 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003258
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003259 free(s);
3260 PDATA_PUSH(self->stack, l, -1);
3261 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003262
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003263 finally:
3264 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003265
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003266 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003267}
3268
Tim Petersee1a53c2003-02-02 02:57:53 +00003269/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3270 * data following.
3271 */
3272static int
3273load_counted_long(Unpicklerobject *self, int size)
3274{
3275 int i;
3276 char *nbytes;
3277 unsigned char *pdata;
3278 PyObject *along;
3279
3280 assert(size == 1 || size == 4);
3281 i = self->read_func(self, &nbytes, size);
3282 if (i < 0) return -1;
3283
3284 size = calc_binint(nbytes, size);
3285 if (size < 0) {
3286 /* Corrupt or hostile pickle -- we never write one like
3287 * this.
3288 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003289 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003290 "byte count");
3291 return -1;
3292 }
3293
3294 if (size == 0)
3295 along = PyLong_FromLong(0L);
3296 else {
3297 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003298 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003299 if (i < 0) return -1;
3300 along = _PyLong_FromByteArray(pdata, (size_t)size,
3301 1 /* little endian */, 1 /* signed */);
3302 }
3303 if (along == NULL)
3304 return -1;
3305 PDATA_PUSH(self->stack, along, -1);
3306 return 0;
3307}
Tim Peters84e87f32001-03-17 04:50:51 +00003308
Guido van Rossum60456fd1997-04-09 17:36:32 +00003309static int
Tim Peterscba30e22003-02-01 06:24:36 +00003310load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003311{
3312 PyObject *py_float = 0;
3313 char *endptr, *s;
3314 int len, res = -1;
3315 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003316
Tim Peters0bc93f52003-02-02 18:29:33 +00003317 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003318 if (len < 2) return bad_readline();
3319 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003320
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003321 errno = 0;
3322 d = strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003323
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003324 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3325 PyErr_SetString(PyExc_ValueError,
3326 "could not convert string to float");
3327 goto finally;
3328 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003329
Tim Peterscba30e22003-02-01 06:24:36 +00003330 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003331 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003332
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003333 free(s);
3334 PDATA_PUSH(self->stack, py_float, -1);
3335 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003336
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003337 finally:
3338 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003339
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003340 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003341}
3342
Guido van Rossum60456fd1997-04-09 17:36:32 +00003343static int
Tim Peterscba30e22003-02-01 06:24:36 +00003344load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003345{
Tim Peters9905b942003-03-20 20:53:32 +00003346 PyObject *py_float;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003347 double x;
3348 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003349
Tim Peters0bc93f52003-02-02 18:29:33 +00003350 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003351 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003352
Tim Peters9905b942003-03-20 20:53:32 +00003353 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3354 if (x == -1.0 && PyErr_Occurred())
3355 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003356
Tim Peters9905b942003-03-20 20:53:32 +00003357 py_float = PyFloat_FromDouble(x);
3358 if (py_float == NULL)
3359 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003360
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003361 PDATA_PUSH(self->stack, py_float, -1);
3362 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003363}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003364
3365static int
Tim Peterscba30e22003-02-01 06:24:36 +00003366load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003367{
3368 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003369 int len, res = -1;
3370 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003371
Tim Peters0bc93f52003-02-02 18:29:33 +00003372 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003373 if (len < 2) return bad_readline();
3374 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003375
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003376
3377 /* Strip outermost quotes */
3378 while (s[len-1] <= ' ')
3379 len--;
3380 if(s[0]=='"' && s[len-1]=='"'){
3381 s[len-1] = '\0';
3382 p = s + 1 ;
3383 len -= 2;
3384 } else if(s[0]=='\'' && s[len-1]=='\''){
3385 s[len-1] = '\0';
3386 p = s + 1 ;
3387 len -= 2;
3388 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003389 goto insecure;
3390 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003391
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003392 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3393 if (str) {
3394 PDATA_PUSH(self->stack, str, -1);
3395 res = 0;
3396 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003397 free(s);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003398 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003399
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003400 insecure:
3401 free(s);
3402 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3403 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003404}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003405
3406
3407static int
Tim Peterscba30e22003-02-01 06:24:36 +00003408load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003409{
3410 PyObject *py_string = 0;
3411 long l;
3412 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003413
Tim Peters0bc93f52003-02-02 18:29:33 +00003414 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003415
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003416 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003417
Tim Peters0bc93f52003-02-02 18:29:33 +00003418 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003419 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003420
Tim Peterscba30e22003-02-01 06:24:36 +00003421 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003422 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003424 PDATA_PUSH(self->stack, py_string, -1);
3425 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003426}
3427
3428
3429static int
Tim Peterscba30e22003-02-01 06:24:36 +00003430load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003431{
3432 PyObject *py_string = 0;
3433 unsigned char l;
3434 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003435
Tim Peters0bc93f52003-02-02 18:29:33 +00003436 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003437 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003438
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003439 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003440
Tim Peters0bc93f52003-02-02 18:29:33 +00003441 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003442
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003443 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003444
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003445 PDATA_PUSH(self->stack, py_string, -1);
3446 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003447}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003448
3449
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003450#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003451static int
Tim Peterscba30e22003-02-01 06:24:36 +00003452load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003453{
3454 PyObject *str = 0;
3455 int len, res = -1;
3456 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003457
Tim Peters0bc93f52003-02-02 18:29:33 +00003458 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003459 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003460
Tim Peterscba30e22003-02-01 06:24:36 +00003461 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003462 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003463
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003464 PDATA_PUSH(self->stack, str, -1);
3465 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003466
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003467 finally:
3468 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003469}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003470#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003471
3472
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003473#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003474static int
Tim Peterscba30e22003-02-01 06:24:36 +00003475load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003476{
3477 PyObject *unicode;
3478 long l;
3479 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003480
Tim Peters0bc93f52003-02-02 18:29:33 +00003481 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003482
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003483 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003484
Tim Peters0bc93f52003-02-02 18:29:33 +00003485 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003486 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003487
Tim Peterscba30e22003-02-01 06:24:36 +00003488 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003489 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003490
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003491 PDATA_PUSH(self->stack, unicode, -1);
3492 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003493}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003494#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003495
3496
3497static int
Tim Peterscba30e22003-02-01 06:24:36 +00003498load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003499{
3500 PyObject *tup;
3501 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003503 if ((i = marker(self)) < 0) return -1;
3504 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3505 PDATA_PUSH(self->stack, tup, -1);
3506 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003507}
3508
3509static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003510load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003511{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003512 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003513
Tim Peters1d63c9f2003-02-02 20:29:39 +00003514 if (tup == NULL)
3515 return -1;
3516
3517 while (--len >= 0) {
3518 PyObject *element;
3519
3520 PDATA_POP(self->stack, element);
3521 if (element == NULL)
3522 return -1;
3523 PyTuple_SET_ITEM(tup, len, element);
3524 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003525 PDATA_PUSH(self->stack, tup, -1);
3526 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003527}
3528
3529static int
Tim Peterscba30e22003-02-01 06:24:36 +00003530load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003531{
3532 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003533
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003534 if (!( list=PyList_New(0))) return -1;
3535 PDATA_PUSH(self->stack, list, -1);
3536 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003537}
3538
3539static int
Tim Peterscba30e22003-02-01 06:24:36 +00003540load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003541{
3542 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003543
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003544 if (!( dict=PyDict_New())) return -1;
3545 PDATA_PUSH(self->stack, dict, -1);
3546 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003547}
3548
3549
3550static int
Tim Peterscba30e22003-02-01 06:24:36 +00003551load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003552{
3553 PyObject *list = 0;
3554 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003555
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003556 if ((i = marker(self)) < 0) return -1;
3557 if (!( list=Pdata_popList(self->stack, i))) return -1;
3558 PDATA_PUSH(self->stack, list, -1);
3559 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003560}
3561
3562static int
Tim Peterscba30e22003-02-01 06:24:36 +00003563load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003564{
3565 PyObject *dict, *key, *value;
3566 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003567
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003568 if ((i = marker(self)) < 0) return -1;
3569 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003570
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003571 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003572
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003573 for (k = i+1; k < j; k += 2) {
3574 key =self->stack->data[k-1];
3575 value=self->stack->data[k ];
3576 if (PyDict_SetItem(dict, key, value) < 0) {
3577 Py_DECREF(dict);
3578 return -1;
3579 }
3580 }
3581 Pdata_clear(self->stack, i);
3582 PDATA_PUSH(self->stack, dict, -1);
3583 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003584}
3585
3586static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003587Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003588{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003589 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003590
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003591 if (PyClass_Check(cls)) {
3592 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003593
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003594 if ((l=PyObject_Size(args)) < 0) goto err;
3595 if (!( l )) {
3596 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003597
Tim Peterscba30e22003-02-01 06:24:36 +00003598 __getinitargs__ = PyObject_GetAttr(cls,
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00003599 __getinitargs___str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003600 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003601 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003602 so bypass usual construction */
3603 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003604
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003605 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003606 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003607 goto err;
3608 return inst;
3609 }
3610 Py_DECREF(__getinitargs__);
3611 }
Tim Peters84e87f32001-03-17 04:50:51 +00003612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003613 if ((r=PyInstance_New(cls, args, NULL))) return r;
3614 else goto err;
3615 }
Tim Peters84e87f32001-03-17 04:50:51 +00003616
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003617 if (args==Py_None) {
3618 /* Special case, call cls.__basicnew__() */
3619 PyObject *basicnew;
Tim Peters84e87f32001-03-17 04:50:51 +00003620
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003621 basicnew = PyObject_GetAttr(cls, __basicnew___str);
3622 if (!basicnew) return NULL;
3623 r=PyObject_CallObject(basicnew, NULL);
3624 Py_DECREF(basicnew);
3625 if (r) return r;
3626 }
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003628 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003630 err:
3631 {
3632 PyObject *tp, *v, *tb;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003633
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003634 PyErr_Fetch(&tp, &v, &tb);
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003635 if ((r=PyTuple_Pack(3,v,cls,args))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003636 Py_XDECREF(v);
3637 v=r;
3638 }
3639 PyErr_Restore(tp,v,tb);
3640 }
3641 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003642}
Tim Peters84e87f32001-03-17 04:50:51 +00003643
Guido van Rossum60456fd1997-04-09 17:36:32 +00003644
3645static int
Tim Peterscba30e22003-02-01 06:24:36 +00003646load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003647{
3648 PyObject *class, *tup, *obj=0;
3649 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003650
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003651 if ((i = marker(self)) < 0) return -1;
3652 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3653 PDATA_POP(self->stack, class);
3654 if (class) {
3655 obj = Instance_New(class, tup);
3656 Py_DECREF(class);
3657 }
3658 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003660 if (! obj) return -1;
3661 PDATA_PUSH(self->stack, obj, -1);
3662 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003663}
3664
3665
3666static int
Tim Peterscba30e22003-02-01 06:24:36 +00003667load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003668{
3669 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3670 int i, len;
3671 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003672
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003673 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003674
Tim Peters0bc93f52003-02-02 18:29:33 +00003675 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003676 if (len < 2) return bad_readline();
3677 module_name = PyString_FromStringAndSize(s, len - 1);
3678 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003679
Tim Peters0bc93f52003-02-02 18:29:33 +00003680 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003681 if (len < 2) return bad_readline();
3682 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003683 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003684 self->find_class);
3685 Py_DECREF(class_name);
3686 }
3687 }
3688 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003689
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003690 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003691
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003692 if ((tup=Pdata_popTuple(self->stack, i))) {
3693 obj = Instance_New(class, tup);
3694 Py_DECREF(tup);
3695 }
3696 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003697
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003698 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003699
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003700 PDATA_PUSH(self->stack, obj, -1);
3701 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003702}
3703
Tim Peterseab7db32003-02-13 18:24:14 +00003704static int
3705load_newobj(Unpicklerobject *self)
3706{
3707 PyObject *args = NULL;
3708 PyObject *clsraw = NULL;
3709 PyTypeObject *cls; /* clsraw cast to its true type */
3710 PyObject *obj;
3711
3712 /* Stack is ... cls argtuple, and we want to call
3713 * cls.__new__(cls, *argtuple).
3714 */
3715 PDATA_POP(self->stack, args);
3716 if (args == NULL) goto Fail;
3717 if (! PyTuple_Check(args)) {
3718 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3719 "tuple.");
3720 goto Fail;
3721 }
3722
3723 PDATA_POP(self->stack, clsraw);
3724 cls = (PyTypeObject *)clsraw;
3725 if (cls == NULL) goto Fail;
3726 if (! PyType_Check(cls)) {
3727 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3728 "isn't a type object");
3729 goto Fail;
3730 }
3731 if (cls->tp_new == NULL) {
3732 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3733 "has NULL tp_new");
3734 goto Fail;
3735 }
3736
3737 /* Call __new__. */
3738 obj = cls->tp_new(cls, args, NULL);
3739 if (obj == NULL) goto Fail;
3740
3741 Py_DECREF(args);
3742 Py_DECREF(clsraw);
3743 PDATA_PUSH(self->stack, obj, -1);
3744 return 0;
3745
3746 Fail:
3747 Py_XDECREF(args);
3748 Py_XDECREF(clsraw);
3749 return -1;
3750}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003751
3752static int
Tim Peterscba30e22003-02-01 06:24:36 +00003753load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003754{
3755 PyObject *class = 0, *module_name = 0, *class_name = 0;
3756 int len;
3757 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003758
Tim Peters0bc93f52003-02-02 18:29:33 +00003759 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003760 if (len < 2) return bad_readline();
3761 module_name = PyString_FromStringAndSize(s, len - 1);
3762 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003763
Tim Peters0bc93f52003-02-02 18:29:33 +00003764 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003765 if (len < 2) {
3766 Py_DECREF(module_name);
3767 return bad_readline();
3768 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003769 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003770 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003771 self->find_class);
3772 Py_DECREF(class_name);
3773 }
3774 }
3775 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003776
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003777 if (! class) return -1;
3778 PDATA_PUSH(self->stack, class, -1);
3779 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003780}
3781
3782
3783static int
Tim Peterscba30e22003-02-01 06:24:36 +00003784load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003785{
3786 PyObject *pid = 0;
3787 int len;
3788 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003789
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003790 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003791 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003792 if (len < 2) return bad_readline();
3793
3794 pid = PyString_FromStringAndSize(s, len - 1);
3795 if (!pid) return -1;
3796
3797 if (PyList_Check(self->pers_func)) {
3798 if (PyList_Append(self->pers_func, pid) < 0) {
3799 Py_DECREF(pid);
3800 return -1;
3801 }
3802 }
3803 else {
3804 ARG_TUP(self, pid);
3805 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003806 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003807 NULL);
3808 FREE_ARG_TUP(self);
3809 }
3810 }
3811
3812 if (! pid) return -1;
3813
3814 PDATA_PUSH(self->stack, pid, -1);
3815 return 0;
3816 }
3817 else {
3818 PyErr_SetString(UnpicklingError,
3819 "A load persistent id instruction was encountered,\n"
3820 "but no persistent_load function was specified.");
3821 return -1;
3822 }
3823}
3824
3825static int
Tim Peterscba30e22003-02-01 06:24:36 +00003826load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003827{
3828 PyObject *pid = 0;
3829
3830 if (self->pers_func) {
3831 PDATA_POP(self->stack, pid);
3832 if (! pid) return -1;
3833
3834 if (PyList_Check(self->pers_func)) {
3835 if (PyList_Append(self->pers_func, pid) < 0) {
3836 Py_DECREF(pid);
3837 return -1;
3838 }
3839 }
3840 else {
3841 ARG_TUP(self, pid);
3842 if (self->arg) {
3843 pid = PyObject_Call(self->pers_func, self->arg,
3844 NULL);
3845 FREE_ARG_TUP(self);
3846 }
3847 if (! pid) return -1;
3848 }
3849
3850 PDATA_PUSH(self->stack, pid, -1);
3851 return 0;
3852 }
3853 else {
3854 PyErr_SetString(UnpicklingError,
3855 "A load persistent id instruction was encountered,\n"
3856 "but no persistent_load function was specified.");
3857 return -1;
3858 }
3859}
3860
3861
3862static int
Tim Peterscba30e22003-02-01 06:24:36 +00003863load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003864{
3865 int len;
3866
3867 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3868
3869 /* Note that we split the (pickle.py) stack into two stacks,
3870 an object stack and a mark stack. We have to be clever and
3871 pop the right one. We do this by looking at the top of the
3872 mark stack.
3873 */
3874
3875 if ((self->num_marks > 0) &&
3876 (self->marks[self->num_marks - 1] == len))
3877 self->num_marks--;
3878 else {
3879 len--;
3880 Py_DECREF(self->stack->data[len]);
3881 self->stack->length=len;
3882 }
3883
3884 return 0;
3885}
3886
3887
3888static int
Tim Peterscba30e22003-02-01 06:24:36 +00003889load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003890{
3891 int i;
3892
3893 if ((i = marker(self)) < 0)
3894 return -1;
3895
3896 Pdata_clear(self->stack, i);
3897
3898 return 0;
3899}
3900
3901
3902static int
Tim Peterscba30e22003-02-01 06:24:36 +00003903load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003904{
3905 PyObject *last;
3906 int len;
3907
3908 if ((len = self->stack->length) <= 0) return stackUnderflow();
3909 last=self->stack->data[len-1];
3910 Py_INCREF(last);
3911 PDATA_PUSH(self->stack, last, -1);
3912 return 0;
3913}
3914
3915
3916static int
Tim Peterscba30e22003-02-01 06:24:36 +00003917load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003918{
3919 PyObject *py_str = 0, *value = 0;
3920 int len;
3921 char *s;
3922 int rc;
3923
Tim Peters0bc93f52003-02-02 18:29:33 +00003924 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003925 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003926
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003927 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003928
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003929 value = PyDict_GetItem(self->memo, py_str);
3930 if (! value) {
3931 PyErr_SetObject(BadPickleGet, py_str);
3932 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003933 }
3934 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003935 PDATA_APPEND(self->stack, value, -1);
3936 rc = 0;
3937 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003938
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003939 Py_DECREF(py_str);
3940 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003941}
3942
3943
3944static int
Tim Peterscba30e22003-02-01 06:24:36 +00003945load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003946{
3947 PyObject *py_key = 0, *value = 0;
3948 unsigned char key;
3949 char *s;
3950 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003951
Tim Peters0bc93f52003-02-02 18:29:33 +00003952 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003953
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003954 key = (unsigned char)s[0];
3955 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003956
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003957 value = PyDict_GetItem(self->memo, py_key);
3958 if (! value) {
3959 PyErr_SetObject(BadPickleGet, py_key);
3960 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003961 }
3962 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003963 PDATA_APPEND(self->stack, value, -1);
3964 rc = 0;
3965 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003966
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003967 Py_DECREF(py_key);
3968 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003969}
3970
3971
3972static int
Tim Peterscba30e22003-02-01 06:24:36 +00003973load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003974{
3975 PyObject *py_key = 0, *value = 0;
3976 unsigned char c;
3977 char *s;
3978 long key;
3979 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003980
Tim Peters0bc93f52003-02-02 18:29:33 +00003981 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003982
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003983 c = (unsigned char)s[0];
3984 key = (long)c;
3985 c = (unsigned char)s[1];
3986 key |= (long)c << 8;
3987 c = (unsigned char)s[2];
3988 key |= (long)c << 16;
3989 c = (unsigned char)s[3];
3990 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003991
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003992 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3993
3994 value = PyDict_GetItem(self->memo, py_key);
3995 if (! value) {
3996 PyErr_SetObject(BadPickleGet, py_key);
3997 rc = -1;
Tim Peters92c8bb32003-02-13 23:00:26 +00003998 }
3999 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004000 PDATA_APPEND(self->stack, value, -1);
4001 rc = 0;
4002 }
4003
4004 Py_DECREF(py_key);
4005 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004006}
4007
Tim Peters2d629652003-02-04 05:06:17 +00004008/* Push an object from the extension registry (EXT[124]). nbytes is
4009 * the number of bytes following the opcode, holding the index (code) value.
4010 */
4011static int
4012load_extension(Unpicklerobject *self, int nbytes)
4013{
4014 char *codebytes; /* the nbytes bytes after the opcode */
4015 long code; /* calc_binint returns long */
4016 PyObject *py_code; /* code as a Python int */
4017 PyObject *obj; /* the object to push */
4018 PyObject *pair; /* (module_name, class_name) */
4019 PyObject *module_name, *class_name;
4020
4021 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4022 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4023 code = calc_binint(codebytes, nbytes);
4024 if (code <= 0) { /* note that 0 is forbidden */
4025 /* Corrupt or hostile pickle. */
4026 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4027 return -1;
4028 }
4029
4030 /* Look for the code in the cache. */
4031 py_code = PyInt_FromLong(code);
4032 if (py_code == NULL) return -1;
4033 obj = PyDict_GetItem(extension_cache, py_code);
4034 if (obj != NULL) {
4035 /* Bingo. */
4036 Py_DECREF(py_code);
4037 PDATA_APPEND(self->stack, obj, -1);
4038 return 0;
4039 }
4040
4041 /* Look up the (module_name, class_name) pair. */
4042 pair = PyDict_GetItem(inverted_registry, py_code);
4043 if (pair == NULL) {
4044 Py_DECREF(py_code);
4045 PyErr_Format(PyExc_ValueError, "unregistered extension "
4046 "code %ld", code);
4047 return -1;
4048 }
4049 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004050 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004051 */
4052 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4053 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4054 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4055 Py_DECREF(py_code);
4056 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4057 "isn't a 2-tuple of strings", code);
4058 return -1;
4059 }
4060 /* Load the object. */
4061 obj = find_class(module_name, class_name, self->find_class);
4062 if (obj == NULL) {
4063 Py_DECREF(py_code);
4064 return -1;
4065 }
4066 /* Cache code -> obj. */
4067 code = PyDict_SetItem(extension_cache, py_code, obj);
4068 Py_DECREF(py_code);
4069 if (code < 0) {
4070 Py_DECREF(obj);
4071 return -1;
4072 }
4073 PDATA_PUSH(self->stack, obj, -1);
4074 return 0;
4075}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004076
4077static int
Tim Peterscba30e22003-02-01 06:24:36 +00004078load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004079{
4080 PyObject *py_str = 0, *value = 0;
4081 int len, l;
4082 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004083
Tim Peters0bc93f52003-02-02 18:29:33 +00004084 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004085 if (l < 2) return bad_readline();
4086 if (!( len=self->stack->length )) return stackUnderflow();
4087 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4088 value=self->stack->data[len-1];
4089 l=PyDict_SetItem(self->memo, py_str, value);
4090 Py_DECREF(py_str);
4091 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004092}
4093
4094
4095static int
Tim Peterscba30e22003-02-01 06:24:36 +00004096load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004097{
4098 PyObject *py_key = 0, *value = 0;
4099 unsigned char key;
4100 char *s;
4101 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004102
Tim Peters0bc93f52003-02-02 18:29:33 +00004103 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004104 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004105
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004106 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004107
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004108 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4109 value=self->stack->data[len-1];
4110 len=PyDict_SetItem(self->memo, py_key, value);
4111 Py_DECREF(py_key);
4112 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004113}
4114
4115
4116static int
Tim Peterscba30e22003-02-01 06:24:36 +00004117load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004118{
4119 PyObject *py_key = 0, *value = 0;
4120 long key;
4121 unsigned char c;
4122 char *s;
4123 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004124
Tim Peters0bc93f52003-02-02 18:29:33 +00004125 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004126 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004127
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004128 c = (unsigned char)s[0];
4129 key = (long)c;
4130 c = (unsigned char)s[1];
4131 key |= (long)c << 8;
4132 c = (unsigned char)s[2];
4133 key |= (long)c << 16;
4134 c = (unsigned char)s[3];
4135 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004136
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004137 if (!( py_key = PyInt_FromLong(key))) return -1;
4138 value=self->stack->data[len-1];
4139 len=PyDict_SetItem(self->memo, py_key, value);
4140 Py_DECREF(py_key);
4141 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004142}
4143
4144
4145static int
Tim Peterscba30e22003-02-01 06:24:36 +00004146do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004147{
4148 PyObject *value = 0, *list = 0, *append_method = 0;
4149 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004151 len=self->stack->length;
4152 if (!( len >= x && x > 0 )) return stackUnderflow();
4153 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004154 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004155
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004156 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004157
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004158 if (PyList_Check(list)) {
4159 PyObject *slice;
4160 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004161
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004162 slice=Pdata_popList(self->stack, x);
4163 list_len = PyList_GET_SIZE(list);
4164 i=PyList_SetSlice(list, list_len, list_len, slice);
4165 Py_DECREF(slice);
4166 return i;
4167 }
4168 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004169
Tim Peterscba30e22003-02-01 06:24:36 +00004170 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004171 return -1;
4172
4173 for (i = x; i < len; i++) {
4174 PyObject *junk;
4175
4176 value=self->stack->data[i];
4177 junk=0;
4178 ARG_TUP(self, value);
4179 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004180 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004181 NULL);
4182 FREE_ARG_TUP(self);
4183 }
4184 if (! junk) {
4185 Pdata_clear(self->stack, i+1);
4186 self->stack->length=x;
4187 Py_DECREF(append_method);
4188 return -1;
4189 }
4190 Py_DECREF(junk);
4191 }
4192 self->stack->length=x;
4193 Py_DECREF(append_method);
4194 }
4195
4196 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004197}
4198
4199
4200static int
Tim Peterscba30e22003-02-01 06:24:36 +00004201load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004202{
4203 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004204}
4205
4206
4207static int
Tim Peterscba30e22003-02-01 06:24:36 +00004208load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004209{
4210 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004211}
4212
4213
4214static int
Tim Peterscba30e22003-02-01 06:24:36 +00004215do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004216{
4217 PyObject *value = 0, *key = 0, *dict = 0;
4218 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004219
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004220 if (!( (len=self->stack->length) >= x
4221 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004222
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004223 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004224
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004225 for (i = x+1; i < len; i += 2) {
4226 key =self->stack->data[i-1];
4227 value=self->stack->data[i ];
4228 if (PyObject_SetItem(dict, key, value) < 0) {
4229 r=-1;
4230 break;
4231 }
4232 }
4233
4234 Pdata_clear(self->stack, x);
4235
4236 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004237}
4238
4239
Tim Peters84e87f32001-03-17 04:50:51 +00004240static int
Tim Peterscba30e22003-02-01 06:24:36 +00004241load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004242{
4243 return do_setitems(self, self->stack->length - 2);
4244}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004245
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004246static int
Tim Peterscba30e22003-02-01 06:24:36 +00004247load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004248{
4249 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004250}
4251
Tim Peters84e87f32001-03-17 04:50:51 +00004252
Guido van Rossum60456fd1997-04-09 17:36:32 +00004253static int
Tim Peterscba30e22003-02-01 06:24:36 +00004254load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004255{
Tim Peters080c88b2003-02-15 03:01:11 +00004256 PyObject *state, *inst, *slotstate;
4257 PyObject *__setstate__;
4258 PyObject *d_key, *d_value;
4259 int i;
4260 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004261
Tim Peters080c88b2003-02-15 03:01:11 +00004262 /* Stack is ... instance, state. We want to leave instance at
4263 * the stack top, possibly mutated via instance.__setstate__(state).
4264 */
4265 if (self->stack->length < 2)
4266 return stackUnderflow();
4267 PDATA_POP(self->stack, state);
4268 if (state == NULL)
4269 return -1;
4270 inst = self->stack->data[self->stack->length - 1];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004271
Tim Peters080c88b2003-02-15 03:01:11 +00004272 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4273 if (__setstate__ != NULL) {
4274 PyObject *junk = NULL;
4275
4276 /* The explicit __setstate__ is responsible for everything. */
4277 ARG_TUP(self, state);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004278 if (self->arg) {
4279 junk = PyObject_Call(__setstate__, self->arg, NULL);
4280 FREE_ARG_TUP(self);
4281 }
4282 Py_DECREF(__setstate__);
Tim Peters080c88b2003-02-15 03:01:11 +00004283 if (junk == NULL)
4284 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004285 Py_DECREF(junk);
4286 return 0;
4287 }
Jeremy Hyltonf00368f2003-06-16 20:19:49 +00004288 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4289 return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004290 PyErr_Clear();
Tim Peters080c88b2003-02-15 03:01:11 +00004291
4292 /* A default __setstate__. First see whether state embeds a
4293 * slot state dict too (a proto 2 addition).
4294 */
4295 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4296 PyObject *temp = state;
4297 state = PyTuple_GET_ITEM(temp, 0);
4298 slotstate = PyTuple_GET_ITEM(temp, 1);
4299 Py_INCREF(state);
4300 Py_INCREF(slotstate);
4301 Py_DECREF(temp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004302 }
Tim Peters080c88b2003-02-15 03:01:11 +00004303 else
4304 slotstate = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004305
Tim Peters080c88b2003-02-15 03:01:11 +00004306 /* Set inst.__dict__ from the state dict (if any). */
4307 if (state != Py_None) {
4308 PyObject *dict;
4309 if (! PyDict_Check(state)) {
4310 PyErr_SetString(UnpicklingError, "state is not a "
4311 "dictionary");
4312 goto finally;
4313 }
4314 dict = PyObject_GetAttr(inst, __dict___str);
4315 if (dict == NULL)
4316 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004317
Tim Peters080c88b2003-02-15 03:01:11 +00004318 i = 0;
4319 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4320 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4321 goto finally;
4322 }
4323 Py_DECREF(dict);
4324 }
4325
4326 /* Also set instance attributes from the slotstate dict (if any). */
4327 if (slotstate != NULL) {
4328 if (! PyDict_Check(slotstate)) {
4329 PyErr_SetString(UnpicklingError, "slot state is not "
4330 "a dictionary");
4331 goto finally;
4332 }
4333 i = 0;
4334 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4335 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4336 goto finally;
4337 }
4338 }
4339 res = 0;
4340
4341 finally:
4342 Py_DECREF(state);
4343 Py_XDECREF(slotstate);
4344 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004345}
4346
4347
4348static int
Tim Peterscba30e22003-02-01 06:24:36 +00004349load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004350{
4351 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004352
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004353 /* Note that we split the (pickle.py) stack into two stacks, an
4354 object stack and a mark stack. Here we push a mark onto the
4355 mark stack.
4356 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004357
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004358 if ((self->num_marks + 1) >= self->marks_size) {
4359 s=self->marks_size+20;
4360 if (s <= self->num_marks) s=self->num_marks + 1;
4361 if (self->marks == NULL)
4362 self->marks=(int *)malloc(s * sizeof(int));
4363 else
Tim Peterscba30e22003-02-01 06:24:36 +00004364 self->marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004365 s * sizeof(int));
4366 if (! self->marks) {
4367 PyErr_NoMemory();
4368 return -1;
4369 }
4370 self->marks_size = s;
4371 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004372
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004373 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004374
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004375 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004376}
4377
Guido van Rossum60456fd1997-04-09 17:36:32 +00004378static int
Tim Peterscba30e22003-02-01 06:24:36 +00004379load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004380{
4381 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004382
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004383 PDATA_POP(self->stack, arg_tup);
4384 if (! arg_tup) return -1;
4385 PDATA_POP(self->stack, callable);
4386 if (callable) {
4387 ob = Instance_New(callable, arg_tup);
4388 Py_DECREF(callable);
4389 }
4390 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004391
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004392 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004393
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004394 PDATA_PUSH(self->stack, ob, -1);
4395 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004396}
Tim Peters84e87f32001-03-17 04:50:51 +00004397
Tim Peters4190fb82003-02-02 16:09:05 +00004398/* Just raises an error if we don't know the protocol specified. PROTO
4399 * is the first opcode for protocols >= 2.
4400 */
4401static int
4402load_proto(Unpicklerobject *self)
4403{
4404 int i;
4405 char *protobyte;
4406
4407 i = self->read_func(self, &protobyte, 1);
4408 if (i < 0)
4409 return -1;
4410
4411 i = calc_binint(protobyte, 1);
4412 /* No point checking for < 0, since calc_binint returns an unsigned
4413 * int when chewing on 1 byte.
4414 */
4415 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004416 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004417 return 0;
4418
4419 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4420 return -1;
4421}
4422
Guido van Rossum60456fd1997-04-09 17:36:32 +00004423static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004424load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004425{
4426 PyObject *err = 0, *val = 0;
4427 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004428
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004429 self->num_marks = 0;
4430 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004431
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004432 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004433 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004434 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004435
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004436 switch (s[0]) {
4437 case NONE:
4438 if (load_none(self) < 0)
4439 break;
4440 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004441
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004442 case BININT:
4443 if (load_binint(self) < 0)
4444 break;
4445 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004446
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004447 case BININT1:
4448 if (load_binint1(self) < 0)
4449 break;
4450 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004451
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004452 case BININT2:
4453 if (load_binint2(self) < 0)
4454 break;
4455 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004456
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004457 case INT:
4458 if (load_int(self) < 0)
4459 break;
4460 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004461
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004462 case LONG:
4463 if (load_long(self) < 0)
4464 break;
4465 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004466
Tim Petersee1a53c2003-02-02 02:57:53 +00004467 case LONG1:
4468 if (load_counted_long(self, 1) < 0)
4469 break;
4470 continue;
4471
4472 case LONG4:
4473 if (load_counted_long(self, 4) < 0)
4474 break;
4475 continue;
4476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004477 case FLOAT:
4478 if (load_float(self) < 0)
4479 break;
4480 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004481
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004482 case BINFLOAT:
4483 if (load_binfloat(self) < 0)
4484 break;
4485 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004486
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004487 case BINSTRING:
4488 if (load_binstring(self) < 0)
4489 break;
4490 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004491
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004492 case SHORT_BINSTRING:
4493 if (load_short_binstring(self) < 0)
4494 break;
4495 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004496
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004497 case STRING:
4498 if (load_string(self) < 0)
4499 break;
4500 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004501
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004502#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004503 case UNICODE:
4504 if (load_unicode(self) < 0)
4505 break;
4506 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004507
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004508 case BINUNICODE:
4509 if (load_binunicode(self) < 0)
4510 break;
4511 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004512#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004513
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004514 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004515 if (load_counted_tuple(self, 0) < 0)
4516 break;
4517 continue;
4518
4519 case TUPLE1:
4520 if (load_counted_tuple(self, 1) < 0)
4521 break;
4522 continue;
4523
4524 case TUPLE2:
4525 if (load_counted_tuple(self, 2) < 0)
4526 break;
4527 continue;
4528
4529 case TUPLE3:
4530 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004531 break;
4532 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004533
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004534 case TUPLE:
4535 if (load_tuple(self) < 0)
4536 break;
4537 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004538
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004539 case EMPTY_LIST:
4540 if (load_empty_list(self) < 0)
4541 break;
4542 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004543
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004544 case LIST:
4545 if (load_list(self) < 0)
4546 break;
4547 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004548
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004549 case EMPTY_DICT:
4550 if (load_empty_dict(self) < 0)
4551 break;
4552 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004553
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004554 case DICT:
4555 if (load_dict(self) < 0)
4556 break;
4557 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004558
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004559 case OBJ:
4560 if (load_obj(self) < 0)
4561 break;
4562 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004563
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004564 case INST:
4565 if (load_inst(self) < 0)
4566 break;
4567 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004568
Tim Peterseab7db32003-02-13 18:24:14 +00004569 case NEWOBJ:
4570 if (load_newobj(self) < 0)
4571 break;
4572 continue;
4573
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004574 case GLOBAL:
4575 if (load_global(self) < 0)
4576 break;
4577 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004578
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004579 case APPEND:
4580 if (load_append(self) < 0)
4581 break;
4582 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004583
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004584 case APPENDS:
4585 if (load_appends(self) < 0)
4586 break;
4587 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004588
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004589 case BUILD:
4590 if (load_build(self) < 0)
4591 break;
4592 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004593
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004594 case DUP:
4595 if (load_dup(self) < 0)
4596 break;
4597 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004598
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004599 case BINGET:
4600 if (load_binget(self) < 0)
4601 break;
4602 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004603
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004604 case LONG_BINGET:
4605 if (load_long_binget(self) < 0)
4606 break;
4607 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004608
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004609 case GET:
4610 if (load_get(self) < 0)
4611 break;
4612 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004613
Tim Peters2d629652003-02-04 05:06:17 +00004614 case EXT1:
4615 if (load_extension(self, 1) < 0)
4616 break;
4617 continue;
4618
4619 case EXT2:
4620 if (load_extension(self, 2) < 0)
4621 break;
4622 continue;
4623
4624 case EXT4:
4625 if (load_extension(self, 4) < 0)
4626 break;
4627 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004628 case MARK:
4629 if (load_mark(self) < 0)
4630 break;
4631 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004632
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004633 case BINPUT:
4634 if (load_binput(self) < 0)
4635 break;
4636 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004637
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004638 case LONG_BINPUT:
4639 if (load_long_binput(self) < 0)
4640 break;
4641 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004642
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004643 case PUT:
4644 if (load_put(self) < 0)
4645 break;
4646 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004648 case POP:
4649 if (load_pop(self) < 0)
4650 break;
4651 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004652
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004653 case POP_MARK:
4654 if (load_pop_mark(self) < 0)
4655 break;
4656 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004658 case SETITEM:
4659 if (load_setitem(self) < 0)
4660 break;
4661 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004662
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004663 case SETITEMS:
4664 if (load_setitems(self) < 0)
4665 break;
4666 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004667
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004668 case STOP:
4669 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004670
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004671 case PERSID:
4672 if (load_persid(self) < 0)
4673 break;
4674 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004675
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004676 case BINPERSID:
4677 if (load_binpersid(self) < 0)
4678 break;
4679 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004680
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004681 case REDUCE:
4682 if (load_reduce(self) < 0)
4683 break;
4684 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004685
Tim Peters4190fb82003-02-02 16:09:05 +00004686 case PROTO:
4687 if (load_proto(self) < 0)
4688 break;
4689 continue;
4690
Tim Peters3c67d792003-02-02 17:59:11 +00004691 case NEWTRUE:
4692 if (load_bool(self, Py_True) < 0)
4693 break;
4694 continue;
4695
4696 case NEWFALSE:
4697 if (load_bool(self, Py_False) < 0)
4698 break;
4699 continue;
4700
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004701 case '\0':
4702 /* end of file */
4703 PyErr_SetNone(PyExc_EOFError);
4704 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004705
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004706 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004707 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004708 "invalid load key, '%s'.",
4709 "c", s[0]);
4710 return NULL;
4711 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004712
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004713 break;
4714 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004715
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004716 if ((err = PyErr_Occurred())) {
4717 if (err == PyExc_EOFError) {
4718 PyErr_SetNone(PyExc_EOFError);
4719 }
4720 return NULL;
4721 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004722
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004723 PDATA_POP(self->stack, val);
4724 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004725}
Tim Peters84e87f32001-03-17 04:50:51 +00004726
Guido van Rossum60456fd1997-04-09 17:36:32 +00004727
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004728/* No-load functions to support noload, which is used to
4729 find persistent references. */
4730
4731static int
Tim Peterscba30e22003-02-01 06:24:36 +00004732noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004733{
4734 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004735
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004736 if ((i = marker(self)) < 0) return -1;
4737 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004738}
4739
4740
4741static int
Tim Peterscba30e22003-02-01 06:24:36 +00004742noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004743{
4744 int i;
4745 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004746
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004747 if ((i = marker(self)) < 0) return -1;
4748 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004749 if (self->readline_func(self, &s) < 0) return -1;
4750 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004751 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004752 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004753}
4754
4755static int
Tim Peterseab7db32003-02-13 18:24:14 +00004756noload_newobj(Unpicklerobject *self)
4757{
4758 PyObject *obj;
4759
4760 PDATA_POP(self->stack, obj); /* pop argtuple */
4761 if (obj == NULL) return -1;
4762 Py_DECREF(obj);
4763
4764 PDATA_POP(self->stack, obj); /* pop cls */
4765 if (obj == NULL) return -1;
4766 Py_DECREF(obj);
4767
4768 PDATA_APPEND(self->stack, Py_None, -1);
4769 return 0;
4770}
4771
4772static int
Tim Peterscba30e22003-02-01 06:24:36 +00004773noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004774{
4775 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004776
Tim Peters0bc93f52003-02-02 18:29:33 +00004777 if (self->readline_func(self, &s) < 0) return -1;
4778 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004779 PDATA_APPEND(self->stack, Py_None,-1);
4780 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004781}
4782
4783static int
Tim Peterscba30e22003-02-01 06:24:36 +00004784noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004785{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004786
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004787 if (self->stack->length < 2) return stackUnderflow();
4788 Pdata_clear(self->stack, self->stack->length-2);
4789 PDATA_APPEND(self->stack, Py_None,-1);
4790 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004791}
4792
4793static int
4794noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004795
Guido van Rossum053b8df1998-11-25 16:18:00 +00004796 if (self->stack->length < 1) return stackUnderflow();
4797 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004798 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004799}
4800
Tim Peters2d629652003-02-04 05:06:17 +00004801static int
4802noload_extension(Unpicklerobject *self, int nbytes)
4803{
4804 char *codebytes;
4805
4806 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4807 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4808 PDATA_APPEND(self->stack, Py_None, -1);
4809 return 0;
4810}
4811
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004812
4813static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004814noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004815{
4816 PyObject *err = 0, *val = 0;
4817 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004818
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004819 self->num_marks = 0;
4820 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004821
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004822 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004823 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004824 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004825
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004826 switch (s[0]) {
4827 case NONE:
4828 if (load_none(self) < 0)
4829 break;
4830 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004831
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004832 case BININT:
4833 if (load_binint(self) < 0)
4834 break;
4835 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004836
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004837 case BININT1:
4838 if (load_binint1(self) < 0)
4839 break;
4840 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004841
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004842 case BININT2:
4843 if (load_binint2(self) < 0)
4844 break;
4845 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004846
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004847 case INT:
4848 if (load_int(self) < 0)
4849 break;
4850 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004851
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004852 case LONG:
4853 if (load_long(self) < 0)
4854 break;
4855 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004856
Tim Peters4190fb82003-02-02 16:09:05 +00004857 case LONG1:
4858 if (load_counted_long(self, 1) < 0)
4859 break;
4860 continue;
4861
4862 case LONG4:
4863 if (load_counted_long(self, 4) < 0)
4864 break;
4865 continue;
4866
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004867 case FLOAT:
4868 if (load_float(self) < 0)
4869 break;
4870 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004871
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004872 case BINFLOAT:
4873 if (load_binfloat(self) < 0)
4874 break;
4875 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004876
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004877 case BINSTRING:
4878 if (load_binstring(self) < 0)
4879 break;
4880 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004881
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004882 case SHORT_BINSTRING:
4883 if (load_short_binstring(self) < 0)
4884 break;
4885 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004886
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004887 case STRING:
4888 if (load_string(self) < 0)
4889 break;
4890 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004891
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004892#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004893 case UNICODE:
4894 if (load_unicode(self) < 0)
4895 break;
4896 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004897
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004898 case BINUNICODE:
4899 if (load_binunicode(self) < 0)
4900 break;
4901 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004902#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004903
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004904 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004905 if (load_counted_tuple(self, 0) < 0)
4906 break;
4907 continue;
4908
4909 case TUPLE1:
4910 if (load_counted_tuple(self, 1) < 0)
4911 break;
4912 continue;
4913
4914 case TUPLE2:
4915 if (load_counted_tuple(self, 2) < 0)
4916 break;
4917 continue;
4918
4919 case TUPLE3:
4920 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004921 break;
4922 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004923
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004924 case TUPLE:
4925 if (load_tuple(self) < 0)
4926 break;
4927 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004928
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004929 case EMPTY_LIST:
4930 if (load_empty_list(self) < 0)
4931 break;
4932 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004933
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004934 case LIST:
4935 if (load_list(self) < 0)
4936 break;
4937 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004938
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004939 case EMPTY_DICT:
4940 if (load_empty_dict(self) < 0)
4941 break;
4942 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004943
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004944 case DICT:
4945 if (load_dict(self) < 0)
4946 break;
4947 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004948
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004949 case OBJ:
4950 if (noload_obj(self) < 0)
4951 break;
4952 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004953
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004954 case INST:
4955 if (noload_inst(self) < 0)
4956 break;
4957 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004958
Tim Peterseab7db32003-02-13 18:24:14 +00004959 case NEWOBJ:
4960 if (noload_newobj(self) < 0)
4961 break;
4962 continue;
4963
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004964 case GLOBAL:
4965 if (noload_global(self) < 0)
4966 break;
4967 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004968
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004969 case APPEND:
4970 if (load_append(self) < 0)
4971 break;
4972 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004973
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004974 case APPENDS:
4975 if (load_appends(self) < 0)
4976 break;
4977 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004978
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004979 case BUILD:
4980 if (noload_build(self) < 0)
4981 break;
4982 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004983
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004984 case DUP:
4985 if (load_dup(self) < 0)
4986 break;
4987 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004988
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004989 case BINGET:
4990 if (load_binget(self) < 0)
4991 break;
4992 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004993
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004994 case LONG_BINGET:
4995 if (load_long_binget(self) < 0)
4996 break;
4997 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004998
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004999 case GET:
5000 if (load_get(self) < 0)
5001 break;
5002 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005003
Tim Peters2d629652003-02-04 05:06:17 +00005004 case EXT1:
5005 if (noload_extension(self, 1) < 0)
5006 break;
5007 continue;
5008
5009 case EXT2:
5010 if (noload_extension(self, 2) < 0)
5011 break;
5012 continue;
5013
5014 case EXT4:
5015 if (noload_extension(self, 4) < 0)
5016 break;
5017 continue;
5018
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005019 case MARK:
5020 if (load_mark(self) < 0)
5021 break;
5022 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005023
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005024 case BINPUT:
5025 if (load_binput(self) < 0)
5026 break;
5027 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005028
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005029 case LONG_BINPUT:
5030 if (load_long_binput(self) < 0)
5031 break;
5032 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00005033
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005034 case PUT:
5035 if (load_put(self) < 0)
5036 break;
5037 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005038
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005039 case POP:
5040 if (load_pop(self) < 0)
5041 break;
5042 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005043
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005044 case POP_MARK:
5045 if (load_pop_mark(self) < 0)
5046 break;
5047 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005048
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005049 case SETITEM:
5050 if (load_setitem(self) < 0)
5051 break;
5052 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005053
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005054 case SETITEMS:
5055 if (load_setitems(self) < 0)
5056 break;
5057 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005058
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005059 case STOP:
5060 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005061
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005062 case PERSID:
5063 if (load_persid(self) < 0)
5064 break;
5065 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005066
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005067 case BINPERSID:
5068 if (load_binpersid(self) < 0)
5069 break;
5070 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005071
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005072 case REDUCE:
5073 if (noload_reduce(self) < 0)
5074 break;
5075 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005076
Tim Peters4190fb82003-02-02 16:09:05 +00005077 case PROTO:
5078 if (load_proto(self) < 0)
5079 break;
5080 continue;
5081
Tim Peters3c67d792003-02-02 17:59:11 +00005082 case NEWTRUE:
5083 if (load_bool(self, Py_True) < 0)
5084 break;
5085 continue;
5086
5087 case NEWFALSE:
5088 if (load_bool(self, Py_False) < 0)
5089 break;
5090 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005091 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005092 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005093 "invalid load key, '%s'.",
5094 "c", s[0]);
5095 return NULL;
5096 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005097
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005098 break;
5099 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005100
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005101 if ((err = PyErr_Occurred())) {
5102 if (err == PyExc_EOFError) {
5103 PyErr_SetNone(PyExc_EOFError);
5104 }
5105 return NULL;
5106 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005107
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005108 PDATA_POP(self->stack, val);
5109 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005110}
Tim Peters84e87f32001-03-17 04:50:51 +00005111
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005112
Guido van Rossum60456fd1997-04-09 17:36:32 +00005113static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005114Unpickler_load(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005115{
Tim Peterscba30e22003-02-01 06:24:36 +00005116 if (!( PyArg_ParseTuple(args, ":load")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005117 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005118
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005119 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005120}
5121
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005122static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005123Unpickler_noload(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005124{
Tim Peterscba30e22003-02-01 06:24:36 +00005125 if (!( PyArg_ParseTuple(args, ":noload")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005126 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005127
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005128 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005129}
5130
Guido van Rossum60456fd1997-04-09 17:36:32 +00005131
5132static struct PyMethodDef Unpickler_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005133 {"load", (PyCFunction)Unpickler_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005134 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005135 },
Neal Norwitzb0493252002-03-31 14:44:22 +00005136 {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005137 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005138 "noload() -- not load a pickle, but go through most of the motions\n"
5139 "\n"
5140 "This function can be used to read past a pickle without instantiating\n"
5141 "any objects or importing any modules. It can also be used to find all\n"
5142 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005143 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005144 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005145 {NULL, NULL} /* sentinel */
5146};
5147
5148
5149static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005150newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005151{
5152 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005153
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005154 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005155 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005156
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005157 self->file = NULL;
5158 self->arg = NULL;
5159 self->stack = (Pdata*)Pdata_New();
5160 self->pers_func = NULL;
5161 self->last_string = NULL;
5162 self->marks = NULL;
5163 self->num_marks = 0;
5164 self->marks_size = 0;
5165 self->buf_size = 0;
5166 self->read = NULL;
5167 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005168 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005169
Tim Peterscba30e22003-02-01 06:24:36 +00005170 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005171 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005172
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005173 Py_INCREF(f);
5174 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005175
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005176 /* Set read, readline based on type of f */
5177 if (PyFile_Check(f)) {
5178 self->fp = PyFile_AsFile(f);
5179 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005180 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005181 "I/O operation on closed file");
5182 goto err;
5183 }
5184 self->read_func = read_file;
5185 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005186 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005187 else if (PycStringIO_InputCheck(f)) {
5188 self->fp = NULL;
5189 self->read_func = read_cStringIO;
5190 self->readline_func = readline_cStringIO;
5191 }
5192 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005193
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005194 self->fp = NULL;
5195 self->read_func = read_other;
5196 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005197
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005198 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5199 (self->read = PyObject_GetAttr(f, read_str)))) {
5200 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005201 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005202 "argument must have 'read' and "
5203 "'readline' attributes" );
5204 goto err;
5205 }
5206 }
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005207 PyObject_GC_Track(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005208
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005209 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005210
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005211 err:
5212 Py_DECREF((PyObject *)self);
5213 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005214}
5215
5216
5217static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005218get_Unpickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005219{
5220 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00005221
Tim Peterscba30e22003-02-01 06:24:36 +00005222 if (!( PyArg_ParseTuple(args, "O:Unpickler", &file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005223 return NULL;
5224 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005225}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005226
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005227
Guido van Rossum60456fd1997-04-09 17:36:32 +00005228static void
Tim Peterscba30e22003-02-01 06:24:36 +00005229Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005230{
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005231 PyObject_GC_UnTrack((PyObject *)self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005232 Py_XDECREF(self->readline);
5233 Py_XDECREF(self->read);
5234 Py_XDECREF(self->file);
5235 Py_XDECREF(self->memo);
5236 Py_XDECREF(self->stack);
5237 Py_XDECREF(self->pers_func);
5238 Py_XDECREF(self->arg);
5239 Py_XDECREF(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005240 Py_XDECREF(self->find_class);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005241
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005242 if (self->marks) {
5243 free(self->marks);
5244 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005245
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005246 if (self->buf_size) {
5247 free(self->buf);
5248 }
Tim Peters84e87f32001-03-17 04:50:51 +00005249
Tim Peters3cfe7542003-05-21 21:29:48 +00005250 self->ob_type->tp_free((PyObject *)self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005251}
5252
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005253static int
5254Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5255{
5256 int err;
5257
5258#define VISIT(SLOT) \
5259 if (SLOT) { \
5260 err = visit((PyObject *)(SLOT), arg); \
5261 if (err) \
5262 return err; \
5263 }
5264 VISIT(self->readline);
5265 VISIT(self->read);
5266 VISIT(self->file);
5267 VISIT(self->memo);
5268 VISIT(self->stack);
5269 VISIT(self->pers_func);
5270 VISIT(self->arg);
5271 VISIT(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005272 VISIT(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005273#undef VISIT
5274 return 0;
5275}
5276
5277static int
5278Unpickler_clear(Unpicklerobject *self)
5279{
5280#define CLEAR(SLOT) Py_XDECREF(SLOT); SLOT = NULL
5281 CLEAR(self->readline);
5282 CLEAR(self->read);
5283 CLEAR(self->file);
5284 CLEAR(self->memo);
5285 CLEAR(self->stack);
5286 CLEAR(self->pers_func);
5287 CLEAR(self->arg);
5288 CLEAR(self->last_string);
Jeremy Hyltonfff093f2003-07-11 19:42:49 +00005289 CLEAR(self->find_class);
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005290#undef CLEAR
5291 return 0;
5292}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005293
5294static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005295Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005296{
5297 if (!strcmp(name, "persistent_load")) {
5298 if (!self->pers_func) {
5299 PyErr_SetString(PyExc_AttributeError, name);
5300 return NULL;
5301 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005302
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005303 Py_INCREF(self->pers_func);
5304 return self->pers_func;
5305 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005306
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005307 if (!strcmp(name, "find_global")) {
5308 if (!self->find_class) {
5309 PyErr_SetString(PyExc_AttributeError, name);
5310 return NULL;
5311 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005312
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005313 Py_INCREF(self->find_class);
5314 return self->find_class;
5315 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005316
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005317 if (!strcmp(name, "memo")) {
5318 if (!self->memo) {
5319 PyErr_SetString(PyExc_AttributeError, name);
5320 return NULL;
5321 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005322
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005323 Py_INCREF(self->memo);
5324 return self->memo;
5325 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005326
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005327 if (!strcmp(name, "UnpicklingError")) {
5328 Py_INCREF(UnpicklingError);
5329 return UnpicklingError;
5330 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005331
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005332 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005333}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005334
Guido van Rossum60456fd1997-04-09 17:36:32 +00005335
5336static int
Tim Peterscba30e22003-02-01 06:24:36 +00005337Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005338{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005339
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005340 if (!strcmp(name, "persistent_load")) {
5341 Py_XDECREF(self->pers_func);
5342 self->pers_func = value;
5343 Py_XINCREF(value);
5344 return 0;
5345 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005346
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005347 if (!strcmp(name, "find_global")) {
5348 Py_XDECREF(self->find_class);
5349 self->find_class = value;
5350 Py_XINCREF(value);
5351 return 0;
5352 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005353
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005354 if (! value) {
5355 PyErr_SetString(PyExc_TypeError,
5356 "attribute deletion is not supported");
5357 return -1;
5358 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005359
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005360 if (strcmp(name, "memo") == 0) {
5361 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005362 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005363 "memo must be a dictionary");
5364 return -1;
5365 }
5366 Py_XDECREF(self->memo);
5367 self->memo = value;
5368 Py_INCREF(value);
5369 return 0;
5370 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005371
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005372 PyErr_SetString(PyExc_AttributeError, name);
5373 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005374}
5375
Tim Peters5bd2a792003-02-01 16:45:06 +00005376/* ---------------------------------------------------------------------------
5377 * Module-level functions.
5378 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005379
Tim Peters5bd2a792003-02-01 16:45:06 +00005380/* dump(obj, file, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005381static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005382cpm_dump(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005383{
5384 PyObject *ob, *file, *res = NULL;
5385 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005386 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005387
Tim Peters5bd2a792003-02-01 16:45:06 +00005388 if (!( PyArg_ParseTuple(args, "OO|i", &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005389 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005390
Tim Peters5bd2a792003-02-01 16:45:06 +00005391 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005392 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005393
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005394 if (dump(pickler, ob) < 0)
5395 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005396
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005397 Py_INCREF(Py_None);
5398 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005399
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005400 finally:
5401 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005402
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005403 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005404}
5405
5406
Tim Peters5bd2a792003-02-01 16:45:06 +00005407/* dumps(obj, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005408static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005409cpm_dumps(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005410{
5411 PyObject *ob, *file = 0, *res = NULL;
5412 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005413 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005414
Tim Peters5bd2a792003-02-01 16:45:06 +00005415 if (!( PyArg_ParseTuple(args, "O|i:dumps", &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005416 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005417
Tim Peterscba30e22003-02-01 06:24:36 +00005418 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005419 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005420
Tim Peters5bd2a792003-02-01 16:45:06 +00005421 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005422 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005424 if (dump(pickler, ob) < 0)
5425 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005426
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005427 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005428
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005429 finally:
5430 Py_XDECREF(pickler);
5431 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005432
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005433 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005434}
5435
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005436
Tim Peters5bd2a792003-02-01 16:45:06 +00005437/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005438static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005439cpm_load(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005440{
5441 Unpicklerobject *unpickler = 0;
5442 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005443
Tim Peterscba30e22003-02-01 06:24:36 +00005444 if (!( PyArg_ParseTuple(args, "O:load", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005445 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005446
Tim Peterscba30e22003-02-01 06:24:36 +00005447 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005448 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005449
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005450 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005451
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005452 finally:
5453 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005454
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005455 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005456}
5457
5458
Tim Peters5bd2a792003-02-01 16:45:06 +00005459/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005460static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005461cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005462{
5463 PyObject *ob, *file = 0, *res = NULL;
5464 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005465
Tim Peterscba30e22003-02-01 06:24:36 +00005466 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005467 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005468
Tim Peterscba30e22003-02-01 06:24:36 +00005469 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005470 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005471
Tim Peterscba30e22003-02-01 06:24:36 +00005472 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005473 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005474
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005475 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005477 finally:
5478 Py_XDECREF(file);
5479 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005480
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005481 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005482}
5483
5484
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005485PyDoc_STRVAR(Unpicklertype__doc__,
5486"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005487
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005488static PyTypeObject Unpicklertype = {
5489 PyObject_HEAD_INIT(NULL)
Jeremy Hylton7b5ce7f2003-04-09 21:25:30 +00005490 0, /*ob_size*/
5491 "cPickle.Unpickler", /*tp_name*/
5492 sizeof(Unpicklerobject), /*tp_basicsize*/
5493 0,
5494 (destructor)Unpickler_dealloc, /* tp_dealloc */
5495 0, /* tp_print */
5496 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5497 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5498 0, /* tp_compare */
5499 0, /* tp_repr */
5500 0, /* tp_as_number */
5501 0, /* tp_as_sequence */
5502 0, /* tp_as_mapping */
5503 0, /* tp_hash */
5504 0, /* tp_call */
5505 0, /* tp_str */
5506 0, /* tp_getattro */
5507 0, /* tp_setattro */
5508 0, /* tp_as_buffer */
5509 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5510 Unpicklertype__doc__, /* tp_doc */
5511 (traverseproc)Unpickler_traverse, /* tp_traverse */
5512 (inquiry)Unpickler_clear, /* tp_clear */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005513};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005514
Guido van Rossum60456fd1997-04-09 17:36:32 +00005515static struct PyMethodDef cPickle_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005516 {"dump", (PyCFunction)cpm_dump, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005517 PyDoc_STR("dump(object, file, proto=0) -- "
5518 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005519 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005520 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005521 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005522
Neal Norwitzb0493252002-03-31 14:44:22 +00005523 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005524 PyDoc_STR("dumps(object, proto=0) -- "
5525 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005526 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005527 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005528 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005529
Neal Norwitzb0493252002-03-31 14:44:22 +00005530 {"load", (PyCFunction)cpm_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005531 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005532
Neal Norwitzb0493252002-03-31 14:44:22 +00005533 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005534 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005535
Neal Norwitzb0493252002-03-31 14:44:22 +00005536 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005537 PyDoc_STR("Pickler(file, proto=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005538 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005539 "This takes a file-like object for writing a pickle data stream.\n"
5540 "The optional proto argument tells the pickler to use the given\n"
5541 "protocol; supported protocols are 0, 1, 2. The default\n"
5542 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5543 "only protocol that can be written to a file opened in text\n"
5544 "mode and read back successfully. When using a protocol higher\n"
5545 "than 0, make sure the file is opened in binary mode, both when\n"
5546 "pickling and unpickling.)\n"
5547 "\n"
5548 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5549 "more efficient than protocol 1.\n"
5550 "\n"
5551 "Specifying a negative protocol version selects the highest\n"
5552 "protocol version supported. The higher the protocol used, the\n"
5553 "more recent the version of Python needed to read the pickle\n"
5554 "produced.\n"
5555 "\n"
5556 "The file parameter must have a write() method that accepts a single\n"
5557 "string argument. It can thus be an open file object, a StringIO\n"
5558 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005559 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005560
Neal Norwitzb0493252002-03-31 14:44:22 +00005561 {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005562 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5563
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005564 { NULL, NULL }
5565};
5566
Guido van Rossum60456fd1997-04-09 17:36:32 +00005567static int
Tim Peterscba30e22003-02-01 06:24:36 +00005568init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005569{
5570 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005571
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005572#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005573
Tim Peters3cfe7542003-05-21 21:29:48 +00005574 if (PyType_Ready(&Unpicklertype) < 0)
5575 return -1;
5576 if (PyType_Ready(&Picklertype) < 0)
5577 return -1;
5578
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005579 INIT_STR(__class__);
5580 INIT_STR(__getinitargs__);
5581 INIT_STR(__dict__);
5582 INIT_STR(__getstate__);
5583 INIT_STR(__setstate__);
5584 INIT_STR(__name__);
5585 INIT_STR(__main__);
5586 INIT_STR(__reduce__);
Guido van Rossumb289b872003-02-19 01:45:13 +00005587 INIT_STR(__reduce_ex__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005588 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005589 INIT_STR(append);
5590 INIT_STR(read);
5591 INIT_STR(readline);
5592 INIT_STR(copy_reg);
5593 INIT_STR(dispatch_table);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005594 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005595
Tim Peterscba30e22003-02-01 06:24:36 +00005596 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005597 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005598
Tim Peters1f1b2d22003-02-01 02:16:37 +00005599 /* This is special because we want to use a different
5600 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005601 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005602 if (!dispatch_table) return -1;
5603
5604 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005605 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005606 if (!extension_registry) return -1;
5607
5608 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005609 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005610 if (!inverted_registry) return -1;
5611
5612 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005613 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005614 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005615
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005616 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005617
Tim Peters731098b2003-02-04 20:56:09 +00005618 if (!(empty_tuple = PyTuple_New(0)))
5619 return -1;
5620
5621 two_tuple = PyTuple_New(2);
5622 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005623 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005624 /* We use this temp container with no regard to refcounts, or to
5625 * keeping containees alive. Exempt from GC, because we don't
5626 * want anything looking at two_tuple() by magic.
5627 */
5628 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005629
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005630 /* Ugh */
5631 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5632 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5633 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005634
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005635 if (!( t=PyDict_New())) return -1;
5636 if (!( r=PyRun_String(
5637 "def __init__(self, *args): self.args=args\n\n"
5638 "def __str__(self):\n"
5639 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5640 Py_file_input,
5641 module_dict, t) )) return -1;
5642 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005643
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005644 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005645 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005646 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005647
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005648 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005649
Tim Peterscba30e22003-02-01 06:24:36 +00005650 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005651 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005652 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005653 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005654
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005655 if (!( t=PyDict_New())) return -1;
5656 if (!( r=PyRun_String(
5657 "def __init__(self, *args): self.args=args\n\n"
5658 "def __str__(self):\n"
5659 " a=self.args\n"
5660 " a=a and type(a[0]) or '(what)'\n"
5661 " return 'Cannot pickle %s objects' % a\n"
5662 , Py_file_input,
5663 module_dict, t) )) return -1;
5664 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005665
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005666 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005667 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005668 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005669
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005670 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005671
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005672 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005673 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005674 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005675
Martin v. Löwis658009a2002-09-16 17:26:24 +00005676 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5677 UnpicklingError, NULL)))
5678 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005679
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005680 if (PyDict_SetItemString(module_dict, "PickleError",
5681 PickleError) < 0)
5682 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005683
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005684 if (PyDict_SetItemString(module_dict, "PicklingError",
5685 PicklingError) < 0)
5686 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005687
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005688 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5689 UnpicklingError) < 0)
5690 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005691
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005692 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5693 UnpickleableError) < 0)
5694 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005696 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5697 BadPickleGet) < 0)
5698 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005699
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005700 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005701
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005702 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005703}
5704
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005705#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5706#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005707#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005708PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005709initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005710{
5711 PyObject *m, *d, *di, *v, *k;
5712 int i;
Tim Peters5b7da392003-02-04 00:21:07 +00005713 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005714 PyObject *format_version;
5715 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005716
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005717 Picklertype.ob_type = &PyType_Type;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005718 Unpicklertype.ob_type = &PyType_Type;
5719 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005720
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005721 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005722 * so we're forced to use a temporary dictionary. :(
5723 */
5724 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005725 if (!di) return;
5726 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005727
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005728 /* Create the module and add the functions */
5729 m = Py_InitModule4("cPickle", cPickle_methods,
5730 cPickle_module_documentation,
5731 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005732
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005733 /* Add some symbolic constants to the module */
5734 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005735 v = PyString_FromString(rev);
5736 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005737 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005738
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005739 /* Copy data from di. Waaa. */
5740 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5741 if (PyObject_SetItem(d, k, v) < 0) {
5742 Py_DECREF(di);
5743 return;
5744 }
5745 }
5746 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005747
Tim Peters8587b3c2003-02-13 15:44:41 +00005748 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5749 if (i < 0)
5750 return;
5751
Tim Peters5b7da392003-02-04 00:21:07 +00005752 /* These are purely informational; no code uses them. */
5753 /* File format version we write. */
5754 format_version = PyString_FromString("2.0");
5755 /* Format versions we can read. */
5756 compatible_formats = Py_BuildValue("[sssss]",
5757 "1.0", /* Original protocol 0 */
5758 "1.1", /* Protocol 0 + INST */
5759 "1.2", /* Original protocol 1 */
5760 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005761 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005762 PyDict_SetItemString(d, "format_version", format_version);
5763 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5764 Py_XDECREF(format_version);
5765 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005766}