blob: c131e9ea06434a74b0b24c1f2692c7b99182d2c5 [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,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000124 *write_str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000125 *read_str, *readline_str, *__main___str, *__basicnew___str,
Tim Peters1f1b2d22003-02-01 02:16:37 +0000126 *copy_reg_str, *dispatch_table_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000127
Guido van Rossum053b8df1998-11-25 16:18:00 +0000128/*************************************************************************
129 Internal Data type for pickle data. */
130
131typedef struct {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000132 PyObject_HEAD
Tim Peters1d63c9f2003-02-02 20:29:39 +0000133 int length; /* number of initial slots in data currently used */
134 int size; /* number of slots in data allocated */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000135 PyObject **data;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000136} Pdata;
137
Tim Peters84e87f32001-03-17 04:50:51 +0000138static void
Tim Peterscba30e22003-02-01 06:24:36 +0000139Pdata_dealloc(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000140{
141 int i;
142 PyObject **p;
Tim Peterscba30e22003-02-01 06:24:36 +0000143
Tim Peters1d63c9f2003-02-02 20:29:39 +0000144 for (i = self->length, p = self->data; --i >= 0; p++) {
145 Py_DECREF(*p);
146 }
147 if (self->data)
148 free(self->data);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000149 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000150}
151
152static PyTypeObject PdataType = {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000153 PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0,
154 (destructor)Pdata_dealloc,
155 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
Guido van Rossum053b8df1998-11-25 16:18:00 +0000156};
157
158#define Pdata_Check(O) ((O)->ob_type == &PdataType)
159
160static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000161Pdata_New(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000162{
163 Pdata *self;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000164
Tim Peters1d63c9f2003-02-02 20:29:39 +0000165 if (!(self = PyObject_New(Pdata, &PdataType)))
166 return NULL;
167 self->size = 8;
168 self->length = 0;
169 self->data = malloc(self->size * sizeof(PyObject*));
170 if (self->data)
171 return (PyObject*)self;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000172 Py_DECREF(self);
173 return PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +0000174}
175
Tim Peters84e87f32001-03-17 04:50:51 +0000176static int
Tim Peterscba30e22003-02-01 06:24:36 +0000177stackUnderflow(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000178{
179 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
180 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000181}
182
Tim Peters1d63c9f2003-02-02 20:29:39 +0000183/* Retain only the initial clearto items. If clearto >= the current
184 * number of items, this is a (non-erroneous) NOP.
185 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000186static int
Tim Peterscba30e22003-02-01 06:24:36 +0000187Pdata_clear(Pdata *self, int clearto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000188{
189 int i;
190 PyObject **p;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000191
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000192 if (clearto < 0) return stackUnderflow();
193 if (clearto >= self->length) return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000194
Tim Peters1d63c9f2003-02-02 20:29:39 +0000195 for (i = self->length, p = self->data + clearto;
196 --i >= clearto;
197 p++) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000198 Py_DECREF(*p);
Tim Peters1d63c9f2003-02-02 20:29:39 +0000199 }
200 self->length = clearto;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000201
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000202 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000203}
204
Tim Peters84e87f32001-03-17 04:50:51 +0000205static int
Tim Peterscba30e22003-02-01 06:24:36 +0000206Pdata_grow(Pdata *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000207{
Tim Peters1d63c9f2003-02-02 20:29:39 +0000208 int bigger;
209 size_t nbytes;
210
Tim Peters1d63c9f2003-02-02 20:29:39 +0000211 bigger = self->size << 1;
Tim Peterse0a39072003-02-03 15:45:56 +0000212 if (bigger <= 0) /* was 0, or new value overflows */
Tim Peters1d63c9f2003-02-02 20:29:39 +0000213 goto nomemory;
214 if ((int)(size_t)bigger != bigger)
215 goto nomemory;
216 nbytes = (size_t)bigger * sizeof(PyObject *);
217 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
218 goto nomemory;
219 self->data = realloc(self->data, nbytes);
220 if (self->data == NULL)
221 goto nomemory;
222 self->size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000223 return 0;
Tim Peters1d63c9f2003-02-02 20:29:39 +0000224
225 nomemory:
226 self->size = 0;
227 PyErr_NoMemory();
228 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000229}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000230
Tim Peterse0a39072003-02-03 15:45:56 +0000231/* D is a Pdata*. Pop the topmost element and store it into V, which
232 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
Tim Peters1d63c9f2003-02-02 20:29:39 +0000233 * is raised and V is set to NULL. D and V may be evaluated several times.
234 */
235#define PDATA_POP(D, V) { \
Tim Peterse0a39072003-02-03 15:45:56 +0000236 if ((D)->length) \
237 (V) = (D)->data[--((D)->length)]; \
238 else { \
239 PyErr_SetString(UnpicklingError, "bad pickle data"); \
240 (V) = NULL; \
241 } \
Guido van Rossum053b8df1998-11-25 16:18:00 +0000242}
243
Tim Peterse0a39072003-02-03 15:45:56 +0000244/* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
245 * D. If the Pdata stack can't be grown to hold the new value, both
246 * raise MemoryError and execute "return ER". The difference is in ownership
247 * of O after: _PUSH transfers ownership of O from the caller to the stack
248 * (no incref of O is done, and in case of error O is decrefed), while
249 * _APPEND pushes a new reference.
250 */
251
252/* Push O on stack D, giving ownership of O to the stack. */
253#define PDATA_PUSH(D, O, ER) { \
254 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
255 Pdata_grow((Pdata*)(D)) < 0) { \
256 Py_DECREF(O); \
257 return ER; \
258 } \
259 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
260}
261
262/* Push O on stack D, pushing a new reference. */
263#define PDATA_APPEND(D, O, ER) { \
264 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
265 Pdata_grow((Pdata*)(D)) < 0) \
266 return ER; \
267 Py_INCREF(O); \
268 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
269}
270
271
Guido van Rossum053b8df1998-11-25 16:18:00 +0000272static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000273Pdata_popTuple(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000274{
275 PyObject *r;
276 int i, j, l;
Tim Peterscba30e22003-02-01 06:24:36 +0000277
Tim Peters1d63c9f2003-02-02 20:29:39 +0000278 l = self->length-start;
279 r = PyTuple_New(l);
280 if (r == NULL)
281 return NULL;
282 for (i = start, j = 0 ; j < l; i++, j++)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000283 PyTuple_SET_ITEM(r, j, self->data[i]);
Tim Peterscba30e22003-02-01 06:24:36 +0000284
Tim Peters1d63c9f2003-02-02 20:29:39 +0000285 self->length = start;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000286 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000287}
288
289static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000290Pdata_popList(Pdata *self, int start)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000291{
292 PyObject *r;
293 int i, j, l;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000294
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000295 l=self->length-start;
296 if (!( r=PyList_New(l))) return NULL;
297 for (i=start, j=0 ; j < l; i++, j++)
298 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000299
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000300 self->length=start;
301 return r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000302}
303
Guido van Rossum053b8df1998-11-25 16:18:00 +0000304/*************************************************************************/
305
306#define ARG_TUP(self, o) { \
307 if (self->arg || (self->arg=PyTuple_New(1))) { \
308 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
309 PyTuple_SET_ITEM(self->arg,0,o); \
310 } \
311 else { \
312 Py_DECREF(o); \
313 } \
314}
315
316#define FREE_ARG_TUP(self) { \
317 if (self->arg->ob_refcnt > 1) { \
318 Py_DECREF(self->arg); \
319 self->arg=NULL; \
320 } \
321 }
322
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000323typedef struct Picklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000324 PyObject_HEAD
325 FILE *fp;
326 PyObject *write;
327 PyObject *file;
328 PyObject *memo;
329 PyObject *arg;
330 PyObject *pers_func;
331 PyObject *inst_pers_func;
Tim Peters797ec242003-02-01 06:22:36 +0000332
333 /* pickle protocol number, >= 0 */
334 int proto;
335
336 /* bool, true if proto > 0 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000337 int bin;
Tim Peters797ec242003-02-01 06:22:36 +0000338
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000339 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Martin v. Löwis5a395302002-08-04 08:20:23 +0000340 int nesting;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000341 int (*write_func)(struct Picklerobject *, char *, int);
342 char *write_buf;
343 int buf_size;
344 PyObject *dispatch_table;
345 int fast_container; /* count nested container dumps */
346 PyObject *fast_memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000347} Picklerobject;
348
Barry Warsaw52acb492001-12-21 20:04:22 +0000349#ifndef PY_CPICKLE_FAST_LIMIT
350#define PY_CPICKLE_FAST_LIMIT 50
351#endif
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000352
Jeremy Hylton938ace62002-07-17 16:30:39 +0000353static PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000354
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000355typedef struct Unpicklerobject {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000356 PyObject_HEAD
357 FILE *fp;
358 PyObject *file;
359 PyObject *readline;
360 PyObject *read;
361 PyObject *memo;
362 PyObject *arg;
363 Pdata *stack;
364 PyObject *mark;
365 PyObject *pers_func;
366 PyObject *last_string;
367 int *marks;
368 int num_marks;
369 int marks_size;
370 int (*read_func)(struct Unpicklerobject *, char **, int);
371 int (*readline_func)(struct Unpicklerobject *, char **);
372 int buf_size;
373 char *buf;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000374 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000375} Unpicklerobject;
Tim Peters84e87f32001-03-17 04:50:51 +0000376
Jeremy Hylton938ace62002-07-17 16:30:39 +0000377static PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000378
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000379/* Forward decls that need the above structs */
380static int save(Picklerobject *, PyObject *, int);
381static int put2(Picklerobject *, PyObject *);
382
Guido van Rossumd385d591997-04-09 17:47:47 +0000383static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000384PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000385cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
386{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000387 va_list va;
388 PyObject *args=0, *retval=0;
389 va_start(va, format);
Tim Peterscba30e22003-02-01 06:24:36 +0000390
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000391 if (format) args = Py_VaBuildValue(format, va);
392 va_end(va);
393 if (format && ! args) return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000394 if (stringformat && !(retval=PyString_FromString(stringformat)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000395 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000396
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000397 if (retval) {
398 if (args) {
399 PyObject *v;
400 v=PyString_Format(retval, args);
401 Py_DECREF(retval);
402 Py_DECREF(args);
403 if (! v) return NULL;
404 retval=v;
405 }
406 }
407 else
408 if (args) retval=args;
409 else {
410 PyErr_SetObject(ErrType,Py_None);
411 return NULL;
412 }
413 PyErr_SetObject(ErrType,retval);
414 Py_DECREF(retval);
415 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000416}
417
Tim Peters84e87f32001-03-17 04:50:51 +0000418static int
Tim Peterscba30e22003-02-01 06:24:36 +0000419write_file(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000420{
421 size_t nbyteswritten;
Tim Peters84e87f32001-03-17 04:50:51 +0000422
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000423 if (s == NULL) {
424 return 0;
425 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000426
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000427 Py_BEGIN_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000428 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000429 Py_END_ALLOW_THREADS
Jeremy Hylton7fa4bfa2002-07-18 20:58:57 +0000430 if (nbyteswritten != (size_t)n) {
431 PyErr_SetFromErrno(PyExc_IOError);
432 return -1;
433 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000434
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000435 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000436}
437
Tim Peters84e87f32001-03-17 04:50:51 +0000438static int
Tim Peterscba30e22003-02-01 06:24:36 +0000439write_cStringIO(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000440{
441 if (s == NULL) {
442 return 0;
443 }
Tim Peterscba30e22003-02-01 06:24:36 +0000444
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000445 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
446 return -1;
447 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000448
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000449 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000450}
451
Tim Peters84e87f32001-03-17 04:50:51 +0000452static int
Tim Peterscba30e22003-02-01 06:24:36 +0000453write_none(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000454{
455 if (s == NULL) return 0;
456 return n;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000457}
458
Tim Peters84e87f32001-03-17 04:50:51 +0000459static int
Tim Peterscba30e22003-02-01 06:24:36 +0000460write_other(Picklerobject *self, char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000461{
462 PyObject *py_str = 0, *junk = 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000463
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000464 if (s == NULL) {
465 if (!( self->buf_size )) return 0;
Tim Peterscba30e22003-02-01 06:24:36 +0000466 py_str = PyString_FromStringAndSize(self->write_buf,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000467 self->buf_size);
Tim Peterscba30e22003-02-01 06:24:36 +0000468 if (!py_str)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000469 return -1;
470 }
471 else {
472 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
473 if (write_other(self, NULL, 0) < 0)
474 return -1;
475 }
Tim Peterscba30e22003-02-01 06:24:36 +0000476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000477 if (n > WRITE_BUF_SIZE) {
478 if (!( py_str =
Tim Peterscba30e22003-02-01 06:24:36 +0000479 PyString_FromStringAndSize(s, n)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000480 return -1;
481 }
482 else {
483 memcpy(self->write_buf + self->buf_size, s, n);
484 self->buf_size += n;
485 return n;
486 }
487 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000488
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000489 if (self->write) {
490 /* object with write method */
491 ARG_TUP(self, py_str);
492 if (self->arg) {
493 junk = PyObject_Call(self->write, self->arg, NULL);
494 FREE_ARG_TUP(self);
495 }
496 if (junk) Py_DECREF(junk);
497 else return -1;
498 }
499 else
500 PDATA_PUSH(self->file, py_str, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000501
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000502 self->buf_size = 0;
503 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000504}
505
506
Tim Peters84e87f32001-03-17 04:50:51 +0000507static int
Tim Petersee1a53c2003-02-02 02:57:53 +0000508read_file(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000509{
510 size_t nbytesread;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000511
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000512 if (self->buf_size == 0) {
513 int size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000514
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000515 size = ((n < 32) ? 32 : n);
Tim Petersee1a53c2003-02-02 02:57:53 +0000516 if (!( self->buf = (char *)malloc(size))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000517 PyErr_NoMemory();
518 return -1;
519 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000520
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000521 self->buf_size = size;
522 }
523 else if (n > self->buf_size) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000524 self->buf = (char *)realloc(self->buf, n);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000525 if (!self->buf) {
526 PyErr_NoMemory();
527 return -1;
528 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000529 self->buf_size = n;
530 }
Tim Peters84e87f32001-03-17 04:50:51 +0000531
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000532 Py_BEGIN_ALLOW_THREADS
533 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
534 Py_END_ALLOW_THREADS
535 if (nbytesread != (size_t)n) {
536 if (feof(self->fp)) {
537 PyErr_SetNone(PyExc_EOFError);
538 return -1;
539 }
Tim Peterscba30e22003-02-01 06:24:36 +0000540
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000541 PyErr_SetFromErrno(PyExc_IOError);
542 return -1;
543 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000544
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000545 *s = self->buf;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000546
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000547 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000548}
549
550
Tim Peters84e87f32001-03-17 04:50:51 +0000551static int
Tim Peterscba30e22003-02-01 06:24:36 +0000552readline_file(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000553{
554 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000555
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000556 if (self->buf_size == 0) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000557 if (!( self->buf = (char *)malloc(40))) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000558 PyErr_NoMemory();
559 return -1;
560 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000561 self->buf_size = 40;
562 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000563
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000564 i = 0;
565 while (1) {
Tim Petersee1a53c2003-02-02 02:57:53 +0000566 int bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000567 for (; i < (self->buf_size - 1); i++) {
Tim Peterscba30e22003-02-01 06:24:36 +0000568 if (feof(self->fp) ||
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000569 (self->buf[i] = getc(self->fp)) == '\n') {
570 self->buf[i + 1] = '\0';
571 *s = self->buf;
572 return i + 1;
573 }
574 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000575 bigger = self->buf_size << 1;
576 if (bigger <= 0) { /* overflow */
577 PyErr_NoMemory();
578 return -1;
579 }
580 self->buf = (char *)realloc(self->buf, bigger);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000581 if (!self->buf) {
582 PyErr_NoMemory();
583 return -1;
584 }
Tim Petersee1a53c2003-02-02 02:57:53 +0000585 self->buf_size = bigger;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000586 }
Tim Peters84e87f32001-03-17 04:50:51 +0000587}
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000588
589
Tim Peters84e87f32001-03-17 04:50:51 +0000590static int
Tim Peterscba30e22003-02-01 06:24:36 +0000591read_cStringIO(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000592{
593 char *ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000594
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000595 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
596 PyErr_SetNone(PyExc_EOFError);
597 return -1;
598 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000599
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000600 *s = ptr;
Tim Peterscba30e22003-02-01 06:24:36 +0000601
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000602 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000603}
604
605
Tim Peters84e87f32001-03-17 04:50:51 +0000606static int
Tim Peterscba30e22003-02-01 06:24:36 +0000607readline_cStringIO(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000608{
609 int n;
610 char *ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000611
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000612 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
613 return -1;
614 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000615
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000616 *s = ptr;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000617
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000618 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000619}
620
621
Tim Peters84e87f32001-03-17 04:50:51 +0000622static int
Tim Peterscba30e22003-02-01 06:24:36 +0000623read_other(Unpicklerobject *self, char **s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000624{
625 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000626
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000627 if (!( bytes = PyInt_FromLong(n))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000628
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000629 ARG_TUP(self, bytes);
630 if (self->arg) {
631 str = PyObject_Call(self->read, self->arg, NULL);
632 FREE_ARG_TUP(self);
633 }
634 if (! str) return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000635
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000636 Py_XDECREF(self->last_string);
637 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000638
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000639 if (! (*s = PyString_AsString(str))) return -1;
640 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000641}
642
643
Tim Peters84e87f32001-03-17 04:50:51 +0000644static int
Tim Peterscba30e22003-02-01 06:24:36 +0000645readline_other(Unpicklerobject *self, char **s)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000646{
647 PyObject *str;
648 int str_size;
Tim Peterscba30e22003-02-01 06:24:36 +0000649
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000650 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
651 return -1;
652 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000653
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000654 if ((str_size = PyString_Size(str)) < 0)
655 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000656
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000657 Py_XDECREF(self->last_string);
658 self->last_string = str;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000659
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000660 if (! (*s = PyString_AsString(str)))
661 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000662
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000663 return str_size;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000664}
665
Tim Petersee1a53c2003-02-02 02:57:53 +0000666/* Copy the first n bytes from s into newly malloc'ed memory, plus a
667 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
668 * The caller is responsible for free()'ing the return value.
669 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000670static char *
Tim Petersee1a53c2003-02-02 02:57:53 +0000671pystrndup(char *s, int n)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000672{
Tim Petersee1a53c2003-02-02 02:57:53 +0000673 char *r = (char *)malloc(n+1);
674 if (r == NULL)
675 return (char*)PyErr_NoMemory();
676 memcpy(r, s, n);
677 r[n] = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000678 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000679}
680
681
682static int
Tim Peterscba30e22003-02-01 06:24:36 +0000683get(Picklerobject *self, PyObject *id)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000684{
685 PyObject *value, *mv;
686 long c_value;
687 char s[30];
688 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000689
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000690 if (!( mv = PyDict_GetItem(self->memo, id))) {
691 PyErr_SetObject(PyExc_KeyError, id);
692 return -1;
693 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000694
Tim Peterscba30e22003-02-01 06:24:36 +0000695 if (!( value = PyTuple_GetItem(mv, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000696 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +0000697
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000698 if (!( PyInt_Check(value))) {
699 PyErr_SetString(PicklingError, "no int where int expected in memo");
700 return -1;
701 }
702 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000703
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000704 if (!self->bin) {
705 s[0] = GET;
706 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
707 len = strlen(s);
708 }
709 else if (Pdata_Check(self->file)) {
710 if (write_other(self, NULL, 0) < 0) return -1;
711 PDATA_APPEND(self->file, mv, -1);
712 return 0;
713 }
714 else {
715 if (c_value < 256) {
716 s[0] = BINGET;
717 s[1] = (int)(c_value & 0xff);
718 len = 2;
719 }
720 else {
721 s[0] = LONG_BINGET;
722 s[1] = (int)(c_value & 0xff);
723 s[2] = (int)((c_value >> 8) & 0xff);
724 s[3] = (int)((c_value >> 16) & 0xff);
725 s[4] = (int)((c_value >> 24) & 0xff);
726 len = 5;
727 }
728 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000729
Tim Peters0bc93f52003-02-02 18:29:33 +0000730 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000731 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +0000732
Jeremy Hyltona0fb1772001-10-12 04:11:06 +0000733 return 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000734}
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000735
Guido van Rossum60456fd1997-04-09 17:36:32 +0000736
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000737static int
Tim Peterscba30e22003-02-01 06:24:36 +0000738put(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000739{
Tim Peterscba30e22003-02-01 06:24:36 +0000740 if (ob->ob_refcnt < 2 || self->fast)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000741 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000742
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000743 return put2(self, ob);
744}
Guido van Rossum053b8df1998-11-25 16:18:00 +0000745
Guido van Rossum053b8df1998-11-25 16:18:00 +0000746
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000747static int
Tim Peterscba30e22003-02-01 06:24:36 +0000748put2(Picklerobject *self, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000749{
750 char c_str[30];
751 int p;
752 size_t len;
753 int res = -1;
754 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000755
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000756 if (self->fast)
757 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000758
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000759 if ((p = PyDict_Size(self->memo)) < 0)
760 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000761
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000762 /* Make sure memo keys are positive! */
Tim Peters70b02d72003-02-02 17:26:40 +0000763 /* XXX Why?
764 * XXX And does "positive" really mean non-negative?
765 * XXX pickle.py starts with PUT index 0, not 1. This makes for
766 * XXX gratuitous differences between the pickling modules.
767 */
Tim Peterscba30e22003-02-01 06:24:36 +0000768 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000769
Tim Peterscba30e22003-02-01 06:24:36 +0000770 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000771 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000772
Tim Peterscba30e22003-02-01 06:24:36 +0000773 if (!( memo_len = PyInt_FromLong(p)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000774 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000775
Tim Peterscba30e22003-02-01 06:24:36 +0000776 if (!( t = PyTuple_New(2)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000777 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000778
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000779 PyTuple_SET_ITEM(t, 0, memo_len);
780 Py_INCREF(memo_len);
781 PyTuple_SET_ITEM(t, 1, ob);
782 Py_INCREF(ob);
783
784 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
785 goto finally;
786
787 if (!self->bin) {
788 c_str[0] = PUT;
789 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
790 len = strlen(c_str);
791 }
792 else if (Pdata_Check(self->file)) {
793 if (write_other(self, NULL, 0) < 0) return -1;
794 PDATA_APPEND(self->file, memo_len, -1);
795 res=0; /* Job well done ;) */
796 goto finally;
797 }
798 else {
799 if (p >= 256) {
800 c_str[0] = LONG_BINPUT;
801 c_str[1] = (int)(p & 0xff);
802 c_str[2] = (int)((p >> 8) & 0xff);
803 c_str[3] = (int)((p >> 16) & 0xff);
804 c_str[4] = (int)((p >> 24) & 0xff);
805 len = 5;
806 }
807 else {
808 c_str[0] = BINPUT;
809 c_str[1] = p;
810 len = 2;
811 }
812 }
813
Tim Peters0bc93f52003-02-02 18:29:33 +0000814 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000815 goto finally;
816
817 res = 0;
818
819 finally:
820 Py_XDECREF(py_ob_id);
821 Py_XDECREF(memo_len);
822 Py_XDECREF(t);
823
824 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000825}
826
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000827#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000828
829static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000830PyImport_Import(PyObject *module_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000831{
832 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
833 static PyObject *standard_builtins=0;
834 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000835
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000836 if (!( silly_list )) {
Tim Peterscba30e22003-02-01 06:24:36 +0000837 if (!( __import___str=PyString_FromString("__import__")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000838 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000839 if (!( __builtins___str=PyString_FromString("__builtins__")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000840 return NULL;
Tim Peterscba30e22003-02-01 06:24:36 +0000841 if (!( silly_list=Py_BuildValue("[s]","__doc__")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000842 return NULL;
843 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000844
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000845 if ((globals=PyEval_GetGlobals())) {
846 Py_INCREF(globals);
847 __builtins__=PyObject_GetItem(globals,__builtins___str);
Tim Peterscba30e22003-02-01 06:24:36 +0000848 if (!__builtins__)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000849 goto err;
850 }
851 else {
852 PyErr_Clear();
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000853
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000854 if (!(standard_builtins ||
Tim Peterscba30e22003-02-01 06:24:36 +0000855 (standard_builtins=PyImport_ImportModule("__builtin__"))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000856 return NULL;
Tim Peters84e87f32001-03-17 04:50:51 +0000857
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000858 __builtins__=standard_builtins;
859 Py_INCREF(__builtins__);
860 globals = Py_BuildValue("{sO}", "__builtins__", __builtins__);
Tim Peterscba30e22003-02-01 06:24:36 +0000861 if (!globals)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000862 goto err;
863 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000864
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000865 if (PyDict_Check(__builtins__)) {
866 __import__=PyObject_GetItem(__builtins__,__import___str);
867 if (!__import__) goto err;
868 }
869 else {
870 __import__=PyObject_GetAttr(__builtins__,__import___str);
871 if (!__import__) goto err;
872 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000873
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000874 r=PyObject_CallFunction(__import__,"OOOO",
875 module_name, globals, globals, silly_list);
Tim Peterscba30e22003-02-01 06:24:36 +0000876 if (!r)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000877 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000878
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000879 Py_DECREF(globals);
880 Py_DECREF(__builtins__);
881 Py_DECREF(__import__);
Tim Peters84e87f32001-03-17 04:50:51 +0000882
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000883 return r;
884 err:
885 Py_XDECREF(globals);
886 Py_XDECREF(__builtins__);
887 Py_XDECREF(__import__);
888 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000889}
890
891static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +0000892whichmodule(PyObject *global, PyObject *global_name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000893{
894 int i, j;
895 PyObject *module = 0, *modules_dict = 0,
896 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000897
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000898 module = PyObject_GetAttrString(global, "__module__");
899 if (module) return module;
900 PyErr_Clear();
Guido van Rossum45188231997-09-28 05:38:51 +0000901
Tim Peterscba30e22003-02-01 06:24:36 +0000902 if (!( modules_dict = PySys_GetObject("modules")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000903 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000904
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000905 i = 0;
906 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000907
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000908 if (PyObject_Compare(name, __main___str)==0) continue;
Tim Peters84e87f32001-03-17 04:50:51 +0000909
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000910 global_name_attr = PyObject_GetAttr(module, global_name);
911 if (!global_name_attr) {
912 PyErr_Clear();
913 continue;
914 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000915
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000916 if (global_name_attr != global) {
917 Py_DECREF(global_name_attr);
918 continue;
919 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000920
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000921 Py_DECREF(global_name_attr);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000922
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000923 break;
924 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000925
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000926 /* The following implements the rule in pickle.py added in 1.5
927 that used __main__ if no module is found. I don't actually
928 like this rule. jlf
929 */
930 if (!j) {
931 j=1;
932 name=__main___str;
933 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000934
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000935 Py_INCREF(name);
936 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000937}
938
939
Guido van Rossum60456fd1997-04-09 17:36:32 +0000940static int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000941fast_save_enter(Picklerobject *self, PyObject *obj)
942{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000943 /* if fast_container < 0, we're doing an error exit. */
944 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
945 PyObject *key = NULL;
946 if (self->fast_memo == NULL) {
947 self->fast_memo = PyDict_New();
948 if (self->fast_memo == NULL) {
949 self->fast_container = -1;
950 return 0;
951 }
952 }
953 key = PyLong_FromVoidPtr(obj);
954 if (key == NULL)
955 return 0;
956 if (PyDict_GetItem(self->fast_memo, key)) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000957 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000958 PyErr_Format(PyExc_ValueError,
959 "fast mode: can't pickle cyclic objects including object type %s at %p",
960 obj->ob_type->tp_name, obj);
961 self->fast_container = -1;
962 return 0;
963 }
964 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000965 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000966 self->fast_container = -1;
967 return 0;
968 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000969 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000970 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000971 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000972}
973
Tim Peterscba30e22003-02-01 06:24:36 +0000974int
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000975fast_save_leave(Picklerobject *self, PyObject *obj)
976{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000977 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
978 PyObject *key = PyLong_FromVoidPtr(obj);
979 if (key == NULL)
980 return 0;
981 if (PyDict_DelItem(self->fast_memo, key) < 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +0000982 Py_DECREF(key);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000983 return 0;
984 }
Tim Peters4e52ca82002-12-07 02:43:28 +0000985 Py_DECREF(key);
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000986 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000987 return 1;
Jeremy Hylton499ab6a2001-10-15 21:37:58 +0000988}
989
990static int
Tim Peterscba30e22003-02-01 06:24:36 +0000991save_none(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000992{
993 static char none = NONE;
Tim Peters0bc93f52003-02-02 18:29:33 +0000994 if (self->write_func(self, &none, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000995 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000996
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +0000997 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000998}
999
Guido van Rossum77f6a652002-04-03 22:41:51 +00001000static int
Tim Peterscba30e22003-02-01 06:24:36 +00001001save_bool(Picklerobject *self, PyObject *args)
Guido van Rossum77f6a652002-04-03 22:41:51 +00001002{
Guido van Rossume2763392002-04-05 19:30:08 +00001003 static char *buf[2] = {FALSE, TRUE};
1004 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
Guido van Rossum77f6a652002-04-03 22:41:51 +00001005 long l = PyInt_AS_LONG((PyIntObject *)args);
1006
Tim Peters3c67d792003-02-02 17:59:11 +00001007 if (self->proto >= 2) {
1008 char opcode = l ? NEWTRUE : NEWFALSE;
1009 if (self->write_func(self, &opcode, 1) < 0)
1010 return -1;
1011 }
1012 else if (self->write_func(self, buf[l], len[l]) < 0)
Guido van Rossum77f6a652002-04-03 22:41:51 +00001013 return -1;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001014 return 0;
1015}
Tim Peters84e87f32001-03-17 04:50:51 +00001016
Guido van Rossum60456fd1997-04-09 17:36:32 +00001017static int
Tim Peterscba30e22003-02-01 06:24:36 +00001018save_int(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001019{
1020 char c_str[32];
1021 long l = PyInt_AS_LONG((PyIntObject *)args);
1022 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001023
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001024 if (!self->bin
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001025#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001026 || l > 0x7fffffffL
1027 || l < -0x80000000L
Guido van Rossumde8d6d71997-05-13 18:00:44 +00001028#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001029 ) {
1030 /* Text-mode pickle, or long too big to fit in the 4-byte
1031 * signed BININT format: store as a string.
1032 */
1033 c_str[0] = INT;
1034 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
Tim Peters0bc93f52003-02-02 18:29:33 +00001035 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001036 return -1;
1037 }
1038 else {
1039 /* Binary pickle and l fits in a signed 4-byte int. */
1040 c_str[1] = (int)( l & 0xff);
1041 c_str[2] = (int)((l >> 8) & 0xff);
1042 c_str[3] = (int)((l >> 16) & 0xff);
1043 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001044
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001045 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1046 if (c_str[2] == 0) {
1047 c_str[0] = BININT1;
1048 len = 2;
1049 }
1050 else {
1051 c_str[0] = BININT2;
1052 len = 3;
1053 }
1054 }
1055 else {
1056 c_str[0] = BININT;
1057 len = 5;
1058 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001059
Tim Peters0bc93f52003-02-02 18:29:33 +00001060 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001061 return -1;
1062 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001063
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001064 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001065}
1066
1067
1068static int
Tim Peterscba30e22003-02-01 06:24:36 +00001069save_long(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001070{
Tim Petersee1a53c2003-02-02 02:57:53 +00001071 int size;
1072 int res = -1;
1073 PyObject *repr = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001074
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001075 static char l = LONG;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001076
Tim Petersee1a53c2003-02-02 02:57:53 +00001077 if (self->proto >= 2) {
1078 /* Linear-time pickling. */
1079 size_t nbits;
1080 size_t nbytes;
1081 unsigned char *pdata;
1082 char c_str[5];
1083 int i;
1084 int sign = _PyLong_Sign(args);
1085
1086 if (sign == 0) {
1087 /* It's 0 -- an empty bytestring. */
1088 c_str[0] = LONG1;
1089 c_str[1] = 0;
1090 i = self->write_func(self, c_str, 2);
1091 if (i < 0) goto finally;
1092 res = 0;
1093 goto finally;
1094 }
1095 nbits = _PyLong_NumBits(args);
1096 if (nbits == (size_t)-1 && PyErr_Occurred())
1097 goto finally;
1098 /* How many bytes do we need? There are nbits >> 3 full
1099 * bytes of data, and nbits & 7 leftover bits. If there
1100 * are any leftover bits, then we clearly need another
1101 * byte. Wnat's not so obvious is that we *probably*
1102 * need another byte even if there aren't any leftovers:
1103 * the most-significant bit of the most-significant byte
1104 * acts like a sign bit, and it's usually got a sense
1105 * opposite of the one we need. The exception is longs
1106 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1107 * its own 256's-complement, so has the right sign bit
1108 * even without the extra byte. That's a pain to check
1109 * for in advance, though, so we always grab an extra
1110 * byte at the start, and cut it back later if possible.
1111 */
1112 nbytes = (nbits >> 3) + 1;
1113 if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) {
1114 PyErr_SetString(PyExc_OverflowError, "long too large "
1115 "to pickle");
1116 goto finally;
1117 }
1118 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1119 if (repr == NULL) goto finally;
1120 pdata = (unsigned char *)PyString_AS_STRING(repr);
1121 i = _PyLong_AsByteArray((PyLongObject *)args,
1122 pdata, nbytes,
1123 1 /* little endian */, 1 /* signed */);
1124 if (i < 0) goto finally;
1125 /* If the long is negative, this may be a byte more than
1126 * needed. This is so iff the MSB is all redundant sign
1127 * bits.
1128 */
1129 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1130 (pdata[nbytes - 2] & 0x80) != 0)
1131 --nbytes;
1132
1133 if (nbytes < 256) {
1134 c_str[0] = LONG1;
1135 c_str[1] = (char)nbytes;
1136 size = 2;
1137 }
1138 else {
1139 c_str[0] = LONG4;
1140 size = (int)nbytes;
1141 for (i = 1; i < 5; i++) {
1142 c_str[i] = (char)(size & 0xff);
1143 size >>= 8;
1144 }
1145 size = 5;
1146 }
1147 i = self->write_func(self, c_str, size);
1148 if (i < 0) goto finally;
1149 i = self->write_func(self, (char *)pdata, (int)nbytes);
1150 if (i < 0) goto finally;
1151 res = 0;
1152 goto finally;
1153 }
1154
1155 /* proto < 2: write the repr and newline. This is quadratic-time
1156 * (in the number of digits), in both directions.
1157 */
Tim Peterscba30e22003-02-01 06:24:36 +00001158 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001159 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001160
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001161 if ((size = PyString_Size(repr)) < 0)
1162 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001163
Tim Peters0bc93f52003-02-02 18:29:33 +00001164 if (self->write_func(self, &l, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001165 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001166
Tim Peters0bc93f52003-02-02 18:29:33 +00001167 if (self->write_func(self,
1168 PyString_AS_STRING((PyStringObject *)repr),
1169 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001170 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001171
Tim Peters0bc93f52003-02-02 18:29:33 +00001172 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001173 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001174
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001175 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001176
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001177 finally:
1178 Py_XDECREF(repr);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001179 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001180}
1181
1182
1183static int
Tim Peterscba30e22003-02-01 06:24:36 +00001184save_float(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001185{
1186 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001187
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001188 if (self->bin) {
1189 int s, e;
1190 double f;
1191 long fhi, flo;
1192 char str[9];
1193 unsigned char *p = (unsigned char *)str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001194
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001195 *p = BINFLOAT;
1196 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001197
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001198 if (x < 0) {
1199 s = 1;
1200 x = -x;
1201 }
1202 else
1203 s = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001204
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001205 f = frexp(x, &e);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001206
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001207 /* Normalize f to be in the range [1.0, 2.0) */
1208 if (0.5 <= f && f < 1.0) {
1209 f *= 2.0;
1210 e--;
1211 }
1212 else if (f == 0.0) {
1213 e = 0;
1214 }
1215 else {
1216 PyErr_SetString(PyExc_SystemError,
1217 "frexp() result out of range");
1218 return -1;
1219 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001220
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001221 if (e >= 1024) {
1222 /* XXX 1024 itself is reserved for Inf/NaN */
1223 PyErr_SetString(PyExc_OverflowError,
1224 "float too large to pack with d format");
1225 return -1;
1226 }
1227 else if (e < -1022) {
1228 /* Gradual underflow */
1229 f = ldexp(f, 1022 + e);
1230 e = 0;
1231 }
1232 else if (!(e == 0 && f == 0.0)) {
1233 e += 1023;
1234 f -= 1.0; /* Get rid of leading 1 */
1235 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001236
Tim Peterscba30e22003-02-01 06:24:36 +00001237 /* fhi receives the high 28 bits;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001238 flo the low 24 bits (== 52 bits) */
1239 f *= 268435456.0; /* 2**28 */
1240 fhi = (long) floor(f); /* Truncate */
1241 f -= (double)fhi;
1242 f *= 16777216.0; /* 2**24 */
1243 flo = (long) floor(f + 0.5); /* Round */
Guido van Rossum60456fd1997-04-09 17:36:32 +00001244
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001245 /* First byte */
1246 *p = (s<<7) | (e>>4);
1247 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001248
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001249 /* Second byte */
1250 *p = (unsigned char) (((e&0xF)<<4) | (fhi>>24));
1251 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001252
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001253 /* Third byte */
1254 *p = (unsigned char) ((fhi>>16) & 0xFF);
1255 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001256
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001257 /* Fourth byte */
1258 *p = (unsigned char) ((fhi>>8) & 0xFF);
1259 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001260
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001261 /* Fifth byte */
1262 *p = (unsigned char) (fhi & 0xFF);
1263 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001264
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001265 /* Sixth byte */
1266 *p = (unsigned char) ((flo>>16) & 0xFF);
1267 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001268
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001269 /* Seventh byte */
1270 *p = (unsigned char) ((flo>>8) & 0xFF);
1271 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001272
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001273 /* Eighth byte */
1274 *p = (unsigned char) (flo & 0xFF);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001275
Tim Peters0bc93f52003-02-02 18:29:33 +00001276 if (self->write_func(self, str, 9) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001277 return -1;
1278 }
1279 else {
1280 char c_str[250];
1281 c_str[0] = FLOAT;
1282 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001283
Tim Peters0bc93f52003-02-02 18:29:33 +00001284 if (self->write_func(self, c_str, strlen(c_str)) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001285 return -1;
1286 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001287
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001288 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001289}
1290
1291
1292static int
Tim Peterscba30e22003-02-01 06:24:36 +00001293save_string(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001294{
1295 int size, len;
1296 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001297
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001298 if ((size = PyString_Size(args)) < 0)
1299 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001301 if (!self->bin) {
1302 char *repr_str;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001303
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001304 static char string = STRING;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001305
Tim Peterscba30e22003-02-01 06:24:36 +00001306 if (!( repr = PyObject_Repr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001307 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001308
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001309 if ((len = PyString_Size(repr)) < 0)
1310 goto err;
1311 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001312
Tim Peters0bc93f52003-02-02 18:29:33 +00001313 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001314 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001315
Tim Peters0bc93f52003-02-02 18:29:33 +00001316 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001317 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001318
Tim Peters0bc93f52003-02-02 18:29:33 +00001319 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001320 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001321
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001322 Py_XDECREF(repr);
1323 }
1324 else {
1325 int i;
1326 char c_str[5];
Guido van Rossum60456fd1997-04-09 17:36:32 +00001327
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001328 if ((size = PyString_Size(args)) < 0)
1329 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001330
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001331 if (size < 256) {
1332 c_str[0] = SHORT_BINSTRING;
1333 c_str[1] = size;
1334 len = 2;
1335 }
1336 else {
1337 c_str[0] = BINSTRING;
1338 for (i = 1; i < 5; i++)
1339 c_str[i] = (int)(size >> ((i - 1) * 8));
1340 len = 5;
1341 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001342
Tim Peters0bc93f52003-02-02 18:29:33 +00001343 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001344 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001345
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001346 if (size > 128 && Pdata_Check(self->file)) {
1347 if (write_other(self, NULL, 0) < 0) return -1;
1348 PDATA_APPEND(self->file, args, -1);
1349 }
1350 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001351 if (self->write_func(self,
1352 PyString_AS_STRING(
1353 (PyStringObject *)args),
1354 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001355 return -1;
1356 }
1357 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001358
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001359 if (doput)
1360 if (put(self, args) < 0)
1361 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001362
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001363 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001364
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001365 err:
1366 Py_XDECREF(repr);
1367 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001368}
1369
1370
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001371#ifdef Py_USING_UNICODE
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001372/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1373 backslash and newline characters to \uXXXX escapes. */
1374static PyObject *
1375modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1376{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001377 PyObject *repr;
1378 char *p;
1379 char *q;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001380
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001381 static const char *hexdigit = "0123456789ABCDEF";
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001382
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001383 repr = PyString_FromStringAndSize(NULL, 6 * size);
1384 if (repr == NULL)
1385 return NULL;
1386 if (size == 0)
1387 return repr;
1388
1389 p = q = PyString_AS_STRING(repr);
1390 while (size-- > 0) {
1391 Py_UNICODE ch = *s++;
1392 /* Map 16-bit characters to '\uxxxx' */
1393 if (ch >= 256 || ch == '\\' || ch == '\n') {
1394 *p++ = '\\';
1395 *p++ = 'u';
1396 *p++ = hexdigit[(ch >> 12) & 0xf];
1397 *p++ = hexdigit[(ch >> 8) & 0xf];
1398 *p++ = hexdigit[(ch >> 4) & 0xf];
1399 *p++ = hexdigit[ch & 15];
1400 }
1401 /* Copy everything else as-is */
1402 else
1403 *p++ = (char) ch;
1404 }
1405 *p = '\0';
Tim Peters5de98422002-04-27 18:44:32 +00001406 _PyString_Resize(&repr, p - q);
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001407 return repr;
Guido van Rossumfb10c3f2000-12-19 02:08:38 +00001408}
1409
1410
Guido van Rossum60456fd1997-04-09 17:36:32 +00001411static int
Tim Peterscba30e22003-02-01 06:24:36 +00001412save_unicode(Picklerobject *self, PyObject *args, int doput)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001413{
1414 int size, len;
1415 PyObject *repr=0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001416
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001417 if (!PyUnicode_Check(args))
1418 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001419
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001420 if (!self->bin) {
1421 char *repr_str;
1422 static char string = UNICODE;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001423
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001424 repr = modified_EncodeRawUnicodeEscape(
1425 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
Tim Peterscba30e22003-02-01 06:24:36 +00001426 if (!repr)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001427 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001428
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001429 if ((len = PyString_Size(repr)) < 0)
1430 goto err;
1431 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001432
Tim Peters0bc93f52003-02-02 18:29:33 +00001433 if (self->write_func(self, &string, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001434 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001435
Tim Peters0bc93f52003-02-02 18:29:33 +00001436 if (self->write_func(self, repr_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001437 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001438
Tim Peters0bc93f52003-02-02 18:29:33 +00001439 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001440 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001441
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001442 Py_XDECREF(repr);
1443 }
1444 else {
1445 int i;
1446 char c_str[5];
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001447
Tim Peterscba30e22003-02-01 06:24:36 +00001448 if (!( repr = PyUnicode_AsUTF8String(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001449 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001450
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001451 if ((size = PyString_Size(repr)) < 0)
1452 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001453
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001454 c_str[0] = BINUNICODE;
1455 for (i = 1; i < 5; i++)
1456 c_str[i] = (int)(size >> ((i - 1) * 8));
1457 len = 5;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001458
Tim Peters0bc93f52003-02-02 18:29:33 +00001459 if (self->write_func(self, c_str, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001460 goto err;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001461
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001462 if (size > 128 && Pdata_Check(self->file)) {
1463 if (write_other(self, NULL, 0) < 0)
1464 goto err;
1465 PDATA_APPEND(self->file, repr, -1);
1466 }
1467 else {
Tim Peters0bc93f52003-02-02 18:29:33 +00001468 if (self->write_func(self, PyString_AS_STRING(repr),
1469 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001470 goto err;
1471 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001472
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001473 Py_DECREF(repr);
1474 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001475
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001476 if (doput)
1477 if (put(self, args) < 0)
1478 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001479
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001480 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001481
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001482 err:
1483 Py_XDECREF(repr);
1484 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001485}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001486#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001487
Tim Peters1d63c9f2003-02-02 20:29:39 +00001488/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1489static int
Tim Peters67920142003-02-05 03:46:17 +00001490store_tuple_elements(Picklerobject *self, PyObject *t, int len)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001491{
1492 int i;
1493 int res = -1; /* guilty until proved innocent */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001494
Tim Peters1d63c9f2003-02-02 20:29:39 +00001495 assert(PyTuple_Size(t) == len);
1496
1497 for (i = 0; i < len; i++) {
1498 PyObject *element = PyTuple_GET_ITEM(t, i);
1499
1500 if (element == NULL)
1501 goto finally;
1502 if (save(self, element, 0) < 0)
1503 goto finally;
1504 }
1505 res = 0;
1506
1507 finally:
1508 return res;
1509}
1510
1511/* Tuples are ubiquitous in the pickle protocols, so many techniques are
1512 * used across protocols to minimize the space needed to pickle them.
Tim Peters67920142003-02-05 03:46:17 +00001513 * Tuples are also the only builtin immutable type that can be recursive
Tim Peters1d63c9f2003-02-02 20:29:39 +00001514 * (a tuple can be reached from itself), and that requires some subtle
1515 * magic so that it works in all cases. IOW, this is a long routine.
1516 */
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001517static int
Tim Peterscba30e22003-02-01 06:24:36 +00001518save_tuple(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001519{
Tim Peters1d63c9f2003-02-02 20:29:39 +00001520 PyObject *py_tuple_id = NULL;
1521 int len, i;
1522 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001523
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001524 static char tuple = TUPLE;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001525 static char pop = POP;
1526 static char pop_mark = POP_MARK;
1527 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
Guido van Rossum60456fd1997-04-09 17:36:32 +00001528
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001529 if ((len = PyTuple_Size(args)) < 0)
1530 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001531
Tim Peters1d63c9f2003-02-02 20:29:39 +00001532 if (len == 0) {
1533 char c_str[2];
Tim Peters84e87f32001-03-17 04:50:51 +00001534
Tim Peters1d63c9f2003-02-02 20:29:39 +00001535 if (self->proto) {
1536 c_str[0] = EMPTY_TUPLE;
1537 len = 1;
1538 }
1539 else {
1540 c_str[0] = MARK;
1541 c_str[1] = TUPLE;
1542 len = 2;
1543 }
1544 if (self->write_func(self, c_str, len) >= 0)
1545 res = 0;
1546 /* Don't memoize an empty tuple. */
1547 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001548 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001549
Tim Peters1d63c9f2003-02-02 20:29:39 +00001550 /* A non-empty tuple. */
1551
1552 /* id(tuple) isn't in the memo now. If it shows up there after
1553 * saving the tuple elements, the tuple must be recursive, in
1554 * which case we'll pop everything we put on the stack, and fetch
1555 * its value from the memo.
1556 */
1557 py_tuple_id = PyLong_FromVoidPtr(args);
1558 if (py_tuple_id == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001559 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001560
Tim Peters1d63c9f2003-02-02 20:29:39 +00001561 if (len <= 3 && self->proto >= 2) {
1562 /* Use TUPLE{1,2,3} opcodes. */
Tim Peters67920142003-02-05 03:46:17 +00001563 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001564 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001565 if (PyDict_GetItem(self->memo, py_tuple_id)) {
Tim Peters1d63c9f2003-02-02 20:29:39 +00001566 /* pop the len elements */
1567 for (i = 0; i < len; ++i)
1568 if (self->write_func(self, &pop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001569 goto finally;
Tim Peters1d63c9f2003-02-02 20:29:39 +00001570 /* fetch from memo */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001571 if (get(self, py_tuple_id) < 0)
1572 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001573 res = 0;
1574 goto finally;
1575 }
Tim Peters1d63c9f2003-02-02 20:29:39 +00001576 /* Not recursive. */
1577 if (self->write_func(self, len2opcode + len, 1) < 0)
1578 goto finally;
1579 goto memoize;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001580 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001581
Tim Peters1d63c9f2003-02-02 20:29:39 +00001582 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1583 * Generate MARK elt1 elt2 ... TUPLE
1584 */
1585 if (self->write_func(self, &MARKv, 1) < 0)
1586 goto finally;
1587
Tim Peters67920142003-02-05 03:46:17 +00001588 if (store_tuple_elements(self, args, len) < 0)
Tim Peters1d63c9f2003-02-02 20:29:39 +00001589 goto finally;
1590
1591 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1592 /* pop the stack stuff we pushed */
1593 if (self->bin) {
1594 if (self->write_func(self, &pop_mark, 1) < 0)
1595 goto finally;
1596 }
1597 else {
1598 /* Note that we pop one more than len, to remove
1599 * the MARK too.
1600 */
1601 for (i = 0; i <= len; i++)
1602 if (self->write_func(self, &pop, 1) < 0)
1603 goto finally;
1604 }
1605 /* fetch from memo */
1606 if (get(self, py_tuple_id) >= 0)
1607 res = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001608 goto finally;
1609 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001610
Tim Peters1d63c9f2003-02-02 20:29:39 +00001611 /* Not recursive. */
1612 if (self->write_func(self, &tuple, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001613 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00001614
Tim Peters1d63c9f2003-02-02 20:29:39 +00001615 memoize:
1616 if (put(self, args) >= 0)
1617 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001619 finally:
1620 Py_XDECREF(py_tuple_id);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001621 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001622}
1623
Tim Peters1092d642003-02-11 21:06:20 +00001624/* iter is an iterator giving items, and we batch up chunks of
1625 * MARK item item ... item APPENDS
1626 * opcode sequences. Calling code should have arranged to first create an
1627 * empty list, or list-like object, for the APPENDS to operate on.
1628 * Returns 0 on success, <0 on error.
1629 */
1630static int
1631batch_list(Picklerobject *self, PyObject *iter)
1632{
1633 PyObject *obj;
1634 PyObject *slice[BATCHSIZE];
1635 int i, n;
1636
1637 static char append = APPEND;
1638 static char appends = APPENDS;
1639
1640 assert(iter != NULL);
1641
1642 if (self->proto == 0) {
1643 /* APPENDS isn't available; do one at a time. */
1644 for (;;) {
1645 obj = PyIter_Next(iter);
1646 if (obj == NULL) {
1647 if (PyErr_Occurred())
1648 return -1;
1649 break;
1650 }
1651 i = save(self, obj, 0);
1652 Py_DECREF(obj);
1653 if (i < 0)
1654 return -1;
1655 if (self->write_func(self, &append, 1) < 0)
1656 return -1;
Tim Peters1092d642003-02-11 21:06:20 +00001657 }
1658 return 0;
1659 }
1660
1661 /* proto > 0: write in batches of BATCHSIZE. */
1662 do {
1663 /* Get next group of (no more than) BATCHSIZE elements. */
1664 for (n = 0; n < BATCHSIZE; ++n) {
1665 obj = PyIter_Next(iter);
1666 if (obj == NULL) {
1667 if (PyErr_Occurred())
1668 goto BatchFailed;
1669 break;
1670 }
1671 slice[n] = obj;
1672 }
1673
1674 if (n > 1) {
1675 /* Pump out MARK, slice[0:n], APPENDS. */
1676 if (self->write_func(self, &MARKv, 1) < 0)
1677 goto BatchFailed;
1678 for (i = 0; i < n; ++i) {
1679 if (save(self, slice[i], 0) < 0)
1680 goto BatchFailed;
1681 }
1682 if (self->write_func(self, &appends, 1) < 0)
1683 goto BatchFailed;
1684 }
1685 else if (n == 1) {
1686 if (save(self, slice[0], 0) < 0)
1687 goto BatchFailed;
1688 if (self->write_func(self, &append, 1) < 0)
1689 goto BatchFailed;
1690 }
1691
1692 for (i = 0; i < n; ++i) {
1693 Py_DECREF(slice[i]);
1694 }
Tim Peters90975f12003-02-12 05:28:58 +00001695 } while (n == BATCHSIZE);
Tim Peters1092d642003-02-11 21:06:20 +00001696 return 0;
1697
1698BatchFailed:
1699 while (--n >= 0) {
1700 Py_DECREF(slice[n]);
1701 }
1702 return -1;
1703}
1704
Guido van Rossum60456fd1997-04-09 17:36:32 +00001705static int
Tim Peterscba30e22003-02-01 06:24:36 +00001706save_list(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001707{
Tim Peters1092d642003-02-11 21:06:20 +00001708 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001709 char s[3];
Tim Peters1092d642003-02-11 21:06:20 +00001710 int len;
1711 PyObject *iter;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001712
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001713 if (self->fast && !fast_save_enter(self, args))
1714 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001715
Tim Peters1092d642003-02-11 21:06:20 +00001716 /* Create an empty list. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001717 if (self->bin) {
1718 s[0] = EMPTY_LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001719 len = 1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001720 }
1721 else {
1722 s[0] = MARK;
1723 s[1] = LIST;
Tim Peters1092d642003-02-11 21:06:20 +00001724 len = 2;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001725 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001726
Tim Peters1092d642003-02-11 21:06:20 +00001727 if (self->write_func(self, s, len) < 0)
1728 goto finally;
1729
1730 /* Get list length, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001731 if ((len = PyList_Size(args)) < 0)
1732 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001733
Tim Peters1092d642003-02-11 21:06:20 +00001734 /* Memoize. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001735 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001736 if (put(self, args) >= 0)
1737 res = 0;
1738 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001739 }
Tim Peters90975f12003-02-12 05:28:58 +00001740 if (put2(self, args) < 0)
1741 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001742
Tim Peters1092d642003-02-11 21:06:20 +00001743 /* Materialize the list elements. */
1744 iter = PyObject_GetIter(args);
1745 if (iter == NULL)
1746 goto finally;
1747 res = batch_list(self, iter);
1748 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001749
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001750 finally:
1751 if (self->fast && !fast_save_leave(self, args))
1752 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001753
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001754 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001755}
1756
1757
Tim Peters42f08ac2003-02-11 22:43:24 +00001758/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1759 * MARK key value ... key value SETITEMS
1760 * opcode sequences. Calling code should have arranged to first create an
1761 * empty dict, or dict-like object, for the SETITEMS to operate on.
1762 * Returns 0 on success, <0 on error.
1763 *
1764 * This is very much like batch_list(). The difference between saving
1765 * elements directly, and picking apart two-tuples, is so long-winded at
1766 * the C level, though, that attempts to combine these routines were too
1767 * ugly to bear.
1768 */
1769static int
1770batch_dict(Picklerobject *self, PyObject *iter)
1771{
1772 PyObject *p;
1773 PyObject *slice[BATCHSIZE];
1774 int i, n;
1775
1776 static char setitem = SETITEM;
1777 static char setitems = SETITEMS;
1778
1779 assert(iter != NULL);
1780
1781 if (self->proto == 0) {
1782 /* SETITEMS isn't available; do one at a time. */
1783 for (;;) {
1784 p = PyIter_Next(iter);
1785 if (p == NULL) {
1786 if (PyErr_Occurred())
1787 return -1;
1788 break;
1789 }
1790 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1791 PyErr_SetString(PyExc_TypeError, "dict items "
1792 "iterator must return 2-tuples");
1793 return -1;
1794 }
1795 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1796 if (i >= 0)
1797 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1798 Py_DECREF(p);
1799 if (i < 0)
1800 return -1;
1801 if (self->write_func(self, &setitem, 1) < 0)
1802 return -1;
Tim Peters42f08ac2003-02-11 22:43:24 +00001803 }
1804 return 0;
1805 }
1806
1807 /* proto > 0: write in batches of BATCHSIZE. */
1808 do {
1809 /* Get next group of (no more than) BATCHSIZE elements. */
1810 for (n = 0; n < BATCHSIZE; ++n) {
1811 p = PyIter_Next(iter);
1812 if (p == NULL) {
1813 if (PyErr_Occurred())
1814 goto BatchFailed;
1815 break;
1816 }
1817 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1818 PyErr_SetString(PyExc_TypeError, "dict items "
1819 "iterator must return 2-tuples");
1820 goto BatchFailed;
1821 }
1822 slice[n] = p;
1823 }
1824
1825 if (n > 1) {
1826 /* Pump out MARK, slice[0:n], SETITEMS. */
1827 if (self->write_func(self, &MARKv, 1) < 0)
1828 goto BatchFailed;
1829 for (i = 0; i < n; ++i) {
1830 p = slice[i];
1831 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1832 goto BatchFailed;
1833 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1834 goto BatchFailed;
1835 }
1836 if (self->write_func(self, &setitems, 1) < 0)
1837 goto BatchFailed;
1838 }
1839 else if (n == 1) {
1840 p = slice[0];
1841 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1842 goto BatchFailed;
1843 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1844 goto BatchFailed;
1845 if (self->write_func(self, &setitem, 1) < 0)
1846 goto BatchFailed;
1847 }
1848
1849 for (i = 0; i < n; ++i) {
1850 Py_DECREF(slice[i]);
1851 }
Tim Peters90975f12003-02-12 05:28:58 +00001852 } while (n == BATCHSIZE);
Tim Peters42f08ac2003-02-11 22:43:24 +00001853 return 0;
1854
1855BatchFailed:
1856 while (--n >= 0) {
1857 Py_DECREF(slice[n]);
1858 }
1859 return -1;
1860}
1861
Guido van Rossum60456fd1997-04-09 17:36:32 +00001862static int
Tim Peterscba30e22003-02-01 06:24:36 +00001863save_dict(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001864{
Tim Peters42f08ac2003-02-11 22:43:24 +00001865 int res = -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001866 char s[3];
Tim Peters42f08ac2003-02-11 22:43:24 +00001867 int len;
1868 PyObject *iter;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001869
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001870 if (self->fast && !fast_save_enter(self, args))
1871 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001872
Tim Peters42f08ac2003-02-11 22:43:24 +00001873 /* Create an empty dict. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001874 if (self->bin) {
1875 s[0] = EMPTY_DICT;
1876 len = 1;
1877 }
1878 else {
1879 s[0] = MARK;
1880 s[1] = DICT;
1881 len = 2;
1882 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001883
Tim Peters0bc93f52003-02-02 18:29:33 +00001884 if (self->write_func(self, s, len) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001885 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001886
Tim Peters42f08ac2003-02-11 22:43:24 +00001887 /* Get dict size, and bow out early if empty. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001888 if ((len = PyDict_Size(args)) < 0)
1889 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001890
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001891 if (len == 0) {
Tim Peters90975f12003-02-12 05:28:58 +00001892 if (put(self, args) >= 0)
1893 res = 0;
1894 goto finally;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001895 }
Tim Peters90975f12003-02-12 05:28:58 +00001896 if (put2(self, args) < 0)
1897 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001898
Tim Peters42f08ac2003-02-11 22:43:24 +00001899 /* Materialize the dict items. */
1900 iter = PyObject_CallMethod(args, "iteritems", "()");
1901 if (iter == NULL)
1902 goto finally;
1903 res = batch_dict(self, iter);
1904 Py_DECREF(iter);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001905
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001906 finally:
1907 if (self->fast && !fast_save_leave(self, args))
1908 res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001909
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001910 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001911}
1912
1913
Tim Peters84e87f32001-03-17 04:50:51 +00001914static int
Tim Peterscba30e22003-02-01 06:24:36 +00001915save_inst(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001916{
1917 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1918 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1919 char *module_str, *name_str;
1920 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001921
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001922 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001923
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001924 if (self->fast && !fast_save_enter(self, args))
1925 goto finally;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00001926
Tim Peters0bc93f52003-02-02 18:29:33 +00001927 if (self->write_func(self, &MARKv, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001928 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001929
Tim Peterscba30e22003-02-01 06:24:36 +00001930 if (!( class = PyObject_GetAttr(args, __class___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001931 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001932
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001933 if (self->bin) {
1934 if (save(self, class, 0) < 0)
1935 goto finally;
1936 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001937
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001938 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1939 PyObject *element = 0;
1940 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001941
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001942 if (!( class_args =
Tim Peterscba30e22003-02-01 06:24:36 +00001943 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001944 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001945
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001946 if ((len = PyObject_Size(class_args)) < 0)
1947 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001948
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001949 for (i = 0; i < len; i++) {
Tim Peterscba30e22003-02-01 06:24:36 +00001950 if (!( element = PySequence_GetItem(class_args, i)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001951 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001952
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001953 if (save(self, element, 0) < 0) {
1954 Py_DECREF(element);
1955 goto finally;
1956 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001957
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001958 Py_DECREF(element);
1959 }
1960 }
1961 else {
1962 PyErr_Clear();
1963 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001964
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001965 if (!self->bin) {
1966 if (!( name = ((PyClassObject *)class)->cl_name )) {
1967 PyErr_SetString(PicklingError, "class has no name");
1968 goto finally;
1969 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001970
Tim Peterscba30e22003-02-01 06:24:36 +00001971 if (!( module = whichmodule(class, name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001972 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001973
Tim Peters84e87f32001-03-17 04:50:51 +00001974
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001975 if ((module_size = PyString_Size(module)) < 0 ||
1976 (name_size = PyString_Size(name)) < 0)
1977 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001978
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001979 module_str = PyString_AS_STRING((PyStringObject *)module);
1980 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001981
Tim Peters0bc93f52003-02-02 18:29:33 +00001982 if (self->write_func(self, &inst, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001983 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001984
Tim Peters0bc93f52003-02-02 18:29:33 +00001985 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001986 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001987
Tim Peters0bc93f52003-02-02 18:29:33 +00001988 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001989 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001990
Tim Peters0bc93f52003-02-02 18:29:33 +00001991 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001992 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001993
Tim Peters0bc93f52003-02-02 18:29:33 +00001994 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001995 goto finally;
1996 }
Tim Peters0bc93f52003-02-02 18:29:33 +00001997 else if (self->write_func(self, &obj, 1) < 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00001998 goto finally;
1999 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002000
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002001 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2002 state = PyObject_Call(getstate_func, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00002003 if (!state)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002004 goto finally;
2005 }
2006 else {
2007 PyErr_Clear();
Guido van Rossum60456fd1997-04-09 17:36:32 +00002008
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002009 if (!( state = PyObject_GetAttr(args, __dict___str))) {
2010 PyErr_Clear();
2011 res = 0;
2012 goto finally;
2013 }
2014 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002015
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002016 if (!PyDict_Check(state)) {
2017 if (put2(self, args) < 0)
2018 goto finally;
2019 }
2020 else {
2021 if (put(self, args) < 0)
2022 goto finally;
2023 }
Tim Peters84e87f32001-03-17 04:50:51 +00002024
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002025 if (save(self, state, 0) < 0)
2026 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002027
Tim Peters0bc93f52003-02-02 18:29:33 +00002028 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002029 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002030
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002031 res = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002032
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002033 finally:
2034 if (self->fast && !fast_save_leave(self, args))
2035 res = -1;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002036
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002037 Py_XDECREF(module);
2038 Py_XDECREF(class);
2039 Py_XDECREF(state);
2040 Py_XDECREF(getinitargs_func);
2041 Py_XDECREF(getstate_func);
2042 Py_XDECREF(class_args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002043
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002044 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002045}
2046
2047
Guido van Rossum60456fd1997-04-09 17:36:32 +00002048static int
Tim Peterscba30e22003-02-01 06:24:36 +00002049save_global(Picklerobject *self, PyObject *args, PyObject *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002050{
Jeremy Hylton9ee91f12002-07-11 22:02:33 +00002051 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002052 char *name_str, *module_str;
2053 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002054
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002055 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002056
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002057 if (name) {
2058 global_name = name;
2059 Py_INCREF(global_name);
2060 }
2061 else {
Tim Peterscba30e22003-02-01 06:24:36 +00002062 if (!( global_name = PyObject_GetAttr(args, __name___str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002063 goto finally;
2064 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002065
Tim Peterscba30e22003-02-01 06:24:36 +00002066 if (!( module = whichmodule(args, global_name)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002067 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002068
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002069 if ((module_size = PyString_Size(module)) < 0 ||
2070 (name_size = PyString_Size(global_name)) < 0)
2071 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002072
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002073 module_str = PyString_AS_STRING((PyStringObject *)module);
2074 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002075
Guido van Rossum75bfd052002-12-24 18:10:07 +00002076 /* XXX This can be doing a relative import. Clearly it shouldn't,
2077 but I don't know how to stop it. :-( */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002078 mod = PyImport_ImportModule(module_str);
2079 if (mod == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002080 cPickle_ErrFormat(PicklingError,
2081 "Can't pickle %s: it's not found as %s.%s",
2082 "OSS", args, module, global_name);
2083 goto finally;
2084 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002085 klass = PyObject_GetAttrString(mod, name_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002086 if (klass == NULL) {
2087 cPickle_ErrFormat(PicklingError,
2088 "Can't pickle %s: it's not found as %s.%s",
2089 "OSS", args, module, global_name);
2090 goto finally;
2091 }
2092 if (klass != args) {
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002093 Py_DECREF(klass);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002094 cPickle_ErrFormat(PicklingError,
Tim Peters731098b2003-02-04 20:56:09 +00002095 "Can't pickle %s: it's not the same object "
2096 "as %s.%s",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002097 "OSS", args, module, global_name);
2098 goto finally;
2099 }
Jeremy Hylton0e1f7a82002-07-11 22:01:40 +00002100 Py_DECREF(klass);
Guido van Rossuma92d16a2001-08-18 21:22:07 +00002101
Tim Peters731098b2003-02-04 20:56:09 +00002102 if (self->proto >= 2) {
2103 /* See whether this is in the extension registry, and if
2104 * so generate an EXT opcode.
2105 */
2106 PyObject *py_code; /* extension code as Python object */
Tim Peters3e667d52003-02-04 21:47:44 +00002107 long code; /* extension code as C value */
Tim Peters731098b2003-02-04 20:56:09 +00002108 char c_str[5];
2109 int n;
2110
2111 PyTuple_SET_ITEM(two_tuple, 0, module);
2112 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2113 py_code = PyDict_GetItem(extension_registry, two_tuple);
2114 if (py_code == NULL)
2115 goto gen_global; /* not registered */
2116
2117 /* Verify py_code has the right type and value. */
2118 if (!PyInt_Check(py_code)) {
2119 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
Tim Peters6288e232003-02-05 03:53:10 +00002120 "extension code %s isn't an integer",
Tim Peters731098b2003-02-04 20:56:09 +00002121 "OO", args, py_code);
2122 goto finally;
2123 }
2124 code = PyInt_AS_LONG(py_code);
2125 if (code <= 0 || code > 0x7fffffffL) {
2126 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2127 "extension code %ld is out of range",
2128 "Ol", args, code);
2129 goto finally;
2130 }
2131
2132 /* Generate an EXT opcode. */
2133 if (code <= 0xff) {
2134 c_str[0] = EXT1;
2135 c_str[1] = (char)code;
2136 n = 2;
2137 }
2138 else if (code <= 0xffff) {
2139 c_str[0] = EXT2;
2140 c_str[1] = (char)(code & 0xff);
2141 c_str[2] = (char)((code >> 8) & 0xff);
2142 n = 3;
2143 }
2144 else {
2145 c_str[0] = EXT4;
2146 c_str[1] = (char)(code & 0xff);
2147 c_str[2] = (char)((code >> 8) & 0xff);
2148 c_str[3] = (char)((code >> 16) & 0xff);
2149 c_str[4] = (char)((code >> 24) & 0xff);
2150 n = 5;
2151 }
2152
2153 if (self->write_func(self, c_str, n) >= 0)
2154 res = 0;
2155 goto finally; /* and don't memoize */
2156 }
2157
2158 gen_global:
Tim Peters0bc93f52003-02-02 18:29:33 +00002159 if (self->write_func(self, &global, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002160 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002161
Tim Peters0bc93f52003-02-02 18:29:33 +00002162 if (self->write_func(self, module_str, module_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002163 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002164
Tim Peters0bc93f52003-02-02 18:29:33 +00002165 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002166 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002167
Tim Peters0bc93f52003-02-02 18:29:33 +00002168 if (self->write_func(self, name_str, name_size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002169 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002170
Tim Peters0bc93f52003-02-02 18:29:33 +00002171 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002172 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002173
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002174 if (put(self, args) < 0)
2175 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002176
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002177 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002178
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002179 finally:
2180 Py_XDECREF(module);
2181 Py_XDECREF(global_name);
2182 Py_XDECREF(mod);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002184 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002185}
2186
Guido van Rossum60456fd1997-04-09 17:36:32 +00002187static int
Tim Peterscba30e22003-02-01 06:24:36 +00002188save_pers(Picklerobject *self, PyObject *args, PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002189{
2190 PyObject *pid = 0;
2191 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002192
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002193 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002194
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002195 Py_INCREF(args);
2196 ARG_TUP(self, args);
2197 if (self->arg) {
2198 pid = PyObject_Call(f, self->arg, NULL);
2199 FREE_ARG_TUP(self);
2200 }
2201 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002203 if (pid != Py_None) {
2204 if (!self->bin) {
2205 if (!PyString_Check(pid)) {
2206 PyErr_SetString(PicklingError,
2207 "persistent id must be string");
2208 goto finally;
2209 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002210
Tim Peters0bc93f52003-02-02 18:29:33 +00002211 if (self->write_func(self, &persid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002212 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002213
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002214 if ((size = PyString_Size(pid)) < 0)
2215 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002216
Tim Peters0bc93f52003-02-02 18:29:33 +00002217 if (self->write_func(self,
2218 PyString_AS_STRING(
2219 (PyStringObject *)pid),
2220 size) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002221 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002222
Tim Peters0bc93f52003-02-02 18:29:33 +00002223 if (self->write_func(self, "\n", 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002224 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00002225
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002226 res = 1;
2227 goto finally;
2228 }
2229 else if (save(self, pid, 1) >= 0) {
Tim Peters0bc93f52003-02-02 18:29:33 +00002230 if (self->write_func(self, &binpersid, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002231 res = -1;
2232 else
2233 res = 1;
2234 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002235
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002236 goto finally;
2237 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002238
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002239 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002240
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002241 finally:
2242 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002243
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002244 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002245}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002246
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002247
Tim Peters84e87f32001-03-17 04:50:51 +00002248static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002249save_reduce(Picklerobject *self, PyObject *callable,
Tim Peterscba30e22003-02-01 06:24:36 +00002250 PyObject *tup, PyObject *state, PyObject *ob)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002251{
2252 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002253
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002254 if (save(self, callable, 0) < 0)
2255 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002256
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002257 if (save(self, tup, 0) < 0)
2258 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002259
Tim Peters0bc93f52003-02-02 18:29:33 +00002260 if (self->write_func(self, &reduce, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002261 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002262
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002263 if (ob != NULL) {
2264 if (state && !PyDict_Check(state)) {
2265 if (put2(self, ob) < 0)
2266 return -1;
2267 }
2268 else {
2269 if (put(self, ob) < 0)
2270 return -1;
2271 }
2272 }
Tim Peters84e87f32001-03-17 04:50:51 +00002273
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002274 if (state) {
2275 if (save(self, state, 0) < 0)
2276 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002277
Tim Peters0bc93f52003-02-02 18:29:33 +00002278 if (self->write_func(self, &build, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002279 return -1;
2280 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002281
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002282 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002283}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002284
Guido van Rossum60456fd1997-04-09 17:36:32 +00002285static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00002286save(Picklerobject *self, PyObject *args, int pers_save)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002287{
2288 PyTypeObject *type;
2289 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
2290 *callable = 0, *state = 0;
2291 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002292
Martin v. Löwis5a395302002-08-04 08:20:23 +00002293 if (self->nesting++ > Py_GetRecursionLimit()){
2294 PyErr_SetString(PyExc_RuntimeError,
2295 "maximum recursion depth exceeded");
2296 goto finally;
2297 }
2298
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002299 if (!pers_save && self->pers_func) {
2300 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2301 res = tmp;
2302 goto finally;
2303 }
2304 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002305
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002306 if (args == Py_None) {
2307 res = save_none(self, args);
2308 goto finally;
2309 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002310
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002311 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002312
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002313 switch (type->tp_name[0]) {
Guido van Rossum77f6a652002-04-03 22:41:51 +00002314 case 'b':
2315 if (args == Py_False || args == Py_True) {
2316 res = save_bool(self, args);
2317 goto finally;
2318 }
2319 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002320 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002321 if (type == &PyInt_Type) {
2322 res = save_int(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 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002328 if (type == &PyLong_Type) {
2329 res = save_long(self, args);
2330 goto finally;
2331 }
2332 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002333
Guido van Rossum60456fd1997-04-09 17:36:32 +00002334 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002335 if (type == &PyFloat_Type) {
2336 res = save_float(self, args);
2337 goto finally;
2338 }
2339 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002340
Guido van Rossum60456fd1997-04-09 17:36:32 +00002341 case 't':
Tim Peters1d63c9f2003-02-02 20:29:39 +00002342 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2343 res = save_tuple(self, args);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002344 goto finally;
2345 }
2346 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002347
Guido van Rossum60456fd1997-04-09 17:36:32 +00002348 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002349 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2350 res = save_string(self, args, 0);
2351 goto finally;
2352 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002353
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002354#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002355 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002356 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2357 res = save_unicode(self, args, 0);
2358 goto finally;
2359 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002360#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002361 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002362
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002363 if (args->ob_refcnt > 1) {
Tim Peterscba30e22003-02-01 06:24:36 +00002364 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002365 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002366
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002367 if (PyDict_GetItem(self->memo, py_ob_id)) {
2368 if (get(self, py_ob_id) < 0)
2369 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002370
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002371 res = 0;
2372 goto finally;
2373 }
2374 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002375
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002376 switch (type->tp_name[0]) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002377 case 's':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002378 if (type == &PyString_Type) {
2379 res = save_string(self, args, 1);
2380 goto finally;
2381 }
2382 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002383
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002384#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002385 case 'u':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002386 if (type == &PyUnicode_Type) {
2387 res = save_unicode(self, args, 1);
2388 goto finally;
2389 }
2390 break;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002391#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002392
Guido van Rossum60456fd1997-04-09 17:36:32 +00002393 case 't':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002394 if (type == &PyTuple_Type) {
2395 res = save_tuple(self, args);
2396 goto finally;
2397 }
2398 if (type == &PyType_Type) {
2399 res = save_global(self, args, NULL);
2400 goto finally;
2401 }
2402 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002403
Guido van Rossum60456fd1997-04-09 17:36:32 +00002404 case 'l':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002405 if (type == &PyList_Type) {
2406 res = save_list(self, args);
2407 goto finally;
2408 }
2409 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002410
2411 case 'd':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002412 if (type == &PyDict_Type) {
2413 res = save_dict(self, args);
2414 goto finally;
2415 }
2416 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002417
2418 case 'i':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002419 if (type == &PyInstance_Type) {
2420 res = save_inst(self, args);
2421 goto finally;
2422 }
2423 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002424
2425 case 'c':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002426 if (type == &PyClass_Type) {
2427 res = save_global(self, args, NULL);
2428 goto finally;
2429 }
2430 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002431
2432 case 'f':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002433 if (type == &PyFunction_Type) {
2434 res = save_global(self, args, NULL);
2435 goto finally;
2436 }
2437 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002438
2439 case 'b':
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002440 if (type == &PyCFunction_Type) {
2441 res = save_global(self, args, NULL);
2442 goto finally;
2443 }
2444 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002445
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002446 if (!pers_save && self->inst_pers_func) {
2447 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2448 res = tmp;
2449 goto finally;
2450 }
2451 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002452
Jeremy Hylton39c61162002-07-16 19:47:43 +00002453 if (PyType_IsSubtype(type, &PyType_Type)) {
2454 res = save_global(self, args, NULL);
2455 goto finally;
2456 }
2457
Tim Peters5aa3da62003-02-13 21:03:57 +00002458 assert(t == NULL); /* just a reminder */
2459 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2460 if (__reduce__ != NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002461 Py_INCREF(__reduce__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002462 Py_INCREF(args);
2463 ARG_TUP(self, args);
2464 if (self->arg) {
2465 t = PyObject_Call(__reduce__, self->arg, NULL);
2466 FREE_ARG_TUP(self);
2467 }
2468 if (! t) goto finally;
2469 }
2470 else {
Tim Peters5aa3da62003-02-13 21:03:57 +00002471 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2472 if (__reduce__ == NULL)
2473 PyErr_Clear();
2474 else {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002475 t = PyObject_Call(__reduce__, empty_tuple, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00002476 if (!t)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002477 goto finally;
2478 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002479 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002480
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002481 if (t) {
2482 if (PyString_Check(t)) {
2483 res = save_global(self, args, t);
2484 goto finally;
2485 }
Tim Peters84e87f32001-03-17 04:50:51 +00002486
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002487 if (!PyTuple_Check(t)) {
Tim Peters5aa3da62003-02-13 21:03:57 +00002488 cPickle_ErrFormat(PicklingError, "Value returned by "
2489 "%s must be a tuple",
2490 "O", __reduce__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002491 goto finally;
2492 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002493
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002494 size = PyTuple_Size(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002495
Tim Peters5aa3da62003-02-13 21:03:57 +00002496 if (size != 3 && size != 2) {
2497 cPickle_ErrFormat(PicklingError, "tuple returned by "
2498 "%s must contain only two or three elements",
2499 "O", __reduce__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002500 goto finally;
2501 }
Tim Peters84e87f32001-03-17 04:50:51 +00002502
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002503 callable = PyTuple_GET_ITEM(t, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002504 arg_tup = PyTuple_GET_ITEM(t, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002505
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002506 if (size > 2) {
2507 state = PyTuple_GET_ITEM(t, 2);
Guido van Rossum8e0ad0c2003-01-31 21:10:31 +00002508 if (state == Py_None)
2509 state = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002510 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002511
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002512 if (!( PyTuple_Check(arg_tup) || arg_tup==Py_None )) {
Tim Peters5aa3da62003-02-13 21:03:57 +00002513 cPickle_ErrFormat(PicklingError, "Second element of "
2514 "tuple returned by %s must be a tuple",
2515 "O", __reduce__);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002516 goto finally;
2517 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002518
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002519 res = save_reduce(self, callable, arg_tup, state, args);
2520 goto finally;
2521 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002522
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002523 PyErr_SetObject(UnpickleableError, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002524
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002525 finally:
Martin v. Löwis5a395302002-08-04 08:20:23 +00002526 self->nesting--;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002527 Py_XDECREF(py_ob_id);
2528 Py_XDECREF(__reduce__);
2529 Py_XDECREF(t);
Tim Peters84e87f32001-03-17 04:50:51 +00002530
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002531 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002532}
2533
2534
2535static int
Tim Peterscba30e22003-02-01 06:24:36 +00002536dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002537{
2538 static char stop = STOP;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002539
Tim Peters4190fb82003-02-02 16:09:05 +00002540 if (self->proto >= 2) {
2541 char bytes[2];
2542
2543 bytes[0] = PROTO;
Tim Peters87482ea2003-02-02 16:16:30 +00002544 assert(self->proto >= 0 && self->proto < 256);
2545 bytes[1] = (char)self->proto;
Tim Peters4190fb82003-02-02 16:09:05 +00002546 if (self->write_func(self, bytes, 2) < 0)
2547 return -1;
2548 }
2549
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002550 if (save(self, args, 0) < 0)
2551 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002552
Tim Peters4190fb82003-02-02 16:09:05 +00002553 if (self->write_func(self, &stop, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002554 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002555
Tim Peters4190fb82003-02-02 16:09:05 +00002556 if (self->write_func(self, NULL, 0) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002557 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002558
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002559 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002560}
2561
2562static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002563Pickle_clear_memo(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002564{
Tim Peterscba30e22003-02-01 06:24:36 +00002565 if (self->memo)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002566 PyDict_Clear(self->memo);
2567 Py_INCREF(Py_None);
2568 return Py_None;
2569}
2570
2571static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002572Pickle_getvalue(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002573{
2574 int l, i, rsize, ssize, clear=1, lm;
2575 long ik;
2576 PyObject *k, *r;
2577 char *s, *p, *have_get;
2578 Pdata *data;
2579
2580 /* Can be called by Python code or C code */
Tim Peterscba30e22003-02-01 06:24:36 +00002581 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002582 return NULL;
2583
2584 /* Check to make sure we are based on a list */
2585 if (! Pdata_Check(self->file)) {
2586 PyErr_SetString(PicklingError,
2587 "Attempt to getvalue() a non-list-based pickler");
2588 return NULL;
2589 }
2590
2591 /* flush write buffer */
2592 if (write_other(self, NULL, 0) < 0) return NULL;
2593
2594 data=(Pdata*)self->file;
2595 l=data->length;
2596
2597 /* set up an array to hold get/put status */
Tim Petersac5687a2003-02-02 18:08:34 +00002598 lm = PyDict_Size(self->memo);
2599 if (lm < 0) return NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002600 lm++;
Tim Petersac5687a2003-02-02 18:08:34 +00002601 have_get = malloc(lm);
2602 if (have_get == NULL) return PyErr_NoMemory();
2603 memset(have_get, 0, lm);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002604
2605 /* Scan for gets. */
Tim Petersac5687a2003-02-02 18:08:34 +00002606 for (rsize = 0, i = l; --i >= 0; ) {
2607 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002608
Tim Petersac5687a2003-02-02 18:08:34 +00002609 if (PyString_Check(k))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002610 rsize += PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002611
2612 else if (PyInt_Check(k)) { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002613 ik = PyInt_AS_LONG((PyIntObject*)k);
2614 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002615 PyErr_SetString(PicklingError,
2616 "Invalid get data");
2617 return NULL;
2618 }
Tim Petersac5687a2003-02-02 18:08:34 +00002619 if (have_get[ik]) /* with matching get */
2620 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002621 }
2622
2623 else if (! (PyTuple_Check(k) &&
2624 PyTuple_GET_SIZE(k) == 2 &&
Tim Petersac5687a2003-02-02 18:08:34 +00002625 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002626 ) {
2627 PyErr_SetString(PicklingError,
2628 "Unexpected data in internal list");
2629 return NULL;
2630 }
2631
2632 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002633 ik = PyInt_AS_LONG((PyIntObject *)k);
2634 if (ik >= lm || ik == 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002635 PyErr_SetString(PicklingError,
2636 "Invalid get data");
2637 return NULL;
2638 }
Tim Petersac5687a2003-02-02 18:08:34 +00002639 have_get[ik] = 1;
2640 rsize += ik < 256 ? 2 : 5;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002641 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002642 }
2643
2644 /* Now generate the result */
Tim Petersac5687a2003-02-02 18:08:34 +00002645 r = PyString_FromStringAndSize(NULL, rsize);
2646 if (r == NULL) goto err;
2647 s = PyString_AS_STRING((PyStringObject *)r);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002648
Tim Petersac5687a2003-02-02 18:08:34 +00002649 for (i = 0; i < l; i++) {
2650 k = data->data[i];
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002651
2652 if (PyString_Check(k)) {
Tim Petersac5687a2003-02-02 18:08:34 +00002653 ssize = PyString_GET_SIZE(k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002654 if (ssize) {
Tim Petersac5687a2003-02-02 18:08:34 +00002655 p=PyString_AS_STRING((PyStringObject *)k);
2656 while (--ssize >= 0)
2657 *s++ = *p++;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002658 }
2659 }
2660
2661 else if (PyTuple_Check(k)) { /* get */
Tim Petersac5687a2003-02-02 18:08:34 +00002662 ik = PyInt_AS_LONG((PyIntObject *)
2663 PyTuple_GET_ITEM(k, 0));
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002664 if (ik < 256) {
2665 *s++ = BINGET;
2666 *s++ = (int)(ik & 0xff);
2667 }
2668 else {
2669 *s++ = LONG_BINGET;
2670 *s++ = (int)(ik & 0xff);
2671 *s++ = (int)((ik >> 8) & 0xff);
2672 *s++ = (int)((ik >> 16) & 0xff);
2673 *s++ = (int)((ik >> 24) & 0xff);
2674 }
2675 }
2676
2677 else { /* put */
Tim Petersac5687a2003-02-02 18:08:34 +00002678 ik = PyInt_AS_LONG((PyIntObject*)k);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002679
2680 if (have_get[ik]) { /* with matching get */
2681 if (ik < 256) {
2682 *s++ = BINPUT;
2683 *s++ = (int)(ik & 0xff);
2684 }
2685 else {
2686 *s++ = LONG_BINPUT;
2687 *s++ = (int)(ik & 0xff);
2688 *s++ = (int)((ik >> 8) & 0xff);
2689 *s++ = (int)((ik >> 16) & 0xff);
2690 *s++ = (int)((ik >> 24) & 0xff);
2691 }
2692 }
2693 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002694 }
2695
2696 if (clear) {
2697 PyDict_Clear(self->memo);
Tim Petersac5687a2003-02-02 18:08:34 +00002698 Pdata_clear(data, 0);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002699 }
2700
2701 free(have_get);
2702 return r;
2703 err:
2704 free(have_get);
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002705 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002706}
2707
2708static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002709Pickler_dump(Picklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002710{
2711 PyObject *ob;
2712 int get=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002713
Tim Peterscba30e22003-02-01 06:24:36 +00002714 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002715 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002716
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002717 if (dump(self, ob) < 0)
2718 return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002719
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002720 if (get) return Pickle_getvalue(self, NULL);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002721
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002722 /* XXX Why does dump() return self? */
2723 Py_INCREF(self);
2724 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002725}
2726
2727
Tim Peterscba30e22003-02-01 06:24:36 +00002728static struct PyMethodDef Pickler_methods[] =
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002729{
Neal Norwitzb0493252002-03-31 14:44:22 +00002730 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002731 PyDoc_STR("dump(object) -- "
2732 "Write an object in pickle format to the object's pickle stream")},
Fred Drake0ebacc82002-05-01 20:36:39 +00002733 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002734 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
Neal Norwitzb0493252002-03-31 14:44:22 +00002735 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00002736 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002737 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002738};
2739
2740
2741static Picklerobject *
Tim Peters5bd2a792003-02-01 16:45:06 +00002742newPicklerobject(PyObject *file, int proto)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002743{
2744 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002745
Tim Peters5bd2a792003-02-01 16:45:06 +00002746 if (proto < 0)
Tim Peters8587b3c2003-02-13 15:44:41 +00002747 proto = HIGHEST_PROTOCOL;
2748 if (proto > HIGHEST_PROTOCOL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002749 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2750 "the highest available protocol is %d",
Tim Peters8587b3c2003-02-13 15:44:41 +00002751 proto, HIGHEST_PROTOCOL);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002752 return NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002753 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002754
Tim Peters5bd2a792003-02-01 16:45:06 +00002755 self = PyObject_New(Picklerobject, &Picklertype);
2756 if (self == NULL)
2757 return NULL;
2758 self->proto = proto;
2759 self->bin = proto > 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002760 self->fp = NULL;
2761 self->write = NULL;
2762 self->memo = NULL;
2763 self->arg = NULL;
2764 self->pers_func = NULL;
2765 self->inst_pers_func = NULL;
2766 self->write_buf = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002767 self->fast = 0;
Martin v. Löwis5a395302002-08-04 08:20:23 +00002768 self->nesting = 0;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002769 self->fast_container = 0;
2770 self->fast_memo = NULL;
2771 self->buf_size = 0;
2772 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002773
Tim Peters5bd2a792003-02-01 16:45:06 +00002774 self->file = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002775 if (file)
2776 Py_INCREF(file);
Tim Peters5bd2a792003-02-01 16:45:06 +00002777 else {
2778 file = Pdata_New();
2779 if (file == NULL)
2780 goto err;
2781 }
2782 self->file = file;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002783
Tim Peterscba30e22003-02-01 06:24:36 +00002784 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002785 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002786
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002787 if (PyFile_Check(file)) {
2788 self->fp = PyFile_AsFile(file);
2789 if (self->fp == NULL) {
Tim Peters5bd2a792003-02-01 16:45:06 +00002790 PyErr_SetString(PyExc_ValueError,
2791 "I/O operation on closed file");
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002792 goto err;
2793 }
2794 self->write_func = write_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002795 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002796 else if (PycStringIO_OutputCheck(file)) {
2797 self->write_func = write_cStringIO;
2798 }
2799 else if (file == Py_None) {
2800 self->write_func = write_none;
2801 }
2802 else {
2803 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002804
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002805 if (! Pdata_Check(file)) {
2806 self->write = PyObject_GetAttr(file, write_str);
2807 if (!self->write) {
2808 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00002809 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002810 "argument must have 'write' "
2811 "attribute");
2812 goto err;
2813 }
2814 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002815
Tim Peters5bd2a792003-02-01 16:45:06 +00002816 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2817 if (self->write_buf == NULL) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002818 PyErr_NoMemory();
2819 goto err;
2820 }
2821 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002822
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002823 if (PyEval_GetRestricted()) {
2824 /* Restricted execution, get private tables */
Tim Peters5b7da392003-02-04 00:21:07 +00002825 PyObject *m = PyImport_Import(copy_reg_str);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002826
Tim Peters5b7da392003-02-04 00:21:07 +00002827 if (m == NULL)
2828 goto err;
2829 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002830 Py_DECREF(m);
Tim Peters5b7da392003-02-04 00:21:07 +00002831 if (self->dispatch_table == NULL)
2832 goto err;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002833 }
2834 else {
Tim Peters5b7da392003-02-04 00:21:07 +00002835 self->dispatch_table = dispatch_table;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002836 Py_INCREF(dispatch_table);
2837 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002838
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002839 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002840
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002841 err:
Tim Peters5bd2a792003-02-01 16:45:06 +00002842 Py_DECREF(self);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002843 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002844}
2845
2846
2847static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00002848get_Pickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002849{
2850 PyObject *file = NULL;
Tim Peters5bd2a792003-02-01 16:45:06 +00002851 int proto = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002852
Tim Peters5bd2a792003-02-01 16:45:06 +00002853 /* XXX What is this doing? The documented signature is
2854 * XXX Pickler(file, proto=0), but this accepts Pickler() and
2855 * XXX Pickler(integer) too. The meaning then is clear as mud.
2856 * XXX Bug? Feature?
2857 */
2858 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002859 PyErr_Clear();
Tim Peters5bd2a792003-02-01 16:45:06 +00002860 proto = 0;
2861 if (!PyArg_ParseTuple(args, "O|i:Pickler", &file, &proto))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002862 return NULL;
2863 }
Tim Peters5bd2a792003-02-01 16:45:06 +00002864 return (PyObject *)newPicklerobject(file, proto);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002865}
2866
2867
2868static void
Tim Peterscba30e22003-02-01 06:24:36 +00002869Pickler_dealloc(Picklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002870{
2871 Py_XDECREF(self->write);
2872 Py_XDECREF(self->memo);
2873 Py_XDECREF(self->fast_memo);
2874 Py_XDECREF(self->arg);
2875 Py_XDECREF(self->file);
2876 Py_XDECREF(self->pers_func);
2877 Py_XDECREF(self->inst_pers_func);
2878 Py_XDECREF(self->dispatch_table);
Tim Peters5bd2a792003-02-01 16:45:06 +00002879 PyMem_Free(self->write_buf);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002880 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002881}
2882
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002883static PyObject *
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002884Pickler_get_pers_func(Picklerobject *p)
2885{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002886 if (p->pers_func == NULL)
2887 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2888 else
2889 Py_INCREF(p->pers_func);
2890 return p->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002891}
2892
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002893static int
2894Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2895{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002896 if (v == NULL) {
2897 PyErr_SetString(PyExc_TypeError,
2898 "attribute deletion is not supported");
2899 return -1;
2900 }
2901 Py_XDECREF(p->pers_func);
2902 Py_INCREF(v);
2903 p->pers_func = v;
2904 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002905}
2906
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002907static int
2908Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2909{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002910 if (v == NULL) {
2911 PyErr_SetString(PyExc_TypeError,
2912 "attribute deletion is not supported");
2913 return -1;
2914 }
2915 Py_XDECREF(p->inst_pers_func);
2916 Py_INCREF(v);
2917 p->inst_pers_func = v;
2918 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002919}
2920
2921static PyObject *
2922Pickler_get_memo(Picklerobject *p)
2923{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002924 if (p->memo == NULL)
2925 PyErr_SetString(PyExc_AttributeError, "memo");
2926 else
2927 Py_INCREF(p->memo);
2928 return p->memo;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002929}
2930
2931static int
2932Pickler_set_memo(Picklerobject *p, PyObject *v)
2933{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002934 if (v == NULL) {
2935 PyErr_SetString(PyExc_TypeError,
2936 "attribute deletion is not supported");
2937 return -1;
2938 }
2939 if (!PyDict_Check(v)) {
2940 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2941 return -1;
2942 }
2943 Py_XDECREF(p->memo);
2944 Py_INCREF(v);
2945 p->memo = v;
2946 return 0;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002947}
2948
2949static PyObject *
2950Pickler_get_error(Picklerobject *p)
2951{
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00002952 /* why is this an attribute on the Pickler? */
2953 Py_INCREF(PicklingError);
2954 return PicklingError;
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002955}
2956
2957static PyMemberDef Pickler_members[] = {
2958 {"binary", T_INT, offsetof(Picklerobject, bin)},
2959 {"fast", T_INT, offsetof(Picklerobject, fast)},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00002960 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002961};
2962
2963static PyGetSetDef Pickler_getsets[] = {
Tim Peterscba30e22003-02-01 06:24:36 +00002964 {"persistent_id", (getter)Pickler_get_pers_func,
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002965 (setter)Pickler_set_pers_func},
2966 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
2967 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
Jeremy Hylton3eb46f32001-10-16 17:10:49 +00002968 {"PicklingError", (getter)Pickler_get_error, NULL},
2969 {NULL}
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002970};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002971
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002972PyDoc_STRVAR(Picklertype__doc__,
2973"Objects that know how to pickle objects\n");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002974
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002975static PyTypeObject Picklertype = {
2976 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002977 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00002978 "cPickle.Pickler", /*tp_name*/
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002979 sizeof(Picklerobject), /*tp_basicsize*/
2980 0,
2981 (destructor)Pickler_dealloc, /* tp_dealloc */
2982 0, /* tp_print */
2983 0, /* tp_getattr */
2984 0, /* tp_setattr */
2985 0, /* tp_compare */
2986 0, /* tp_repr */
2987 0, /* tp_as_number */
2988 0, /* tp_as_sequence */
2989 0, /* tp_as_mapping */
2990 0, /* tp_hash */
2991 0, /* tp_call */
2992 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00002993 PyObject_GenericGetAttr, /* tp_getattro */
2994 PyObject_GenericSetAttr, /* tp_setattro */
Jeremy Hyltona0fb1772001-10-12 04:11:06 +00002995 0, /* tp_as_buffer */
2996 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2997 Picklertype__doc__, /* tp_doc */
2998 0, /* tp_traverse */
2999 0, /* tp_clear */
3000 0, /* tp_richcompare */
3001 0, /* tp_weaklistoffset */
3002 0, /* tp_iter */
3003 0, /* tp_iternext */
3004 Pickler_methods, /* tp_methods */
3005 Pickler_members, /* tp_members */
3006 Pickler_getsets, /* tp_getset */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003007};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003008
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003009static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003010find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003011{
3012 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003013
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003014 if (fc) {
3015 if (fc==Py_None) {
3016 PyErr_SetString(UnpicklingError,
3017 "Global and instance pickles are not supported.");
3018 return NULL;
3019 }
Tim Peterscba30e22003-02-01 06:24:36 +00003020 return PyObject_CallFunction(fc, "OO", py_module_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003021 py_global_name);
3022 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003023
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003024 module = PySys_GetObject("modules");
3025 if (module == NULL)
3026 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00003027
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003028 module = PyDict_GetItem(module, py_module_name);
3029 if (module == NULL) {
3030 module = PyImport_Import(py_module_name);
3031 if (!module)
3032 return NULL;
3033 global = PyObject_GetAttr(module, py_global_name);
3034 Py_DECREF(module);
3035 }
3036 else
3037 global = PyObject_GetAttr(module, py_global_name);
3038 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003039}
3040
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003041static int
Tim Peterscba30e22003-02-01 06:24:36 +00003042marker(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003043{
3044 if (self->num_marks < 1) {
3045 PyErr_SetString(UnpicklingError, "could not find MARK");
3046 return -1;
3047 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003048
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003049 return self->marks[--self->num_marks];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003050}
3051
Tim Peters84e87f32001-03-17 04:50:51 +00003052
Guido van Rossum60456fd1997-04-09 17:36:32 +00003053static int
Tim Peterscba30e22003-02-01 06:24:36 +00003054load_none(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003055{
3056 PDATA_APPEND(self->stack, Py_None, -1);
3057 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003058}
3059
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003060static int
Tim Peterscba30e22003-02-01 06:24:36 +00003061bad_readline(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003062{
3063 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3064 return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003065}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003066
3067static int
Tim Peterscba30e22003-02-01 06:24:36 +00003068load_int(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003069{
3070 PyObject *py_int = 0;
3071 char *endptr, *s;
3072 int len, res = -1;
3073 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003074
Tim Peters0bc93f52003-02-02 18:29:33 +00003075 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003076 if (len < 2) return bad_readline();
3077 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003078
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003079 errno = 0;
3080 l = strtol(s, &endptr, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003081
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003082 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3083 /* Hm, maybe we've got something long. Let's try reading
3084 it as a Python long object. */
3085 errno = 0;
3086 py_int = PyLong_FromString(s, NULL, 0);
3087 if (py_int == NULL) {
3088 PyErr_SetString(PyExc_ValueError,
3089 "could not convert string to int");
3090 goto finally;
3091 }
3092 }
3093 else {
Guido van Rossume2763392002-04-05 19:30:08 +00003094 if (len == 3 && (l == 0 || l == 1)) {
3095 if (!( py_int = PyBool_FromLong(l))) goto finally;
3096 }
3097 else {
3098 if (!( py_int = PyInt_FromLong(l))) goto finally;
3099 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003100 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003101
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003102 free(s);
3103 PDATA_PUSH(self->stack, py_int, -1);
3104 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003105
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003106 finally:
3107 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003108
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003109 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003110}
3111
Tim Peters3c67d792003-02-02 17:59:11 +00003112static int
3113load_bool(Unpicklerobject *self, PyObject *boolean)
3114{
3115 assert(boolean == Py_True || boolean == Py_False);
Tim Peterse0a39072003-02-03 15:45:56 +00003116 PDATA_APPEND(self->stack, boolean, -1);
Tim Peters3c67d792003-02-02 17:59:11 +00003117 return 0;
3118}
3119
Tim Petersee1a53c2003-02-02 02:57:53 +00003120/* s contains x bytes of a little-endian integer. Return its value as a
3121 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3122 * int, but when x is 4 it's a signed one. This is an historical source
3123 * of x-platform bugs.
3124 */
Tim Peters84e87f32001-03-17 04:50:51 +00003125static long
Tim Petersee1a53c2003-02-02 02:57:53 +00003126calc_binint(char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003127{
3128 unsigned char c;
3129 int i;
3130 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003131
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003132 for (i = 0, l = 0L; i < x; i++) {
3133 c = (unsigned char)s[i];
3134 l |= (long)c << (i * 8);
3135 }
Tim Petersbfa18f72001-04-10 01:54:42 +00003136#if SIZEOF_LONG > 4
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003137 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3138 * is signed, so on a box with longs bigger than 4 bytes we need
3139 * to extend a BININT's sign bit to the full width.
3140 */
3141 if (x == 4 && l & (1L << 31))
3142 l |= (~0L) << 32;
Tim Petersbfa18f72001-04-10 01:54:42 +00003143#endif
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003144 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003145}
3146
3147
3148static int
Tim Peterscba30e22003-02-01 06:24:36 +00003149load_binintx(Unpicklerobject *self, char *s, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003150{
3151 PyObject *py_int = 0;
3152 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003153
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003154 l = calc_binint(s, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003155
Tim Peterscba30e22003-02-01 06:24:36 +00003156 if (!( py_int = PyInt_FromLong(l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003157 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003158
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003159 PDATA_PUSH(self->stack, py_int, -1);
3160 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003161}
3162
3163
3164static int
Tim Peterscba30e22003-02-01 06:24:36 +00003165load_binint(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003166{
3167 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003168
Tim Peters0bc93f52003-02-02 18:29:33 +00003169 if (self->read_func(self, &s, 4) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003170 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003171
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003172 return load_binintx(self, s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003173}
3174
3175
3176static int
Tim Peterscba30e22003-02-01 06:24:36 +00003177load_binint1(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003178{
3179 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003180
Tim Peters0bc93f52003-02-02 18:29:33 +00003181 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003182 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003183
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003184 return load_binintx(self, s, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003185}
3186
3187
3188static int
Tim Peterscba30e22003-02-01 06:24:36 +00003189load_binint2(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003190{
3191 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003192
Tim Peters0bc93f52003-02-02 18:29:33 +00003193 if (self->read_func(self, &s, 2) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003194 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003195
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003196 return load_binintx(self, s, 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003197}
Tim Peters84e87f32001-03-17 04:50:51 +00003198
Guido van Rossum60456fd1997-04-09 17:36:32 +00003199static int
Tim Peterscba30e22003-02-01 06:24:36 +00003200load_long(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003201{
3202 PyObject *l = 0;
3203 char *end, *s;
3204 int len, res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003205
Tim Peters0bc93f52003-02-02 18:29:33 +00003206 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003207 if (len < 2) return bad_readline();
3208 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003209
Tim Peterscba30e22003-02-01 06:24:36 +00003210 if (!( l = PyLong_FromString(s, &end, 0)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003211 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003212
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003213 free(s);
3214 PDATA_PUSH(self->stack, l, -1);
3215 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003216
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003217 finally:
3218 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003219
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003220 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003221}
3222
Tim Petersee1a53c2003-02-02 02:57:53 +00003223/* 'size' bytes contain the # of bytes of little-endian 256's-complement
3224 * data following.
3225 */
3226static int
3227load_counted_long(Unpicklerobject *self, int size)
3228{
3229 int i;
3230 char *nbytes;
3231 unsigned char *pdata;
3232 PyObject *along;
3233
3234 assert(size == 1 || size == 4);
3235 i = self->read_func(self, &nbytes, size);
3236 if (i < 0) return -1;
3237
3238 size = calc_binint(nbytes, size);
3239 if (size < 0) {
3240 /* Corrupt or hostile pickle -- we never write one like
3241 * this.
3242 */
Tim Peters0c7c48e2003-02-03 22:07:24 +00003243 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
Tim Petersee1a53c2003-02-02 02:57:53 +00003244 "byte count");
3245 return -1;
3246 }
3247
3248 if (size == 0)
3249 along = PyLong_FromLong(0L);
3250 else {
3251 /* Read the raw little-endian bytes & convert. */
Neal Norwitz529baf22003-02-02 17:08:33 +00003252 i = self->read_func(self, (char **)&pdata, size);
Tim Petersee1a53c2003-02-02 02:57:53 +00003253 if (i < 0) return -1;
3254 along = _PyLong_FromByteArray(pdata, (size_t)size,
3255 1 /* little endian */, 1 /* signed */);
3256 }
3257 if (along == NULL)
3258 return -1;
3259 PDATA_PUSH(self->stack, along, -1);
3260 return 0;
3261}
Tim Peters84e87f32001-03-17 04:50:51 +00003262
Guido van Rossum60456fd1997-04-09 17:36:32 +00003263static int
Tim Peterscba30e22003-02-01 06:24:36 +00003264load_float(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003265{
3266 PyObject *py_float = 0;
3267 char *endptr, *s;
3268 int len, res = -1;
3269 double d;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003270
Tim Peters0bc93f52003-02-02 18:29:33 +00003271 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003272 if (len < 2) return bad_readline();
3273 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003274
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003275 errno = 0;
3276 d = strtod(s, &endptr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003277
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003278 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3279 PyErr_SetString(PyExc_ValueError,
3280 "could not convert string to float");
3281 goto finally;
3282 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003283
Tim Peterscba30e22003-02-01 06:24:36 +00003284 if (!( py_float = PyFloat_FromDouble(d)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003285 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003286
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003287 free(s);
3288 PDATA_PUSH(self->stack, py_float, -1);
3289 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003290
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003291 finally:
3292 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003293
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003294 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003295}
3296
Guido van Rossum60456fd1997-04-09 17:36:32 +00003297static int
Tim Peterscba30e22003-02-01 06:24:36 +00003298load_binfloat(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003299{
3300 PyObject *py_float = 0;
3301 int s, e;
3302 long fhi, flo;
3303 double x;
3304 char *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003305
Tim Peters0bc93f52003-02-02 18:29:33 +00003306 if (self->read_func(self, &p, 8) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003307 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003308
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003309 /* First byte */
3310 s = (*p>>7) & 1;
3311 e = (*p & 0x7F) << 4;
3312 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003313
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003314 /* Second byte */
3315 e |= (*p>>4) & 0xF;
3316 fhi = (*p & 0xF) << 24;
3317 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003318
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003319 /* Third byte */
3320 fhi |= (*p & 0xFF) << 16;
3321 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003322
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003323 /* Fourth byte */
3324 fhi |= (*p & 0xFF) << 8;
3325 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003326
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003327 /* Fifth byte */
3328 fhi |= *p & 0xFF;
3329 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003330
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003331 /* Sixth byte */
3332 flo = (*p & 0xFF) << 16;
3333 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003334
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003335 /* Seventh byte */
3336 flo |= (*p & 0xFF) << 8;
3337 p++;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003338
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003339 /* Eighth byte */
3340 flo |= *p & 0xFF;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003341
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003342 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
3343 x /= 268435456.0; /* 2**28 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003344
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003345 /* XXX This sadly ignores Inf/NaN */
3346 if (e == 0)
3347 e = -1022;
3348 else {
3349 x += 1.0;
3350 e -= 1023;
3351 }
3352 x = ldexp(x, e);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003353
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003354 if (s)
3355 x = -x;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003356
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003357 if (!( py_float = PyFloat_FromDouble(x))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003358
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003359 PDATA_PUSH(self->stack, py_float, -1);
3360 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003361}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003362
3363static int
Tim Peterscba30e22003-02-01 06:24:36 +00003364load_string(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003365{
3366 PyObject *str = 0;
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003367 int len, res = -1;
3368 char *s, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003369
Tim Peters0bc93f52003-02-02 18:29:33 +00003370 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003371 if (len < 2) return bad_readline();
3372 if (!( s=pystrndup(s,len))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003373
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003374
3375 /* Strip outermost quotes */
3376 while (s[len-1] <= ' ')
3377 len--;
3378 if(s[0]=='"' && s[len-1]=='"'){
3379 s[len-1] = '\0';
3380 p = s + 1 ;
3381 len -= 2;
3382 } else if(s[0]=='\'' && s[len-1]=='\''){
3383 s[len-1] = '\0';
3384 p = s + 1 ;
3385 len -= 2;
3386 } else
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003387 goto insecure;
3388 /********************************************/
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003389
Martin v. Löwis8a8da792002-08-14 07:46:28 +00003390 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3391 if (str) {
3392 PDATA_PUSH(self->stack, str, -1);
3393 res = 0;
3394 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003395 free(s);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003396 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003397
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003398 insecure:
3399 free(s);
3400 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3401 return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003402}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003403
3404
3405static int
Tim Peterscba30e22003-02-01 06:24:36 +00003406load_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003407{
3408 PyObject *py_string = 0;
3409 long l;
3410 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003411
Tim Peters0bc93f52003-02-02 18:29:33 +00003412 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003413
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003414 l = calc_binint(s, 4);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003415
Tim Peters0bc93f52003-02-02 18:29:33 +00003416 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003417 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003418
Tim Peterscba30e22003-02-01 06:24:36 +00003419 if (!( py_string = PyString_FromStringAndSize(s, l)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003420 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003421
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003422 PDATA_PUSH(self->stack, py_string, -1);
3423 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003424}
3425
3426
3427static int
Tim Peterscba30e22003-02-01 06:24:36 +00003428load_short_binstring(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003429{
3430 PyObject *py_string = 0;
3431 unsigned char l;
3432 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003433
Tim Peters0bc93f52003-02-02 18:29:33 +00003434 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003435 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003436
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003437 l = (unsigned char)s[0];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003438
Tim Peters0bc93f52003-02-02 18:29:33 +00003439 if (self->read_func(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003440
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003441 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003442
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003443 PDATA_PUSH(self->stack, py_string, -1);
3444 return 0;
Tim Peters84e87f32001-03-17 04:50:51 +00003445}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003446
3447
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003448#ifdef Py_USING_UNICODE
Guido van Rossum60456fd1997-04-09 17:36:32 +00003449static int
Tim Peterscba30e22003-02-01 06:24:36 +00003450load_unicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003451{
3452 PyObject *str = 0;
3453 int len, res = -1;
3454 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003455
Tim Peters0bc93f52003-02-02 18:29:33 +00003456 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003457 if (len < 1) return bad_readline();
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003458
Tim Peterscba30e22003-02-01 06:24:36 +00003459 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003460 goto finally;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003461
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003462 PDATA_PUSH(self->stack, str, -1);
3463 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003464
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003465 finally:
3466 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00003467}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003468#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003469
3470
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003471#ifdef Py_USING_UNICODE
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003472static int
Tim Peterscba30e22003-02-01 06:24:36 +00003473load_binunicode(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003474{
3475 PyObject *unicode;
3476 long l;
3477 char *s;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003478
Tim Peters0bc93f52003-02-02 18:29:33 +00003479 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003480
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003481 l = calc_binint(s, 4);
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003482
Tim Peters0bc93f52003-02-02 18:29:33 +00003483 if (self->read_func(self, &s, l) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003484 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003485
Tim Peterscba30e22003-02-01 06:24:36 +00003486 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003487 return -1;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003488
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003489 PDATA_PUSH(self->stack, unicode, -1);
3490 return 0;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003491}
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003492#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003493
3494
3495static int
Tim Peterscba30e22003-02-01 06:24:36 +00003496load_tuple(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003497{
3498 PyObject *tup;
3499 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003500
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003501 if ((i = marker(self)) < 0) return -1;
3502 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3503 PDATA_PUSH(self->stack, tup, -1);
3504 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003505}
3506
3507static int
Tim Peters1d63c9f2003-02-02 20:29:39 +00003508load_counted_tuple(Unpicklerobject *self, int len)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003509{
Tim Peters1d63c9f2003-02-02 20:29:39 +00003510 PyObject *tup = PyTuple_New(len);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003511
Tim Peters1d63c9f2003-02-02 20:29:39 +00003512 if (tup == NULL)
3513 return -1;
3514
3515 while (--len >= 0) {
3516 PyObject *element;
3517
3518 PDATA_POP(self->stack, element);
3519 if (element == NULL)
3520 return -1;
3521 PyTuple_SET_ITEM(tup, len, element);
3522 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003523 PDATA_PUSH(self->stack, tup, -1);
3524 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003525}
3526
3527static int
Tim Peterscba30e22003-02-01 06:24:36 +00003528load_empty_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003529{
3530 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003531
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003532 if (!( list=PyList_New(0))) return -1;
3533 PDATA_PUSH(self->stack, list, -1);
3534 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003535}
3536
3537static int
Tim Peterscba30e22003-02-01 06:24:36 +00003538load_empty_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003539{
3540 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003541
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003542 if (!( dict=PyDict_New())) return -1;
3543 PDATA_PUSH(self->stack, dict, -1);
3544 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003545}
3546
3547
3548static int
Tim Peterscba30e22003-02-01 06:24:36 +00003549load_list(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003550{
3551 PyObject *list = 0;
3552 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003553
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003554 if ((i = marker(self)) < 0) return -1;
3555 if (!( list=Pdata_popList(self->stack, i))) return -1;
3556 PDATA_PUSH(self->stack, list, -1);
3557 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003558}
3559
3560static int
Tim Peterscba30e22003-02-01 06:24:36 +00003561load_dict(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003562{
3563 PyObject *dict, *key, *value;
3564 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003565
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003566 if ((i = marker(self)) < 0) return -1;
3567 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003568
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003569 if (!( dict = PyDict_New())) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003570
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003571 for (k = i+1; k < j; k += 2) {
3572 key =self->stack->data[k-1];
3573 value=self->stack->data[k ];
3574 if (PyDict_SetItem(dict, key, value) < 0) {
3575 Py_DECREF(dict);
3576 return -1;
3577 }
3578 }
3579 Pdata_clear(self->stack, i);
3580 PDATA_PUSH(self->stack, dict, -1);
3581 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003582}
3583
3584static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00003585Instance_New(PyObject *cls, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003586{
Tim Peters1f1b2d22003-02-01 02:16:37 +00003587 PyObject *r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003588
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003589 if (PyClass_Check(cls)) {
3590 int l;
Tim Peters84e87f32001-03-17 04:50:51 +00003591
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003592 if ((l=PyObject_Size(args)) < 0) goto err;
3593 if (!( l )) {
3594 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003595
Tim Peterscba30e22003-02-01 06:24:36 +00003596 __getinitargs__ = PyObject_GetAttr(cls,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003597 __getinitargs___str);
3598 if (!__getinitargs__) {
Tim Peterscba30e22003-02-01 06:24:36 +00003599 /* We have a class with no __getinitargs__,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003600 so bypass usual construction */
3601 PyObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003602
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003603 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00003604 if (!( inst=PyInstance_NewRaw(cls, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003605 goto err;
3606 return inst;
3607 }
3608 Py_DECREF(__getinitargs__);
3609 }
Tim Peters84e87f32001-03-17 04:50:51 +00003610
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003611 if ((r=PyInstance_New(cls, args, NULL))) return r;
3612 else goto err;
3613 }
Tim Peters84e87f32001-03-17 04:50:51 +00003614
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003615 if (args==Py_None) {
3616 /* Special case, call cls.__basicnew__() */
3617 PyObject *basicnew;
Tim Peters84e87f32001-03-17 04:50:51 +00003618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003619 basicnew = PyObject_GetAttr(cls, __basicnew___str);
3620 if (!basicnew) return NULL;
3621 r=PyObject_CallObject(basicnew, NULL);
3622 Py_DECREF(basicnew);
3623 if (r) return r;
3624 }
Guido van Rossum9716aaa1997-12-08 15:15:16 +00003625
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003626 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00003627
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003628 err:
3629 {
3630 PyObject *tp, *v, *tb;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003631
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003632 PyErr_Fetch(&tp, &v, &tb);
3633 if ((r=Py_BuildValue("OOO",v,cls,args))) {
3634 Py_XDECREF(v);
3635 v=r;
3636 }
3637 PyErr_Restore(tp,v,tb);
3638 }
3639 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003640}
Tim Peters84e87f32001-03-17 04:50:51 +00003641
Guido van Rossum60456fd1997-04-09 17:36:32 +00003642
3643static int
Tim Peterscba30e22003-02-01 06:24:36 +00003644load_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003645{
3646 PyObject *class, *tup, *obj=0;
3647 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003648
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003649 if ((i = marker(self)) < 0) return -1;
3650 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3651 PDATA_POP(self->stack, class);
3652 if (class) {
3653 obj = Instance_New(class, tup);
3654 Py_DECREF(class);
3655 }
3656 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003657
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003658 if (! obj) return -1;
3659 PDATA_PUSH(self->stack, obj, -1);
3660 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003661}
3662
3663
3664static int
Tim Peterscba30e22003-02-01 06:24:36 +00003665load_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003666{
3667 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3668 int i, len;
3669 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003670
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003671 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003672
Tim Peters0bc93f52003-02-02 18:29:33 +00003673 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003674 if (len < 2) return bad_readline();
3675 module_name = PyString_FromStringAndSize(s, len - 1);
3676 if (!module_name) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003677
Tim Peters0bc93f52003-02-02 18:29:33 +00003678 if ((len = self->readline_func(self, &s)) >= 0) {
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003679 if (len < 2) return bad_readline();
3680 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003681 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003682 self->find_class);
3683 Py_DECREF(class_name);
3684 }
3685 }
3686 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003687
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003688 if (! class) return -1;
Tim Peters84e87f32001-03-17 04:50:51 +00003689
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003690 if ((tup=Pdata_popTuple(self->stack, i))) {
3691 obj = Instance_New(class, tup);
3692 Py_DECREF(tup);
3693 }
3694 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003695
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003696 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003697
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003698 PDATA_PUSH(self->stack, obj, -1);
3699 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003700}
3701
Tim Peterseab7db32003-02-13 18:24:14 +00003702static int
3703load_newobj(Unpicklerobject *self)
3704{
3705 PyObject *args = NULL;
3706 PyObject *clsraw = NULL;
3707 PyTypeObject *cls; /* clsraw cast to its true type */
3708 PyObject *obj;
3709
3710 /* Stack is ... cls argtuple, and we want to call
3711 * cls.__new__(cls, *argtuple).
3712 */
3713 PDATA_POP(self->stack, args);
3714 if (args == NULL) goto Fail;
3715 if (! PyTuple_Check(args)) {
3716 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3717 "tuple.");
3718 goto Fail;
3719 }
3720
3721 PDATA_POP(self->stack, clsraw);
3722 cls = (PyTypeObject *)clsraw;
3723 if (cls == NULL) goto Fail;
3724 if (! PyType_Check(cls)) {
3725 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3726 "isn't a type object");
3727 goto Fail;
3728 }
3729 if (cls->tp_new == NULL) {
3730 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3731 "has NULL tp_new");
3732 goto Fail;
3733 }
3734
3735 /* Call __new__. */
3736 obj = cls->tp_new(cls, args, NULL);
3737 if (obj == NULL) goto Fail;
3738
3739 Py_DECREF(args);
3740 Py_DECREF(clsraw);
3741 PDATA_PUSH(self->stack, obj, -1);
3742 return 0;
3743
3744 Fail:
3745 Py_XDECREF(args);
3746 Py_XDECREF(clsraw);
3747 return -1;
3748}
Guido van Rossum60456fd1997-04-09 17:36:32 +00003749
3750static int
Tim Peterscba30e22003-02-01 06:24:36 +00003751load_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003752{
3753 PyObject *class = 0, *module_name = 0, *class_name = 0;
3754 int len;
3755 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003756
Tim Peters0bc93f52003-02-02 18:29:33 +00003757 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003758 if (len < 2) return bad_readline();
3759 module_name = PyString_FromStringAndSize(s, len - 1);
3760 if (!module_name) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003761
Tim Peters0bc93f52003-02-02 18:29:33 +00003762 if ((len = self->readline_func(self, &s)) >= 0) {
Tim Peters4e52ca82002-12-07 02:43:28 +00003763 if (len < 2) {
3764 Py_DECREF(module_name);
3765 return bad_readline();
3766 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003767 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Tim Peterscba30e22003-02-01 06:24:36 +00003768 class = find_class(module_name, class_name,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003769 self->find_class);
3770 Py_DECREF(class_name);
3771 }
3772 }
3773 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003775 if (! class) return -1;
3776 PDATA_PUSH(self->stack, class, -1);
3777 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003778}
3779
3780
3781static int
Tim Peterscba30e22003-02-01 06:24:36 +00003782load_persid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003783{
3784 PyObject *pid = 0;
3785 int len;
3786 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003787
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003788 if (self->pers_func) {
Tim Peters0bc93f52003-02-02 18:29:33 +00003789 if ((len = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003790 if (len < 2) return bad_readline();
3791
3792 pid = PyString_FromStringAndSize(s, len - 1);
3793 if (!pid) return -1;
3794
3795 if (PyList_Check(self->pers_func)) {
3796 if (PyList_Append(self->pers_func, pid) < 0) {
3797 Py_DECREF(pid);
3798 return -1;
3799 }
3800 }
3801 else {
3802 ARG_TUP(self, pid);
3803 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00003804 pid = PyObject_Call(self->pers_func, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003805 NULL);
3806 FREE_ARG_TUP(self);
3807 }
3808 }
3809
3810 if (! pid) return -1;
3811
3812 PDATA_PUSH(self->stack, pid, -1);
3813 return 0;
3814 }
3815 else {
3816 PyErr_SetString(UnpicklingError,
3817 "A load persistent id instruction was encountered,\n"
3818 "but no persistent_load function was specified.");
3819 return -1;
3820 }
3821}
3822
3823static int
Tim Peterscba30e22003-02-01 06:24:36 +00003824load_binpersid(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003825{
3826 PyObject *pid = 0;
3827
3828 if (self->pers_func) {
3829 PDATA_POP(self->stack, pid);
3830 if (! pid) return -1;
3831
3832 if (PyList_Check(self->pers_func)) {
3833 if (PyList_Append(self->pers_func, pid) < 0) {
3834 Py_DECREF(pid);
3835 return -1;
3836 }
3837 }
3838 else {
3839 ARG_TUP(self, pid);
3840 if (self->arg) {
3841 pid = PyObject_Call(self->pers_func, self->arg,
3842 NULL);
3843 FREE_ARG_TUP(self);
3844 }
3845 if (! pid) return -1;
3846 }
3847
3848 PDATA_PUSH(self->stack, pid, -1);
3849 return 0;
3850 }
3851 else {
3852 PyErr_SetString(UnpicklingError,
3853 "A load persistent id instruction was encountered,\n"
3854 "but no persistent_load function was specified.");
3855 return -1;
3856 }
3857}
3858
3859
3860static int
Tim Peterscba30e22003-02-01 06:24:36 +00003861load_pop(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003862{
3863 int len;
3864
3865 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3866
3867 /* Note that we split the (pickle.py) stack into two stacks,
3868 an object stack and a mark stack. We have to be clever and
3869 pop the right one. We do this by looking at the top of the
3870 mark stack.
3871 */
3872
3873 if ((self->num_marks > 0) &&
3874 (self->marks[self->num_marks - 1] == len))
3875 self->num_marks--;
3876 else {
3877 len--;
3878 Py_DECREF(self->stack->data[len]);
3879 self->stack->length=len;
3880 }
3881
3882 return 0;
3883}
3884
3885
3886static int
Tim Peterscba30e22003-02-01 06:24:36 +00003887load_pop_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003888{
3889 int i;
3890
3891 if ((i = marker(self)) < 0)
3892 return -1;
3893
3894 Pdata_clear(self->stack, i);
3895
3896 return 0;
3897}
3898
3899
3900static int
Tim Peterscba30e22003-02-01 06:24:36 +00003901load_dup(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003902{
3903 PyObject *last;
3904 int len;
3905
3906 if ((len = self->stack->length) <= 0) return stackUnderflow();
3907 last=self->stack->data[len-1];
3908 Py_INCREF(last);
3909 PDATA_PUSH(self->stack, last, -1);
3910 return 0;
3911}
3912
3913
3914static int
Tim Peterscba30e22003-02-01 06:24:36 +00003915load_get(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003916{
3917 PyObject *py_str = 0, *value = 0;
3918 int len;
3919 char *s;
3920 int rc;
3921
Tim Peters0bc93f52003-02-02 18:29:33 +00003922 if ((len = self->readline_func(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003923 if (len < 2) return bad_readline();
Tim Peters84e87f32001-03-17 04:50:51 +00003924
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003925 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003926
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003927 value = PyDict_GetItem(self->memo, py_str);
3928 if (! value) {
3929 PyErr_SetObject(BadPickleGet, py_str);
3930 rc = -1;
3931 } else {
3932 PDATA_APPEND(self->stack, value, -1);
3933 rc = 0;
3934 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003935
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003936 Py_DECREF(py_str);
3937 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003938}
3939
3940
3941static int
Tim Peterscba30e22003-02-01 06:24:36 +00003942load_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003943{
3944 PyObject *py_key = 0, *value = 0;
3945 unsigned char key;
3946 char *s;
3947 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003948
Tim Peters0bc93f52003-02-02 18:29:33 +00003949 if (self->read_func(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003950
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003951 key = (unsigned char)s[0];
3952 if (!( py_key = PyInt_FromLong((long)key))) return -1;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003953
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003954 value = PyDict_GetItem(self->memo, py_key);
3955 if (! value) {
3956 PyErr_SetObject(BadPickleGet, py_key);
3957 rc = -1;
3958 } else {
3959 PDATA_APPEND(self->stack, value, -1);
3960 rc = 0;
3961 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003962
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003963 Py_DECREF(py_key);
3964 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003965}
3966
3967
3968static int
Tim Peterscba30e22003-02-01 06:24:36 +00003969load_long_binget(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003970{
3971 PyObject *py_key = 0, *value = 0;
3972 unsigned char c;
3973 char *s;
3974 long key;
3975 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003976
Tim Peters0bc93f52003-02-02 18:29:33 +00003977 if (self->read_func(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003978
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003979 c = (unsigned char)s[0];
3980 key = (long)c;
3981 c = (unsigned char)s[1];
3982 key |= (long)c << 8;
3983 c = (unsigned char)s[2];
3984 key |= (long)c << 16;
3985 c = (unsigned char)s[3];
3986 key |= (long)c << 24;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003987
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00003988 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3989
3990 value = PyDict_GetItem(self->memo, py_key);
3991 if (! value) {
3992 PyErr_SetObject(BadPickleGet, py_key);
3993 rc = -1;
3994 } else {
3995 PDATA_APPEND(self->stack, value, -1);
3996 rc = 0;
3997 }
3998
3999 Py_DECREF(py_key);
4000 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004001}
4002
Tim Peters2d629652003-02-04 05:06:17 +00004003/* Push an object from the extension registry (EXT[124]). nbytes is
4004 * the number of bytes following the opcode, holding the index (code) value.
4005 */
4006static int
4007load_extension(Unpicklerobject *self, int nbytes)
4008{
4009 char *codebytes; /* the nbytes bytes after the opcode */
4010 long code; /* calc_binint returns long */
4011 PyObject *py_code; /* code as a Python int */
4012 PyObject *obj; /* the object to push */
4013 PyObject *pair; /* (module_name, class_name) */
4014 PyObject *module_name, *class_name;
4015
4016 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4017 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4018 code = calc_binint(codebytes, nbytes);
4019 if (code <= 0) { /* note that 0 is forbidden */
4020 /* Corrupt or hostile pickle. */
4021 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4022 return -1;
4023 }
4024
4025 /* Look for the code in the cache. */
4026 py_code = PyInt_FromLong(code);
4027 if (py_code == NULL) return -1;
4028 obj = PyDict_GetItem(extension_cache, py_code);
4029 if (obj != NULL) {
4030 /* Bingo. */
4031 Py_DECREF(py_code);
4032 PDATA_APPEND(self->stack, obj, -1);
4033 return 0;
4034 }
4035
4036 /* Look up the (module_name, class_name) pair. */
4037 pair = PyDict_GetItem(inverted_registry, py_code);
4038 if (pair == NULL) {
4039 Py_DECREF(py_code);
4040 PyErr_Format(PyExc_ValueError, "unregistered extension "
4041 "code %ld", code);
4042 return -1;
4043 }
4044 /* Since the extension registry is manipulable via Python code,
Tim Petersfa05ce32003-02-04 05:20:32 +00004045 * confirm that pair is really a 2-tuple of strings.
Tim Peters2d629652003-02-04 05:06:17 +00004046 */
4047 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4048 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4049 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4050 Py_DECREF(py_code);
4051 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4052 "isn't a 2-tuple of strings", code);
4053 return -1;
4054 }
4055 /* Load the object. */
4056 obj = find_class(module_name, class_name, self->find_class);
4057 if (obj == NULL) {
4058 Py_DECREF(py_code);
4059 return -1;
4060 }
4061 /* Cache code -> obj. */
4062 code = PyDict_SetItem(extension_cache, py_code, obj);
4063 Py_DECREF(py_code);
4064 if (code < 0) {
4065 Py_DECREF(obj);
4066 return -1;
4067 }
4068 PDATA_PUSH(self->stack, obj, -1);
4069 return 0;
4070}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004071
4072static int
Tim Peterscba30e22003-02-01 06:24:36 +00004073load_put(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004074{
4075 PyObject *py_str = 0, *value = 0;
4076 int len, l;
4077 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004078
Tim Peters0bc93f52003-02-02 18:29:33 +00004079 if ((l = self->readline_func(self, &s)) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004080 if (l < 2) return bad_readline();
4081 if (!( len=self->stack->length )) return stackUnderflow();
4082 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4083 value=self->stack->data[len-1];
4084 l=PyDict_SetItem(self->memo, py_str, value);
4085 Py_DECREF(py_str);
4086 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004087}
4088
4089
4090static int
Tim Peterscba30e22003-02-01 06:24:36 +00004091load_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004092{
4093 PyObject *py_key = 0, *value = 0;
4094 unsigned char key;
4095 char *s;
4096 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004097
Tim Peters0bc93f52003-02-02 18:29:33 +00004098 if (self->read_func(self, &s, 1) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004099 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004100
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004101 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00004102
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004103 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4104 value=self->stack->data[len-1];
4105 len=PyDict_SetItem(self->memo, py_key, value);
4106 Py_DECREF(py_key);
4107 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004108}
4109
4110
4111static int
Tim Peterscba30e22003-02-01 06:24:36 +00004112load_long_binput(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004113{
4114 PyObject *py_key = 0, *value = 0;
4115 long key;
4116 unsigned char c;
4117 char *s;
4118 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004119
Tim Peters0bc93f52003-02-02 18:29:33 +00004120 if (self->read_func(self, &s, 4) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004121 if (!( len=self->stack->length )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004122
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004123 c = (unsigned char)s[0];
4124 key = (long)c;
4125 c = (unsigned char)s[1];
4126 key |= (long)c << 8;
4127 c = (unsigned char)s[2];
4128 key |= (long)c << 16;
4129 c = (unsigned char)s[3];
4130 key |= (long)c << 24;
Tim Peters84e87f32001-03-17 04:50:51 +00004131
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004132 if (!( py_key = PyInt_FromLong(key))) return -1;
4133 value=self->stack->data[len-1];
4134 len=PyDict_SetItem(self->memo, py_key, value);
4135 Py_DECREF(py_key);
4136 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004137}
4138
4139
4140static int
Tim Peterscba30e22003-02-01 06:24:36 +00004141do_append(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004142{
4143 PyObject *value = 0, *list = 0, *append_method = 0;
4144 int len, i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004145
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004146 len=self->stack->length;
4147 if (!( len >= x && x > 0 )) return stackUnderflow();
4148 /* nothing to do */
Tim Peterscba30e22003-02-01 06:24:36 +00004149 if (len==x) return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004151 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004152
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004153 if (PyList_Check(list)) {
4154 PyObject *slice;
4155 int list_len;
Tim Peters84e87f32001-03-17 04:50:51 +00004156
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004157 slice=Pdata_popList(self->stack, x);
4158 list_len = PyList_GET_SIZE(list);
4159 i=PyList_SetSlice(list, list_len, list_len, slice);
4160 Py_DECREF(slice);
4161 return i;
4162 }
4163 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004164
Tim Peterscba30e22003-02-01 06:24:36 +00004165 if (!( append_method = PyObject_GetAttr(list, append_str)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004166 return -1;
4167
4168 for (i = x; i < len; i++) {
4169 PyObject *junk;
4170
4171 value=self->stack->data[i];
4172 junk=0;
4173 ARG_TUP(self, value);
4174 if (self->arg) {
Tim Peterscba30e22003-02-01 06:24:36 +00004175 junk = PyObject_Call(append_method, self->arg,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004176 NULL);
4177 FREE_ARG_TUP(self);
4178 }
4179 if (! junk) {
4180 Pdata_clear(self->stack, i+1);
4181 self->stack->length=x;
4182 Py_DECREF(append_method);
4183 return -1;
4184 }
4185 Py_DECREF(junk);
4186 }
4187 self->stack->length=x;
4188 Py_DECREF(append_method);
4189 }
4190
4191 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004192}
4193
4194
4195static int
Tim Peterscba30e22003-02-01 06:24:36 +00004196load_append(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004197{
4198 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004199}
4200
4201
4202static int
Tim Peterscba30e22003-02-01 06:24:36 +00004203load_appends(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004204{
4205 return do_append(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004206}
4207
4208
4209static int
Tim Peterscba30e22003-02-01 06:24:36 +00004210do_setitems(Unpicklerobject *self, int x)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004211{
4212 PyObject *value = 0, *key = 0, *dict = 0;
4213 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004214
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004215 if (!( (len=self->stack->length) >= x
4216 && x > 0 )) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004217
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004218 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00004219
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004220 for (i = x+1; i < len; i += 2) {
4221 key =self->stack->data[i-1];
4222 value=self->stack->data[i ];
4223 if (PyObject_SetItem(dict, key, value) < 0) {
4224 r=-1;
4225 break;
4226 }
4227 }
4228
4229 Pdata_clear(self->stack, x);
4230
4231 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004232}
4233
4234
Tim Peters84e87f32001-03-17 04:50:51 +00004235static int
Tim Peterscba30e22003-02-01 06:24:36 +00004236load_setitem(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004237{
4238 return do_setitems(self, self->stack->length - 2);
4239}
Guido van Rossum60456fd1997-04-09 17:36:32 +00004240
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004241static int
Tim Peterscba30e22003-02-01 06:24:36 +00004242load_setitems(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004243{
4244 return do_setitems(self, marker(self));
Guido van Rossum60456fd1997-04-09 17:36:32 +00004245}
4246
Tim Peters84e87f32001-03-17 04:50:51 +00004247
Guido van Rossum60456fd1997-04-09 17:36:32 +00004248static int
Tim Peterscba30e22003-02-01 06:24:36 +00004249load_build(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004250{
4251 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
4252 *junk = 0, *__setstate__ = 0;
4253 int i, r = 0;
4254
4255 if (self->stack->length < 2) return stackUnderflow();
4256 PDATA_POP(self->stack, value);
4257 if (! value) return -1;
4258 inst=self->stack->data[self->stack->length-1];
4259
4260 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
4261 ARG_TUP(self, value);
4262 if (self->arg) {
4263 junk = PyObject_Call(__setstate__, self->arg, NULL);
4264 FREE_ARG_TUP(self);
4265 }
4266 Py_DECREF(__setstate__);
4267 if (! junk) return -1;
4268 Py_DECREF(junk);
4269 return 0;
4270 }
4271
4272 PyErr_Clear();
4273 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
4274 i = 0;
4275 while (PyDict_Next(value, &i, &d_key, &d_value)) {
4276 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
4277 r=-1;
4278 break;
4279 }
4280 }
4281 Py_DECREF(instdict);
4282 }
4283 else r=-1;
4284
4285 Py_XDECREF(value);
4286
4287 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004288}
4289
4290
4291static int
Tim Peterscba30e22003-02-01 06:24:36 +00004292load_mark(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004293{
4294 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004295
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004296 /* Note that we split the (pickle.py) stack into two stacks, an
4297 object stack and a mark stack. Here we push a mark onto the
4298 mark stack.
4299 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00004300
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004301 if ((self->num_marks + 1) >= self->marks_size) {
4302 s=self->marks_size+20;
4303 if (s <= self->num_marks) s=self->num_marks + 1;
4304 if (self->marks == NULL)
4305 self->marks=(int *)malloc(s * sizeof(int));
4306 else
Tim Peterscba30e22003-02-01 06:24:36 +00004307 self->marks=(int *)realloc(self->marks,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004308 s * sizeof(int));
4309 if (! self->marks) {
4310 PyErr_NoMemory();
4311 return -1;
4312 }
4313 self->marks_size = s;
4314 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004315
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004316 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004317
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004318 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004319}
4320
Guido van Rossum60456fd1997-04-09 17:36:32 +00004321static int
Tim Peterscba30e22003-02-01 06:24:36 +00004322load_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004323{
4324 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004325
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004326 PDATA_POP(self->stack, arg_tup);
4327 if (! arg_tup) return -1;
4328 PDATA_POP(self->stack, callable);
4329 if (callable) {
4330 ob = Instance_New(callable, arg_tup);
4331 Py_DECREF(callable);
4332 }
4333 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004334
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004335 if (! ob) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004336
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004337 PDATA_PUSH(self->stack, ob, -1);
4338 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004339}
Tim Peters84e87f32001-03-17 04:50:51 +00004340
Tim Peters4190fb82003-02-02 16:09:05 +00004341/* Just raises an error if we don't know the protocol specified. PROTO
4342 * is the first opcode for protocols >= 2.
4343 */
4344static int
4345load_proto(Unpicklerobject *self)
4346{
4347 int i;
4348 char *protobyte;
4349
4350 i = self->read_func(self, &protobyte, 1);
4351 if (i < 0)
4352 return -1;
4353
4354 i = calc_binint(protobyte, 1);
4355 /* No point checking for < 0, since calc_binint returns an unsigned
4356 * int when chewing on 1 byte.
4357 */
4358 assert(i >= 0);
Tim Peters8587b3c2003-02-13 15:44:41 +00004359 if (i <= HIGHEST_PROTOCOL)
Tim Peters4190fb82003-02-02 16:09:05 +00004360 return 0;
4361
4362 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4363 return -1;
4364}
4365
Guido van Rossum60456fd1997-04-09 17:36:32 +00004366static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004367load(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004368{
4369 PyObject *err = 0, *val = 0;
4370 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004371
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004372 self->num_marks = 0;
4373 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004374
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004375 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004376 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004377 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004378
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004379 switch (s[0]) {
4380 case NONE:
4381 if (load_none(self) < 0)
4382 break;
4383 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004384
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004385 case BININT:
4386 if (load_binint(self) < 0)
4387 break;
4388 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004389
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004390 case BININT1:
4391 if (load_binint1(self) < 0)
4392 break;
4393 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004394
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004395 case BININT2:
4396 if (load_binint2(self) < 0)
4397 break;
4398 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004399
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004400 case INT:
4401 if (load_int(self) < 0)
4402 break;
4403 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004404
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004405 case LONG:
4406 if (load_long(self) < 0)
4407 break;
4408 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004409
Tim Petersee1a53c2003-02-02 02:57:53 +00004410 case LONG1:
4411 if (load_counted_long(self, 1) < 0)
4412 break;
4413 continue;
4414
4415 case LONG4:
4416 if (load_counted_long(self, 4) < 0)
4417 break;
4418 continue;
4419
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004420 case FLOAT:
4421 if (load_float(self) < 0)
4422 break;
4423 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004424
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004425 case BINFLOAT:
4426 if (load_binfloat(self) < 0)
4427 break;
4428 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004429
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004430 case BINSTRING:
4431 if (load_binstring(self) < 0)
4432 break;
4433 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004434
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004435 case SHORT_BINSTRING:
4436 if (load_short_binstring(self) < 0)
4437 break;
4438 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004439
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004440 case STRING:
4441 if (load_string(self) < 0)
4442 break;
4443 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004444
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004445#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004446 case UNICODE:
4447 if (load_unicode(self) < 0)
4448 break;
4449 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004450
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004451 case BINUNICODE:
4452 if (load_binunicode(self) < 0)
4453 break;
4454 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004455#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004456
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004457 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004458 if (load_counted_tuple(self, 0) < 0)
4459 break;
4460 continue;
4461
4462 case TUPLE1:
4463 if (load_counted_tuple(self, 1) < 0)
4464 break;
4465 continue;
4466
4467 case TUPLE2:
4468 if (load_counted_tuple(self, 2) < 0)
4469 break;
4470 continue;
4471
4472 case TUPLE3:
4473 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004474 break;
4475 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004476
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004477 case TUPLE:
4478 if (load_tuple(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 EMPTY_LIST:
4483 if (load_empty_list(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 LIST:
4488 if (load_list(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 EMPTY_DICT:
4493 if (load_empty_dict(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 DICT:
4498 if (load_dict(self) < 0)
4499 break;
4500 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004501
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004502 case OBJ:
4503 if (load_obj(self) < 0)
4504 break;
4505 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004506
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004507 case INST:
4508 if (load_inst(self) < 0)
4509 break;
4510 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004511
Tim Peterseab7db32003-02-13 18:24:14 +00004512 case NEWOBJ:
4513 if (load_newobj(self) < 0)
4514 break;
4515 continue;
4516
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004517 case GLOBAL:
4518 if (load_global(self) < 0)
4519 break;
4520 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004521
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004522 case APPEND:
4523 if (load_append(self) < 0)
4524 break;
4525 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004526
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004527 case APPENDS:
4528 if (load_appends(self) < 0)
4529 break;
4530 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004531
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004532 case BUILD:
4533 if (load_build(self) < 0)
4534 break;
4535 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004536
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004537 case DUP:
4538 if (load_dup(self) < 0)
4539 break;
4540 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004541
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004542 case BINGET:
4543 if (load_binget(self) < 0)
4544 break;
4545 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004546
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004547 case LONG_BINGET:
4548 if (load_long_binget(self) < 0)
4549 break;
4550 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004551
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004552 case GET:
4553 if (load_get(self) < 0)
4554 break;
4555 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004556
Tim Peters2d629652003-02-04 05:06:17 +00004557 case EXT1:
4558 if (load_extension(self, 1) < 0)
4559 break;
4560 continue;
4561
4562 case EXT2:
4563 if (load_extension(self, 2) < 0)
4564 break;
4565 continue;
4566
4567 case EXT4:
4568 if (load_extension(self, 4) < 0)
4569 break;
4570 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004571 case MARK:
4572 if (load_mark(self) < 0)
4573 break;
4574 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004575
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004576 case BINPUT:
4577 if (load_binput(self) < 0)
4578 break;
4579 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004580
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004581 case LONG_BINPUT:
4582 if (load_long_binput(self) < 0)
4583 break;
4584 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004585
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004586 case PUT:
4587 if (load_put(self) < 0)
4588 break;
4589 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004590
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004591 case POP:
4592 if (load_pop(self) < 0)
4593 break;
4594 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004595
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004596 case POP_MARK:
4597 if (load_pop_mark(self) < 0)
4598 break;
4599 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004600
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004601 case SETITEM:
4602 if (load_setitem(self) < 0)
4603 break;
4604 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004605
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004606 case SETITEMS:
4607 if (load_setitems(self) < 0)
4608 break;
4609 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004610
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004611 case STOP:
4612 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004613
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004614 case PERSID:
4615 if (load_persid(self) < 0)
4616 break;
4617 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004618
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004619 case BINPERSID:
4620 if (load_binpersid(self) < 0)
4621 break;
4622 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004623
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004624 case REDUCE:
4625 if (load_reduce(self) < 0)
4626 break;
4627 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004628
Tim Peters4190fb82003-02-02 16:09:05 +00004629 case PROTO:
4630 if (load_proto(self) < 0)
4631 break;
4632 continue;
4633
Tim Peters3c67d792003-02-02 17:59:11 +00004634 case NEWTRUE:
4635 if (load_bool(self, Py_True) < 0)
4636 break;
4637 continue;
4638
4639 case NEWFALSE:
4640 if (load_bool(self, Py_False) < 0)
4641 break;
4642 continue;
4643
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004644 case '\0':
4645 /* end of file */
4646 PyErr_SetNone(PyExc_EOFError);
4647 break;
Tim Peterscba30e22003-02-01 06:24:36 +00004648
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004649 default:
Tim Peterscba30e22003-02-01 06:24:36 +00004650 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004651 "invalid load key, '%s'.",
4652 "c", s[0]);
4653 return NULL;
4654 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004655
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004656 break;
4657 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004658
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004659 if ((err = PyErr_Occurred())) {
4660 if (err == PyExc_EOFError) {
4661 PyErr_SetNone(PyExc_EOFError);
4662 }
4663 return NULL;
4664 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004665
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004666 PDATA_POP(self->stack, val);
4667 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004668}
Tim Peters84e87f32001-03-17 04:50:51 +00004669
Guido van Rossum60456fd1997-04-09 17:36:32 +00004670
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004671/* No-load functions to support noload, which is used to
4672 find persistent references. */
4673
4674static int
Tim Peterscba30e22003-02-01 06:24:36 +00004675noload_obj(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004676{
4677 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004678
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004679 if ((i = marker(self)) < 0) return -1;
4680 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004681}
4682
4683
4684static int
Tim Peterscba30e22003-02-01 06:24:36 +00004685noload_inst(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004686{
4687 int i;
4688 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004689
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004690 if ((i = marker(self)) < 0) return -1;
4691 Pdata_clear(self->stack, i);
Tim Peters0bc93f52003-02-02 18:29:33 +00004692 if (self->readline_func(self, &s) < 0) return -1;
4693 if (self->readline_func(self, &s) < 0) return -1;
Tim Peterseab7db32003-02-13 18:24:14 +00004694 PDATA_APPEND(self->stack, Py_None, -1);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004695 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004696}
4697
4698static int
Tim Peterseab7db32003-02-13 18:24:14 +00004699noload_newobj(Unpicklerobject *self)
4700{
4701 PyObject *obj;
4702
4703 PDATA_POP(self->stack, obj); /* pop argtuple */
4704 if (obj == NULL) return -1;
4705 Py_DECREF(obj);
4706
4707 PDATA_POP(self->stack, obj); /* pop cls */
4708 if (obj == NULL) return -1;
4709 Py_DECREF(obj);
4710
4711 PDATA_APPEND(self->stack, Py_None, -1);
4712 return 0;
4713}
4714
4715static int
Tim Peterscba30e22003-02-01 06:24:36 +00004716noload_global(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004717{
4718 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004719
Tim Peters0bc93f52003-02-02 18:29:33 +00004720 if (self->readline_func(self, &s) < 0) return -1;
4721 if (self->readline_func(self, &s) < 0) return -1;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004722 PDATA_APPEND(self->stack, Py_None,-1);
4723 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004724}
4725
4726static int
Tim Peterscba30e22003-02-01 06:24:36 +00004727noload_reduce(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004728{
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004729
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004730 if (self->stack->length < 2) return stackUnderflow();
4731 Pdata_clear(self->stack, self->stack->length-2);
4732 PDATA_APPEND(self->stack, Py_None,-1);
4733 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004734}
4735
4736static int
4737noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004738
Guido van Rossum053b8df1998-11-25 16:18:00 +00004739 if (self->stack->length < 1) return stackUnderflow();
4740 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00004741 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004742}
4743
Tim Peters2d629652003-02-04 05:06:17 +00004744static int
4745noload_extension(Unpicklerobject *self, int nbytes)
4746{
4747 char *codebytes;
4748
4749 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4750 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4751 PDATA_APPEND(self->stack, Py_None, -1);
4752 return 0;
4753}
4754
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004755
4756static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00004757noload(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004758{
4759 PyObject *err = 0, *val = 0;
4760 char *s;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004761
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004762 self->num_marks = 0;
4763 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004764
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004765 while (1) {
Tim Peters0bc93f52003-02-02 18:29:33 +00004766 if (self->read_func(self, &s, 1) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004767 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004768
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004769 switch (s[0]) {
4770 case NONE:
4771 if (load_none(self) < 0)
4772 break;
4773 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004774
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004775 case BININT:
4776 if (load_binint(self) < 0)
4777 break;
4778 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004779
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004780 case BININT1:
4781 if (load_binint1(self) < 0)
4782 break;
4783 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004784
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004785 case BININT2:
4786 if (load_binint2(self) < 0)
4787 break;
4788 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004789
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004790 case INT:
4791 if (load_int(self) < 0)
4792 break;
4793 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004794
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004795 case LONG:
4796 if (load_long(self) < 0)
4797 break;
4798 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004799
Tim Peters4190fb82003-02-02 16:09:05 +00004800 case LONG1:
4801 if (load_counted_long(self, 1) < 0)
4802 break;
4803 continue;
4804
4805 case LONG4:
4806 if (load_counted_long(self, 4) < 0)
4807 break;
4808 continue;
4809
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004810 case FLOAT:
4811 if (load_float(self) < 0)
4812 break;
4813 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004814
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004815 case BINFLOAT:
4816 if (load_binfloat(self) < 0)
4817 break;
4818 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004819
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004820 case BINSTRING:
4821 if (load_binstring(self) < 0)
4822 break;
4823 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004824
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004825 case SHORT_BINSTRING:
4826 if (load_short_binstring(self) < 0)
4827 break;
4828 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004829
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004830 case STRING:
4831 if (load_string(self) < 0)
4832 break;
4833 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004834
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004835#ifdef Py_USING_UNICODE
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004836 case UNICODE:
4837 if (load_unicode(self) < 0)
4838 break;
4839 continue;
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004840
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004841 case BINUNICODE:
4842 if (load_binunicode(self) < 0)
4843 break;
4844 continue;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004845#endif
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00004846
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004847 case EMPTY_TUPLE:
Tim Peters1d63c9f2003-02-02 20:29:39 +00004848 if (load_counted_tuple(self, 0) < 0)
4849 break;
4850 continue;
4851
4852 case TUPLE1:
4853 if (load_counted_tuple(self, 1) < 0)
4854 break;
4855 continue;
4856
4857 case TUPLE2:
4858 if (load_counted_tuple(self, 2) < 0)
4859 break;
4860 continue;
4861
4862 case TUPLE3:
4863 if (load_counted_tuple(self, 3) < 0)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004864 break;
4865 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004866
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004867 case TUPLE:
4868 if (load_tuple(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 EMPTY_LIST:
4873 if (load_empty_list(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 LIST:
4878 if (load_list(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 EMPTY_DICT:
4883 if (load_empty_dict(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 DICT:
4888 if (load_dict(self) < 0)
4889 break;
4890 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004891
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004892 case OBJ:
4893 if (noload_obj(self) < 0)
4894 break;
4895 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004896
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004897 case INST:
4898 if (noload_inst(self) < 0)
4899 break;
4900 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004901
Tim Peterseab7db32003-02-13 18:24:14 +00004902 case NEWOBJ:
4903 if (noload_newobj(self) < 0)
4904 break;
4905 continue;
4906
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004907 case GLOBAL:
4908 if (noload_global(self) < 0)
4909 break;
4910 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004911
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004912 case APPEND:
4913 if (load_append(self) < 0)
4914 break;
4915 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004916
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004917 case APPENDS:
4918 if (load_appends(self) < 0)
4919 break;
4920 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004921
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004922 case BUILD:
4923 if (noload_build(self) < 0)
4924 break;
4925 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004926
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004927 case DUP:
4928 if (load_dup(self) < 0)
4929 break;
4930 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004931
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004932 case BINGET:
4933 if (load_binget(self) < 0)
4934 break;
4935 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004936
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004937 case LONG_BINGET:
4938 if (load_long_binget(self) < 0)
4939 break;
4940 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004941
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004942 case GET:
4943 if (load_get(self) < 0)
4944 break;
4945 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004946
Tim Peters2d629652003-02-04 05:06:17 +00004947 case EXT1:
4948 if (noload_extension(self, 1) < 0)
4949 break;
4950 continue;
4951
4952 case EXT2:
4953 if (noload_extension(self, 2) < 0)
4954 break;
4955 continue;
4956
4957 case EXT4:
4958 if (noload_extension(self, 4) < 0)
4959 break;
4960 continue;
4961
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004962 case MARK:
4963 if (load_mark(self) < 0)
4964 break;
4965 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004966
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004967 case BINPUT:
4968 if (load_binput(self) < 0)
4969 break;
4970 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004971
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004972 case LONG_BINPUT:
4973 if (load_long_binput(self) < 0)
4974 break;
4975 continue;
Tim Peters84e87f32001-03-17 04:50:51 +00004976
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004977 case PUT:
4978 if (load_put(self) < 0)
4979 break;
4980 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004981
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004982 case POP:
4983 if (load_pop(self) < 0)
4984 break;
4985 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004986
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004987 case POP_MARK:
4988 if (load_pop_mark(self) < 0)
4989 break;
4990 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004991
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004992 case SETITEM:
4993 if (load_setitem(self) < 0)
4994 break;
4995 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004996
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00004997 case SETITEMS:
4998 if (load_setitems(self) < 0)
4999 break;
5000 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005001
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005002 case STOP:
5003 break;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005004
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005005 case PERSID:
5006 if (load_persid(self) < 0)
5007 break;
5008 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005009
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005010 case BINPERSID:
5011 if (load_binpersid(self) < 0)
5012 break;
5013 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005014
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005015 case REDUCE:
5016 if (noload_reduce(self) < 0)
5017 break;
5018 continue;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005019
Tim Peters4190fb82003-02-02 16:09:05 +00005020 case PROTO:
5021 if (load_proto(self) < 0)
5022 break;
5023 continue;
5024
Tim Peters3c67d792003-02-02 17:59:11 +00005025 case NEWTRUE:
5026 if (load_bool(self, Py_True) < 0)
5027 break;
5028 continue;
5029
5030 case NEWFALSE:
5031 if (load_bool(self, Py_False) < 0)
5032 break;
5033 continue;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005034 default:
Tim Peterscba30e22003-02-01 06:24:36 +00005035 cPickle_ErrFormat(UnpicklingError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005036 "invalid load key, '%s'.",
5037 "c", s[0]);
5038 return NULL;
5039 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005040
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005041 break;
5042 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005043
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005044 if ((err = PyErr_Occurred())) {
5045 if (err == PyExc_EOFError) {
5046 PyErr_SetNone(PyExc_EOFError);
5047 }
5048 return NULL;
5049 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005050
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005051 PDATA_POP(self->stack, val);
5052 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005053}
Tim Peters84e87f32001-03-17 04:50:51 +00005054
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005055
Guido van Rossum60456fd1997-04-09 17:36:32 +00005056static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005057Unpickler_load(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005058{
Tim Peterscba30e22003-02-01 06:24:36 +00005059 if (!( PyArg_ParseTuple(args, ":load")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005060 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005061
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005062 return load(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005063}
5064
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005065static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005066Unpickler_noload(Unpicklerobject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005067{
Tim Peterscba30e22003-02-01 06:24:36 +00005068 if (!( PyArg_ParseTuple(args, ":noload")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005069 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005070
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005071 return noload(self);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005072}
5073
Guido van Rossum60456fd1997-04-09 17:36:32 +00005074
5075static struct PyMethodDef Unpickler_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005076 {"load", (PyCFunction)Unpickler_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005077 PyDoc_STR("load() -- Load a pickle")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005078 },
Neal Norwitzb0493252002-03-31 14:44:22 +00005079 {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005080 PyDoc_STR(
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005081 "noload() -- not load a pickle, but go through most of the motions\n"
5082 "\n"
5083 "This function can be used to read past a pickle without instantiating\n"
5084 "any objects or importing any modules. It can also be used to find all\n"
5085 "persistent references without instantiating any objects or importing\n"
Neal Norwitz200788c2002-08-13 22:20:41 +00005086 "any modules.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005087 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00005088 {NULL, NULL} /* sentinel */
5089};
5090
5091
5092static Unpicklerobject *
Tim Peterscba30e22003-02-01 06:24:36 +00005093newUnpicklerobject(PyObject *f)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005094{
5095 Unpicklerobject *self;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005096
Tim Peterscba30e22003-02-01 06:24:36 +00005097 if (!( self = PyObject_New(Unpicklerobject, &Unpicklertype)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005098 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005099
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005100 self->file = NULL;
5101 self->arg = NULL;
5102 self->stack = (Pdata*)Pdata_New();
5103 self->pers_func = NULL;
5104 self->last_string = NULL;
5105 self->marks = NULL;
5106 self->num_marks = 0;
5107 self->marks_size = 0;
5108 self->buf_size = 0;
5109 self->read = NULL;
5110 self->readline = NULL;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005111 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005112
Tim Peterscba30e22003-02-01 06:24:36 +00005113 if (!( self->memo = PyDict_New()))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005114 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005115
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005116 Py_INCREF(f);
5117 self->file = f;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005118
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005119 /* Set read, readline based on type of f */
5120 if (PyFile_Check(f)) {
5121 self->fp = PyFile_AsFile(f);
5122 if (self->fp == NULL) {
Tim Peterscba30e22003-02-01 06:24:36 +00005123 PyErr_SetString(PyExc_ValueError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005124 "I/O operation on closed file");
5125 goto err;
5126 }
5127 self->read_func = read_file;
5128 self->readline_func = readline_file;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00005129 }
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005130 else if (PycStringIO_InputCheck(f)) {
5131 self->fp = NULL;
5132 self->read_func = read_cStringIO;
5133 self->readline_func = readline_cStringIO;
5134 }
5135 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00005136
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005137 self->fp = NULL;
5138 self->read_func = read_other;
5139 self->readline_func = readline_other;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005140
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005141 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5142 (self->read = PyObject_GetAttr(f, read_str)))) {
5143 PyErr_Clear();
Tim Peterscba30e22003-02-01 06:24:36 +00005144 PyErr_SetString( PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005145 "argument must have 'read' and "
5146 "'readline' attributes" );
5147 goto err;
5148 }
5149 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00005150
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005151 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005152
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005153 err:
5154 Py_DECREF((PyObject *)self);
5155 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005156}
5157
5158
5159static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005160get_Unpickler(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005161{
5162 PyObject *file;
Tim Peters84e87f32001-03-17 04:50:51 +00005163
Tim Peterscba30e22003-02-01 06:24:36 +00005164 if (!( PyArg_ParseTuple(args, "O:Unpickler", &file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005165 return NULL;
5166 return (PyObject *)newUnpicklerobject(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005167}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005168
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005169
Guido van Rossum60456fd1997-04-09 17:36:32 +00005170static void
Tim Peterscba30e22003-02-01 06:24:36 +00005171Unpickler_dealloc(Unpicklerobject *self)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005172{
5173 Py_XDECREF(self->readline);
5174 Py_XDECREF(self->read);
5175 Py_XDECREF(self->file);
5176 Py_XDECREF(self->memo);
5177 Py_XDECREF(self->stack);
5178 Py_XDECREF(self->pers_func);
5179 Py_XDECREF(self->arg);
5180 Py_XDECREF(self->last_string);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005181
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005182 if (self->marks) {
5183 free(self->marks);
5184 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005185
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005186 if (self->buf_size) {
5187 free(self->buf);
5188 }
Tim Peters84e87f32001-03-17 04:50:51 +00005189
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005190 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005191}
5192
5193
5194static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005195Unpickler_getattr(Unpicklerobject *self, char *name)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005196{
5197 if (!strcmp(name, "persistent_load")) {
5198 if (!self->pers_func) {
5199 PyErr_SetString(PyExc_AttributeError, name);
5200 return NULL;
5201 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005202
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005203 Py_INCREF(self->pers_func);
5204 return self->pers_func;
5205 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005206
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005207 if (!strcmp(name, "find_global")) {
5208 if (!self->find_class) {
5209 PyErr_SetString(PyExc_AttributeError, name);
5210 return NULL;
5211 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005212
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005213 Py_INCREF(self->find_class);
5214 return self->find_class;
5215 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005216
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005217 if (!strcmp(name, "memo")) {
5218 if (!self->memo) {
5219 PyErr_SetString(PyExc_AttributeError, name);
5220 return NULL;
5221 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005222
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005223 Py_INCREF(self->memo);
5224 return self->memo;
5225 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005226
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005227 if (!strcmp(name, "UnpicklingError")) {
5228 Py_INCREF(UnpicklingError);
5229 return UnpicklingError;
5230 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005231
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005232 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005233}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005234
Guido van Rossum60456fd1997-04-09 17:36:32 +00005235
5236static int
Tim Peterscba30e22003-02-01 06:24:36 +00005237Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005238{
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005239
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005240 if (!strcmp(name, "persistent_load")) {
5241 Py_XDECREF(self->pers_func);
5242 self->pers_func = value;
5243 Py_XINCREF(value);
5244 return 0;
5245 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005246
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005247 if (!strcmp(name, "find_global")) {
5248 Py_XDECREF(self->find_class);
5249 self->find_class = value;
5250 Py_XINCREF(value);
5251 return 0;
5252 }
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00005253
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005254 if (! value) {
5255 PyErr_SetString(PyExc_TypeError,
5256 "attribute deletion is not supported");
5257 return -1;
5258 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005259
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005260 if (strcmp(name, "memo") == 0) {
5261 if (!PyDict_Check(value)) {
Tim Peterscba30e22003-02-01 06:24:36 +00005262 PyErr_SetString(PyExc_TypeError,
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005263 "memo must be a dictionary");
5264 return -1;
5265 }
5266 Py_XDECREF(self->memo);
5267 self->memo = value;
5268 Py_INCREF(value);
5269 return 0;
5270 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00005271
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005272 PyErr_SetString(PyExc_AttributeError, name);
5273 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005274}
5275
Tim Peters5bd2a792003-02-01 16:45:06 +00005276/* ---------------------------------------------------------------------------
5277 * Module-level functions.
5278 */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005279
Tim Peters5bd2a792003-02-01 16:45:06 +00005280/* dump(obj, file, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005281static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005282cpm_dump(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005283{
5284 PyObject *ob, *file, *res = NULL;
5285 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005286 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005287
Tim Peters5bd2a792003-02-01 16:45:06 +00005288 if (!( PyArg_ParseTuple(args, "OO|i", &ob, &file, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005289 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005290
Tim Peters5bd2a792003-02-01 16:45:06 +00005291 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005292 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005293
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005294 if (dump(pickler, ob) < 0)
5295 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005296
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005297 Py_INCREF(Py_None);
5298 res = Py_None;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005299
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005300 finally:
5301 Py_XDECREF(pickler);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005302
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005303 return res;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005304}
5305
5306
Tim Peters5bd2a792003-02-01 16:45:06 +00005307/* dumps(obj, proto=0). */
Guido van Rossum60456fd1997-04-09 17:36:32 +00005308static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005309cpm_dumps(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005310{
5311 PyObject *ob, *file = 0, *res = NULL;
5312 Picklerobject *pickler = 0;
Tim Peters5bd2a792003-02-01 16:45:06 +00005313 int proto = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005314
Tim Peters5bd2a792003-02-01 16:45:06 +00005315 if (!( PyArg_ParseTuple(args, "O|i:dumps", &ob, &proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005316 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005317
Tim Peterscba30e22003-02-01 06:24:36 +00005318 if (!( file = PycStringIO->NewOutput(128)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005319 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005320
Tim Peters5bd2a792003-02-01 16:45:06 +00005321 if (!( pickler = newPicklerobject(file, proto)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005322 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005323
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005324 if (dump(pickler, ob) < 0)
5325 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005326
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005327 res = PycStringIO->cgetvalue(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005328
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005329 finally:
5330 Py_XDECREF(pickler);
5331 Py_XDECREF(file);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005332
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005333 return res;
Tim Peters84e87f32001-03-17 04:50:51 +00005334}
5335
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005336
Tim Peters5bd2a792003-02-01 16:45:06 +00005337/* load(fileobj). */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005338static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005339cpm_load(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005340{
5341 Unpicklerobject *unpickler = 0;
5342 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005343
Tim Peterscba30e22003-02-01 06:24:36 +00005344 if (!( PyArg_ParseTuple(args, "O:load", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005345 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005346
Tim Peterscba30e22003-02-01 06:24:36 +00005347 if (!( unpickler = newUnpicklerobject(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005348 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005349
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005350 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005351
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005352 finally:
5353 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005354
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005355 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005356}
5357
5358
Tim Peters5bd2a792003-02-01 16:45:06 +00005359/* loads(string) */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005360static PyObject *
Tim Peterscba30e22003-02-01 06:24:36 +00005361cpm_loads(PyObject *self, PyObject *args)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005362{
5363 PyObject *ob, *file = 0, *res = NULL;
5364 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005365
Tim Peterscba30e22003-02-01 06:24:36 +00005366 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005367 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005368
Tim Peterscba30e22003-02-01 06:24:36 +00005369 if (!( file = PycStringIO->NewInput(ob)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005370 goto finally;
Tim Peters84e87f32001-03-17 04:50:51 +00005371
Tim Peterscba30e22003-02-01 06:24:36 +00005372 if (!( unpickler = newUnpicklerobject(file)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005373 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005374
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005375 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005376
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005377 finally:
5378 Py_XDECREF(file);
5379 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005380
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005381 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005382}
5383
5384
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005385PyDoc_STRVAR(Unpicklertype__doc__,
5386"Objects that know how to unpickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005387
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005388static PyTypeObject Unpicklertype = {
5389 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00005390 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +00005391 "cPickle.Unpickler", /*tp_name*/
Guido van Rossum60456fd1997-04-09 17:36:32 +00005392 sizeof(Unpicklerobject), /*tp_basicsize*/
5393 0, /*tp_itemsize*/
5394 /* methods */
5395 (destructor)Unpickler_dealloc, /*tp_dealloc*/
5396 (printfunc)0, /*tp_print*/
5397 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
5398 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
5399 (cmpfunc)0, /*tp_compare*/
5400 (reprfunc)0, /*tp_repr*/
5401 0, /*tp_as_number*/
5402 0, /*tp_as_sequence*/
5403 0, /*tp_as_mapping*/
5404 (hashfunc)0, /*tp_hash*/
5405 (ternaryfunc)0, /*tp_call*/
5406 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005407
Guido van Rossum60456fd1997-04-09 17:36:32 +00005408 /* Space for future expansion */
5409 0L,0L,0L,0L,
5410 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00005411};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005412
Guido van Rossum60456fd1997-04-09 17:36:32 +00005413static struct PyMethodDef cPickle_methods[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00005414 {"dump", (PyCFunction)cpm_dump, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005415 PyDoc_STR("dump(object, file, proto=0) -- "
5416 "Write an object in pickle format to the given file.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005417 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005418 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005419 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005420
Neal Norwitzb0493252002-03-31 14:44:22 +00005421 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005422 PyDoc_STR("dumps(object, proto=0) -- "
5423 "Return a string containing an object in pickle format.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005424 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005425 "See the Pickler docstring for the meaning of optional argument proto.")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005426 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005427
Neal Norwitzb0493252002-03-31 14:44:22 +00005428 {"load", (PyCFunction)cpm_load, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005429 PyDoc_STR("load(file) -- Load a pickle from the given file")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005430
Neal Norwitzb0493252002-03-31 14:44:22 +00005431 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
Neal Norwitz200788c2002-08-13 22:20:41 +00005432 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
Tim Peters5bd2a792003-02-01 16:45:06 +00005433
Neal Norwitzb0493252002-03-31 14:44:22 +00005434 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005435 PyDoc_STR("Pickler(file, proto=0) -- Create a pickler.\n"
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005436 "\n"
Tim Peters5bd2a792003-02-01 16:45:06 +00005437 "This takes a file-like object for writing a pickle data stream.\n"
5438 "The optional proto argument tells the pickler to use the given\n"
5439 "protocol; supported protocols are 0, 1, 2. The default\n"
5440 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5441 "only protocol that can be written to a file opened in text\n"
5442 "mode and read back successfully. When using a protocol higher\n"
5443 "than 0, make sure the file is opened in binary mode, both when\n"
5444 "pickling and unpickling.)\n"
5445 "\n"
5446 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5447 "more efficient than protocol 1.\n"
5448 "\n"
5449 "Specifying a negative protocol version selects the highest\n"
5450 "protocol version supported. The higher the protocol used, the\n"
5451 "more recent the version of Python needed to read the pickle\n"
5452 "produced.\n"
5453 "\n"
5454 "The file parameter must have a write() method that accepts a single\n"
5455 "string argument. It can thus be an open file object, a StringIO\n"
5456 "object, or any other custom object that meets this interface.\n")
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005457 },
Tim Peters5bd2a792003-02-01 16:45:06 +00005458
Neal Norwitzb0493252002-03-31 14:44:22 +00005459 {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
Tim Peters5bd2a792003-02-01 16:45:06 +00005460 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5461
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005462 { NULL, NULL }
5463};
5464
Guido van Rossum60456fd1997-04-09 17:36:32 +00005465static int
Tim Peterscba30e22003-02-01 06:24:36 +00005466init_stuff(PyObject *module_dict)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005467{
5468 PyObject *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005469
Martin v. Löwis43c9d8a2002-04-01 12:34:33 +00005470#define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005471
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005472 INIT_STR(__class__);
5473 INIT_STR(__getinitargs__);
5474 INIT_STR(__dict__);
5475 INIT_STR(__getstate__);
5476 INIT_STR(__setstate__);
5477 INIT_STR(__name__);
5478 INIT_STR(__main__);
5479 INIT_STR(__reduce__);
5480 INIT_STR(write);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005481 INIT_STR(append);
5482 INIT_STR(read);
5483 INIT_STR(readline);
5484 INIT_STR(copy_reg);
5485 INIT_STR(dispatch_table);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005486 INIT_STR(__basicnew__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005487
Tim Peterscba30e22003-02-01 06:24:36 +00005488 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005489 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005490
Tim Peters1f1b2d22003-02-01 02:16:37 +00005491 /* This is special because we want to use a different
5492 one in restricted mode. */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005493 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
Tim Peters5b7da392003-02-04 00:21:07 +00005494 if (!dispatch_table) return -1;
5495
5496 extension_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005497 "_extension_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005498 if (!extension_registry) return -1;
5499
5500 inverted_registry = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005501 "_inverted_registry");
Tim Peters5b7da392003-02-04 00:21:07 +00005502 if (!inverted_registry) return -1;
5503
5504 extension_cache = PyObject_GetAttrString(copy_reg,
Guido van Rossumd4b920c2003-02-04 01:54:49 +00005505 "_extension_cache");
Tim Peters5b7da392003-02-04 00:21:07 +00005506 if (!extension_cache) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005507
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005508 Py_DECREF(copy_reg);
Guido van Rossum60456fd1997-04-09 17:36:32 +00005509
Tim Peters731098b2003-02-04 20:56:09 +00005510 if (!(empty_tuple = PyTuple_New(0)))
5511 return -1;
5512
5513 two_tuple = PyTuple_New(2);
5514 if (two_tuple == NULL)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005515 return -1;
Tim Peters3e667d52003-02-04 21:47:44 +00005516 /* We use this temp container with no regard to refcounts, or to
5517 * keeping containees alive. Exempt from GC, because we don't
5518 * want anything looking at two_tuple() by magic.
5519 */
5520 PyObject_GC_UnTrack(two_tuple);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00005521
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005522 /* Ugh */
5523 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5524 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5525 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005526
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005527 if (!( t=PyDict_New())) return -1;
5528 if (!( r=PyRun_String(
5529 "def __init__(self, *args): self.args=args\n\n"
5530 "def __str__(self):\n"
5531 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5532 Py_file_input,
5533 module_dict, t) )) return -1;
5534 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005535
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005536 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
Tim Peterscba30e22003-02-01 06:24:36 +00005537 if (!PickleError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005538 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005539
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005540 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005541
Tim Peterscba30e22003-02-01 06:24:36 +00005542 PicklingError = PyErr_NewException("cPickle.PicklingError",
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005543 PickleError, NULL);
Tim Peterscba30e22003-02-01 06:24:36 +00005544 if (!PicklingError)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005545 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005546
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005547 if (!( t=PyDict_New())) return -1;
5548 if (!( r=PyRun_String(
5549 "def __init__(self, *args): self.args=args\n\n"
5550 "def __str__(self):\n"
5551 " a=self.args\n"
5552 " a=a and type(a[0]) or '(what)'\n"
5553 " return 'Cannot pickle %s objects' % a\n"
5554 , Py_file_input,
5555 module_dict, t) )) return -1;
5556 Py_DECREF(r);
Tim Peters84e87f32001-03-17 04:50:51 +00005557
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005558 if (!( UnpickleableError = PyErr_NewException(
Tim Peterscba30e22003-02-01 06:24:36 +00005559 "cPickle.UnpickleableError", PicklingError, t)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005560 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005561
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005562 Py_DECREF(t);
Guido van Rossumc03158b1999-06-09 15:23:31 +00005563
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005564 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
Tim Peterscba30e22003-02-01 06:24:36 +00005565 PickleError, NULL)))
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005566 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005567
Martin v. Löwis658009a2002-09-16 17:26:24 +00005568 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5569 UnpicklingError, NULL)))
5570 return -1;
Tim Peterscba30e22003-02-01 06:24:36 +00005571
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005572 if (PyDict_SetItemString(module_dict, "PickleError",
5573 PickleError) < 0)
5574 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005575
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005576 if (PyDict_SetItemString(module_dict, "PicklingError",
5577 PicklingError) < 0)
5578 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005579
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005580 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5581 UnpicklingError) < 0)
5582 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005583
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005584 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5585 UnpickleableError) < 0)
5586 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00005587
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005588 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5589 BadPickleGet) < 0)
5590 return -1;
Guido van Rossumc03158b1999-06-09 15:23:31 +00005591
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005592 PycString_IMPORT;
Guido van Rossum053b8df1998-11-25 16:18:00 +00005593
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005594 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005595}
5596
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005597#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5598#define PyMODINIT_FUNC void
Guido van Rossumf9ffb031999-02-04 14:54:04 +00005599#endif
Mark Hammondfe51c6d2002-08-02 02:27:13 +00005600PyMODINIT_FUNC
Tim Peterscba30e22003-02-01 06:24:36 +00005601initcPickle(void)
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005602{
5603 PyObject *m, *d, *di, *v, *k;
5604 int i;
Tim Peters5b7da392003-02-04 00:21:07 +00005605 char *rev = "1.71"; /* XXX when does this change? */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005606 PyObject *format_version;
5607 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005608
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005609 Picklertype.ob_type = &PyType_Type;
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005610 Unpicklertype.ob_type = &PyType_Type;
5611 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005612
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005613 /* Initialize some pieces. We need to do this before module creation,
Tim Peters5b7da392003-02-04 00:21:07 +00005614 * so we're forced to use a temporary dictionary. :(
5615 */
5616 di = PyDict_New();
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005617 if (!di) return;
5618 if (init_stuff(di) < 0) return;
Guido van Rossumebba4202000-09-07 14:35:37 +00005619
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005620 /* Create the module and add the functions */
5621 m = Py_InitModule4("cPickle", cPickle_methods,
5622 cPickle_module_documentation,
5623 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005624
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005625 /* Add some symbolic constants to the module */
5626 d = PyModule_GetDict(m);
Tim Peters5b7da392003-02-04 00:21:07 +00005627 v = PyString_FromString(rev);
5628 PyDict_SetItemString(d, "__version__", v);
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005629 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00005630
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005631 /* Copy data from di. Waaa. */
5632 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5633 if (PyObject_SetItem(d, k, v) < 0) {
5634 Py_DECREF(di);
5635 return;
5636 }
5637 }
5638 Py_DECREF(di);
Guido van Rossumebba4202000-09-07 14:35:37 +00005639
Tim Peters8587b3c2003-02-13 15:44:41 +00005640 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5641 if (i < 0)
5642 return;
5643
Tim Peters5b7da392003-02-04 00:21:07 +00005644 /* These are purely informational; no code uses them. */
5645 /* File format version we write. */
5646 format_version = PyString_FromString("2.0");
5647 /* Format versions we can read. */
5648 compatible_formats = Py_BuildValue("[sssss]",
5649 "1.0", /* Original protocol 0 */
5650 "1.1", /* Protocol 0 + INST */
5651 "1.2", /* Original protocol 1 */
5652 "1.3", /* Protocol 1 + BINFLOAT */
Tim Peters0dd23aa2003-02-04 00:30:46 +00005653 "2.0"); /* Original protocol 2 */
Martin v. Löwis2f6ef4c2002-04-01 17:40:08 +00005654 PyDict_SetItemString(d, "format_version", format_version);
5655 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5656 Py_XDECREF(format_version);
5657 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00005658}