blob: c3e10e7292654ad19e5271a44ae261eabf81c53f [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001/*
Guido van Rossumc03158b1999-06-09 15:23:31 +00002 * cPickle.c,v 1.67 1999/05/12 16:09:45 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 Rossumc03158b1999-06-09 15:23:31 +000052"cPickle.c,v 1.67 1999/05/12 16:09:45 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 Rossum60456fd1997-04-09 17:36:32 +000092#define APPEND 'a'
93#define BUILD 'b'
94#define GLOBAL 'c'
95#define DICT 'd'
96#define EMPTY_DICT '}'
97#define APPENDS 'e'
98#define GET 'g'
99#define BINGET 'h'
100#define INST 'i'
101#define LONG_BINGET 'j'
102#define LIST 'l'
103#define EMPTY_LIST ']'
104#define OBJ 'o'
105#define PUT 'p'
106#define BINPUT 'q'
107#define LONG_BINPUT 'r'
108#define SETITEM 's'
109#define TUPLE 't'
110#define EMPTY_TUPLE ')'
111#define SETITEMS 'u'
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000112
Guido van Rossum60456fd1997-04-09 17:36:32 +0000113static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000114
115/* atol function from string module */
116static PyObject *atol_func;
117
Guido van Rossumc03158b1999-06-09 15:23:31 +0000118static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000119static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000120static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000121static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000122static PyObject *BadPickleGet;
123
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000124
Guido van Rossum60456fd1997-04-09 17:36:32 +0000125static PyObject *dispatch_table;
126static PyObject *safe_constructors;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000127static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000128
Guido van Rossum60456fd1997-04-09 17:36:32 +0000129static PyObject *__class___str, *__getinitargs___str, *__dict___str,
130 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
131 *write_str, *__safe_for_unpickling___str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000132 *read_str, *readline_str, *__main___str, *__basicnew___str,
Guido van Rossum053b8df1998-11-25 16:18:00 +0000133 *copy_reg_str, *dispatch_table_str, *safe_constructors_str, *empty_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000134
Guido van Rossum60456fd1997-04-09 17:36:32 +0000135static int save();
136static int put2();
137
Guido van Rossum053b8df1998-11-25 16:18:00 +0000138#ifndef PyList_SET_ITEM
139#define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
140#endif
141#ifndef PyList_GET_SIZE
142#define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
143#endif
144#ifndef PyTuple_SET_ITEM
145#define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
146#endif
147#ifndef PyTuple_GET_SIZE
148#define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
149#endif
150#ifndef PyString_GET_SIZE
151#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
152#endif
153
154/*************************************************************************
155 Internal Data type for pickle data. */
156
157typedef struct {
158 PyObject_HEAD
159 int length, size;
160 PyObject **data;
161} Pdata;
162
163static void
164Pdata_dealloc(Pdata *self) {
165 int i;
166 PyObject **p;
167
168 for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
169
170 if (self->data) free(self->data);
171
172 PyMem_DEL(self);
173}
174
175static PyTypeObject PdataType = {
176 PyObject_HEAD_INIT(NULL) 0, "Pdata", sizeof(Pdata), 0,
177 (destructor)Pdata_dealloc,
178 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
179};
180
181#define Pdata_Check(O) ((O)->ob_type == &PdataType)
182
183static PyObject *
184Pdata_New() {
185 Pdata *self;
186
187 UNLESS (self = PyObject_NEW(Pdata, &PdataType)) return NULL;
188 self->size=8;
189 self->length=0;
190 self->data=malloc(self->size * sizeof(PyObject*));
191 if (self->data) return (PyObject*)self;
192 Py_DECREF(self);
193 return PyErr_NoMemory();
194}
195
196static int
197stackUnderflow() {
198 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
199 return -1;
200}
201
202static int
203Pdata_clear(Pdata *self, int clearto) {
204 int i;
205 PyObject **p;
206
207 if (clearto < 0) return stackUnderflow();
208 if (clearto >= self->length) return 0;
209
210 for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
211 Py_DECREF(*p);
212 self->length=clearto;
213
214 return 0;
215}
216
217
218static int
219Pdata_grow(Pdata *self) {
220 if (! self->size) {
221 PyErr_NoMemory();
222 return -1;
223 }
224 self->size *= 2;
225 self->data = realloc(self->data, self->size*sizeof(PyObject*));
226 if (! self->data) {
227 self->size = 0;
228 PyErr_NoMemory();
229 return -1;
230 }
231 return 0;
232}
233
234#define PDATA_POP(D,V) { \
235 if ((D)->length) V=D->data[--((D)->length)]; \
236 else { \
237 PyErr_SetString(UnpicklingError, "bad pickle data"); \
238 V=NULL; \
239 } \
240}
241
242
243static PyObject *
244Pdata_popTuple(Pdata *self, int start) {
245 PyObject *r;
246 int i, j, l;
247
248 l=self->length-start;
249 UNLESS (r=PyTuple_New(l)) return NULL;
250 for (i=start, j=0 ; j < l; )
251 PyTuple_SET_ITEM(r,j++,self->data[i++]);
252
253 self->length=start;
254 return r;
255}
256
257static PyObject *
258Pdata_popList(Pdata *self, int start) {
259 PyObject *r;
260 int i, j, l;
261
262 l=self->length-start;
263 UNLESS (r=PyList_New(l)) return NULL;
264 for (i=start, j=0 ; j < l; )
265 PyList_SET_ITEM(r,j++,self->data[i++]);
266
267 self->length=start;
268 return r;
269}
270
271#define PDATA_APPEND_(D,O,ER) { \
272 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
273}
274
275#define PDATA_APPEND(D,O,ER) { \
276 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
277 Pdata_grow((Pdata*)(D)) < 0) \
278 return ER; \
279 Py_INCREF(O); \
280 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
281}
282
283#define PDATA_PUSH(D,O,ER) { \
284 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
285 Pdata_grow((Pdata*)(D)) < 0) { \
286 Py_DECREF(O); \
287 return ER; \
288 } \
289 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
290}
291
292/*************************************************************************/
293
294#define ARG_TUP(self, o) { \
295 if (self->arg || (self->arg=PyTuple_New(1))) { \
296 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
297 PyTuple_SET_ITEM(self->arg,0,o); \
298 } \
299 else { \
300 Py_DECREF(o); \
301 } \
302}
303
304#define FREE_ARG_TUP(self) { \
305 if (self->arg->ob_refcnt > 1) { \
306 Py_DECREF(self->arg); \
307 self->arg=NULL; \
308 } \
309 }
310
Guido van Rossum60456fd1997-04-09 17:36:32 +0000311typedef struct {
312 PyObject_HEAD
313 FILE *fp;
314 PyObject *write;
315 PyObject *file;
316 PyObject *memo;
317 PyObject *arg;
318 PyObject *pers_func;
319 PyObject *inst_pers_func;
320 int bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000321 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000322 int (*write_func)();
323 char *write_buf;
324 int buf_size;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000325 PyObject *dispatch_table;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000326} Picklerobject;
327
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000328staticforward PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000329
Guido van Rossum60456fd1997-04-09 17:36:32 +0000330typedef struct {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000331 PyObject_HEAD
332 FILE *fp;
333 PyObject *file;
334 PyObject *readline;
335 PyObject *read;
336 PyObject *memo;
337 PyObject *arg;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000338 Pdata *stack;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000339 PyObject *mark;
340 PyObject *pers_func;
341 PyObject *last_string;
342 int *marks;
343 int num_marks;
344 int marks_size;
345 int (*read_func)();
346 int (*readline_func)();
347 int buf_size;
348 char *buf;
349 PyObject *safe_constructors;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +0000350 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000351} Unpicklerobject;
352
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000353staticforward PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000354
Guido van Rossum60456fd1997-04-09 17:36:32 +0000355int
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000356cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000357 PyObject *v;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000358
Guido van Rossum053b8df1998-11-25 16:18:00 +0000359 if ((v = PyObject_GetItem(o,key))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000360 Py_DECREF(v);
361 return 1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000362 }
363
Guido van Rossum60456fd1997-04-09 17:36:32 +0000364 PyErr_Clear();
365 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000366}
367
Guido van Rossumd385d591997-04-09 17:47:47 +0000368static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000369PyObject *
370#ifdef HAVE_STDARG_PROTOTYPES
371/* VARARGS 2 */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000372cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000373#else
374/* VARARGS */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000375cPickle_ErrFormat(va_alist) va_dcl {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000376#endif
Guido van Rossum60456fd1997-04-09 17:36:32 +0000377 va_list va;
378 PyObject *args=0, *retval=0;
379#ifdef HAVE_STDARG_PROTOTYPES
380 va_start(va, format);
381#else
382 PyObject *ErrType;
383 char *stringformat, *format;
384 va_start(va);
385 ErrType = va_arg(va, PyObject *);
386 stringformat = va_arg(va, char *);
387 format = va_arg(va, char *);
388#endif
389
Guido van Rossum053b8df1998-11-25 16:18:00 +0000390 if (format) args = Py_VaBuildValue(format, va);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000391 va_end(va);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000392 if (format && ! args) return NULL;
393 if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000394
Guido van Rossum053b8df1998-11-25 16:18:00 +0000395 if (retval) {
396 if (args) {
397 PyObject *v;
398 v=PyString_Format(retval, args);
399 Py_DECREF(retval);
400 Py_DECREF(args);
401 if (! v) return NULL;
402 retval=v;
403 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000404 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000405 else
Guido van Rossum053b8df1998-11-25 16:18:00 +0000406 if (args) retval=args;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000407 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000408 PyErr_SetObject(ErrType,Py_None);
409 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000410 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000411 PyErr_SetObject(ErrType,retval);
412 Py_DECREF(retval);
413 return NULL;
414}
415
416static int
417write_file(Picklerobject *self, char *s, int n) {
418 if (s == NULL) {
419 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000420 }
421
Guido van Rossum60456fd1997-04-09 17:36:32 +0000422 if ((int)fwrite(s, sizeof(char), n, self->fp) != n) {
423 PyErr_SetFromErrno(PyExc_IOError);
424 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000425 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000426
427 return n;
428}
429
Guido van Rossum60456fd1997-04-09 17:36:32 +0000430static int
431write_cStringIO(Picklerobject *self, char *s, int n) {
432 if (s == NULL) {
433 return 0;
434 }
435
436 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
437 return -1;
438 }
439
440 return n;
441}
442
Guido van Rossum60456fd1997-04-09 17:36:32 +0000443static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000444write_none(Picklerobject *self, char *s, int n) {
445 if (s == NULL) return 0;
446 return n;
447}
448
Guido van Rossum142eeb81997-08-13 03:14:41 +0000449static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000450write_other(Picklerobject *self, char *s, int n) {
451 PyObject *py_str = 0, *junk = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000452
453 if (s == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000454 UNLESS (self->buf_size) return 0;
455 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000456 PyString_FromStringAndSize(self->write_buf, self->buf_size))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000457 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000458 }
459 else {
460 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
461 if (write_other(self, NULL, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000462 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000463 }
464
465 if (n > WRITE_BUF_SIZE) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000466 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000467 PyString_FromStringAndSize(s, n))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000468 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000469 }
470 else {
471 memcpy(self->write_buf + self->buf_size, s, n);
472 self->buf_size += n;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000473 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000474 }
475 }
476
Guido van Rossum053b8df1998-11-25 16:18:00 +0000477 if (self->write) {
478 /* object with write method */
479 ARG_TUP(self, py_str);
480 if (self->arg) {
481 junk = PyObject_CallObject(self->write, self->arg);
482 FREE_ARG_TUP(self);
483 }
484 if (junk) Py_DECREF(junk);
485 else return -1;
486 }
487 else
488 PDATA_PUSH(self->file, py_str, -1);
489
490 self->buf_size = 0;
491 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000492}
493
494
495static int
496read_file(Unpicklerobject *self, char **s, int n) {
497
498 if (self->buf_size == 0) {
499 int size;
500
501 size = ((n < 32) ? 32 : n);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000502 UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000503 PyErr_NoMemory();
504 return -1;
505 }
506
507 self->buf_size = size;
508 }
509 else if (n > self->buf_size) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000510 UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000511 PyErr_NoMemory();
512 return -1;
513 }
514
515 self->buf_size = n;
516 }
517
518 if ((int)fread(self->buf, sizeof(char), n, self->fp) != n) {
519 if (feof(self->fp)) {
520 PyErr_SetNone(PyExc_EOFError);
521 return -1;
522 }
523
524 PyErr_SetFromErrno(PyExc_IOError);
525 return -1;
526 }
527
528 *s = self->buf;
529
530 return n;
531}
532
533
534static int
535readline_file(Unpicklerobject *self, char **s) {
536 int i;
537
538 if (self->buf_size == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000539 UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000540 PyErr_NoMemory();
541 return -1;
542 }
543
544 self->buf_size = 40;
545 }
546
547 i = 0;
548 while (1) {
549 for (; i < (self->buf_size - 1); i++) {
550 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
551 self->buf[i + 1] = '\0';
552 *s = self->buf;
553 return i + 1;
554 }
555 }
556
Guido van Rossum053b8df1998-11-25 16:18:00 +0000557 UNLESS (self->buf = (char *)realloc(self->buf,
Guido van Rossum60456fd1997-04-09 17:36:32 +0000558 (self->buf_size * 2) * sizeof(char))) {
559 PyErr_NoMemory();
560 return -1;
561 }
562
563 self->buf_size *= 2;
564 }
565
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000566}
567
568
569static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000570read_cStringIO(Unpicklerobject *self, char **s, int n) {
571 char *ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000572
Guido van Rossum60456fd1997-04-09 17:36:32 +0000573 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
574 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000575 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000576 }
577
Guido van Rossum60456fd1997-04-09 17:36:32 +0000578 *s = ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000579
Guido van Rossum60456fd1997-04-09 17:36:32 +0000580 return n;
581}
582
583
584static int
585readline_cStringIO(Unpicklerobject *self, char **s) {
586 int n;
587 char *ptr;
588
589 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
590 return -1;
591 }
592
593 *s = ptr;
594
595 return n;
596}
597
598
599static int
600read_other(Unpicklerobject *self, char **s, int n) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000601 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000602 int res = -1;
603
Guido van Rossum053b8df1998-11-25 16:18:00 +0000604 UNLESS (bytes = PyInt_FromLong(n)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000605
Guido van Rossum053b8df1998-11-25 16:18:00 +0000606 ARG_TUP(self, bytes);
607 if (self->arg) {
608 str = PyObject_CallObject(self->read, self->arg);
609 FREE_ARG_TUP(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000610 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000611 if (! str) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000612
613 Py_XDECREF(self->last_string);
614 self->last_string = str;
615
Guido van Rossum053b8df1998-11-25 16:18:00 +0000616 if (! (*s = PyString_AsString(str))) return -1;
617 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000618}
619
620
621static int
622readline_other(Unpicklerobject *self, char **s) {
623 PyObject *str;
624 int str_size;
625
Guido van Rossum053b8df1998-11-25 16:18:00 +0000626 UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000627 return -1;
628 }
629
Guido van Rossum053b8df1998-11-25 16:18:00 +0000630 if ((str_size = PyString_Size(str)) < 0)
631 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000632
633 Py_XDECREF(self->last_string);
634 self->last_string = str;
635
Guido van Rossum053b8df1998-11-25 16:18:00 +0000636 if (! (*s = PyString_AsString(str)))
637 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000638
639 return str_size;
640}
641
642
643static char *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000644pystrndup(char *s, int l) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000645 char *r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000646 UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
Guido van Rossum60456fd1997-04-09 17:36:32 +0000647 memcpy(r,s,l);
648 r[l]=0;
649 return r;
650}
651
652
653static int
654get(Picklerobject *self, PyObject *id) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000655 PyObject *value, *mv;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000656 long c_value;
657 char s[30];
658 int len;
659
Guido van Rossum053b8df1998-11-25 16:18:00 +0000660 UNLESS (mv = PyDict_GetItem(self->memo, id)) {
661 PyErr_SetObject(PyExc_KeyError, id);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000662 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000663 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000664
Guido van Rossum053b8df1998-11-25 16:18:00 +0000665 UNLESS (value = PyTuple_GetItem(mv, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000666 return -1;
667
Guido van Rossum053b8df1998-11-25 16:18:00 +0000668 UNLESS (PyInt_Check(value)) {
669 PyErr_SetString(PicklingError, "no int where int expected in memo");
670 return -1;
671 }
672 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000673
674 if (!self->bin) {
675 s[0] = GET;
676 sprintf(s + 1, "%ld\n", c_value);
677 len = strlen(s);
678 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000679 else if (Pdata_Check(self->file)) {
680 if (write_other(self, NULL, 0) < 0) return -1;
681 PDATA_APPEND(self->file, mv, -1);
682 return 0;
683 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000684 else {
685 if (c_value < 256) {
686 s[0] = BINGET;
687 s[1] = (int)(c_value & 0xff);
688 len = 2;
689 }
690 else {
691 s[0] = LONG_BINGET;
692 s[1] = (int)(c_value & 0xff);
693 s[2] = (int)((c_value >> 8) & 0xff);
694 s[3] = (int)((c_value >> 16) & 0xff);
695 s[4] = (int)((c_value >> 24) & 0xff);
696 len = 5;
697 }
698 }
699
700 if ((*self->write_func)(self, s, len) < 0)
701 return -1;
702
703 return 0;
704}
705
706
707static int
708put(Picklerobject *self, PyObject *ob) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000709 if (ob->ob_refcnt < 2 || self->fast)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000710 return 0;
711
712 return put2(self, ob);
713}
714
715
716static int
717put2(Picklerobject *self, PyObject *ob) {
718 char c_str[30];
719 int p, len, res = -1;
720 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000721
722 if (self->fast) return 0;
723
Guido van Rossum60456fd1997-04-09 17:36:32 +0000724 if ((p = PyDict_Size(self->memo)) < 0)
725 goto finally;
726
Guido van Rossum053b8df1998-11-25 16:18:00 +0000727 p++; /* Make sure memo keys are positive! */
728
729 UNLESS (py_ob_id = PyInt_FromLong((long)ob))
730 goto finally;
731
732 UNLESS (memo_len = PyInt_FromLong(p))
733 goto finally;
734
735 UNLESS (t = PyTuple_New(2))
736 goto finally;
737
738 PyTuple_SET_ITEM(t, 0, memo_len);
739 Py_INCREF(memo_len);
740 PyTuple_SET_ITEM(t, 1, ob);
741 Py_INCREF(ob);
742
743 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
744 goto finally;
745
Guido van Rossum60456fd1997-04-09 17:36:32 +0000746 if (!self->bin) {
747 c_str[0] = PUT;
748 sprintf(c_str + 1, "%d\n", p);
749 len = strlen(c_str);
750 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000751 else if (Pdata_Check(self->file)) {
752 if (write_other(self, NULL, 0) < 0) return -1;
753 PDATA_APPEND(self->file, memo_len, -1);
754 res=0; /* Job well done ;) */
755 goto finally;
756 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000757 else {
758 if (p >= 256) {
759 c_str[0] = LONG_BINPUT;
760 c_str[1] = (int)(p & 0xff);
761 c_str[2] = (int)((p >> 8) & 0xff);
762 c_str[3] = (int)((p >> 16) & 0xff);
763 c_str[4] = (int)((p >> 24) & 0xff);
764 len = 5;
765 }
766 else {
767 c_str[0] = BINPUT;
768 c_str[1] = p;
769 len = 2;
770 }
771 }
772
773 if ((*self->write_func)(self, c_str, len) < 0)
774 goto finally;
775
Guido van Rossum60456fd1997-04-09 17:36:32 +0000776 res = 0;
777
778finally:
779 Py_XDECREF(py_ob_id);
780 Py_XDECREF(memo_len);
781 Py_XDECREF(t);
782
783 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000784}
785
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000786#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000787
788static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000789PyImport_Import(PyObject *module_name) {
790 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
791 static PyObject *standard_builtins=0;
792 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
793
Guido van Rossum053b8df1998-11-25 16:18:00 +0000794 UNLESS (silly_list) {
795 UNLESS (__import___str=PyString_FromString("__import__"))
796 return NULL;
797 UNLESS (__builtins___str=PyString_FromString("__builtins__"))
798 return NULL;
799 UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
800 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000801 }
802
Guido van Rossum053b8df1998-11-25 16:18:00 +0000803 if ((globals=PyEval_GetGlobals())) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000804 Py_INCREF(globals);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000805 UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
806 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000807 }
808 else {
809 PyErr_Clear();
810
Guido van Rossum053b8df1998-11-25 16:18:00 +0000811 UNLESS (standard_builtins ||
812 (standard_builtins=PyImport_ImportModule("__builtin__")))
813 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000814
815 __builtins__=standard_builtins;
816 Py_INCREF(__builtins__);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000817 UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
818 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000819 }
820
Guido van Rossum053b8df1998-11-25 16:18:00 +0000821 if (PyDict_Check(__builtins__)) {
822 UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000823 }
824 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000825 UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000826 }
827
Guido van Rossum053b8df1998-11-25 16:18:00 +0000828 UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
829 module_name, globals, globals, silly_list))
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000830 goto err;
831
832 Py_DECREF(globals);
833 Py_DECREF(__builtins__);
834 Py_DECREF(__import__);
835
836 return r;
837err:
838 Py_XDECREF(globals);
839 Py_XDECREF(__builtins__);
840 Py_XDECREF(__import__);
841 return NULL;
842}
843
844static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +0000845whichmodule(PyObject *global, PyObject *global_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000846 int i, j;
847 PyObject *module = 0, *modules_dict = 0,
848 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000849
Guido van Rossum45188231997-09-28 05:38:51 +0000850 module = PyObject_GetAttrString(global, "__module__");
851 if (module) return module;
852 PyErr_Clear();
853
Guido van Rossum053b8df1998-11-25 16:18:00 +0000854 UNLESS (modules_dict = PySys_GetObject("modules"))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000855 return NULL;
856
Guido van Rossum60456fd1997-04-09 17:36:32 +0000857 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000858 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
859
Guido van Rossum053b8df1998-11-25 16:18:00 +0000860 if (PyObject_Compare(name, __main___str)==0) continue;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000861
Guido van Rossum053b8df1998-11-25 16:18:00 +0000862 UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000863 PyErr_Clear();
864 continue;
865 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000866
Guido van Rossum60456fd1997-04-09 17:36:32 +0000867 if (global_name_attr != global) {
868 Py_DECREF(global_name_attr);
869 continue;
870 }
871
872 Py_DECREF(global_name_attr);
873
874 break;
875 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000876
877 /* The following implements the rule in pickle.py added in 1.5
878 that used __main__ if no module is found. I don't actually
879 like this rule. jlf
880 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000881 if (!j) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000882 j=1;
883 name=__main___str;
884 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000885
886 Py_INCREF(name);
887 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000888}
889
890
Guido van Rossum60456fd1997-04-09 17:36:32 +0000891static int
892save_none(Picklerobject *self, PyObject *args) {
893 static char none = NONE;
894 if ((*self->write_func)(self, &none, 1) < 0)
895 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000896
Guido van Rossum60456fd1997-04-09 17:36:32 +0000897 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000898}
899
900
Guido van Rossum60456fd1997-04-09 17:36:32 +0000901static int
902save_int(Picklerobject *self, PyObject *args) {
903 char c_str[32];
904 long l = PyInt_AS_LONG((PyIntObject *)args);
905 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000906
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000907 if (!self->bin
908#if SIZEOF_LONG > 4
Guido van Rossum053b8df1998-11-25 16:18:00 +0000909 || (l >> 32)
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000910#endif
Guido van Rossum053b8df1998-11-25 16:18:00 +0000911 ) {
912 /* Save extra-long ints in non-binary mode, so that
913 we can use python long parsing code to restore,
914 if necessary. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000915 c_str[0] = INT;
916 sprintf(c_str + 1, "%ld\n", l);
917 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
918 return -1;
919 }
920 else {
921 c_str[1] = (int)( l & 0xff);
922 c_str[2] = (int)((l >> 8) & 0xff);
923 c_str[3] = (int)((l >> 16) & 0xff);
924 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000925
Guido van Rossum60456fd1997-04-09 17:36:32 +0000926 if ((c_str[4] == 0) && (c_str[3] == 0)) {
927 if (c_str[2] == 0) {
928 c_str[0] = BININT1;
929 len = 2;
930 }
931 else {
932 c_str[0] = BININT2;
933 len = 3;
934 }
935 }
936 else {
937 c_str[0] = BININT;
938 len = 5;
939 }
940
941 if ((*self->write_func)(self, c_str, len) < 0)
942 return -1;
943 }
944
945 return 0;
946}
947
948
949static int
950save_long(Picklerobject *self, PyObject *args) {
951 int size, res = -1;
952 PyObject *repr = 0;
953
954 static char l = LONG;
955
Guido van Rossum053b8df1998-11-25 16:18:00 +0000956 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000957 goto finally;
958
959 if ((size = PyString_Size(repr)) < 0)
960 goto finally;
961
962 if ((*self->write_func)(self, &l, 1) < 0)
963 goto finally;
964
965 if ((*self->write_func)(self,
966 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
967 goto finally;
968
969 if ((*self->write_func)(self, "\n", 1) < 0)
970 goto finally;
971
972 res = 0;
973
974finally:
975 Py_XDECREF(repr);
976
977 return res;
978}
979
980
981static int
982save_float(Picklerobject *self, PyObject *args) {
983 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
984
Guido van Rossum60456fd1997-04-09 17:36:32 +0000985 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000986 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000987 double f;
988 long fhi, flo;
989 char str[9], *p = str;
990
991 *p = BINFLOAT;
992 p++;
993
994 if (x < 0) {
995 s = 1;
996 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000997 }
998 else
Guido van Rossum60456fd1997-04-09 17:36:32 +0000999 s = 0;
1000
1001 f = frexp(x, &e);
1002
1003 /* Normalize f to be in the range [1.0, 2.0) */
1004 if (0.5 <= f && f < 1.0) {
1005 f *= 2.0;
1006 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001007 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001008 else if (f == 0.0) {
1009 e = 0;
1010 }
1011 else {
1012 PyErr_SetString(PyExc_SystemError,
1013 "frexp() result out of range");
1014 return -1;
1015 }
1016
1017 if (e >= 1024) {
1018 /* XXX 1024 itself is reserved for Inf/NaN */
1019 PyErr_SetString(PyExc_OverflowError,
1020 "float too large to pack with d format");
1021 return -1;
1022 }
1023 else if (e < -1022) {
1024 /* Gradual underflow */
1025 f = ldexp(f, 1022 + e);
1026 e = 0;
1027 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001028 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001029 e += 1023;
1030 f -= 1.0; /* Get rid of leading 1 */
1031 }
1032
1033 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1034 f *= 268435456.0; /* 2**28 */
1035 fhi = (long) floor(f); /* Truncate */
1036 f -= (double)fhi;
1037 f *= 16777216.0; /* 2**24 */
1038 flo = (long) floor(f + 0.5); /* Round */
1039
1040 /* First byte */
1041 *p = (s<<7) | (e>>4);
1042 p++;
1043
1044 /* Second byte */
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001045 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum60456fd1997-04-09 17:36:32 +00001046 p++;
1047
1048 /* Third byte */
1049 *p = (fhi>>16) & 0xFF;
1050 p++;
1051
1052 /* Fourth byte */
1053 *p = (fhi>>8) & 0xFF;
1054 p++;
1055
1056 /* Fifth byte */
1057 *p = fhi & 0xFF;
1058 p++;
1059
1060 /* Sixth byte */
1061 *p = (flo>>16) & 0xFF;
1062 p++;
1063
1064 /* Seventh byte */
1065 *p = (flo>>8) & 0xFF;
1066 p++;
1067
1068 /* Eighth byte */
1069 *p = flo & 0xFF;
1070
1071 if ((*self->write_func)(self, str, 9) < 0)
1072 return -1;
1073 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001074 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001075 char c_str[250];
1076 c_str[0] = FLOAT;
Guido van Rossum104be4a1998-04-03 21:13:02 +00001077 sprintf(c_str + 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001078
1079 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
1080 return -1;
1081 }
1082
1083 return 0;
1084}
1085
1086
1087static int
Guido van Rossum142eeb81997-08-13 03:14:41 +00001088save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001089 int size, len;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001090 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001091
Guido van Rossum053b8df1998-11-25 16:18:00 +00001092 if ((size = PyString_Size(args)) < 0)
1093 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001094
1095 if (!self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001096 char *repr_str;
1097
1098 static char string = STRING;
1099
Guido van Rossum053b8df1998-11-25 16:18:00 +00001100 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001101 return -1;
1102
Guido van Rossum053b8df1998-11-25 16:18:00 +00001103 if ((len = PyString_Size(repr)) < 0)
1104 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001105 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001106
1107 if ((*self->write_func)(self, &string, 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001108 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001109
1110 if ((*self->write_func)(self, repr_str, len) < 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, "\n", 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001114 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001115
1116 Py_XDECREF(repr);
1117 }
1118 else {
1119 int i;
1120 char c_str[5];
1121
Guido van Rossum053b8df1998-11-25 16:18:00 +00001122 if ((size = PyString_Size(args)) < 0)
1123 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001124
1125 if (size < 256) {
1126 c_str[0] = SHORT_BINSTRING;
1127 c_str[1] = size;
1128 len = 2;
1129 }
1130 else {
1131 c_str[0] = BINSTRING;
1132 for (i = 1; i < 5; i++)
1133 c_str[i] = (int)(size >> ((i - 1) * 8));
1134 len = 5;
1135 }
1136
1137 if ((*self->write_func)(self, c_str, len) < 0)
1138 return -1;
1139
Guido van Rossum053b8df1998-11-25 16:18:00 +00001140 if (size > 128 && Pdata_Check(self->file)) {
1141 if (write_other(self, NULL, 0) < 0) return -1;
1142 PDATA_APPEND(self->file, args, -1);
1143 }
1144 else {
1145 if ((*self->write_func)(self,
1146 PyString_AS_STRING((PyStringObject *)args), size) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001147 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001148 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001149 }
1150
Guido van Rossum142eeb81997-08-13 03:14:41 +00001151 if (doput)
1152 if (put(self, args) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001153 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001154
1155 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001156
1157err:
1158 Py_XDECREF(repr);
1159 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001160}
1161
1162
1163static int
1164save_tuple(Picklerobject *self, PyObject *args) {
1165 PyObject *element = 0, *py_tuple_id = 0;
1166 int len, i, has_key, res = -1;
1167
1168 static char tuple = TUPLE;
1169
1170 if ((*self->write_func)(self, &MARKv, 1) < 0)
1171 goto finally;
1172
1173 if ((len = PyTuple_Size(args)) < 0)
1174 goto finally;
1175
1176 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001177 UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001178 goto finally;
1179
1180 if (save(self, element, 0) < 0)
1181 goto finally;
1182 }
1183
Guido van Rossum053b8df1998-11-25 16:18:00 +00001184 UNLESS (py_tuple_id = PyInt_FromLong((long)args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001185 goto finally;
1186
1187 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001188 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001189 goto finally;
1190
1191 if (has_key) {
1192 if (self->bin) {
1193 static char pop_mark = POP_MARK;
1194
1195 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1196 goto finally;
1197 }
1198 else {
1199 static char pop = POP;
1200
1201 for (i = 0; i <= len; i++) {
1202 if ((*self->write_func)(self, &pop, 1) < 0)
1203 goto finally;
1204 }
1205 }
1206
1207 if (get(self, py_tuple_id) < 0)
1208 goto finally;
1209
1210 res = 0;
1211 goto finally;
1212 }
1213 }
1214
1215 if ((*self->write_func)(self, &tuple, 1) < 0) {
1216 goto finally;
1217 }
1218
1219 if (put(self, args) < 0)
1220 goto finally;
1221
1222 res = 0;
1223
1224finally:
1225 Py_XDECREF(py_tuple_id);
1226
1227 return res;
1228}
1229
1230static int
1231save_empty_tuple(Picklerobject *self, PyObject *args) {
1232 static char tuple = EMPTY_TUPLE;
1233
1234 return (*self->write_func)(self, &tuple, 1);
1235}
1236
1237
1238static int
1239save_list(Picklerobject *self, PyObject *args) {
1240 PyObject *element = 0;
1241 int s_len, len, i, using_appends, res = -1;
1242 char s[3];
1243
1244 static char append = APPEND, appends = APPENDS;
1245
Guido van Rossum053b8df1998-11-25 16:18:00 +00001246 if (self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001247 s[0] = EMPTY_LIST;
1248 s_len = 1;
1249 }
1250 else {
1251 s[0] = MARK;
1252 s[1] = LIST;
1253 s_len = 2;
1254 }
1255
1256 if ((len = PyList_Size(args)) < 0)
1257 goto finally;
1258
1259 if ((*self->write_func)(self, s, s_len) < 0)
1260 goto finally;
1261
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001262 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001263 if (put(self, args) < 0)
1264 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001265 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001266 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001267 if (put2(self, args) < 0)
1268 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001269 }
1270
Guido van Rossum142eeb81997-08-13 03:14:41 +00001271 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001272 if ((*self->write_func)(self, &MARKv, 1) < 0)
1273 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001274
Guido van Rossum60456fd1997-04-09 17:36:32 +00001275 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001276 UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001277 goto finally;
1278
1279 if (save(self, element, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001280 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001281
1282 if (!using_appends) {
1283 if ((*self->write_func)(self, &append, 1) < 0)
1284 goto finally;
1285 }
1286 }
1287
1288 if (using_appends) {
1289 if ((*self->write_func)(self, &appends, 1) < 0)
1290 goto finally;
1291 }
1292
1293 res = 0;
1294
1295finally:
1296
1297 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001298}
1299
1300
Guido van Rossum60456fd1997-04-09 17:36:32 +00001301static int
1302save_dict(Picklerobject *self, PyObject *args) {
1303 PyObject *key = 0, *value = 0;
1304 int i, len, res = -1, using_setitems;
1305 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001306
Guido van Rossum60456fd1997-04-09 17:36:32 +00001307 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001308
Guido van Rossum60456fd1997-04-09 17:36:32 +00001309 if (self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001310 s[0] = EMPTY_DICT;
1311 len = 1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001312 }
1313 else {
1314 s[0] = MARK;
1315 s[1] = DICT;
1316 len = 2;
1317 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001318
Guido van Rossum60456fd1997-04-09 17:36:32 +00001319 if ((*self->write_func)(self, s, len) < 0)
1320 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001321
Guido van Rossum60456fd1997-04-09 17:36:32 +00001322 if ((len = PyDict_Size(args)) < 0)
1323 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001324
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001325 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001326 if (put(self, args) < 0)
1327 goto finally;
1328 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001329 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001330 if (put2(self, args) < 0)
1331 goto finally;
1332 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001333
Guido van Rossum142eeb81997-08-13 03:14:41 +00001334 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001335 if ((*self->write_func)(self, &MARKv, 1) < 0)
1336 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001337
Guido van Rossum60456fd1997-04-09 17:36:32 +00001338 i = 0;
1339 while (PyDict_Next(args, &i, &key, &value)) {
1340 if (save(self, key, 0) < 0)
1341 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001342
Guido van Rossum60456fd1997-04-09 17:36:32 +00001343 if (save(self, value, 0) < 0)
1344 goto finally;
1345
1346 if (!using_setitems) {
1347 if ((*self->write_func)(self, &setitem, 1) < 0)
1348 goto finally;
1349 }
1350 }
1351
1352 if (using_setitems) {
1353 if ((*self->write_func)(self, &setitems, 1) < 0)
1354 goto finally;
1355 }
1356
1357 res = 0;
1358
1359finally:
1360
1361 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001362}
1363
1364
Guido van Rossum60456fd1997-04-09 17:36:32 +00001365static int
1366save_inst(Picklerobject *self, PyObject *args) {
1367 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1368 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1369 char *module_str, *name_str;
1370 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001371
Guido van Rossum60456fd1997-04-09 17:36:32 +00001372 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001373
Guido van Rossum60456fd1997-04-09 17:36:32 +00001374 if ((*self->write_func)(self, &MARKv, 1) < 0)
1375 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001376
Guido van Rossum053b8df1998-11-25 16:18:00 +00001377 UNLESS (class = PyObject_GetAttr(args, __class___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001378 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001379
Guido van Rossum60456fd1997-04-09 17:36:32 +00001380 if (self->bin) {
1381 if (save(self, class, 0) < 0)
1382 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001383 }
1384
Guido van Rossum142eeb81997-08-13 03:14:41 +00001385 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001386 PyObject *element = 0;
1387 int i, len;
1388
Guido van Rossum053b8df1998-11-25 16:18:00 +00001389 UNLESS (class_args =
Guido van Rossum60456fd1997-04-09 17:36:32 +00001390 PyObject_CallObject(getinitargs_func, empty_tuple))
1391 goto finally;
1392
1393 if ((len = PyObject_Length(class_args)) < 0)
1394 goto finally;
1395
1396 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001397 UNLESS (element = PySequence_GetItem(class_args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001398 goto finally;
1399
1400 if (save(self, element, 0) < 0) {
1401 Py_DECREF(element);
1402 goto finally;
1403 }
1404
1405 Py_DECREF(element);
1406 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001407 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001408 else {
1409 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001410 }
1411
Guido van Rossum60456fd1997-04-09 17:36:32 +00001412 if (!self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001413 UNLESS (name = ((PyClassObject *)class)->cl_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001414 PyErr_SetString(PicklingError, "class has no name");
1415 goto finally;
1416 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001417
Guido van Rossum053b8df1998-11-25 16:18:00 +00001418 UNLESS (module = whichmodule(class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001419 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001420
1421
1422 if ((module_size = PyString_Size(module)) < 0 ||
1423 (name_size = PyString_Size(name)) < 0)
1424 goto finally;
1425
Guido van Rossum60456fd1997-04-09 17:36:32 +00001426 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001427 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001428
Guido van Rossum60456fd1997-04-09 17:36:32 +00001429 if ((*self->write_func)(self, &inst, 1) < 0)
1430 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001431
Guido van Rossum60456fd1997-04-09 17:36:32 +00001432 if ((*self->write_func)(self, module_str, module_size) < 0)
1433 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001434
Guido van Rossum60456fd1997-04-09 17:36:32 +00001435 if ((*self->write_func)(self, "\n", 1) < 0)
1436 goto finally;
1437
1438 if ((*self->write_func)(self, name_str, name_size) < 0)
1439 goto finally;
1440
1441 if ((*self->write_func)(self, "\n", 1) < 0)
1442 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001443 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001444 else if ((*self->write_func)(self, &obj, 1) < 0) {
1445 goto finally;
1446 }
1447
Guido van Rossum142eeb81997-08-13 03:14:41 +00001448 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001449 UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001450 goto finally;
1451 }
1452 else {
1453 PyErr_Clear();
1454
Guido van Rossum053b8df1998-11-25 16:18:00 +00001455 UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001456 PyErr_Clear();
1457 res = 0;
1458 goto finally;
1459 }
1460 }
1461
1462 if (!PyDict_Check(state)) {
1463 if (put2(self, args) < 0)
1464 goto finally;
1465 }
1466 else {
1467 if (put(self, args) < 0)
1468 goto finally;
1469 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001470
Guido van Rossum60456fd1997-04-09 17:36:32 +00001471 if (save(self, state, 0) < 0)
1472 goto finally;
1473
1474 if ((*self->write_func)(self, &build, 1) < 0)
1475 goto finally;
1476
1477 res = 0;
1478
1479finally:
1480 Py_XDECREF(module);
1481 Py_XDECREF(class);
1482 Py_XDECREF(state);
1483 Py_XDECREF(getinitargs_func);
1484 Py_XDECREF(getstate_func);
1485 Py_XDECREF(class_args);
1486
1487 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001488}
1489
1490
Guido van Rossum60456fd1997-04-09 17:36:32 +00001491static int
1492save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1493 PyObject *global_name = 0, *module = 0;
1494 char *name_str, *module_str;
1495 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001496
Guido van Rossum60456fd1997-04-09 17:36:32 +00001497 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001498
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001499 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001500 global_name = name;
1501 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001502 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001503 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001504 UNLESS (global_name = PyObject_GetAttr(args, __name___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001505 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001506 }
1507
Guido van Rossum053b8df1998-11-25 16:18:00 +00001508 UNLESS (module = whichmodule(args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001509 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001510
Guido van Rossum053b8df1998-11-25 16:18:00 +00001511 if ((module_size = PyString_Size(module)) < 0 ||
1512 (name_size = PyString_Size(global_name)) < 0)
1513 goto finally;
1514
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001515 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001516 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001517
Guido van Rossum60456fd1997-04-09 17:36:32 +00001518 if ((*self->write_func)(self, &global, 1) < 0)
1519 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001520
Guido van Rossum60456fd1997-04-09 17:36:32 +00001521 if ((*self->write_func)(self, module_str, module_size) < 0)
1522 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001523
Guido van Rossum60456fd1997-04-09 17:36:32 +00001524 if ((*self->write_func)(self, "\n", 1) < 0)
1525 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001526
Guido van Rossum60456fd1997-04-09 17:36:32 +00001527 if ((*self->write_func)(self, name_str, name_size) < 0)
1528 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001529
Guido van Rossum60456fd1997-04-09 17:36:32 +00001530 if ((*self->write_func)(self, "\n", 1) < 0)
1531 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001532
Guido van Rossum60456fd1997-04-09 17:36:32 +00001533 if (put(self, args) < 0)
1534 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001535
Guido van Rossum60456fd1997-04-09 17:36:32 +00001536 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001537
Guido van Rossum60456fd1997-04-09 17:36:32 +00001538finally:
1539 Py_XDECREF(module);
1540 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001541
Guido van Rossum60456fd1997-04-09 17:36:32 +00001542 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001543}
1544
Guido van Rossum60456fd1997-04-09 17:36:32 +00001545static int
1546save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1547 PyObject *pid = 0;
1548 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001549
Guido van Rossum60456fd1997-04-09 17:36:32 +00001550 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001551
Guido van Rossum053b8df1998-11-25 16:18:00 +00001552 Py_INCREF(args);
1553 ARG_TUP(self, args);
1554 if (self->arg) {
1555 pid = PyObject_CallObject(f, self->arg);
1556 FREE_ARG_TUP(self);
1557 }
1558 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001559
Guido van Rossum60456fd1997-04-09 17:36:32 +00001560 if (pid != Py_None) {
1561 if (!self->bin) {
1562 if (!PyString_Check(pid)) {
1563 PyErr_SetString(PicklingError,
1564 "persistent id must be string");
1565 goto finally;
1566 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001567
Guido van Rossum60456fd1997-04-09 17:36:32 +00001568 if ((*self->write_func)(self, &persid, 1) < 0)
1569 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001570
Guido van Rossum60456fd1997-04-09 17:36:32 +00001571 if ((size = PyString_Size(pid)) < 0)
1572 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001573
Guido van Rossum60456fd1997-04-09 17:36:32 +00001574 if ((*self->write_func)(self,
1575 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1576 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001577
Guido van Rossum60456fd1997-04-09 17:36:32 +00001578 if ((*self->write_func)(self, "\n", 1) < 0)
1579 goto finally;
1580
1581 res = 1;
1582 goto finally;
1583 }
1584 else if (save(self, pid, 1) >= 0) {
1585 if ((*self->write_func)(self, &binpersid, 1) < 0)
1586 res = -1;
1587 else
1588 res = 1;
1589 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001590
Guido van Rossum60456fd1997-04-09 17:36:32 +00001591 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001592 }
1593
Guido van Rossum60456fd1997-04-09 17:36:32 +00001594 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001595
Guido van Rossum60456fd1997-04-09 17:36:32 +00001596finally:
1597 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001598
Guido van Rossum60456fd1997-04-09 17:36:32 +00001599 return res;
1600}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001601
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001602
Guido van Rossum60456fd1997-04-09 17:36:32 +00001603static int
1604save_reduce(Picklerobject *self, PyObject *callable,
1605 PyObject *tup, PyObject *state, PyObject *ob) {
1606 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001607
Guido van Rossum60456fd1997-04-09 17:36:32 +00001608 if (save(self, callable, 0) < 0)
1609 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001610
Guido van Rossum60456fd1997-04-09 17:36:32 +00001611 if (save(self, tup, 0) < 0)
1612 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001613
Guido van Rossum60456fd1997-04-09 17:36:32 +00001614 if ((*self->write_func)(self, &reduce, 1) < 0)
1615 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001616
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001617 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001618 if (state && !PyDict_Check(state)) {
1619 if (put2(self, ob) < 0)
1620 return -1;
1621 }
1622 else {
1623 if (put(self, ob) < 0)
1624 return -1;
1625 }
1626 }
1627
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001628 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001629 if (save(self, state, 0) < 0)
1630 return -1;
1631
1632 if ((*self->write_func)(self, &build, 1) < 0)
1633 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001634 }
1635
Guido van Rossum60456fd1997-04-09 17:36:32 +00001636 return 0;
1637}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001638
Guido van Rossum60456fd1997-04-09 17:36:32 +00001639static int
1640save(Picklerobject *self, PyObject *args, int pers_save) {
1641 PyTypeObject *type;
1642 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001643 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001644 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001645
Guido van Rossum60456fd1997-04-09 17:36:32 +00001646 if (!pers_save && self->pers_func) {
1647 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1648 res = tmp;
1649 goto finally;
1650 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001651 }
1652
Guido van Rossum60456fd1997-04-09 17:36:32 +00001653 if (args == Py_None) {
1654 res = save_none(self, args);
1655 goto finally;
1656 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001657
Guido van Rossum60456fd1997-04-09 17:36:32 +00001658 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001659
Guido van Rossum60456fd1997-04-09 17:36:32 +00001660 switch (type->tp_name[0]) {
1661 case 'i':
1662 if (type == &PyInt_Type) {
1663 res = save_int(self, args);
1664 goto finally;
1665 }
1666 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001667
Guido van Rossum60456fd1997-04-09 17:36:32 +00001668 case 'l':
1669 if (type == &PyLong_Type) {
1670 res = save_long(self, args);
1671 goto finally;
1672 }
1673 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001674
Guido van Rossum60456fd1997-04-09 17:36:32 +00001675 case 'f':
1676 if (type == &PyFloat_Type) {
1677 res = save_float(self, args);
1678 goto finally;
1679 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001680 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001681
Guido van Rossum60456fd1997-04-09 17:36:32 +00001682 case 't':
1683 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001684 if (self->bin) res = save_empty_tuple(self, args);
1685 else res = save_tuple(self, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001686 goto finally;
1687 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001688 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001689
Guido van Rossum60456fd1997-04-09 17:36:32 +00001690 case 's':
Guido van Rossum053b8df1998-11-25 16:18:00 +00001691 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001692 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001693 goto finally;
1694 }
1695 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001696
Guido van Rossum60456fd1997-04-09 17:36:32 +00001697 if (args->ob_refcnt > 1) {
1698 long ob_id;
1699 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001700
Guido van Rossum60456fd1997-04-09 17:36:32 +00001701 ob_id = (long)args;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001702
Guido van Rossum053b8df1998-11-25 16:18:00 +00001703 UNLESS (py_ob_id = PyInt_FromLong(ob_id))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001704 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001705
Guido van Rossum60456fd1997-04-09 17:36:32 +00001706 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1707 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001708
Guido van Rossum60456fd1997-04-09 17:36:32 +00001709 if (has_key) {
1710 if (get(self, py_ob_id) < 0)
1711 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001712
Guido van Rossum60456fd1997-04-09 17:36:32 +00001713 res = 0;
1714 goto finally;
1715 }
1716 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001717
Guido van Rossum60456fd1997-04-09 17:36:32 +00001718 switch (type->tp_name[0]) {
1719 case 's':
1720 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001721 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001722 goto finally;
1723 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001724 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001725
Guido van Rossum60456fd1997-04-09 17:36:32 +00001726 case 't':
1727 if (type == &PyTuple_Type) {
1728 res = save_tuple(self, args);
1729 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001730 }
1731 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001732
Guido van Rossum60456fd1997-04-09 17:36:32 +00001733 case 'l':
1734 if (type == &PyList_Type) {
1735 res = save_list(self, args);
1736 goto finally;
1737 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001738 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001739
1740 case 'd':
1741 if (type == &PyDict_Type) {
1742 res = save_dict(self, args);
1743 goto finally;
1744 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001745 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001746
1747 case 'i':
1748 if (type == &PyInstance_Type) {
1749 res = save_inst(self, args);
1750 goto finally;
1751 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001752 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001753
1754 case 'c':
1755 if (type == &PyClass_Type) {
1756 res = save_global(self, args, NULL);
1757 goto finally;
1758 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001759 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001760
1761 case 'f':
1762 if (type == &PyFunction_Type) {
1763 res = save_global(self, args, NULL);
1764 goto finally;
1765 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001766 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001767
1768 case 'b':
1769 if (type == &PyCFunction_Type) {
1770 res = save_global(self, args, NULL);
1771 goto finally;
1772 }
1773 }
1774
1775 if (!pers_save && self->inst_pers_func) {
1776 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1777 res = tmp;
1778 goto finally;
1779 }
1780 }
1781
Guido van Rossum142eeb81997-08-13 03:14:41 +00001782 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001783 Py_INCREF(__reduce__);
1784
Guido van Rossum60456fd1997-04-09 17:36:32 +00001785 Py_INCREF(args);
Guido van Rossum053b8df1998-11-25 16:18:00 +00001786 ARG_TUP(self, args);
1787 if (self->arg) {
1788 t = PyObject_CallObject(__reduce__, self->arg);
1789 FREE_ARG_TUP(self);
1790 }
1791 if (! t) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001792 }
1793 else {
1794 PyErr_Clear();
1795
Guido van Rossum142eeb81997-08-13 03:14:41 +00001796 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001797 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001798 goto finally;
1799 }
1800 else {
1801 PyErr_Clear();
1802 }
1803 }
1804
1805 if (t) {
1806 if (PyString_Check(t)) {
1807 res = save_global(self, args, t);
1808 goto finally;
1809 }
1810
1811 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001812 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001813 "be a tuple", "O", __reduce__);
1814 goto finally;
1815 }
1816
1817 size = PyTuple_Size(t);
1818
1819 if ((size != 3) && (size != 2)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001820 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001821 "contain only two or three elements", "O", __reduce__);
1822 goto finally;
1823 }
1824
1825 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001826
Guido van Rossum60456fd1997-04-09 17:36:32 +00001827 arg_tup = PyTuple_GET_ITEM(t, 1);
1828
1829 if (size > 2) {
1830 state = PyTuple_GET_ITEM(t, 2);
1831 }
1832
Guido van Rossum053b8df1998-11-25 16:18:00 +00001833 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001834 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001835 "returned by %s must be a tuple", "O", __reduce__);
1836 goto finally;
1837 }
1838
1839 res = save_reduce(self, callable, arg_tup, state, args);
1840 goto finally;
1841 }
1842
Guido van Rossumc03158b1999-06-09 15:23:31 +00001843 PyErr_SetObject(UnpickleableError, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001844
1845finally:
1846 Py_XDECREF(py_ob_id);
1847 Py_XDECREF(__reduce__);
1848 Py_XDECREF(t);
1849
1850 return res;
1851}
1852
1853
1854static int
1855dump(Picklerobject *self, PyObject *args) {
1856 static char stop = STOP;
1857
1858 if (save(self, args, 0) < 0)
1859 return -1;
1860
1861 if ((*self->write_func)(self, &stop, 1) < 0)
1862 return -1;
1863
1864 if ((*self->write_func)(self, NULL, 0) < 0)
1865 return -1;
1866
1867 return 0;
1868}
1869
1870static PyObject *
Guido van Rossum053b8df1998-11-25 16:18:00 +00001871Pickle_clear_memo(Picklerobject *self, PyObject *args) {
1872 if (args && ! PyArg_ParseTuple(args,"")) return NULL;
1873 if (self->memo) PyDict_Clear(self->memo);
1874 Py_INCREF(Py_None);
1875 return Py_None;
1876}
1877
1878static PyObject *
1879Pickle_getvalue(Picklerobject *self, PyObject *args) {
1880 int l, i, rsize, ssize, clear=1, lm;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001881 long ik;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001882 PyObject *k, *r;
1883 char *s, *p, *have_get;
1884 Pdata *data;
1885
1886 if (args && ! PyArg_ParseTuple(args,"|i",&clear)) return NULL;
1887
1888 /* Check to make sure we are based on a list */
1889 if (! Pdata_Check(self->file)) {
1890 PyErr_SetString(PicklingError,
1891 "Attempt to getvalue a non-list-based pickler");
1892 return NULL;
1893 }
1894
1895 /* flush write buffer */
1896 if (write_other(self, NULL, 0) < 0) return NULL;
1897
1898 data=(Pdata*)self->file;
1899 l=data->length;
1900
1901 /* set up an array to hold get/put status */
1902 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
1903 lm++;
1904 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
1905 memset(have_get,0,lm);
1906
1907 /* Scan for gets. */
1908 for (rsize=0, i=l; --i >= 0; ) {
1909 k=data->data[i];
1910
1911 if (PyString_Check(k)) {
1912 rsize += PyString_GET_SIZE(k);
1913 }
1914
1915 else if (PyInt_Check(k)) { /* put */
1916 ik=PyInt_AS_LONG((PyIntObject*)k);
1917 if (ik >= lm || ik==0) {
1918 PyErr_SetString(PicklingError,
1919 "Invalid get data");
1920 return NULL;
1921 }
1922 if (have_get[ik]) { /* with matching get */
1923 if (ik < 256) rsize += 2;
1924 else rsize+=5;
1925 }
1926 }
1927
1928 else if (! (PyTuple_Check(k) &&
1929 PyTuple_GET_SIZE(k) == 2 &&
1930 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
1931 ) {
1932 PyErr_SetString(PicklingError,
1933 "Unexpected data in internal list");
1934 return NULL;
1935 }
1936
1937 else { /* put */
1938 ik=PyInt_AS_LONG((PyIntObject*)k);
1939 if (ik >= lm || ik==0) {
1940 PyErr_SetString(PicklingError,
1941 "Invalid get data");
1942 return NULL;
1943 }
1944 have_get[ik]=1;
1945 if (ik < 256) rsize += 2;
1946 else rsize+=5;
1947 }
1948
1949 }
1950
1951 /* Now generate the result */
1952 UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
1953 s=PyString_AS_STRING((PyStringObject*)r);
1954
1955 for (i=0; i<l; i++) {
1956 k=data->data[i];
1957
1958 if (PyString_Check(k)) {
1959 ssize=PyString_GET_SIZE(k);
1960 if (ssize) {
1961 p=PyString_AS_STRING((PyStringObject*)k);
1962 while (--ssize >= 0) *s++=*p++;
1963 }
1964 }
1965
1966 else if (PyTuple_Check(k)) { /* get */
1967 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
1968 if (ik < 256) {
1969 *s++ = BINGET;
1970 *s++ = (int)(ik & 0xff);
1971 }
1972 else {
1973 *s++ = LONG_BINGET;
1974 *s++ = (int)(ik & 0xff);
1975 *s++ = (int)((ik >> 8) & 0xff);
1976 *s++ = (int)((ik >> 16) & 0xff);
1977 *s++ = (int)((ik >> 24) & 0xff);
1978 }
1979 }
1980
1981 else { /* put */
1982 ik=PyInt_AS_LONG((PyIntObject*)k);
1983
1984 if (have_get[ik]) { /* with matching get */
1985 if (ik < 256) {
1986 *s++ = BINPUT;
1987 *s++ = (int)(ik & 0xff);
1988 }
1989 else {
1990 *s++ = LONG_BINPUT;
1991 *s++ = (int)(ik & 0xff);
1992 *s++ = (int)((ik >> 8) & 0xff);
1993 *s++ = (int)((ik >> 16) & 0xff);
1994 *s++ = (int)((ik >> 24) & 0xff);
1995 }
1996 }
1997 }
1998
1999 }
2000
2001 if (clear) {
2002 PyDict_Clear(self->memo);
2003 Pdata_clear(data,0);
2004 }
2005
2006 free(have_get);
2007 return r;
2008err:
2009 free(have_get);
2010 return NULL;
2011}
2012
2013static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002014Pickler_dump(Picklerobject *self, PyObject *args) {
2015 PyObject *ob;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002016 int get=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002017
Guido van Rossum053b8df1998-11-25 16:18:00 +00002018 UNLESS (PyArg_ParseTuple(args, "O|i", &ob, &get))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002019 return NULL;
2020
2021 if (dump(self, ob) < 0)
2022 return NULL;
2023
Guido van Rossum053b8df1998-11-25 16:18:00 +00002024 if (get) return Pickle_getvalue(self, NULL);
2025
2026 Py_INCREF(self);
2027 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002028}
2029
2030
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002031static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00002032 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002033 "dump(object) --"
2034 "Write an object in pickle format to the object's pickle stream\n"
2035 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00002036 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002037 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum053b8df1998-11-25 16:18:00 +00002038 {"getvalue", (PyCFunction)Pickle_getvalue, 1,
2039 "getvalue() -- Finish picking a list-based pickle"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002040 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002041};
2042
2043
2044static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002045newPicklerobject(PyObject *file, int bin) {
2046 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002047
Guido van Rossum053b8df1998-11-25 16:18:00 +00002048 UNLESS (self = PyObject_NEW(Picklerobject, &Picklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002049 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002050
2051 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002052 self->write = NULL;
2053 self->memo = NULL;
2054 self->arg = NULL;
2055 self->pers_func = NULL;
2056 self->inst_pers_func = NULL;
2057 self->write_buf = NULL;
2058 self->bin = bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002059 self->fast = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002060 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002061 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002062
Guido van Rossum053b8df1998-11-25 16:18:00 +00002063 if (file)
2064 Py_INCREF(file);
2065 else
Guido van Rossum50f385c1998-12-04 18:48:44 +00002066 file=Pdata_New();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002067
Guido van Rossum60456fd1997-04-09 17:36:32 +00002068 self->file = file;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002069
Guido van Rossum053b8df1998-11-25 16:18:00 +00002070 UNLESS (self->memo = PyDict_New()) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002071 Py_XDECREF((PyObject *)self);
2072 return NULL;
2073 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002074
Guido van Rossum60456fd1997-04-09 17:36:32 +00002075 if (PyFile_Check(file)) {
2076 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002077 if (self->fp == NULL) {
2078 PyErr_SetString(PyExc_IOError, "output file closed");
2079 return NULL;
2080 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002081 self->write_func = write_file;
2082 }
2083 else if (PycStringIO_OutputCheck(file)) {
2084 self->write_func = write_cStringIO;
2085 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002086 else if (file == Py_None) {
2087 self->write_func = write_none;
2088 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002089 else {
2090 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002091
Guido van Rossum053b8df1998-11-25 16:18:00 +00002092 if (! Pdata_Check(file)) {
2093 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002094 PyErr_Clear();
2095 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002096 "attribute");
2097 goto err;
2098 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002099 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002100
Guido van Rossum053b8df1998-11-25 16:18:00 +00002101 UNLESS (self->write_buf =
Guido van Rossum60456fd1997-04-09 17:36:32 +00002102 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
2103 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002104 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002105 }
2106 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002107
Guido van Rossum053b8df1998-11-25 16:18:00 +00002108 if (PyEval_GetRestricted()) {
2109 /* Restricted execution, get private tables */
2110 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002111
Guido van Rossum053b8df1998-11-25 16:18:00 +00002112 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2113 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2114 Py_DECREF(m);
2115 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002116 }
2117 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002118 self->dispatch_table=dispatch_table;
2119 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002120 }
2121
Guido van Rossum60456fd1997-04-09 17:36:32 +00002122 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002123
2124err:
2125 Py_DECREF((PyObject *)self);
2126 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002127}
2128
2129
2130static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002131get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002132 PyObject *file=NULL;
2133 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002134
Guido van Rossum053b8df1998-11-25 16:18:00 +00002135 bin=1;
2136 if (! PyArg_ParseTuple(args, "|i", &bin)) {
2137 PyErr_Clear();
2138 bin=0;
2139 if (! PyArg_ParseTuple(args, "O|i", &file, &bin))
2140 return NULL;
2141 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002142 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002143}
2144
2145
2146static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002147Pickler_dealloc(Picklerobject *self) {
2148 Py_XDECREF(self->write);
2149 Py_XDECREF(self->memo);
2150 Py_XDECREF(self->arg);
2151 Py_XDECREF(self->file);
2152 Py_XDECREF(self->pers_func);
2153 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002154 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002155
2156 if (self->write_buf) {
2157 free(self->write_buf);
2158 }
2159
2160 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002161}
2162
2163
2164static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002165Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002166
2167 switch (*name) {
2168 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002169 if (strcmp(name, "persistent_id") == 0) {
2170 if (!self->pers_func) {
2171 PyErr_SetString(PyExc_AttributeError, name);
2172 return NULL;
2173 }
2174
2175 Py_INCREF(self->pers_func);
2176 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002177 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002178 break;
2179 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002180 if (strcmp(name, "memo") == 0) {
2181 if (!self->memo) {
2182 PyErr_SetString(PyExc_AttributeError, name);
2183 return NULL;
2184 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002185
Guido van Rossum60456fd1997-04-09 17:36:32 +00002186 Py_INCREF(self->memo);
2187 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002188 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002189 break;
2190 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002191 if (strcmp(name, "PicklingError") == 0) {
2192 Py_INCREF(PicklingError);
2193 return PicklingError;
2194 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002195 break;
2196 case 'b':
2197 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002198 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002199 break;
2200 case 'f':
2201 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002202 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002203 break;
2204 case 'g':
2205 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2206 PyErr_SetString(PyExc_AttributeError, name);
2207 return NULL;
2208 }
2209 break;
2210 }
2211 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002212}
2213
2214
2215int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002216Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002217
Guido van Rossum053b8df1998-11-25 16:18:00 +00002218 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002219 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002220 "attribute deletion is not supported");
2221 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002222 }
2223
Guido van Rossum60456fd1997-04-09 17:36:32 +00002224 if (strcmp(name, "persistent_id") == 0) {
2225 Py_XDECREF(self->pers_func);
2226 self->pers_func = value;
2227 Py_INCREF(value);
2228 return 0;
2229 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002230
Guido van Rossum60456fd1997-04-09 17:36:32 +00002231 if (strcmp(name, "inst_persistent_id") == 0) {
2232 Py_XDECREF(self->inst_pers_func);
2233 self->inst_pers_func = value;
2234 Py_INCREF(value);
2235 return 0;
2236 }
2237
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002238 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002239 if (! PyDict_Check(value)) {
2240 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2241 return -1;
2242 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002243 Py_XDECREF(self->memo);
2244 self->memo = value;
2245 Py_INCREF(value);
2246 return 0;
2247 }
2248
Guido van Rossum053b8df1998-11-25 16:18:00 +00002249 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002250 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002251 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002252 }
2253
Guido van Rossum053b8df1998-11-25 16:18:00 +00002254 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002255 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002256 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002257 }
2258
Guido van Rossum60456fd1997-04-09 17:36:32 +00002259 PyErr_SetString(PyExc_AttributeError, name);
2260 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002261}
2262
2263
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002264static char Picklertype__doc__[] =
2265"Objects that know how to pickle objects\n"
2266;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002267
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002268static PyTypeObject Picklertype = {
2269 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002270 0, /*ob_size*/
2271 "Pickler", /*tp_name*/
2272 sizeof(Picklerobject), /*tp_basicsize*/
2273 0, /*tp_itemsize*/
2274 /* methods */
2275 (destructor)Pickler_dealloc, /*tp_dealloc*/
2276 (printfunc)0, /*tp_print*/
2277 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2278 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2279 (cmpfunc)0, /*tp_compare*/
2280 (reprfunc)0, /*tp_repr*/
2281 0, /*tp_as_number*/
2282 0, /*tp_as_sequence*/
2283 0, /*tp_as_mapping*/
2284 (hashfunc)0, /*tp_hash*/
2285 (ternaryfunc)0, /*tp_call*/
2286 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002287
Guido van Rossum60456fd1997-04-09 17:36:32 +00002288 /* Space for future expansion */
2289 0L,0L,0L,0L,
2290 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002291};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002292
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002293static PyObject *
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002294find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002295 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002296
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002297 if (fc) {
2298 if (fc==Py_None) {
2299 PyErr_SetString(UnpicklingError,
2300 "Global and instance pickles are not supported.");
2301 return NULL;
2302 }
2303 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2304 }
2305
Jeremy Hyltond1055231998-08-11 19:52:51 +00002306 module = PySys_GetObject("modules");
2307 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002308 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002309
2310 module = PyDict_GetItem(module, py_module_name);
2311 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002312 module = PyImport_Import(py_module_name);
2313 if (!module)
2314 return NULL;
2315 global = PyObject_GetAttr(module, py_global_name);
2316 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002317 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002318 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002319 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002320 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002321 char buf[256 + 37];
2322 sprintf(buf, "Failed to import class %.128s from module %.128s",
2323 PyString_AS_STRING((PyStringObject*)py_global_name),
2324 PyString_AS_STRING((PyStringObject*)py_module_name));
2325 PyErr_SetString(PyExc_SystemError, buf);
2326 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002327 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002328 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002329}
2330
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002331static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002332marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002333 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002334 PyErr_SetString(UnpicklingError, "could not find MARK");
2335 return -1;
2336 }
2337
2338 return self->marks[--self->num_marks];
2339}
2340
2341
2342static int
2343load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002344 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002345 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002346}
2347
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002348static int
2349bad_readline() {
2350 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2351 return -1;
2352}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002353
2354static int
2355load_int(Unpicklerobject *self) {
2356 PyObject *py_int = 0;
2357 char *endptr, *s;
2358 int len, res = -1;
2359 long l;
2360
2361 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002362 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002363 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002364
2365 errno = 0;
2366 l = strtol(s, &endptr, 0);
2367
2368 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2369 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002370 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002371 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002372 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002373
Guido van Rossum053b8df1998-11-25 16:18:00 +00002374 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2375 PyErr_SetString(PyExc_ValueError,
2376 "could not convert string to int");
2377 goto finally;
2378 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002379 }
2380 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002381 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002382 }
2383
Guido van Rossum053b8df1998-11-25 16:18:00 +00002384 free(s);
2385 PDATA_PUSH(self->stack, py_int, -1);
2386 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002387
2388finally:
2389 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002390
2391 return res;
2392}
2393
2394
2395static long
2396calc_binint(char *s, int x) {
2397 unsigned char c;
2398 int i;
2399 long l;
2400
2401 for (i = 0, l = 0L; i < x; i++) {
2402 c = (unsigned char)s[i];
2403 l |= (long)c << (i * 8);
2404 }
2405
2406 return l;
2407}
2408
2409
2410static int
2411load_binintx(Unpicklerobject *self, char *s, int x) {
2412 PyObject *py_int = 0;
2413 long l;
2414
2415 l = calc_binint(s, x);
2416
Guido van Rossum053b8df1998-11-25 16:18:00 +00002417 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002418 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002419
Guido van Rossum053b8df1998-11-25 16:18:00 +00002420 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002421 return 0;
2422}
2423
2424
2425static int
2426load_binint(Unpicklerobject *self) {
2427 char *s;
2428
2429 if ((*self->read_func)(self, &s, 4) < 0)
2430 return -1;
2431
2432 return load_binintx(self, s, 4);
2433}
2434
2435
2436static int
2437load_binint1(Unpicklerobject *self) {
2438 char *s;
2439
2440 if ((*self->read_func)(self, &s, 1) < 0)
2441 return -1;
2442
2443 return load_binintx(self, s, 1);
2444}
2445
2446
2447static int
2448load_binint2(Unpicklerobject *self) {
2449 char *s;
2450
2451 if ((*self->read_func)(self, &s, 2) < 0)
2452 return -1;
2453
2454 return load_binintx(self, s, 2);
2455}
2456
2457static int
2458load_long(Unpicklerobject *self) {
2459 PyObject *l = 0;
2460 char *end, *s;
2461 int len, res = -1;
2462
Guido van Rossum60456fd1997-04-09 17:36:32 +00002463 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002464 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002465 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002466
Guido van Rossum053b8df1998-11-25 16:18:00 +00002467 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002468 goto finally;
2469
Guido van Rossum053b8df1998-11-25 16:18:00 +00002470 free(s);
2471 PDATA_PUSH(self->stack, l, -1);
2472 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002473
2474finally:
2475 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002476
2477 return res;
2478}
2479
2480
2481static int
2482load_float(Unpicklerobject *self) {
2483 PyObject *py_float = 0;
2484 char *endptr, *s;
2485 int len, res = -1;
2486 double d;
2487
2488 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002489 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002490 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002491
2492 errno = 0;
2493 d = strtod(s, &endptr);
2494
2495 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2496 PyErr_SetString(PyExc_ValueError,
2497 "could not convert string to float");
2498 goto finally;
2499 }
2500
Guido van Rossum053b8df1998-11-25 16:18:00 +00002501 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002502 goto finally;
2503
Guido van Rossum053b8df1998-11-25 16:18:00 +00002504 free(s);
2505 PDATA_PUSH(self->stack, py_float, -1);
2506 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002507
2508finally:
2509 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002510
2511 return res;
2512}
2513
Guido van Rossum60456fd1997-04-09 17:36:32 +00002514static int
2515load_binfloat(Unpicklerobject *self) {
2516 PyObject *py_float = 0;
2517 int s, e, res = -1;
2518 long fhi, flo;
2519 double x;
2520 char *p;
2521
2522 if ((*self->read_func)(self, &p, 8) < 0)
2523 return -1;
2524
2525 /* First byte */
2526 s = (*p>>7) & 1;
2527 e = (*p & 0x7F) << 4;
2528 p++;
2529
2530 /* Second byte */
2531 e |= (*p>>4) & 0xF;
2532 fhi = (*p & 0xF) << 24;
2533 p++;
2534
2535 /* Third byte */
2536 fhi |= (*p & 0xFF) << 16;
2537 p++;
2538
2539 /* Fourth byte */
2540 fhi |= (*p & 0xFF) << 8;
2541 p++;
2542
2543 /* Fifth byte */
2544 fhi |= *p & 0xFF;
2545 p++;
2546
2547 /* Sixth byte */
2548 flo = (*p & 0xFF) << 16;
2549 p++;
2550
2551 /* Seventh byte */
2552 flo |= (*p & 0xFF) << 8;
2553 p++;
2554
2555 /* Eighth byte */
2556 flo |= *p & 0xFF;
2557
2558 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2559 x /= 268435456.0; /* 2**28 */
2560
2561 /* XXX This sadly ignores Inf/NaN */
2562 if (e == 0)
2563 e = -1022;
2564 else {
2565 x += 1.0;
2566 e -= 1023;
2567 }
2568 x = ldexp(x, e);
2569
2570 if (s)
2571 x = -x;
2572
Guido van Rossum053b8df1998-11-25 16:18:00 +00002573 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002574
Guido van Rossum053b8df1998-11-25 16:18:00 +00002575 PDATA_PUSH(self->stack, py_float, -1);
2576 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002577}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002578
2579static int
2580load_string(Unpicklerobject *self) {
2581 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002582 int len, res = -1, nslash;
2583 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002584
2585 static PyObject *eval_dict = 0;
2586
2587 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002588 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002589 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002590
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002591 /* Check for unquoted quotes (evil strings) */
2592 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002593 if (q != '"' && q != '\'') goto insecure;
2594 for (p=s+1, nslash=0; *p; p++) {
2595 if (*p==q && nslash%2==0) break;
2596 if (*p=='\\') nslash++;
2597 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002598 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002599 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002600 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002601 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002602 }
2603 else goto insecure;
2604 /********************************************/
2605
Guido van Rossum053b8df1998-11-25 16:18:00 +00002606 UNLESS (eval_dict)
2607 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002608 goto finally;
2609
Guido van Rossum053b8df1998-11-25 16:18:00 +00002610 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002611 goto finally;
2612
Guido van Rossum053b8df1998-11-25 16:18:00 +00002613 free(s);
2614 PDATA_PUSH(self->stack, str, -1);
2615 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002616
2617finally:
2618 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002619
2620 return res;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002621
2622insecure:
2623 free(s);
2624 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2625 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002626}
2627
2628
2629static int
2630load_binstring(Unpicklerobject *self) {
2631 PyObject *py_string = 0;
2632 long l;
2633 int res = -1;
2634 char *s;
2635
Guido van Rossum053b8df1998-11-25 16:18:00 +00002636 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002637
2638 l = calc_binint(s, 4);
2639
2640 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002641 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002642
Guido van Rossum053b8df1998-11-25 16:18:00 +00002643 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2644 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002645
Guido van Rossum053b8df1998-11-25 16:18:00 +00002646 PDATA_PUSH(self->stack, py_string, -1);
2647 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002648}
2649
2650
2651static int
2652load_short_binstring(Unpicklerobject *self) {
2653 PyObject *py_string = 0;
2654 unsigned char l;
2655 int res = -1;
2656 char *s;
2657
2658 if ((*self->read_func)(self, &s, 1) < 0)
2659 return -1;
2660
2661 l = (unsigned char)s[0];
2662
Guido van Rossum053b8df1998-11-25 16:18:00 +00002663 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002664
Guido van Rossum053b8df1998-11-25 16:18:00 +00002665 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002666
Guido van Rossum053b8df1998-11-25 16:18:00 +00002667 PDATA_PUSH(self->stack, py_string, -1);
2668 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002669}
2670
2671
2672static int
2673load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002674 PyObject *tup;
2675 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002676
Guido van Rossum053b8df1998-11-25 16:18:00 +00002677 if ((i = marker(self)) < 0) return -1;
2678 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2679 PDATA_PUSH(self->stack, tup, -1);
2680 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002681}
2682
2683static int
2684load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002685 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002686
Guido van Rossum053b8df1998-11-25 16:18:00 +00002687 UNLESS (tup=PyTuple_New(0)) return -1;
2688 PDATA_PUSH(self->stack, tup, -1);
2689 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002690}
2691
2692static int
2693load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002694 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002695
Guido van Rossum053b8df1998-11-25 16:18:00 +00002696 UNLESS (list=PyList_New(0)) return -1;
2697 PDATA_PUSH(self->stack, list, -1);
2698 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002699}
2700
2701static int
2702load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002703 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002704
Guido van Rossum053b8df1998-11-25 16:18:00 +00002705 UNLESS (dict=PyDict_New()) return -1;
2706 PDATA_PUSH(self->stack, dict, -1);
2707 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002708}
2709
2710
2711static int
2712load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002713 PyObject *list = 0;
2714 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002715
Guido van Rossum053b8df1998-11-25 16:18:00 +00002716 if ((i = marker(self)) < 0) return -1;
2717 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2718 PDATA_PUSH(self->stack, list, -1);
2719 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002720}
2721
2722static int
2723load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002724 PyObject *dict, *key, *value;
2725 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002726
Guido van Rossum053b8df1998-11-25 16:18:00 +00002727 if ((i = marker(self)) < 0) return -1;
2728 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002729
Guido van Rossum053b8df1998-11-25 16:18:00 +00002730 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002731
Guido van Rossum053b8df1998-11-25 16:18:00 +00002732 for (k = i+1; k < j; k += 2) {
2733 key =self->stack->data[k-1];
2734 value=self->stack->data[k ];
2735 if (PyDict_SetItem(dict, key, value) < 0) {
2736 Py_DECREF(dict);
2737 return -1;
2738 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002739 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002740 Pdata_clear(self->stack, i);
2741 PDATA_PUSH(self->stack, dict, -1);
2742 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002743}
2744
2745static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002746Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002747 int has_key;
2748 PyObject *safe=0, *r=0;
2749
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002750 if (PyClass_Check(cls)) {
2751 int l;
2752
Guido van Rossum053b8df1998-11-25 16:18:00 +00002753 if ((l=PyObject_Length(args)) < 0) goto err;
2754 UNLESS (l) {
2755 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002756
Guido van Rossum053b8df1998-11-25 16:18:00 +00002757 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2758 /* We have a class with no __getinitargs__, so bypass usual
2759 construction */
2760 PyInstanceObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002761
Guido van Rossum053b8df1998-11-25 16:18:00 +00002762 PyErr_Clear();
2763 UNLESS (inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
2764 goto err;
2765 inst->in_class=(PyClassObject*)cls;
2766 Py_INCREF(cls);
2767 UNLESS (inst->in_dict=PyDict_New()) {
2768 Py_DECREF(inst);
2769 goto err;
2770 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002771
Guido van Rossum053b8df1998-11-25 16:18:00 +00002772 return (PyObject *)inst;
2773 }
2774 Py_DECREF(__getinitargs__);
2775 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002776
Guido van Rossum053b8df1998-11-25 16:18:00 +00002777 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002778 else goto err;
2779 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002780
2781
2782 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2783 goto err;
2784
2785 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002786 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002787 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002788 cPickle_ErrFormat(UnpicklingError,
2789 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002790 Py_XDECREF(safe);
2791 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002792 }
2793
Guido van Rossum053b8df1998-11-25 16:18:00 +00002794 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002795 /* Special case, call cls.__basicnew__() */
2796 PyObject *basicnew;
2797
Guido van Rossum053b8df1998-11-25 16:18:00 +00002798 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002799 r=PyObject_CallObject(basicnew, NULL);
2800 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002801 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002802 }
2803
Guido van Rossum053b8df1998-11-25 16:18:00 +00002804 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002805
Guido van Rossum60456fd1997-04-09 17:36:32 +00002806err:
2807 {
2808 PyObject *tp, *v, *tb;
2809
2810 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002811 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2812 Py_XDECREF(v);
2813 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002814 }
2815 PyErr_Restore(tp,v,tb);
2816 }
2817 return NULL;
2818}
2819
2820
2821static int
2822load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002823 PyObject *class, *tup, *obj=0;
2824 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002825
Guido van Rossum053b8df1998-11-25 16:18:00 +00002826 if ((i = marker(self)) < 0) return -1;
2827 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
2828 PDATA_POP(self->stack, class);
2829 if (class) {
2830 obj = Instance_New(class, tup);
2831 Py_DECREF(class);
2832 }
2833 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002834
Guido van Rossum053b8df1998-11-25 16:18:00 +00002835 if (! obj) return -1;
2836 PDATA_PUSH(self->stack, obj, -1);
2837 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002838}
2839
2840
2841static int
2842load_inst(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002843 PyObject *tup, *class, *obj, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002844 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002845 char *s;
2846
Guido van Rossum053b8df1998-11-25 16:18:00 +00002847 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002848
Guido van Rossum053b8df1998-11-25 16:18:00 +00002849 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002850 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002851 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002852
Guido van Rossum053b8df1998-11-25 16:18:00 +00002853 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002854 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002855 if (class_name = PyString_FromStringAndSize(s, len - 1)) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002856 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002857 Py_DECREF(class_name);
2858 }
2859 }
2860 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002861
Guido van Rossum053b8df1998-11-25 16:18:00 +00002862 if (! class) return -1;
2863
2864 if (tup=Pdata_popTuple(self->stack, i)) {
2865 obj = Instance_New(class, tup);
2866 Py_DECREF(tup);
2867 }
2868 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002869
Guido van Rossum053b8df1998-11-25 16:18:00 +00002870 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002871
Guido van Rossum053b8df1998-11-25 16:18:00 +00002872 PDATA_PUSH(self->stack, obj, -1);
2873 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002874}
2875
2876
2877static int
2878load_global(Unpicklerobject *self) {
2879 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002880 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002881 char *s;
2882
Guido van Rossum053b8df1998-11-25 16:18:00 +00002883 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002884 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002885 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002886
Guido van Rossum053b8df1998-11-25 16:18:00 +00002887 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002888 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002889 if (class_name = PyString_FromStringAndSize(s, len - 1)) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002890 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002891 Py_DECREF(class_name);
2892 }
2893 }
2894 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002895
Guido van Rossum053b8df1998-11-25 16:18:00 +00002896 if (! class) return -1;
2897 PDATA_PUSH(self->stack, class, -1);
2898 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002899}
2900
2901
2902static int
2903load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002904 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002905 int len, res = -1;
2906 char *s;
2907
2908 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002909 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002910 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00002911
Guido van Rossum053b8df1998-11-25 16:18:00 +00002912 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002913
Guido van Rossum053b8df1998-11-25 16:18:00 +00002914 if (PyList_Check(self->pers_func)) {
2915 if (PyList_Append(self->pers_func, pid) < 0) {
2916 Py_DECREF(pid);
2917 return -1;
2918 }
2919 }
2920 else {
2921 ARG_TUP(self, pid);
2922 if (self->arg) {
2923 pid = PyObject_CallObject(self->pers_func, self->arg);
2924 FREE_ARG_TUP(self);
2925 }
2926 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002927
Guido van Rossum053b8df1998-11-25 16:18:00 +00002928 if (! pid) return -1;
2929
2930 PDATA_PUSH(self->stack, pid, -1);
2931 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002932 }
2933 else {
2934 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002935 "A load persistent id instruction was encountered,\n"
2936 "but no persistent_load function was specified.");
2937 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002938 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002939}
2940
Guido van Rossum60456fd1997-04-09 17:36:32 +00002941static int
2942load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002943 PyObject *pid = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002944 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002945
2946 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002947 PDATA_POP(self->stack, pid);
2948 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002949
Guido van Rossum053b8df1998-11-25 16:18:00 +00002950 if (PyList_Check(self->pers_func)) {
2951 if (PyList_Append(self->pers_func, pid) < 0) {
2952 Py_DECREF(pid);
2953 return -1;
2954 }
2955 }
2956 else {
2957 ARG_TUP(self, pid);
2958 if (self->arg) {
2959 pid = PyObject_CallObject(self->pers_func, self->arg);
2960 FREE_ARG_TUP(self);
2961 }
2962 if (! pid) return -1;
2963 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002964
Guido van Rossum053b8df1998-11-25 16:18:00 +00002965 PDATA_PUSH(self->stack, pid, -1);
2966 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002967 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002968 else {
2969 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002970 "A load persistent id instruction was encountered,\n"
2971 "but no persistent_load function was specified.");
2972 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002973 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002974}
2975
2976
2977static int
2978load_pop(Unpicklerobject *self) {
2979 int len;
2980
Guido van Rossum053b8df1998-11-25 16:18:00 +00002981 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00002982
2983 if ((self->num_marks > 0) &&
2984 (self->marks[self->num_marks - 1] == len))
2985 self->num_marks--;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002986 else
2987 Py_DECREF(self->stack->data[--(self->stack->length)]);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002988
2989 return 0;
2990}
2991
2992
2993static int
2994load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002995 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002996
2997 if ((i = marker(self)) < 0)
2998 return -1;
2999
Guido van Rossum053b8df1998-11-25 16:18:00 +00003000 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003001
3002 return 0;
3003}
3004
3005
3006static int
3007load_dup(Unpicklerobject *self) {
3008 PyObject *last;
3009 int len;
3010
Guido van Rossum053b8df1998-11-25 16:18:00 +00003011 if ((len = self->stack->length) <= 0) return stackUnderflow();
3012 last=self->stack->data[len-1];
3013 Py_INCREF(last);
3014 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003015 return 0;
3016}
3017
3018
3019static int
3020load_get(Unpicklerobject *self) {
3021 PyObject *py_str = 0, *value = 0;
3022 int len, res = -1;
3023 char *s;
3024
Guido van Rossum053b8df1998-11-25 16:18:00 +00003025 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003026 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003027
Guido van Rossum053b8df1998-11-25 16:18:00 +00003028 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3029
3030 value = PyDict_GetItem(self->memo, py_str);
3031 Py_DECREF(py_str);
3032 if (! value) {
3033 PyErr_SetObject(BadPickleGet, py_str);
3034 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00003035 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003036
Guido van Rossum053b8df1998-11-25 16:18:00 +00003037 PDATA_APPEND(self->stack, value, -1);
3038 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003039}
3040
3041
3042static int
3043load_binget(Unpicklerobject *self) {
3044 PyObject *py_key = 0, *value = 0;
3045 unsigned char key;
3046 int res = -1;
3047 char *s;
3048
Guido van Rossum053b8df1998-11-25 16:18:00 +00003049 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003050
3051 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003052 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3053
3054 value = PyDict_GetItem(self->memo, py_key);
3055 Py_DECREF(py_key);
3056 if (! value) {
3057 PyErr_SetObject(BadPickleGet, py_key);
3058 return -1;
3059 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003060
Guido van Rossum053b8df1998-11-25 16:18:00 +00003061 PDATA_APPEND(self->stack, value, -1);
3062 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003063}
3064
3065
3066static int
3067load_long_binget(Unpicklerobject *self) {
3068 PyObject *py_key = 0, *value = 0;
3069 unsigned char c, *s;
3070 long key;
3071 int res = -1;
3072
Guido van Rossum053b8df1998-11-25 16:18:00 +00003073 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003074
3075 c = (unsigned char)s[0];
3076 key = (long)c;
3077 c = (unsigned char)s[1];
3078 key |= (long)c << 8;
3079 c = (unsigned char)s[2];
3080 key |= (long)c << 16;
3081 c = (unsigned char)s[3];
3082 key |= (long)c << 24;
3083
Guido van Rossum053b8df1998-11-25 16:18:00 +00003084 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3085
3086 value = PyDict_GetItem(self->memo, py_key);
3087 Py_DECREF(py_key);
3088 if (! value) {
3089 PyErr_SetObject(BadPickleGet, py_key);
3090 return -1;
3091 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003092
Guido van Rossum053b8df1998-11-25 16:18:00 +00003093 PDATA_APPEND(self->stack, value, -1);
3094 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003095}
3096
3097
3098static int
3099load_put(Unpicklerobject *self) {
3100 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003101 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003102 char *s;
3103
Guido van Rossum053b8df1998-11-25 16:18:00 +00003104 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003105 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003106 UNLESS (len=self->stack->length) return stackUnderflow();
3107 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3108 value=self->stack->data[len-1];
3109 l=PyDict_SetItem(self->memo, py_str, value);
3110 Py_DECREF(py_str);
3111 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003112}
3113
3114
3115static int
3116load_binput(Unpicklerobject *self) {
3117 PyObject *py_key = 0, *value = 0;
3118 unsigned char key, *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003119 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003120
Guido van Rossum053b8df1998-11-25 16:18:00 +00003121 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3122 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003123
3124 key = (unsigned char)s[0];
3125
Guido van Rossum053b8df1998-11-25 16:18:00 +00003126 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3127 value=self->stack->data[len-1];
3128 len=PyDict_SetItem(self->memo, py_key, value);
3129 Py_DECREF(py_key);
3130 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003131}
3132
3133
3134static int
3135load_long_binput(Unpicklerobject *self) {
3136 PyObject *py_key = 0, *value = 0;
3137 long key;
3138 unsigned char c, *s;
3139 int len, res = -1;
3140
Guido van Rossum053b8df1998-11-25 16:18:00 +00003141 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3142 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003143
3144 c = (unsigned char)s[0];
3145 key = (long)c;
3146 c = (unsigned char)s[1];
3147 key |= (long)c << 8;
3148 c = (unsigned char)s[2];
3149 key |= (long)c << 16;
3150 c = (unsigned char)s[3];
3151 key |= (long)c << 24;
3152
Guido van Rossum053b8df1998-11-25 16:18:00 +00003153 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3154 value=self->stack->data[len-1];
3155 len=PyDict_SetItem(self->memo, py_key, value);
3156 Py_DECREF(py_key);
3157 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003158}
3159
3160
3161static int
3162do_append(Unpicklerobject *self, int x) {
3163 PyObject *value = 0, *list = 0, *append_method = 0;
3164 int len, i;
3165
Guido van Rossum053b8df1998-11-25 16:18:00 +00003166 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3167 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003168
Guido van Rossum053b8df1998-11-25 16:18:00 +00003169 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003170
3171 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003172 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003173 int list_len;
3174
Guido van Rossum053b8df1998-11-25 16:18:00 +00003175 slice=Pdata_popList(self->stack, x);
3176 list_len = PyList_GET_SIZE(list);
3177 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003178 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003179 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003180 }
3181 else {
3182
Guido van Rossum053b8df1998-11-25 16:18:00 +00003183 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003184 return -1;
3185
3186 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003187 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003188
Guido van Rossum053b8df1998-11-25 16:18:00 +00003189 value=self->stack->data[i];
3190 junk=0;
3191 ARG_TUP(self, value);
3192 if (self->arg) {
3193 junk = PyObject_CallObject(append_method, self->arg);
3194 FREE_ARG_TUP(self);
3195 }
3196 if (! junk) {
3197 Pdata_clear(self->stack, i+1);
3198 self->stack->length=x;
3199 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003200 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003201 }
3202 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003203 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003204 self->stack->length=x;
3205 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003206 }
3207
Guido van Rossum60456fd1997-04-09 17:36:32 +00003208 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003209}
3210
3211
3212static int
3213load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003214 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003215}
3216
3217
3218static int
3219load_appends(Unpicklerobject *self) {
3220 return do_append(self, marker(self));
3221}
3222
3223
3224static int
3225do_setitems(Unpicklerobject *self, int x) {
3226 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003227 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003228
Guido van Rossum053b8df1998-11-25 16:18:00 +00003229 UNLESS ((len=self->stack->length) >= x
3230 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003231
Guido van Rossum053b8df1998-11-25 16:18:00 +00003232 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003233
Guido van Rossum053b8df1998-11-25 16:18:00 +00003234 for (i = x+1; i < len; i += 2) {
3235 key =self->stack->data[i-1];
3236 value=self->stack->data[i ];
3237 if (PyObject_SetItem(dict, key, value) < 0) {
3238 r=-1;
3239 break;
3240 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003241 }
3242
Guido van Rossum053b8df1998-11-25 16:18:00 +00003243 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003244
Guido van Rossum053b8df1998-11-25 16:18:00 +00003245 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003246}
3247
3248
3249static int
3250load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003251 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003252}
3253
Guido van Rossum60456fd1997-04-09 17:36:32 +00003254static int
3255load_setitems(Unpicklerobject *self) {
3256 return do_setitems(self, marker(self));
3257}
3258
3259
3260static int
3261load_build(Unpicklerobject *self) {
3262 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3263 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003264 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003265
Guido van Rossum053b8df1998-11-25 16:18:00 +00003266 if (self->stack->length < 2) return stackUnderflow();
3267 PDATA_POP(self->stack, value);
3268 if (! value) return -1;
3269 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003270
Guido van Rossum053b8df1998-11-25 16:18:00 +00003271 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3272 ARG_TUP(self, value);
3273 if (self->arg) {
3274 junk = PyObject_CallObject(__setstate__, self->arg);
3275 FREE_ARG_TUP(self);
3276 }
3277 Py_DECREF(__setstate__);
3278 if (! junk) return -1;
3279 Py_DECREF(junk);
3280 return 0;
3281 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003282
Guido van Rossum053b8df1998-11-25 16:18:00 +00003283 PyErr_Clear();
3284 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003285 i = 0;
3286 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003287 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3288 r=-1;
3289 break;
3290 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003291 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003292 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003293 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003294 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003295
Guido van Rossum053b8df1998-11-25 16:18:00 +00003296 Py_XDECREF(value);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003297
Guido van Rossum053b8df1998-11-25 16:18:00 +00003298 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003299}
3300
3301
3302static int
3303load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003304 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003305
Guido van Rossum053b8df1998-11-25 16:18:00 +00003306 if ((self->num_marks + 1) >= self->marks_size) {
3307 s=self->marks_size+20;
3308 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003309 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003310 self->marks=(int *)malloc(s * sizeof(int));
3311 else
3312 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003313 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003314 PyErr_NoMemory();
3315 return -1;
3316 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003317 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003318 }
3319
Guido van Rossum053b8df1998-11-25 16:18:00 +00003320 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003321
3322 return 0;
3323}
3324
3325static int
3326load_reduce(Unpicklerobject *self) {
3327 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003328
Guido van Rossum053b8df1998-11-25 16:18:00 +00003329 PDATA_POP(self->stack, arg_tup);
3330 if (! arg_tup) return -1;
3331 PDATA_POP(self->stack, callable);
3332 if (callable) {
3333 ob = Instance_New(callable, arg_tup);
3334 Py_DECREF(callable);
3335 }
3336 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003337
Guido van Rossum053b8df1998-11-25 16:18:00 +00003338 if (! ob) return -1;
3339
3340 PDATA_PUSH(self->stack, ob, -1);
3341 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003342}
3343
3344static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003345load(Unpicklerobject *self) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00003346 PyObject *stack = 0, *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003347 char *s;
3348
Guido van Rossum60456fd1997-04-09 17:36:32 +00003349 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003350 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003351
3352 while (1) {
3353 if ((*self->read_func)(self, &s, 1) < 0)
3354 break;
3355
3356 switch (s[0]) {
3357 case NONE:
3358 if (load_none(self) < 0)
3359 break;
3360 continue;
3361
3362 case BININT:
3363 if (load_binint(self) < 0)
3364 break;
3365 continue;
3366
3367 case BININT1:
3368 if (load_binint1(self) < 0)
3369 break;
3370 continue;
3371
3372 case BININT2:
3373 if (load_binint2(self) < 0)
3374 break;
3375 continue;
3376
3377 case INT:
3378 if (load_int(self) < 0)
3379 break;
3380 continue;
3381
3382 case LONG:
3383 if (load_long(self) < 0)
3384 break;
3385 continue;
3386
3387 case FLOAT:
3388 if (load_float(self) < 0)
3389 break;
3390 continue;
3391
Guido van Rossum60456fd1997-04-09 17:36:32 +00003392 case BINFLOAT:
3393 if (load_binfloat(self) < 0)
3394 break;
3395 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003396
3397 case BINSTRING:
3398 if (load_binstring(self) < 0)
3399 break;
3400 continue;
3401
3402 case SHORT_BINSTRING:
3403 if (load_short_binstring(self) < 0)
3404 break;
3405 continue;
3406
3407 case STRING:
3408 if (load_string(self) < 0)
3409 break;
3410 continue;
3411
3412 case EMPTY_TUPLE:
3413 if (load_empty_tuple(self) < 0)
3414 break;
3415 continue;
3416
3417 case TUPLE:
3418 if (load_tuple(self) < 0)
3419 break;
3420 continue;
3421
3422 case EMPTY_LIST:
3423 if (load_empty_list(self) < 0)
3424 break;
3425 continue;
3426
3427 case LIST:
3428 if (load_list(self) < 0)
3429 break;
3430 continue;
3431
3432 case EMPTY_DICT:
3433 if (load_empty_dict(self) < 0)
3434 break;
3435 continue;
3436
3437 case DICT:
3438 if (load_dict(self) < 0)
3439 break;
3440 continue;
3441
3442 case OBJ:
3443 if (load_obj(self) < 0)
3444 break;
3445 continue;
3446
3447 case INST:
3448 if (load_inst(self) < 0)
3449 break;
3450 continue;
3451
3452 case GLOBAL:
3453 if (load_global(self) < 0)
3454 break;
3455 continue;
3456
3457 case APPEND:
3458 if (load_append(self) < 0)
3459 break;
3460 continue;
3461
3462 case APPENDS:
3463 if (load_appends(self) < 0)
3464 break;
3465 continue;
3466
3467 case BUILD:
3468 if (load_build(self) < 0)
3469 break;
3470 continue;
3471
3472 case DUP:
3473 if (load_dup(self) < 0)
3474 break;
3475 continue;
3476
3477 case BINGET:
3478 if (load_binget(self) < 0)
3479 break;
3480 continue;
3481
3482 case LONG_BINGET:
3483 if (load_long_binget(self) < 0)
3484 break;
3485 continue;
3486
3487 case GET:
3488 if (load_get(self) < 0)
3489 break;
3490 continue;
3491
3492 case MARK:
3493 if (load_mark(self) < 0)
3494 break;
3495 continue;
3496
3497 case BINPUT:
3498 if (load_binput(self) < 0)
3499 break;
3500 continue;
3501
3502 case LONG_BINPUT:
3503 if (load_long_binput(self) < 0)
3504 break;
3505 continue;
3506
3507 case PUT:
3508 if (load_put(self) < 0)
3509 break;
3510 continue;
3511
3512 case POP:
3513 if (load_pop(self) < 0)
3514 break;
3515 continue;
3516
3517 case POP_MARK:
3518 if (load_pop_mark(self) < 0)
3519 break;
3520 continue;
3521
3522 case SETITEM:
3523 if (load_setitem(self) < 0)
3524 break;
3525 continue;
3526
3527 case SETITEMS:
3528 if (load_setitems(self) < 0)
3529 break;
3530 continue;
3531
3532 case STOP:
3533 break;
3534
3535 case PERSID:
3536 if (load_persid(self) < 0)
3537 break;
3538 continue;
3539
3540 case BINPERSID:
3541 if (load_binpersid(self) < 0)
3542 break;
3543 continue;
3544
3545 case REDUCE:
3546 if (load_reduce(self) < 0)
3547 break;
3548 continue;
3549
3550 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003551 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003552 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003553 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003554 }
3555
3556 break;
3557 }
3558
Guido van Rossum053b8df1998-11-25 16:18:00 +00003559 if ((err = PyErr_Occurred())) {
3560 if (err == PyExc_EOFError) {
3561 PyErr_SetNone(PyExc_EOFError);
3562 }
3563 return NULL;
3564 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003565
Guido van Rossum053b8df1998-11-25 16:18:00 +00003566 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003567 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003568}
3569
3570
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003571/* No-load functions to support noload, which is used to
3572 find persistent references. */
3573
3574static int
3575noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003576 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003577
3578 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003579 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003580}
3581
3582
3583static int
3584noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003585 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003586 char *s;
3587
3588 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003589 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003590 if ((*self->readline_func)(self, &s) < 0) return -1;
3591 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003592 PDATA_APPEND(self->stack, Py_None,-1);
3593 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003594}
3595
3596static int
3597noload_global(Unpicklerobject *self) {
3598 char *s;
3599
3600 if ((*self->readline_func)(self, &s) < 0) return -1;
3601 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003602 PDATA_APPEND(self->stack, Py_None,-1);
3603 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003604}
3605
3606static int
3607noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003608
Guido van Rossum053b8df1998-11-25 16:18:00 +00003609 if (self->stack->length < 2) return stackUnderflow();
3610 Pdata_clear(self->stack, self->stack->length-2);
3611 PDATA_APPEND(self->stack, Py_None,-1);
3612 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003613}
3614
3615static int
3616noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003617
Guido van Rossum053b8df1998-11-25 16:18:00 +00003618 if (self->stack->length < 1) return stackUnderflow();
3619 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003620 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003621}
3622
3623
3624static PyObject *
3625noload(Unpicklerobject *self) {
3626 PyObject *stack = 0, *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003627 char *s;
3628
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003629 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003630 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003631
3632 while (1) {
3633 if ((*self->read_func)(self, &s, 1) < 0)
3634 break;
3635
3636 switch (s[0]) {
3637 case NONE:
3638 if (load_none(self) < 0)
3639 break;
3640 continue;
3641
3642 case BININT:
3643 if (load_binint(self) < 0)
3644 break;
3645 continue;
3646
3647 case BININT1:
3648 if (load_binint1(self) < 0)
3649 break;
3650 continue;
3651
3652 case BININT2:
3653 if (load_binint2(self) < 0)
3654 break;
3655 continue;
3656
3657 case INT:
3658 if (load_int(self) < 0)
3659 break;
3660 continue;
3661
3662 case LONG:
3663 if (load_long(self) < 0)
3664 break;
3665 continue;
3666
3667 case FLOAT:
3668 if (load_float(self) < 0)
3669 break;
3670 continue;
3671
3672 case BINFLOAT:
3673 if (load_binfloat(self) < 0)
3674 break;
3675 continue;
3676
3677 case BINSTRING:
3678 if (load_binstring(self) < 0)
3679 break;
3680 continue;
3681
3682 case SHORT_BINSTRING:
3683 if (load_short_binstring(self) < 0)
3684 break;
3685 continue;
3686
3687 case STRING:
3688 if (load_string(self) < 0)
3689 break;
3690 continue;
3691
3692 case EMPTY_TUPLE:
3693 if (load_empty_tuple(self) < 0)
3694 break;
3695 continue;
3696
3697 case TUPLE:
3698 if (load_tuple(self) < 0)
3699 break;
3700 continue;
3701
3702 case EMPTY_LIST:
3703 if (load_empty_list(self) < 0)
3704 break;
3705 continue;
3706
3707 case LIST:
3708 if (load_list(self) < 0)
3709 break;
3710 continue;
3711
3712 case EMPTY_DICT:
3713 if (load_empty_dict(self) < 0)
3714 break;
3715 continue;
3716
3717 case DICT:
3718 if (load_dict(self) < 0)
3719 break;
3720 continue;
3721
3722 case OBJ:
3723 if (noload_obj(self) < 0)
3724 break;
3725 continue;
3726
3727 case INST:
3728 if (noload_inst(self) < 0)
3729 break;
3730 continue;
3731
3732 case GLOBAL:
3733 if (noload_global(self) < 0)
3734 break;
3735 continue;
3736
3737 case APPEND:
3738 if (load_append(self) < 0)
3739 break;
3740 continue;
3741
3742 case APPENDS:
3743 if (load_appends(self) < 0)
3744 break;
3745 continue;
3746
3747 case BUILD:
3748 if (noload_build(self) < 0)
3749 break;
3750 continue;
3751
3752 case DUP:
3753 if (load_dup(self) < 0)
3754 break;
3755 continue;
3756
3757 case BINGET:
3758 if (load_binget(self) < 0)
3759 break;
3760 continue;
3761
3762 case LONG_BINGET:
3763 if (load_long_binget(self) < 0)
3764 break;
3765 continue;
3766
3767 case GET:
3768 if (load_get(self) < 0)
3769 break;
3770 continue;
3771
3772 case MARK:
3773 if (load_mark(self) < 0)
3774 break;
3775 continue;
3776
3777 case BINPUT:
3778 if (load_binput(self) < 0)
3779 break;
3780 continue;
3781
3782 case LONG_BINPUT:
3783 if (load_long_binput(self) < 0)
3784 break;
3785 continue;
3786
3787 case PUT:
3788 if (load_put(self) < 0)
3789 break;
3790 continue;
3791
3792 case POP:
3793 if (load_pop(self) < 0)
3794 break;
3795 continue;
3796
3797 case POP_MARK:
3798 if (load_pop_mark(self) < 0)
3799 break;
3800 continue;
3801
3802 case SETITEM:
3803 if (load_setitem(self) < 0)
3804 break;
3805 continue;
3806
3807 case SETITEMS:
3808 if (load_setitems(self) < 0)
3809 break;
3810 continue;
3811
3812 case STOP:
3813 break;
3814
3815 case PERSID:
3816 if (load_persid(self) < 0)
3817 break;
3818 continue;
3819
3820 case BINPERSID:
3821 if (load_binpersid(self) < 0)
3822 break;
3823 continue;
3824
3825 case REDUCE:
3826 if (noload_reduce(self) < 0)
3827 break;
3828 continue;
3829
3830 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003831 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003832 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003833 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003834 }
3835
3836 break;
3837 }
3838
Guido van Rossum053b8df1998-11-25 16:18:00 +00003839 if ((err = PyErr_Occurred())) {
3840 if (err == PyExc_EOFError) {
3841 PyErr_SetNone(PyExc_EOFError);
3842 }
3843 return NULL;
3844 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003845
Guido van Rossum053b8df1998-11-25 16:18:00 +00003846 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003847 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003848}
3849
3850
Guido van Rossum60456fd1997-04-09 17:36:32 +00003851static PyObject *
3852Unpickler_load(Unpicklerobject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003853 UNLESS (PyArg_ParseTuple(args, ""))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003854 return NULL;
3855
3856 return load(self);
3857}
3858
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003859static PyObject *
3860Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003861 UNLESS (PyArg_ParseTuple(args, ""))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003862 return NULL;
3863
3864 return noload(self);
3865}
3866
Guido van Rossum60456fd1997-04-09 17:36:32 +00003867
3868static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003869 {"load", (PyCFunction)Unpickler_load, 1,
3870 "load() -- Load a pickle"
3871 },
3872 {"noload", (PyCFunction)Unpickler_noload, 1,
3873 "noload() -- not load a pickle, but go through most of the motions\n"
3874 "\n"
3875 "This function can be used to read past a pickle without instantiating\n"
3876 "any objects or importing any modules. It can also be used to find all\n"
3877 "persistent references without instantiating any objects or importing\n"
3878 "any modules.\n"
3879 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00003880 {NULL, NULL} /* sentinel */
3881};
3882
3883
3884static Unpicklerobject *
3885newUnpicklerobject(PyObject *f) {
3886 Unpicklerobject *self;
3887
Guido van Rossum053b8df1998-11-25 16:18:00 +00003888 UNLESS (self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003889 return NULL;
3890
3891 self->file = NULL;
3892 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003893 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003894 self->pers_func = NULL;
3895 self->last_string = NULL;
3896 self->marks = NULL;
3897 self->num_marks = 0;
3898 self->marks_size = 0;
3899 self->buf_size = 0;
3900 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00003901 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00003902 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003903 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003904
Guido van Rossum053b8df1998-11-25 16:18:00 +00003905 UNLESS (self->memo = PyDict_New()) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003906 Py_XDECREF((PyObject *)self);
3907 return NULL;
3908 }
3909
3910 Py_INCREF(f);
3911 self->file = f;
3912
3913 /* Set read, readline based on type of f */
3914 if (PyFile_Check(f)) {
3915 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00003916 if (self->fp == NULL) {
3917 PyErr_SetString(PyExc_IOError, "input file closed");
3918 return NULL;
3919 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003920 self->read_func = read_file;
3921 self->readline_func = readline_file;
3922 }
3923 else if (PycStringIO_InputCheck(f)) {
3924 self->fp = NULL;
3925 self->read_func = read_cStringIO;
3926 self->readline_func = readline_cStringIO;
3927 }
3928 else {
3929
3930 self->fp = NULL;
3931 self->read_func = read_other;
3932 self->readline_func = readline_other;
3933
Guido van Rossum053b8df1998-11-25 16:18:00 +00003934 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003935 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003936 PyErr_Clear();
3937 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
3938 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00003939 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003940 }
3941 }
3942
Guido van Rossum053b8df1998-11-25 16:18:00 +00003943 if (PyEval_GetRestricted()) {
3944 /* Restricted execution, get private tables */
3945 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003946
Guido van Rossum053b8df1998-11-25 16:18:00 +00003947 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
3948 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
3949 Py_DECREF(m);
3950 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003951 }
3952 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003953 self->safe_constructors=safe_constructors;
3954 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003955 }
3956
Guido van Rossum60456fd1997-04-09 17:36:32 +00003957 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003958
3959err:
3960 Py_DECREF((PyObject *)self);
3961 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003962}
3963
3964
3965static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003966get_Unpickler(PyObject *self, PyObject *args) {
3967 PyObject *file;
3968
Guido van Rossum053b8df1998-11-25 16:18:00 +00003969 UNLESS (PyArg_ParseTuple(args, "O", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003970 return NULL;
3971 return (PyObject *)newUnpicklerobject(file);
3972}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003973
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003974
Guido van Rossum60456fd1997-04-09 17:36:32 +00003975static void
3976Unpickler_dealloc(Unpicklerobject *self) {
3977 Py_XDECREF(self->readline);
3978 Py_XDECREF(self->read);
3979 Py_XDECREF(self->file);
3980 Py_XDECREF(self->memo);
3981 Py_XDECREF(self->stack);
3982 Py_XDECREF(self->pers_func);
3983 Py_XDECREF(self->arg);
3984 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003985 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003986
Guido van Rossum60456fd1997-04-09 17:36:32 +00003987 if (self->marks) {
3988 free(self->marks);
3989 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003990
Guido van Rossum60456fd1997-04-09 17:36:32 +00003991 if (self->buf_size) {
3992 free(self->buf);
3993 }
3994
3995 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003996}
3997
3998
3999static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004000Unpickler_getattr(Unpicklerobject *self, char *name) {
4001 if (!strcmp(name, "persistent_load")) {
4002 if (!self->pers_func) {
4003 PyErr_SetString(PyExc_AttributeError, name);
4004 return NULL;
4005 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004006
Guido van Rossum60456fd1997-04-09 17:36:32 +00004007 Py_INCREF(self->pers_func);
4008 return self->pers_func;
4009 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004010
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004011 if (!strcmp(name, "find_global")) {
4012 if (!self->find_class) {
4013 PyErr_SetString(PyExc_AttributeError, name);
4014 return NULL;
4015 }
4016
4017 Py_INCREF(self->find_class);
4018 return self->find_class;
4019 }
4020
Guido van Rossum60456fd1997-04-09 17:36:32 +00004021 if (!strcmp(name, "memo")) {
4022 if (!self->memo) {
4023 PyErr_SetString(PyExc_AttributeError, name);
4024 return NULL;
4025 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004026
Guido van Rossum60456fd1997-04-09 17:36:32 +00004027 Py_INCREF(self->memo);
4028 return self->memo;
4029 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004030
Guido van Rossum60456fd1997-04-09 17:36:32 +00004031 if (!strcmp(name, "UnpicklingError")) {
4032 Py_INCREF(UnpicklingError);
4033 return UnpicklingError;
4034 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004035
Guido van Rossum60456fd1997-04-09 17:36:32 +00004036 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4037}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004038
Guido van Rossum60456fd1997-04-09 17:36:32 +00004039
4040static int
4041Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004042
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004043 if (!strcmp(name, "persistent_load")) {
4044 Py_XDECREF(self->pers_func);
4045 self->pers_func = value;
4046 Py_XINCREF(value);
4047 return 0;
4048 }
4049
4050 if (!strcmp(name, "find_global")) {
4051 Py_XDECREF(self->find_class);
4052 self->find_class = value;
4053 Py_XINCREF(value);
4054 return 0;
4055 }
4056
Guido van Rossum053b8df1998-11-25 16:18:00 +00004057 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004058 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004059 "attribute deletion is not supported");
4060 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004061 }
4062
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004063 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004064 if (! PyDict_Check(value)) {
4065 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4066 return -1;
4067 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004068 Py_XDECREF(self->memo);
4069 self->memo = value;
4070 Py_INCREF(value);
4071 return 0;
4072 }
4073
Guido van Rossum60456fd1997-04-09 17:36:32 +00004074 PyErr_SetString(PyExc_AttributeError, name);
4075 return -1;
4076}
4077
4078
4079static PyObject *
4080cpm_dump(PyObject *self, PyObject *args) {
4081 PyObject *ob, *file, *res = NULL;
4082 Picklerobject *pickler = 0;
4083 int bin = 0;
4084
Guido van Rossum053b8df1998-11-25 16:18:00 +00004085 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004086 goto finally;
4087
Guido van Rossum053b8df1998-11-25 16:18:00 +00004088 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004089 goto finally;
4090
4091 if (dump(pickler, ob) < 0)
4092 goto finally;
4093
4094 Py_INCREF(Py_None);
4095 res = Py_None;
4096
4097finally:
4098 Py_XDECREF(pickler);
4099
4100 return res;
4101}
4102
4103
4104static PyObject *
4105cpm_dumps(PyObject *self, PyObject *args) {
4106 PyObject *ob, *file = 0, *res = NULL;
4107 Picklerobject *pickler = 0;
4108 int bin = 0;
4109
Guido van Rossum053b8df1998-11-25 16:18:00 +00004110 UNLESS (PyArg_ParseTuple(args, "O|i", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004111 goto finally;
4112
Guido van Rossum053b8df1998-11-25 16:18:00 +00004113 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004114 goto finally;
4115
Guido van Rossum053b8df1998-11-25 16:18:00 +00004116 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004117 goto finally;
4118
4119 if (dump(pickler, ob) < 0)
4120 goto finally;
4121
4122 res = PycStringIO->cgetvalue(file);
4123
4124finally:
4125 Py_XDECREF(pickler);
4126 Py_XDECREF(file);
4127
4128 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004129}
4130
4131
4132static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004133cpm_load(PyObject *self, PyObject *args) {
4134 Unpicklerobject *unpickler = 0;
4135 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004136
Guido van Rossum053b8df1998-11-25 16:18:00 +00004137 UNLESS (PyArg_ParseTuple(args, "O", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004138 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004139
Guido van Rossum053b8df1998-11-25 16:18:00 +00004140 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004141 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004142
Guido van Rossum60456fd1997-04-09 17:36:32 +00004143 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004144
Guido van Rossum60456fd1997-04-09 17:36:32 +00004145finally:
4146 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004147
Guido van Rossum60456fd1997-04-09 17:36:32 +00004148 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004149}
4150
4151
4152static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004153cpm_loads(PyObject *self, PyObject *args) {
4154 PyObject *ob, *file = 0, *res = NULL;
4155 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004156
Guido van Rossum053b8df1998-11-25 16:18:00 +00004157 UNLESS (PyArg_ParseTuple(args, "S", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004158 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004159
Guido van Rossum053b8df1998-11-25 16:18:00 +00004160 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004161 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004162
Guido van Rossum053b8df1998-11-25 16:18:00 +00004163 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004164 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004165
Guido van Rossum60456fd1997-04-09 17:36:32 +00004166 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004167
Guido van Rossum60456fd1997-04-09 17:36:32 +00004168finally:
4169 Py_XDECREF(file);
4170 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004171
Guido van Rossum60456fd1997-04-09 17:36:32 +00004172 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004173}
4174
4175
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004176static char Unpicklertype__doc__[] =
4177"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004178
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004179static PyTypeObject Unpicklertype = {
4180 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004181 0, /*ob_size*/
4182 "Unpickler", /*tp_name*/
4183 sizeof(Unpicklerobject), /*tp_basicsize*/
4184 0, /*tp_itemsize*/
4185 /* methods */
4186 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4187 (printfunc)0, /*tp_print*/
4188 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4189 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4190 (cmpfunc)0, /*tp_compare*/
4191 (reprfunc)0, /*tp_repr*/
4192 0, /*tp_as_number*/
4193 0, /*tp_as_sequence*/
4194 0, /*tp_as_mapping*/
4195 (hashfunc)0, /*tp_hash*/
4196 (ternaryfunc)0, /*tp_call*/
4197 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004198
Guido van Rossum60456fd1997-04-09 17:36:32 +00004199 /* Space for future expansion */
4200 0L,0L,0L,0L,
4201 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004202};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004203
Guido van Rossum60456fd1997-04-09 17:36:32 +00004204static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004205 {"dump", (PyCFunction)cpm_dump, 1,
4206 "dump(object, file, [binary]) --"
4207 "Write an object in pickle format to the given file\n"
4208 "\n"
4209 "If the optional argument, binary, is provided and is true, then the\n"
4210 "pickle will be written in binary format, which is more space and\n"
4211 "computationally efficient. \n"
4212 },
4213 {"dumps", (PyCFunction)cpm_dumps, 1,
4214 "dumps(object, [binary]) --"
4215 "Return a string containing an object in pickle format\n"
4216 "\n"
4217 "If the optional argument, binary, is provided and is true, then the\n"
4218 "pickle will be written in binary format, which is more space and\n"
4219 "computationally efficient. \n"
4220 },
4221 {"load", (PyCFunction)cpm_load, 1,
4222 "load(file) -- Load a pickle from the given file"},
4223 {"loads", (PyCFunction)cpm_loads, 1,
4224 "loads(string) -- Load a pickle from the given string"},
4225 {"Pickler", (PyCFunction)get_Pickler, 1,
4226 "Pickler(file, [binary]) -- Create a pickler\n"
4227 "\n"
4228 "If the optional argument, binary, is provided and is true, then\n"
4229 "pickles will be written in binary format, which is more space and\n"
4230 "computationally efficient. \n"
4231 },
4232 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4233 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004234 { NULL, NULL }
4235};
4236
4237
Guido van Rossum60456fd1997-04-09 17:36:32 +00004238#define CHECK_FOR_ERRORS(MESS) \
Guido van Rossum053b8df1998-11-25 16:18:00 +00004239if (PyErr_Occurred()) { \
Guido van Rossum60456fd1997-04-09 17:36:32 +00004240 PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \
4241 PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \
4242 fprintf(stderr, # MESS ":\n\t"); \
4243 PyObject_Print(__sys_exc_type, stderr,0); \
4244 fprintf(stderr,", "); \
4245 PyObject_Print(__sys_exc_value, stderr,0); \
4246 fprintf(stderr,"\n"); \
4247 fflush(stderr); \
4248 Py_FatalError(# MESS); \
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004249}
4250
4251
Guido van Rossum60456fd1997-04-09 17:36:32 +00004252static int
4253init_stuff(PyObject *module, PyObject *module_dict) {
Guido van Rossumc03158b1999-06-09 15:23:31 +00004254 PyObject *string, *copy_reg, *t;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004255
4256#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4257
4258 INIT_STR(__class__);
4259 INIT_STR(__getinitargs__);
4260 INIT_STR(__dict__);
4261 INIT_STR(__getstate__);
4262 INIT_STR(__setstate__);
4263 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004264 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004265 INIT_STR(__reduce__);
4266 INIT_STR(write);
4267 INIT_STR(__safe_for_unpickling__);
4268 INIT_STR(append);
4269 INIT_STR(read);
4270 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004271 INIT_STR(copy_reg);
4272 INIT_STR(dispatch_table);
4273 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004274 INIT_STR(__basicnew__);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004275 UNLESS (empty_str=PyString_FromString("")) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004276
Guido van Rossum053b8df1998-11-25 16:18:00 +00004277 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004278 return -1;
4279
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004280 /* These next few are special because we want to use different
4281 ones in restricted mode. */
4282
Guido van Rossum053b8df1998-11-25 16:18:00 +00004283 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004284 return -1;
4285
Guido van Rossum053b8df1998-11-25 16:18:00 +00004286 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4287 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004288 return -1;
4289
4290 Py_DECREF(copy_reg);
4291
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004292 /* Down to here ********************************** */
4293
Guido van Rossum053b8df1998-11-25 16:18:00 +00004294 UNLESS (string = PyImport_ImportModule("string"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004295 return -1;
4296
Guido van Rossum053b8df1998-11-25 16:18:00 +00004297 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004298 return -1;
4299
4300 Py_DECREF(string);
4301
Guido van Rossum053b8df1998-11-25 16:18:00 +00004302 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004303 return -1;
4304
Guido van Rossumc03158b1999-06-09 15:23:31 +00004305 /* Ugh */
4306 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4307 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4308 return -1;
4309
4310 UNLESS (t=PyDict_New()) return -1;
4311 if (PyRun_String("def __init__(self, *args): self.args=args\n\n"
4312 "def __str__(self):\n"
4313 " return self.args and ('%s' % self.args[0]) or '???'\n",
4314 Py_file_input,
4315 module_dict, t)
4316 < 0) return -1;
4317
4318 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4319 return -1;
4320
4321 Py_DECREF(t);
4322
4323
4324 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
4325 PickleError, NULL))
4326 return -1;
4327
4328 UNLESS (t=PyDict_New()) return -1;
4329 if (PyRun_String("def __init__(self, *args): self.args=args\n\n"
4330 "def __str__(self):\n"
4331 " a=self.args\n"
4332 " a=a and type(a[0]) or '(what)'\n"
4333 " return 'Cannot pickle %s objects' % a\n"
4334 , Py_file_input,
4335 module_dict, t)
4336 < 0) return -1;
4337
4338 UNLESS (UnpickleableError = PyErr_NewException(
4339 "cPickle.UnpickleableError", PicklingError, t))
4340 return -1;
4341
4342 Py_DECREF(t);
4343
4344 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
4345 PickleError, NULL))
4346 return -1;
4347
4348 if (PyDict_SetItemString(module_dict, "PickleError",
4349 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004350 return -1;
4351
4352 if (PyDict_SetItemString(module_dict, "PicklingError",
4353 PicklingError) < 0)
4354 return -1;
4355
Guido van Rossum60456fd1997-04-09 17:36:32 +00004356 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4357 UnpicklingError) < 0)
4358 return -1;
4359
Guido van Rossumc03158b1999-06-09 15:23:31 +00004360 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4361 UnpickleableError) < 0)
4362 return -1;
4363
Guido van Rossum053b8df1998-11-25 16:18:00 +00004364 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4365 return -1;
4366
4367 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4368 BadPickleGet) < 0)
4369 return -1;
4370
Guido van Rossum60456fd1997-04-09 17:36:32 +00004371 PycString_IMPORT;
4372
4373 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004374}
4375
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004376#ifndef DL_EXPORT /* declarations for DLL import/export */
4377#define DL_EXPORT(RTYPE) RTYPE
4378#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004379DL_EXPORT(void)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004380initcPickle() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004381 PyObject *m, *d, *v;
Guido van Rossumc03158b1999-06-09 15:23:31 +00004382 char *rev="1.67";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004383 PyObject *format_version;
4384 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004385
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004386 Picklertype.ob_type = &PyType_Type;
4387 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004388 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004389
Guido van Rossum60456fd1997-04-09 17:36:32 +00004390 /* Create the module and add the functions */
4391 m = Py_InitModule4("cPickle", cPickle_methods,
4392 cPickle_module_documentation,
4393 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004394
Guido van Rossum60456fd1997-04-09 17:36:32 +00004395 /* Add some symbolic constants to the module */
4396 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004397 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004398 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004399
Guido van Rossum60456fd1997-04-09 17:36:32 +00004400 format_version = PyString_FromString("1.3");
4401 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004402
Guido van Rossum60456fd1997-04-09 17:36:32 +00004403 PyDict_SetItemString(d, "format_version", format_version);
4404 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004405 Py_XDECREF(format_version);
4406 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004407
Guido van Rossum60456fd1997-04-09 17:36:32 +00004408 init_stuff(m, d);
4409 CHECK_FOR_ERRORS("can't initialize module cPickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004410}