blob: 6cea899a230b47ed0e1c157231157816d6113a45 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001/*
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002 * cPickle.c,v 1.66 1999/04/13 17:23:48 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 Rossum1b9e0aa1999-04-19 17:58:18 +000052"cPickle.c,v 1.66 1999/04/13 17:23:48 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
118static PyObject *PicklingError;
119static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000120static PyObject *BadPickleGet;
121
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000122
Guido van Rossum60456fd1997-04-09 17:36:32 +0000123static PyObject *dispatch_table;
124static PyObject *safe_constructors;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000125static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000126
Guido van Rossum60456fd1997-04-09 17:36:32 +0000127static PyObject *__class___str, *__getinitargs___str, *__dict___str,
128 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
129 *write_str, *__safe_for_unpickling___str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000130 *read_str, *readline_str, *__main___str, *__basicnew___str,
Guido van Rossum053b8df1998-11-25 16:18:00 +0000131 *copy_reg_str, *dispatch_table_str, *safe_constructors_str, *empty_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000132
Guido van Rossum60456fd1997-04-09 17:36:32 +0000133static int save();
134static int put2();
135
Guido van Rossum053b8df1998-11-25 16:18:00 +0000136#ifndef PyList_SET_ITEM
137#define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
138#endif
139#ifndef PyList_GET_SIZE
140#define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
141#endif
142#ifndef PyTuple_SET_ITEM
143#define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
144#endif
145#ifndef PyTuple_GET_SIZE
146#define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
147#endif
148#ifndef PyString_GET_SIZE
149#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
150#endif
151
152/*************************************************************************
153 Internal Data type for pickle data. */
154
155typedef struct {
156 PyObject_HEAD
157 int length, size;
158 PyObject **data;
159} Pdata;
160
161static void
162Pdata_dealloc(Pdata *self) {
163 int i;
164 PyObject **p;
165
166 for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
167
168 if (self->data) free(self->data);
169
170 PyMem_DEL(self);
171}
172
173static PyTypeObject PdataType = {
174 PyObject_HEAD_INIT(NULL) 0, "Pdata", sizeof(Pdata), 0,
175 (destructor)Pdata_dealloc,
176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
177};
178
179#define Pdata_Check(O) ((O)->ob_type == &PdataType)
180
181static PyObject *
182Pdata_New() {
183 Pdata *self;
184
185 UNLESS (self = PyObject_NEW(Pdata, &PdataType)) return NULL;
186 self->size=8;
187 self->length=0;
188 self->data=malloc(self->size * sizeof(PyObject*));
189 if (self->data) return (PyObject*)self;
190 Py_DECREF(self);
191 return PyErr_NoMemory();
192}
193
194static int
195stackUnderflow() {
196 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
197 return -1;
198}
199
200static int
201Pdata_clear(Pdata *self, int clearto) {
202 int i;
203 PyObject **p;
204
205 if (clearto < 0) return stackUnderflow();
206 if (clearto >= self->length) return 0;
207
208 for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
209 Py_DECREF(*p);
210 self->length=clearto;
211
212 return 0;
213}
214
215
216static int
217Pdata_grow(Pdata *self) {
218 if (! self->size) {
219 PyErr_NoMemory();
220 return -1;
221 }
222 self->size *= 2;
223 self->data = realloc(self->data, self->size*sizeof(PyObject*));
224 if (! self->data) {
225 self->size = 0;
226 PyErr_NoMemory();
227 return -1;
228 }
229 return 0;
230}
231
232#define PDATA_POP(D,V) { \
233 if ((D)->length) V=D->data[--((D)->length)]; \
234 else { \
235 PyErr_SetString(UnpicklingError, "bad pickle data"); \
236 V=NULL; \
237 } \
238}
239
240
241static PyObject *
242Pdata_popTuple(Pdata *self, int start) {
243 PyObject *r;
244 int i, j, l;
245
246 l=self->length-start;
247 UNLESS (r=PyTuple_New(l)) return NULL;
248 for (i=start, j=0 ; j < l; )
249 PyTuple_SET_ITEM(r,j++,self->data[i++]);
250
251 self->length=start;
252 return r;
253}
254
255static PyObject *
256Pdata_popList(Pdata *self, int start) {
257 PyObject *r;
258 int i, j, l;
259
260 l=self->length-start;
261 UNLESS (r=PyList_New(l)) return NULL;
262 for (i=start, j=0 ; j < l; )
263 PyList_SET_ITEM(r,j++,self->data[i++]);
264
265 self->length=start;
266 return r;
267}
268
269#define PDATA_APPEND_(D,O,ER) { \
270 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
271}
272
273#define PDATA_APPEND(D,O,ER) { \
274 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
275 Pdata_grow((Pdata*)(D)) < 0) \
276 return ER; \
277 Py_INCREF(O); \
278 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
279}
280
281#define PDATA_PUSH(D,O,ER) { \
282 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
283 Pdata_grow((Pdata*)(D)) < 0) { \
284 Py_DECREF(O); \
285 return ER; \
286 } \
287 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
288}
289
290/*************************************************************************/
291
292#define ARG_TUP(self, o) { \
293 if (self->arg || (self->arg=PyTuple_New(1))) { \
294 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
295 PyTuple_SET_ITEM(self->arg,0,o); \
296 } \
297 else { \
298 Py_DECREF(o); \
299 } \
300}
301
302#define FREE_ARG_TUP(self) { \
303 if (self->arg->ob_refcnt > 1) { \
304 Py_DECREF(self->arg); \
305 self->arg=NULL; \
306 } \
307 }
308
Guido van Rossum60456fd1997-04-09 17:36:32 +0000309typedef struct {
310 PyObject_HEAD
311 FILE *fp;
312 PyObject *write;
313 PyObject *file;
314 PyObject *memo;
315 PyObject *arg;
316 PyObject *pers_func;
317 PyObject *inst_pers_func;
318 int bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000319 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000320 int (*write_func)();
321 char *write_buf;
322 int buf_size;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000323 PyObject *dispatch_table;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000324} Picklerobject;
325
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000326staticforward PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000327
Guido van Rossum60456fd1997-04-09 17:36:32 +0000328typedef struct {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000329 PyObject_HEAD
330 FILE *fp;
331 PyObject *file;
332 PyObject *readline;
333 PyObject *read;
334 PyObject *memo;
335 PyObject *arg;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000336 Pdata *stack;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000337 PyObject *mark;
338 PyObject *pers_func;
339 PyObject *last_string;
340 int *marks;
341 int num_marks;
342 int marks_size;
343 int (*read_func)();
344 int (*readline_func)();
345 int buf_size;
346 char *buf;
347 PyObject *safe_constructors;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +0000348 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000349} Unpicklerobject;
350
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000351staticforward PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000352
Guido van Rossum60456fd1997-04-09 17:36:32 +0000353int
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000354cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000355 PyObject *v;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000356
Guido van Rossum053b8df1998-11-25 16:18:00 +0000357 if ((v = PyObject_GetItem(o,key))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000358 Py_DECREF(v);
359 return 1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000360 }
361
Guido van Rossum60456fd1997-04-09 17:36:32 +0000362 PyErr_Clear();
363 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000364}
365
Guido van Rossumd385d591997-04-09 17:47:47 +0000366static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000367PyObject *
368#ifdef HAVE_STDARG_PROTOTYPES
369/* VARARGS 2 */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000370cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000371#else
372/* VARARGS */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000373cPickle_ErrFormat(va_alist) va_dcl {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000374#endif
Guido van Rossum60456fd1997-04-09 17:36:32 +0000375 va_list va;
376 PyObject *args=0, *retval=0;
377#ifdef HAVE_STDARG_PROTOTYPES
378 va_start(va, format);
379#else
380 PyObject *ErrType;
381 char *stringformat, *format;
382 va_start(va);
383 ErrType = va_arg(va, PyObject *);
384 stringformat = va_arg(va, char *);
385 format = va_arg(va, char *);
386#endif
387
Guido van Rossum053b8df1998-11-25 16:18:00 +0000388 if (format) args = Py_VaBuildValue(format, va);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000389 va_end(va);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000390 if (format && ! args) return NULL;
391 if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000392
Guido van Rossum053b8df1998-11-25 16:18:00 +0000393 if (retval) {
394 if (args) {
395 PyObject *v;
396 v=PyString_Format(retval, args);
397 Py_DECREF(retval);
398 Py_DECREF(args);
399 if (! v) return NULL;
400 retval=v;
401 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000402 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000403 else
Guido van Rossum053b8df1998-11-25 16:18:00 +0000404 if (args) retval=args;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000405 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000406 PyErr_SetObject(ErrType,Py_None);
407 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000408 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000409 PyErr_SetObject(ErrType,retval);
410 Py_DECREF(retval);
411 return NULL;
412}
413
414static int
415write_file(Picklerobject *self, char *s, int n) {
416 if (s == NULL) {
417 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000418 }
419
Guido van Rossum60456fd1997-04-09 17:36:32 +0000420 if ((int)fwrite(s, sizeof(char), n, self->fp) != n) {
421 PyErr_SetFromErrno(PyExc_IOError);
422 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000423 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000424
425 return n;
426}
427
Guido van Rossum60456fd1997-04-09 17:36:32 +0000428static int
429write_cStringIO(Picklerobject *self, char *s, int n) {
430 if (s == NULL) {
431 return 0;
432 }
433
434 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
435 return -1;
436 }
437
438 return n;
439}
440
Guido van Rossum60456fd1997-04-09 17:36:32 +0000441static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000442write_none(Picklerobject *self, char *s, int n) {
443 if (s == NULL) return 0;
444 return n;
445}
446
Guido van Rossum142eeb81997-08-13 03:14:41 +0000447static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000448write_other(Picklerobject *self, char *s, int n) {
449 PyObject *py_str = 0, *junk = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000450
451 if (s == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000452 UNLESS (self->buf_size) return 0;
453 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000454 PyString_FromStringAndSize(self->write_buf, self->buf_size))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000455 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000456 }
457 else {
458 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
459 if (write_other(self, NULL, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000460 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000461 }
462
463 if (n > WRITE_BUF_SIZE) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000464 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000465 PyString_FromStringAndSize(s, n))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000466 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000467 }
468 else {
469 memcpy(self->write_buf + self->buf_size, s, n);
470 self->buf_size += n;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000471 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000472 }
473 }
474
Guido van Rossum053b8df1998-11-25 16:18:00 +0000475 if (self->write) {
476 /* object with write method */
477 ARG_TUP(self, py_str);
478 if (self->arg) {
479 junk = PyObject_CallObject(self->write, self->arg);
480 FREE_ARG_TUP(self);
481 }
482 if (junk) Py_DECREF(junk);
483 else return -1;
484 }
485 else
486 PDATA_PUSH(self->file, py_str, -1);
487
488 self->buf_size = 0;
489 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000490}
491
492
493static int
494read_file(Unpicklerobject *self, char **s, int n) {
495
496 if (self->buf_size == 0) {
497 int size;
498
499 size = ((n < 32) ? 32 : n);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000500 UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000501 PyErr_NoMemory();
502 return -1;
503 }
504
505 self->buf_size = size;
506 }
507 else if (n > self->buf_size) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000508 UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000509 PyErr_NoMemory();
510 return -1;
511 }
512
513 self->buf_size = n;
514 }
515
516 if ((int)fread(self->buf, sizeof(char), n, self->fp) != n) {
517 if (feof(self->fp)) {
518 PyErr_SetNone(PyExc_EOFError);
519 return -1;
520 }
521
522 PyErr_SetFromErrno(PyExc_IOError);
523 return -1;
524 }
525
526 *s = self->buf;
527
528 return n;
529}
530
531
532static int
533readline_file(Unpicklerobject *self, char **s) {
534 int i;
535
536 if (self->buf_size == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000537 UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000538 PyErr_NoMemory();
539 return -1;
540 }
541
542 self->buf_size = 40;
543 }
544
545 i = 0;
546 while (1) {
547 for (; i < (self->buf_size - 1); i++) {
548 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
549 self->buf[i + 1] = '\0';
550 *s = self->buf;
551 return i + 1;
552 }
553 }
554
Guido van Rossum053b8df1998-11-25 16:18:00 +0000555 UNLESS (self->buf = (char *)realloc(self->buf,
Guido van Rossum60456fd1997-04-09 17:36:32 +0000556 (self->buf_size * 2) * sizeof(char))) {
557 PyErr_NoMemory();
558 return -1;
559 }
560
561 self->buf_size *= 2;
562 }
563
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000564}
565
566
567static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000568read_cStringIO(Unpicklerobject *self, char **s, int n) {
569 char *ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000570
Guido van Rossum60456fd1997-04-09 17:36:32 +0000571 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
572 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000573 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000574 }
575
Guido van Rossum60456fd1997-04-09 17:36:32 +0000576 *s = ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000577
Guido van Rossum60456fd1997-04-09 17:36:32 +0000578 return n;
579}
580
581
582static int
583readline_cStringIO(Unpicklerobject *self, char **s) {
584 int n;
585 char *ptr;
586
587 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
588 return -1;
589 }
590
591 *s = ptr;
592
593 return n;
594}
595
596
597static int
598read_other(Unpicklerobject *self, char **s, int n) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000599 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000600 int res = -1;
601
Guido van Rossum053b8df1998-11-25 16:18:00 +0000602 UNLESS (bytes = PyInt_FromLong(n)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000603
Guido van Rossum053b8df1998-11-25 16:18:00 +0000604 ARG_TUP(self, bytes);
605 if (self->arg) {
606 str = PyObject_CallObject(self->read, self->arg);
607 FREE_ARG_TUP(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000608 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000609 if (! str) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000610
611 Py_XDECREF(self->last_string);
612 self->last_string = str;
613
Guido van Rossum053b8df1998-11-25 16:18:00 +0000614 if (! (*s = PyString_AsString(str))) return -1;
615 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000616}
617
618
619static int
620readline_other(Unpicklerobject *self, char **s) {
621 PyObject *str;
622 int str_size;
623
Guido van Rossum053b8df1998-11-25 16:18:00 +0000624 UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000625 return -1;
626 }
627
Guido van Rossum053b8df1998-11-25 16:18:00 +0000628 if ((str_size = PyString_Size(str)) < 0)
629 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000630
631 Py_XDECREF(self->last_string);
632 self->last_string = str;
633
Guido van Rossum053b8df1998-11-25 16:18:00 +0000634 if (! (*s = PyString_AsString(str)))
635 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000636
637 return str_size;
638}
639
640
641static char *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000642pystrndup(char *s, int l) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000643 char *r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000644 UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
Guido van Rossum60456fd1997-04-09 17:36:32 +0000645 memcpy(r,s,l);
646 r[l]=0;
647 return r;
648}
649
650
651static int
652get(Picklerobject *self, PyObject *id) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000653 PyObject *value, *mv;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000654 long c_value;
655 char s[30];
656 int len;
657
Guido van Rossum053b8df1998-11-25 16:18:00 +0000658 UNLESS (mv = PyDict_GetItem(self->memo, id)) {
659 PyErr_SetObject(PyExc_KeyError, id);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000660 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000661 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000662
Guido van Rossum053b8df1998-11-25 16:18:00 +0000663 UNLESS (value = PyTuple_GetItem(mv, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000664 return -1;
665
Guido van Rossum053b8df1998-11-25 16:18:00 +0000666 UNLESS (PyInt_Check(value)) {
667 PyErr_SetString(PicklingError, "no int where int expected in memo");
668 return -1;
669 }
670 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671
672 if (!self->bin) {
673 s[0] = GET;
674 sprintf(s + 1, "%ld\n", c_value);
675 len = strlen(s);
676 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000677 else if (Pdata_Check(self->file)) {
678 if (write_other(self, NULL, 0) < 0) return -1;
679 PDATA_APPEND(self->file, mv, -1);
680 return 0;
681 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000682 else {
683 if (c_value < 256) {
684 s[0] = BINGET;
685 s[1] = (int)(c_value & 0xff);
686 len = 2;
687 }
688 else {
689 s[0] = LONG_BINGET;
690 s[1] = (int)(c_value & 0xff);
691 s[2] = (int)((c_value >> 8) & 0xff);
692 s[3] = (int)((c_value >> 16) & 0xff);
693 s[4] = (int)((c_value >> 24) & 0xff);
694 len = 5;
695 }
696 }
697
698 if ((*self->write_func)(self, s, len) < 0)
699 return -1;
700
701 return 0;
702}
703
704
705static int
706put(Picklerobject *self, PyObject *ob) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000707 if (ob->ob_refcnt < 2 || self->fast)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000708 return 0;
709
710 return put2(self, ob);
711}
712
713
714static int
715put2(Picklerobject *self, PyObject *ob) {
716 char c_str[30];
717 int p, len, res = -1;
718 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000719
720 if (self->fast) return 0;
721
Guido van Rossum60456fd1997-04-09 17:36:32 +0000722 if ((p = PyDict_Size(self->memo)) < 0)
723 goto finally;
724
Guido van Rossum053b8df1998-11-25 16:18:00 +0000725 p++; /* Make sure memo keys are positive! */
726
727 UNLESS (py_ob_id = PyInt_FromLong((long)ob))
728 goto finally;
729
730 UNLESS (memo_len = PyInt_FromLong(p))
731 goto finally;
732
733 UNLESS (t = PyTuple_New(2))
734 goto finally;
735
736 PyTuple_SET_ITEM(t, 0, memo_len);
737 Py_INCREF(memo_len);
738 PyTuple_SET_ITEM(t, 1, ob);
739 Py_INCREF(ob);
740
741 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
742 goto finally;
743
Guido van Rossum60456fd1997-04-09 17:36:32 +0000744 if (!self->bin) {
745 c_str[0] = PUT;
746 sprintf(c_str + 1, "%d\n", p);
747 len = strlen(c_str);
748 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000749 else if (Pdata_Check(self->file)) {
750 if (write_other(self, NULL, 0) < 0) return -1;
751 PDATA_APPEND(self->file, memo_len, -1);
752 res=0; /* Job well done ;) */
753 goto finally;
754 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000755 else {
756 if (p >= 256) {
757 c_str[0] = LONG_BINPUT;
758 c_str[1] = (int)(p & 0xff);
759 c_str[2] = (int)((p >> 8) & 0xff);
760 c_str[3] = (int)((p >> 16) & 0xff);
761 c_str[4] = (int)((p >> 24) & 0xff);
762 len = 5;
763 }
764 else {
765 c_str[0] = BINPUT;
766 c_str[1] = p;
767 len = 2;
768 }
769 }
770
771 if ((*self->write_func)(self, c_str, len) < 0)
772 goto finally;
773
Guido van Rossum60456fd1997-04-09 17:36:32 +0000774 res = 0;
775
776finally:
777 Py_XDECREF(py_ob_id);
778 Py_XDECREF(memo_len);
779 Py_XDECREF(t);
780
781 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000782}
783
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000784#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000785
786static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000787PyImport_Import(PyObject *module_name) {
788 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
789 static PyObject *standard_builtins=0;
790 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
791
Guido van Rossum053b8df1998-11-25 16:18:00 +0000792 UNLESS (silly_list) {
793 UNLESS (__import___str=PyString_FromString("__import__"))
794 return NULL;
795 UNLESS (__builtins___str=PyString_FromString("__builtins__"))
796 return NULL;
797 UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
798 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000799 }
800
Guido van Rossum053b8df1998-11-25 16:18:00 +0000801 if ((globals=PyEval_GetGlobals())) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000802 Py_INCREF(globals);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000803 UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
804 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000805 }
806 else {
807 PyErr_Clear();
808
Guido van Rossum053b8df1998-11-25 16:18:00 +0000809 UNLESS (standard_builtins ||
810 (standard_builtins=PyImport_ImportModule("__builtin__")))
811 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000812
813 __builtins__=standard_builtins;
814 Py_INCREF(__builtins__);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000815 UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
816 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000817 }
818
Guido van Rossum053b8df1998-11-25 16:18:00 +0000819 if (PyDict_Check(__builtins__)) {
820 UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000821 }
822 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000823 UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000824 }
825
Guido van Rossum053b8df1998-11-25 16:18:00 +0000826 UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
827 module_name, globals, globals, silly_list))
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000828 goto err;
829
830 Py_DECREF(globals);
831 Py_DECREF(__builtins__);
832 Py_DECREF(__import__);
833
834 return r;
835err:
836 Py_XDECREF(globals);
837 Py_XDECREF(__builtins__);
838 Py_XDECREF(__import__);
839 return NULL;
840}
841
842static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +0000843whichmodule(PyObject *global, PyObject *global_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000844 int i, j;
845 PyObject *module = 0, *modules_dict = 0,
846 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000847
Guido van Rossum45188231997-09-28 05:38:51 +0000848 module = PyObject_GetAttrString(global, "__module__");
849 if (module) return module;
850 PyErr_Clear();
851
Guido van Rossum053b8df1998-11-25 16:18:00 +0000852 UNLESS (modules_dict = PySys_GetObject("modules"))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000853 return NULL;
854
Guido van Rossum60456fd1997-04-09 17:36:32 +0000855 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000856 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
857
Guido van Rossum053b8df1998-11-25 16:18:00 +0000858 if (PyObject_Compare(name, __main___str)==0) continue;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000859
Guido van Rossum053b8df1998-11-25 16:18:00 +0000860 UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000861 PyErr_Clear();
862 continue;
863 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000864
Guido van Rossum60456fd1997-04-09 17:36:32 +0000865 if (global_name_attr != global) {
866 Py_DECREF(global_name_attr);
867 continue;
868 }
869
870 Py_DECREF(global_name_attr);
871
872 break;
873 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000874
875 /* The following implements the rule in pickle.py added in 1.5
876 that used __main__ if no module is found. I don't actually
877 like this rule. jlf
878 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000879 if (!j) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000880 j=1;
881 name=__main___str;
882 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000883
884 Py_INCREF(name);
885 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000886}
887
888
Guido van Rossum60456fd1997-04-09 17:36:32 +0000889static int
890save_none(Picklerobject *self, PyObject *args) {
891 static char none = NONE;
892 if ((*self->write_func)(self, &none, 1) < 0)
893 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000894
Guido van Rossum60456fd1997-04-09 17:36:32 +0000895 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000896}
897
898
Guido van Rossum60456fd1997-04-09 17:36:32 +0000899static int
900save_int(Picklerobject *self, PyObject *args) {
901 char c_str[32];
902 long l = PyInt_AS_LONG((PyIntObject *)args);
903 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000904
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000905 if (!self->bin
906#if SIZEOF_LONG > 4
Guido van Rossum053b8df1998-11-25 16:18:00 +0000907 || (l >> 32)
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000908#endif
Guido van Rossum053b8df1998-11-25 16:18:00 +0000909 ) {
910 /* Save extra-long ints in non-binary mode, so that
911 we can use python long parsing code to restore,
912 if necessary. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000913 c_str[0] = INT;
914 sprintf(c_str + 1, "%ld\n", l);
915 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
916 return -1;
917 }
918 else {
919 c_str[1] = (int)( l & 0xff);
920 c_str[2] = (int)((l >> 8) & 0xff);
921 c_str[3] = (int)((l >> 16) & 0xff);
922 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000923
Guido van Rossum60456fd1997-04-09 17:36:32 +0000924 if ((c_str[4] == 0) && (c_str[3] == 0)) {
925 if (c_str[2] == 0) {
926 c_str[0] = BININT1;
927 len = 2;
928 }
929 else {
930 c_str[0] = BININT2;
931 len = 3;
932 }
933 }
934 else {
935 c_str[0] = BININT;
936 len = 5;
937 }
938
939 if ((*self->write_func)(self, c_str, len) < 0)
940 return -1;
941 }
942
943 return 0;
944}
945
946
947static int
948save_long(Picklerobject *self, PyObject *args) {
949 int size, res = -1;
950 PyObject *repr = 0;
951
952 static char l = LONG;
953
Guido van Rossum053b8df1998-11-25 16:18:00 +0000954 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000955 goto finally;
956
957 if ((size = PyString_Size(repr)) < 0)
958 goto finally;
959
960 if ((*self->write_func)(self, &l, 1) < 0)
961 goto finally;
962
963 if ((*self->write_func)(self,
964 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
965 goto finally;
966
967 if ((*self->write_func)(self, "\n", 1) < 0)
968 goto finally;
969
970 res = 0;
971
972finally:
973 Py_XDECREF(repr);
974
975 return res;
976}
977
978
979static int
980save_float(Picklerobject *self, PyObject *args) {
981 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
982
Guido van Rossum60456fd1997-04-09 17:36:32 +0000983 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000984 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000985 double f;
986 long fhi, flo;
987 char str[9], *p = str;
988
989 *p = BINFLOAT;
990 p++;
991
992 if (x < 0) {
993 s = 1;
994 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000995 }
996 else
Guido van Rossum60456fd1997-04-09 17:36:32 +0000997 s = 0;
998
999 f = frexp(x, &e);
1000
1001 /* Normalize f to be in the range [1.0, 2.0) */
1002 if (0.5 <= f && f < 1.0) {
1003 f *= 2.0;
1004 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001005 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001006 else if (f == 0.0) {
1007 e = 0;
1008 }
1009 else {
1010 PyErr_SetString(PyExc_SystemError,
1011 "frexp() result out of range");
1012 return -1;
1013 }
1014
1015 if (e >= 1024) {
1016 /* XXX 1024 itself is reserved for Inf/NaN */
1017 PyErr_SetString(PyExc_OverflowError,
1018 "float too large to pack with d format");
1019 return -1;
1020 }
1021 else if (e < -1022) {
1022 /* Gradual underflow */
1023 f = ldexp(f, 1022 + e);
1024 e = 0;
1025 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001026 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001027 e += 1023;
1028 f -= 1.0; /* Get rid of leading 1 */
1029 }
1030
1031 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1032 f *= 268435456.0; /* 2**28 */
1033 fhi = (long) floor(f); /* Truncate */
1034 f -= (double)fhi;
1035 f *= 16777216.0; /* 2**24 */
1036 flo = (long) floor(f + 0.5); /* Round */
1037
1038 /* First byte */
1039 *p = (s<<7) | (e>>4);
1040 p++;
1041
1042 /* Second byte */
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001043 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum60456fd1997-04-09 17:36:32 +00001044 p++;
1045
1046 /* Third byte */
1047 *p = (fhi>>16) & 0xFF;
1048 p++;
1049
1050 /* Fourth byte */
1051 *p = (fhi>>8) & 0xFF;
1052 p++;
1053
1054 /* Fifth byte */
1055 *p = fhi & 0xFF;
1056 p++;
1057
1058 /* Sixth byte */
1059 *p = (flo>>16) & 0xFF;
1060 p++;
1061
1062 /* Seventh byte */
1063 *p = (flo>>8) & 0xFF;
1064 p++;
1065
1066 /* Eighth byte */
1067 *p = flo & 0xFF;
1068
1069 if ((*self->write_func)(self, str, 9) < 0)
1070 return -1;
1071 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001072 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001073 char c_str[250];
1074 c_str[0] = FLOAT;
Guido van Rossum104be4a1998-04-03 21:13:02 +00001075 sprintf(c_str + 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001076
1077 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
1078 return -1;
1079 }
1080
1081 return 0;
1082}
1083
1084
1085static int
Guido van Rossum142eeb81997-08-13 03:14:41 +00001086save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001087 int size, len;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001088 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001089
Guido van Rossum053b8df1998-11-25 16:18:00 +00001090 if ((size = PyString_Size(args)) < 0)
1091 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001092
1093 if (!self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001094 char *repr_str;
1095
1096 static char string = STRING;
1097
Guido van Rossum053b8df1998-11-25 16:18:00 +00001098 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001099 return -1;
1100
Guido van Rossum053b8df1998-11-25 16:18:00 +00001101 if ((len = PyString_Size(repr)) < 0)
1102 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001103 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001104
1105 if ((*self->write_func)(self, &string, 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001106 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001107
1108 if ((*self->write_func)(self, repr_str, len) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001109 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001110
1111 if ((*self->write_func)(self, "\n", 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001112 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001113
1114 Py_XDECREF(repr);
1115 }
1116 else {
1117 int i;
1118 char c_str[5];
1119
Guido van Rossum053b8df1998-11-25 16:18:00 +00001120 if ((size = PyString_Size(args)) < 0)
1121 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001122
1123 if (size < 256) {
1124 c_str[0] = SHORT_BINSTRING;
1125 c_str[1] = size;
1126 len = 2;
1127 }
1128 else {
1129 c_str[0] = BINSTRING;
1130 for (i = 1; i < 5; i++)
1131 c_str[i] = (int)(size >> ((i - 1) * 8));
1132 len = 5;
1133 }
1134
1135 if ((*self->write_func)(self, c_str, len) < 0)
1136 return -1;
1137
Guido van Rossum053b8df1998-11-25 16:18:00 +00001138 if (size > 128 && Pdata_Check(self->file)) {
1139 if (write_other(self, NULL, 0) < 0) return -1;
1140 PDATA_APPEND(self->file, args, -1);
1141 }
1142 else {
1143 if ((*self->write_func)(self,
1144 PyString_AS_STRING((PyStringObject *)args), size) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001145 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001146 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001147 }
1148
Guido van Rossum142eeb81997-08-13 03:14:41 +00001149 if (doput)
1150 if (put(self, args) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001151 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001152
1153 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001154
1155err:
1156 Py_XDECREF(repr);
1157 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001158}
1159
1160
1161static int
1162save_tuple(Picklerobject *self, PyObject *args) {
1163 PyObject *element = 0, *py_tuple_id = 0;
1164 int len, i, has_key, res = -1;
1165
1166 static char tuple = TUPLE;
1167
1168 if ((*self->write_func)(self, &MARKv, 1) < 0)
1169 goto finally;
1170
1171 if ((len = PyTuple_Size(args)) < 0)
1172 goto finally;
1173
1174 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001175 UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001176 goto finally;
1177
1178 if (save(self, element, 0) < 0)
1179 goto finally;
1180 }
1181
Guido van Rossum053b8df1998-11-25 16:18:00 +00001182 UNLESS (py_tuple_id = PyInt_FromLong((long)args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001183 goto finally;
1184
1185 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001186 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001187 goto finally;
1188
1189 if (has_key) {
1190 if (self->bin) {
1191 static char pop_mark = POP_MARK;
1192
1193 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1194 goto finally;
1195 }
1196 else {
1197 static char pop = POP;
1198
1199 for (i = 0; i <= len; i++) {
1200 if ((*self->write_func)(self, &pop, 1) < 0)
1201 goto finally;
1202 }
1203 }
1204
1205 if (get(self, py_tuple_id) < 0)
1206 goto finally;
1207
1208 res = 0;
1209 goto finally;
1210 }
1211 }
1212
1213 if ((*self->write_func)(self, &tuple, 1) < 0) {
1214 goto finally;
1215 }
1216
1217 if (put(self, args) < 0)
1218 goto finally;
1219
1220 res = 0;
1221
1222finally:
1223 Py_XDECREF(py_tuple_id);
1224
1225 return res;
1226}
1227
1228static int
1229save_empty_tuple(Picklerobject *self, PyObject *args) {
1230 static char tuple = EMPTY_TUPLE;
1231
1232 return (*self->write_func)(self, &tuple, 1);
1233}
1234
1235
1236static int
1237save_list(Picklerobject *self, PyObject *args) {
1238 PyObject *element = 0;
1239 int s_len, len, i, using_appends, res = -1;
1240 char s[3];
1241
1242 static char append = APPEND, appends = APPENDS;
1243
Guido van Rossum053b8df1998-11-25 16:18:00 +00001244 if (self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001245 s[0] = EMPTY_LIST;
1246 s_len = 1;
1247 }
1248 else {
1249 s[0] = MARK;
1250 s[1] = LIST;
1251 s_len = 2;
1252 }
1253
1254 if ((len = PyList_Size(args)) < 0)
1255 goto finally;
1256
1257 if ((*self->write_func)(self, s, s_len) < 0)
1258 goto finally;
1259
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001260 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001261 if (put(self, args) < 0)
1262 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001263 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001264 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001265 if (put2(self, args) < 0)
1266 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001267 }
1268
Guido van Rossum142eeb81997-08-13 03:14:41 +00001269 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001270 if ((*self->write_func)(self, &MARKv, 1) < 0)
1271 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001272
Guido van Rossum60456fd1997-04-09 17:36:32 +00001273 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001274 UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001275 goto finally;
1276
1277 if (save(self, element, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001278 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001279
1280 if (!using_appends) {
1281 if ((*self->write_func)(self, &append, 1) < 0)
1282 goto finally;
1283 }
1284 }
1285
1286 if (using_appends) {
1287 if ((*self->write_func)(self, &appends, 1) < 0)
1288 goto finally;
1289 }
1290
1291 res = 0;
1292
1293finally:
1294
1295 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001296}
1297
1298
Guido van Rossum60456fd1997-04-09 17:36:32 +00001299static int
1300save_dict(Picklerobject *self, PyObject *args) {
1301 PyObject *key = 0, *value = 0;
1302 int i, len, res = -1, using_setitems;
1303 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001304
Guido van Rossum60456fd1997-04-09 17:36:32 +00001305 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001306
Guido van Rossum60456fd1997-04-09 17:36:32 +00001307 if (self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001308 s[0] = EMPTY_DICT;
1309 len = 1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001310 }
1311 else {
1312 s[0] = MARK;
1313 s[1] = DICT;
1314 len = 2;
1315 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001316
Guido van Rossum60456fd1997-04-09 17:36:32 +00001317 if ((*self->write_func)(self, s, len) < 0)
1318 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001319
Guido van Rossum60456fd1997-04-09 17:36:32 +00001320 if ((len = PyDict_Size(args)) < 0)
1321 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001322
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001323 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001324 if (put(self, args) < 0)
1325 goto finally;
1326 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001327 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001328 if (put2(self, args) < 0)
1329 goto finally;
1330 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001331
Guido van Rossum142eeb81997-08-13 03:14:41 +00001332 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001333 if ((*self->write_func)(self, &MARKv, 1) < 0)
1334 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001335
Guido van Rossum60456fd1997-04-09 17:36:32 +00001336 i = 0;
1337 while (PyDict_Next(args, &i, &key, &value)) {
1338 if (save(self, key, 0) < 0)
1339 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001340
Guido van Rossum60456fd1997-04-09 17:36:32 +00001341 if (save(self, value, 0) < 0)
1342 goto finally;
1343
1344 if (!using_setitems) {
1345 if ((*self->write_func)(self, &setitem, 1) < 0)
1346 goto finally;
1347 }
1348 }
1349
1350 if (using_setitems) {
1351 if ((*self->write_func)(self, &setitems, 1) < 0)
1352 goto finally;
1353 }
1354
1355 res = 0;
1356
1357finally:
1358
1359 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001360}
1361
1362
Guido van Rossum60456fd1997-04-09 17:36:32 +00001363static int
1364save_inst(Picklerobject *self, PyObject *args) {
1365 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1366 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1367 char *module_str, *name_str;
1368 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001369
Guido van Rossum60456fd1997-04-09 17:36:32 +00001370 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001371
Guido van Rossum60456fd1997-04-09 17:36:32 +00001372 if ((*self->write_func)(self, &MARKv, 1) < 0)
1373 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001374
Guido van Rossum053b8df1998-11-25 16:18:00 +00001375 UNLESS (class = PyObject_GetAttr(args, __class___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001376 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001377
Guido van Rossum60456fd1997-04-09 17:36:32 +00001378 if (self->bin) {
1379 if (save(self, class, 0) < 0)
1380 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001381 }
1382
Guido van Rossum142eeb81997-08-13 03:14:41 +00001383 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001384 PyObject *element = 0;
1385 int i, len;
1386
Guido van Rossum053b8df1998-11-25 16:18:00 +00001387 UNLESS (class_args =
Guido van Rossum60456fd1997-04-09 17:36:32 +00001388 PyObject_CallObject(getinitargs_func, empty_tuple))
1389 goto finally;
1390
1391 if ((len = PyObject_Length(class_args)) < 0)
1392 goto finally;
1393
1394 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001395 UNLESS (element = PySequence_GetItem(class_args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001396 goto finally;
1397
1398 if (save(self, element, 0) < 0) {
1399 Py_DECREF(element);
1400 goto finally;
1401 }
1402
1403 Py_DECREF(element);
1404 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001405 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001406 else {
1407 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001408 }
1409
Guido van Rossum60456fd1997-04-09 17:36:32 +00001410 if (!self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001411 UNLESS (name = ((PyClassObject *)class)->cl_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001412 PyErr_SetString(PicklingError, "class has no name");
1413 goto finally;
1414 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001415
Guido van Rossum053b8df1998-11-25 16:18:00 +00001416 UNLESS (module = whichmodule(class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001417 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001418
1419
1420 if ((module_size = PyString_Size(module)) < 0 ||
1421 (name_size = PyString_Size(name)) < 0)
1422 goto finally;
1423
Guido van Rossum60456fd1997-04-09 17:36:32 +00001424 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001425 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001426
Guido van Rossum60456fd1997-04-09 17:36:32 +00001427 if ((*self->write_func)(self, &inst, 1) < 0)
1428 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001429
Guido van Rossum60456fd1997-04-09 17:36:32 +00001430 if ((*self->write_func)(self, module_str, module_size) < 0)
1431 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001432
Guido van Rossum60456fd1997-04-09 17:36:32 +00001433 if ((*self->write_func)(self, "\n", 1) < 0)
1434 goto finally;
1435
1436 if ((*self->write_func)(self, name_str, name_size) < 0)
1437 goto finally;
1438
1439 if ((*self->write_func)(self, "\n", 1) < 0)
1440 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001441 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001442 else if ((*self->write_func)(self, &obj, 1) < 0) {
1443 goto finally;
1444 }
1445
Guido van Rossum142eeb81997-08-13 03:14:41 +00001446 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001447 UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001448 goto finally;
1449 }
1450 else {
1451 PyErr_Clear();
1452
Guido van Rossum053b8df1998-11-25 16:18:00 +00001453 UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001454 PyErr_Clear();
1455 res = 0;
1456 goto finally;
1457 }
1458 }
1459
1460 if (!PyDict_Check(state)) {
1461 if (put2(self, args) < 0)
1462 goto finally;
1463 }
1464 else {
1465 if (put(self, args) < 0)
1466 goto finally;
1467 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001468
Guido van Rossum60456fd1997-04-09 17:36:32 +00001469 if (save(self, state, 0) < 0)
1470 goto finally;
1471
1472 if ((*self->write_func)(self, &build, 1) < 0)
1473 goto finally;
1474
1475 res = 0;
1476
1477finally:
1478 Py_XDECREF(module);
1479 Py_XDECREF(class);
1480 Py_XDECREF(state);
1481 Py_XDECREF(getinitargs_func);
1482 Py_XDECREF(getstate_func);
1483 Py_XDECREF(class_args);
1484
1485 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001486}
1487
1488
Guido van Rossum60456fd1997-04-09 17:36:32 +00001489static int
1490save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1491 PyObject *global_name = 0, *module = 0;
1492 char *name_str, *module_str;
1493 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001494
Guido van Rossum60456fd1997-04-09 17:36:32 +00001495 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001496
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001497 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001498 global_name = name;
1499 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001500 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001501 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001502 UNLESS (global_name = PyObject_GetAttr(args, __name___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001503 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001504 }
1505
Guido van Rossum053b8df1998-11-25 16:18:00 +00001506 UNLESS (module = whichmodule(args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001507 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001508
Guido van Rossum053b8df1998-11-25 16:18:00 +00001509 if ((module_size = PyString_Size(module)) < 0 ||
1510 (name_size = PyString_Size(global_name)) < 0)
1511 goto finally;
1512
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001513 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001514 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001515
Guido van Rossum60456fd1997-04-09 17:36:32 +00001516 if ((*self->write_func)(self, &global, 1) < 0)
1517 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001518
Guido van Rossum60456fd1997-04-09 17:36:32 +00001519 if ((*self->write_func)(self, module_str, module_size) < 0)
1520 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001521
Guido van Rossum60456fd1997-04-09 17:36:32 +00001522 if ((*self->write_func)(self, "\n", 1) < 0)
1523 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001524
Guido van Rossum60456fd1997-04-09 17:36:32 +00001525 if ((*self->write_func)(self, name_str, name_size) < 0)
1526 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001527
Guido van Rossum60456fd1997-04-09 17:36:32 +00001528 if ((*self->write_func)(self, "\n", 1) < 0)
1529 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001530
Guido van Rossum60456fd1997-04-09 17:36:32 +00001531 if (put(self, args) < 0)
1532 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001533
Guido van Rossum60456fd1997-04-09 17:36:32 +00001534 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001535
Guido van Rossum60456fd1997-04-09 17:36:32 +00001536finally:
1537 Py_XDECREF(module);
1538 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001539
Guido van Rossum60456fd1997-04-09 17:36:32 +00001540 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001541}
1542
Guido van Rossum60456fd1997-04-09 17:36:32 +00001543static int
1544save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1545 PyObject *pid = 0;
1546 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001547
Guido van Rossum60456fd1997-04-09 17:36:32 +00001548 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001549
Guido van Rossum053b8df1998-11-25 16:18:00 +00001550 Py_INCREF(args);
1551 ARG_TUP(self, args);
1552 if (self->arg) {
1553 pid = PyObject_CallObject(f, self->arg);
1554 FREE_ARG_TUP(self);
1555 }
1556 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001557
Guido van Rossum60456fd1997-04-09 17:36:32 +00001558 if (pid != Py_None) {
1559 if (!self->bin) {
1560 if (!PyString_Check(pid)) {
1561 PyErr_SetString(PicklingError,
1562 "persistent id must be string");
1563 goto finally;
1564 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001565
Guido van Rossum60456fd1997-04-09 17:36:32 +00001566 if ((*self->write_func)(self, &persid, 1) < 0)
1567 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001568
Guido van Rossum60456fd1997-04-09 17:36:32 +00001569 if ((size = PyString_Size(pid)) < 0)
1570 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001571
Guido van Rossum60456fd1997-04-09 17:36:32 +00001572 if ((*self->write_func)(self,
1573 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1574 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001575
Guido van Rossum60456fd1997-04-09 17:36:32 +00001576 if ((*self->write_func)(self, "\n", 1) < 0)
1577 goto finally;
1578
1579 res = 1;
1580 goto finally;
1581 }
1582 else if (save(self, pid, 1) >= 0) {
1583 if ((*self->write_func)(self, &binpersid, 1) < 0)
1584 res = -1;
1585 else
1586 res = 1;
1587 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001588
Guido van Rossum60456fd1997-04-09 17:36:32 +00001589 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001590 }
1591
Guido van Rossum60456fd1997-04-09 17:36:32 +00001592 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001593
Guido van Rossum60456fd1997-04-09 17:36:32 +00001594finally:
1595 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001596
Guido van Rossum60456fd1997-04-09 17:36:32 +00001597 return res;
1598}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001599
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001600
Guido van Rossum60456fd1997-04-09 17:36:32 +00001601static int
1602save_reduce(Picklerobject *self, PyObject *callable,
1603 PyObject *tup, PyObject *state, PyObject *ob) {
1604 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001605
Guido van Rossum60456fd1997-04-09 17:36:32 +00001606 if (save(self, callable, 0) < 0)
1607 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001608
Guido van Rossum60456fd1997-04-09 17:36:32 +00001609 if (save(self, tup, 0) < 0)
1610 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001611
Guido van Rossum60456fd1997-04-09 17:36:32 +00001612 if ((*self->write_func)(self, &reduce, 1) < 0)
1613 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001614
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001615 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001616 if (state && !PyDict_Check(state)) {
1617 if (put2(self, ob) < 0)
1618 return -1;
1619 }
1620 else {
1621 if (put(self, ob) < 0)
1622 return -1;
1623 }
1624 }
1625
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001626 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001627 if (save(self, state, 0) < 0)
1628 return -1;
1629
1630 if ((*self->write_func)(self, &build, 1) < 0)
1631 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001632 }
1633
Guido van Rossum60456fd1997-04-09 17:36:32 +00001634 return 0;
1635}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001636
Guido van Rossum60456fd1997-04-09 17:36:32 +00001637static int
1638save(Picklerobject *self, PyObject *args, int pers_save) {
1639 PyTypeObject *type;
1640 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001641 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001642 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001643
Guido van Rossum60456fd1997-04-09 17:36:32 +00001644 if (!pers_save && self->pers_func) {
1645 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1646 res = tmp;
1647 goto finally;
1648 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001649 }
1650
Guido van Rossum60456fd1997-04-09 17:36:32 +00001651 if (args == Py_None) {
1652 res = save_none(self, args);
1653 goto finally;
1654 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001655
Guido van Rossum60456fd1997-04-09 17:36:32 +00001656 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001657
Guido van Rossum60456fd1997-04-09 17:36:32 +00001658 switch (type->tp_name[0]) {
1659 case 'i':
1660 if (type == &PyInt_Type) {
1661 res = save_int(self, args);
1662 goto finally;
1663 }
1664 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001665
Guido van Rossum60456fd1997-04-09 17:36:32 +00001666 case 'l':
1667 if (type == &PyLong_Type) {
1668 res = save_long(self, args);
1669 goto finally;
1670 }
1671 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001672
Guido van Rossum60456fd1997-04-09 17:36:32 +00001673 case 'f':
1674 if (type == &PyFloat_Type) {
1675 res = save_float(self, args);
1676 goto finally;
1677 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001678 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001679
Guido van Rossum60456fd1997-04-09 17:36:32 +00001680 case 't':
1681 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001682 if (self->bin) res = save_empty_tuple(self, args);
1683 else res = save_tuple(self, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001684 goto finally;
1685 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001686 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001687
Guido van Rossum60456fd1997-04-09 17:36:32 +00001688 case 's':
Guido van Rossum053b8df1998-11-25 16:18:00 +00001689 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001690 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001691 goto finally;
1692 }
1693 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001694
Guido van Rossum60456fd1997-04-09 17:36:32 +00001695 if (args->ob_refcnt > 1) {
1696 long ob_id;
1697 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001698
Guido van Rossum60456fd1997-04-09 17:36:32 +00001699 ob_id = (long)args;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001700
Guido van Rossum053b8df1998-11-25 16:18:00 +00001701 UNLESS (py_ob_id = PyInt_FromLong(ob_id))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001702 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001703
Guido van Rossum60456fd1997-04-09 17:36:32 +00001704 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1705 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001706
Guido van Rossum60456fd1997-04-09 17:36:32 +00001707 if (has_key) {
1708 if (get(self, py_ob_id) < 0)
1709 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001710
Guido van Rossum60456fd1997-04-09 17:36:32 +00001711 res = 0;
1712 goto finally;
1713 }
1714 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001715
Guido van Rossum60456fd1997-04-09 17:36:32 +00001716 switch (type->tp_name[0]) {
1717 case 's':
1718 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001719 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001720 goto finally;
1721 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001722 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001723
Guido van Rossum60456fd1997-04-09 17:36:32 +00001724 case 't':
1725 if (type == &PyTuple_Type) {
1726 res = save_tuple(self, args);
1727 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001728 }
1729 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001730
Guido van Rossum60456fd1997-04-09 17:36:32 +00001731 case 'l':
1732 if (type == &PyList_Type) {
1733 res = save_list(self, args);
1734 goto finally;
1735 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001736 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001737
1738 case 'd':
1739 if (type == &PyDict_Type) {
1740 res = save_dict(self, args);
1741 goto finally;
1742 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001743 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001744
1745 case 'i':
1746 if (type == &PyInstance_Type) {
1747 res = save_inst(self, args);
1748 goto finally;
1749 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001750 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001751
1752 case 'c':
1753 if (type == &PyClass_Type) {
1754 res = save_global(self, args, NULL);
1755 goto finally;
1756 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001757 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001758
1759 case 'f':
1760 if (type == &PyFunction_Type) {
1761 res = save_global(self, args, NULL);
1762 goto finally;
1763 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001764 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001765
1766 case 'b':
1767 if (type == &PyCFunction_Type) {
1768 res = save_global(self, args, NULL);
1769 goto finally;
1770 }
1771 }
1772
1773 if (!pers_save && self->inst_pers_func) {
1774 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1775 res = tmp;
1776 goto finally;
1777 }
1778 }
1779
Guido van Rossum142eeb81997-08-13 03:14:41 +00001780 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001781 Py_INCREF(__reduce__);
1782
Guido van Rossum60456fd1997-04-09 17:36:32 +00001783 Py_INCREF(args);
Guido van Rossum053b8df1998-11-25 16:18:00 +00001784 ARG_TUP(self, args);
1785 if (self->arg) {
1786 t = PyObject_CallObject(__reduce__, self->arg);
1787 FREE_ARG_TUP(self);
1788 }
1789 if (! t) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001790 }
1791 else {
1792 PyErr_Clear();
1793
Guido van Rossum142eeb81997-08-13 03:14:41 +00001794 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001795 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001796 goto finally;
1797 }
1798 else {
1799 PyErr_Clear();
1800 }
1801 }
1802
1803 if (t) {
1804 if (PyString_Check(t)) {
1805 res = save_global(self, args, t);
1806 goto finally;
1807 }
1808
1809 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001810 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001811 "be a tuple", "O", __reduce__);
1812 goto finally;
1813 }
1814
1815 size = PyTuple_Size(t);
1816
1817 if ((size != 3) && (size != 2)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001818 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001819 "contain only two or three elements", "O", __reduce__);
1820 goto finally;
1821 }
1822
1823 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001824
Guido van Rossum60456fd1997-04-09 17:36:32 +00001825 arg_tup = PyTuple_GET_ITEM(t, 1);
1826
1827 if (size > 2) {
1828 state = PyTuple_GET_ITEM(t, 2);
1829 }
1830
Guido van Rossum053b8df1998-11-25 16:18:00 +00001831 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001832 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001833 "returned by %s must be a tuple", "O", __reduce__);
1834 goto finally;
1835 }
1836
1837 res = save_reduce(self, callable, arg_tup, state, args);
1838 goto finally;
1839 }
1840
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001841 cPickle_ErrFormat(PicklingError, "Cannot pickle %s objects.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00001842 "O", (PyObject *)type);
1843
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;
2516 int s, e, res = -1;
2517 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;
2632 int res = -1;
2633 char *s;
2634
Guido van Rossum053b8df1998-11-25 16:18:00 +00002635 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002636
2637 l = calc_binint(s, 4);
2638
2639 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002640 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002641
Guido van Rossum053b8df1998-11-25 16:18:00 +00002642 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2643 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002644
Guido van Rossum053b8df1998-11-25 16:18:00 +00002645 PDATA_PUSH(self->stack, py_string, -1);
2646 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002647}
2648
2649
2650static int
2651load_short_binstring(Unpicklerobject *self) {
2652 PyObject *py_string = 0;
2653 unsigned char l;
2654 int res = -1;
2655 char *s;
2656
2657 if ((*self->read_func)(self, &s, 1) < 0)
2658 return -1;
2659
2660 l = (unsigned char)s[0];
2661
Guido van Rossum053b8df1998-11-25 16:18:00 +00002662 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002663
Guido van Rossum053b8df1998-11-25 16:18:00 +00002664 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002665
Guido van Rossum053b8df1998-11-25 16:18:00 +00002666 PDATA_PUSH(self->stack, py_string, -1);
2667 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002668}
2669
2670
2671static int
2672load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002673 PyObject *tup;
2674 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002675
Guido van Rossum053b8df1998-11-25 16:18:00 +00002676 if ((i = marker(self)) < 0) return -1;
2677 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2678 PDATA_PUSH(self->stack, tup, -1);
2679 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002680}
2681
2682static int
2683load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002684 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002685
Guido van Rossum053b8df1998-11-25 16:18:00 +00002686 UNLESS (tup=PyTuple_New(0)) return -1;
2687 PDATA_PUSH(self->stack, tup, -1);
2688 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002689}
2690
2691static int
2692load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002693 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002694
Guido van Rossum053b8df1998-11-25 16:18:00 +00002695 UNLESS (list=PyList_New(0)) return -1;
2696 PDATA_PUSH(self->stack, list, -1);
2697 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002698}
2699
2700static int
2701load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002702 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002703
Guido van Rossum053b8df1998-11-25 16:18:00 +00002704 UNLESS (dict=PyDict_New()) return -1;
2705 PDATA_PUSH(self->stack, dict, -1);
2706 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002707}
2708
2709
2710static int
2711load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002712 PyObject *list = 0;
2713 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002714
Guido van Rossum053b8df1998-11-25 16:18:00 +00002715 if ((i = marker(self)) < 0) return -1;
2716 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2717 PDATA_PUSH(self->stack, list, -1);
2718 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002719}
2720
2721static int
2722load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002723 PyObject *dict, *key, *value;
2724 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002725
Guido van Rossum053b8df1998-11-25 16:18:00 +00002726 if ((i = marker(self)) < 0) return -1;
2727 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002728
Guido van Rossum053b8df1998-11-25 16:18:00 +00002729 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002730
Guido van Rossum053b8df1998-11-25 16:18:00 +00002731 for (k = i+1; k < j; k += 2) {
2732 key =self->stack->data[k-1];
2733 value=self->stack->data[k ];
2734 if (PyDict_SetItem(dict, key, value) < 0) {
2735 Py_DECREF(dict);
2736 return -1;
2737 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002738 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002739 Pdata_clear(self->stack, i);
2740 PDATA_PUSH(self->stack, dict, -1);
2741 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002742}
2743
2744static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002745Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002746 int has_key;
2747 PyObject *safe=0, *r=0;
2748
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002749 if (PyClass_Check(cls)) {
2750 int l;
2751
Guido van Rossum053b8df1998-11-25 16:18:00 +00002752 if ((l=PyObject_Length(args)) < 0) goto err;
2753 UNLESS (l) {
2754 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002755
Guido van Rossum053b8df1998-11-25 16:18:00 +00002756 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2757 /* We have a class with no __getinitargs__, so bypass usual
2758 construction */
2759 PyInstanceObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002760
Guido van Rossum053b8df1998-11-25 16:18:00 +00002761 PyErr_Clear();
2762 UNLESS (inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
2763 goto err;
2764 inst->in_class=(PyClassObject*)cls;
2765 Py_INCREF(cls);
2766 UNLESS (inst->in_dict=PyDict_New()) {
2767 Py_DECREF(inst);
2768 goto err;
2769 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002770
Guido van Rossum053b8df1998-11-25 16:18:00 +00002771 return (PyObject *)inst;
2772 }
2773 Py_DECREF(__getinitargs__);
2774 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002775
Guido van Rossum053b8df1998-11-25 16:18:00 +00002776 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002777 else goto err;
2778 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002779
2780
2781 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2782 goto err;
2783
2784 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002785 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002786 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002787 cPickle_ErrFormat(UnpicklingError,
2788 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002789 Py_XDECREF(safe);
2790 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002791 }
2792
Guido van Rossum053b8df1998-11-25 16:18:00 +00002793 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002794 /* Special case, call cls.__basicnew__() */
2795 PyObject *basicnew;
2796
Guido van Rossum053b8df1998-11-25 16:18:00 +00002797 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002798 r=PyObject_CallObject(basicnew, NULL);
2799 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002800 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002801 }
2802
Guido van Rossum053b8df1998-11-25 16:18:00 +00002803 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002804
Guido van Rossum60456fd1997-04-09 17:36:32 +00002805err:
2806 {
2807 PyObject *tp, *v, *tb;
2808
2809 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002810 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2811 Py_XDECREF(v);
2812 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002813 }
2814 PyErr_Restore(tp,v,tb);
2815 }
2816 return NULL;
2817}
2818
2819
2820static int
2821load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002822 PyObject *class, *tup, *obj=0;
2823 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002824
Guido van Rossum053b8df1998-11-25 16:18:00 +00002825 if ((i = marker(self)) < 0) return -1;
2826 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
2827 PDATA_POP(self->stack, class);
2828 if (class) {
2829 obj = Instance_New(class, tup);
2830 Py_DECREF(class);
2831 }
2832 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002833
Guido van Rossum053b8df1998-11-25 16:18:00 +00002834 if (! obj) return -1;
2835 PDATA_PUSH(self->stack, obj, -1);
2836 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002837}
2838
2839
2840static int
2841load_inst(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002842 PyObject *tup, *class, *obj, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002843 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002844 char *s;
2845
Guido van Rossum053b8df1998-11-25 16:18:00 +00002846 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002847
Guido van Rossum053b8df1998-11-25 16:18:00 +00002848 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002849 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002850 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002851
Guido van Rossum053b8df1998-11-25 16:18:00 +00002852 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002853 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002854 if (class_name = PyString_FromStringAndSize(s, len - 1)) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002855 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002856 Py_DECREF(class_name);
2857 }
2858 }
2859 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002860
Guido van Rossum053b8df1998-11-25 16:18:00 +00002861 if (! class) return -1;
2862
2863 if (tup=Pdata_popTuple(self->stack, i)) {
2864 obj = Instance_New(class, tup);
2865 Py_DECREF(tup);
2866 }
2867 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002868
Guido van Rossum053b8df1998-11-25 16:18:00 +00002869 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002870
Guido van Rossum053b8df1998-11-25 16:18:00 +00002871 PDATA_PUSH(self->stack, obj, -1);
2872 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002873}
2874
2875
2876static int
2877load_global(Unpicklerobject *self) {
2878 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002879 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002880 char *s;
2881
Guido van Rossum053b8df1998-11-25 16:18:00 +00002882 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002883 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002884 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002885
Guido van Rossum053b8df1998-11-25 16:18:00 +00002886 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002887 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002888 if (class_name = PyString_FromStringAndSize(s, len - 1)) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002889 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002890 Py_DECREF(class_name);
2891 }
2892 }
2893 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002894
Guido van Rossum053b8df1998-11-25 16:18:00 +00002895 if (! class) return -1;
2896 PDATA_PUSH(self->stack, class, -1);
2897 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002898}
2899
2900
2901static int
2902load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002903 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002904 int len, res = -1;
2905 char *s;
2906
2907 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002908 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002909 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00002910
Guido van Rossum053b8df1998-11-25 16:18:00 +00002911 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002912
Guido van Rossum053b8df1998-11-25 16:18:00 +00002913 if (PyList_Check(self->pers_func)) {
2914 if (PyList_Append(self->pers_func, pid) < 0) {
2915 Py_DECREF(pid);
2916 return -1;
2917 }
2918 }
2919 else {
2920 ARG_TUP(self, pid);
2921 if (self->arg) {
2922 pid = PyObject_CallObject(self->pers_func, self->arg);
2923 FREE_ARG_TUP(self);
2924 }
2925 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002926
Guido van Rossum053b8df1998-11-25 16:18:00 +00002927 if (! pid) return -1;
2928
2929 PDATA_PUSH(self->stack, pid, -1);
2930 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002931 }
2932 else {
2933 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002934 "A load persistent id instruction was encountered,\n"
2935 "but no persistent_load function was specified.");
2936 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002937 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002938}
2939
Guido van Rossum60456fd1997-04-09 17:36:32 +00002940static int
2941load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002942 PyObject *pid = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002943 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002944
2945 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002946 PDATA_POP(self->stack, pid);
2947 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002948
Guido van Rossum053b8df1998-11-25 16:18:00 +00002949 if (PyList_Check(self->pers_func)) {
2950 if (PyList_Append(self->pers_func, pid) < 0) {
2951 Py_DECREF(pid);
2952 return -1;
2953 }
2954 }
2955 else {
2956 ARG_TUP(self, pid);
2957 if (self->arg) {
2958 pid = PyObject_CallObject(self->pers_func, self->arg);
2959 FREE_ARG_TUP(self);
2960 }
2961 if (! pid) return -1;
2962 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002963
Guido van Rossum053b8df1998-11-25 16:18:00 +00002964 PDATA_PUSH(self->stack, pid, -1);
2965 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002966 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002967 else {
2968 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002969 "A load persistent id instruction was encountered,\n"
2970 "but no persistent_load function was specified.");
2971 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002972 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002973}
2974
2975
2976static int
2977load_pop(Unpicklerobject *self) {
2978 int len;
2979
Guido van Rossum053b8df1998-11-25 16:18:00 +00002980 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00002981
2982 if ((self->num_marks > 0) &&
2983 (self->marks[self->num_marks - 1] == len))
2984 self->num_marks--;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002985 else
2986 Py_DECREF(self->stack->data[--(self->stack->length)]);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002987
2988 return 0;
2989}
2990
2991
2992static int
2993load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002994 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002995
2996 if ((i = marker(self)) < 0)
2997 return -1;
2998
Guido van Rossum053b8df1998-11-25 16:18:00 +00002999 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003000
3001 return 0;
3002}
3003
3004
3005static int
3006load_dup(Unpicklerobject *self) {
3007 PyObject *last;
3008 int len;
3009
Guido van Rossum053b8df1998-11-25 16:18:00 +00003010 if ((len = self->stack->length) <= 0) return stackUnderflow();
3011 last=self->stack->data[len-1];
3012 Py_INCREF(last);
3013 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003014 return 0;
3015}
3016
3017
3018static int
3019load_get(Unpicklerobject *self) {
3020 PyObject *py_str = 0, *value = 0;
3021 int len, res = -1;
3022 char *s;
3023
Guido van Rossum053b8df1998-11-25 16:18:00 +00003024 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003025 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003026
Guido van Rossum053b8df1998-11-25 16:18:00 +00003027 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3028
3029 value = PyDict_GetItem(self->memo, py_str);
3030 Py_DECREF(py_str);
3031 if (! value) {
3032 PyErr_SetObject(BadPickleGet, py_str);
3033 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00003034 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003035
Guido van Rossum053b8df1998-11-25 16:18:00 +00003036 PDATA_APPEND(self->stack, value, -1);
3037 return 0;
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;
3045 int res = -1;
3046 char *s;
3047
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);
3054 Py_DECREF(py_key);
3055 if (! value) {
3056 PyErr_SetObject(BadPickleGet, py_key);
3057 return -1;
3058 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003059
Guido van Rossum053b8df1998-11-25 16:18:00 +00003060 PDATA_APPEND(self->stack, value, -1);
3061 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003062}
3063
3064
3065static int
3066load_long_binget(Unpicklerobject *self) {
3067 PyObject *py_key = 0, *value = 0;
3068 unsigned char c, *s;
3069 long key;
3070 int res = -1;
3071
Guido van Rossum053b8df1998-11-25 16:18:00 +00003072 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003073
3074 c = (unsigned char)s[0];
3075 key = (long)c;
3076 c = (unsigned char)s[1];
3077 key |= (long)c << 8;
3078 c = (unsigned char)s[2];
3079 key |= (long)c << 16;
3080 c = (unsigned char)s[3];
3081 key |= (long)c << 24;
3082
Guido van Rossum053b8df1998-11-25 16:18:00 +00003083 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3084
3085 value = PyDict_GetItem(self->memo, py_key);
3086 Py_DECREF(py_key);
3087 if (! value) {
3088 PyErr_SetObject(BadPickleGet, py_key);
3089 return -1;
3090 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003091
Guido van Rossum053b8df1998-11-25 16:18:00 +00003092 PDATA_APPEND(self->stack, value, -1);
3093 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003094}
3095
3096
3097static int
3098load_put(Unpicklerobject *self) {
3099 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003100 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003101 char *s;
3102
Guido van Rossum053b8df1998-11-25 16:18:00 +00003103 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003104 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003105 UNLESS (len=self->stack->length) return stackUnderflow();
3106 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3107 value=self->stack->data[len-1];
3108 l=PyDict_SetItem(self->memo, py_str, value);
3109 Py_DECREF(py_str);
3110 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003111}
3112
3113
3114static int
3115load_binput(Unpicklerobject *self) {
3116 PyObject *py_key = 0, *value = 0;
3117 unsigned char key, *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003118 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003119
Guido van Rossum053b8df1998-11-25 16:18:00 +00003120 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3121 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003122
3123 key = (unsigned char)s[0];
3124
Guido van Rossum053b8df1998-11-25 16:18:00 +00003125 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3126 value=self->stack->data[len-1];
3127 len=PyDict_SetItem(self->memo, py_key, value);
3128 Py_DECREF(py_key);
3129 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003130}
3131
3132
3133static int
3134load_long_binput(Unpicklerobject *self) {
3135 PyObject *py_key = 0, *value = 0;
3136 long key;
3137 unsigned char c, *s;
3138 int len, res = -1;
3139
Guido van Rossum053b8df1998-11-25 16:18:00 +00003140 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3141 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003142
3143 c = (unsigned char)s[0];
3144 key = (long)c;
3145 c = (unsigned char)s[1];
3146 key |= (long)c << 8;
3147 c = (unsigned char)s[2];
3148 key |= (long)c << 16;
3149 c = (unsigned char)s[3];
3150 key |= (long)c << 24;
3151
Guido van Rossum053b8df1998-11-25 16:18:00 +00003152 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3153 value=self->stack->data[len-1];
3154 len=PyDict_SetItem(self->memo, py_key, value);
3155 Py_DECREF(py_key);
3156 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003157}
3158
3159
3160static int
3161do_append(Unpicklerobject *self, int x) {
3162 PyObject *value = 0, *list = 0, *append_method = 0;
3163 int len, i;
3164
Guido van Rossum053b8df1998-11-25 16:18:00 +00003165 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3166 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003167
Guido van Rossum053b8df1998-11-25 16:18:00 +00003168 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003169
3170 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003171 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003172 int list_len;
3173
Guido van Rossum053b8df1998-11-25 16:18:00 +00003174 slice=Pdata_popList(self->stack, x);
3175 list_len = PyList_GET_SIZE(list);
3176 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003177 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003178 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003179 }
3180 else {
3181
Guido van Rossum053b8df1998-11-25 16:18:00 +00003182 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003183 return -1;
3184
3185 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003186 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003187
Guido van Rossum053b8df1998-11-25 16:18:00 +00003188 value=self->stack->data[i];
3189 junk=0;
3190 ARG_TUP(self, value);
3191 if (self->arg) {
3192 junk = PyObject_CallObject(append_method, self->arg);
3193 FREE_ARG_TUP(self);
3194 }
3195 if (! junk) {
3196 Pdata_clear(self->stack, i+1);
3197 self->stack->length=x;
3198 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003199 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003200 }
3201 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003202 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003203 self->stack->length=x;
3204 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003205 }
3206
Guido van Rossum60456fd1997-04-09 17:36:32 +00003207 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003208}
3209
3210
3211static int
3212load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003213 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003214}
3215
3216
3217static int
3218load_appends(Unpicklerobject *self) {
3219 return do_append(self, marker(self));
3220}
3221
3222
3223static int
3224do_setitems(Unpicklerobject *self, int x) {
3225 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003226 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003227
Guido van Rossum053b8df1998-11-25 16:18:00 +00003228 UNLESS ((len=self->stack->length) >= x
3229 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003230
Guido van Rossum053b8df1998-11-25 16:18:00 +00003231 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003232
Guido van Rossum053b8df1998-11-25 16:18:00 +00003233 for (i = x+1; i < len; i += 2) {
3234 key =self->stack->data[i-1];
3235 value=self->stack->data[i ];
3236 if (PyObject_SetItem(dict, key, value) < 0) {
3237 r=-1;
3238 break;
3239 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003240 }
3241
Guido van Rossum053b8df1998-11-25 16:18:00 +00003242 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003243
Guido van Rossum053b8df1998-11-25 16:18:00 +00003244 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003245}
3246
3247
3248static int
3249load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003250 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003251}
3252
Guido van Rossum60456fd1997-04-09 17:36:32 +00003253static int
3254load_setitems(Unpicklerobject *self) {
3255 return do_setitems(self, marker(self));
3256}
3257
3258
3259static int
3260load_build(Unpicklerobject *self) {
3261 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3262 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003263 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003264
Guido van Rossum053b8df1998-11-25 16:18:00 +00003265 if (self->stack->length < 2) return stackUnderflow();
3266 PDATA_POP(self->stack, value);
3267 if (! value) return -1;
3268 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003269
Guido van Rossum053b8df1998-11-25 16:18:00 +00003270 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3271 ARG_TUP(self, value);
3272 if (self->arg) {
3273 junk = PyObject_CallObject(__setstate__, self->arg);
3274 FREE_ARG_TUP(self);
3275 }
3276 Py_DECREF(__setstate__);
3277 if (! junk) return -1;
3278 Py_DECREF(junk);
3279 return 0;
3280 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003281
Guido van Rossum053b8df1998-11-25 16:18:00 +00003282 PyErr_Clear();
3283 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003284 i = 0;
3285 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003286 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3287 r=-1;
3288 break;
3289 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003290 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003291 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003292 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003293 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003294
Guido van Rossum053b8df1998-11-25 16:18:00 +00003295 Py_XDECREF(value);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003296
Guido van Rossum053b8df1998-11-25 16:18:00 +00003297 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003298}
3299
3300
3301static int
3302load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003303 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003304
Guido van Rossum053b8df1998-11-25 16:18:00 +00003305 if ((self->num_marks + 1) >= self->marks_size) {
3306 s=self->marks_size+20;
3307 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003308 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003309 self->marks=(int *)malloc(s * sizeof(int));
3310 else
3311 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003312 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003313 PyErr_NoMemory();
3314 return -1;
3315 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003316 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003317 }
3318
Guido van Rossum053b8df1998-11-25 16:18:00 +00003319 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003320
3321 return 0;
3322}
3323
3324static int
3325load_reduce(Unpicklerobject *self) {
3326 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003327
Guido van Rossum053b8df1998-11-25 16:18:00 +00003328 PDATA_POP(self->stack, arg_tup);
3329 if (! arg_tup) return -1;
3330 PDATA_POP(self->stack, callable);
3331 if (callable) {
3332 ob = Instance_New(callable, arg_tup);
3333 Py_DECREF(callable);
3334 }
3335 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003336
Guido van Rossum053b8df1998-11-25 16:18:00 +00003337 if (! ob) return -1;
3338
3339 PDATA_PUSH(self->stack, ob, -1);
3340 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003341}
3342
3343static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003344load(Unpicklerobject *self) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00003345 PyObject *stack = 0, *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003346 char *s;
3347
Guido van Rossum60456fd1997-04-09 17:36:32 +00003348 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003349 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003350
3351 while (1) {
3352 if ((*self->read_func)(self, &s, 1) < 0)
3353 break;
3354
3355 switch (s[0]) {
3356 case NONE:
3357 if (load_none(self) < 0)
3358 break;
3359 continue;
3360
3361 case BININT:
3362 if (load_binint(self) < 0)
3363 break;
3364 continue;
3365
3366 case BININT1:
3367 if (load_binint1(self) < 0)
3368 break;
3369 continue;
3370
3371 case BININT2:
3372 if (load_binint2(self) < 0)
3373 break;
3374 continue;
3375
3376 case INT:
3377 if (load_int(self) < 0)
3378 break;
3379 continue;
3380
3381 case LONG:
3382 if (load_long(self) < 0)
3383 break;
3384 continue;
3385
3386 case FLOAT:
3387 if (load_float(self) < 0)
3388 break;
3389 continue;
3390
Guido van Rossum60456fd1997-04-09 17:36:32 +00003391 case BINFLOAT:
3392 if (load_binfloat(self) < 0)
3393 break;
3394 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003395
3396 case BINSTRING:
3397 if (load_binstring(self) < 0)
3398 break;
3399 continue;
3400
3401 case SHORT_BINSTRING:
3402 if (load_short_binstring(self) < 0)
3403 break;
3404 continue;
3405
3406 case STRING:
3407 if (load_string(self) < 0)
3408 break;
3409 continue;
3410
3411 case EMPTY_TUPLE:
3412 if (load_empty_tuple(self) < 0)
3413 break;
3414 continue;
3415
3416 case TUPLE:
3417 if (load_tuple(self) < 0)
3418 break;
3419 continue;
3420
3421 case EMPTY_LIST:
3422 if (load_empty_list(self) < 0)
3423 break;
3424 continue;
3425
3426 case LIST:
3427 if (load_list(self) < 0)
3428 break;
3429 continue;
3430
3431 case EMPTY_DICT:
3432 if (load_empty_dict(self) < 0)
3433 break;
3434 continue;
3435
3436 case DICT:
3437 if (load_dict(self) < 0)
3438 break;
3439 continue;
3440
3441 case OBJ:
3442 if (load_obj(self) < 0)
3443 break;
3444 continue;
3445
3446 case INST:
3447 if (load_inst(self) < 0)
3448 break;
3449 continue;
3450
3451 case GLOBAL:
3452 if (load_global(self) < 0)
3453 break;
3454 continue;
3455
3456 case APPEND:
3457 if (load_append(self) < 0)
3458 break;
3459 continue;
3460
3461 case APPENDS:
3462 if (load_appends(self) < 0)
3463 break;
3464 continue;
3465
3466 case BUILD:
3467 if (load_build(self) < 0)
3468 break;
3469 continue;
3470
3471 case DUP:
3472 if (load_dup(self) < 0)
3473 break;
3474 continue;
3475
3476 case BINGET:
3477 if (load_binget(self) < 0)
3478 break;
3479 continue;
3480
3481 case LONG_BINGET:
3482 if (load_long_binget(self) < 0)
3483 break;
3484 continue;
3485
3486 case GET:
3487 if (load_get(self) < 0)
3488 break;
3489 continue;
3490
3491 case MARK:
3492 if (load_mark(self) < 0)
3493 break;
3494 continue;
3495
3496 case BINPUT:
3497 if (load_binput(self) < 0)
3498 break;
3499 continue;
3500
3501 case LONG_BINPUT:
3502 if (load_long_binput(self) < 0)
3503 break;
3504 continue;
3505
3506 case PUT:
3507 if (load_put(self) < 0)
3508 break;
3509 continue;
3510
3511 case POP:
3512 if (load_pop(self) < 0)
3513 break;
3514 continue;
3515
3516 case POP_MARK:
3517 if (load_pop_mark(self) < 0)
3518 break;
3519 continue;
3520
3521 case SETITEM:
3522 if (load_setitem(self) < 0)
3523 break;
3524 continue;
3525
3526 case SETITEMS:
3527 if (load_setitems(self) < 0)
3528 break;
3529 continue;
3530
3531 case STOP:
3532 break;
3533
3534 case PERSID:
3535 if (load_persid(self) < 0)
3536 break;
3537 continue;
3538
3539 case BINPERSID:
3540 if (load_binpersid(self) < 0)
3541 break;
3542 continue;
3543
3544 case REDUCE:
3545 if (load_reduce(self) < 0)
3546 break;
3547 continue;
3548
3549 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003550 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003551 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003552 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003553 }
3554
3555 break;
3556 }
3557
Guido van Rossum053b8df1998-11-25 16:18:00 +00003558 if ((err = PyErr_Occurred())) {
3559 if (err == PyExc_EOFError) {
3560 PyErr_SetNone(PyExc_EOFError);
3561 }
3562 return NULL;
3563 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003564
Guido van Rossum053b8df1998-11-25 16:18:00 +00003565 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003566 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003567}
3568
3569
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003570/* No-load functions to support noload, which is used to
3571 find persistent references. */
3572
3573static int
3574noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003575 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003576
3577 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003578 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003579}
3580
3581
3582static int
3583noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003584 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003585 char *s;
3586
3587 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003588 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003589 if ((*self->readline_func)(self, &s) < 0) return -1;
3590 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003591 PDATA_APPEND(self->stack, Py_None,-1);
3592 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003593}
3594
3595static int
3596noload_global(Unpicklerobject *self) {
3597 char *s;
3598
3599 if ((*self->readline_func)(self, &s) < 0) return -1;
3600 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003601 PDATA_APPEND(self->stack, Py_None,-1);
3602 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003603}
3604
3605static int
3606noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003607
Guido van Rossum053b8df1998-11-25 16:18:00 +00003608 if (self->stack->length < 2) return stackUnderflow();
3609 Pdata_clear(self->stack, self->stack->length-2);
3610 PDATA_APPEND(self->stack, Py_None,-1);
3611 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003612}
3613
3614static int
3615noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003616
Guido van Rossum053b8df1998-11-25 16:18:00 +00003617 if (self->stack->length < 1) return stackUnderflow();
3618 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003619 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003620}
3621
3622
3623static PyObject *
3624noload(Unpicklerobject *self) {
3625 PyObject *stack = 0, *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003626 char *s;
3627
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003628 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003629 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003630
3631 while (1) {
3632 if ((*self->read_func)(self, &s, 1) < 0)
3633 break;
3634
3635 switch (s[0]) {
3636 case NONE:
3637 if (load_none(self) < 0)
3638 break;
3639 continue;
3640
3641 case BININT:
3642 if (load_binint(self) < 0)
3643 break;
3644 continue;
3645
3646 case BININT1:
3647 if (load_binint1(self) < 0)
3648 break;
3649 continue;
3650
3651 case BININT2:
3652 if (load_binint2(self) < 0)
3653 break;
3654 continue;
3655
3656 case INT:
3657 if (load_int(self) < 0)
3658 break;
3659 continue;
3660
3661 case LONG:
3662 if (load_long(self) < 0)
3663 break;
3664 continue;
3665
3666 case FLOAT:
3667 if (load_float(self) < 0)
3668 break;
3669 continue;
3670
3671 case BINFLOAT:
3672 if (load_binfloat(self) < 0)
3673 break;
3674 continue;
3675
3676 case BINSTRING:
3677 if (load_binstring(self) < 0)
3678 break;
3679 continue;
3680
3681 case SHORT_BINSTRING:
3682 if (load_short_binstring(self) < 0)
3683 break;
3684 continue;
3685
3686 case STRING:
3687 if (load_string(self) < 0)
3688 break;
3689 continue;
3690
3691 case EMPTY_TUPLE:
3692 if (load_empty_tuple(self) < 0)
3693 break;
3694 continue;
3695
3696 case TUPLE:
3697 if (load_tuple(self) < 0)
3698 break;
3699 continue;
3700
3701 case EMPTY_LIST:
3702 if (load_empty_list(self) < 0)
3703 break;
3704 continue;
3705
3706 case LIST:
3707 if (load_list(self) < 0)
3708 break;
3709 continue;
3710
3711 case EMPTY_DICT:
3712 if (load_empty_dict(self) < 0)
3713 break;
3714 continue;
3715
3716 case DICT:
3717 if (load_dict(self) < 0)
3718 break;
3719 continue;
3720
3721 case OBJ:
3722 if (noload_obj(self) < 0)
3723 break;
3724 continue;
3725
3726 case INST:
3727 if (noload_inst(self) < 0)
3728 break;
3729 continue;
3730
3731 case GLOBAL:
3732 if (noload_global(self) < 0)
3733 break;
3734 continue;
3735
3736 case APPEND:
3737 if (load_append(self) < 0)
3738 break;
3739 continue;
3740
3741 case APPENDS:
3742 if (load_appends(self) < 0)
3743 break;
3744 continue;
3745
3746 case BUILD:
3747 if (noload_build(self) < 0)
3748 break;
3749 continue;
3750
3751 case DUP:
3752 if (load_dup(self) < 0)
3753 break;
3754 continue;
3755
3756 case BINGET:
3757 if (load_binget(self) < 0)
3758 break;
3759 continue;
3760
3761 case LONG_BINGET:
3762 if (load_long_binget(self) < 0)
3763 break;
3764 continue;
3765
3766 case GET:
3767 if (load_get(self) < 0)
3768 break;
3769 continue;
3770
3771 case MARK:
3772 if (load_mark(self) < 0)
3773 break;
3774 continue;
3775
3776 case BINPUT:
3777 if (load_binput(self) < 0)
3778 break;
3779 continue;
3780
3781 case LONG_BINPUT:
3782 if (load_long_binput(self) < 0)
3783 break;
3784 continue;
3785
3786 case PUT:
3787 if (load_put(self) < 0)
3788 break;
3789 continue;
3790
3791 case POP:
3792 if (load_pop(self) < 0)
3793 break;
3794 continue;
3795
3796 case POP_MARK:
3797 if (load_pop_mark(self) < 0)
3798 break;
3799 continue;
3800
3801 case SETITEM:
3802 if (load_setitem(self) < 0)
3803 break;
3804 continue;
3805
3806 case SETITEMS:
3807 if (load_setitems(self) < 0)
3808 break;
3809 continue;
3810
3811 case STOP:
3812 break;
3813
3814 case PERSID:
3815 if (load_persid(self) < 0)
3816 break;
3817 continue;
3818
3819 case BINPERSID:
3820 if (load_binpersid(self) < 0)
3821 break;
3822 continue;
3823
3824 case REDUCE:
3825 if (noload_reduce(self) < 0)
3826 break;
3827 continue;
3828
3829 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003830 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003831 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003832 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003833 }
3834
3835 break;
3836 }
3837
Guido van Rossum053b8df1998-11-25 16:18:00 +00003838 if ((err = PyErr_Occurred())) {
3839 if (err == PyExc_EOFError) {
3840 PyErr_SetNone(PyExc_EOFError);
3841 }
3842 return NULL;
3843 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003844
Guido van Rossum053b8df1998-11-25 16:18:00 +00003845 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003846 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003847}
3848
3849
Guido van Rossum60456fd1997-04-09 17:36:32 +00003850static PyObject *
3851Unpickler_load(Unpicklerobject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003852 UNLESS (PyArg_ParseTuple(args, ""))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003853 return NULL;
3854
3855 return load(self);
3856}
3857
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003858static PyObject *
3859Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003860 UNLESS (PyArg_ParseTuple(args, ""))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003861 return NULL;
3862
3863 return noload(self);
3864}
3865
Guido van Rossum60456fd1997-04-09 17:36:32 +00003866
3867static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003868 {"load", (PyCFunction)Unpickler_load, 1,
3869 "load() -- Load a pickle"
3870 },
3871 {"noload", (PyCFunction)Unpickler_noload, 1,
3872 "noload() -- not load a pickle, but go through most of the motions\n"
3873 "\n"
3874 "This function can be used to read past a pickle without instantiating\n"
3875 "any objects or importing any modules. It can also be used to find all\n"
3876 "persistent references without instantiating any objects or importing\n"
3877 "any modules.\n"
3878 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00003879 {NULL, NULL} /* sentinel */
3880};
3881
3882
3883static Unpicklerobject *
3884newUnpicklerobject(PyObject *f) {
3885 Unpicklerobject *self;
3886
Guido van Rossum053b8df1998-11-25 16:18:00 +00003887 UNLESS (self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003888 return NULL;
3889
3890 self->file = NULL;
3891 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003892 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003893 self->pers_func = NULL;
3894 self->last_string = NULL;
3895 self->marks = NULL;
3896 self->num_marks = 0;
3897 self->marks_size = 0;
3898 self->buf_size = 0;
3899 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00003900 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00003901 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003902 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003903
Guido van Rossum053b8df1998-11-25 16:18:00 +00003904 UNLESS (self->memo = PyDict_New()) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003905 Py_XDECREF((PyObject *)self);
3906 return NULL;
3907 }
3908
3909 Py_INCREF(f);
3910 self->file = f;
3911
3912 /* Set read, readline based on type of f */
3913 if (PyFile_Check(f)) {
3914 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00003915 if (self->fp == NULL) {
3916 PyErr_SetString(PyExc_IOError, "input file closed");
3917 return NULL;
3918 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003919 self->read_func = read_file;
3920 self->readline_func = readline_file;
3921 }
3922 else if (PycStringIO_InputCheck(f)) {
3923 self->fp = NULL;
3924 self->read_func = read_cStringIO;
3925 self->readline_func = readline_cStringIO;
3926 }
3927 else {
3928
3929 self->fp = NULL;
3930 self->read_func = read_other;
3931 self->readline_func = readline_other;
3932
Guido van Rossum053b8df1998-11-25 16:18:00 +00003933 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003934 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003935 PyErr_Clear();
3936 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
3937 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00003938 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003939 }
3940 }
3941
Guido van Rossum053b8df1998-11-25 16:18:00 +00003942 if (PyEval_GetRestricted()) {
3943 /* Restricted execution, get private tables */
3944 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003945
Guido van Rossum053b8df1998-11-25 16:18:00 +00003946 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
3947 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
3948 Py_DECREF(m);
3949 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003950 }
3951 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003952 self->safe_constructors=safe_constructors;
3953 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003954 }
3955
Guido van Rossum60456fd1997-04-09 17:36:32 +00003956 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003957
3958err:
3959 Py_DECREF((PyObject *)self);
3960 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003961}
3962
3963
3964static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003965get_Unpickler(PyObject *self, PyObject *args) {
3966 PyObject *file;
3967
Guido van Rossum053b8df1998-11-25 16:18:00 +00003968 UNLESS (PyArg_ParseTuple(args, "O", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003969 return NULL;
3970 return (PyObject *)newUnpicklerobject(file);
3971}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003972
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003973
Guido van Rossum60456fd1997-04-09 17:36:32 +00003974static void
3975Unpickler_dealloc(Unpicklerobject *self) {
3976 Py_XDECREF(self->readline);
3977 Py_XDECREF(self->read);
3978 Py_XDECREF(self->file);
3979 Py_XDECREF(self->memo);
3980 Py_XDECREF(self->stack);
3981 Py_XDECREF(self->pers_func);
3982 Py_XDECREF(self->arg);
3983 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003984 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003985
Guido van Rossum60456fd1997-04-09 17:36:32 +00003986 if (self->marks) {
3987 free(self->marks);
3988 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003989
Guido van Rossum60456fd1997-04-09 17:36:32 +00003990 if (self->buf_size) {
3991 free(self->buf);
3992 }
3993
3994 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003995}
3996
3997
3998static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003999Unpickler_getattr(Unpicklerobject *self, char *name) {
4000 if (!strcmp(name, "persistent_load")) {
4001 if (!self->pers_func) {
4002 PyErr_SetString(PyExc_AttributeError, name);
4003 return NULL;
4004 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004005
Guido van Rossum60456fd1997-04-09 17:36:32 +00004006 Py_INCREF(self->pers_func);
4007 return self->pers_func;
4008 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004009
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004010 if (!strcmp(name, "find_global")) {
4011 if (!self->find_class) {
4012 PyErr_SetString(PyExc_AttributeError, name);
4013 return NULL;
4014 }
4015
4016 Py_INCREF(self->find_class);
4017 return self->find_class;
4018 }
4019
Guido van Rossum60456fd1997-04-09 17:36:32 +00004020 if (!strcmp(name, "memo")) {
4021 if (!self->memo) {
4022 PyErr_SetString(PyExc_AttributeError, name);
4023 return NULL;
4024 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004025
Guido van Rossum60456fd1997-04-09 17:36:32 +00004026 Py_INCREF(self->memo);
4027 return self->memo;
4028 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004029
Guido van Rossum60456fd1997-04-09 17:36:32 +00004030 if (!strcmp(name, "UnpicklingError")) {
4031 Py_INCREF(UnpicklingError);
4032 return UnpicklingError;
4033 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004034
Guido van Rossum60456fd1997-04-09 17:36:32 +00004035 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4036}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004037
Guido van Rossum60456fd1997-04-09 17:36:32 +00004038
4039static int
4040Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004041
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004042 if (!strcmp(name, "persistent_load")) {
4043 Py_XDECREF(self->pers_func);
4044 self->pers_func = value;
4045 Py_XINCREF(value);
4046 return 0;
4047 }
4048
4049 if (!strcmp(name, "find_global")) {
4050 Py_XDECREF(self->find_class);
4051 self->find_class = value;
4052 Py_XINCREF(value);
4053 return 0;
4054 }
4055
Guido van Rossum053b8df1998-11-25 16:18:00 +00004056 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004057 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004058 "attribute deletion is not supported");
4059 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004060 }
4061
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004062 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004063 if (! PyDict_Check(value)) {
4064 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4065 return -1;
4066 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004067 Py_XDECREF(self->memo);
4068 self->memo = value;
4069 Py_INCREF(value);
4070 return 0;
4071 }
4072
Guido van Rossum60456fd1997-04-09 17:36:32 +00004073 PyErr_SetString(PyExc_AttributeError, name);
4074 return -1;
4075}
4076
4077
4078static PyObject *
4079cpm_dump(PyObject *self, PyObject *args) {
4080 PyObject *ob, *file, *res = NULL;
4081 Picklerobject *pickler = 0;
4082 int bin = 0;
4083
Guido van Rossum053b8df1998-11-25 16:18:00 +00004084 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004085 goto finally;
4086
Guido van Rossum053b8df1998-11-25 16:18:00 +00004087 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004088 goto finally;
4089
4090 if (dump(pickler, ob) < 0)
4091 goto finally;
4092
4093 Py_INCREF(Py_None);
4094 res = Py_None;
4095
4096finally:
4097 Py_XDECREF(pickler);
4098
4099 return res;
4100}
4101
4102
4103static PyObject *
4104cpm_dumps(PyObject *self, PyObject *args) {
4105 PyObject *ob, *file = 0, *res = NULL;
4106 Picklerobject *pickler = 0;
4107 int bin = 0;
4108
Guido van Rossum053b8df1998-11-25 16:18:00 +00004109 UNLESS (PyArg_ParseTuple(args, "O|i", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004110 goto finally;
4111
Guido van Rossum053b8df1998-11-25 16:18:00 +00004112 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004113 goto finally;
4114
Guido van Rossum053b8df1998-11-25 16:18:00 +00004115 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004116 goto finally;
4117
4118 if (dump(pickler, ob) < 0)
4119 goto finally;
4120
4121 res = PycStringIO->cgetvalue(file);
4122
4123finally:
4124 Py_XDECREF(pickler);
4125 Py_XDECREF(file);
4126
4127 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004128}
4129
4130
4131static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004132cpm_load(PyObject *self, PyObject *args) {
4133 Unpicklerobject *unpickler = 0;
4134 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004135
Guido van Rossum053b8df1998-11-25 16:18:00 +00004136 UNLESS (PyArg_ParseTuple(args, "O", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004137 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004138
Guido van Rossum053b8df1998-11-25 16:18:00 +00004139 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004140 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004141
Guido van Rossum60456fd1997-04-09 17:36:32 +00004142 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004143
Guido van Rossum60456fd1997-04-09 17:36:32 +00004144finally:
4145 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004146
Guido van Rossum60456fd1997-04-09 17:36:32 +00004147 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004148}
4149
4150
4151static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004152cpm_loads(PyObject *self, PyObject *args) {
4153 PyObject *ob, *file = 0, *res = NULL;
4154 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004155
Guido van Rossum053b8df1998-11-25 16:18:00 +00004156 UNLESS (PyArg_ParseTuple(args, "S", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004157 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004158
Guido van Rossum053b8df1998-11-25 16:18:00 +00004159 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004160 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004161
Guido van Rossum053b8df1998-11-25 16:18:00 +00004162 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004163 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004164
Guido van Rossum60456fd1997-04-09 17:36:32 +00004165 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004166
Guido van Rossum60456fd1997-04-09 17:36:32 +00004167finally:
4168 Py_XDECREF(file);
4169 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004170
Guido van Rossum60456fd1997-04-09 17:36:32 +00004171 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004172}
4173
4174
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004175static char Unpicklertype__doc__[] =
4176"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004177
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004178static PyTypeObject Unpicklertype = {
4179 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004180 0, /*ob_size*/
4181 "Unpickler", /*tp_name*/
4182 sizeof(Unpicklerobject), /*tp_basicsize*/
4183 0, /*tp_itemsize*/
4184 /* methods */
4185 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4186 (printfunc)0, /*tp_print*/
4187 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4188 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4189 (cmpfunc)0, /*tp_compare*/
4190 (reprfunc)0, /*tp_repr*/
4191 0, /*tp_as_number*/
4192 0, /*tp_as_sequence*/
4193 0, /*tp_as_mapping*/
4194 (hashfunc)0, /*tp_hash*/
4195 (ternaryfunc)0, /*tp_call*/
4196 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004197
Guido van Rossum60456fd1997-04-09 17:36:32 +00004198 /* Space for future expansion */
4199 0L,0L,0L,0L,
4200 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004201};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004202
Guido van Rossum60456fd1997-04-09 17:36:32 +00004203static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004204 {"dump", (PyCFunction)cpm_dump, 1,
4205 "dump(object, file, [binary]) --"
4206 "Write an object in pickle format to the given file\n"
4207 "\n"
4208 "If the optional argument, binary, is provided and is true, then the\n"
4209 "pickle will be written in binary format, which is more space and\n"
4210 "computationally efficient. \n"
4211 },
4212 {"dumps", (PyCFunction)cpm_dumps, 1,
4213 "dumps(object, [binary]) --"
4214 "Return a string containing an object in pickle format\n"
4215 "\n"
4216 "If the optional argument, binary, is provided and is true, then the\n"
4217 "pickle will be written in binary format, which is more space and\n"
4218 "computationally efficient. \n"
4219 },
4220 {"load", (PyCFunction)cpm_load, 1,
4221 "load(file) -- Load a pickle from the given file"},
4222 {"loads", (PyCFunction)cpm_loads, 1,
4223 "loads(string) -- Load a pickle from the given string"},
4224 {"Pickler", (PyCFunction)get_Pickler, 1,
4225 "Pickler(file, [binary]) -- Create a pickler\n"
4226 "\n"
4227 "If the optional argument, binary, is provided and is true, then\n"
4228 "pickles will be written in binary format, which is more space and\n"
4229 "computationally efficient. \n"
4230 },
4231 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4232 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004233 { NULL, NULL }
4234};
4235
4236
Guido van Rossum60456fd1997-04-09 17:36:32 +00004237#define CHECK_FOR_ERRORS(MESS) \
Guido van Rossum053b8df1998-11-25 16:18:00 +00004238if (PyErr_Occurred()) { \
Guido van Rossum60456fd1997-04-09 17:36:32 +00004239 PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \
4240 PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \
4241 fprintf(stderr, # MESS ":\n\t"); \
4242 PyObject_Print(__sys_exc_type, stderr,0); \
4243 fprintf(stderr,", "); \
4244 PyObject_Print(__sys_exc_value, stderr,0); \
4245 fprintf(stderr,"\n"); \
4246 fflush(stderr); \
4247 Py_FatalError(# MESS); \
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004248}
4249
4250
Guido van Rossum60456fd1997-04-09 17:36:32 +00004251static int
4252init_stuff(PyObject *module, PyObject *module_dict) {
4253 PyObject *string, *copy_reg;
4254
4255#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4256
4257 INIT_STR(__class__);
4258 INIT_STR(__getinitargs__);
4259 INIT_STR(__dict__);
4260 INIT_STR(__getstate__);
4261 INIT_STR(__setstate__);
4262 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004263 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004264 INIT_STR(__reduce__);
4265 INIT_STR(write);
4266 INIT_STR(__safe_for_unpickling__);
4267 INIT_STR(append);
4268 INIT_STR(read);
4269 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004270 INIT_STR(copy_reg);
4271 INIT_STR(dispatch_table);
4272 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004273 INIT_STR(__basicnew__);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004274 UNLESS (empty_str=PyString_FromString("")) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004275
Guido van Rossum053b8df1998-11-25 16:18:00 +00004276 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004277 return -1;
4278
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004279 /* These next few are special because we want to use different
4280 ones in restricted mode. */
4281
Guido van Rossum053b8df1998-11-25 16:18:00 +00004282 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004283 return -1;
4284
Guido van Rossum053b8df1998-11-25 16:18:00 +00004285 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4286 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004287 return -1;
4288
4289 Py_DECREF(copy_reg);
4290
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004291 /* Down to here ********************************** */
4292
Guido van Rossum053b8df1998-11-25 16:18:00 +00004293 UNLESS (string = PyImport_ImportModule("string"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004294 return -1;
4295
Guido van Rossum053b8df1998-11-25 16:18:00 +00004296 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004297 return -1;
4298
4299 Py_DECREF(string);
4300
Guido van Rossum053b8df1998-11-25 16:18:00 +00004301 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004302 return -1;
4303
Guido van Rossum053b8df1998-11-25 16:18:00 +00004304 UNLESS (PicklingError = PyString_FromString("cPickle.PicklingError"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004305 return -1;
4306
4307 if (PyDict_SetItemString(module_dict, "PicklingError",
4308 PicklingError) < 0)
4309 return -1;
4310
Guido van Rossum053b8df1998-11-25 16:18:00 +00004311 UNLESS (UnpicklingError = PyString_FromString("cPickle.UnpicklingError"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004312 return -1;
4313
4314 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4315 UnpicklingError) < 0)
4316 return -1;
4317
Guido van Rossum053b8df1998-11-25 16:18:00 +00004318 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4319 return -1;
4320
4321 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4322 BadPickleGet) < 0)
4323 return -1;
4324
Guido van Rossum60456fd1997-04-09 17:36:32 +00004325 PycString_IMPORT;
4326
4327 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004328}
4329
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004330#ifndef DL_EXPORT /* declarations for DLL import/export */
4331#define DL_EXPORT(RTYPE) RTYPE
4332#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004333DL_EXPORT(void)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004334initcPickle() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004335 PyObject *m, *d, *v;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004336 char *rev="1.66";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004337 PyObject *format_version;
4338 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004339
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004340 Picklertype.ob_type = &PyType_Type;
4341 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004342 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004343
Guido van Rossum60456fd1997-04-09 17:36:32 +00004344 /* Create the module and add the functions */
4345 m = Py_InitModule4("cPickle", cPickle_methods,
4346 cPickle_module_documentation,
4347 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004348
Guido van Rossum60456fd1997-04-09 17:36:32 +00004349 /* Add some symbolic constants to the module */
4350 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004351 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004352 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004353
Guido van Rossum60456fd1997-04-09 17:36:32 +00004354 format_version = PyString_FromString("1.3");
4355 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004356
Guido van Rossum60456fd1997-04-09 17:36:32 +00004357 PyDict_SetItemString(d, "format_version", format_version);
4358 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004359 Py_XDECREF(format_version);
4360 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004361
Guido van Rossum60456fd1997-04-09 17:36:32 +00004362 init_stuff(m, d);
4363 CHECK_FOR_ERRORS("can't initialize module cPickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004364}