blob: 0737d947d0609a68719e119d5edf76e1aa07271e [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001/*
Guido van Rossum2f80d961999-07-13 15:18:58 +00002 * cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp
Guido van Rossum053b8df1998-11-25 16:18:00 +00003 *
4 * Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * o Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the disclaimer that follows.
13 *
14 * o Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions, and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * o Neither the name of Digital Creations nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 *
24 * THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
25 * IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
28 * CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
31 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
35 * DAMAGE.
36 *
37 #
38 # If you have questions regarding this software, contact:
39 #
40 # Digital Creations, L.C.
41 # 910 Princess Ann Street
42 # Fredericksburge, Virginia 22401
43 #
44 # info@digicool.com
45 #
46 # (540) 371-6909
47 */
Guido van Rossum2f4caa41997-01-06 22:59:08 +000048
49static char cPickle_module_documentation[] =
Guido van Rossum142eeb81997-08-13 03:14:41 +000050"C implementation and optimization of the Python pickle module\n"
51"\n"
Guido van Rossum2f80d961999-07-13 15:18:58 +000052"cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp\n"
Guido van Rossum2f4caa41997-01-06 22:59:08 +000053;
54
55#include "Python.h"
56#include "cStringIO.h"
Guido van Rossum60456fd1997-04-09 17:36:32 +000057#include "mymath.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +000058
Guido van Rossum142eeb81997-08-13 03:14:41 +000059#ifndef Py_eval_input
60#include <graminit.h>
61#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000062#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000063
Guido van Rossum2f4caa41997-01-06 22:59:08 +000064#include <errno.h>
65
Guido van Rossum2f4caa41997-01-06 22:59:08 +000066#define UNLESS(E) if (!(E))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000067
Guido van Rossum60456fd1997-04-09 17:36:32 +000068#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000069
Guido van Rossum60456fd1997-04-09 17:36:32 +000070#define WRITE_BUF_SIZE 256
71
72
73#define MARK '('
74#define STOP '.'
75#define POP '0'
76#define POP_MARK '1'
77#define DUP '2'
78#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000079#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000080#define INT 'I'
81#define BININT 'J'
82#define BININT1 'K'
83#define LONG 'L'
84#define BININT2 'M'
85#define NONE 'N'
86#define PERSID 'P'
87#define BINPERSID 'Q'
88#define REDUCE 'R'
89#define STRING 'S'
90#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000091#define SHORT_BINSTRING 'U'
Guido van 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
Guido van Rossum053b8df1998-11-25 16:18:00 +0000603 UNLESS (bytes = PyInt_FromLong(n)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000604
Guido van Rossum053b8df1998-11-25 16:18:00 +0000605 ARG_TUP(self, bytes);
606 if (self->arg) {
607 str = PyObject_CallObject(self->read, self->arg);
608 FREE_ARG_TUP(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000609 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000610 if (! str) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000611
612 Py_XDECREF(self->last_string);
613 self->last_string = str;
614
Guido van Rossum053b8df1998-11-25 16:18:00 +0000615 if (! (*s = PyString_AsString(str))) return -1;
616 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000617}
618
619
620static int
621readline_other(Unpicklerobject *self, char **s) {
622 PyObject *str;
623 int str_size;
624
Guido van Rossum053b8df1998-11-25 16:18:00 +0000625 UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000626 return -1;
627 }
628
Guido van Rossum053b8df1998-11-25 16:18:00 +0000629 if ((str_size = PyString_Size(str)) < 0)
630 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000631
632 Py_XDECREF(self->last_string);
633 self->last_string = str;
634
Guido van Rossum053b8df1998-11-25 16:18:00 +0000635 if (! (*s = PyString_AsString(str)))
636 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000637
638 return str_size;
639}
640
641
642static char *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000643pystrndup(char *s, int l) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000644 char *r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000645 UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
Guido van Rossum60456fd1997-04-09 17:36:32 +0000646 memcpy(r,s,l);
647 r[l]=0;
648 return r;
649}
650
651
652static int
653get(Picklerobject *self, PyObject *id) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000654 PyObject *value, *mv;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000655 long c_value;
656 char s[30];
657 int len;
658
Guido van Rossum053b8df1998-11-25 16:18:00 +0000659 UNLESS (mv = PyDict_GetItem(self->memo, id)) {
660 PyErr_SetObject(PyExc_KeyError, id);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000661 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000662 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000663
Guido van Rossum053b8df1998-11-25 16:18:00 +0000664 UNLESS (value = PyTuple_GetItem(mv, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000665 return -1;
666
Guido van Rossum053b8df1998-11-25 16:18:00 +0000667 UNLESS (PyInt_Check(value)) {
668 PyErr_SetString(PicklingError, "no int where int expected in memo");
669 return -1;
670 }
671 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000672
673 if (!self->bin) {
674 s[0] = GET;
675 sprintf(s + 1, "%ld\n", c_value);
676 len = strlen(s);
677 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000678 else if (Pdata_Check(self->file)) {
679 if (write_other(self, NULL, 0) < 0) return -1;
680 PDATA_APPEND(self->file, mv, -1);
681 return 0;
682 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000683 else {
684 if (c_value < 256) {
685 s[0] = BINGET;
686 s[1] = (int)(c_value & 0xff);
687 len = 2;
688 }
689 else {
690 s[0] = LONG_BINGET;
691 s[1] = (int)(c_value & 0xff);
692 s[2] = (int)((c_value >> 8) & 0xff);
693 s[3] = (int)((c_value >> 16) & 0xff);
694 s[4] = (int)((c_value >> 24) & 0xff);
695 len = 5;
696 }
697 }
698
699 if ((*self->write_func)(self, s, len) < 0)
700 return -1;
701
702 return 0;
703}
704
705
706static int
707put(Picklerobject *self, PyObject *ob) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000708 if (ob->ob_refcnt < 2 || self->fast)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000709 return 0;
710
711 return put2(self, ob);
712}
713
714
715static int
716put2(Picklerobject *self, PyObject *ob) {
717 char c_str[30];
718 int p, len, res = -1;
719 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000720
721 if (self->fast) return 0;
722
Guido van Rossum60456fd1997-04-09 17:36:32 +0000723 if ((p = PyDict_Size(self->memo)) < 0)
724 goto finally;
725
Guido van Rossum053b8df1998-11-25 16:18:00 +0000726 p++; /* Make sure memo keys are positive! */
727
728 UNLESS (py_ob_id = PyInt_FromLong((long)ob))
729 goto finally;
730
731 UNLESS (memo_len = PyInt_FromLong(p))
732 goto finally;
733
734 UNLESS (t = PyTuple_New(2))
735 goto finally;
736
737 PyTuple_SET_ITEM(t, 0, memo_len);
738 Py_INCREF(memo_len);
739 PyTuple_SET_ITEM(t, 1, ob);
740 Py_INCREF(ob);
741
742 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
743 goto finally;
744
Guido van Rossum60456fd1997-04-09 17:36:32 +0000745 if (!self->bin) {
746 c_str[0] = PUT;
747 sprintf(c_str + 1, "%d\n", p);
748 len = strlen(c_str);
749 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000750 else if (Pdata_Check(self->file)) {
751 if (write_other(self, NULL, 0) < 0) return -1;
752 PDATA_APPEND(self->file, memo_len, -1);
753 res=0; /* Job well done ;) */
754 goto finally;
755 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000756 else {
757 if (p >= 256) {
758 c_str[0] = LONG_BINPUT;
759 c_str[1] = (int)(p & 0xff);
760 c_str[2] = (int)((p >> 8) & 0xff);
761 c_str[3] = (int)((p >> 16) & 0xff);
762 c_str[4] = (int)((p >> 24) & 0xff);
763 len = 5;
764 }
765 else {
766 c_str[0] = BINPUT;
767 c_str[1] = p;
768 len = 2;
769 }
770 }
771
772 if ((*self->write_func)(self, c_str, len) < 0)
773 goto finally;
774
Guido van Rossum60456fd1997-04-09 17:36:32 +0000775 res = 0;
776
777finally:
778 Py_XDECREF(py_ob_id);
779 Py_XDECREF(memo_len);
780 Py_XDECREF(t);
781
782 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000783}
784
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000785#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000786
787static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000788PyImport_Import(PyObject *module_name) {
789 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
790 static PyObject *standard_builtins=0;
791 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
792
Guido van Rossum053b8df1998-11-25 16:18:00 +0000793 UNLESS (silly_list) {
794 UNLESS (__import___str=PyString_FromString("__import__"))
795 return NULL;
796 UNLESS (__builtins___str=PyString_FromString("__builtins__"))
797 return NULL;
798 UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
799 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000800 }
801
Guido van Rossum053b8df1998-11-25 16:18:00 +0000802 if ((globals=PyEval_GetGlobals())) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000803 Py_INCREF(globals);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000804 UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
805 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000806 }
807 else {
808 PyErr_Clear();
809
Guido van Rossum053b8df1998-11-25 16:18:00 +0000810 UNLESS (standard_builtins ||
811 (standard_builtins=PyImport_ImportModule("__builtin__")))
812 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000813
814 __builtins__=standard_builtins;
815 Py_INCREF(__builtins__);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000816 UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
817 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000818 }
819
Guido van Rossum053b8df1998-11-25 16:18:00 +0000820 if (PyDict_Check(__builtins__)) {
821 UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000822 }
823 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000824 UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000825 }
826
Guido van Rossum053b8df1998-11-25 16:18:00 +0000827 UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
828 module_name, globals, globals, silly_list))
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000829 goto err;
830
831 Py_DECREF(globals);
832 Py_DECREF(__builtins__);
833 Py_DECREF(__import__);
834
835 return r;
836err:
837 Py_XDECREF(globals);
838 Py_XDECREF(__builtins__);
839 Py_XDECREF(__import__);
840 return NULL;
841}
842
843static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +0000844whichmodule(PyObject *global, PyObject *global_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000845 int i, j;
846 PyObject *module = 0, *modules_dict = 0,
847 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000848
Guido van Rossum45188231997-09-28 05:38:51 +0000849 module = PyObject_GetAttrString(global, "__module__");
850 if (module) return module;
851 PyErr_Clear();
852
Guido van Rossum053b8df1998-11-25 16:18:00 +0000853 UNLESS (modules_dict = PySys_GetObject("modules"))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000854 return NULL;
855
Guido van Rossum60456fd1997-04-09 17:36:32 +0000856 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000857 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
858
Guido van Rossum053b8df1998-11-25 16:18:00 +0000859 if (PyObject_Compare(name, __main___str)==0) continue;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000860
Guido van Rossum053b8df1998-11-25 16:18:00 +0000861 UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000862 PyErr_Clear();
863 continue;
864 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000865
Guido van Rossum60456fd1997-04-09 17:36:32 +0000866 if (global_name_attr != global) {
867 Py_DECREF(global_name_attr);
868 continue;
869 }
870
871 Py_DECREF(global_name_attr);
872
873 break;
874 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000875
876 /* The following implements the rule in pickle.py added in 1.5
877 that used __main__ if no module is found. I don't actually
878 like this rule. jlf
879 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000880 if (!j) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000881 j=1;
882 name=__main___str;
883 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000884
885 Py_INCREF(name);
886 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000887}
888
889
Guido van Rossum60456fd1997-04-09 17:36:32 +0000890static int
891save_none(Picklerobject *self, PyObject *args) {
892 static char none = NONE;
893 if ((*self->write_func)(self, &none, 1) < 0)
894 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000895
Guido van Rossum60456fd1997-04-09 17:36:32 +0000896 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000897}
898
899
Guido van Rossum60456fd1997-04-09 17:36:32 +0000900static int
901save_int(Picklerobject *self, PyObject *args) {
902 char c_str[32];
903 long l = PyInt_AS_LONG((PyIntObject *)args);
904 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000905
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000906 if (!self->bin
907#if SIZEOF_LONG > 4
Guido van Rossum053b8df1998-11-25 16:18:00 +0000908 || (l >> 32)
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000909#endif
Guido van Rossum053b8df1998-11-25 16:18:00 +0000910 ) {
911 /* Save extra-long ints in non-binary mode, so that
912 we can use python long parsing code to restore,
913 if necessary. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000914 c_str[0] = INT;
915 sprintf(c_str + 1, "%ld\n", l);
916 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
917 return -1;
918 }
919 else {
920 c_str[1] = (int)( l & 0xff);
921 c_str[2] = (int)((l >> 8) & 0xff);
922 c_str[3] = (int)((l >> 16) & 0xff);
923 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000924
Guido van Rossum60456fd1997-04-09 17:36:32 +0000925 if ((c_str[4] == 0) && (c_str[3] == 0)) {
926 if (c_str[2] == 0) {
927 c_str[0] = BININT1;
928 len = 2;
929 }
930 else {
931 c_str[0] = BININT2;
932 len = 3;
933 }
934 }
935 else {
936 c_str[0] = BININT;
937 len = 5;
938 }
939
940 if ((*self->write_func)(self, c_str, len) < 0)
941 return -1;
942 }
943
944 return 0;
945}
946
947
948static int
949save_long(Picklerobject *self, PyObject *args) {
950 int size, res = -1;
951 PyObject *repr = 0;
952
953 static char l = LONG;
954
Guido van Rossum053b8df1998-11-25 16:18:00 +0000955 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000956 goto finally;
957
958 if ((size = PyString_Size(repr)) < 0)
959 goto finally;
960
961 if ((*self->write_func)(self, &l, 1) < 0)
962 goto finally;
963
964 if ((*self->write_func)(self,
965 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
966 goto finally;
967
968 if ((*self->write_func)(self, "\n", 1) < 0)
969 goto finally;
970
971 res = 0;
972
973finally:
974 Py_XDECREF(repr);
975
976 return res;
977}
978
979
980static int
981save_float(Picklerobject *self, PyObject *args) {
982 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
983
Guido van Rossum60456fd1997-04-09 17:36:32 +0000984 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000985 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000986 double f;
987 long fhi, flo;
988 char str[9], *p = str;
989
990 *p = BINFLOAT;
991 p++;
992
993 if (x < 0) {
994 s = 1;
995 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000996 }
997 else
Guido van Rossum60456fd1997-04-09 17:36:32 +0000998 s = 0;
999
1000 f = frexp(x, &e);
1001
1002 /* Normalize f to be in the range [1.0, 2.0) */
1003 if (0.5 <= f && f < 1.0) {
1004 f *= 2.0;
1005 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001006 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001007 else if (f == 0.0) {
1008 e = 0;
1009 }
1010 else {
1011 PyErr_SetString(PyExc_SystemError,
1012 "frexp() result out of range");
1013 return -1;
1014 }
1015
1016 if (e >= 1024) {
1017 /* XXX 1024 itself is reserved for Inf/NaN */
1018 PyErr_SetString(PyExc_OverflowError,
1019 "float too large to pack with d format");
1020 return -1;
1021 }
1022 else if (e < -1022) {
1023 /* Gradual underflow */
1024 f = ldexp(f, 1022 + e);
1025 e = 0;
1026 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001027 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001028 e += 1023;
1029 f -= 1.0; /* Get rid of leading 1 */
1030 }
1031
1032 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1033 f *= 268435456.0; /* 2**28 */
1034 fhi = (long) floor(f); /* Truncate */
1035 f -= (double)fhi;
1036 f *= 16777216.0; /* 2**24 */
1037 flo = (long) floor(f + 0.5); /* Round */
1038
1039 /* First byte */
1040 *p = (s<<7) | (e>>4);
1041 p++;
1042
1043 /* Second byte */
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001044 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum60456fd1997-04-09 17:36:32 +00001045 p++;
1046
1047 /* Third byte */
1048 *p = (fhi>>16) & 0xFF;
1049 p++;
1050
1051 /* Fourth byte */
1052 *p = (fhi>>8) & 0xFF;
1053 p++;
1054
1055 /* Fifth byte */
1056 *p = fhi & 0xFF;
1057 p++;
1058
1059 /* Sixth byte */
1060 *p = (flo>>16) & 0xFF;
1061 p++;
1062
1063 /* Seventh byte */
1064 *p = (flo>>8) & 0xFF;
1065 p++;
1066
1067 /* Eighth byte */
1068 *p = flo & 0xFF;
1069
1070 if ((*self->write_func)(self, str, 9) < 0)
1071 return -1;
1072 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001073 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001074 char c_str[250];
1075 c_str[0] = FLOAT;
Guido van Rossum104be4a1998-04-03 21:13:02 +00001076 sprintf(c_str + 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001077
1078 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
1079 return -1;
1080 }
1081
1082 return 0;
1083}
1084
1085
1086static int
Guido van Rossum142eeb81997-08-13 03:14:41 +00001087save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001088 int size, len;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001089 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001090
Guido van Rossum053b8df1998-11-25 16:18:00 +00001091 if ((size = PyString_Size(args)) < 0)
1092 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001093
1094 if (!self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001095 char *repr_str;
1096
1097 static char string = STRING;
1098
Guido van Rossum053b8df1998-11-25 16:18:00 +00001099 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001100 return -1;
1101
Guido van Rossum053b8df1998-11-25 16:18:00 +00001102 if ((len = PyString_Size(repr)) < 0)
1103 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001104 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001105
1106 if ((*self->write_func)(self, &string, 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001107 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001108
1109 if ((*self->write_func)(self, repr_str, len) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001110 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001111
1112 if ((*self->write_func)(self, "\n", 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001113 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001114
1115 Py_XDECREF(repr);
1116 }
1117 else {
1118 int i;
1119 char c_str[5];
1120
Guido van Rossum053b8df1998-11-25 16:18:00 +00001121 if ((size = PyString_Size(args)) < 0)
1122 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001123
1124 if (size < 256) {
1125 c_str[0] = SHORT_BINSTRING;
1126 c_str[1] = size;
1127 len = 2;
1128 }
1129 else {
1130 c_str[0] = BINSTRING;
1131 for (i = 1; i < 5; i++)
1132 c_str[i] = (int)(size >> ((i - 1) * 8));
1133 len = 5;
1134 }
1135
1136 if ((*self->write_func)(self, c_str, len) < 0)
1137 return -1;
1138
Guido van Rossum053b8df1998-11-25 16:18:00 +00001139 if (size > 128 && Pdata_Check(self->file)) {
1140 if (write_other(self, NULL, 0) < 0) return -1;
1141 PDATA_APPEND(self->file, args, -1);
1142 }
1143 else {
1144 if ((*self->write_func)(self,
1145 PyString_AS_STRING((PyStringObject *)args), size) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001146 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001147 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001148 }
1149
Guido van Rossum142eeb81997-08-13 03:14:41 +00001150 if (doput)
1151 if (put(self, args) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001152 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001153
1154 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001155
1156err:
1157 Py_XDECREF(repr);
1158 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001159}
1160
1161
1162static int
1163save_tuple(Picklerobject *self, PyObject *args) {
1164 PyObject *element = 0, *py_tuple_id = 0;
1165 int len, i, has_key, res = -1;
1166
1167 static char tuple = TUPLE;
1168
1169 if ((*self->write_func)(self, &MARKv, 1) < 0)
1170 goto finally;
1171
1172 if ((len = PyTuple_Size(args)) < 0)
1173 goto finally;
1174
1175 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001176 UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001177 goto finally;
1178
1179 if (save(self, element, 0) < 0)
1180 goto finally;
1181 }
1182
Guido van Rossum053b8df1998-11-25 16:18:00 +00001183 UNLESS (py_tuple_id = PyInt_FromLong((long)args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001184 goto finally;
1185
1186 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001187 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001188 goto finally;
1189
1190 if (has_key) {
1191 if (self->bin) {
1192 static char pop_mark = POP_MARK;
1193
1194 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1195 goto finally;
1196 }
1197 else {
1198 static char pop = POP;
1199
1200 for (i = 0; i <= len; i++) {
1201 if ((*self->write_func)(self, &pop, 1) < 0)
1202 goto finally;
1203 }
1204 }
1205
1206 if (get(self, py_tuple_id) < 0)
1207 goto finally;
1208
1209 res = 0;
1210 goto finally;
1211 }
1212 }
1213
1214 if ((*self->write_func)(self, &tuple, 1) < 0) {
1215 goto finally;
1216 }
1217
1218 if (put(self, args) < 0)
1219 goto finally;
1220
1221 res = 0;
1222
1223finally:
1224 Py_XDECREF(py_tuple_id);
1225
1226 return res;
1227}
1228
1229static int
1230save_empty_tuple(Picklerobject *self, PyObject *args) {
1231 static char tuple = EMPTY_TUPLE;
1232
1233 return (*self->write_func)(self, &tuple, 1);
1234}
1235
1236
1237static int
1238save_list(Picklerobject *self, PyObject *args) {
1239 PyObject *element = 0;
1240 int s_len, len, i, using_appends, res = -1;
1241 char s[3];
1242
1243 static char append = APPEND, appends = APPENDS;
1244
Guido van Rossum053b8df1998-11-25 16:18:00 +00001245 if (self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001246 s[0] = EMPTY_LIST;
1247 s_len = 1;
1248 }
1249 else {
1250 s[0] = MARK;
1251 s[1] = LIST;
1252 s_len = 2;
1253 }
1254
1255 if ((len = PyList_Size(args)) < 0)
1256 goto finally;
1257
1258 if ((*self->write_func)(self, s, s_len) < 0)
1259 goto finally;
1260
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001261 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001262 if (put(self, args) < 0)
1263 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001264 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001265 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001266 if (put2(self, args) < 0)
1267 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001268 }
1269
Guido van Rossum142eeb81997-08-13 03:14:41 +00001270 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001271 if ((*self->write_func)(self, &MARKv, 1) < 0)
1272 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001273
Guido van Rossum60456fd1997-04-09 17:36:32 +00001274 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001275 UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001276 goto finally;
1277
1278 if (save(self, element, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001279 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001280
1281 if (!using_appends) {
1282 if ((*self->write_func)(self, &append, 1) < 0)
1283 goto finally;
1284 }
1285 }
1286
1287 if (using_appends) {
1288 if ((*self->write_func)(self, &appends, 1) < 0)
1289 goto finally;
1290 }
1291
1292 res = 0;
1293
1294finally:
1295
1296 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001297}
1298
1299
Guido van Rossum60456fd1997-04-09 17:36:32 +00001300static int
1301save_dict(Picklerobject *self, PyObject *args) {
1302 PyObject *key = 0, *value = 0;
1303 int i, len, res = -1, using_setitems;
1304 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001305
Guido van Rossum60456fd1997-04-09 17:36:32 +00001306 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001307
Guido van Rossum60456fd1997-04-09 17:36:32 +00001308 if (self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001309 s[0] = EMPTY_DICT;
1310 len = 1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001311 }
1312 else {
1313 s[0] = MARK;
1314 s[1] = DICT;
1315 len = 2;
1316 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001317
Guido van Rossum60456fd1997-04-09 17:36:32 +00001318 if ((*self->write_func)(self, s, len) < 0)
1319 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001320
Guido van Rossum60456fd1997-04-09 17:36:32 +00001321 if ((len = PyDict_Size(args)) < 0)
1322 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001323
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001324 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001325 if (put(self, args) < 0)
1326 goto finally;
1327 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001328 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001329 if (put2(self, args) < 0)
1330 goto finally;
1331 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001332
Guido van Rossum142eeb81997-08-13 03:14:41 +00001333 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001334 if ((*self->write_func)(self, &MARKv, 1) < 0)
1335 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001336
Guido van Rossum60456fd1997-04-09 17:36:32 +00001337 i = 0;
1338 while (PyDict_Next(args, &i, &key, &value)) {
1339 if (save(self, key, 0) < 0)
1340 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001341
Guido van Rossum60456fd1997-04-09 17:36:32 +00001342 if (save(self, value, 0) < 0)
1343 goto finally;
1344
1345 if (!using_setitems) {
1346 if ((*self->write_func)(self, &setitem, 1) < 0)
1347 goto finally;
1348 }
1349 }
1350
1351 if (using_setitems) {
1352 if ((*self->write_func)(self, &setitems, 1) < 0)
1353 goto finally;
1354 }
1355
1356 res = 0;
1357
1358finally:
1359
1360 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001361}
1362
1363
Guido van Rossum60456fd1997-04-09 17:36:32 +00001364static int
1365save_inst(Picklerobject *self, PyObject *args) {
1366 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1367 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1368 char *module_str, *name_str;
1369 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001370
Guido van Rossum60456fd1997-04-09 17:36:32 +00001371 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001372
Guido van Rossum60456fd1997-04-09 17:36:32 +00001373 if ((*self->write_func)(self, &MARKv, 1) < 0)
1374 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001375
Guido van Rossum053b8df1998-11-25 16:18:00 +00001376 UNLESS (class = PyObject_GetAttr(args, __class___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001377 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001378
Guido van Rossum60456fd1997-04-09 17:36:32 +00001379 if (self->bin) {
1380 if (save(self, class, 0) < 0)
1381 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001382 }
1383
Guido van Rossum142eeb81997-08-13 03:14:41 +00001384 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001385 PyObject *element = 0;
1386 int i, len;
1387
Guido van Rossum053b8df1998-11-25 16:18:00 +00001388 UNLESS (class_args =
Guido van Rossum60456fd1997-04-09 17:36:32 +00001389 PyObject_CallObject(getinitargs_func, empty_tuple))
1390 goto finally;
1391
1392 if ((len = PyObject_Length(class_args)) < 0)
1393 goto finally;
1394
1395 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001396 UNLESS (element = PySequence_GetItem(class_args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001397 goto finally;
1398
1399 if (save(self, element, 0) < 0) {
1400 Py_DECREF(element);
1401 goto finally;
1402 }
1403
1404 Py_DECREF(element);
1405 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001406 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001407 else {
1408 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001409 }
1410
Guido van Rossum60456fd1997-04-09 17:36:32 +00001411 if (!self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001412 UNLESS (name = ((PyClassObject *)class)->cl_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001413 PyErr_SetString(PicklingError, "class has no name");
1414 goto finally;
1415 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001416
Guido van Rossum053b8df1998-11-25 16:18:00 +00001417 UNLESS (module = whichmodule(class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001418 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001419
1420
1421 if ((module_size = PyString_Size(module)) < 0 ||
1422 (name_size = PyString_Size(name)) < 0)
1423 goto finally;
1424
Guido van Rossum60456fd1997-04-09 17:36:32 +00001425 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001426 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001427
Guido van Rossum60456fd1997-04-09 17:36:32 +00001428 if ((*self->write_func)(self, &inst, 1) < 0)
1429 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001430
Guido van Rossum60456fd1997-04-09 17:36:32 +00001431 if ((*self->write_func)(self, module_str, module_size) < 0)
1432 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001433
Guido van Rossum60456fd1997-04-09 17:36:32 +00001434 if ((*self->write_func)(self, "\n", 1) < 0)
1435 goto finally;
1436
1437 if ((*self->write_func)(self, name_str, name_size) < 0)
1438 goto finally;
1439
1440 if ((*self->write_func)(self, "\n", 1) < 0)
1441 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001442 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001443 else if ((*self->write_func)(self, &obj, 1) < 0) {
1444 goto finally;
1445 }
1446
Guido van Rossum142eeb81997-08-13 03:14:41 +00001447 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001448 UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001449 goto finally;
1450 }
1451 else {
1452 PyErr_Clear();
1453
Guido van Rossum053b8df1998-11-25 16:18:00 +00001454 UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001455 PyErr_Clear();
1456 res = 0;
1457 goto finally;
1458 }
1459 }
1460
1461 if (!PyDict_Check(state)) {
1462 if (put2(self, args) < 0)
1463 goto finally;
1464 }
1465 else {
1466 if (put(self, args) < 0)
1467 goto finally;
1468 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001469
Guido van Rossum60456fd1997-04-09 17:36:32 +00001470 if (save(self, state, 0) < 0)
1471 goto finally;
1472
1473 if ((*self->write_func)(self, &build, 1) < 0)
1474 goto finally;
1475
1476 res = 0;
1477
1478finally:
1479 Py_XDECREF(module);
1480 Py_XDECREF(class);
1481 Py_XDECREF(state);
1482 Py_XDECREF(getinitargs_func);
1483 Py_XDECREF(getstate_func);
1484 Py_XDECREF(class_args);
1485
1486 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001487}
1488
1489
Guido van Rossum60456fd1997-04-09 17:36:32 +00001490static int
1491save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1492 PyObject *global_name = 0, *module = 0;
1493 char *name_str, *module_str;
1494 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001495
Guido van Rossum60456fd1997-04-09 17:36:32 +00001496 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001497
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001498 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001499 global_name = name;
1500 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001501 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001502 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001503 UNLESS (global_name = PyObject_GetAttr(args, __name___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001504 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001505 }
1506
Guido van Rossum053b8df1998-11-25 16:18:00 +00001507 UNLESS (module = whichmodule(args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001508 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001509
Guido van Rossum053b8df1998-11-25 16:18:00 +00001510 if ((module_size = PyString_Size(module)) < 0 ||
1511 (name_size = PyString_Size(global_name)) < 0)
1512 goto finally;
1513
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001514 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001515 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001516
Guido van Rossum60456fd1997-04-09 17:36:32 +00001517 if ((*self->write_func)(self, &global, 1) < 0)
1518 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001519
Guido van Rossum60456fd1997-04-09 17:36:32 +00001520 if ((*self->write_func)(self, module_str, module_size) < 0)
1521 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001522
Guido van Rossum60456fd1997-04-09 17:36:32 +00001523 if ((*self->write_func)(self, "\n", 1) < 0)
1524 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001525
Guido van Rossum60456fd1997-04-09 17:36:32 +00001526 if ((*self->write_func)(self, name_str, name_size) < 0)
1527 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001528
Guido van Rossum60456fd1997-04-09 17:36:32 +00001529 if ((*self->write_func)(self, "\n", 1) < 0)
1530 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001531
Guido van Rossum60456fd1997-04-09 17:36:32 +00001532 if (put(self, args) < 0)
1533 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001534
Guido van Rossum60456fd1997-04-09 17:36:32 +00001535 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001536
Guido van Rossum60456fd1997-04-09 17:36:32 +00001537finally:
1538 Py_XDECREF(module);
1539 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001540
Guido van Rossum60456fd1997-04-09 17:36:32 +00001541 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001542}
1543
Guido van Rossum60456fd1997-04-09 17:36:32 +00001544static int
1545save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1546 PyObject *pid = 0;
1547 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001548
Guido van Rossum60456fd1997-04-09 17:36:32 +00001549 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001550
Guido van Rossum053b8df1998-11-25 16:18:00 +00001551 Py_INCREF(args);
1552 ARG_TUP(self, args);
1553 if (self->arg) {
1554 pid = PyObject_CallObject(f, self->arg);
1555 FREE_ARG_TUP(self);
1556 }
1557 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001558
Guido van Rossum60456fd1997-04-09 17:36:32 +00001559 if (pid != Py_None) {
1560 if (!self->bin) {
1561 if (!PyString_Check(pid)) {
1562 PyErr_SetString(PicklingError,
1563 "persistent id must be string");
1564 goto finally;
1565 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001566
Guido van Rossum60456fd1997-04-09 17:36:32 +00001567 if ((*self->write_func)(self, &persid, 1) < 0)
1568 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001569
Guido van Rossum60456fd1997-04-09 17:36:32 +00001570 if ((size = PyString_Size(pid)) < 0)
1571 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001572
Guido van Rossum60456fd1997-04-09 17:36:32 +00001573 if ((*self->write_func)(self,
1574 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1575 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001576
Guido van Rossum60456fd1997-04-09 17:36:32 +00001577 if ((*self->write_func)(self, "\n", 1) < 0)
1578 goto finally;
1579
1580 res = 1;
1581 goto finally;
1582 }
1583 else if (save(self, pid, 1) >= 0) {
1584 if ((*self->write_func)(self, &binpersid, 1) < 0)
1585 res = -1;
1586 else
1587 res = 1;
1588 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001589
Guido van Rossum60456fd1997-04-09 17:36:32 +00001590 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001591 }
1592
Guido van Rossum60456fd1997-04-09 17:36:32 +00001593 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001594
Guido van Rossum60456fd1997-04-09 17:36:32 +00001595finally:
1596 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001597
Guido van Rossum60456fd1997-04-09 17:36:32 +00001598 return res;
1599}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001600
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001601
Guido van Rossum60456fd1997-04-09 17:36:32 +00001602static int
1603save_reduce(Picklerobject *self, PyObject *callable,
1604 PyObject *tup, PyObject *state, PyObject *ob) {
1605 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001606
Guido van Rossum60456fd1997-04-09 17:36:32 +00001607 if (save(self, callable, 0) < 0)
1608 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001609
Guido van Rossum60456fd1997-04-09 17:36:32 +00001610 if (save(self, tup, 0) < 0)
1611 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001612
Guido van Rossum60456fd1997-04-09 17:36:32 +00001613 if ((*self->write_func)(self, &reduce, 1) < 0)
1614 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001615
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001616 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001617 if (state && !PyDict_Check(state)) {
1618 if (put2(self, ob) < 0)
1619 return -1;
1620 }
1621 else {
1622 if (put(self, ob) < 0)
1623 return -1;
1624 }
1625 }
1626
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001627 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001628 if (save(self, state, 0) < 0)
1629 return -1;
1630
1631 if ((*self->write_func)(self, &build, 1) < 0)
1632 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001633 }
1634
Guido van Rossum60456fd1997-04-09 17:36:32 +00001635 return 0;
1636}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001637
Guido van Rossum60456fd1997-04-09 17:36:32 +00001638static int
1639save(Picklerobject *self, PyObject *args, int pers_save) {
1640 PyTypeObject *type;
1641 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001642 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001643 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001644
Guido van Rossum60456fd1997-04-09 17:36:32 +00001645 if (!pers_save && self->pers_func) {
1646 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1647 res = tmp;
1648 goto finally;
1649 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001650 }
1651
Guido van Rossum60456fd1997-04-09 17:36:32 +00001652 if (args == Py_None) {
1653 res = save_none(self, args);
1654 goto finally;
1655 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001656
Guido van Rossum60456fd1997-04-09 17:36:32 +00001657 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001658
Guido van Rossum60456fd1997-04-09 17:36:32 +00001659 switch (type->tp_name[0]) {
1660 case 'i':
1661 if (type == &PyInt_Type) {
1662 res = save_int(self, args);
1663 goto finally;
1664 }
1665 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001666
Guido van Rossum60456fd1997-04-09 17:36:32 +00001667 case 'l':
1668 if (type == &PyLong_Type) {
1669 res = save_long(self, args);
1670 goto finally;
1671 }
1672 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001673
Guido van Rossum60456fd1997-04-09 17:36:32 +00001674 case 'f':
1675 if (type == &PyFloat_Type) {
1676 res = save_float(self, args);
1677 goto finally;
1678 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001679 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001680
Guido van Rossum60456fd1997-04-09 17:36:32 +00001681 case 't':
1682 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001683 if (self->bin) res = save_empty_tuple(self, args);
1684 else res = save_tuple(self, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001685 goto finally;
1686 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001687 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001688
Guido van Rossum60456fd1997-04-09 17:36:32 +00001689 case 's':
Guido van Rossum053b8df1998-11-25 16:18:00 +00001690 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001691 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001692 goto finally;
1693 }
1694 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001695
Guido van Rossum60456fd1997-04-09 17:36:32 +00001696 if (args->ob_refcnt > 1) {
1697 long ob_id;
1698 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001699
Guido van Rossum60456fd1997-04-09 17:36:32 +00001700 ob_id = (long)args;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001701
Guido van Rossum053b8df1998-11-25 16:18:00 +00001702 UNLESS (py_ob_id = PyInt_FromLong(ob_id))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001703 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001704
Guido van Rossum60456fd1997-04-09 17:36:32 +00001705 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1706 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001707
Guido van Rossum60456fd1997-04-09 17:36:32 +00001708 if (has_key) {
1709 if (get(self, py_ob_id) < 0)
1710 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001711
Guido van Rossum60456fd1997-04-09 17:36:32 +00001712 res = 0;
1713 goto finally;
1714 }
1715 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001716
Guido van Rossum60456fd1997-04-09 17:36:32 +00001717 switch (type->tp_name[0]) {
1718 case 's':
1719 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001720 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001721 goto finally;
1722 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001723 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001724
Guido van Rossum60456fd1997-04-09 17:36:32 +00001725 case 't':
1726 if (type == &PyTuple_Type) {
1727 res = save_tuple(self, args);
1728 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001729 }
1730 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001731
Guido van Rossum60456fd1997-04-09 17:36:32 +00001732 case 'l':
1733 if (type == &PyList_Type) {
1734 res = save_list(self, args);
1735 goto finally;
1736 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001737 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001738
1739 case 'd':
1740 if (type == &PyDict_Type) {
1741 res = save_dict(self, args);
1742 goto finally;
1743 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001744 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001745
1746 case 'i':
1747 if (type == &PyInstance_Type) {
1748 res = save_inst(self, args);
1749 goto finally;
1750 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001751 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001752
1753 case 'c':
1754 if (type == &PyClass_Type) {
1755 res = save_global(self, args, NULL);
1756 goto finally;
1757 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001758 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001759
1760 case 'f':
1761 if (type == &PyFunction_Type) {
1762 res = save_global(self, args, NULL);
1763 goto finally;
1764 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001765 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001766
1767 case 'b':
1768 if (type == &PyCFunction_Type) {
1769 res = save_global(self, args, NULL);
1770 goto finally;
1771 }
1772 }
1773
1774 if (!pers_save && self->inst_pers_func) {
1775 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1776 res = tmp;
1777 goto finally;
1778 }
1779 }
1780
Guido van Rossum142eeb81997-08-13 03:14:41 +00001781 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001782 Py_INCREF(__reduce__);
1783
Guido van Rossum60456fd1997-04-09 17:36:32 +00001784 Py_INCREF(args);
Guido van Rossum053b8df1998-11-25 16:18:00 +00001785 ARG_TUP(self, args);
1786 if (self->arg) {
1787 t = PyObject_CallObject(__reduce__, self->arg);
1788 FREE_ARG_TUP(self);
1789 }
1790 if (! t) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001791 }
1792 else {
1793 PyErr_Clear();
1794
Guido van Rossum142eeb81997-08-13 03:14:41 +00001795 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001796 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001797 goto finally;
1798 }
1799 else {
1800 PyErr_Clear();
1801 }
1802 }
1803
1804 if (t) {
1805 if (PyString_Check(t)) {
1806 res = save_global(self, args, t);
1807 goto finally;
1808 }
1809
1810 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001811 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001812 "be a tuple", "O", __reduce__);
1813 goto finally;
1814 }
1815
1816 size = PyTuple_Size(t);
1817
1818 if ((size != 3) && (size != 2)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001819 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001820 "contain only two or three elements", "O", __reduce__);
1821 goto finally;
1822 }
1823
1824 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001825
Guido van Rossum60456fd1997-04-09 17:36:32 +00001826 arg_tup = PyTuple_GET_ITEM(t, 1);
1827
1828 if (size > 2) {
1829 state = PyTuple_GET_ITEM(t, 2);
1830 }
1831
Guido van Rossum053b8df1998-11-25 16:18:00 +00001832 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001833 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001834 "returned by %s must be a tuple", "O", __reduce__);
1835 goto finally;
1836 }
1837
1838 res = save_reduce(self, callable, arg_tup, state, args);
1839 goto finally;
1840 }
1841
Guido van Rossumc03158b1999-06-09 15:23:31 +00001842 PyErr_SetObject(UnpickleableError, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001843
1844finally:
1845 Py_XDECREF(py_ob_id);
1846 Py_XDECREF(__reduce__);
1847 Py_XDECREF(t);
1848
1849 return res;
1850}
1851
1852
1853static int
1854dump(Picklerobject *self, PyObject *args) {
1855 static char stop = STOP;
1856
1857 if (save(self, args, 0) < 0)
1858 return -1;
1859
1860 if ((*self->write_func)(self, &stop, 1) < 0)
1861 return -1;
1862
1863 if ((*self->write_func)(self, NULL, 0) < 0)
1864 return -1;
1865
1866 return 0;
1867}
1868
1869static PyObject *
Guido van Rossum053b8df1998-11-25 16:18:00 +00001870Pickle_clear_memo(Picklerobject *self, PyObject *args) {
1871 if (args && ! PyArg_ParseTuple(args,"")) return NULL;
1872 if (self->memo) PyDict_Clear(self->memo);
1873 Py_INCREF(Py_None);
1874 return Py_None;
1875}
1876
1877static PyObject *
1878Pickle_getvalue(Picklerobject *self, PyObject *args) {
1879 int l, i, rsize, ssize, clear=1, lm;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001880 long ik;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001881 PyObject *k, *r;
1882 char *s, *p, *have_get;
1883 Pdata *data;
1884
1885 if (args && ! PyArg_ParseTuple(args,"|i",&clear)) return NULL;
1886
1887 /* Check to make sure we are based on a list */
1888 if (! Pdata_Check(self->file)) {
1889 PyErr_SetString(PicklingError,
1890 "Attempt to getvalue a non-list-based pickler");
1891 return NULL;
1892 }
1893
1894 /* flush write buffer */
1895 if (write_other(self, NULL, 0) < 0) return NULL;
1896
1897 data=(Pdata*)self->file;
1898 l=data->length;
1899
1900 /* set up an array to hold get/put status */
1901 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
1902 lm++;
1903 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
1904 memset(have_get,0,lm);
1905
1906 /* Scan for gets. */
1907 for (rsize=0, i=l; --i >= 0; ) {
1908 k=data->data[i];
1909
1910 if (PyString_Check(k)) {
1911 rsize += PyString_GET_SIZE(k);
1912 }
1913
1914 else if (PyInt_Check(k)) { /* put */
1915 ik=PyInt_AS_LONG((PyIntObject*)k);
1916 if (ik >= lm || ik==0) {
1917 PyErr_SetString(PicklingError,
1918 "Invalid get data");
1919 return NULL;
1920 }
1921 if (have_get[ik]) { /* with matching get */
1922 if (ik < 256) rsize += 2;
1923 else rsize+=5;
1924 }
1925 }
1926
1927 else if (! (PyTuple_Check(k) &&
1928 PyTuple_GET_SIZE(k) == 2 &&
1929 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
1930 ) {
1931 PyErr_SetString(PicklingError,
1932 "Unexpected data in internal list");
1933 return NULL;
1934 }
1935
1936 else { /* put */
1937 ik=PyInt_AS_LONG((PyIntObject*)k);
1938 if (ik >= lm || ik==0) {
1939 PyErr_SetString(PicklingError,
1940 "Invalid get data");
1941 return NULL;
1942 }
1943 have_get[ik]=1;
1944 if (ik < 256) rsize += 2;
1945 else rsize+=5;
1946 }
1947
1948 }
1949
1950 /* Now generate the result */
1951 UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
1952 s=PyString_AS_STRING((PyStringObject*)r);
1953
1954 for (i=0; i<l; i++) {
1955 k=data->data[i];
1956
1957 if (PyString_Check(k)) {
1958 ssize=PyString_GET_SIZE(k);
1959 if (ssize) {
1960 p=PyString_AS_STRING((PyStringObject*)k);
1961 while (--ssize >= 0) *s++=*p++;
1962 }
1963 }
1964
1965 else if (PyTuple_Check(k)) { /* get */
1966 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
1967 if (ik < 256) {
1968 *s++ = BINGET;
1969 *s++ = (int)(ik & 0xff);
1970 }
1971 else {
1972 *s++ = LONG_BINGET;
1973 *s++ = (int)(ik & 0xff);
1974 *s++ = (int)((ik >> 8) & 0xff);
1975 *s++ = (int)((ik >> 16) & 0xff);
1976 *s++ = (int)((ik >> 24) & 0xff);
1977 }
1978 }
1979
1980 else { /* put */
1981 ik=PyInt_AS_LONG((PyIntObject*)k);
1982
1983 if (have_get[ik]) { /* with matching get */
1984 if (ik < 256) {
1985 *s++ = BINPUT;
1986 *s++ = (int)(ik & 0xff);
1987 }
1988 else {
1989 *s++ = LONG_BINPUT;
1990 *s++ = (int)(ik & 0xff);
1991 *s++ = (int)((ik >> 8) & 0xff);
1992 *s++ = (int)((ik >> 16) & 0xff);
1993 *s++ = (int)((ik >> 24) & 0xff);
1994 }
1995 }
1996 }
1997
1998 }
1999
2000 if (clear) {
2001 PyDict_Clear(self->memo);
2002 Pdata_clear(data,0);
2003 }
2004
2005 free(have_get);
2006 return r;
2007err:
2008 free(have_get);
2009 return NULL;
2010}
2011
2012static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002013Pickler_dump(Picklerobject *self, PyObject *args) {
2014 PyObject *ob;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002015 int get=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002016
Guido van Rossum053b8df1998-11-25 16:18:00 +00002017 UNLESS (PyArg_ParseTuple(args, "O|i", &ob, &get))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002018 return NULL;
2019
2020 if (dump(self, ob) < 0)
2021 return NULL;
2022
Guido van Rossum053b8df1998-11-25 16:18:00 +00002023 if (get) return Pickle_getvalue(self, NULL);
2024
2025 Py_INCREF(self);
2026 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002027}
2028
2029
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002030static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00002031 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002032 "dump(object) --"
2033 "Write an object in pickle format to the object's pickle stream\n"
2034 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00002035 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002036 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum053b8df1998-11-25 16:18:00 +00002037 {"getvalue", (PyCFunction)Pickle_getvalue, 1,
2038 "getvalue() -- Finish picking a list-based pickle"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002039 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002040};
2041
2042
2043static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002044newPicklerobject(PyObject *file, int bin) {
2045 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002046
Guido van Rossum053b8df1998-11-25 16:18:00 +00002047 UNLESS (self = PyObject_NEW(Picklerobject, &Picklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002048 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002049
2050 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002051 self->write = NULL;
2052 self->memo = NULL;
2053 self->arg = NULL;
2054 self->pers_func = NULL;
2055 self->inst_pers_func = NULL;
2056 self->write_buf = NULL;
2057 self->bin = bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002058 self->fast = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002059 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002060 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002061
Guido van Rossum053b8df1998-11-25 16:18:00 +00002062 if (file)
2063 Py_INCREF(file);
2064 else
Guido van Rossum50f385c1998-12-04 18:48:44 +00002065 file=Pdata_New();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002066
Guido van Rossum60456fd1997-04-09 17:36:32 +00002067 self->file = file;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002068
Guido van Rossum053b8df1998-11-25 16:18:00 +00002069 UNLESS (self->memo = PyDict_New()) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002070 Py_XDECREF((PyObject *)self);
2071 return NULL;
2072 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002073
Guido van Rossum60456fd1997-04-09 17:36:32 +00002074 if (PyFile_Check(file)) {
2075 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002076 if (self->fp == NULL) {
2077 PyErr_SetString(PyExc_IOError, "output file closed");
2078 return NULL;
2079 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002080 self->write_func = write_file;
2081 }
2082 else if (PycStringIO_OutputCheck(file)) {
2083 self->write_func = write_cStringIO;
2084 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002085 else if (file == Py_None) {
2086 self->write_func = write_none;
2087 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002088 else {
2089 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002090
Guido van Rossum053b8df1998-11-25 16:18:00 +00002091 if (! Pdata_Check(file)) {
2092 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002093 PyErr_Clear();
2094 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002095 "attribute");
2096 goto err;
2097 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002098 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002099
Guido van Rossum053b8df1998-11-25 16:18:00 +00002100 UNLESS (self->write_buf =
Guido van Rossum60456fd1997-04-09 17:36:32 +00002101 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
2102 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002103 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002104 }
2105 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002106
Guido van Rossum053b8df1998-11-25 16:18:00 +00002107 if (PyEval_GetRestricted()) {
2108 /* Restricted execution, get private tables */
2109 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002110
Guido van Rossum053b8df1998-11-25 16:18:00 +00002111 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2112 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2113 Py_DECREF(m);
2114 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002115 }
2116 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002117 self->dispatch_table=dispatch_table;
2118 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002119 }
2120
Guido van Rossum60456fd1997-04-09 17:36:32 +00002121 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002122
2123err:
2124 Py_DECREF((PyObject *)self);
2125 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002126}
2127
2128
2129static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002130get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002131 PyObject *file=NULL;
2132 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002133
Guido van Rossum053b8df1998-11-25 16:18:00 +00002134 bin=1;
2135 if (! PyArg_ParseTuple(args, "|i", &bin)) {
2136 PyErr_Clear();
2137 bin=0;
2138 if (! PyArg_ParseTuple(args, "O|i", &file, &bin))
2139 return NULL;
2140 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002141 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002142}
2143
2144
2145static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002146Pickler_dealloc(Picklerobject *self) {
2147 Py_XDECREF(self->write);
2148 Py_XDECREF(self->memo);
2149 Py_XDECREF(self->arg);
2150 Py_XDECREF(self->file);
2151 Py_XDECREF(self->pers_func);
2152 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002153 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002154
2155 if (self->write_buf) {
2156 free(self->write_buf);
2157 }
2158
2159 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002160}
2161
2162
2163static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002164Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002165
2166 switch (*name) {
2167 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002168 if (strcmp(name, "persistent_id") == 0) {
2169 if (!self->pers_func) {
2170 PyErr_SetString(PyExc_AttributeError, name);
2171 return NULL;
2172 }
2173
2174 Py_INCREF(self->pers_func);
2175 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002176 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002177 break;
2178 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002179 if (strcmp(name, "memo") == 0) {
2180 if (!self->memo) {
2181 PyErr_SetString(PyExc_AttributeError, name);
2182 return NULL;
2183 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002184
Guido van Rossum60456fd1997-04-09 17:36:32 +00002185 Py_INCREF(self->memo);
2186 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002187 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002188 break;
2189 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002190 if (strcmp(name, "PicklingError") == 0) {
2191 Py_INCREF(PicklingError);
2192 return PicklingError;
2193 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002194 break;
2195 case 'b':
2196 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002197 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002198 break;
2199 case 'f':
2200 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002201 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002202 break;
2203 case 'g':
2204 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2205 PyErr_SetString(PyExc_AttributeError, name);
2206 return NULL;
2207 }
2208 break;
2209 }
2210 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002211}
2212
2213
2214int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002215Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002216
Guido van Rossum053b8df1998-11-25 16:18:00 +00002217 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002218 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002219 "attribute deletion is not supported");
2220 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002221 }
2222
Guido van Rossum60456fd1997-04-09 17:36:32 +00002223 if (strcmp(name, "persistent_id") == 0) {
2224 Py_XDECREF(self->pers_func);
2225 self->pers_func = value;
2226 Py_INCREF(value);
2227 return 0;
2228 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002229
Guido van Rossum60456fd1997-04-09 17:36:32 +00002230 if (strcmp(name, "inst_persistent_id") == 0) {
2231 Py_XDECREF(self->inst_pers_func);
2232 self->inst_pers_func = value;
2233 Py_INCREF(value);
2234 return 0;
2235 }
2236
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002237 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002238 if (! PyDict_Check(value)) {
2239 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2240 return -1;
2241 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002242 Py_XDECREF(self->memo);
2243 self->memo = value;
2244 Py_INCREF(value);
2245 return 0;
2246 }
2247
Guido van Rossum053b8df1998-11-25 16:18:00 +00002248 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002249 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002250 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002251 }
2252
Guido van Rossum053b8df1998-11-25 16:18:00 +00002253 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002254 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002255 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002256 }
2257
Guido van Rossum60456fd1997-04-09 17:36:32 +00002258 PyErr_SetString(PyExc_AttributeError, name);
2259 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002260}
2261
2262
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002263static char Picklertype__doc__[] =
2264"Objects that know how to pickle objects\n"
2265;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002266
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002267static PyTypeObject Picklertype = {
2268 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002269 0, /*ob_size*/
2270 "Pickler", /*tp_name*/
2271 sizeof(Picklerobject), /*tp_basicsize*/
2272 0, /*tp_itemsize*/
2273 /* methods */
2274 (destructor)Pickler_dealloc, /*tp_dealloc*/
2275 (printfunc)0, /*tp_print*/
2276 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2277 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2278 (cmpfunc)0, /*tp_compare*/
2279 (reprfunc)0, /*tp_repr*/
2280 0, /*tp_as_number*/
2281 0, /*tp_as_sequence*/
2282 0, /*tp_as_mapping*/
2283 (hashfunc)0, /*tp_hash*/
2284 (ternaryfunc)0, /*tp_call*/
2285 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002286
Guido van Rossum60456fd1997-04-09 17:36:32 +00002287 /* Space for future expansion */
2288 0L,0L,0L,0L,
2289 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002290};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002291
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002292static PyObject *
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002293find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002294 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002295
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002296 if (fc) {
2297 if (fc==Py_None) {
2298 PyErr_SetString(UnpicklingError,
2299 "Global and instance pickles are not supported.");
2300 return NULL;
2301 }
2302 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2303 }
2304
Jeremy Hyltond1055231998-08-11 19:52:51 +00002305 module = PySys_GetObject("modules");
2306 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002307 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002308
2309 module = PyDict_GetItem(module, py_module_name);
2310 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002311 module = PyImport_Import(py_module_name);
2312 if (!module)
2313 return NULL;
2314 global = PyObject_GetAttr(module, py_global_name);
2315 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002316 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002317 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002318 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002319 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002320 char buf[256 + 37];
2321 sprintf(buf, "Failed to import class %.128s from module %.128s",
2322 PyString_AS_STRING((PyStringObject*)py_global_name),
2323 PyString_AS_STRING((PyStringObject*)py_module_name));
2324 PyErr_SetString(PyExc_SystemError, buf);
2325 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002326 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002327 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002328}
2329
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002330static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002331marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002332 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002333 PyErr_SetString(UnpicklingError, "could not find MARK");
2334 return -1;
2335 }
2336
2337 return self->marks[--self->num_marks];
2338}
2339
2340
2341static int
2342load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002343 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002344 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002345}
2346
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002347static int
2348bad_readline() {
2349 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2350 return -1;
2351}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002352
2353static int
2354load_int(Unpicklerobject *self) {
2355 PyObject *py_int = 0;
2356 char *endptr, *s;
2357 int len, res = -1;
2358 long l;
2359
2360 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002361 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002362 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002363
2364 errno = 0;
2365 l = strtol(s, &endptr, 0);
2366
2367 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2368 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002369 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002370 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002371 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002372
Guido van Rossum053b8df1998-11-25 16:18:00 +00002373 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2374 PyErr_SetString(PyExc_ValueError,
2375 "could not convert string to int");
2376 goto finally;
2377 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002378 }
2379 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002380 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002381 }
2382
Guido van Rossum053b8df1998-11-25 16:18:00 +00002383 free(s);
2384 PDATA_PUSH(self->stack, py_int, -1);
2385 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002386
2387finally:
2388 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002389
2390 return res;
2391}
2392
2393
2394static long
2395calc_binint(char *s, int x) {
2396 unsigned char c;
2397 int i;
2398 long l;
2399
2400 for (i = 0, l = 0L; i < x; i++) {
2401 c = (unsigned char)s[i];
2402 l |= (long)c << (i * 8);
2403 }
2404
2405 return l;
2406}
2407
2408
2409static int
2410load_binintx(Unpicklerobject *self, char *s, int x) {
2411 PyObject *py_int = 0;
2412 long l;
2413
2414 l = calc_binint(s, x);
2415
Guido van Rossum053b8df1998-11-25 16:18:00 +00002416 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002417 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002418
Guido van Rossum053b8df1998-11-25 16:18:00 +00002419 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002420 return 0;
2421}
2422
2423
2424static int
2425load_binint(Unpicklerobject *self) {
2426 char *s;
2427
2428 if ((*self->read_func)(self, &s, 4) < 0)
2429 return -1;
2430
2431 return load_binintx(self, s, 4);
2432}
2433
2434
2435static int
2436load_binint1(Unpicklerobject *self) {
2437 char *s;
2438
2439 if ((*self->read_func)(self, &s, 1) < 0)
2440 return -1;
2441
2442 return load_binintx(self, s, 1);
2443}
2444
2445
2446static int
2447load_binint2(Unpicklerobject *self) {
2448 char *s;
2449
2450 if ((*self->read_func)(self, &s, 2) < 0)
2451 return -1;
2452
2453 return load_binintx(self, s, 2);
2454}
2455
2456static int
2457load_long(Unpicklerobject *self) {
2458 PyObject *l = 0;
2459 char *end, *s;
2460 int len, res = -1;
2461
Guido van Rossum60456fd1997-04-09 17:36:32 +00002462 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002463 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002464 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002465
Guido van Rossum053b8df1998-11-25 16:18:00 +00002466 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002467 goto finally;
2468
Guido van Rossum053b8df1998-11-25 16:18:00 +00002469 free(s);
2470 PDATA_PUSH(self->stack, l, -1);
2471 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002472
2473finally:
2474 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002475
2476 return res;
2477}
2478
2479
2480static int
2481load_float(Unpicklerobject *self) {
2482 PyObject *py_float = 0;
2483 char *endptr, *s;
2484 int len, res = -1;
2485 double d;
2486
2487 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002488 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002489 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002490
2491 errno = 0;
2492 d = strtod(s, &endptr);
2493
2494 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2495 PyErr_SetString(PyExc_ValueError,
2496 "could not convert string to float");
2497 goto finally;
2498 }
2499
Guido van Rossum053b8df1998-11-25 16:18:00 +00002500 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002501 goto finally;
2502
Guido van Rossum053b8df1998-11-25 16:18:00 +00002503 free(s);
2504 PDATA_PUSH(self->stack, py_float, -1);
2505 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002506
2507finally:
2508 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002509
2510 return res;
2511}
2512
Guido van Rossum60456fd1997-04-09 17:36:32 +00002513static int
2514load_binfloat(Unpicklerobject *self) {
2515 PyObject *py_float = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002516 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002517 long fhi, flo;
2518 double x;
2519 char *p;
2520
2521 if ((*self->read_func)(self, &p, 8) < 0)
2522 return -1;
2523
2524 /* First byte */
2525 s = (*p>>7) & 1;
2526 e = (*p & 0x7F) << 4;
2527 p++;
2528
2529 /* Second byte */
2530 e |= (*p>>4) & 0xF;
2531 fhi = (*p & 0xF) << 24;
2532 p++;
2533
2534 /* Third byte */
2535 fhi |= (*p & 0xFF) << 16;
2536 p++;
2537
2538 /* Fourth byte */
2539 fhi |= (*p & 0xFF) << 8;
2540 p++;
2541
2542 /* Fifth byte */
2543 fhi |= *p & 0xFF;
2544 p++;
2545
2546 /* Sixth byte */
2547 flo = (*p & 0xFF) << 16;
2548 p++;
2549
2550 /* Seventh byte */
2551 flo |= (*p & 0xFF) << 8;
2552 p++;
2553
2554 /* Eighth byte */
2555 flo |= *p & 0xFF;
2556
2557 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2558 x /= 268435456.0; /* 2**28 */
2559
2560 /* XXX This sadly ignores Inf/NaN */
2561 if (e == 0)
2562 e = -1022;
2563 else {
2564 x += 1.0;
2565 e -= 1023;
2566 }
2567 x = ldexp(x, e);
2568
2569 if (s)
2570 x = -x;
2571
Guido van Rossum053b8df1998-11-25 16:18:00 +00002572 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002573
Guido van Rossum053b8df1998-11-25 16:18:00 +00002574 PDATA_PUSH(self->stack, py_float, -1);
2575 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002576}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002577
2578static int
2579load_string(Unpicklerobject *self) {
2580 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002581 int len, res = -1, nslash;
2582 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002583
2584 static PyObject *eval_dict = 0;
2585
2586 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002587 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002588 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002589
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002590 /* Check for unquoted quotes (evil strings) */
2591 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002592 if (q != '"' && q != '\'') goto insecure;
2593 for (p=s+1, nslash=0; *p; p++) {
2594 if (*p==q && nslash%2==0) break;
2595 if (*p=='\\') nslash++;
2596 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002597 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002598 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002599 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002600 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002601 }
2602 else goto insecure;
2603 /********************************************/
2604
Guido van Rossum053b8df1998-11-25 16:18:00 +00002605 UNLESS (eval_dict)
2606 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002607 goto finally;
2608
Guido van Rossum053b8df1998-11-25 16:18:00 +00002609 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002610 goto finally;
2611
Guido van Rossum053b8df1998-11-25 16:18:00 +00002612 free(s);
2613 PDATA_PUSH(self->stack, str, -1);
2614 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002615
2616finally:
2617 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002618
2619 return res;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002620
2621insecure:
2622 free(s);
2623 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2624 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002625}
2626
2627
2628static int
2629load_binstring(Unpicklerobject *self) {
2630 PyObject *py_string = 0;
2631 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002632 char *s;
2633
Guido van Rossum053b8df1998-11-25 16:18:00 +00002634 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002635
2636 l = calc_binint(s, 4);
2637
2638 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002639 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002640
Guido van Rossum053b8df1998-11-25 16:18:00 +00002641 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2642 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002643
Guido van Rossum053b8df1998-11-25 16:18:00 +00002644 PDATA_PUSH(self->stack, py_string, -1);
2645 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002646}
2647
2648
2649static int
2650load_short_binstring(Unpicklerobject *self) {
2651 PyObject *py_string = 0;
2652 unsigned char l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002653 char *s;
2654
2655 if ((*self->read_func)(self, &s, 1) < 0)
2656 return -1;
2657
2658 l = (unsigned char)s[0];
2659
Guido van Rossum053b8df1998-11-25 16:18:00 +00002660 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002661
Guido van Rossum053b8df1998-11-25 16:18:00 +00002662 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002663
Guido van Rossum053b8df1998-11-25 16:18:00 +00002664 PDATA_PUSH(self->stack, py_string, -1);
2665 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002666}
2667
2668
2669static int
2670load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002671 PyObject *tup;
2672 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002673
Guido van Rossum053b8df1998-11-25 16:18:00 +00002674 if ((i = marker(self)) < 0) return -1;
2675 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2676 PDATA_PUSH(self->stack, tup, -1);
2677 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002678}
2679
2680static int
2681load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002682 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002683
Guido van Rossum053b8df1998-11-25 16:18:00 +00002684 UNLESS (tup=PyTuple_New(0)) return -1;
2685 PDATA_PUSH(self->stack, tup, -1);
2686 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002687}
2688
2689static int
2690load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002691 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002692
Guido van Rossum053b8df1998-11-25 16:18:00 +00002693 UNLESS (list=PyList_New(0)) return -1;
2694 PDATA_PUSH(self->stack, list, -1);
2695 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002696}
2697
2698static int
2699load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002700 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002701
Guido van Rossum053b8df1998-11-25 16:18:00 +00002702 UNLESS (dict=PyDict_New()) return -1;
2703 PDATA_PUSH(self->stack, dict, -1);
2704 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002705}
2706
2707
2708static int
2709load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002710 PyObject *list = 0;
2711 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002712
Guido van Rossum053b8df1998-11-25 16:18:00 +00002713 if ((i = marker(self)) < 0) return -1;
2714 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2715 PDATA_PUSH(self->stack, list, -1);
2716 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002717}
2718
2719static int
2720load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002721 PyObject *dict, *key, *value;
2722 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002723
Guido van Rossum053b8df1998-11-25 16:18:00 +00002724 if ((i = marker(self)) < 0) return -1;
2725 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002726
Guido van Rossum053b8df1998-11-25 16:18:00 +00002727 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002728
Guido van Rossum053b8df1998-11-25 16:18:00 +00002729 for (k = i+1; k < j; k += 2) {
2730 key =self->stack->data[k-1];
2731 value=self->stack->data[k ];
2732 if (PyDict_SetItem(dict, key, value) < 0) {
2733 Py_DECREF(dict);
2734 return -1;
2735 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002736 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002737 Pdata_clear(self->stack, i);
2738 PDATA_PUSH(self->stack, dict, -1);
2739 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002740}
2741
2742static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002743Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002744 int has_key;
2745 PyObject *safe=0, *r=0;
2746
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002747 if (PyClass_Check(cls)) {
2748 int l;
2749
Guido van Rossum053b8df1998-11-25 16:18:00 +00002750 if ((l=PyObject_Length(args)) < 0) goto err;
2751 UNLESS (l) {
2752 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002753
Guido van Rossum053b8df1998-11-25 16:18:00 +00002754 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2755 /* We have a class with no __getinitargs__, so bypass usual
2756 construction */
2757 PyInstanceObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002758
Guido van Rossum053b8df1998-11-25 16:18:00 +00002759 PyErr_Clear();
2760 UNLESS (inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
2761 goto err;
2762 inst->in_class=(PyClassObject*)cls;
2763 Py_INCREF(cls);
2764 UNLESS (inst->in_dict=PyDict_New()) {
2765 Py_DECREF(inst);
2766 goto err;
2767 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002768
Guido van Rossum053b8df1998-11-25 16:18:00 +00002769 return (PyObject *)inst;
2770 }
2771 Py_DECREF(__getinitargs__);
2772 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002773
Guido van Rossum053b8df1998-11-25 16:18:00 +00002774 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002775 else goto err;
2776 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002777
2778
2779 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2780 goto err;
2781
2782 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002783 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002784 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002785 cPickle_ErrFormat(UnpicklingError,
2786 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002787 Py_XDECREF(safe);
2788 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002789 }
2790
Guido van Rossum053b8df1998-11-25 16:18:00 +00002791 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002792 /* Special case, call cls.__basicnew__() */
2793 PyObject *basicnew;
2794
Guido van Rossum053b8df1998-11-25 16:18:00 +00002795 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002796 r=PyObject_CallObject(basicnew, NULL);
2797 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002798 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002799 }
2800
Guido van Rossum053b8df1998-11-25 16:18:00 +00002801 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002802
Guido van Rossum60456fd1997-04-09 17:36:32 +00002803err:
2804 {
2805 PyObject *tp, *v, *tb;
2806
2807 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002808 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2809 Py_XDECREF(v);
2810 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002811 }
2812 PyErr_Restore(tp,v,tb);
2813 }
2814 return NULL;
2815}
2816
2817
2818static int
2819load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002820 PyObject *class, *tup, *obj=0;
2821 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002822
Guido van Rossum053b8df1998-11-25 16:18:00 +00002823 if ((i = marker(self)) < 0) return -1;
2824 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
2825 PDATA_POP(self->stack, class);
2826 if (class) {
2827 obj = Instance_New(class, tup);
2828 Py_DECREF(class);
2829 }
2830 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002831
Guido van Rossum053b8df1998-11-25 16:18:00 +00002832 if (! obj) return -1;
2833 PDATA_PUSH(self->stack, obj, -1);
2834 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002835}
2836
2837
2838static int
2839load_inst(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002840 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002841 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002842 char *s;
2843
Guido van Rossum053b8df1998-11-25 16:18:00 +00002844 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002845
Guido van Rossum053b8df1998-11-25 16:18:00 +00002846 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002847 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002848 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002849
Guido van Rossum053b8df1998-11-25 16:18:00 +00002850 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002851 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002852 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002853 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002854 Py_DECREF(class_name);
2855 }
2856 }
2857 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002858
Guido van Rossum053b8df1998-11-25 16:18:00 +00002859 if (! class) return -1;
2860
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002861 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002862 obj = Instance_New(class, tup);
2863 Py_DECREF(tup);
2864 }
2865 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002866
Guido van Rossum053b8df1998-11-25 16:18:00 +00002867 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002868
Guido van Rossum053b8df1998-11-25 16:18:00 +00002869 PDATA_PUSH(self->stack, obj, -1);
2870 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002871}
2872
2873
2874static int
2875load_global(Unpicklerobject *self) {
2876 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002877 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002878 char *s;
2879
Guido van Rossum053b8df1998-11-25 16:18:00 +00002880 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002881 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002882 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002883
Guido van Rossum053b8df1998-11-25 16:18:00 +00002884 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002885 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002886 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002887 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002888 Py_DECREF(class_name);
2889 }
2890 }
2891 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002892
Guido van Rossum053b8df1998-11-25 16:18:00 +00002893 if (! class) return -1;
2894 PDATA_PUSH(self->stack, class, -1);
2895 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002896}
2897
2898
2899static int
2900load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002901 PyObject *pid = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002902 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002903 char *s;
2904
2905 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002906 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002907 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00002908
Guido van Rossum053b8df1998-11-25 16:18:00 +00002909 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002910
Guido van Rossum053b8df1998-11-25 16:18:00 +00002911 if (PyList_Check(self->pers_func)) {
2912 if (PyList_Append(self->pers_func, pid) < 0) {
2913 Py_DECREF(pid);
2914 return -1;
2915 }
2916 }
2917 else {
2918 ARG_TUP(self, pid);
2919 if (self->arg) {
2920 pid = PyObject_CallObject(self->pers_func, self->arg);
2921 FREE_ARG_TUP(self);
2922 }
2923 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002924
Guido van Rossum053b8df1998-11-25 16:18:00 +00002925 if (! pid) return -1;
2926
2927 PDATA_PUSH(self->stack, pid, -1);
2928 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002929 }
2930 else {
2931 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002932 "A load persistent id instruction was encountered,\n"
2933 "but no persistent_load function was specified.");
2934 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002935 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002936}
2937
Guido van Rossum60456fd1997-04-09 17:36:32 +00002938static int
2939load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002940 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002941
2942 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002943 PDATA_POP(self->stack, pid);
2944 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002945
Guido van Rossum053b8df1998-11-25 16:18:00 +00002946 if (PyList_Check(self->pers_func)) {
2947 if (PyList_Append(self->pers_func, pid) < 0) {
2948 Py_DECREF(pid);
2949 return -1;
2950 }
2951 }
2952 else {
2953 ARG_TUP(self, pid);
2954 if (self->arg) {
2955 pid = PyObject_CallObject(self->pers_func, self->arg);
2956 FREE_ARG_TUP(self);
2957 }
2958 if (! pid) return -1;
2959 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002960
Guido van Rossum053b8df1998-11-25 16:18:00 +00002961 PDATA_PUSH(self->stack, pid, -1);
2962 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002963 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002964 else {
2965 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002966 "A load persistent id instruction was encountered,\n"
2967 "but no persistent_load function was specified.");
2968 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002969 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002970}
2971
2972
2973static int
2974load_pop(Unpicklerobject *self) {
2975 int len;
2976
Guido van Rossum053b8df1998-11-25 16:18:00 +00002977 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00002978
2979 if ((self->num_marks > 0) &&
2980 (self->marks[self->num_marks - 1] == len))
2981 self->num_marks--;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002982 else
2983 Py_DECREF(self->stack->data[--(self->stack->length)]);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002984
2985 return 0;
2986}
2987
2988
2989static int
2990load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002991 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002992
2993 if ((i = marker(self)) < 0)
2994 return -1;
2995
Guido van Rossum053b8df1998-11-25 16:18:00 +00002996 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002997
2998 return 0;
2999}
3000
3001
3002static int
3003load_dup(Unpicklerobject *self) {
3004 PyObject *last;
3005 int len;
3006
Guido van Rossum053b8df1998-11-25 16:18:00 +00003007 if ((len = self->stack->length) <= 0) return stackUnderflow();
3008 last=self->stack->data[len-1];
3009 Py_INCREF(last);
3010 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003011 return 0;
3012}
3013
3014
3015static int
3016load_get(Unpicklerobject *self) {
3017 PyObject *py_str = 0, *value = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003018 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003019 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003020 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003021
Guido van Rossum053b8df1998-11-25 16:18:00 +00003022 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003023 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003024
Guido van Rossum053b8df1998-11-25 16:18:00 +00003025 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3026
3027 value = PyDict_GetItem(self->memo, py_str);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003028 if (! value) {
3029 PyErr_SetObject(BadPickleGet, py_str);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003030 rc = -1;
3031 } else {
3032 PDATA_APPEND(self->stack, value, -1);
3033 rc = 0;
3034 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003035
Guido van Rossum2f80d961999-07-13 15:18:58 +00003036 Py_DECREF(py_str);
3037 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003038}
3039
3040
3041static int
3042load_binget(Unpicklerobject *self) {
3043 PyObject *py_key = 0, *value = 0;
3044 unsigned char key;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003045 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003046 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003047
Guido van Rossum053b8df1998-11-25 16:18:00 +00003048 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003049
3050 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003051 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3052
3053 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003054 if (! value) {
3055 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003056 rc = -1;
3057 } else {
3058 PDATA_APPEND(self->stack, value, -1);
3059 rc = 0;
3060 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003061
Guido van Rossum2f80d961999-07-13 15:18:58 +00003062 Py_DECREF(py_key);
3063 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003064}
3065
3066
3067static int
3068load_long_binget(Unpicklerobject *self) {
3069 PyObject *py_key = 0, *value = 0;
3070 unsigned char c, *s;
3071 long key;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003072 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003073
Guido van Rossum053b8df1998-11-25 16:18:00 +00003074 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003075
3076 c = (unsigned char)s[0];
3077 key = (long)c;
3078 c = (unsigned char)s[1];
3079 key |= (long)c << 8;
3080 c = (unsigned char)s[2];
3081 key |= (long)c << 16;
3082 c = (unsigned char)s[3];
3083 key |= (long)c << 24;
3084
Guido van Rossum053b8df1998-11-25 16:18:00 +00003085 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3086
3087 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003088 if (! value) {
3089 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003090 rc = -1;
3091 } else {
3092 PDATA_APPEND(self->stack, value, -1);
3093 rc = 0;
3094 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003095
Guido van Rossum2f80d961999-07-13 15:18:58 +00003096 Py_DECREF(py_key);
3097 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003098}
3099
3100
3101static int
3102load_put(Unpicklerobject *self) {
3103 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003104 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003105 char *s;
3106
Guido van Rossum053b8df1998-11-25 16:18:00 +00003107 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003108 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003109 UNLESS (len=self->stack->length) return stackUnderflow();
3110 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3111 value=self->stack->data[len-1];
3112 l=PyDict_SetItem(self->memo, py_str, value);
3113 Py_DECREF(py_str);
3114 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003115}
3116
3117
3118static int
3119load_binput(Unpicklerobject *self) {
3120 PyObject *py_key = 0, *value = 0;
3121 unsigned char key, *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003122 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003123
Guido van Rossum053b8df1998-11-25 16:18:00 +00003124 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3125 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003126
3127 key = (unsigned char)s[0];
3128
Guido van Rossum053b8df1998-11-25 16:18:00 +00003129 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3130 value=self->stack->data[len-1];
3131 len=PyDict_SetItem(self->memo, py_key, value);
3132 Py_DECREF(py_key);
3133 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003134}
3135
3136
3137static int
3138load_long_binput(Unpicklerobject *self) {
3139 PyObject *py_key = 0, *value = 0;
3140 long key;
3141 unsigned char c, *s;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003142 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003143
Guido van Rossum053b8df1998-11-25 16:18:00 +00003144 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3145 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003146
3147 c = (unsigned char)s[0];
3148 key = (long)c;
3149 c = (unsigned char)s[1];
3150 key |= (long)c << 8;
3151 c = (unsigned char)s[2];
3152 key |= (long)c << 16;
3153 c = (unsigned char)s[3];
3154 key |= (long)c << 24;
3155
Guido van Rossum053b8df1998-11-25 16:18:00 +00003156 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3157 value=self->stack->data[len-1];
3158 len=PyDict_SetItem(self->memo, py_key, value);
3159 Py_DECREF(py_key);
3160 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003161}
3162
3163
3164static int
3165do_append(Unpicklerobject *self, int x) {
3166 PyObject *value = 0, *list = 0, *append_method = 0;
3167 int len, i;
3168
Guido van Rossum053b8df1998-11-25 16:18:00 +00003169 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3170 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003171
Guido van Rossum053b8df1998-11-25 16:18:00 +00003172 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003173
3174 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003175 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003176 int list_len;
3177
Guido van Rossum053b8df1998-11-25 16:18:00 +00003178 slice=Pdata_popList(self->stack, x);
3179 list_len = PyList_GET_SIZE(list);
3180 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003181 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003182 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003183 }
3184 else {
3185
Guido van Rossum053b8df1998-11-25 16:18:00 +00003186 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003187 return -1;
3188
3189 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003190 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003191
Guido van Rossum053b8df1998-11-25 16:18:00 +00003192 value=self->stack->data[i];
3193 junk=0;
3194 ARG_TUP(self, value);
3195 if (self->arg) {
3196 junk = PyObject_CallObject(append_method, self->arg);
3197 FREE_ARG_TUP(self);
3198 }
3199 if (! junk) {
3200 Pdata_clear(self->stack, i+1);
3201 self->stack->length=x;
3202 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003203 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003204 }
3205 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003206 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003207 self->stack->length=x;
3208 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003209 }
3210
Guido van Rossum60456fd1997-04-09 17:36:32 +00003211 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003212}
3213
3214
3215static int
3216load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003217 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003218}
3219
3220
3221static int
3222load_appends(Unpicklerobject *self) {
3223 return do_append(self, marker(self));
3224}
3225
3226
3227static int
3228do_setitems(Unpicklerobject *self, int x) {
3229 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003230 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003231
Guido van Rossum053b8df1998-11-25 16:18:00 +00003232 UNLESS ((len=self->stack->length) >= x
3233 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003234
Guido van Rossum053b8df1998-11-25 16:18:00 +00003235 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003236
Guido van Rossum053b8df1998-11-25 16:18:00 +00003237 for (i = x+1; i < len; i += 2) {
3238 key =self->stack->data[i-1];
3239 value=self->stack->data[i ];
3240 if (PyObject_SetItem(dict, key, value) < 0) {
3241 r=-1;
3242 break;
3243 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003244 }
3245
Guido van Rossum053b8df1998-11-25 16:18:00 +00003246 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003247
Guido van Rossum053b8df1998-11-25 16:18:00 +00003248 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003249}
3250
3251
3252static int
3253load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003254 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003255}
3256
Guido van Rossum60456fd1997-04-09 17:36:32 +00003257static int
3258load_setitems(Unpicklerobject *self) {
3259 return do_setitems(self, marker(self));
3260}
3261
3262
3263static int
3264load_build(Unpicklerobject *self) {
3265 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3266 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003267 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003268
Guido van Rossum053b8df1998-11-25 16:18:00 +00003269 if (self->stack->length < 2) return stackUnderflow();
3270 PDATA_POP(self->stack, value);
3271 if (! value) return -1;
3272 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003273
Guido van Rossum053b8df1998-11-25 16:18:00 +00003274 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3275 ARG_TUP(self, value);
3276 if (self->arg) {
3277 junk = PyObject_CallObject(__setstate__, self->arg);
3278 FREE_ARG_TUP(self);
3279 }
3280 Py_DECREF(__setstate__);
3281 if (! junk) return -1;
3282 Py_DECREF(junk);
3283 return 0;
3284 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003285
Guido van Rossum053b8df1998-11-25 16:18:00 +00003286 PyErr_Clear();
3287 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003288 i = 0;
3289 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003290 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3291 r=-1;
3292 break;
3293 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003294 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003295 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003296 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003297 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003298
Guido van Rossum053b8df1998-11-25 16:18:00 +00003299 Py_XDECREF(value);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003300
Guido van Rossum053b8df1998-11-25 16:18:00 +00003301 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003302}
3303
3304
3305static int
3306load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003307 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003308
Guido van Rossum053b8df1998-11-25 16:18:00 +00003309 if ((self->num_marks + 1) >= self->marks_size) {
3310 s=self->marks_size+20;
3311 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003312 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003313 self->marks=(int *)malloc(s * sizeof(int));
3314 else
3315 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003316 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003317 PyErr_NoMemory();
3318 return -1;
3319 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003320 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003321 }
3322
Guido van Rossum053b8df1998-11-25 16:18:00 +00003323 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003324
3325 return 0;
3326}
3327
3328static int
3329load_reduce(Unpicklerobject *self) {
3330 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003331
Guido van Rossum053b8df1998-11-25 16:18:00 +00003332 PDATA_POP(self->stack, arg_tup);
3333 if (! arg_tup) return -1;
3334 PDATA_POP(self->stack, callable);
3335 if (callable) {
3336 ob = Instance_New(callable, arg_tup);
3337 Py_DECREF(callable);
3338 }
3339 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003340
Guido van Rossum053b8df1998-11-25 16:18:00 +00003341 if (! ob) return -1;
3342
3343 PDATA_PUSH(self->stack, ob, -1);
3344 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003345}
3346
3347static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003348load(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003349 PyObject *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003350 char *s;
3351
Guido van Rossum60456fd1997-04-09 17:36:32 +00003352 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003353 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003354
3355 while (1) {
3356 if ((*self->read_func)(self, &s, 1) < 0)
3357 break;
3358
3359 switch (s[0]) {
3360 case NONE:
3361 if (load_none(self) < 0)
3362 break;
3363 continue;
3364
3365 case BININT:
3366 if (load_binint(self) < 0)
3367 break;
3368 continue;
3369
3370 case BININT1:
3371 if (load_binint1(self) < 0)
3372 break;
3373 continue;
3374
3375 case BININT2:
3376 if (load_binint2(self) < 0)
3377 break;
3378 continue;
3379
3380 case INT:
3381 if (load_int(self) < 0)
3382 break;
3383 continue;
3384
3385 case LONG:
3386 if (load_long(self) < 0)
3387 break;
3388 continue;
3389
3390 case FLOAT:
3391 if (load_float(self) < 0)
3392 break;
3393 continue;
3394
Guido van Rossum60456fd1997-04-09 17:36:32 +00003395 case BINFLOAT:
3396 if (load_binfloat(self) < 0)
3397 break;
3398 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003399
3400 case BINSTRING:
3401 if (load_binstring(self) < 0)
3402 break;
3403 continue;
3404
3405 case SHORT_BINSTRING:
3406 if (load_short_binstring(self) < 0)
3407 break;
3408 continue;
3409
3410 case STRING:
3411 if (load_string(self) < 0)
3412 break;
3413 continue;
3414
3415 case EMPTY_TUPLE:
3416 if (load_empty_tuple(self) < 0)
3417 break;
3418 continue;
3419
3420 case TUPLE:
3421 if (load_tuple(self) < 0)
3422 break;
3423 continue;
3424
3425 case EMPTY_LIST:
3426 if (load_empty_list(self) < 0)
3427 break;
3428 continue;
3429
3430 case LIST:
3431 if (load_list(self) < 0)
3432 break;
3433 continue;
3434
3435 case EMPTY_DICT:
3436 if (load_empty_dict(self) < 0)
3437 break;
3438 continue;
3439
3440 case DICT:
3441 if (load_dict(self) < 0)
3442 break;
3443 continue;
3444
3445 case OBJ:
3446 if (load_obj(self) < 0)
3447 break;
3448 continue;
3449
3450 case INST:
3451 if (load_inst(self) < 0)
3452 break;
3453 continue;
3454
3455 case GLOBAL:
3456 if (load_global(self) < 0)
3457 break;
3458 continue;
3459
3460 case APPEND:
3461 if (load_append(self) < 0)
3462 break;
3463 continue;
3464
3465 case APPENDS:
3466 if (load_appends(self) < 0)
3467 break;
3468 continue;
3469
3470 case BUILD:
3471 if (load_build(self) < 0)
3472 break;
3473 continue;
3474
3475 case DUP:
3476 if (load_dup(self) < 0)
3477 break;
3478 continue;
3479
3480 case BINGET:
3481 if (load_binget(self) < 0)
3482 break;
3483 continue;
3484
3485 case LONG_BINGET:
3486 if (load_long_binget(self) < 0)
3487 break;
3488 continue;
3489
3490 case GET:
3491 if (load_get(self) < 0)
3492 break;
3493 continue;
3494
3495 case MARK:
3496 if (load_mark(self) < 0)
3497 break;
3498 continue;
3499
3500 case BINPUT:
3501 if (load_binput(self) < 0)
3502 break;
3503 continue;
3504
3505 case LONG_BINPUT:
3506 if (load_long_binput(self) < 0)
3507 break;
3508 continue;
3509
3510 case PUT:
3511 if (load_put(self) < 0)
3512 break;
3513 continue;
3514
3515 case POP:
3516 if (load_pop(self) < 0)
3517 break;
3518 continue;
3519
3520 case POP_MARK:
3521 if (load_pop_mark(self) < 0)
3522 break;
3523 continue;
3524
3525 case SETITEM:
3526 if (load_setitem(self) < 0)
3527 break;
3528 continue;
3529
3530 case SETITEMS:
3531 if (load_setitems(self) < 0)
3532 break;
3533 continue;
3534
3535 case STOP:
3536 break;
3537
3538 case PERSID:
3539 if (load_persid(self) < 0)
3540 break;
3541 continue;
3542
3543 case BINPERSID:
3544 if (load_binpersid(self) < 0)
3545 break;
3546 continue;
3547
3548 case REDUCE:
3549 if (load_reduce(self) < 0)
3550 break;
3551 continue;
3552
3553 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003554 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003555 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003556 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003557 }
3558
3559 break;
3560 }
3561
Guido van Rossum053b8df1998-11-25 16:18:00 +00003562 if ((err = PyErr_Occurred())) {
3563 if (err == PyExc_EOFError) {
3564 PyErr_SetNone(PyExc_EOFError);
3565 }
3566 return NULL;
3567 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003568
Guido van Rossum053b8df1998-11-25 16:18:00 +00003569 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003570 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003571}
3572
3573
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003574/* No-load functions to support noload, which is used to
3575 find persistent references. */
3576
3577static int
3578noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003579 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003580
3581 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003582 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003583}
3584
3585
3586static int
3587noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003588 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003589 char *s;
3590
3591 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003592 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003593 if ((*self->readline_func)(self, &s) < 0) return -1;
3594 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003595 PDATA_APPEND(self->stack, Py_None,-1);
3596 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003597}
3598
3599static int
3600noload_global(Unpicklerobject *self) {
3601 char *s;
3602
3603 if ((*self->readline_func)(self, &s) < 0) return -1;
3604 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003605 PDATA_APPEND(self->stack, Py_None,-1);
3606 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003607}
3608
3609static int
3610noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003611
Guido van Rossum053b8df1998-11-25 16:18:00 +00003612 if (self->stack->length < 2) return stackUnderflow();
3613 Pdata_clear(self->stack, self->stack->length-2);
3614 PDATA_APPEND(self->stack, Py_None,-1);
3615 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003616}
3617
3618static int
3619noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003620
Guido van Rossum053b8df1998-11-25 16:18:00 +00003621 if (self->stack->length < 1) return stackUnderflow();
3622 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003623 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003624}
3625
3626
3627static PyObject *
3628noload(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003629 PyObject *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003630 char *s;
3631
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003632 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003633 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003634
3635 while (1) {
3636 if ((*self->read_func)(self, &s, 1) < 0)
3637 break;
3638
3639 switch (s[0]) {
3640 case NONE:
3641 if (load_none(self) < 0)
3642 break;
3643 continue;
3644
3645 case BININT:
3646 if (load_binint(self) < 0)
3647 break;
3648 continue;
3649
3650 case BININT1:
3651 if (load_binint1(self) < 0)
3652 break;
3653 continue;
3654
3655 case BININT2:
3656 if (load_binint2(self) < 0)
3657 break;
3658 continue;
3659
3660 case INT:
3661 if (load_int(self) < 0)
3662 break;
3663 continue;
3664
3665 case LONG:
3666 if (load_long(self) < 0)
3667 break;
3668 continue;
3669
3670 case FLOAT:
3671 if (load_float(self) < 0)
3672 break;
3673 continue;
3674
3675 case BINFLOAT:
3676 if (load_binfloat(self) < 0)
3677 break;
3678 continue;
3679
3680 case BINSTRING:
3681 if (load_binstring(self) < 0)
3682 break;
3683 continue;
3684
3685 case SHORT_BINSTRING:
3686 if (load_short_binstring(self) < 0)
3687 break;
3688 continue;
3689
3690 case STRING:
3691 if (load_string(self) < 0)
3692 break;
3693 continue;
3694
3695 case EMPTY_TUPLE:
3696 if (load_empty_tuple(self) < 0)
3697 break;
3698 continue;
3699
3700 case TUPLE:
3701 if (load_tuple(self) < 0)
3702 break;
3703 continue;
3704
3705 case EMPTY_LIST:
3706 if (load_empty_list(self) < 0)
3707 break;
3708 continue;
3709
3710 case LIST:
3711 if (load_list(self) < 0)
3712 break;
3713 continue;
3714
3715 case EMPTY_DICT:
3716 if (load_empty_dict(self) < 0)
3717 break;
3718 continue;
3719
3720 case DICT:
3721 if (load_dict(self) < 0)
3722 break;
3723 continue;
3724
3725 case OBJ:
3726 if (noload_obj(self) < 0)
3727 break;
3728 continue;
3729
3730 case INST:
3731 if (noload_inst(self) < 0)
3732 break;
3733 continue;
3734
3735 case GLOBAL:
3736 if (noload_global(self) < 0)
3737 break;
3738 continue;
3739
3740 case APPEND:
3741 if (load_append(self) < 0)
3742 break;
3743 continue;
3744
3745 case APPENDS:
3746 if (load_appends(self) < 0)
3747 break;
3748 continue;
3749
3750 case BUILD:
3751 if (noload_build(self) < 0)
3752 break;
3753 continue;
3754
3755 case DUP:
3756 if (load_dup(self) < 0)
3757 break;
3758 continue;
3759
3760 case BINGET:
3761 if (load_binget(self) < 0)
3762 break;
3763 continue;
3764
3765 case LONG_BINGET:
3766 if (load_long_binget(self) < 0)
3767 break;
3768 continue;
3769
3770 case GET:
3771 if (load_get(self) < 0)
3772 break;
3773 continue;
3774
3775 case MARK:
3776 if (load_mark(self) < 0)
3777 break;
3778 continue;
3779
3780 case BINPUT:
3781 if (load_binput(self) < 0)
3782 break;
3783 continue;
3784
3785 case LONG_BINPUT:
3786 if (load_long_binput(self) < 0)
3787 break;
3788 continue;
3789
3790 case PUT:
3791 if (load_put(self) < 0)
3792 break;
3793 continue;
3794
3795 case POP:
3796 if (load_pop(self) < 0)
3797 break;
3798 continue;
3799
3800 case POP_MARK:
3801 if (load_pop_mark(self) < 0)
3802 break;
3803 continue;
3804
3805 case SETITEM:
3806 if (load_setitem(self) < 0)
3807 break;
3808 continue;
3809
3810 case SETITEMS:
3811 if (load_setitems(self) < 0)
3812 break;
3813 continue;
3814
3815 case STOP:
3816 break;
3817
3818 case PERSID:
3819 if (load_persid(self) < 0)
3820 break;
3821 continue;
3822
3823 case BINPERSID:
3824 if (load_binpersid(self) < 0)
3825 break;
3826 continue;
3827
3828 case REDUCE:
3829 if (noload_reduce(self) < 0)
3830 break;
3831 continue;
3832
3833 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003834 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003835 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003836 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003837 }
3838
3839 break;
3840 }
3841
Guido van Rossum053b8df1998-11-25 16:18:00 +00003842 if ((err = PyErr_Occurred())) {
3843 if (err == PyExc_EOFError) {
3844 PyErr_SetNone(PyExc_EOFError);
3845 }
3846 return NULL;
3847 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003848
Guido van Rossum053b8df1998-11-25 16:18:00 +00003849 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003850 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003851}
3852
3853
Guido van Rossum60456fd1997-04-09 17:36:32 +00003854static PyObject *
3855Unpickler_load(Unpicklerobject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003856 UNLESS (PyArg_ParseTuple(args, ""))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003857 return NULL;
3858
3859 return load(self);
3860}
3861
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003862static PyObject *
3863Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003864 UNLESS (PyArg_ParseTuple(args, ""))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003865 return NULL;
3866
3867 return noload(self);
3868}
3869
Guido van Rossum60456fd1997-04-09 17:36:32 +00003870
3871static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003872 {"load", (PyCFunction)Unpickler_load, 1,
3873 "load() -- Load a pickle"
3874 },
3875 {"noload", (PyCFunction)Unpickler_noload, 1,
3876 "noload() -- not load a pickle, but go through most of the motions\n"
3877 "\n"
3878 "This function can be used to read past a pickle without instantiating\n"
3879 "any objects or importing any modules. It can also be used to find all\n"
3880 "persistent references without instantiating any objects or importing\n"
3881 "any modules.\n"
3882 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00003883 {NULL, NULL} /* sentinel */
3884};
3885
3886
3887static Unpicklerobject *
3888newUnpicklerobject(PyObject *f) {
3889 Unpicklerobject *self;
3890
Guido van Rossum053b8df1998-11-25 16:18:00 +00003891 UNLESS (self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003892 return NULL;
3893
3894 self->file = NULL;
3895 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003896 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003897 self->pers_func = NULL;
3898 self->last_string = NULL;
3899 self->marks = NULL;
3900 self->num_marks = 0;
3901 self->marks_size = 0;
3902 self->buf_size = 0;
3903 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00003904 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00003905 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003906 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003907
Guido van Rossum053b8df1998-11-25 16:18:00 +00003908 UNLESS (self->memo = PyDict_New()) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003909 Py_XDECREF((PyObject *)self);
3910 return NULL;
3911 }
3912
3913 Py_INCREF(f);
3914 self->file = f;
3915
3916 /* Set read, readline based on type of f */
3917 if (PyFile_Check(f)) {
3918 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00003919 if (self->fp == NULL) {
3920 PyErr_SetString(PyExc_IOError, "input file closed");
3921 return NULL;
3922 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003923 self->read_func = read_file;
3924 self->readline_func = readline_file;
3925 }
3926 else if (PycStringIO_InputCheck(f)) {
3927 self->fp = NULL;
3928 self->read_func = read_cStringIO;
3929 self->readline_func = readline_cStringIO;
3930 }
3931 else {
3932
3933 self->fp = NULL;
3934 self->read_func = read_other;
3935 self->readline_func = readline_other;
3936
Guido van Rossum053b8df1998-11-25 16:18:00 +00003937 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003938 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003939 PyErr_Clear();
3940 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
3941 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00003942 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003943 }
3944 }
3945
Guido van Rossum053b8df1998-11-25 16:18:00 +00003946 if (PyEval_GetRestricted()) {
3947 /* Restricted execution, get private tables */
3948 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003949
Guido van Rossum053b8df1998-11-25 16:18:00 +00003950 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
3951 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
3952 Py_DECREF(m);
3953 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003954 }
3955 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003956 self->safe_constructors=safe_constructors;
3957 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003958 }
3959
Guido van Rossum60456fd1997-04-09 17:36:32 +00003960 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003961
3962err:
3963 Py_DECREF((PyObject *)self);
3964 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003965}
3966
3967
3968static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003969get_Unpickler(PyObject *self, PyObject *args) {
3970 PyObject *file;
3971
Guido van Rossum053b8df1998-11-25 16:18:00 +00003972 UNLESS (PyArg_ParseTuple(args, "O", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003973 return NULL;
3974 return (PyObject *)newUnpicklerobject(file);
3975}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003976
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003977
Guido van Rossum60456fd1997-04-09 17:36:32 +00003978static void
3979Unpickler_dealloc(Unpicklerobject *self) {
3980 Py_XDECREF(self->readline);
3981 Py_XDECREF(self->read);
3982 Py_XDECREF(self->file);
3983 Py_XDECREF(self->memo);
3984 Py_XDECREF(self->stack);
3985 Py_XDECREF(self->pers_func);
3986 Py_XDECREF(self->arg);
3987 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003988 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003989
Guido van Rossum60456fd1997-04-09 17:36:32 +00003990 if (self->marks) {
3991 free(self->marks);
3992 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003993
Guido van Rossum60456fd1997-04-09 17:36:32 +00003994 if (self->buf_size) {
3995 free(self->buf);
3996 }
3997
3998 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003999}
4000
4001
4002static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004003Unpickler_getattr(Unpicklerobject *self, char *name) {
4004 if (!strcmp(name, "persistent_load")) {
4005 if (!self->pers_func) {
4006 PyErr_SetString(PyExc_AttributeError, name);
4007 return NULL;
4008 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004009
Guido van Rossum60456fd1997-04-09 17:36:32 +00004010 Py_INCREF(self->pers_func);
4011 return self->pers_func;
4012 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004013
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004014 if (!strcmp(name, "find_global")) {
4015 if (!self->find_class) {
4016 PyErr_SetString(PyExc_AttributeError, name);
4017 return NULL;
4018 }
4019
4020 Py_INCREF(self->find_class);
4021 return self->find_class;
4022 }
4023
Guido van Rossum60456fd1997-04-09 17:36:32 +00004024 if (!strcmp(name, "memo")) {
4025 if (!self->memo) {
4026 PyErr_SetString(PyExc_AttributeError, name);
4027 return NULL;
4028 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004029
Guido van Rossum60456fd1997-04-09 17:36:32 +00004030 Py_INCREF(self->memo);
4031 return self->memo;
4032 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004033
Guido van Rossum60456fd1997-04-09 17:36:32 +00004034 if (!strcmp(name, "UnpicklingError")) {
4035 Py_INCREF(UnpicklingError);
4036 return UnpicklingError;
4037 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004038
Guido van Rossum60456fd1997-04-09 17:36:32 +00004039 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4040}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004041
Guido van Rossum60456fd1997-04-09 17:36:32 +00004042
4043static int
4044Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004045
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004046 if (!strcmp(name, "persistent_load")) {
4047 Py_XDECREF(self->pers_func);
4048 self->pers_func = value;
4049 Py_XINCREF(value);
4050 return 0;
4051 }
4052
4053 if (!strcmp(name, "find_global")) {
4054 Py_XDECREF(self->find_class);
4055 self->find_class = value;
4056 Py_XINCREF(value);
4057 return 0;
4058 }
4059
Guido van Rossum053b8df1998-11-25 16:18:00 +00004060 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004061 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004062 "attribute deletion is not supported");
4063 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004064 }
4065
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004066 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004067 if (! PyDict_Check(value)) {
4068 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4069 return -1;
4070 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004071 Py_XDECREF(self->memo);
4072 self->memo = value;
4073 Py_INCREF(value);
4074 return 0;
4075 }
4076
Guido van Rossum60456fd1997-04-09 17:36:32 +00004077 PyErr_SetString(PyExc_AttributeError, name);
4078 return -1;
4079}
4080
4081
4082static PyObject *
4083cpm_dump(PyObject *self, PyObject *args) {
4084 PyObject *ob, *file, *res = NULL;
4085 Picklerobject *pickler = 0;
4086 int bin = 0;
4087
Guido van Rossum053b8df1998-11-25 16:18:00 +00004088 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004089 goto finally;
4090
Guido van Rossum053b8df1998-11-25 16:18:00 +00004091 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004092 goto finally;
4093
4094 if (dump(pickler, ob) < 0)
4095 goto finally;
4096
4097 Py_INCREF(Py_None);
4098 res = Py_None;
4099
4100finally:
4101 Py_XDECREF(pickler);
4102
4103 return res;
4104}
4105
4106
4107static PyObject *
4108cpm_dumps(PyObject *self, PyObject *args) {
4109 PyObject *ob, *file = 0, *res = NULL;
4110 Picklerobject *pickler = 0;
4111 int bin = 0;
4112
Guido van Rossum053b8df1998-11-25 16:18:00 +00004113 UNLESS (PyArg_ParseTuple(args, "O|i", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004114 goto finally;
4115
Guido van Rossum053b8df1998-11-25 16:18:00 +00004116 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004117 goto finally;
4118
Guido van Rossum053b8df1998-11-25 16:18:00 +00004119 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004120 goto finally;
4121
4122 if (dump(pickler, ob) < 0)
4123 goto finally;
4124
4125 res = PycStringIO->cgetvalue(file);
4126
4127finally:
4128 Py_XDECREF(pickler);
4129 Py_XDECREF(file);
4130
4131 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004132}
4133
4134
4135static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004136cpm_load(PyObject *self, PyObject *args) {
4137 Unpicklerobject *unpickler = 0;
4138 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004139
Guido van Rossum053b8df1998-11-25 16:18:00 +00004140 UNLESS (PyArg_ParseTuple(args, "O", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004141 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004142
Guido van Rossum053b8df1998-11-25 16:18:00 +00004143 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004144 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004145
Guido van Rossum60456fd1997-04-09 17:36:32 +00004146 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004147
Guido van Rossum60456fd1997-04-09 17:36:32 +00004148finally:
4149 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004150
Guido van Rossum60456fd1997-04-09 17:36:32 +00004151 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004152}
4153
4154
4155static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004156cpm_loads(PyObject *self, PyObject *args) {
4157 PyObject *ob, *file = 0, *res = NULL;
4158 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004159
Guido van Rossum053b8df1998-11-25 16:18:00 +00004160 UNLESS (PyArg_ParseTuple(args, "S", &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 (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004164 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004165
Guido van Rossum053b8df1998-11-25 16:18:00 +00004166 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004167 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004168
Guido van Rossum60456fd1997-04-09 17:36:32 +00004169 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004170
Guido van Rossum60456fd1997-04-09 17:36:32 +00004171finally:
4172 Py_XDECREF(file);
4173 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004174
Guido van Rossum60456fd1997-04-09 17:36:32 +00004175 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004176}
4177
4178
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004179static char Unpicklertype__doc__[] =
4180"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004181
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004182static PyTypeObject Unpicklertype = {
4183 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004184 0, /*ob_size*/
4185 "Unpickler", /*tp_name*/
4186 sizeof(Unpicklerobject), /*tp_basicsize*/
4187 0, /*tp_itemsize*/
4188 /* methods */
4189 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4190 (printfunc)0, /*tp_print*/
4191 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4192 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4193 (cmpfunc)0, /*tp_compare*/
4194 (reprfunc)0, /*tp_repr*/
4195 0, /*tp_as_number*/
4196 0, /*tp_as_sequence*/
4197 0, /*tp_as_mapping*/
4198 (hashfunc)0, /*tp_hash*/
4199 (ternaryfunc)0, /*tp_call*/
4200 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004201
Guido van Rossum60456fd1997-04-09 17:36:32 +00004202 /* Space for future expansion */
4203 0L,0L,0L,0L,
4204 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004205};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004206
Guido van Rossum60456fd1997-04-09 17:36:32 +00004207static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004208 {"dump", (PyCFunction)cpm_dump, 1,
4209 "dump(object, file, [binary]) --"
4210 "Write an object in pickle format to the given file\n"
4211 "\n"
4212 "If the optional argument, binary, is provided and is true, then the\n"
4213 "pickle will be written in binary format, which is more space and\n"
4214 "computationally efficient. \n"
4215 },
4216 {"dumps", (PyCFunction)cpm_dumps, 1,
4217 "dumps(object, [binary]) --"
4218 "Return a string containing an object in pickle format\n"
4219 "\n"
4220 "If the optional argument, binary, is provided and is true, then the\n"
4221 "pickle will be written in binary format, which is more space and\n"
4222 "computationally efficient. \n"
4223 },
4224 {"load", (PyCFunction)cpm_load, 1,
4225 "load(file) -- Load a pickle from the given file"},
4226 {"loads", (PyCFunction)cpm_loads, 1,
4227 "loads(string) -- Load a pickle from the given string"},
4228 {"Pickler", (PyCFunction)get_Pickler, 1,
4229 "Pickler(file, [binary]) -- Create a pickler\n"
4230 "\n"
4231 "If the optional argument, binary, is provided and is true, then\n"
4232 "pickles will be written in binary format, which is more space and\n"
4233 "computationally efficient. \n"
4234 },
4235 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4236 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004237 { NULL, NULL }
4238};
4239
Guido van Rossum60456fd1997-04-09 17:36:32 +00004240static int
4241init_stuff(PyObject *module, PyObject *module_dict) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004242 PyObject *string, *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004243
4244#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4245
4246 INIT_STR(__class__);
4247 INIT_STR(__getinitargs__);
4248 INIT_STR(__dict__);
4249 INIT_STR(__getstate__);
4250 INIT_STR(__setstate__);
4251 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004252 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004253 INIT_STR(__reduce__);
4254 INIT_STR(write);
4255 INIT_STR(__safe_for_unpickling__);
4256 INIT_STR(append);
4257 INIT_STR(read);
4258 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004259 INIT_STR(copy_reg);
4260 INIT_STR(dispatch_table);
4261 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004262 INIT_STR(__basicnew__);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004263 UNLESS (empty_str=PyString_FromString("")) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004264
Guido van Rossum053b8df1998-11-25 16:18:00 +00004265 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004266 return -1;
4267
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004268 /* These next few are special because we want to use different
4269 ones in restricted mode. */
4270
Guido van Rossum053b8df1998-11-25 16:18:00 +00004271 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004272 return -1;
4273
Guido van Rossum053b8df1998-11-25 16:18:00 +00004274 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4275 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004276 return -1;
4277
4278 Py_DECREF(copy_reg);
4279
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004280 /* Down to here ********************************** */
4281
Guido van Rossum053b8df1998-11-25 16:18:00 +00004282 UNLESS (string = PyImport_ImportModule("string"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004283 return -1;
4284
Guido van Rossum053b8df1998-11-25 16:18:00 +00004285 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004286 return -1;
4287
4288 Py_DECREF(string);
4289
Guido van Rossum053b8df1998-11-25 16:18:00 +00004290 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004291 return -1;
4292
Guido van Rossumc03158b1999-06-09 15:23:31 +00004293 /* Ugh */
4294 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4295 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4296 return -1;
4297
4298 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004299 UNLESS (r=PyRun_String(
4300 "def __init__(self, *args): self.args=args\n\n"
4301 "def __str__(self):\n"
4302 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4303 Py_file_input,
4304 module_dict, t) ) return -1;
4305 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004306
4307 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4308 return -1;
4309
4310 Py_DECREF(t);
4311
4312
4313 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
4314 PickleError, NULL))
4315 return -1;
4316
4317 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004318 UNLESS (r=PyRun_String(
4319 "def __init__(self, *args): self.args=args\n\n"
4320 "def __str__(self):\n"
4321 " a=self.args\n"
4322 " a=a and type(a[0]) or '(what)'\n"
4323 " return 'Cannot pickle %s objects' % a\n"
4324 , Py_file_input,
4325 module_dict, t) ) return -1;
4326 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004327
4328 UNLESS (UnpickleableError = PyErr_NewException(
4329 "cPickle.UnpickleableError", PicklingError, t))
4330 return -1;
4331
4332 Py_DECREF(t);
4333
4334 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
4335 PickleError, NULL))
4336 return -1;
4337
4338 if (PyDict_SetItemString(module_dict, "PickleError",
4339 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004340 return -1;
4341
4342 if (PyDict_SetItemString(module_dict, "PicklingError",
4343 PicklingError) < 0)
4344 return -1;
4345
Guido van Rossum60456fd1997-04-09 17:36:32 +00004346 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4347 UnpicklingError) < 0)
4348 return -1;
4349
Guido van Rossumc03158b1999-06-09 15:23:31 +00004350 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4351 UnpickleableError) < 0)
4352 return -1;
4353
Guido van Rossum053b8df1998-11-25 16:18:00 +00004354 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4355 return -1;
4356
4357 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4358 BadPickleGet) < 0)
4359 return -1;
4360
Guido van Rossum60456fd1997-04-09 17:36:32 +00004361 PycString_IMPORT;
4362
4363 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004364}
4365
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004366#ifndef DL_EXPORT /* declarations for DLL import/export */
4367#define DL_EXPORT(RTYPE) RTYPE
4368#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004369DL_EXPORT(void)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004370initcPickle() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004371 PyObject *m, *d, *v;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004372 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004373 PyObject *format_version;
4374 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004375
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004376 Picklertype.ob_type = &PyType_Type;
4377 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004378 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004379
Guido van Rossum60456fd1997-04-09 17:36:32 +00004380 /* Create the module and add the functions */
4381 m = Py_InitModule4("cPickle", cPickle_methods,
4382 cPickle_module_documentation,
4383 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004384
Guido van Rossum60456fd1997-04-09 17:36:32 +00004385 /* Add some symbolic constants to the module */
4386 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004387 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004388 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004389
Guido van Rossum60456fd1997-04-09 17:36:32 +00004390 format_version = PyString_FromString("1.3");
4391 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004392
Guido van Rossum60456fd1997-04-09 17:36:32 +00004393 PyDict_SetItemString(d, "format_version", format_version);
4394 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004395 Py_XDECREF(format_version);
4396 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004397
Guido van Rossum60456fd1997-04-09 17:36:32 +00004398 init_stuff(m, d);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004399}