blob: 0d9193687625092ffb0be84f80a475817bae0cd5 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001/*
Guido van Rossum2f80d961999-07-13 15:18:58 +00002 * cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp
Guido van Rossum053b8df1998-11-25 16:18:00 +00003 *
4 * Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * o Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the disclaimer that follows.
13 *
14 * o Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions, and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * o Neither the name of Digital Creations nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 *
24 * THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
25 * IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
28 * CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
31 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
35 * DAMAGE.
36 *
37 #
38 # If you have questions regarding this software, contact:
39 #
40 # Digital Creations, L.C.
41 # 910 Princess Ann Street
42 # Fredericksburge, Virginia 22401
43 #
44 # info@digicool.com
45 #
46 # (540) 371-6909
47 */
Guido van Rossum2f4caa41997-01-06 22:59:08 +000048
49static char cPickle_module_documentation[] =
Guido van Rossum142eeb81997-08-13 03:14:41 +000050"C implementation and optimization of the Python pickle module\n"
51"\n"
Guido van Rossum2f80d961999-07-13 15:18:58 +000052"cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp\n"
Guido van Rossum2f4caa41997-01-06 22:59:08 +000053;
54
55#include "Python.h"
56#include "cStringIO.h"
Guido van Rossum60456fd1997-04-09 17:36:32 +000057#include "mymath.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +000058
Guido van Rossum142eeb81997-08-13 03:14:41 +000059#ifndef Py_eval_input
60#include <graminit.h>
61#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000062#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000063
Guido van Rossum2f4caa41997-01-06 22:59:08 +000064#include <errno.h>
65
Guido van Rossum2f4caa41997-01-06 22:59:08 +000066#define UNLESS(E) if (!(E))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000067
Guido van Rossum60456fd1997-04-09 17:36:32 +000068#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000069
Guido van Rossum60456fd1997-04-09 17:36:32 +000070#define WRITE_BUF_SIZE 256
71
72
73#define MARK '('
74#define STOP '.'
75#define POP '0'
76#define POP_MARK '1'
77#define DUP '2'
78#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000079#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000080#define INT 'I'
81#define BININT 'J'
82#define BININT1 'K'
83#define LONG 'L'
84#define BININT2 'M'
85#define NONE 'N'
86#define PERSID 'P'
87#define BINPERSID 'Q'
88#define REDUCE 'R'
89#define STRING 'S'
90#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000091#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000092#define UNICODE 'V'
93#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000094#define APPEND 'a'
95#define BUILD 'b'
96#define GLOBAL 'c'
97#define DICT 'd'
98#define EMPTY_DICT '}'
99#define APPENDS 'e'
100#define GET 'g'
101#define BINGET 'h'
102#define INST 'i'
103#define LONG_BINGET 'j'
104#define LIST 'l'
105#define EMPTY_LIST ']'
106#define OBJ 'o'
107#define PUT 'p'
108#define BINPUT 'q'
109#define LONG_BINPUT 'r'
110#define SETITEM 's'
111#define TUPLE 't'
112#define EMPTY_TUPLE ')'
113#define SETITEMS 'u'
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000114
Guido van Rossum60456fd1997-04-09 17:36:32 +0000115static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000116
117/* atol function from string module */
118static PyObject *atol_func;
119
Guido van Rossumc03158b1999-06-09 15:23:31 +0000120static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000121static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000122static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000123static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000124static PyObject *BadPickleGet;
125
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000126
Guido van Rossum60456fd1997-04-09 17:36:32 +0000127static PyObject *dispatch_table;
128static PyObject *safe_constructors;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000129static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000130
Guido van Rossum60456fd1997-04-09 17:36:32 +0000131static PyObject *__class___str, *__getinitargs___str, *__dict___str,
132 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
133 *write_str, *__safe_for_unpickling___str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000134 *read_str, *readline_str, *__main___str, *__basicnew___str,
Guido van Rossum053b8df1998-11-25 16:18:00 +0000135 *copy_reg_str, *dispatch_table_str, *safe_constructors_str, *empty_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000136
Guido van Rossum60456fd1997-04-09 17:36:32 +0000137static int save();
138static int put2();
139
Guido van Rossum053b8df1998-11-25 16:18:00 +0000140#ifndef PyList_SET_ITEM
141#define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
142#endif
143#ifndef PyList_GET_SIZE
144#define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
145#endif
146#ifndef PyTuple_SET_ITEM
147#define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
148#endif
149#ifndef PyTuple_GET_SIZE
150#define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
151#endif
152#ifndef PyString_GET_SIZE
153#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
154#endif
155
156/*************************************************************************
157 Internal Data type for pickle data. */
158
159typedef struct {
160 PyObject_HEAD
161 int length, size;
162 PyObject **data;
163} Pdata;
164
165static void
166Pdata_dealloc(Pdata *self) {
167 int i;
168 PyObject **p;
169
170 for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
171
172 if (self->data) free(self->data);
173
Guido van Rossumb18618d2000-05-03 23:44:39 +0000174 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000175}
176
177static PyTypeObject PdataType = {
178 PyObject_HEAD_INIT(NULL) 0, "Pdata", sizeof(Pdata), 0,
179 (destructor)Pdata_dealloc,
180 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
181};
182
183#define Pdata_Check(O) ((O)->ob_type == &PdataType)
184
185static PyObject *
186Pdata_New() {
187 Pdata *self;
188
Guido van Rossumb18618d2000-05-03 23:44:39 +0000189 UNLESS (self = PyObject_New(Pdata, &PdataType)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000190 self->size=8;
191 self->length=0;
192 self->data=malloc(self->size * sizeof(PyObject*));
193 if (self->data) return (PyObject*)self;
194 Py_DECREF(self);
195 return PyErr_NoMemory();
196}
197
198static int
199stackUnderflow() {
200 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
201 return -1;
202}
203
204static int
205Pdata_clear(Pdata *self, int clearto) {
206 int i;
207 PyObject **p;
208
209 if (clearto < 0) return stackUnderflow();
210 if (clearto >= self->length) return 0;
211
212 for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
213 Py_DECREF(*p);
214 self->length=clearto;
215
216 return 0;
217}
218
219
220static int
221Pdata_grow(Pdata *self) {
222 if (! self->size) {
223 PyErr_NoMemory();
224 return -1;
225 }
226 self->size *= 2;
227 self->data = realloc(self->data, self->size*sizeof(PyObject*));
228 if (! self->data) {
229 self->size = 0;
230 PyErr_NoMemory();
231 return -1;
232 }
233 return 0;
234}
235
236#define PDATA_POP(D,V) { \
237 if ((D)->length) V=D->data[--((D)->length)]; \
238 else { \
239 PyErr_SetString(UnpicklingError, "bad pickle data"); \
240 V=NULL; \
241 } \
242}
243
244
245static PyObject *
246Pdata_popTuple(Pdata *self, int start) {
247 PyObject *r;
248 int i, j, l;
249
250 l=self->length-start;
251 UNLESS (r=PyTuple_New(l)) return NULL;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000252 for (i=start, j=0 ; j < l; i++, j++)
253 PyTuple_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000254
255 self->length=start;
256 return r;
257}
258
259static PyObject *
260Pdata_popList(Pdata *self, int start) {
261 PyObject *r;
262 int i, j, l;
263
264 l=self->length-start;
265 UNLESS (r=PyList_New(l)) return NULL;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000266 for (i=start, j=0 ; j < l; i++, j++)
267 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000268
269 self->length=start;
270 return r;
271}
272
273#define PDATA_APPEND_(D,O,ER) { \
274 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
275}
276
277#define PDATA_APPEND(D,O,ER) { \
278 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
279 Pdata_grow((Pdata*)(D)) < 0) \
280 return ER; \
281 Py_INCREF(O); \
282 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
283}
284
285#define PDATA_PUSH(D,O,ER) { \
286 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
287 Pdata_grow((Pdata*)(D)) < 0) { \
288 Py_DECREF(O); \
289 return ER; \
290 } \
291 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
292}
293
294/*************************************************************************/
295
296#define ARG_TUP(self, o) { \
297 if (self->arg || (self->arg=PyTuple_New(1))) { \
298 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
299 PyTuple_SET_ITEM(self->arg,0,o); \
300 } \
301 else { \
302 Py_DECREF(o); \
303 } \
304}
305
306#define FREE_ARG_TUP(self) { \
307 if (self->arg->ob_refcnt > 1) { \
308 Py_DECREF(self->arg); \
309 self->arg=NULL; \
310 } \
311 }
312
Guido van Rossum60456fd1997-04-09 17:36:32 +0000313typedef struct {
314 PyObject_HEAD
315 FILE *fp;
316 PyObject *write;
317 PyObject *file;
318 PyObject *memo;
319 PyObject *arg;
320 PyObject *pers_func;
321 PyObject *inst_pers_func;
322 int bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000323 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000324 int (*write_func)();
325 char *write_buf;
326 int buf_size;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000327 PyObject *dispatch_table;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000328} Picklerobject;
329
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000330staticforward PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000331
Guido van Rossum60456fd1997-04-09 17:36:32 +0000332typedef struct {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000333 PyObject_HEAD
334 FILE *fp;
335 PyObject *file;
336 PyObject *readline;
337 PyObject *read;
338 PyObject *memo;
339 PyObject *arg;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000340 Pdata *stack;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000341 PyObject *mark;
342 PyObject *pers_func;
343 PyObject *last_string;
344 int *marks;
345 int num_marks;
346 int marks_size;
347 int (*read_func)();
348 int (*readline_func)();
349 int buf_size;
350 char *buf;
351 PyObject *safe_constructors;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +0000352 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000353} Unpicklerobject;
354
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000355staticforward PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000356
Guido van Rossum60456fd1997-04-09 17:36:32 +0000357int
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000358cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000359 PyObject *v;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000360
Guido van Rossum053b8df1998-11-25 16:18:00 +0000361 if ((v = PyObject_GetItem(o,key))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000362 Py_DECREF(v);
363 return 1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000364 }
365
Guido van Rossum60456fd1997-04-09 17:36:32 +0000366 PyErr_Clear();
367 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000368}
369
Guido van Rossumd385d591997-04-09 17:47:47 +0000370static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000371PyObject *
372#ifdef HAVE_STDARG_PROTOTYPES
373/* VARARGS 2 */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000374cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000375#else
376/* VARARGS */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000377cPickle_ErrFormat(va_alist) va_dcl {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000378#endif
Guido van Rossum60456fd1997-04-09 17:36:32 +0000379 va_list va;
380 PyObject *args=0, *retval=0;
381#ifdef HAVE_STDARG_PROTOTYPES
382 va_start(va, format);
383#else
384 PyObject *ErrType;
385 char *stringformat, *format;
386 va_start(va);
387 ErrType = va_arg(va, PyObject *);
388 stringformat = va_arg(va, char *);
389 format = va_arg(va, char *);
390#endif
391
Guido van Rossum053b8df1998-11-25 16:18:00 +0000392 if (format) args = Py_VaBuildValue(format, va);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000393 va_end(va);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000394 if (format && ! args) return NULL;
395 if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000396
Guido van Rossum053b8df1998-11-25 16:18:00 +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 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000406 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000407 else
Guido van Rossum053b8df1998-11-25 16:18:00 +0000408 if (args) retval=args;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000409 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000410 PyErr_SetObject(ErrType,Py_None);
411 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000412 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000413 PyErr_SetObject(ErrType,retval);
414 Py_DECREF(retval);
415 return NULL;
416}
417
418static int
419write_file(Picklerobject *self, char *s, int n) {
420 if (s == NULL) {
421 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000422 }
423
Guido van Rossum60456fd1997-04-09 17:36:32 +0000424 if ((int)fwrite(s, sizeof(char), n, self->fp) != n) {
425 PyErr_SetFromErrno(PyExc_IOError);
426 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000427 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000428
429 return n;
430}
431
Guido van Rossum60456fd1997-04-09 17:36:32 +0000432static int
433write_cStringIO(Picklerobject *self, char *s, int n) {
434 if (s == NULL) {
435 return 0;
436 }
437
438 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
439 return -1;
440 }
441
442 return n;
443}
444
Guido van Rossum60456fd1997-04-09 17:36:32 +0000445static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000446write_none(Picklerobject *self, char *s, int n) {
447 if (s == NULL) return 0;
448 return n;
449}
450
Guido van Rossum142eeb81997-08-13 03:14:41 +0000451static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000452write_other(Picklerobject *self, char *s, int n) {
453 PyObject *py_str = 0, *junk = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000454
455 if (s == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000456 UNLESS (self->buf_size) return 0;
457 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000458 PyString_FromStringAndSize(self->write_buf, self->buf_size))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000459 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000460 }
461 else {
462 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
463 if (write_other(self, NULL, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000464 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000465 }
466
467 if (n > WRITE_BUF_SIZE) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000468 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000469 PyString_FromStringAndSize(s, n))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000470 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000471 }
472 else {
473 memcpy(self->write_buf + self->buf_size, s, n);
474 self->buf_size += n;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000475 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000476 }
477 }
478
Guido van Rossum053b8df1998-11-25 16:18:00 +0000479 if (self->write) {
480 /* object with write method */
481 ARG_TUP(self, py_str);
482 if (self->arg) {
483 junk = PyObject_CallObject(self->write, self->arg);
484 FREE_ARG_TUP(self);
485 }
486 if (junk) Py_DECREF(junk);
487 else return -1;
488 }
489 else
490 PDATA_PUSH(self->file, py_str, -1);
491
492 self->buf_size = 0;
493 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000494}
495
496
497static int
498read_file(Unpicklerobject *self, char **s, int n) {
499
500 if (self->buf_size == 0) {
501 int size;
502
503 size = ((n < 32) ? 32 : n);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000504 UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000505 PyErr_NoMemory();
506 return -1;
507 }
508
509 self->buf_size = size;
510 }
511 else if (n > self->buf_size) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000512 UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000513 PyErr_NoMemory();
514 return -1;
515 }
516
517 self->buf_size = n;
518 }
519
520 if ((int)fread(self->buf, sizeof(char), n, self->fp) != n) {
521 if (feof(self->fp)) {
522 PyErr_SetNone(PyExc_EOFError);
523 return -1;
524 }
525
526 PyErr_SetFromErrno(PyExc_IOError);
527 return -1;
528 }
529
530 *s = self->buf;
531
532 return n;
533}
534
535
536static int
537readline_file(Unpicklerobject *self, char **s) {
538 int i;
539
540 if (self->buf_size == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000541 UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000542 PyErr_NoMemory();
543 return -1;
544 }
545
546 self->buf_size = 40;
547 }
548
549 i = 0;
550 while (1) {
551 for (; i < (self->buf_size - 1); i++) {
552 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
553 self->buf[i + 1] = '\0';
554 *s = self->buf;
555 return i + 1;
556 }
557 }
558
Guido van Rossum053b8df1998-11-25 16:18:00 +0000559 UNLESS (self->buf = (char *)realloc(self->buf,
Guido van Rossum60456fd1997-04-09 17:36:32 +0000560 (self->buf_size * 2) * sizeof(char))) {
561 PyErr_NoMemory();
562 return -1;
563 }
564
565 self->buf_size *= 2;
566 }
567
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000568}
569
570
571static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000572read_cStringIO(Unpicklerobject *self, char **s, int n) {
573 char *ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000574
Guido van Rossum60456fd1997-04-09 17:36:32 +0000575 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
576 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000577 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000578 }
579
Guido van Rossum60456fd1997-04-09 17:36:32 +0000580 *s = ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000581
Guido van Rossum60456fd1997-04-09 17:36:32 +0000582 return n;
583}
584
585
586static int
587readline_cStringIO(Unpicklerobject *self, char **s) {
588 int n;
589 char *ptr;
590
591 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
592 return -1;
593 }
594
595 *s = ptr;
596
597 return n;
598}
599
600
601static int
602read_other(Unpicklerobject *self, char **s, int n) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000603 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000604
Guido van Rossum053b8df1998-11-25 16:18:00 +0000605 UNLESS (bytes = PyInt_FromLong(n)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000606
Guido van Rossum053b8df1998-11-25 16:18:00 +0000607 ARG_TUP(self, bytes);
608 if (self->arg) {
609 str = PyObject_CallObject(self->read, self->arg);
610 FREE_ARG_TUP(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000611 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000612 if (! str) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000613
614 Py_XDECREF(self->last_string);
615 self->last_string = str;
616
Guido van Rossum053b8df1998-11-25 16:18:00 +0000617 if (! (*s = PyString_AsString(str))) return -1;
618 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000619}
620
621
622static int
623readline_other(Unpicklerobject *self, char **s) {
624 PyObject *str;
625 int str_size;
626
Guido van Rossum053b8df1998-11-25 16:18:00 +0000627 UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000628 return -1;
629 }
630
Guido van Rossum053b8df1998-11-25 16:18:00 +0000631 if ((str_size = PyString_Size(str)) < 0)
632 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000633
634 Py_XDECREF(self->last_string);
635 self->last_string = str;
636
Guido van Rossum053b8df1998-11-25 16:18:00 +0000637 if (! (*s = PyString_AsString(str)))
638 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000639
640 return str_size;
641}
642
643
644static char *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000645pystrndup(char *s, int l) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000646 char *r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000647 UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
Guido van Rossum60456fd1997-04-09 17:36:32 +0000648 memcpy(r,s,l);
649 r[l]=0;
650 return r;
651}
652
653
654static int
655get(Picklerobject *self, PyObject *id) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000656 PyObject *value, *mv;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000657 long c_value;
658 char s[30];
Guido van Rossum534b7c52000-06-28 22:23:56 +0000659 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000660
Guido van Rossum053b8df1998-11-25 16:18:00 +0000661 UNLESS (mv = PyDict_GetItem(self->memo, id)) {
662 PyErr_SetObject(PyExc_KeyError, id);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000663 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000664 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000665
Guido van Rossum053b8df1998-11-25 16:18:00 +0000666 UNLESS (value = PyTuple_GetItem(mv, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000667 return -1;
668
Guido van Rossum053b8df1998-11-25 16:18:00 +0000669 UNLESS (PyInt_Check(value)) {
670 PyErr_SetString(PicklingError, "no int where int expected in memo");
671 return -1;
672 }
673 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000674
675 if (!self->bin) {
676 s[0] = GET;
677 sprintf(s + 1, "%ld\n", c_value);
678 len = strlen(s);
679 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000680 else if (Pdata_Check(self->file)) {
681 if (write_other(self, NULL, 0) < 0) return -1;
682 PDATA_APPEND(self->file, mv, -1);
683 return 0;
684 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000685 else {
686 if (c_value < 256) {
687 s[0] = BINGET;
688 s[1] = (int)(c_value & 0xff);
689 len = 2;
690 }
691 else {
692 s[0] = LONG_BINGET;
693 s[1] = (int)(c_value & 0xff);
694 s[2] = (int)((c_value >> 8) & 0xff);
695 s[3] = (int)((c_value >> 16) & 0xff);
696 s[4] = (int)((c_value >> 24) & 0xff);
697 len = 5;
698 }
699 }
700
701 if ((*self->write_func)(self, s, len) < 0)
702 return -1;
703
704 return 0;
705}
706
707
708static int
709put(Picklerobject *self, PyObject *ob) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000710 if (ob->ob_refcnt < 2 || self->fast)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000711 return 0;
712
713 return put2(self, ob);
714}
715
716
717static int
718put2(Picklerobject *self, PyObject *ob) {
719 char c_str[30];
Guido van Rossum534b7c52000-06-28 22:23:56 +0000720 int p;
721 size_t len;
722 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000723 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000724
725 if (self->fast) return 0;
726
Guido van Rossum60456fd1997-04-09 17:36:32 +0000727 if ((p = PyDict_Size(self->memo)) < 0)
728 goto finally;
729
Guido van Rossum053b8df1998-11-25 16:18:00 +0000730 p++; /* Make sure memo keys are positive! */
731
Guido van Rossum534b7c52000-06-28 22:23:56 +0000732 UNLESS (py_ob_id = PyLong_FromVoidPtr(ob))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000733 goto finally;
734
735 UNLESS (memo_len = PyInt_FromLong(p))
736 goto finally;
737
738 UNLESS (t = PyTuple_New(2))
739 goto finally;
740
741 PyTuple_SET_ITEM(t, 0, memo_len);
742 Py_INCREF(memo_len);
743 PyTuple_SET_ITEM(t, 1, ob);
744 Py_INCREF(ob);
745
746 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
747 goto finally;
748
Guido van Rossum60456fd1997-04-09 17:36:32 +0000749 if (!self->bin) {
750 c_str[0] = PUT;
751 sprintf(c_str + 1, "%d\n", p);
752 len = strlen(c_str);
753 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000754 else if (Pdata_Check(self->file)) {
755 if (write_other(self, NULL, 0) < 0) return -1;
756 PDATA_APPEND(self->file, memo_len, -1);
757 res=0; /* Job well done ;) */
758 goto finally;
759 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000760 else {
761 if (p >= 256) {
762 c_str[0] = LONG_BINPUT;
763 c_str[1] = (int)(p & 0xff);
764 c_str[2] = (int)((p >> 8) & 0xff);
765 c_str[3] = (int)((p >> 16) & 0xff);
766 c_str[4] = (int)((p >> 24) & 0xff);
767 len = 5;
768 }
769 else {
770 c_str[0] = BINPUT;
771 c_str[1] = p;
772 len = 2;
773 }
774 }
775
776 if ((*self->write_func)(self, c_str, len) < 0)
777 goto finally;
778
Guido van Rossum60456fd1997-04-09 17:36:32 +0000779 res = 0;
780
781finally:
782 Py_XDECREF(py_ob_id);
783 Py_XDECREF(memo_len);
784 Py_XDECREF(t);
785
786 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000787}
788
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000789#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000790
791static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000792PyImport_Import(PyObject *module_name) {
793 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
794 static PyObject *standard_builtins=0;
795 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
796
Guido van Rossum053b8df1998-11-25 16:18:00 +0000797 UNLESS (silly_list) {
798 UNLESS (__import___str=PyString_FromString("__import__"))
799 return NULL;
800 UNLESS (__builtins___str=PyString_FromString("__builtins__"))
801 return NULL;
802 UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
803 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000804 }
805
Guido van Rossum053b8df1998-11-25 16:18:00 +0000806 if ((globals=PyEval_GetGlobals())) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000807 Py_INCREF(globals);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000808 UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
809 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000810 }
811 else {
812 PyErr_Clear();
813
Guido van Rossum053b8df1998-11-25 16:18:00 +0000814 UNLESS (standard_builtins ||
815 (standard_builtins=PyImport_ImportModule("__builtin__")))
816 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000817
818 __builtins__=standard_builtins;
819 Py_INCREF(__builtins__);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000820 UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
821 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000822 }
823
Guido van Rossum053b8df1998-11-25 16:18:00 +0000824 if (PyDict_Check(__builtins__)) {
825 UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000826 }
827 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000828 UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000829 }
830
Guido van Rossum053b8df1998-11-25 16:18:00 +0000831 UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
832 module_name, globals, globals, silly_list))
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000833 goto err;
834
835 Py_DECREF(globals);
836 Py_DECREF(__builtins__);
837 Py_DECREF(__import__);
838
839 return r;
840err:
841 Py_XDECREF(globals);
842 Py_XDECREF(__builtins__);
843 Py_XDECREF(__import__);
844 return NULL;
845}
846
847static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +0000848whichmodule(PyObject *global, PyObject *global_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000849 int i, j;
850 PyObject *module = 0, *modules_dict = 0,
851 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000852
Guido van Rossum45188231997-09-28 05:38:51 +0000853 module = PyObject_GetAttrString(global, "__module__");
854 if (module) return module;
855 PyErr_Clear();
856
Guido van Rossum053b8df1998-11-25 16:18:00 +0000857 UNLESS (modules_dict = PySys_GetObject("modules"))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000858 return NULL;
859
Guido van Rossum60456fd1997-04-09 17:36:32 +0000860 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000861 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
862
Guido van Rossum053b8df1998-11-25 16:18:00 +0000863 if (PyObject_Compare(name, __main___str)==0) continue;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000864
Guido van Rossum053b8df1998-11-25 16:18:00 +0000865 UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000866 PyErr_Clear();
867 continue;
868 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000869
Guido van Rossum60456fd1997-04-09 17:36:32 +0000870 if (global_name_attr != global) {
871 Py_DECREF(global_name_attr);
872 continue;
873 }
874
875 Py_DECREF(global_name_attr);
876
877 break;
878 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000879
880 /* The following implements the rule in pickle.py added in 1.5
881 that used __main__ if no module is found. I don't actually
882 like this rule. jlf
883 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000884 if (!j) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000885 j=1;
886 name=__main___str;
887 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000888
889 Py_INCREF(name);
890 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000891}
892
893
Guido van Rossum60456fd1997-04-09 17:36:32 +0000894static int
895save_none(Picklerobject *self, PyObject *args) {
896 static char none = NONE;
897 if ((*self->write_func)(self, &none, 1) < 0)
898 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000899
Guido van Rossum60456fd1997-04-09 17:36:32 +0000900 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000901}
902
903
Guido van Rossum60456fd1997-04-09 17:36:32 +0000904static int
905save_int(Picklerobject *self, PyObject *args) {
906 char c_str[32];
907 long l = PyInt_AS_LONG((PyIntObject *)args);
908 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000909
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000910 if (!self->bin
911#if SIZEOF_LONG > 4
Guido van Rossum053b8df1998-11-25 16:18:00 +0000912 || (l >> 32)
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000913#endif
Guido van Rossum053b8df1998-11-25 16:18:00 +0000914 ) {
915 /* Save extra-long ints in non-binary mode, so that
916 we can use python long parsing code to restore,
917 if necessary. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000918 c_str[0] = INT;
919 sprintf(c_str + 1, "%ld\n", l);
920 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
921 return -1;
922 }
923 else {
924 c_str[1] = (int)( l & 0xff);
925 c_str[2] = (int)((l >> 8) & 0xff);
926 c_str[3] = (int)((l >> 16) & 0xff);
927 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000928
Guido van Rossum60456fd1997-04-09 17:36:32 +0000929 if ((c_str[4] == 0) && (c_str[3] == 0)) {
930 if (c_str[2] == 0) {
931 c_str[0] = BININT1;
932 len = 2;
933 }
934 else {
935 c_str[0] = BININT2;
936 len = 3;
937 }
938 }
939 else {
940 c_str[0] = BININT;
941 len = 5;
942 }
943
944 if ((*self->write_func)(self, c_str, len) < 0)
945 return -1;
946 }
947
948 return 0;
949}
950
951
952static int
953save_long(Picklerobject *self, PyObject *args) {
954 int size, res = -1;
955 PyObject *repr = 0;
956
957 static char l = LONG;
958
Guido van Rossum053b8df1998-11-25 16:18:00 +0000959 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000960 goto finally;
961
962 if ((size = PyString_Size(repr)) < 0)
963 goto finally;
964
965 if ((*self->write_func)(self, &l, 1) < 0)
966 goto finally;
967
968 if ((*self->write_func)(self,
969 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
970 goto finally;
971
972 if ((*self->write_func)(self, "\n", 1) < 0)
973 goto finally;
974
975 res = 0;
976
977finally:
978 Py_XDECREF(repr);
979
980 return res;
981}
982
983
984static int
985save_float(Picklerobject *self, PyObject *args) {
986 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
987
Guido van Rossum60456fd1997-04-09 17:36:32 +0000988 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000989 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000990 double f;
991 long fhi, flo;
992 char str[9], *p = str;
993
994 *p = BINFLOAT;
995 p++;
996
997 if (x < 0) {
998 s = 1;
999 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001000 }
1001 else
Guido van Rossum60456fd1997-04-09 17:36:32 +00001002 s = 0;
1003
1004 f = frexp(x, &e);
1005
1006 /* Normalize f to be in the range [1.0, 2.0) */
1007 if (0.5 <= f && f < 1.0) {
1008 f *= 2.0;
1009 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001010 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001011 else if (f == 0.0) {
1012 e = 0;
1013 }
1014 else {
1015 PyErr_SetString(PyExc_SystemError,
1016 "frexp() result out of range");
1017 return -1;
1018 }
1019
1020 if (e >= 1024) {
1021 /* XXX 1024 itself is reserved for Inf/NaN */
1022 PyErr_SetString(PyExc_OverflowError,
1023 "float too large to pack with d format");
1024 return -1;
1025 }
1026 else if (e < -1022) {
1027 /* Gradual underflow */
1028 f = ldexp(f, 1022 + e);
1029 e = 0;
1030 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001031 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001032 e += 1023;
1033 f -= 1.0; /* Get rid of leading 1 */
1034 }
1035
1036 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1037 f *= 268435456.0; /* 2**28 */
1038 fhi = (long) floor(f); /* Truncate */
1039 f -= (double)fhi;
1040 f *= 16777216.0; /* 2**24 */
1041 flo = (long) floor(f + 0.5); /* Round */
1042
1043 /* First byte */
1044 *p = (s<<7) | (e>>4);
1045 p++;
1046
1047 /* Second byte */
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001048 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum60456fd1997-04-09 17:36:32 +00001049 p++;
1050
1051 /* Third byte */
1052 *p = (fhi>>16) & 0xFF;
1053 p++;
1054
1055 /* Fourth byte */
1056 *p = (fhi>>8) & 0xFF;
1057 p++;
1058
1059 /* Fifth byte */
1060 *p = fhi & 0xFF;
1061 p++;
1062
1063 /* Sixth byte */
1064 *p = (flo>>16) & 0xFF;
1065 p++;
1066
1067 /* Seventh byte */
1068 *p = (flo>>8) & 0xFF;
1069 p++;
1070
1071 /* Eighth byte */
1072 *p = flo & 0xFF;
1073
1074 if ((*self->write_func)(self, str, 9) < 0)
1075 return -1;
1076 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001077 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001078 char c_str[250];
1079 c_str[0] = FLOAT;
Guido van Rossum104be4a1998-04-03 21:13:02 +00001080 sprintf(c_str + 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001081
1082 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
1083 return -1;
1084 }
1085
1086 return 0;
1087}
1088
1089
1090static int
Guido van Rossum142eeb81997-08-13 03:14:41 +00001091save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001092 int size, len;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001093 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001094
Guido van Rossum053b8df1998-11-25 16:18:00 +00001095 if ((size = PyString_Size(args)) < 0)
1096 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001097
1098 if (!self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001099 char *repr_str;
1100
1101 static char string = STRING;
1102
Guido van Rossum053b8df1998-11-25 16:18:00 +00001103 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001104 return -1;
1105
Guido van Rossum053b8df1998-11-25 16:18:00 +00001106 if ((len = PyString_Size(repr)) < 0)
1107 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001108 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001109
1110 if ((*self->write_func)(self, &string, 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001111 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001112
1113 if ((*self->write_func)(self, repr_str, len) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001114 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001115
1116 if ((*self->write_func)(self, "\n", 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001117 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001118
1119 Py_XDECREF(repr);
1120 }
1121 else {
1122 int i;
1123 char c_str[5];
1124
Guido van Rossum053b8df1998-11-25 16:18:00 +00001125 if ((size = PyString_Size(args)) < 0)
1126 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001127
1128 if (size < 256) {
1129 c_str[0] = SHORT_BINSTRING;
1130 c_str[1] = size;
1131 len = 2;
1132 }
1133 else {
1134 c_str[0] = BINSTRING;
1135 for (i = 1; i < 5; i++)
1136 c_str[i] = (int)(size >> ((i - 1) * 8));
1137 len = 5;
1138 }
1139
1140 if ((*self->write_func)(self, c_str, len) < 0)
1141 return -1;
1142
Guido van Rossum053b8df1998-11-25 16:18:00 +00001143 if (size > 128 && Pdata_Check(self->file)) {
1144 if (write_other(self, NULL, 0) < 0) return -1;
1145 PDATA_APPEND(self->file, args, -1);
1146 }
1147 else {
1148 if ((*self->write_func)(self,
1149 PyString_AS_STRING((PyStringObject *)args), size) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001150 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001151 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001152 }
1153
Guido van Rossum142eeb81997-08-13 03:14:41 +00001154 if (doput)
1155 if (put(self, args) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001156 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001157
1158 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001159
1160err:
1161 Py_XDECREF(repr);
1162 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001163}
1164
1165
1166static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001167save_unicode(Picklerobject *self, PyObject *args, int doput) {
1168 int size, len;
1169 PyObject *repr=0;
1170
1171 if (!PyUnicode_Check(args))
1172 return -1;
1173
1174 if (!self->bin) {
1175 char *repr_str;
1176 static char string = UNICODE;
1177
1178 UNLESS (repr = PyUnicode_AsRawUnicodeEscapeString(args))
1179 return -1;
1180
1181 if ((len = PyString_Size(repr)) < 0)
1182 goto err;
1183 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1184
1185 if ((*self->write_func)(self, &string, 1) < 0)
1186 goto err;
1187
1188 if ((*self->write_func)(self, repr_str, len) < 0)
1189 goto err;
1190
1191 if ((*self->write_func)(self, "\n", 1) < 0)
1192 goto err;
1193
1194 Py_XDECREF(repr);
1195 }
1196 else {
1197 int i;
1198 char c_str[5];
1199
1200 UNLESS (repr = PyUnicode_AsUTF8String(args))
1201 return -1;
1202
1203 if ((size = PyString_Size(repr)) < 0)
1204 goto err;
1205
1206 c_str[0] = BINUNICODE;
1207 for (i = 1; i < 5; i++)
1208 c_str[i] = (int)(size >> ((i - 1) * 8));
1209 len = 5;
1210
1211 if ((*self->write_func)(self, c_str, len) < 0)
1212 goto err;
1213
1214 if (size > 128 && Pdata_Check(self->file)) {
1215 if (write_other(self, NULL, 0) < 0)
1216 goto err;
1217 PDATA_APPEND(self->file, repr, -1);
1218 }
1219 else {
1220 if ((*self->write_func)(self, PyString_AS_STRING(repr), size) < 0)
1221 goto err;
1222 }
1223
1224 Py_DECREF(repr);
1225 }
1226
1227 if (doput)
1228 if (put(self, args) < 0)
1229 return -1;
1230
1231 return 0;
1232
1233err:
1234 Py_XDECREF(repr);
1235 return -1;
1236}
1237
1238
1239static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001240save_tuple(Picklerobject *self, PyObject *args) {
1241 PyObject *element = 0, *py_tuple_id = 0;
1242 int len, i, has_key, res = -1;
1243
1244 static char tuple = TUPLE;
1245
1246 if ((*self->write_func)(self, &MARKv, 1) < 0)
1247 goto finally;
1248
1249 if ((len = PyTuple_Size(args)) < 0)
1250 goto finally;
1251
1252 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001253 UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001254 goto finally;
1255
1256 if (save(self, element, 0) < 0)
1257 goto finally;
1258 }
1259
Guido van Rossum534b7c52000-06-28 22:23:56 +00001260 UNLESS (py_tuple_id = PyLong_FromVoidPtr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001261 goto finally;
1262
1263 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001264 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001265 goto finally;
1266
1267 if (has_key) {
1268 if (self->bin) {
1269 static char pop_mark = POP_MARK;
1270
1271 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1272 goto finally;
1273 }
1274 else {
1275 static char pop = POP;
1276
1277 for (i = 0; i <= len; i++) {
1278 if ((*self->write_func)(self, &pop, 1) < 0)
1279 goto finally;
1280 }
1281 }
1282
1283 if (get(self, py_tuple_id) < 0)
1284 goto finally;
1285
1286 res = 0;
1287 goto finally;
1288 }
1289 }
1290
1291 if ((*self->write_func)(self, &tuple, 1) < 0) {
1292 goto finally;
1293 }
1294
1295 if (put(self, args) < 0)
1296 goto finally;
1297
1298 res = 0;
1299
1300finally:
1301 Py_XDECREF(py_tuple_id);
1302
1303 return res;
1304}
1305
1306static int
1307save_empty_tuple(Picklerobject *self, PyObject *args) {
1308 static char tuple = EMPTY_TUPLE;
1309
1310 return (*self->write_func)(self, &tuple, 1);
1311}
1312
1313
1314static int
1315save_list(Picklerobject *self, PyObject *args) {
1316 PyObject *element = 0;
1317 int s_len, len, i, using_appends, res = -1;
1318 char s[3];
1319
1320 static char append = APPEND, appends = APPENDS;
1321
Guido van Rossum053b8df1998-11-25 16:18:00 +00001322 if (self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001323 s[0] = EMPTY_LIST;
1324 s_len = 1;
1325 }
1326 else {
1327 s[0] = MARK;
1328 s[1] = LIST;
1329 s_len = 2;
1330 }
1331
1332 if ((len = PyList_Size(args)) < 0)
1333 goto finally;
1334
1335 if ((*self->write_func)(self, s, s_len) < 0)
1336 goto finally;
1337
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001338 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001339 if (put(self, args) < 0)
1340 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001341 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001342 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001343 if (put2(self, args) < 0)
1344 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001345 }
1346
Guido van Rossum142eeb81997-08-13 03:14:41 +00001347 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001348 if ((*self->write_func)(self, &MARKv, 1) < 0)
1349 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001350
Guido van Rossum60456fd1997-04-09 17:36:32 +00001351 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001352 UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001353 goto finally;
1354
1355 if (save(self, element, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001356 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001357
1358 if (!using_appends) {
1359 if ((*self->write_func)(self, &append, 1) < 0)
1360 goto finally;
1361 }
1362 }
1363
1364 if (using_appends) {
1365 if ((*self->write_func)(self, &appends, 1) < 0)
1366 goto finally;
1367 }
1368
1369 res = 0;
1370
1371finally:
1372
1373 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001374}
1375
1376
Guido van Rossum60456fd1997-04-09 17:36:32 +00001377static int
1378save_dict(Picklerobject *self, PyObject *args) {
1379 PyObject *key = 0, *value = 0;
1380 int i, len, res = -1, using_setitems;
1381 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001382
Guido van Rossum60456fd1997-04-09 17:36:32 +00001383 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001384
Guido van Rossum60456fd1997-04-09 17:36:32 +00001385 if (self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001386 s[0] = EMPTY_DICT;
1387 len = 1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001388 }
1389 else {
1390 s[0] = MARK;
1391 s[1] = DICT;
1392 len = 2;
1393 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001394
Guido van Rossum60456fd1997-04-09 17:36:32 +00001395 if ((*self->write_func)(self, s, len) < 0)
1396 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001397
Guido van Rossum60456fd1997-04-09 17:36:32 +00001398 if ((len = PyDict_Size(args)) < 0)
1399 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001400
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001401 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001402 if (put(self, args) < 0)
1403 goto finally;
1404 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001405 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001406 if (put2(self, args) < 0)
1407 goto finally;
1408 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001409
Guido van Rossum142eeb81997-08-13 03:14:41 +00001410 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001411 if ((*self->write_func)(self, &MARKv, 1) < 0)
1412 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001413
Guido van Rossum60456fd1997-04-09 17:36:32 +00001414 i = 0;
1415 while (PyDict_Next(args, &i, &key, &value)) {
1416 if (save(self, key, 0) < 0)
1417 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001418
Guido van Rossum60456fd1997-04-09 17:36:32 +00001419 if (save(self, value, 0) < 0)
1420 goto finally;
1421
1422 if (!using_setitems) {
1423 if ((*self->write_func)(self, &setitem, 1) < 0)
1424 goto finally;
1425 }
1426 }
1427
1428 if (using_setitems) {
1429 if ((*self->write_func)(self, &setitems, 1) < 0)
1430 goto finally;
1431 }
1432
1433 res = 0;
1434
1435finally:
1436
1437 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001438}
1439
1440
Guido van Rossum60456fd1997-04-09 17:36:32 +00001441static int
1442save_inst(Picklerobject *self, PyObject *args) {
1443 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1444 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1445 char *module_str, *name_str;
1446 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001447
Guido van Rossum60456fd1997-04-09 17:36:32 +00001448 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001449
Guido van Rossum60456fd1997-04-09 17:36:32 +00001450 if ((*self->write_func)(self, &MARKv, 1) < 0)
1451 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001452
Guido van Rossum053b8df1998-11-25 16:18:00 +00001453 UNLESS (class = PyObject_GetAttr(args, __class___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001454 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001455
Guido van Rossum60456fd1997-04-09 17:36:32 +00001456 if (self->bin) {
1457 if (save(self, class, 0) < 0)
1458 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001459 }
1460
Guido van Rossum142eeb81997-08-13 03:14:41 +00001461 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001462 PyObject *element = 0;
1463 int i, len;
1464
Guido van Rossum053b8df1998-11-25 16:18:00 +00001465 UNLESS (class_args =
Guido van Rossum60456fd1997-04-09 17:36:32 +00001466 PyObject_CallObject(getinitargs_func, empty_tuple))
1467 goto finally;
1468
1469 if ((len = PyObject_Length(class_args)) < 0)
1470 goto finally;
1471
1472 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001473 UNLESS (element = PySequence_GetItem(class_args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001474 goto finally;
1475
1476 if (save(self, element, 0) < 0) {
1477 Py_DECREF(element);
1478 goto finally;
1479 }
1480
1481 Py_DECREF(element);
1482 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001483 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001484 else {
1485 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001486 }
1487
Guido van Rossum60456fd1997-04-09 17:36:32 +00001488 if (!self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001489 UNLESS (name = ((PyClassObject *)class)->cl_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001490 PyErr_SetString(PicklingError, "class has no name");
1491 goto finally;
1492 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001493
Guido van Rossum053b8df1998-11-25 16:18:00 +00001494 UNLESS (module = whichmodule(class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001495 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001496
1497
1498 if ((module_size = PyString_Size(module)) < 0 ||
1499 (name_size = PyString_Size(name)) < 0)
1500 goto finally;
1501
Guido van Rossum60456fd1997-04-09 17:36:32 +00001502 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001503 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001504
Guido van Rossum60456fd1997-04-09 17:36:32 +00001505 if ((*self->write_func)(self, &inst, 1) < 0)
1506 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001507
Guido van Rossum60456fd1997-04-09 17:36:32 +00001508 if ((*self->write_func)(self, module_str, module_size) < 0)
1509 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001510
Guido van Rossum60456fd1997-04-09 17:36:32 +00001511 if ((*self->write_func)(self, "\n", 1) < 0)
1512 goto finally;
1513
1514 if ((*self->write_func)(self, name_str, name_size) < 0)
1515 goto finally;
1516
1517 if ((*self->write_func)(self, "\n", 1) < 0)
1518 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001519 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001520 else if ((*self->write_func)(self, &obj, 1) < 0) {
1521 goto finally;
1522 }
1523
Guido van Rossum142eeb81997-08-13 03:14:41 +00001524 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001525 UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001526 goto finally;
1527 }
1528 else {
1529 PyErr_Clear();
1530
Guido van Rossum053b8df1998-11-25 16:18:00 +00001531 UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001532 PyErr_Clear();
1533 res = 0;
1534 goto finally;
1535 }
1536 }
1537
1538 if (!PyDict_Check(state)) {
1539 if (put2(self, args) < 0)
1540 goto finally;
1541 }
1542 else {
1543 if (put(self, args) < 0)
1544 goto finally;
1545 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001546
Guido van Rossum60456fd1997-04-09 17:36:32 +00001547 if (save(self, state, 0) < 0)
1548 goto finally;
1549
1550 if ((*self->write_func)(self, &build, 1) < 0)
1551 goto finally;
1552
1553 res = 0;
1554
1555finally:
1556 Py_XDECREF(module);
1557 Py_XDECREF(class);
1558 Py_XDECREF(state);
1559 Py_XDECREF(getinitargs_func);
1560 Py_XDECREF(getstate_func);
1561 Py_XDECREF(class_args);
1562
1563 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001564}
1565
1566
Guido van Rossum60456fd1997-04-09 17:36:32 +00001567static int
1568save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1569 PyObject *global_name = 0, *module = 0;
1570 char *name_str, *module_str;
1571 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001572
Guido van Rossum60456fd1997-04-09 17:36:32 +00001573 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001574
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001575 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001576 global_name = name;
1577 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001578 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001579 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001580 UNLESS (global_name = PyObject_GetAttr(args, __name___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001581 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001582 }
1583
Guido van Rossum053b8df1998-11-25 16:18:00 +00001584 UNLESS (module = whichmodule(args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001585 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001586
Guido van Rossum053b8df1998-11-25 16:18:00 +00001587 if ((module_size = PyString_Size(module)) < 0 ||
1588 (name_size = PyString_Size(global_name)) < 0)
1589 goto finally;
1590
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001591 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001592 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001593
Guido van Rossum60456fd1997-04-09 17:36:32 +00001594 if ((*self->write_func)(self, &global, 1) < 0)
1595 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001596
Guido van Rossum60456fd1997-04-09 17:36:32 +00001597 if ((*self->write_func)(self, module_str, module_size) < 0)
1598 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001599
Guido van Rossum60456fd1997-04-09 17:36:32 +00001600 if ((*self->write_func)(self, "\n", 1) < 0)
1601 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001602
Guido van Rossum60456fd1997-04-09 17:36:32 +00001603 if ((*self->write_func)(self, name_str, name_size) < 0)
1604 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001605
Guido van Rossum60456fd1997-04-09 17:36:32 +00001606 if ((*self->write_func)(self, "\n", 1) < 0)
1607 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001608
Guido van Rossum60456fd1997-04-09 17:36:32 +00001609 if (put(self, args) < 0)
1610 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001611
Guido van Rossum60456fd1997-04-09 17:36:32 +00001612 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001613
Guido van Rossum60456fd1997-04-09 17:36:32 +00001614finally:
1615 Py_XDECREF(module);
1616 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001617
Guido van Rossum60456fd1997-04-09 17:36:32 +00001618 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001619}
1620
Guido van Rossum60456fd1997-04-09 17:36:32 +00001621static int
1622save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1623 PyObject *pid = 0;
1624 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001625
Guido van Rossum60456fd1997-04-09 17:36:32 +00001626 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001627
Guido van Rossum053b8df1998-11-25 16:18:00 +00001628 Py_INCREF(args);
1629 ARG_TUP(self, args);
1630 if (self->arg) {
1631 pid = PyObject_CallObject(f, self->arg);
1632 FREE_ARG_TUP(self);
1633 }
1634 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001635
Guido van Rossum60456fd1997-04-09 17:36:32 +00001636 if (pid != Py_None) {
1637 if (!self->bin) {
1638 if (!PyString_Check(pid)) {
1639 PyErr_SetString(PicklingError,
1640 "persistent id must be string");
1641 goto finally;
1642 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001643
Guido van Rossum60456fd1997-04-09 17:36:32 +00001644 if ((*self->write_func)(self, &persid, 1) < 0)
1645 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001646
Guido van Rossum60456fd1997-04-09 17:36:32 +00001647 if ((size = PyString_Size(pid)) < 0)
1648 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001649
Guido van Rossum60456fd1997-04-09 17:36:32 +00001650 if ((*self->write_func)(self,
1651 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1652 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001653
Guido van Rossum60456fd1997-04-09 17:36:32 +00001654 if ((*self->write_func)(self, "\n", 1) < 0)
1655 goto finally;
1656
1657 res = 1;
1658 goto finally;
1659 }
1660 else if (save(self, pid, 1) >= 0) {
1661 if ((*self->write_func)(self, &binpersid, 1) < 0)
1662 res = -1;
1663 else
1664 res = 1;
1665 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001666
Guido van Rossum60456fd1997-04-09 17:36:32 +00001667 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001668 }
1669
Guido van Rossum60456fd1997-04-09 17:36:32 +00001670 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001671
Guido van Rossum60456fd1997-04-09 17:36:32 +00001672finally:
1673 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001674
Guido van Rossum60456fd1997-04-09 17:36:32 +00001675 return res;
1676}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001677
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001678
Guido van Rossum60456fd1997-04-09 17:36:32 +00001679static int
1680save_reduce(Picklerobject *self, PyObject *callable,
1681 PyObject *tup, PyObject *state, PyObject *ob) {
1682 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001683
Guido van Rossum60456fd1997-04-09 17:36:32 +00001684 if (save(self, callable, 0) < 0)
1685 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001686
Guido van Rossum60456fd1997-04-09 17:36:32 +00001687 if (save(self, tup, 0) < 0)
1688 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001689
Guido van Rossum60456fd1997-04-09 17:36:32 +00001690 if ((*self->write_func)(self, &reduce, 1) < 0)
1691 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001692
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001693 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001694 if (state && !PyDict_Check(state)) {
1695 if (put2(self, ob) < 0)
1696 return -1;
1697 }
1698 else {
1699 if (put(self, ob) < 0)
1700 return -1;
1701 }
1702 }
1703
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001704 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001705 if (save(self, state, 0) < 0)
1706 return -1;
1707
1708 if ((*self->write_func)(self, &build, 1) < 0)
1709 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001710 }
1711
Guido van Rossum60456fd1997-04-09 17:36:32 +00001712 return 0;
1713}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001714
Guido van Rossum60456fd1997-04-09 17:36:32 +00001715static int
1716save(Picklerobject *self, PyObject *args, int pers_save) {
1717 PyTypeObject *type;
1718 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001719 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001720 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001721
Guido van Rossum60456fd1997-04-09 17:36:32 +00001722 if (!pers_save && self->pers_func) {
1723 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1724 res = tmp;
1725 goto finally;
1726 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001727 }
1728
Guido van Rossum60456fd1997-04-09 17:36:32 +00001729 if (args == Py_None) {
1730 res = save_none(self, args);
1731 goto finally;
1732 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001733
Guido van Rossum60456fd1997-04-09 17:36:32 +00001734 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001735
Guido van Rossum60456fd1997-04-09 17:36:32 +00001736 switch (type->tp_name[0]) {
1737 case 'i':
1738 if (type == &PyInt_Type) {
1739 res = save_int(self, args);
1740 goto finally;
1741 }
1742 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001743
Guido van Rossum60456fd1997-04-09 17:36:32 +00001744 case 'l':
1745 if (type == &PyLong_Type) {
1746 res = save_long(self, args);
1747 goto finally;
1748 }
1749 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001750
Guido van Rossum60456fd1997-04-09 17:36:32 +00001751 case 'f':
1752 if (type == &PyFloat_Type) {
1753 res = save_float(self, args);
1754 goto finally;
1755 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001756 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001757
Guido van Rossum60456fd1997-04-09 17:36:32 +00001758 case 't':
1759 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001760 if (self->bin) res = save_empty_tuple(self, args);
1761 else res = save_tuple(self, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001762 goto finally;
1763 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001764 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001765
Guido van Rossum60456fd1997-04-09 17:36:32 +00001766 case 's':
Guido van Rossum053b8df1998-11-25 16:18:00 +00001767 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001768 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001769 goto finally;
1770 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001771
1772 case 'u':
1773 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
1774 res = save_unicode(self, args, 0);
1775 goto finally;
1776 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001777 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001778
Guido van Rossum60456fd1997-04-09 17:36:32 +00001779 if (args->ob_refcnt > 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001780 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001781
Guido van Rossum534b7c52000-06-28 22:23:56 +00001782 UNLESS (py_ob_id = PyLong_FromVoidPtr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001783 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001784
Guido van Rossum60456fd1997-04-09 17:36:32 +00001785 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1786 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001787
Guido van Rossum60456fd1997-04-09 17:36:32 +00001788 if (has_key) {
1789 if (get(self, py_ob_id) < 0)
1790 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001791
Guido van Rossum60456fd1997-04-09 17:36:32 +00001792 res = 0;
1793 goto finally;
1794 }
1795 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001796
Guido van Rossum60456fd1997-04-09 17:36:32 +00001797 switch (type->tp_name[0]) {
1798 case 's':
1799 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001800 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001801 goto finally;
1802 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001803 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001804
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001805 case 'u':
1806 if (type == &PyUnicode_Type) {
1807 res = save_unicode(self, args, 1);
1808 goto finally;
1809 }
1810 break;
1811
Guido van Rossum60456fd1997-04-09 17:36:32 +00001812 case 't':
1813 if (type == &PyTuple_Type) {
1814 res = save_tuple(self, args);
1815 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001816 }
1817 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001818
Guido van Rossum60456fd1997-04-09 17:36:32 +00001819 case 'l':
1820 if (type == &PyList_Type) {
1821 res = save_list(self, args);
1822 goto finally;
1823 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001824 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001825
1826 case 'd':
1827 if (type == &PyDict_Type) {
1828 res = save_dict(self, args);
1829 goto finally;
1830 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001831 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001832
1833 case 'i':
1834 if (type == &PyInstance_Type) {
1835 res = save_inst(self, args);
1836 goto finally;
1837 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001838 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001839
1840 case 'c':
1841 if (type == &PyClass_Type) {
1842 res = save_global(self, args, NULL);
1843 goto finally;
1844 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001845 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001846
1847 case 'f':
1848 if (type == &PyFunction_Type) {
1849 res = save_global(self, args, NULL);
1850 goto finally;
1851 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001852 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001853
1854 case 'b':
1855 if (type == &PyCFunction_Type) {
1856 res = save_global(self, args, NULL);
1857 goto finally;
1858 }
1859 }
1860
1861 if (!pers_save && self->inst_pers_func) {
1862 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1863 res = tmp;
1864 goto finally;
1865 }
1866 }
1867
Guido van Rossum142eeb81997-08-13 03:14:41 +00001868 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001869 Py_INCREF(__reduce__);
1870
Guido van Rossum60456fd1997-04-09 17:36:32 +00001871 Py_INCREF(args);
Guido van Rossum053b8df1998-11-25 16:18:00 +00001872 ARG_TUP(self, args);
1873 if (self->arg) {
1874 t = PyObject_CallObject(__reduce__, self->arg);
1875 FREE_ARG_TUP(self);
1876 }
1877 if (! t) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001878 }
1879 else {
1880 PyErr_Clear();
1881
Guido van Rossum142eeb81997-08-13 03:14:41 +00001882 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001883 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001884 goto finally;
1885 }
1886 else {
1887 PyErr_Clear();
1888 }
1889 }
1890
1891 if (t) {
1892 if (PyString_Check(t)) {
1893 res = save_global(self, args, t);
1894 goto finally;
1895 }
1896
1897 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001898 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001899 "be a tuple", "O", __reduce__);
1900 goto finally;
1901 }
1902
1903 size = PyTuple_Size(t);
1904
1905 if ((size != 3) && (size != 2)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001906 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001907 "contain only two or three elements", "O", __reduce__);
1908 goto finally;
1909 }
1910
1911 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001912
Guido van Rossum60456fd1997-04-09 17:36:32 +00001913 arg_tup = PyTuple_GET_ITEM(t, 1);
1914
1915 if (size > 2) {
1916 state = PyTuple_GET_ITEM(t, 2);
1917 }
1918
Guido van Rossum053b8df1998-11-25 16:18:00 +00001919 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001920 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001921 "returned by %s must be a tuple", "O", __reduce__);
1922 goto finally;
1923 }
1924
1925 res = save_reduce(self, callable, arg_tup, state, args);
1926 goto finally;
1927 }
1928
Guido van Rossumc03158b1999-06-09 15:23:31 +00001929 PyErr_SetObject(UnpickleableError, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001930
1931finally:
1932 Py_XDECREF(py_ob_id);
1933 Py_XDECREF(__reduce__);
1934 Py_XDECREF(t);
1935
1936 return res;
1937}
1938
1939
1940static int
1941dump(Picklerobject *self, PyObject *args) {
1942 static char stop = STOP;
1943
1944 if (save(self, args, 0) < 0)
1945 return -1;
1946
1947 if ((*self->write_func)(self, &stop, 1) < 0)
1948 return -1;
1949
1950 if ((*self->write_func)(self, NULL, 0) < 0)
1951 return -1;
1952
1953 return 0;
1954}
1955
1956static PyObject *
Guido van Rossum053b8df1998-11-25 16:18:00 +00001957Pickle_clear_memo(Picklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00001958 if (args && ! PyArg_ParseTuple(args,":clear_memo")) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001959 if (self->memo) PyDict_Clear(self->memo);
1960 Py_INCREF(Py_None);
1961 return Py_None;
1962}
1963
1964static PyObject *
1965Pickle_getvalue(Picklerobject *self, PyObject *args) {
1966 int l, i, rsize, ssize, clear=1, lm;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001967 long ik;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001968 PyObject *k, *r;
1969 char *s, *p, *have_get;
1970 Pdata *data;
1971
Guido van Rossum43713e52000-02-29 13:59:29 +00001972 if (args && ! PyArg_ParseTuple(args,"|i:getvalue",&clear)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001973
1974 /* Check to make sure we are based on a list */
1975 if (! Pdata_Check(self->file)) {
1976 PyErr_SetString(PicklingError,
1977 "Attempt to getvalue a non-list-based pickler");
1978 return NULL;
1979 }
1980
1981 /* flush write buffer */
1982 if (write_other(self, NULL, 0) < 0) return NULL;
1983
1984 data=(Pdata*)self->file;
1985 l=data->length;
1986
1987 /* set up an array to hold get/put status */
1988 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
1989 lm++;
1990 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
1991 memset(have_get,0,lm);
1992
1993 /* Scan for gets. */
1994 for (rsize=0, i=l; --i >= 0; ) {
1995 k=data->data[i];
1996
1997 if (PyString_Check(k)) {
1998 rsize += PyString_GET_SIZE(k);
1999 }
2000
2001 else if (PyInt_Check(k)) { /* put */
2002 ik=PyInt_AS_LONG((PyIntObject*)k);
2003 if (ik >= lm || ik==0) {
2004 PyErr_SetString(PicklingError,
2005 "Invalid get data");
2006 return NULL;
2007 }
2008 if (have_get[ik]) { /* with matching get */
2009 if (ik < 256) rsize += 2;
2010 else rsize+=5;
2011 }
2012 }
2013
2014 else if (! (PyTuple_Check(k) &&
2015 PyTuple_GET_SIZE(k) == 2 &&
2016 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
2017 ) {
2018 PyErr_SetString(PicklingError,
2019 "Unexpected data in internal list");
2020 return NULL;
2021 }
2022
2023 else { /* put */
2024 ik=PyInt_AS_LONG((PyIntObject*)k);
2025 if (ik >= lm || ik==0) {
2026 PyErr_SetString(PicklingError,
2027 "Invalid get data");
2028 return NULL;
2029 }
2030 have_get[ik]=1;
2031 if (ik < 256) rsize += 2;
2032 else rsize+=5;
2033 }
2034
2035 }
2036
2037 /* Now generate the result */
2038 UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
2039 s=PyString_AS_STRING((PyStringObject*)r);
2040
2041 for (i=0; i<l; i++) {
2042 k=data->data[i];
2043
2044 if (PyString_Check(k)) {
2045 ssize=PyString_GET_SIZE(k);
2046 if (ssize) {
2047 p=PyString_AS_STRING((PyStringObject*)k);
2048 while (--ssize >= 0) *s++=*p++;
2049 }
2050 }
2051
2052 else if (PyTuple_Check(k)) { /* get */
2053 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
2054 if (ik < 256) {
2055 *s++ = BINGET;
2056 *s++ = (int)(ik & 0xff);
2057 }
2058 else {
2059 *s++ = LONG_BINGET;
2060 *s++ = (int)(ik & 0xff);
2061 *s++ = (int)((ik >> 8) & 0xff);
2062 *s++ = (int)((ik >> 16) & 0xff);
2063 *s++ = (int)((ik >> 24) & 0xff);
2064 }
2065 }
2066
2067 else { /* put */
2068 ik=PyInt_AS_LONG((PyIntObject*)k);
2069
2070 if (have_get[ik]) { /* with matching get */
2071 if (ik < 256) {
2072 *s++ = BINPUT;
2073 *s++ = (int)(ik & 0xff);
2074 }
2075 else {
2076 *s++ = LONG_BINPUT;
2077 *s++ = (int)(ik & 0xff);
2078 *s++ = (int)((ik >> 8) & 0xff);
2079 *s++ = (int)((ik >> 16) & 0xff);
2080 *s++ = (int)((ik >> 24) & 0xff);
2081 }
2082 }
2083 }
2084
2085 }
2086
2087 if (clear) {
2088 PyDict_Clear(self->memo);
2089 Pdata_clear(data,0);
2090 }
2091
2092 free(have_get);
2093 return r;
2094err:
2095 free(have_get);
2096 return NULL;
2097}
2098
2099static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002100Pickler_dump(Picklerobject *self, PyObject *args) {
2101 PyObject *ob;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002102 int get=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002103
Guido van Rossum43713e52000-02-29 13:59:29 +00002104 UNLESS (PyArg_ParseTuple(args, "O|i:dump", &ob, &get))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002105 return NULL;
2106
2107 if (dump(self, ob) < 0)
2108 return NULL;
2109
Guido van Rossum053b8df1998-11-25 16:18:00 +00002110 if (get) return Pickle_getvalue(self, NULL);
2111
2112 Py_INCREF(self);
2113 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002114}
2115
2116
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002117static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00002118 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002119 "dump(object) --"
2120 "Write an object in pickle format to the object's pickle stream\n"
2121 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00002122 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002123 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum053b8df1998-11-25 16:18:00 +00002124 {"getvalue", (PyCFunction)Pickle_getvalue, 1,
2125 "getvalue() -- Finish picking a list-based pickle"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002126 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002127};
2128
2129
2130static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002131newPicklerobject(PyObject *file, int bin) {
2132 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002133
Guido van Rossumb18618d2000-05-03 23:44:39 +00002134 UNLESS (self = PyObject_New(Picklerobject, &Picklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002135 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002136
2137 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002138 self->write = NULL;
2139 self->memo = NULL;
2140 self->arg = NULL;
2141 self->pers_func = NULL;
2142 self->inst_pers_func = NULL;
2143 self->write_buf = NULL;
2144 self->bin = bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002145 self->fast = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002146 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002147 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002148
Guido van Rossum053b8df1998-11-25 16:18:00 +00002149 if (file)
2150 Py_INCREF(file);
2151 else
Guido van Rossum50f385c1998-12-04 18:48:44 +00002152 file=Pdata_New();
Guido van Rossum83addc72000-04-21 20:49:36 +00002153
2154 UNLESS (self->file = file)
2155 goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002156
Guido van Rossum83addc72000-04-21 20:49:36 +00002157 UNLESS (self->memo = PyDict_New())
2158 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002159
Guido van Rossum60456fd1997-04-09 17:36:32 +00002160 if (PyFile_Check(file)) {
2161 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002162 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00002163 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
2164 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002165 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002166 self->write_func = write_file;
2167 }
2168 else if (PycStringIO_OutputCheck(file)) {
2169 self->write_func = write_cStringIO;
2170 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002171 else if (file == Py_None) {
2172 self->write_func = write_none;
2173 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002174 else {
2175 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002176
Guido van Rossum053b8df1998-11-25 16:18:00 +00002177 if (! Pdata_Check(file)) {
2178 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002179 PyErr_Clear();
2180 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002181 "attribute");
2182 goto err;
2183 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002184 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002185
Guido van Rossum053b8df1998-11-25 16:18:00 +00002186 UNLESS (self->write_buf =
Guido van Rossum60456fd1997-04-09 17:36:32 +00002187 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
2188 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002189 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002190 }
2191 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002192
Guido van Rossum053b8df1998-11-25 16:18:00 +00002193 if (PyEval_GetRestricted()) {
2194 /* Restricted execution, get private tables */
2195 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002196
Guido van Rossum053b8df1998-11-25 16:18:00 +00002197 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2198 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2199 Py_DECREF(m);
2200 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002201 }
2202 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002203 self->dispatch_table=dispatch_table;
2204 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002205 }
2206
Guido van Rossum60456fd1997-04-09 17:36:32 +00002207 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002208
2209err:
2210 Py_DECREF((PyObject *)self);
2211 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002212}
2213
2214
2215static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002216get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002217 PyObject *file=NULL;
2218 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002219
Guido van Rossum053b8df1998-11-25 16:18:00 +00002220 bin=1;
Guido van Rossum43713e52000-02-29 13:59:29 +00002221 if (! PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002222 PyErr_Clear();
2223 bin=0;
Guido van Rossum43713e52000-02-29 13:59:29 +00002224 if (! PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002225 return NULL;
2226 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002227 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002228}
2229
2230
2231static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002232Pickler_dealloc(Picklerobject *self) {
2233 Py_XDECREF(self->write);
2234 Py_XDECREF(self->memo);
2235 Py_XDECREF(self->arg);
2236 Py_XDECREF(self->file);
2237 Py_XDECREF(self->pers_func);
2238 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002239 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002240
2241 if (self->write_buf) {
2242 free(self->write_buf);
2243 }
2244
Guido van Rossumb18618d2000-05-03 23:44:39 +00002245 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002246}
2247
2248
2249static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002250Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002251
2252 switch (*name) {
2253 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002254 if (strcmp(name, "persistent_id") == 0) {
2255 if (!self->pers_func) {
2256 PyErr_SetString(PyExc_AttributeError, name);
2257 return NULL;
2258 }
2259
2260 Py_INCREF(self->pers_func);
2261 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002262 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002263 break;
2264 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002265 if (strcmp(name, "memo") == 0) {
2266 if (!self->memo) {
2267 PyErr_SetString(PyExc_AttributeError, name);
2268 return NULL;
2269 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002270
Guido van Rossum60456fd1997-04-09 17:36:32 +00002271 Py_INCREF(self->memo);
2272 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002273 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002274 break;
2275 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002276 if (strcmp(name, "PicklingError") == 0) {
2277 Py_INCREF(PicklingError);
2278 return PicklingError;
2279 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002280 break;
2281 case 'b':
2282 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002283 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002284 break;
2285 case 'f':
2286 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002287 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002288 break;
2289 case 'g':
2290 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2291 PyErr_SetString(PyExc_AttributeError, name);
2292 return NULL;
2293 }
2294 break;
2295 }
2296 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002297}
2298
2299
2300int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002301Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002302
Guido van Rossum053b8df1998-11-25 16:18:00 +00002303 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002304 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002305 "attribute deletion is not supported");
2306 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002307 }
2308
Guido van Rossum60456fd1997-04-09 17:36:32 +00002309 if (strcmp(name, "persistent_id") == 0) {
2310 Py_XDECREF(self->pers_func);
2311 self->pers_func = value;
2312 Py_INCREF(value);
2313 return 0;
2314 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002315
Guido van Rossum60456fd1997-04-09 17:36:32 +00002316 if (strcmp(name, "inst_persistent_id") == 0) {
2317 Py_XDECREF(self->inst_pers_func);
2318 self->inst_pers_func = value;
2319 Py_INCREF(value);
2320 return 0;
2321 }
2322
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002323 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002324 if (! PyDict_Check(value)) {
2325 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2326 return -1;
2327 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002328 Py_XDECREF(self->memo);
2329 self->memo = value;
2330 Py_INCREF(value);
2331 return 0;
2332 }
2333
Guido van Rossum053b8df1998-11-25 16:18:00 +00002334 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002335 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002336 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002337 }
2338
Guido van Rossum053b8df1998-11-25 16:18:00 +00002339 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002340 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002341 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002342 }
2343
Guido van Rossum60456fd1997-04-09 17:36:32 +00002344 PyErr_SetString(PyExc_AttributeError, name);
2345 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002346}
2347
2348
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002349static char Picklertype__doc__[] =
2350"Objects that know how to pickle objects\n"
2351;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002352
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002353static PyTypeObject Picklertype = {
2354 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002355 0, /*ob_size*/
2356 "Pickler", /*tp_name*/
2357 sizeof(Picklerobject), /*tp_basicsize*/
2358 0, /*tp_itemsize*/
2359 /* methods */
2360 (destructor)Pickler_dealloc, /*tp_dealloc*/
2361 (printfunc)0, /*tp_print*/
2362 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2363 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2364 (cmpfunc)0, /*tp_compare*/
2365 (reprfunc)0, /*tp_repr*/
2366 0, /*tp_as_number*/
2367 0, /*tp_as_sequence*/
2368 0, /*tp_as_mapping*/
2369 (hashfunc)0, /*tp_hash*/
2370 (ternaryfunc)0, /*tp_call*/
2371 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002372
Guido van Rossum60456fd1997-04-09 17:36:32 +00002373 /* Space for future expansion */
2374 0L,0L,0L,0L,
2375 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002376};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002377
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002378static PyObject *
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002379find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002380 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002381
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002382 if (fc) {
2383 if (fc==Py_None) {
2384 PyErr_SetString(UnpicklingError,
2385 "Global and instance pickles are not supported.");
2386 return NULL;
2387 }
2388 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2389 }
2390
Jeremy Hyltond1055231998-08-11 19:52:51 +00002391 module = PySys_GetObject("modules");
2392 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002393 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002394
2395 module = PyDict_GetItem(module, py_module_name);
2396 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002397 module = PyImport_Import(py_module_name);
2398 if (!module)
2399 return NULL;
2400 global = PyObject_GetAttr(module, py_global_name);
2401 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002402 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002403 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002404 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002405 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002406 char buf[256 + 37];
2407 sprintf(buf, "Failed to import class %.128s from module %.128s",
2408 PyString_AS_STRING((PyStringObject*)py_global_name),
2409 PyString_AS_STRING((PyStringObject*)py_module_name));
2410 PyErr_SetString(PyExc_SystemError, buf);
2411 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002412 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002413 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002414}
2415
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002416static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002417marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002418 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002419 PyErr_SetString(UnpicklingError, "could not find MARK");
2420 return -1;
2421 }
2422
2423 return self->marks[--self->num_marks];
2424}
2425
2426
2427static int
2428load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002429 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002430 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002431}
2432
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002433static int
2434bad_readline() {
2435 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2436 return -1;
2437}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002438
2439static int
2440load_int(Unpicklerobject *self) {
2441 PyObject *py_int = 0;
2442 char *endptr, *s;
2443 int len, res = -1;
2444 long l;
2445
2446 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002447 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002448 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002449
2450 errno = 0;
2451 l = strtol(s, &endptr, 0);
2452
2453 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2454 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002455 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002456 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002457 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002458
Guido van Rossum053b8df1998-11-25 16:18:00 +00002459 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2460 PyErr_SetString(PyExc_ValueError,
2461 "could not convert string to int");
2462 goto finally;
2463 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002464 }
2465 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002466 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002467 }
2468
Guido van Rossum053b8df1998-11-25 16:18:00 +00002469 free(s);
2470 PDATA_PUSH(self->stack, py_int, -1);
2471 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002472
2473finally:
2474 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002475
2476 return res;
2477}
2478
2479
2480static long
2481calc_binint(char *s, int x) {
2482 unsigned char c;
2483 int i;
2484 long l;
2485
2486 for (i = 0, l = 0L; i < x; i++) {
2487 c = (unsigned char)s[i];
2488 l |= (long)c << (i * 8);
2489 }
2490
2491 return l;
2492}
2493
2494
2495static int
2496load_binintx(Unpicklerobject *self, char *s, int x) {
2497 PyObject *py_int = 0;
2498 long l;
2499
2500 l = calc_binint(s, x);
2501
Guido van Rossum053b8df1998-11-25 16:18:00 +00002502 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002503 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002504
Guido van Rossum053b8df1998-11-25 16:18:00 +00002505 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002506 return 0;
2507}
2508
2509
2510static int
2511load_binint(Unpicklerobject *self) {
2512 char *s;
2513
2514 if ((*self->read_func)(self, &s, 4) < 0)
2515 return -1;
2516
2517 return load_binintx(self, s, 4);
2518}
2519
2520
2521static int
2522load_binint1(Unpicklerobject *self) {
2523 char *s;
2524
2525 if ((*self->read_func)(self, &s, 1) < 0)
2526 return -1;
2527
2528 return load_binintx(self, s, 1);
2529}
2530
2531
2532static int
2533load_binint2(Unpicklerobject *self) {
2534 char *s;
2535
2536 if ((*self->read_func)(self, &s, 2) < 0)
2537 return -1;
2538
2539 return load_binintx(self, s, 2);
2540}
2541
2542static int
2543load_long(Unpicklerobject *self) {
2544 PyObject *l = 0;
2545 char *end, *s;
2546 int len, res = -1;
2547
Guido van Rossum60456fd1997-04-09 17:36:32 +00002548 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002549 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002550 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002551
Guido van Rossum053b8df1998-11-25 16:18:00 +00002552 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002553 goto finally;
2554
Guido van Rossum053b8df1998-11-25 16:18:00 +00002555 free(s);
2556 PDATA_PUSH(self->stack, l, -1);
2557 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002558
2559finally:
2560 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002561
2562 return res;
2563}
2564
2565
2566static int
2567load_float(Unpicklerobject *self) {
2568 PyObject *py_float = 0;
2569 char *endptr, *s;
2570 int len, res = -1;
2571 double d;
2572
2573 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002574 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002575 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002576
2577 errno = 0;
2578 d = strtod(s, &endptr);
2579
2580 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2581 PyErr_SetString(PyExc_ValueError,
2582 "could not convert string to float");
2583 goto finally;
2584 }
2585
Guido van Rossum053b8df1998-11-25 16:18:00 +00002586 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002587 goto finally;
2588
Guido van Rossum053b8df1998-11-25 16:18:00 +00002589 free(s);
2590 PDATA_PUSH(self->stack, py_float, -1);
2591 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002592
2593finally:
2594 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002595
2596 return res;
2597}
2598
Guido van Rossum60456fd1997-04-09 17:36:32 +00002599static int
2600load_binfloat(Unpicklerobject *self) {
2601 PyObject *py_float = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002602 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002603 long fhi, flo;
2604 double x;
2605 char *p;
2606
2607 if ((*self->read_func)(self, &p, 8) < 0)
2608 return -1;
2609
2610 /* First byte */
2611 s = (*p>>7) & 1;
2612 e = (*p & 0x7F) << 4;
2613 p++;
2614
2615 /* Second byte */
2616 e |= (*p>>4) & 0xF;
2617 fhi = (*p & 0xF) << 24;
2618 p++;
2619
2620 /* Third byte */
2621 fhi |= (*p & 0xFF) << 16;
2622 p++;
2623
2624 /* Fourth byte */
2625 fhi |= (*p & 0xFF) << 8;
2626 p++;
2627
2628 /* Fifth byte */
2629 fhi |= *p & 0xFF;
2630 p++;
2631
2632 /* Sixth byte */
2633 flo = (*p & 0xFF) << 16;
2634 p++;
2635
2636 /* Seventh byte */
2637 flo |= (*p & 0xFF) << 8;
2638 p++;
2639
2640 /* Eighth byte */
2641 flo |= *p & 0xFF;
2642
2643 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2644 x /= 268435456.0; /* 2**28 */
2645
2646 /* XXX This sadly ignores Inf/NaN */
2647 if (e == 0)
2648 e = -1022;
2649 else {
2650 x += 1.0;
2651 e -= 1023;
2652 }
2653 x = ldexp(x, e);
2654
2655 if (s)
2656 x = -x;
2657
Guido van Rossum053b8df1998-11-25 16:18:00 +00002658 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002659
Guido van Rossum053b8df1998-11-25 16:18:00 +00002660 PDATA_PUSH(self->stack, py_float, -1);
2661 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002662}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002663
2664static int
2665load_string(Unpicklerobject *self) {
2666 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002667 int len, res = -1, nslash;
2668 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002669
2670 static PyObject *eval_dict = 0;
2671
2672 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002673 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002674 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002675
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002676 /* Check for unquoted quotes (evil strings) */
2677 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002678 if (q != '"' && q != '\'') goto insecure;
2679 for (p=s+1, nslash=0; *p; p++) {
2680 if (*p==q && nslash%2==0) break;
2681 if (*p=='\\') nslash++;
2682 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002683 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002684 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002685 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002686 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002687 }
2688 else goto insecure;
2689 /********************************************/
2690
Guido van Rossum053b8df1998-11-25 16:18:00 +00002691 UNLESS (eval_dict)
2692 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002693 goto finally;
2694
Guido van Rossum053b8df1998-11-25 16:18:00 +00002695 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002696 goto finally;
2697
Guido van Rossum053b8df1998-11-25 16:18:00 +00002698 free(s);
2699 PDATA_PUSH(self->stack, str, -1);
2700 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002701
2702finally:
2703 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002704
2705 return res;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002706
2707insecure:
2708 free(s);
2709 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2710 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002711}
2712
2713
2714static int
2715load_binstring(Unpicklerobject *self) {
2716 PyObject *py_string = 0;
2717 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002718 char *s;
2719
Guido van Rossum053b8df1998-11-25 16:18:00 +00002720 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002721
2722 l = calc_binint(s, 4);
2723
2724 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002725 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002726
Guido van Rossum053b8df1998-11-25 16:18:00 +00002727 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2728 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002729
Guido van Rossum053b8df1998-11-25 16:18:00 +00002730 PDATA_PUSH(self->stack, py_string, -1);
2731 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002732}
2733
2734
2735static int
2736load_short_binstring(Unpicklerobject *self) {
2737 PyObject *py_string = 0;
2738 unsigned char l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002739 char *s;
2740
2741 if ((*self->read_func)(self, &s, 1) < 0)
2742 return -1;
2743
2744 l = (unsigned char)s[0];
2745
Guido van Rossum053b8df1998-11-25 16:18:00 +00002746 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002747
Guido van Rossum053b8df1998-11-25 16:18:00 +00002748 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002749
Guido van Rossum053b8df1998-11-25 16:18:00 +00002750 PDATA_PUSH(self->stack, py_string, -1);
2751 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002752}
2753
2754
2755static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002756load_unicode(Unpicklerobject *self) {
2757 PyObject *str = 0;
2758 int len, res = -1;
2759 char *s;
2760
2761 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2762 if (len < 2) return bad_readline();
2763
2764 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
2765 goto finally;
2766
2767 PDATA_PUSH(self->stack, str, -1);
2768 return 0;
2769
2770finally:
2771 return res;
2772}
2773
2774
2775static int
2776load_binunicode(Unpicklerobject *self) {
2777 PyObject *unicode;
2778 long l;
2779 char *s;
2780
2781 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2782
2783 l = calc_binint(s, 4);
2784
2785 if ((*self->read_func)(self, &s, l) < 0)
2786 return -1;
2787
2788 UNLESS (unicode = PyUnicode_DecodeUTF8(s, l, NULL))
2789 return -1;
2790
2791 PDATA_PUSH(self->stack, unicode, -1);
2792 return 0;
2793}
2794
2795
2796static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002797load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002798 PyObject *tup;
2799 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002800
Guido van Rossum053b8df1998-11-25 16:18:00 +00002801 if ((i = marker(self)) < 0) return -1;
2802 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2803 PDATA_PUSH(self->stack, tup, -1);
2804 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002805}
2806
2807static int
2808load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002809 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002810
Guido van Rossum053b8df1998-11-25 16:18:00 +00002811 UNLESS (tup=PyTuple_New(0)) return -1;
2812 PDATA_PUSH(self->stack, tup, -1);
2813 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002814}
2815
2816static int
2817load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002818 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002819
Guido van Rossum053b8df1998-11-25 16:18:00 +00002820 UNLESS (list=PyList_New(0)) return -1;
2821 PDATA_PUSH(self->stack, list, -1);
2822 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002823}
2824
2825static int
2826load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002827 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002828
Guido van Rossum053b8df1998-11-25 16:18:00 +00002829 UNLESS (dict=PyDict_New()) return -1;
2830 PDATA_PUSH(self->stack, dict, -1);
2831 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002832}
2833
2834
2835static int
2836load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002837 PyObject *list = 0;
2838 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002839
Guido van Rossum053b8df1998-11-25 16:18:00 +00002840 if ((i = marker(self)) < 0) return -1;
2841 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2842 PDATA_PUSH(self->stack, list, -1);
2843 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002844}
2845
2846static int
2847load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002848 PyObject *dict, *key, *value;
2849 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002850
Guido van Rossum053b8df1998-11-25 16:18:00 +00002851 if ((i = marker(self)) < 0) return -1;
2852 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002853
Guido van Rossum053b8df1998-11-25 16:18:00 +00002854 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002855
Guido van Rossum053b8df1998-11-25 16:18:00 +00002856 for (k = i+1; k < j; k += 2) {
2857 key =self->stack->data[k-1];
2858 value=self->stack->data[k ];
2859 if (PyDict_SetItem(dict, key, value) < 0) {
2860 Py_DECREF(dict);
2861 return -1;
2862 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002863 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002864 Pdata_clear(self->stack, i);
2865 PDATA_PUSH(self->stack, dict, -1);
2866 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002867}
2868
2869static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002870Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002871 int has_key;
2872 PyObject *safe=0, *r=0;
2873
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002874 if (PyClass_Check(cls)) {
2875 int l;
2876
Guido van Rossum053b8df1998-11-25 16:18:00 +00002877 if ((l=PyObject_Length(args)) < 0) goto err;
2878 UNLESS (l) {
2879 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002880
Guido van Rossum053b8df1998-11-25 16:18:00 +00002881 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2882 /* We have a class with no __getinitargs__, so bypass usual
2883 construction */
2884 PyInstanceObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002885
Guido van Rossum053b8df1998-11-25 16:18:00 +00002886 PyErr_Clear();
Guido van Rossumb18618d2000-05-03 23:44:39 +00002887 UNLESS (inst=PyObject_New(PyInstanceObject, &PyInstance_Type))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002888 goto err;
2889 inst->in_class=(PyClassObject*)cls;
2890 Py_INCREF(cls);
2891 UNLESS (inst->in_dict=PyDict_New()) {
2892 Py_DECREF(inst);
2893 goto err;
2894 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002895
Guido van Rossum053b8df1998-11-25 16:18:00 +00002896 return (PyObject *)inst;
2897 }
2898 Py_DECREF(__getinitargs__);
2899 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002900
Guido van Rossum053b8df1998-11-25 16:18:00 +00002901 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002902 else goto err;
2903 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002904
2905
2906 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2907 goto err;
2908
2909 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002910 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002911 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002912 cPickle_ErrFormat(UnpicklingError,
2913 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002914 Py_XDECREF(safe);
2915 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002916 }
2917
Guido van Rossum053b8df1998-11-25 16:18:00 +00002918 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002919 /* Special case, call cls.__basicnew__() */
2920 PyObject *basicnew;
2921
Guido van Rossum053b8df1998-11-25 16:18:00 +00002922 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002923 r=PyObject_CallObject(basicnew, NULL);
2924 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002925 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002926 }
2927
Guido van Rossum053b8df1998-11-25 16:18:00 +00002928 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002929
Guido van Rossum60456fd1997-04-09 17:36:32 +00002930err:
2931 {
2932 PyObject *tp, *v, *tb;
2933
2934 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002935 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2936 Py_XDECREF(v);
2937 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002938 }
2939 PyErr_Restore(tp,v,tb);
2940 }
2941 return NULL;
2942}
2943
2944
2945static int
2946load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002947 PyObject *class, *tup, *obj=0;
2948 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002949
Guido van Rossum053b8df1998-11-25 16:18:00 +00002950 if ((i = marker(self)) < 0) return -1;
2951 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
2952 PDATA_POP(self->stack, class);
2953 if (class) {
2954 obj = Instance_New(class, tup);
2955 Py_DECREF(class);
2956 }
2957 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002958
Guido van Rossum053b8df1998-11-25 16:18:00 +00002959 if (! obj) return -1;
2960 PDATA_PUSH(self->stack, obj, -1);
2961 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002962}
2963
2964
2965static int
2966load_inst(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002967 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002968 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002969 char *s;
2970
Guido van Rossum053b8df1998-11-25 16:18:00 +00002971 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002972
Guido van Rossum053b8df1998-11-25 16:18:00 +00002973 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002974 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002975 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002976
Guido van Rossum053b8df1998-11-25 16:18:00 +00002977 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002978 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002979 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002980 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002981 Py_DECREF(class_name);
2982 }
2983 }
2984 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002985
Guido van Rossum053b8df1998-11-25 16:18:00 +00002986 if (! class) return -1;
2987
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002988 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002989 obj = Instance_New(class, tup);
2990 Py_DECREF(tup);
2991 }
2992 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002993
Guido van Rossum053b8df1998-11-25 16:18:00 +00002994 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002995
Guido van Rossum053b8df1998-11-25 16:18:00 +00002996 PDATA_PUSH(self->stack, obj, -1);
2997 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002998}
2999
3000
3001static int
3002load_global(Unpicklerobject *self) {
3003 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003004 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003005 char *s;
3006
Guido van Rossum053b8df1998-11-25 16:18:00 +00003007 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003008 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003009 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003010
Guido van Rossum053b8df1998-11-25 16:18:00 +00003011 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003012 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003013 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003014 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003015 Py_DECREF(class_name);
3016 }
3017 }
3018 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003019
Guido van Rossum053b8df1998-11-25 16:18:00 +00003020 if (! class) return -1;
3021 PDATA_PUSH(self->stack, class, -1);
3022 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003023}
3024
3025
3026static int
3027load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003028 PyObject *pid = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003029 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003030 char *s;
3031
3032 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003033 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003034 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003035
Guido van Rossum053b8df1998-11-25 16:18:00 +00003036 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003037
Guido van Rossum053b8df1998-11-25 16:18:00 +00003038 if (PyList_Check(self->pers_func)) {
3039 if (PyList_Append(self->pers_func, pid) < 0) {
3040 Py_DECREF(pid);
3041 return -1;
3042 }
3043 }
3044 else {
3045 ARG_TUP(self, pid);
3046 if (self->arg) {
3047 pid = PyObject_CallObject(self->pers_func, self->arg);
3048 FREE_ARG_TUP(self);
3049 }
3050 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003051
Guido van Rossum053b8df1998-11-25 16:18:00 +00003052 if (! pid) return -1;
3053
3054 PDATA_PUSH(self->stack, pid, -1);
3055 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003056 }
3057 else {
3058 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003059 "A load persistent id instruction was encountered,\n"
3060 "but no persistent_load function was specified.");
3061 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003062 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003063}
3064
Guido van Rossum60456fd1997-04-09 17:36:32 +00003065static int
3066load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003067 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003068
3069 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003070 PDATA_POP(self->stack, pid);
3071 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003072
Guido van Rossum053b8df1998-11-25 16:18:00 +00003073 if (PyList_Check(self->pers_func)) {
3074 if (PyList_Append(self->pers_func, pid) < 0) {
3075 Py_DECREF(pid);
3076 return -1;
3077 }
3078 }
3079 else {
3080 ARG_TUP(self, pid);
3081 if (self->arg) {
3082 pid = PyObject_CallObject(self->pers_func, self->arg);
3083 FREE_ARG_TUP(self);
3084 }
3085 if (! pid) return -1;
3086 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003087
Guido van Rossum053b8df1998-11-25 16:18:00 +00003088 PDATA_PUSH(self->stack, pid, -1);
3089 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003090 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003091 else {
3092 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003093 "A load persistent id instruction was encountered,\n"
3094 "but no persistent_load function was specified.");
3095 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003096 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003097}
3098
3099
3100static int
3101load_pop(Unpicklerobject *self) {
3102 int len;
3103
Guido van Rossum053b8df1998-11-25 16:18:00 +00003104 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003105
Guido van Rossumea2b7152000-05-09 18:14:50 +00003106 /* Note that we split the (pickle.py) stack into two stacks,
3107 an object stack and a mark stack. We have to be clever and
3108 pop the right one. We do this by looking at the top of the
3109 mark stack.
3110 */
3111
Guido van Rossum60456fd1997-04-09 17:36:32 +00003112 if ((self->num_marks > 0) &&
3113 (self->marks[self->num_marks - 1] == len))
3114 self->num_marks--;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003115 else {
3116 len--;
3117 Py_DECREF(self->stack->data[len]);
3118 self->stack->length=len;
3119 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003120
3121 return 0;
3122}
3123
3124
3125static int
3126load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003127 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003128
3129 if ((i = marker(self)) < 0)
3130 return -1;
3131
Guido van Rossum053b8df1998-11-25 16:18:00 +00003132 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003133
3134 return 0;
3135}
3136
3137
3138static int
3139load_dup(Unpicklerobject *self) {
3140 PyObject *last;
3141 int len;
3142
Guido van Rossum053b8df1998-11-25 16:18:00 +00003143 if ((len = self->stack->length) <= 0) return stackUnderflow();
3144 last=self->stack->data[len-1];
3145 Py_INCREF(last);
3146 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003147 return 0;
3148}
3149
3150
3151static int
3152load_get(Unpicklerobject *self) {
3153 PyObject *py_str = 0, *value = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003154 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003155 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003156 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003157
Guido van Rossum053b8df1998-11-25 16:18:00 +00003158 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003159 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003160
Guido van Rossum053b8df1998-11-25 16:18:00 +00003161 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3162
3163 value = PyDict_GetItem(self->memo, py_str);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003164 if (! value) {
3165 PyErr_SetObject(BadPickleGet, py_str);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003166 rc = -1;
3167 } else {
3168 PDATA_APPEND(self->stack, value, -1);
3169 rc = 0;
3170 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003171
Guido van Rossum2f80d961999-07-13 15:18:58 +00003172 Py_DECREF(py_str);
3173 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003174}
3175
3176
3177static int
3178load_binget(Unpicklerobject *self) {
3179 PyObject *py_key = 0, *value = 0;
3180 unsigned char key;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003181 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003182 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003183
Guido van Rossum053b8df1998-11-25 16:18:00 +00003184 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003185
3186 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003187 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3188
3189 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003190 if (! value) {
3191 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003192 rc = -1;
3193 } else {
3194 PDATA_APPEND(self->stack, value, -1);
3195 rc = 0;
3196 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003197
Guido van Rossum2f80d961999-07-13 15:18:58 +00003198 Py_DECREF(py_key);
3199 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003200}
3201
3202
3203static int
3204load_long_binget(Unpicklerobject *self) {
3205 PyObject *py_key = 0, *value = 0;
3206 unsigned char c, *s;
3207 long key;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003208 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003209
Guido van Rossum053b8df1998-11-25 16:18:00 +00003210 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003211
3212 c = (unsigned char)s[0];
3213 key = (long)c;
3214 c = (unsigned char)s[1];
3215 key |= (long)c << 8;
3216 c = (unsigned char)s[2];
3217 key |= (long)c << 16;
3218 c = (unsigned char)s[3];
3219 key |= (long)c << 24;
3220
Guido van Rossum053b8df1998-11-25 16:18:00 +00003221 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3222
3223 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003224 if (! value) {
3225 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003226 rc = -1;
3227 } else {
3228 PDATA_APPEND(self->stack, value, -1);
3229 rc = 0;
3230 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003231
Guido van Rossum2f80d961999-07-13 15:18:58 +00003232 Py_DECREF(py_key);
3233 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003234}
3235
3236
3237static int
3238load_put(Unpicklerobject *self) {
3239 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003240 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003241 char *s;
3242
Guido van Rossum053b8df1998-11-25 16:18:00 +00003243 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003244 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003245 UNLESS (len=self->stack->length) return stackUnderflow();
3246 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3247 value=self->stack->data[len-1];
3248 l=PyDict_SetItem(self->memo, py_str, value);
3249 Py_DECREF(py_str);
3250 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003251}
3252
3253
3254static int
3255load_binput(Unpicklerobject *self) {
3256 PyObject *py_key = 0, *value = 0;
3257 unsigned char key, *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003258 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003259
Guido van Rossum053b8df1998-11-25 16:18:00 +00003260 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3261 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003262
3263 key = (unsigned char)s[0];
3264
Guido van Rossum053b8df1998-11-25 16:18:00 +00003265 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3266 value=self->stack->data[len-1];
3267 len=PyDict_SetItem(self->memo, py_key, value);
3268 Py_DECREF(py_key);
3269 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003270}
3271
3272
3273static int
3274load_long_binput(Unpicklerobject *self) {
3275 PyObject *py_key = 0, *value = 0;
3276 long key;
3277 unsigned char c, *s;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003278 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003279
Guido van Rossum053b8df1998-11-25 16:18:00 +00003280 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3281 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003282
3283 c = (unsigned char)s[0];
3284 key = (long)c;
3285 c = (unsigned char)s[1];
3286 key |= (long)c << 8;
3287 c = (unsigned char)s[2];
3288 key |= (long)c << 16;
3289 c = (unsigned char)s[3];
3290 key |= (long)c << 24;
3291
Guido van Rossum053b8df1998-11-25 16:18:00 +00003292 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3293 value=self->stack->data[len-1];
3294 len=PyDict_SetItem(self->memo, py_key, value);
3295 Py_DECREF(py_key);
3296 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003297}
3298
3299
3300static int
3301do_append(Unpicklerobject *self, int x) {
3302 PyObject *value = 0, *list = 0, *append_method = 0;
3303 int len, i;
3304
Guido van Rossum053b8df1998-11-25 16:18:00 +00003305 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3306 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003307
Guido van Rossum053b8df1998-11-25 16:18:00 +00003308 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003309
3310 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003311 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003312 int list_len;
3313
Guido van Rossum053b8df1998-11-25 16:18:00 +00003314 slice=Pdata_popList(self->stack, x);
3315 list_len = PyList_GET_SIZE(list);
3316 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003317 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003318 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003319 }
3320 else {
3321
Guido van Rossum053b8df1998-11-25 16:18:00 +00003322 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003323 return -1;
3324
3325 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003326 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003327
Guido van Rossum053b8df1998-11-25 16:18:00 +00003328 value=self->stack->data[i];
3329 junk=0;
3330 ARG_TUP(self, value);
3331 if (self->arg) {
3332 junk = PyObject_CallObject(append_method, self->arg);
3333 FREE_ARG_TUP(self);
3334 }
3335 if (! junk) {
3336 Pdata_clear(self->stack, i+1);
3337 self->stack->length=x;
3338 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003339 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003340 }
3341 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003342 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003343 self->stack->length=x;
3344 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003345 }
3346
Guido van Rossum60456fd1997-04-09 17:36:32 +00003347 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003348}
3349
3350
3351static int
3352load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003353 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003354}
3355
3356
3357static int
3358load_appends(Unpicklerobject *self) {
3359 return do_append(self, marker(self));
3360}
3361
3362
3363static int
3364do_setitems(Unpicklerobject *self, int x) {
3365 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003366 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003367
Guido van Rossum053b8df1998-11-25 16:18:00 +00003368 UNLESS ((len=self->stack->length) >= x
3369 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003370
Guido van Rossum053b8df1998-11-25 16:18:00 +00003371 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003372
Guido van Rossum053b8df1998-11-25 16:18:00 +00003373 for (i = x+1; i < len; i += 2) {
3374 key =self->stack->data[i-1];
3375 value=self->stack->data[i ];
3376 if (PyObject_SetItem(dict, key, value) < 0) {
3377 r=-1;
3378 break;
3379 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003380 }
3381
Guido van Rossum053b8df1998-11-25 16:18:00 +00003382 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003383
Guido van Rossum053b8df1998-11-25 16:18:00 +00003384 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003385}
3386
3387
3388static int
3389load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003390 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003391}
3392
Guido van Rossum60456fd1997-04-09 17:36:32 +00003393static int
3394load_setitems(Unpicklerobject *self) {
3395 return do_setitems(self, marker(self));
3396}
3397
3398
3399static int
3400load_build(Unpicklerobject *self) {
3401 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3402 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003403 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003404
Guido van Rossum053b8df1998-11-25 16:18:00 +00003405 if (self->stack->length < 2) return stackUnderflow();
3406 PDATA_POP(self->stack, value);
3407 if (! value) return -1;
3408 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003409
Guido van Rossum053b8df1998-11-25 16:18:00 +00003410 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3411 ARG_TUP(self, value);
3412 if (self->arg) {
3413 junk = PyObject_CallObject(__setstate__, self->arg);
3414 FREE_ARG_TUP(self);
3415 }
3416 Py_DECREF(__setstate__);
3417 if (! junk) return -1;
3418 Py_DECREF(junk);
3419 return 0;
3420 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003421
Guido van Rossum053b8df1998-11-25 16:18:00 +00003422 PyErr_Clear();
3423 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003424 i = 0;
3425 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003426 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3427 r=-1;
3428 break;
3429 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003430 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003431 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003432 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003433 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003434
Guido van Rossum053b8df1998-11-25 16:18:00 +00003435 Py_XDECREF(value);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003436
Guido van Rossum053b8df1998-11-25 16:18:00 +00003437 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003438}
3439
3440
3441static int
3442load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003443 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003444
Guido van Rossumea2b7152000-05-09 18:14:50 +00003445 /* Note that we split the (pickle.py) stack into two stacks, an
3446 object stack and a mark stack. Here we push a mark onto the
3447 mark stack.
3448 */
3449
Guido van Rossum053b8df1998-11-25 16:18:00 +00003450 if ((self->num_marks + 1) >= self->marks_size) {
3451 s=self->marks_size+20;
3452 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003453 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003454 self->marks=(int *)malloc(s * sizeof(int));
3455 else
3456 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003457 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003458 PyErr_NoMemory();
3459 return -1;
3460 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003461 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003462 }
3463
Guido van Rossum053b8df1998-11-25 16:18:00 +00003464 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003465
3466 return 0;
3467}
3468
3469static int
3470load_reduce(Unpicklerobject *self) {
3471 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003472
Guido van Rossum053b8df1998-11-25 16:18:00 +00003473 PDATA_POP(self->stack, arg_tup);
3474 if (! arg_tup) return -1;
3475 PDATA_POP(self->stack, callable);
3476 if (callable) {
3477 ob = Instance_New(callable, arg_tup);
3478 Py_DECREF(callable);
3479 }
3480 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003481
Guido van Rossum053b8df1998-11-25 16:18:00 +00003482 if (! ob) return -1;
3483
3484 PDATA_PUSH(self->stack, ob, -1);
3485 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003486}
3487
3488static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003489load(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003490 PyObject *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003491 char *s;
3492
Guido van Rossum60456fd1997-04-09 17:36:32 +00003493 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003494 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003495
3496 while (1) {
3497 if ((*self->read_func)(self, &s, 1) < 0)
3498 break;
3499
3500 switch (s[0]) {
3501 case NONE:
3502 if (load_none(self) < 0)
3503 break;
3504 continue;
3505
3506 case BININT:
3507 if (load_binint(self) < 0)
3508 break;
3509 continue;
3510
3511 case BININT1:
3512 if (load_binint1(self) < 0)
3513 break;
3514 continue;
3515
3516 case BININT2:
3517 if (load_binint2(self) < 0)
3518 break;
3519 continue;
3520
3521 case INT:
3522 if (load_int(self) < 0)
3523 break;
3524 continue;
3525
3526 case LONG:
3527 if (load_long(self) < 0)
3528 break;
3529 continue;
3530
3531 case FLOAT:
3532 if (load_float(self) < 0)
3533 break;
3534 continue;
3535
Guido van Rossum60456fd1997-04-09 17:36:32 +00003536 case BINFLOAT:
3537 if (load_binfloat(self) < 0)
3538 break;
3539 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003540
3541 case BINSTRING:
3542 if (load_binstring(self) < 0)
3543 break;
3544 continue;
3545
3546 case SHORT_BINSTRING:
3547 if (load_short_binstring(self) < 0)
3548 break;
3549 continue;
3550
3551 case STRING:
3552 if (load_string(self) < 0)
3553 break;
3554 continue;
3555
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003556 case UNICODE:
3557 if (load_unicode(self) < 0)
3558 break;
3559 continue;
3560
3561 case BINUNICODE:
3562 if (load_binunicode(self) < 0)
3563 break;
3564 continue;
3565
Guido van Rossum60456fd1997-04-09 17:36:32 +00003566 case EMPTY_TUPLE:
3567 if (load_empty_tuple(self) < 0)
3568 break;
3569 continue;
3570
3571 case TUPLE:
3572 if (load_tuple(self) < 0)
3573 break;
3574 continue;
3575
3576 case EMPTY_LIST:
3577 if (load_empty_list(self) < 0)
3578 break;
3579 continue;
3580
3581 case LIST:
3582 if (load_list(self) < 0)
3583 break;
3584 continue;
3585
3586 case EMPTY_DICT:
3587 if (load_empty_dict(self) < 0)
3588 break;
3589 continue;
3590
3591 case DICT:
3592 if (load_dict(self) < 0)
3593 break;
3594 continue;
3595
3596 case OBJ:
3597 if (load_obj(self) < 0)
3598 break;
3599 continue;
3600
3601 case INST:
3602 if (load_inst(self) < 0)
3603 break;
3604 continue;
3605
3606 case GLOBAL:
3607 if (load_global(self) < 0)
3608 break;
3609 continue;
3610
3611 case APPEND:
3612 if (load_append(self) < 0)
3613 break;
3614 continue;
3615
3616 case APPENDS:
3617 if (load_appends(self) < 0)
3618 break;
3619 continue;
3620
3621 case BUILD:
3622 if (load_build(self) < 0)
3623 break;
3624 continue;
3625
3626 case DUP:
3627 if (load_dup(self) < 0)
3628 break;
3629 continue;
3630
3631 case BINGET:
3632 if (load_binget(self) < 0)
3633 break;
3634 continue;
3635
3636 case LONG_BINGET:
3637 if (load_long_binget(self) < 0)
3638 break;
3639 continue;
3640
3641 case GET:
3642 if (load_get(self) < 0)
3643 break;
3644 continue;
3645
3646 case MARK:
3647 if (load_mark(self) < 0)
3648 break;
3649 continue;
3650
3651 case BINPUT:
3652 if (load_binput(self) < 0)
3653 break;
3654 continue;
3655
3656 case LONG_BINPUT:
3657 if (load_long_binput(self) < 0)
3658 break;
3659 continue;
3660
3661 case PUT:
3662 if (load_put(self) < 0)
3663 break;
3664 continue;
3665
3666 case POP:
3667 if (load_pop(self) < 0)
3668 break;
3669 continue;
3670
3671 case POP_MARK:
3672 if (load_pop_mark(self) < 0)
3673 break;
3674 continue;
3675
3676 case SETITEM:
3677 if (load_setitem(self) < 0)
3678 break;
3679 continue;
3680
3681 case SETITEMS:
3682 if (load_setitems(self) < 0)
3683 break;
3684 continue;
3685
3686 case STOP:
3687 break;
3688
3689 case PERSID:
3690 if (load_persid(self) < 0)
3691 break;
3692 continue;
3693
3694 case BINPERSID:
3695 if (load_binpersid(self) < 0)
3696 break;
3697 continue;
3698
3699 case REDUCE:
3700 if (load_reduce(self) < 0)
3701 break;
3702 continue;
3703
3704 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003705 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003706 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003707 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003708 }
3709
3710 break;
3711 }
3712
Guido van Rossum053b8df1998-11-25 16:18:00 +00003713 if ((err = PyErr_Occurred())) {
3714 if (err == PyExc_EOFError) {
3715 PyErr_SetNone(PyExc_EOFError);
3716 }
3717 return NULL;
3718 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003719
Guido van Rossum053b8df1998-11-25 16:18:00 +00003720 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003721 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003722}
3723
3724
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003725/* No-load functions to support noload, which is used to
3726 find persistent references. */
3727
3728static int
3729noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003730 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003731
3732 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003733 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003734}
3735
3736
3737static int
3738noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003739 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003740 char *s;
3741
3742 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003743 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003744 if ((*self->readline_func)(self, &s) < 0) return -1;
3745 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003746 PDATA_APPEND(self->stack, Py_None,-1);
3747 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003748}
3749
3750static int
3751noload_global(Unpicklerobject *self) {
3752 char *s;
3753
3754 if ((*self->readline_func)(self, &s) < 0) return -1;
3755 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003756 PDATA_APPEND(self->stack, Py_None,-1);
3757 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003758}
3759
3760static int
3761noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003762
Guido van Rossum053b8df1998-11-25 16:18:00 +00003763 if (self->stack->length < 2) return stackUnderflow();
3764 Pdata_clear(self->stack, self->stack->length-2);
3765 PDATA_APPEND(self->stack, Py_None,-1);
3766 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003767}
3768
3769static int
3770noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003771
Guido van Rossum053b8df1998-11-25 16:18:00 +00003772 if (self->stack->length < 1) return stackUnderflow();
3773 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003774 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003775}
3776
3777
3778static PyObject *
3779noload(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003780 PyObject *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003781 char *s;
3782
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003783 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003784 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003785
3786 while (1) {
3787 if ((*self->read_func)(self, &s, 1) < 0)
3788 break;
3789
3790 switch (s[0]) {
3791 case NONE:
3792 if (load_none(self) < 0)
3793 break;
3794 continue;
3795
3796 case BININT:
3797 if (load_binint(self) < 0)
3798 break;
3799 continue;
3800
3801 case BININT1:
3802 if (load_binint1(self) < 0)
3803 break;
3804 continue;
3805
3806 case BININT2:
3807 if (load_binint2(self) < 0)
3808 break;
3809 continue;
3810
3811 case INT:
3812 if (load_int(self) < 0)
3813 break;
3814 continue;
3815
3816 case LONG:
3817 if (load_long(self) < 0)
3818 break;
3819 continue;
3820
3821 case FLOAT:
3822 if (load_float(self) < 0)
3823 break;
3824 continue;
3825
3826 case BINFLOAT:
3827 if (load_binfloat(self) < 0)
3828 break;
3829 continue;
3830
3831 case BINSTRING:
3832 if (load_binstring(self) < 0)
3833 break;
3834 continue;
3835
3836 case SHORT_BINSTRING:
3837 if (load_short_binstring(self) < 0)
3838 break;
3839 continue;
3840
3841 case STRING:
3842 if (load_string(self) < 0)
3843 break;
3844 continue;
3845
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003846 case UNICODE:
3847 if (load_unicode(self) < 0)
3848 break;
3849 continue;
3850
3851 case BINUNICODE:
3852 if (load_binunicode(self) < 0)
3853 break;
3854 continue;
3855
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003856 case EMPTY_TUPLE:
3857 if (load_empty_tuple(self) < 0)
3858 break;
3859 continue;
3860
3861 case TUPLE:
3862 if (load_tuple(self) < 0)
3863 break;
3864 continue;
3865
3866 case EMPTY_LIST:
3867 if (load_empty_list(self) < 0)
3868 break;
3869 continue;
3870
3871 case LIST:
3872 if (load_list(self) < 0)
3873 break;
3874 continue;
3875
3876 case EMPTY_DICT:
3877 if (load_empty_dict(self) < 0)
3878 break;
3879 continue;
3880
3881 case DICT:
3882 if (load_dict(self) < 0)
3883 break;
3884 continue;
3885
3886 case OBJ:
3887 if (noload_obj(self) < 0)
3888 break;
3889 continue;
3890
3891 case INST:
3892 if (noload_inst(self) < 0)
3893 break;
3894 continue;
3895
3896 case GLOBAL:
3897 if (noload_global(self) < 0)
3898 break;
3899 continue;
3900
3901 case APPEND:
3902 if (load_append(self) < 0)
3903 break;
3904 continue;
3905
3906 case APPENDS:
3907 if (load_appends(self) < 0)
3908 break;
3909 continue;
3910
3911 case BUILD:
3912 if (noload_build(self) < 0)
3913 break;
3914 continue;
3915
3916 case DUP:
3917 if (load_dup(self) < 0)
3918 break;
3919 continue;
3920
3921 case BINGET:
3922 if (load_binget(self) < 0)
3923 break;
3924 continue;
3925
3926 case LONG_BINGET:
3927 if (load_long_binget(self) < 0)
3928 break;
3929 continue;
3930
3931 case GET:
3932 if (load_get(self) < 0)
3933 break;
3934 continue;
3935
3936 case MARK:
3937 if (load_mark(self) < 0)
3938 break;
3939 continue;
3940
3941 case BINPUT:
3942 if (load_binput(self) < 0)
3943 break;
3944 continue;
3945
3946 case LONG_BINPUT:
3947 if (load_long_binput(self) < 0)
3948 break;
3949 continue;
3950
3951 case PUT:
3952 if (load_put(self) < 0)
3953 break;
3954 continue;
3955
3956 case POP:
3957 if (load_pop(self) < 0)
3958 break;
3959 continue;
3960
3961 case POP_MARK:
3962 if (load_pop_mark(self) < 0)
3963 break;
3964 continue;
3965
3966 case SETITEM:
3967 if (load_setitem(self) < 0)
3968 break;
3969 continue;
3970
3971 case SETITEMS:
3972 if (load_setitems(self) < 0)
3973 break;
3974 continue;
3975
3976 case STOP:
3977 break;
3978
3979 case PERSID:
3980 if (load_persid(self) < 0)
3981 break;
3982 continue;
3983
3984 case BINPERSID:
3985 if (load_binpersid(self) < 0)
3986 break;
3987 continue;
3988
3989 case REDUCE:
3990 if (noload_reduce(self) < 0)
3991 break;
3992 continue;
3993
3994 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003995 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003996 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003997 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003998 }
3999
4000 break;
4001 }
4002
Guido van Rossum053b8df1998-11-25 16:18:00 +00004003 if ((err = PyErr_Occurred())) {
4004 if (err == PyExc_EOFError) {
4005 PyErr_SetNone(PyExc_EOFError);
4006 }
4007 return NULL;
4008 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004009
Guido van Rossum053b8df1998-11-25 16:18:00 +00004010 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004011 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004012}
4013
4014
Guido van Rossum60456fd1997-04-09 17:36:32 +00004015static PyObject *
4016Unpickler_load(Unpicklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00004017 UNLESS (PyArg_ParseTuple(args, ":load"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004018 return NULL;
4019
4020 return load(self);
4021}
4022
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004023static PyObject *
4024Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00004025 UNLESS (PyArg_ParseTuple(args, ":noload"))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004026 return NULL;
4027
4028 return noload(self);
4029}
4030
Guido van Rossum60456fd1997-04-09 17:36:32 +00004031
4032static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004033 {"load", (PyCFunction)Unpickler_load, 1,
4034 "load() -- Load a pickle"
4035 },
4036 {"noload", (PyCFunction)Unpickler_noload, 1,
4037 "noload() -- not load a pickle, but go through most of the motions\n"
4038 "\n"
4039 "This function can be used to read past a pickle without instantiating\n"
4040 "any objects or importing any modules. It can also be used to find all\n"
4041 "persistent references without instantiating any objects or importing\n"
4042 "any modules.\n"
4043 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004044 {NULL, NULL} /* sentinel */
4045};
4046
4047
4048static Unpicklerobject *
4049newUnpicklerobject(PyObject *f) {
4050 Unpicklerobject *self;
4051
Guido van Rossumb18618d2000-05-03 23:44:39 +00004052 UNLESS (self = PyObject_New(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004053 return NULL;
4054
4055 self->file = NULL;
4056 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004057 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004058 self->pers_func = NULL;
4059 self->last_string = NULL;
4060 self->marks = NULL;
4061 self->num_marks = 0;
4062 self->marks_size = 0;
4063 self->buf_size = 0;
4064 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00004065 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00004066 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004067 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004068
Guido van Rossum83addc72000-04-21 20:49:36 +00004069 UNLESS (self->memo = PyDict_New())
4070 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004071
4072 Py_INCREF(f);
4073 self->file = f;
4074
4075 /* Set read, readline based on type of f */
4076 if (PyFile_Check(f)) {
4077 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004078 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00004079 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
4080 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004081 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004082 self->read_func = read_file;
4083 self->readline_func = readline_file;
4084 }
4085 else if (PycStringIO_InputCheck(f)) {
4086 self->fp = NULL;
4087 self->read_func = read_cStringIO;
4088 self->readline_func = readline_cStringIO;
4089 }
4090 else {
4091
4092 self->fp = NULL;
4093 self->read_func = read_other;
4094 self->readline_func = readline_other;
4095
Guido van Rossum053b8df1998-11-25 16:18:00 +00004096 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004097 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004098 PyErr_Clear();
4099 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
4100 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00004101 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004102 }
4103 }
4104
Guido van Rossum053b8df1998-11-25 16:18:00 +00004105 if (PyEval_GetRestricted()) {
4106 /* Restricted execution, get private tables */
4107 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004108
Guido van Rossum053b8df1998-11-25 16:18:00 +00004109 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
4110 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
4111 Py_DECREF(m);
4112 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004113 }
4114 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004115 self->safe_constructors=safe_constructors;
4116 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004117 }
4118
Guido van Rossum60456fd1997-04-09 17:36:32 +00004119 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004120
4121err:
4122 Py_DECREF((PyObject *)self);
4123 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004124}
4125
4126
4127static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004128get_Unpickler(PyObject *self, PyObject *args) {
4129 PyObject *file;
4130
Guido van Rossum43713e52000-02-29 13:59:29 +00004131 UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004132 return NULL;
4133 return (PyObject *)newUnpicklerobject(file);
4134}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004135
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004136
Guido van Rossum60456fd1997-04-09 17:36:32 +00004137static void
4138Unpickler_dealloc(Unpicklerobject *self) {
4139 Py_XDECREF(self->readline);
4140 Py_XDECREF(self->read);
4141 Py_XDECREF(self->file);
4142 Py_XDECREF(self->memo);
4143 Py_XDECREF(self->stack);
4144 Py_XDECREF(self->pers_func);
4145 Py_XDECREF(self->arg);
4146 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004147 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004148
Guido van Rossum60456fd1997-04-09 17:36:32 +00004149 if (self->marks) {
4150 free(self->marks);
4151 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004152
Guido van Rossum60456fd1997-04-09 17:36:32 +00004153 if (self->buf_size) {
4154 free(self->buf);
4155 }
4156
Guido van Rossumb18618d2000-05-03 23:44:39 +00004157 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004158}
4159
4160
4161static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004162Unpickler_getattr(Unpicklerobject *self, char *name) {
4163 if (!strcmp(name, "persistent_load")) {
4164 if (!self->pers_func) {
4165 PyErr_SetString(PyExc_AttributeError, name);
4166 return NULL;
4167 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004168
Guido van Rossum60456fd1997-04-09 17:36:32 +00004169 Py_INCREF(self->pers_func);
4170 return self->pers_func;
4171 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004172
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004173 if (!strcmp(name, "find_global")) {
4174 if (!self->find_class) {
4175 PyErr_SetString(PyExc_AttributeError, name);
4176 return NULL;
4177 }
4178
4179 Py_INCREF(self->find_class);
4180 return self->find_class;
4181 }
4182
Guido van Rossum60456fd1997-04-09 17:36:32 +00004183 if (!strcmp(name, "memo")) {
4184 if (!self->memo) {
4185 PyErr_SetString(PyExc_AttributeError, name);
4186 return NULL;
4187 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004188
Guido van Rossum60456fd1997-04-09 17:36:32 +00004189 Py_INCREF(self->memo);
4190 return self->memo;
4191 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004192
Guido van Rossum60456fd1997-04-09 17:36:32 +00004193 if (!strcmp(name, "UnpicklingError")) {
4194 Py_INCREF(UnpicklingError);
4195 return UnpicklingError;
4196 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004197
Guido van Rossum60456fd1997-04-09 17:36:32 +00004198 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4199}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004200
Guido van Rossum60456fd1997-04-09 17:36:32 +00004201
4202static int
4203Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004204
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004205 if (!strcmp(name, "persistent_load")) {
4206 Py_XDECREF(self->pers_func);
4207 self->pers_func = value;
4208 Py_XINCREF(value);
4209 return 0;
4210 }
4211
4212 if (!strcmp(name, "find_global")) {
4213 Py_XDECREF(self->find_class);
4214 self->find_class = value;
4215 Py_XINCREF(value);
4216 return 0;
4217 }
4218
Guido van Rossum053b8df1998-11-25 16:18:00 +00004219 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004220 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004221 "attribute deletion is not supported");
4222 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004223 }
4224
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004225 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004226 if (! PyDict_Check(value)) {
4227 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4228 return -1;
4229 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004230 Py_XDECREF(self->memo);
4231 self->memo = value;
4232 Py_INCREF(value);
4233 return 0;
4234 }
4235
Guido van Rossum60456fd1997-04-09 17:36:32 +00004236 PyErr_SetString(PyExc_AttributeError, name);
4237 return -1;
4238}
4239
4240
4241static PyObject *
4242cpm_dump(PyObject *self, PyObject *args) {
4243 PyObject *ob, *file, *res = NULL;
4244 Picklerobject *pickler = 0;
4245 int bin = 0;
4246
Guido van Rossum053b8df1998-11-25 16:18:00 +00004247 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004248 goto finally;
4249
Guido van Rossum053b8df1998-11-25 16:18:00 +00004250 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004251 goto finally;
4252
4253 if (dump(pickler, ob) < 0)
4254 goto finally;
4255
4256 Py_INCREF(Py_None);
4257 res = Py_None;
4258
4259finally:
4260 Py_XDECREF(pickler);
4261
4262 return res;
4263}
4264
4265
4266static PyObject *
4267cpm_dumps(PyObject *self, PyObject *args) {
4268 PyObject *ob, *file = 0, *res = NULL;
4269 Picklerobject *pickler = 0;
4270 int bin = 0;
4271
Guido van Rossum43713e52000-02-29 13:59:29 +00004272 UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004273 goto finally;
4274
Guido van Rossum053b8df1998-11-25 16:18:00 +00004275 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004276 goto finally;
4277
Guido van Rossum053b8df1998-11-25 16:18:00 +00004278 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004279 goto finally;
4280
4281 if (dump(pickler, ob) < 0)
4282 goto finally;
4283
4284 res = PycStringIO->cgetvalue(file);
4285
4286finally:
4287 Py_XDECREF(pickler);
4288 Py_XDECREF(file);
4289
4290 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004291}
4292
4293
4294static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004295cpm_load(PyObject *self, PyObject *args) {
4296 Unpicklerobject *unpickler = 0;
4297 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004298
Guido van Rossum43713e52000-02-29 13:59:29 +00004299 UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004300 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004301
Guido van Rossum053b8df1998-11-25 16:18:00 +00004302 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004303 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004304
Guido van Rossum60456fd1997-04-09 17:36:32 +00004305 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004306
Guido van Rossum60456fd1997-04-09 17:36:32 +00004307finally:
4308 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004309
Guido van Rossum60456fd1997-04-09 17:36:32 +00004310 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004311}
4312
4313
4314static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004315cpm_loads(PyObject *self, PyObject *args) {
4316 PyObject *ob, *file = 0, *res = NULL;
4317 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004318
Guido van Rossum43713e52000-02-29 13:59:29 +00004319 UNLESS (PyArg_ParseTuple(args, "S:loads", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004320 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004321
Guido van Rossum053b8df1998-11-25 16:18:00 +00004322 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004323 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004324
Guido van Rossum053b8df1998-11-25 16:18:00 +00004325 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004326 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004327
Guido van Rossum60456fd1997-04-09 17:36:32 +00004328 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004329
Guido van Rossum60456fd1997-04-09 17:36:32 +00004330finally:
4331 Py_XDECREF(file);
4332 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004333
Guido van Rossum60456fd1997-04-09 17:36:32 +00004334 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004335}
4336
4337
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004338static char Unpicklertype__doc__[] =
4339"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004340
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004341static PyTypeObject Unpicklertype = {
4342 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004343 0, /*ob_size*/
4344 "Unpickler", /*tp_name*/
4345 sizeof(Unpicklerobject), /*tp_basicsize*/
4346 0, /*tp_itemsize*/
4347 /* methods */
4348 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4349 (printfunc)0, /*tp_print*/
4350 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4351 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4352 (cmpfunc)0, /*tp_compare*/
4353 (reprfunc)0, /*tp_repr*/
4354 0, /*tp_as_number*/
4355 0, /*tp_as_sequence*/
4356 0, /*tp_as_mapping*/
4357 (hashfunc)0, /*tp_hash*/
4358 (ternaryfunc)0, /*tp_call*/
4359 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004360
Guido van Rossum60456fd1997-04-09 17:36:32 +00004361 /* Space for future expansion */
4362 0L,0L,0L,0L,
4363 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004364};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004365
Guido van Rossum60456fd1997-04-09 17:36:32 +00004366static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004367 {"dump", (PyCFunction)cpm_dump, 1,
4368 "dump(object, file, [binary]) --"
4369 "Write an object in pickle format to the given file\n"
4370 "\n"
4371 "If the optional argument, binary, is provided and is true, then the\n"
4372 "pickle will be written in binary format, which is more space and\n"
4373 "computationally efficient. \n"
4374 },
4375 {"dumps", (PyCFunction)cpm_dumps, 1,
4376 "dumps(object, [binary]) --"
4377 "Return a string containing an object in pickle format\n"
4378 "\n"
4379 "If the optional argument, binary, is provided and is true, then the\n"
4380 "pickle will be written in binary format, which is more space and\n"
4381 "computationally efficient. \n"
4382 },
4383 {"load", (PyCFunction)cpm_load, 1,
4384 "load(file) -- Load a pickle from the given file"},
4385 {"loads", (PyCFunction)cpm_loads, 1,
4386 "loads(string) -- Load a pickle from the given string"},
4387 {"Pickler", (PyCFunction)get_Pickler, 1,
4388 "Pickler(file, [binary]) -- Create a pickler\n"
4389 "\n"
4390 "If the optional argument, binary, is provided and is true, then\n"
4391 "pickles will be written in binary format, which is more space and\n"
4392 "computationally efficient. \n"
4393 },
4394 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4395 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004396 { NULL, NULL }
4397};
4398
Guido van Rossum60456fd1997-04-09 17:36:32 +00004399static int
4400init_stuff(PyObject *module, PyObject *module_dict) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004401 PyObject *string, *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004402
4403#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4404
4405 INIT_STR(__class__);
4406 INIT_STR(__getinitargs__);
4407 INIT_STR(__dict__);
4408 INIT_STR(__getstate__);
4409 INIT_STR(__setstate__);
4410 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004411 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004412 INIT_STR(__reduce__);
4413 INIT_STR(write);
4414 INIT_STR(__safe_for_unpickling__);
4415 INIT_STR(append);
4416 INIT_STR(read);
4417 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004418 INIT_STR(copy_reg);
4419 INIT_STR(dispatch_table);
4420 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004421 INIT_STR(__basicnew__);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004422 UNLESS (empty_str=PyString_FromString("")) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004423
Guido van Rossum053b8df1998-11-25 16:18:00 +00004424 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004425 return -1;
4426
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004427 /* These next few are special because we want to use different
4428 ones in restricted mode. */
4429
Guido van Rossum053b8df1998-11-25 16:18:00 +00004430 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004431 return -1;
4432
Guido van Rossum053b8df1998-11-25 16:18:00 +00004433 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4434 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004435 return -1;
4436
4437 Py_DECREF(copy_reg);
4438
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004439 /* Down to here ********************************** */
4440
Guido van Rossum053b8df1998-11-25 16:18:00 +00004441 UNLESS (string = PyImport_ImportModule("string"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004442 return -1;
4443
Guido van Rossum053b8df1998-11-25 16:18:00 +00004444 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004445 return -1;
4446
4447 Py_DECREF(string);
4448
Guido van Rossum053b8df1998-11-25 16:18:00 +00004449 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004450 return -1;
4451
Guido van Rossumc03158b1999-06-09 15:23:31 +00004452 /* Ugh */
4453 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4454 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4455 return -1;
4456
4457 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004458 UNLESS (r=PyRun_String(
4459 "def __init__(self, *args): self.args=args\n\n"
4460 "def __str__(self):\n"
4461 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4462 Py_file_input,
4463 module_dict, t) ) return -1;
4464 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004465
4466 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4467 return -1;
4468
4469 Py_DECREF(t);
4470
4471
4472 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
4473 PickleError, NULL))
4474 return -1;
4475
4476 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004477 UNLESS (r=PyRun_String(
4478 "def __init__(self, *args): self.args=args\n\n"
4479 "def __str__(self):\n"
4480 " a=self.args\n"
4481 " a=a and type(a[0]) or '(what)'\n"
4482 " return 'Cannot pickle %s objects' % a\n"
4483 , Py_file_input,
4484 module_dict, t) ) return -1;
4485 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004486
4487 UNLESS (UnpickleableError = PyErr_NewException(
4488 "cPickle.UnpickleableError", PicklingError, t))
4489 return -1;
4490
4491 Py_DECREF(t);
4492
4493 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
4494 PickleError, NULL))
4495 return -1;
4496
4497 if (PyDict_SetItemString(module_dict, "PickleError",
4498 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004499 return -1;
4500
4501 if (PyDict_SetItemString(module_dict, "PicklingError",
4502 PicklingError) < 0)
4503 return -1;
4504
Guido van Rossum60456fd1997-04-09 17:36:32 +00004505 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4506 UnpicklingError) < 0)
4507 return -1;
4508
Guido van Rossumc03158b1999-06-09 15:23:31 +00004509 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4510 UnpickleableError) < 0)
4511 return -1;
4512
Guido van Rossum053b8df1998-11-25 16:18:00 +00004513 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4514 return -1;
4515
4516 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4517 BadPickleGet) < 0)
4518 return -1;
4519
Guido van Rossum60456fd1997-04-09 17:36:32 +00004520 PycString_IMPORT;
4521
4522 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004523}
4524
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004525#ifndef DL_EXPORT /* declarations for DLL import/export */
4526#define DL_EXPORT(RTYPE) RTYPE
4527#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004528DL_EXPORT(void)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004529initcPickle() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004530 PyObject *m, *d, *v;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004531 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004532 PyObject *format_version;
4533 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004534
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004535 Picklertype.ob_type = &PyType_Type;
4536 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004537 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004538
Guido van Rossum60456fd1997-04-09 17:36:32 +00004539 /* Create the module and add the functions */
4540 m = Py_InitModule4("cPickle", cPickle_methods,
4541 cPickle_module_documentation,
4542 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004543
Guido van Rossum60456fd1997-04-09 17:36:32 +00004544 /* Add some symbolic constants to the module */
4545 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004546 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004547 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004548
Guido van Rossum60456fd1997-04-09 17:36:32 +00004549 format_version = PyString_FromString("1.3");
4550 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004551
Guido van Rossum60456fd1997-04-09 17:36:32 +00004552 PyDict_SetItemString(d, "format_version", format_version);
4553 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004554 Py_XDECREF(format_version);
4555 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004556
Guido van Rossum60456fd1997-04-09 17:36:32 +00004557 init_stuff(m, d);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004558}