blob: aa2c7cba08d2a05b72632a8f0cb5ea9d8148e31f [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001/*
Guido van Rossum2f80d961999-07-13 15:18:58 +00002 * cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp
Guido van Rossum053b8df1998-11-25 16:18:00 +00003 *
4 * Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * o Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the disclaimer that follows.
13 *
14 * o Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions, and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * o Neither the name of Digital Creations nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 *
24 * THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
25 * IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
28 * CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
31 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
35 * DAMAGE.
36 *
37 #
38 # If you have questions regarding this software, contact:
39 #
40 # Digital Creations, L.C.
41 # 910 Princess Ann Street
42 # Fredericksburge, Virginia 22401
43 #
44 # info@digicool.com
45 #
46 # (540) 371-6909
47 */
Guido van Rossum2f4caa41997-01-06 22:59:08 +000048
49static char cPickle_module_documentation[] =
Guido van Rossum142eeb81997-08-13 03:14:41 +000050"C implementation and optimization of the Python pickle module\n"
51"\n"
Guido van Rossum2f80d961999-07-13 15:18:58 +000052"cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp\n"
Guido van Rossum2f4caa41997-01-06 22:59:08 +000053;
54
55#include "Python.h"
56#include "cStringIO.h"
Guido van Rossum60456fd1997-04-09 17:36:32 +000057#include "mymath.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +000058
Guido van Rossum142eeb81997-08-13 03:14:41 +000059#ifndef Py_eval_input
60#include <graminit.h>
61#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000062#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000063
Guido van Rossum2f4caa41997-01-06 22:59:08 +000064#include <errno.h>
65
Guido van Rossum2f4caa41997-01-06 22:59:08 +000066#define UNLESS(E) if (!(E))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000067
Guido van Rossum60456fd1997-04-09 17:36:32 +000068#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000069
Guido van Rossum60456fd1997-04-09 17:36:32 +000070#define WRITE_BUF_SIZE 256
71
72
73#define MARK '('
74#define STOP '.'
75#define POP '0'
76#define POP_MARK '1'
77#define DUP '2'
78#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000079#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000080#define INT 'I'
81#define BININT 'J'
82#define BININT1 'K'
83#define LONG 'L'
84#define BININT2 'M'
85#define NONE 'N'
86#define PERSID 'P'
87#define BINPERSID 'Q'
88#define REDUCE 'R'
89#define STRING 'S'
90#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000091#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000092#define UNICODE 'V'
93#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000094#define APPEND 'a'
95#define BUILD 'b'
96#define GLOBAL 'c'
97#define DICT 'd'
98#define EMPTY_DICT '}'
99#define APPENDS 'e'
100#define GET 'g'
101#define BINGET 'h'
102#define INST 'i'
103#define LONG_BINGET 'j'
104#define LIST 'l'
105#define EMPTY_LIST ']'
106#define OBJ 'o'
107#define PUT 'p'
108#define BINPUT 'q'
109#define LONG_BINPUT 'r'
110#define SETITEM 's'
111#define TUPLE 't'
112#define EMPTY_TUPLE ')'
113#define SETITEMS 'u'
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000114
Guido van Rossum60456fd1997-04-09 17:36:32 +0000115static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000116
117/* atol function from string module */
118static PyObject *atol_func;
119
Guido van Rossumc03158b1999-06-09 15:23:31 +0000120static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000121static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000122static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000123static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000124static PyObject *BadPickleGet;
125
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000126
Guido van Rossum60456fd1997-04-09 17:36:32 +0000127static PyObject *dispatch_table;
128static PyObject *safe_constructors;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000129static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000130
Guido van Rossum60456fd1997-04-09 17:36:32 +0000131static PyObject *__class___str, *__getinitargs___str, *__dict___str,
132 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
133 *write_str, *__safe_for_unpickling___str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000134 *read_str, *readline_str, *__main___str, *__basicnew___str,
Guido van Rossum053b8df1998-11-25 16:18:00 +0000135 *copy_reg_str, *dispatch_table_str, *safe_constructors_str, *empty_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000136
Guido van Rossum60456fd1997-04-09 17:36:32 +0000137static int save();
138static int put2();
139
Guido van Rossum053b8df1998-11-25 16:18:00 +0000140#ifndef PyList_SET_ITEM
141#define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
142#endif
143#ifndef PyList_GET_SIZE
144#define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
145#endif
146#ifndef PyTuple_SET_ITEM
147#define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
148#endif
149#ifndef PyTuple_GET_SIZE
150#define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
151#endif
152#ifndef PyString_GET_SIZE
153#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
154#endif
155
156/*************************************************************************
157 Internal Data type for pickle data. */
158
159typedef struct {
160 PyObject_HEAD
161 int length, size;
162 PyObject **data;
163} Pdata;
164
165static void
166Pdata_dealloc(Pdata *self) {
167 int i;
168 PyObject **p;
169
170 for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
171
172 if (self->data) free(self->data);
173
174 PyMem_DEL(self);
175}
176
177static PyTypeObject PdataType = {
178 PyObject_HEAD_INIT(NULL) 0, "Pdata", sizeof(Pdata), 0,
179 (destructor)Pdata_dealloc,
180 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
181};
182
183#define Pdata_Check(O) ((O)->ob_type == &PdataType)
184
185static PyObject *
186Pdata_New() {
187 Pdata *self;
188
189 UNLESS (self = PyObject_NEW(Pdata, &PdataType)) return NULL;
190 self->size=8;
191 self->length=0;
192 self->data=malloc(self->size * sizeof(PyObject*));
193 if (self->data) return (PyObject*)self;
194 Py_DECREF(self);
195 return PyErr_NoMemory();
196}
197
198static int
199stackUnderflow() {
200 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
201 return -1;
202}
203
204static int
205Pdata_clear(Pdata *self, int clearto) {
206 int i;
207 PyObject **p;
208
209 if (clearto < 0) return stackUnderflow();
210 if (clearto >= self->length) return 0;
211
212 for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
213 Py_DECREF(*p);
214 self->length=clearto;
215
216 return 0;
217}
218
219
220static int
221Pdata_grow(Pdata *self) {
222 if (! self->size) {
223 PyErr_NoMemory();
224 return -1;
225 }
226 self->size *= 2;
227 self->data = realloc(self->data, self->size*sizeof(PyObject*));
228 if (! self->data) {
229 self->size = 0;
230 PyErr_NoMemory();
231 return -1;
232 }
233 return 0;
234}
235
236#define PDATA_POP(D,V) { \
237 if ((D)->length) V=D->data[--((D)->length)]; \
238 else { \
239 PyErr_SetString(UnpicklingError, "bad pickle data"); \
240 V=NULL; \
241 } \
242}
243
244
245static PyObject *
246Pdata_popTuple(Pdata *self, int start) {
247 PyObject *r;
248 int i, j, l;
249
250 l=self->length-start;
251 UNLESS (r=PyTuple_New(l)) return NULL;
252 for (i=start, j=0 ; j < l; )
253 PyTuple_SET_ITEM(r,j++,self->data[i++]);
254
255 self->length=start;
256 return r;
257}
258
259static PyObject *
260Pdata_popList(Pdata *self, int start) {
261 PyObject *r;
262 int i, j, l;
263
264 l=self->length-start;
265 UNLESS (r=PyList_New(l)) return NULL;
266 for (i=start, j=0 ; j < l; )
267 PyList_SET_ITEM(r,j++,self->data[i++]);
268
269 self->length=start;
270 return r;
271}
272
273#define PDATA_APPEND_(D,O,ER) { \
274 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
275}
276
277#define PDATA_APPEND(D,O,ER) { \
278 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
279 Pdata_grow((Pdata*)(D)) < 0) \
280 return ER; \
281 Py_INCREF(O); \
282 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
283}
284
285#define PDATA_PUSH(D,O,ER) { \
286 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
287 Pdata_grow((Pdata*)(D)) < 0) { \
288 Py_DECREF(O); \
289 return ER; \
290 } \
291 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
292}
293
294/*************************************************************************/
295
296#define ARG_TUP(self, o) { \
297 if (self->arg || (self->arg=PyTuple_New(1))) { \
298 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
299 PyTuple_SET_ITEM(self->arg,0,o); \
300 } \
301 else { \
302 Py_DECREF(o); \
303 } \
304}
305
306#define FREE_ARG_TUP(self) { \
307 if (self->arg->ob_refcnt > 1) { \
308 Py_DECREF(self->arg); \
309 self->arg=NULL; \
310 } \
311 }
312
Guido van Rossum60456fd1997-04-09 17:36:32 +0000313typedef struct {
314 PyObject_HEAD
315 FILE *fp;
316 PyObject *write;
317 PyObject *file;
318 PyObject *memo;
319 PyObject *arg;
320 PyObject *pers_func;
321 PyObject *inst_pers_func;
322 int bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000323 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000324 int (*write_func)();
325 char *write_buf;
326 int buf_size;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000327 PyObject *dispatch_table;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000328} Picklerobject;
329
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000330staticforward PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000331
Guido van Rossum60456fd1997-04-09 17:36:32 +0000332typedef struct {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000333 PyObject_HEAD
334 FILE *fp;
335 PyObject *file;
336 PyObject *readline;
337 PyObject *read;
338 PyObject *memo;
339 PyObject *arg;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000340 Pdata *stack;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000341 PyObject *mark;
342 PyObject *pers_func;
343 PyObject *last_string;
344 int *marks;
345 int num_marks;
346 int marks_size;
347 int (*read_func)();
348 int (*readline_func)();
349 int buf_size;
350 char *buf;
351 PyObject *safe_constructors;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +0000352 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000353} Unpicklerobject;
354
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000355staticforward PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000356
Guido van Rossum60456fd1997-04-09 17:36:32 +0000357int
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000358cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000359 PyObject *v;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000360
Guido van Rossum053b8df1998-11-25 16:18:00 +0000361 if ((v = PyObject_GetItem(o,key))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000362 Py_DECREF(v);
363 return 1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000364 }
365
Guido van Rossum60456fd1997-04-09 17:36:32 +0000366 PyErr_Clear();
367 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000368}
369
Guido van Rossumd385d591997-04-09 17:47:47 +0000370static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000371PyObject *
372#ifdef HAVE_STDARG_PROTOTYPES
373/* VARARGS 2 */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000374cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000375#else
376/* VARARGS */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000377cPickle_ErrFormat(va_alist) va_dcl {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000378#endif
Guido van Rossum60456fd1997-04-09 17:36:32 +0000379 va_list va;
380 PyObject *args=0, *retval=0;
381#ifdef HAVE_STDARG_PROTOTYPES
382 va_start(va, format);
383#else
384 PyObject *ErrType;
385 char *stringformat, *format;
386 va_start(va);
387 ErrType = va_arg(va, PyObject *);
388 stringformat = va_arg(va, char *);
389 format = va_arg(va, char *);
390#endif
391
Guido van Rossum053b8df1998-11-25 16:18:00 +0000392 if (format) args = Py_VaBuildValue(format, va);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000393 va_end(va);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000394 if (format && ! args) return NULL;
395 if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000396
Guido van Rossum053b8df1998-11-25 16:18:00 +0000397 if (retval) {
398 if (args) {
399 PyObject *v;
400 v=PyString_Format(retval, args);
401 Py_DECREF(retval);
402 Py_DECREF(args);
403 if (! v) return NULL;
404 retval=v;
405 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000406 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000407 else
Guido van Rossum053b8df1998-11-25 16:18:00 +0000408 if (args) retval=args;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000409 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000410 PyErr_SetObject(ErrType,Py_None);
411 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000412 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000413 PyErr_SetObject(ErrType,retval);
414 Py_DECREF(retval);
415 return NULL;
416}
417
418static int
419write_file(Picklerobject *self, char *s, int n) {
420 if (s == NULL) {
421 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000422 }
423
Guido van Rossum60456fd1997-04-09 17:36:32 +0000424 if ((int)fwrite(s, sizeof(char), n, self->fp) != n) {
425 PyErr_SetFromErrno(PyExc_IOError);
426 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000427 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000428
429 return n;
430}
431
Guido van Rossum60456fd1997-04-09 17:36:32 +0000432static int
433write_cStringIO(Picklerobject *self, char *s, int n) {
434 if (s == NULL) {
435 return 0;
436 }
437
438 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
439 return -1;
440 }
441
442 return n;
443}
444
Guido van Rossum60456fd1997-04-09 17:36:32 +0000445static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000446write_none(Picklerobject *self, char *s, int n) {
447 if (s == NULL) return 0;
448 return n;
449}
450
Guido van Rossum142eeb81997-08-13 03:14:41 +0000451static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000452write_other(Picklerobject *self, char *s, int n) {
453 PyObject *py_str = 0, *junk = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000454
455 if (s == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000456 UNLESS (self->buf_size) return 0;
457 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000458 PyString_FromStringAndSize(self->write_buf, self->buf_size))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000459 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000460 }
461 else {
462 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
463 if (write_other(self, NULL, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000464 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000465 }
466
467 if (n > WRITE_BUF_SIZE) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000468 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000469 PyString_FromStringAndSize(s, n))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000470 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000471 }
472 else {
473 memcpy(self->write_buf + self->buf_size, s, n);
474 self->buf_size += n;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000475 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000476 }
477 }
478
Guido van Rossum053b8df1998-11-25 16:18:00 +0000479 if (self->write) {
480 /* object with write method */
481 ARG_TUP(self, py_str);
482 if (self->arg) {
483 junk = PyObject_CallObject(self->write, self->arg);
484 FREE_ARG_TUP(self);
485 }
486 if (junk) Py_DECREF(junk);
487 else return -1;
488 }
489 else
490 PDATA_PUSH(self->file, py_str, -1);
491
492 self->buf_size = 0;
493 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000494}
495
496
497static int
498read_file(Unpicklerobject *self, char **s, int n) {
499
500 if (self->buf_size == 0) {
501 int size;
502
503 size = ((n < 32) ? 32 : n);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000504 UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000505 PyErr_NoMemory();
506 return -1;
507 }
508
509 self->buf_size = size;
510 }
511 else if (n > self->buf_size) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000512 UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000513 PyErr_NoMemory();
514 return -1;
515 }
516
517 self->buf_size = n;
518 }
519
520 if ((int)fread(self->buf, sizeof(char), n, self->fp) != n) {
521 if (feof(self->fp)) {
522 PyErr_SetNone(PyExc_EOFError);
523 return -1;
524 }
525
526 PyErr_SetFromErrno(PyExc_IOError);
527 return -1;
528 }
529
530 *s = self->buf;
531
532 return n;
533}
534
535
536static int
537readline_file(Unpicklerobject *self, char **s) {
538 int i;
539
540 if (self->buf_size == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000541 UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000542 PyErr_NoMemory();
543 return -1;
544 }
545
546 self->buf_size = 40;
547 }
548
549 i = 0;
550 while (1) {
551 for (; i < (self->buf_size - 1); i++) {
552 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
553 self->buf[i + 1] = '\0';
554 *s = self->buf;
555 return i + 1;
556 }
557 }
558
Guido van Rossum053b8df1998-11-25 16:18:00 +0000559 UNLESS (self->buf = (char *)realloc(self->buf,
Guido van Rossum60456fd1997-04-09 17:36:32 +0000560 (self->buf_size * 2) * sizeof(char))) {
561 PyErr_NoMemory();
562 return -1;
563 }
564
565 self->buf_size *= 2;
566 }
567
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000568}
569
570
571static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000572read_cStringIO(Unpicklerobject *self, char **s, int n) {
573 char *ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000574
Guido van Rossum60456fd1997-04-09 17:36:32 +0000575 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
576 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000577 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000578 }
579
Guido van Rossum60456fd1997-04-09 17:36:32 +0000580 *s = ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000581
Guido van Rossum60456fd1997-04-09 17:36:32 +0000582 return n;
583}
584
585
586static int
587readline_cStringIO(Unpicklerobject *self, char **s) {
588 int n;
589 char *ptr;
590
591 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
592 return -1;
593 }
594
595 *s = ptr;
596
597 return n;
598}
599
600
601static int
602read_other(Unpicklerobject *self, char **s, int n) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000603 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000604
Guido van Rossum053b8df1998-11-25 16:18:00 +0000605 UNLESS (bytes = PyInt_FromLong(n)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000606
Guido van Rossum053b8df1998-11-25 16:18:00 +0000607 ARG_TUP(self, bytes);
608 if (self->arg) {
609 str = PyObject_CallObject(self->read, self->arg);
610 FREE_ARG_TUP(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000611 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000612 if (! str) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000613
614 Py_XDECREF(self->last_string);
615 self->last_string = str;
616
Guido van Rossum053b8df1998-11-25 16:18:00 +0000617 if (! (*s = PyString_AsString(str))) return -1;
618 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000619}
620
621
622static int
623readline_other(Unpicklerobject *self, char **s) {
624 PyObject *str;
625 int str_size;
626
Guido van Rossum053b8df1998-11-25 16:18:00 +0000627 UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000628 return -1;
629 }
630
Guido van Rossum053b8df1998-11-25 16:18:00 +0000631 if ((str_size = PyString_Size(str)) < 0)
632 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000633
634 Py_XDECREF(self->last_string);
635 self->last_string = str;
636
Guido van Rossum053b8df1998-11-25 16:18:00 +0000637 if (! (*s = PyString_AsString(str)))
638 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000639
640 return str_size;
641}
642
643
644static char *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000645pystrndup(char *s, int l) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000646 char *r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000647 UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
Guido van Rossum60456fd1997-04-09 17:36:32 +0000648 memcpy(r,s,l);
649 r[l]=0;
650 return r;
651}
652
653
654static int
655get(Picklerobject *self, PyObject *id) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000656 PyObject *value, *mv;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000657 long c_value;
658 char s[30];
659 int len;
660
Guido van Rossum053b8df1998-11-25 16:18:00 +0000661 UNLESS (mv = PyDict_GetItem(self->memo, id)) {
662 PyErr_SetObject(PyExc_KeyError, id);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000663 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000664 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000665
Guido van Rossum053b8df1998-11-25 16:18:00 +0000666 UNLESS (value = PyTuple_GetItem(mv, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000667 return -1;
668
Guido van Rossum053b8df1998-11-25 16:18:00 +0000669 UNLESS (PyInt_Check(value)) {
670 PyErr_SetString(PicklingError, "no int where int expected in memo");
671 return -1;
672 }
673 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000674
675 if (!self->bin) {
676 s[0] = GET;
677 sprintf(s + 1, "%ld\n", c_value);
678 len = strlen(s);
679 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000680 else if (Pdata_Check(self->file)) {
681 if (write_other(self, NULL, 0) < 0) return -1;
682 PDATA_APPEND(self->file, mv, -1);
683 return 0;
684 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000685 else {
686 if (c_value < 256) {
687 s[0] = BINGET;
688 s[1] = (int)(c_value & 0xff);
689 len = 2;
690 }
691 else {
692 s[0] = LONG_BINGET;
693 s[1] = (int)(c_value & 0xff);
694 s[2] = (int)((c_value >> 8) & 0xff);
695 s[3] = (int)((c_value >> 16) & 0xff);
696 s[4] = (int)((c_value >> 24) & 0xff);
697 len = 5;
698 }
699 }
700
701 if ((*self->write_func)(self, s, len) < 0)
702 return -1;
703
704 return 0;
705}
706
707
708static int
709put(Picklerobject *self, PyObject *ob) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000710 if (ob->ob_refcnt < 2 || self->fast)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000711 return 0;
712
713 return put2(self, ob);
714}
715
716
717static int
718put2(Picklerobject *self, PyObject *ob) {
719 char c_str[30];
720 int p, len, res = -1;
721 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000722
723 if (self->fast) return 0;
724
Guido van Rossum60456fd1997-04-09 17:36:32 +0000725 if ((p = PyDict_Size(self->memo)) < 0)
726 goto finally;
727
Guido van Rossum053b8df1998-11-25 16:18:00 +0000728 p++; /* Make sure memo keys are positive! */
729
730 UNLESS (py_ob_id = PyInt_FromLong((long)ob))
731 goto finally;
732
733 UNLESS (memo_len = PyInt_FromLong(p))
734 goto finally;
735
736 UNLESS (t = PyTuple_New(2))
737 goto finally;
738
739 PyTuple_SET_ITEM(t, 0, memo_len);
740 Py_INCREF(memo_len);
741 PyTuple_SET_ITEM(t, 1, ob);
742 Py_INCREF(ob);
743
744 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
745 goto finally;
746
Guido van Rossum60456fd1997-04-09 17:36:32 +0000747 if (!self->bin) {
748 c_str[0] = PUT;
749 sprintf(c_str + 1, "%d\n", p);
750 len = strlen(c_str);
751 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000752 else if (Pdata_Check(self->file)) {
753 if (write_other(self, NULL, 0) < 0) return -1;
754 PDATA_APPEND(self->file, memo_len, -1);
755 res=0; /* Job well done ;) */
756 goto finally;
757 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000758 else {
759 if (p >= 256) {
760 c_str[0] = LONG_BINPUT;
761 c_str[1] = (int)(p & 0xff);
762 c_str[2] = (int)((p >> 8) & 0xff);
763 c_str[3] = (int)((p >> 16) & 0xff);
764 c_str[4] = (int)((p >> 24) & 0xff);
765 len = 5;
766 }
767 else {
768 c_str[0] = BINPUT;
769 c_str[1] = p;
770 len = 2;
771 }
772 }
773
774 if ((*self->write_func)(self, c_str, len) < 0)
775 goto finally;
776
Guido van Rossum60456fd1997-04-09 17:36:32 +0000777 res = 0;
778
779finally:
780 Py_XDECREF(py_ob_id);
781 Py_XDECREF(memo_len);
782 Py_XDECREF(t);
783
784 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000785}
786
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000787#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000788
789static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000790PyImport_Import(PyObject *module_name) {
791 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
792 static PyObject *standard_builtins=0;
793 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
794
Guido van Rossum053b8df1998-11-25 16:18:00 +0000795 UNLESS (silly_list) {
796 UNLESS (__import___str=PyString_FromString("__import__"))
797 return NULL;
798 UNLESS (__builtins___str=PyString_FromString("__builtins__"))
799 return NULL;
800 UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
801 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000802 }
803
Guido van Rossum053b8df1998-11-25 16:18:00 +0000804 if ((globals=PyEval_GetGlobals())) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000805 Py_INCREF(globals);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000806 UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
807 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000808 }
809 else {
810 PyErr_Clear();
811
Guido van Rossum053b8df1998-11-25 16:18:00 +0000812 UNLESS (standard_builtins ||
813 (standard_builtins=PyImport_ImportModule("__builtin__")))
814 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000815
816 __builtins__=standard_builtins;
817 Py_INCREF(__builtins__);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000818 UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
819 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000820 }
821
Guido van Rossum053b8df1998-11-25 16:18:00 +0000822 if (PyDict_Check(__builtins__)) {
823 UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000824 }
825 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000826 UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000827 }
828
Guido van Rossum053b8df1998-11-25 16:18:00 +0000829 UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
830 module_name, globals, globals, silly_list))
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000831 goto err;
832
833 Py_DECREF(globals);
834 Py_DECREF(__builtins__);
835 Py_DECREF(__import__);
836
837 return r;
838err:
839 Py_XDECREF(globals);
840 Py_XDECREF(__builtins__);
841 Py_XDECREF(__import__);
842 return NULL;
843}
844
845static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +0000846whichmodule(PyObject *global, PyObject *global_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000847 int i, j;
848 PyObject *module = 0, *modules_dict = 0,
849 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000850
Guido van Rossum45188231997-09-28 05:38:51 +0000851 module = PyObject_GetAttrString(global, "__module__");
852 if (module) return module;
853 PyErr_Clear();
854
Guido van Rossum053b8df1998-11-25 16:18:00 +0000855 UNLESS (modules_dict = PySys_GetObject("modules"))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000856 return NULL;
857
Guido van Rossum60456fd1997-04-09 17:36:32 +0000858 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000859 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
860
Guido van Rossum053b8df1998-11-25 16:18:00 +0000861 if (PyObject_Compare(name, __main___str)==0) continue;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000862
Guido van Rossum053b8df1998-11-25 16:18:00 +0000863 UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000864 PyErr_Clear();
865 continue;
866 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000867
Guido van Rossum60456fd1997-04-09 17:36:32 +0000868 if (global_name_attr != global) {
869 Py_DECREF(global_name_attr);
870 continue;
871 }
872
873 Py_DECREF(global_name_attr);
874
875 break;
876 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000877
878 /* The following implements the rule in pickle.py added in 1.5
879 that used __main__ if no module is found. I don't actually
880 like this rule. jlf
881 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000882 if (!j) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000883 j=1;
884 name=__main___str;
885 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000886
887 Py_INCREF(name);
888 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000889}
890
891
Guido van Rossum60456fd1997-04-09 17:36:32 +0000892static int
893save_none(Picklerobject *self, PyObject *args) {
894 static char none = NONE;
895 if ((*self->write_func)(self, &none, 1) < 0)
896 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000897
Guido van Rossum60456fd1997-04-09 17:36:32 +0000898 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000899}
900
901
Guido van Rossum60456fd1997-04-09 17:36:32 +0000902static int
903save_int(Picklerobject *self, PyObject *args) {
904 char c_str[32];
905 long l = PyInt_AS_LONG((PyIntObject *)args);
906 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000907
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000908 if (!self->bin
909#if SIZEOF_LONG > 4
Guido van Rossum053b8df1998-11-25 16:18:00 +0000910 || (l >> 32)
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000911#endif
Guido van Rossum053b8df1998-11-25 16:18:00 +0000912 ) {
913 /* Save extra-long ints in non-binary mode, so that
914 we can use python long parsing code to restore,
915 if necessary. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000916 c_str[0] = INT;
917 sprintf(c_str + 1, "%ld\n", l);
918 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
919 return -1;
920 }
921 else {
922 c_str[1] = (int)( l & 0xff);
923 c_str[2] = (int)((l >> 8) & 0xff);
924 c_str[3] = (int)((l >> 16) & 0xff);
925 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000926
Guido van Rossum60456fd1997-04-09 17:36:32 +0000927 if ((c_str[4] == 0) && (c_str[3] == 0)) {
928 if (c_str[2] == 0) {
929 c_str[0] = BININT1;
930 len = 2;
931 }
932 else {
933 c_str[0] = BININT2;
934 len = 3;
935 }
936 }
937 else {
938 c_str[0] = BININT;
939 len = 5;
940 }
941
942 if ((*self->write_func)(self, c_str, len) < 0)
943 return -1;
944 }
945
946 return 0;
947}
948
949
950static int
951save_long(Picklerobject *self, PyObject *args) {
952 int size, res = -1;
953 PyObject *repr = 0;
954
955 static char l = LONG;
956
Guido van Rossum053b8df1998-11-25 16:18:00 +0000957 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000958 goto finally;
959
960 if ((size = PyString_Size(repr)) < 0)
961 goto finally;
962
963 if ((*self->write_func)(self, &l, 1) < 0)
964 goto finally;
965
966 if ((*self->write_func)(self,
967 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
968 goto finally;
969
970 if ((*self->write_func)(self, "\n", 1) < 0)
971 goto finally;
972
973 res = 0;
974
975finally:
976 Py_XDECREF(repr);
977
978 return res;
979}
980
981
982static int
983save_float(Picklerobject *self, PyObject *args) {
984 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
985
Guido van Rossum60456fd1997-04-09 17:36:32 +0000986 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000987 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000988 double f;
989 long fhi, flo;
990 char str[9], *p = str;
991
992 *p = BINFLOAT;
993 p++;
994
995 if (x < 0) {
996 s = 1;
997 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000998 }
999 else
Guido van Rossum60456fd1997-04-09 17:36:32 +00001000 s = 0;
1001
1002 f = frexp(x, &e);
1003
1004 /* Normalize f to be in the range [1.0, 2.0) */
1005 if (0.5 <= f && f < 1.0) {
1006 f *= 2.0;
1007 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001008 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001009 else if (f == 0.0) {
1010 e = 0;
1011 }
1012 else {
1013 PyErr_SetString(PyExc_SystemError,
1014 "frexp() result out of range");
1015 return -1;
1016 }
1017
1018 if (e >= 1024) {
1019 /* XXX 1024 itself is reserved for Inf/NaN */
1020 PyErr_SetString(PyExc_OverflowError,
1021 "float too large to pack with d format");
1022 return -1;
1023 }
1024 else if (e < -1022) {
1025 /* Gradual underflow */
1026 f = ldexp(f, 1022 + e);
1027 e = 0;
1028 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001029 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001030 e += 1023;
1031 f -= 1.0; /* Get rid of leading 1 */
1032 }
1033
1034 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1035 f *= 268435456.0; /* 2**28 */
1036 fhi = (long) floor(f); /* Truncate */
1037 f -= (double)fhi;
1038 f *= 16777216.0; /* 2**24 */
1039 flo = (long) floor(f + 0.5); /* Round */
1040
1041 /* First byte */
1042 *p = (s<<7) | (e>>4);
1043 p++;
1044
1045 /* Second byte */
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001046 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum60456fd1997-04-09 17:36:32 +00001047 p++;
1048
1049 /* Third byte */
1050 *p = (fhi>>16) & 0xFF;
1051 p++;
1052
1053 /* Fourth byte */
1054 *p = (fhi>>8) & 0xFF;
1055 p++;
1056
1057 /* Fifth byte */
1058 *p = fhi & 0xFF;
1059 p++;
1060
1061 /* Sixth byte */
1062 *p = (flo>>16) & 0xFF;
1063 p++;
1064
1065 /* Seventh byte */
1066 *p = (flo>>8) & 0xFF;
1067 p++;
1068
1069 /* Eighth byte */
1070 *p = flo & 0xFF;
1071
1072 if ((*self->write_func)(self, str, 9) < 0)
1073 return -1;
1074 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001075 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001076 char c_str[250];
1077 c_str[0] = FLOAT;
Guido van Rossum104be4a1998-04-03 21:13:02 +00001078 sprintf(c_str + 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001079
1080 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
1081 return -1;
1082 }
1083
1084 return 0;
1085}
1086
1087
1088static int
Guido van Rossum142eeb81997-08-13 03:14:41 +00001089save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001090 int size, len;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001091 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001092
Guido van Rossum053b8df1998-11-25 16:18:00 +00001093 if ((size = PyString_Size(args)) < 0)
1094 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001095
1096 if (!self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001097 char *repr_str;
1098
1099 static char string = STRING;
1100
Guido van Rossum053b8df1998-11-25 16:18:00 +00001101 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001102 return -1;
1103
Guido van Rossum053b8df1998-11-25 16:18:00 +00001104 if ((len = PyString_Size(repr)) < 0)
1105 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001106 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001107
1108 if ((*self->write_func)(self, &string, 1) < 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, repr_str, len) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001112 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001113
1114 if ((*self->write_func)(self, "\n", 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001115 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001116
1117 Py_XDECREF(repr);
1118 }
1119 else {
1120 int i;
1121 char c_str[5];
1122
Guido van Rossum053b8df1998-11-25 16:18:00 +00001123 if ((size = PyString_Size(args)) < 0)
1124 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001125
1126 if (size < 256) {
1127 c_str[0] = SHORT_BINSTRING;
1128 c_str[1] = size;
1129 len = 2;
1130 }
1131 else {
1132 c_str[0] = BINSTRING;
1133 for (i = 1; i < 5; i++)
1134 c_str[i] = (int)(size >> ((i - 1) * 8));
1135 len = 5;
1136 }
1137
1138 if ((*self->write_func)(self, c_str, len) < 0)
1139 return -1;
1140
Guido van Rossum053b8df1998-11-25 16:18:00 +00001141 if (size > 128 && Pdata_Check(self->file)) {
1142 if (write_other(self, NULL, 0) < 0) return -1;
1143 PDATA_APPEND(self->file, args, -1);
1144 }
1145 else {
1146 if ((*self->write_func)(self,
1147 PyString_AS_STRING((PyStringObject *)args), size) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001148 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001149 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001150 }
1151
Guido van Rossum142eeb81997-08-13 03:14:41 +00001152 if (doput)
1153 if (put(self, args) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001154 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001155
1156 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001157
1158err:
1159 Py_XDECREF(repr);
1160 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001161}
1162
1163
1164static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001165save_unicode(Picklerobject *self, PyObject *args, int doput) {
1166 int size, len;
1167 PyObject *repr=0;
1168
1169 if (!PyUnicode_Check(args))
1170 return -1;
1171
1172 if (!self->bin) {
1173 char *repr_str;
1174 static char string = UNICODE;
1175
1176 UNLESS (repr = PyUnicode_AsRawUnicodeEscapeString(args))
1177 return -1;
1178
1179 if ((len = PyString_Size(repr)) < 0)
1180 goto err;
1181 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1182
1183 if ((*self->write_func)(self, &string, 1) < 0)
1184 goto err;
1185
1186 if ((*self->write_func)(self, repr_str, len) < 0)
1187 goto err;
1188
1189 if ((*self->write_func)(self, "\n", 1) < 0)
1190 goto err;
1191
1192 Py_XDECREF(repr);
1193 }
1194 else {
1195 int i;
1196 char c_str[5];
1197
1198 UNLESS (repr = PyUnicode_AsUTF8String(args))
1199 return -1;
1200
1201 if ((size = PyString_Size(repr)) < 0)
1202 goto err;
1203
1204 c_str[0] = BINUNICODE;
1205 for (i = 1; i < 5; i++)
1206 c_str[i] = (int)(size >> ((i - 1) * 8));
1207 len = 5;
1208
1209 if ((*self->write_func)(self, c_str, len) < 0)
1210 goto err;
1211
1212 if (size > 128 && Pdata_Check(self->file)) {
1213 if (write_other(self, NULL, 0) < 0)
1214 goto err;
1215 PDATA_APPEND(self->file, repr, -1);
1216 }
1217 else {
1218 if ((*self->write_func)(self, PyString_AS_STRING(repr), size) < 0)
1219 goto err;
1220 }
1221
1222 Py_DECREF(repr);
1223 }
1224
1225 if (doput)
1226 if (put(self, args) < 0)
1227 return -1;
1228
1229 return 0;
1230
1231err:
1232 Py_XDECREF(repr);
1233 return -1;
1234}
1235
1236
1237static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001238save_tuple(Picklerobject *self, PyObject *args) {
1239 PyObject *element = 0, *py_tuple_id = 0;
1240 int len, i, has_key, res = -1;
1241
1242 static char tuple = TUPLE;
1243
1244 if ((*self->write_func)(self, &MARKv, 1) < 0)
1245 goto finally;
1246
1247 if ((len = PyTuple_Size(args)) < 0)
1248 goto finally;
1249
1250 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001251 UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001252 goto finally;
1253
1254 if (save(self, element, 0) < 0)
1255 goto finally;
1256 }
1257
Guido van Rossum053b8df1998-11-25 16:18:00 +00001258 UNLESS (py_tuple_id = PyInt_FromLong((long)args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001259 goto finally;
1260
1261 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001262 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001263 goto finally;
1264
1265 if (has_key) {
1266 if (self->bin) {
1267 static char pop_mark = POP_MARK;
1268
1269 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1270 goto finally;
1271 }
1272 else {
1273 static char pop = POP;
1274
1275 for (i = 0; i <= len; i++) {
1276 if ((*self->write_func)(self, &pop, 1) < 0)
1277 goto finally;
1278 }
1279 }
1280
1281 if (get(self, py_tuple_id) < 0)
1282 goto finally;
1283
1284 res = 0;
1285 goto finally;
1286 }
1287 }
1288
1289 if ((*self->write_func)(self, &tuple, 1) < 0) {
1290 goto finally;
1291 }
1292
1293 if (put(self, args) < 0)
1294 goto finally;
1295
1296 res = 0;
1297
1298finally:
1299 Py_XDECREF(py_tuple_id);
1300
1301 return res;
1302}
1303
1304static int
1305save_empty_tuple(Picklerobject *self, PyObject *args) {
1306 static char tuple = EMPTY_TUPLE;
1307
1308 return (*self->write_func)(self, &tuple, 1);
1309}
1310
1311
1312static int
1313save_list(Picklerobject *self, PyObject *args) {
1314 PyObject *element = 0;
1315 int s_len, len, i, using_appends, res = -1;
1316 char s[3];
1317
1318 static char append = APPEND, appends = APPENDS;
1319
Guido van Rossum053b8df1998-11-25 16:18:00 +00001320 if (self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001321 s[0] = EMPTY_LIST;
1322 s_len = 1;
1323 }
1324 else {
1325 s[0] = MARK;
1326 s[1] = LIST;
1327 s_len = 2;
1328 }
1329
1330 if ((len = PyList_Size(args)) < 0)
1331 goto finally;
1332
1333 if ((*self->write_func)(self, s, s_len) < 0)
1334 goto finally;
1335
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001336 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001337 if (put(self, args) < 0)
1338 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001339 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001340 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001341 if (put2(self, args) < 0)
1342 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001343 }
1344
Guido van Rossum142eeb81997-08-13 03:14:41 +00001345 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001346 if ((*self->write_func)(self, &MARKv, 1) < 0)
1347 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001348
Guido van Rossum60456fd1997-04-09 17:36:32 +00001349 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001350 UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001351 goto finally;
1352
1353 if (save(self, element, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001354 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001355
1356 if (!using_appends) {
1357 if ((*self->write_func)(self, &append, 1) < 0)
1358 goto finally;
1359 }
1360 }
1361
1362 if (using_appends) {
1363 if ((*self->write_func)(self, &appends, 1) < 0)
1364 goto finally;
1365 }
1366
1367 res = 0;
1368
1369finally:
1370
1371 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001372}
1373
1374
Guido van Rossum60456fd1997-04-09 17:36:32 +00001375static int
1376save_dict(Picklerobject *self, PyObject *args) {
1377 PyObject *key = 0, *value = 0;
1378 int i, len, res = -1, using_setitems;
1379 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001380
Guido van Rossum60456fd1997-04-09 17:36:32 +00001381 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001382
Guido van Rossum60456fd1997-04-09 17:36:32 +00001383 if (self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001384 s[0] = EMPTY_DICT;
1385 len = 1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001386 }
1387 else {
1388 s[0] = MARK;
1389 s[1] = DICT;
1390 len = 2;
1391 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001392
Guido van Rossum60456fd1997-04-09 17:36:32 +00001393 if ((*self->write_func)(self, s, len) < 0)
1394 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001395
Guido van Rossum60456fd1997-04-09 17:36:32 +00001396 if ((len = PyDict_Size(args)) < 0)
1397 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001398
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001399 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001400 if (put(self, args) < 0)
1401 goto finally;
1402 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001403 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001404 if (put2(self, args) < 0)
1405 goto finally;
1406 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001407
Guido van Rossum142eeb81997-08-13 03:14:41 +00001408 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001409 if ((*self->write_func)(self, &MARKv, 1) < 0)
1410 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001411
Guido van Rossum60456fd1997-04-09 17:36:32 +00001412 i = 0;
1413 while (PyDict_Next(args, &i, &key, &value)) {
1414 if (save(self, key, 0) < 0)
1415 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001416
Guido van Rossum60456fd1997-04-09 17:36:32 +00001417 if (save(self, value, 0) < 0)
1418 goto finally;
1419
1420 if (!using_setitems) {
1421 if ((*self->write_func)(self, &setitem, 1) < 0)
1422 goto finally;
1423 }
1424 }
1425
1426 if (using_setitems) {
1427 if ((*self->write_func)(self, &setitems, 1) < 0)
1428 goto finally;
1429 }
1430
1431 res = 0;
1432
1433finally:
1434
1435 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001436}
1437
1438
Guido van Rossum60456fd1997-04-09 17:36:32 +00001439static int
1440save_inst(Picklerobject *self, PyObject *args) {
1441 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1442 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1443 char *module_str, *name_str;
1444 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001445
Guido van Rossum60456fd1997-04-09 17:36:32 +00001446 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001447
Guido van Rossum60456fd1997-04-09 17:36:32 +00001448 if ((*self->write_func)(self, &MARKv, 1) < 0)
1449 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001450
Guido van Rossum053b8df1998-11-25 16:18:00 +00001451 UNLESS (class = PyObject_GetAttr(args, __class___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001452 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001453
Guido van Rossum60456fd1997-04-09 17:36:32 +00001454 if (self->bin) {
1455 if (save(self, class, 0) < 0)
1456 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001457 }
1458
Guido van Rossum142eeb81997-08-13 03:14:41 +00001459 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001460 PyObject *element = 0;
1461 int i, len;
1462
Guido van Rossum053b8df1998-11-25 16:18:00 +00001463 UNLESS (class_args =
Guido van Rossum60456fd1997-04-09 17:36:32 +00001464 PyObject_CallObject(getinitargs_func, empty_tuple))
1465 goto finally;
1466
1467 if ((len = PyObject_Length(class_args)) < 0)
1468 goto finally;
1469
1470 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001471 UNLESS (element = PySequence_GetItem(class_args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001472 goto finally;
1473
1474 if (save(self, element, 0) < 0) {
1475 Py_DECREF(element);
1476 goto finally;
1477 }
1478
1479 Py_DECREF(element);
1480 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001481 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001482 else {
1483 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001484 }
1485
Guido van Rossum60456fd1997-04-09 17:36:32 +00001486 if (!self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001487 UNLESS (name = ((PyClassObject *)class)->cl_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001488 PyErr_SetString(PicklingError, "class has no name");
1489 goto finally;
1490 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001491
Guido van Rossum053b8df1998-11-25 16:18:00 +00001492 UNLESS (module = whichmodule(class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001493 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001494
1495
1496 if ((module_size = PyString_Size(module)) < 0 ||
1497 (name_size = PyString_Size(name)) < 0)
1498 goto finally;
1499
Guido van Rossum60456fd1997-04-09 17:36:32 +00001500 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001501 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001502
Guido van Rossum60456fd1997-04-09 17:36:32 +00001503 if ((*self->write_func)(self, &inst, 1) < 0)
1504 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001505
Guido van Rossum60456fd1997-04-09 17:36:32 +00001506 if ((*self->write_func)(self, module_str, module_size) < 0)
1507 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001508
Guido van Rossum60456fd1997-04-09 17:36:32 +00001509 if ((*self->write_func)(self, "\n", 1) < 0)
1510 goto finally;
1511
1512 if ((*self->write_func)(self, name_str, name_size) < 0)
1513 goto finally;
1514
1515 if ((*self->write_func)(self, "\n", 1) < 0)
1516 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001517 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001518 else if ((*self->write_func)(self, &obj, 1) < 0) {
1519 goto finally;
1520 }
1521
Guido van Rossum142eeb81997-08-13 03:14:41 +00001522 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001523 UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001524 goto finally;
1525 }
1526 else {
1527 PyErr_Clear();
1528
Guido van Rossum053b8df1998-11-25 16:18:00 +00001529 UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001530 PyErr_Clear();
1531 res = 0;
1532 goto finally;
1533 }
1534 }
1535
1536 if (!PyDict_Check(state)) {
1537 if (put2(self, args) < 0)
1538 goto finally;
1539 }
1540 else {
1541 if (put(self, args) < 0)
1542 goto finally;
1543 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001544
Guido van Rossum60456fd1997-04-09 17:36:32 +00001545 if (save(self, state, 0) < 0)
1546 goto finally;
1547
1548 if ((*self->write_func)(self, &build, 1) < 0)
1549 goto finally;
1550
1551 res = 0;
1552
1553finally:
1554 Py_XDECREF(module);
1555 Py_XDECREF(class);
1556 Py_XDECREF(state);
1557 Py_XDECREF(getinitargs_func);
1558 Py_XDECREF(getstate_func);
1559 Py_XDECREF(class_args);
1560
1561 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001562}
1563
1564
Guido van Rossum60456fd1997-04-09 17:36:32 +00001565static int
1566save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1567 PyObject *global_name = 0, *module = 0;
1568 char *name_str, *module_str;
1569 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001570
Guido van Rossum60456fd1997-04-09 17:36:32 +00001571 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001572
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001573 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001574 global_name = name;
1575 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001576 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001577 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001578 UNLESS (global_name = PyObject_GetAttr(args, __name___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001579 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001580 }
1581
Guido van Rossum053b8df1998-11-25 16:18:00 +00001582 UNLESS (module = whichmodule(args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001583 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001584
Guido van Rossum053b8df1998-11-25 16:18:00 +00001585 if ((module_size = PyString_Size(module)) < 0 ||
1586 (name_size = PyString_Size(global_name)) < 0)
1587 goto finally;
1588
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001589 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001590 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001591
Guido van Rossum60456fd1997-04-09 17:36:32 +00001592 if ((*self->write_func)(self, &global, 1) < 0)
1593 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001594
Guido van Rossum60456fd1997-04-09 17:36:32 +00001595 if ((*self->write_func)(self, module_str, module_size) < 0)
1596 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001597
Guido van Rossum60456fd1997-04-09 17:36:32 +00001598 if ((*self->write_func)(self, "\n", 1) < 0)
1599 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001600
Guido van Rossum60456fd1997-04-09 17:36:32 +00001601 if ((*self->write_func)(self, name_str, name_size) < 0)
1602 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001603
Guido van Rossum60456fd1997-04-09 17:36:32 +00001604 if ((*self->write_func)(self, "\n", 1) < 0)
1605 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001606
Guido van Rossum60456fd1997-04-09 17:36:32 +00001607 if (put(self, args) < 0)
1608 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001609
Guido van Rossum60456fd1997-04-09 17:36:32 +00001610 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001611
Guido van Rossum60456fd1997-04-09 17:36:32 +00001612finally:
1613 Py_XDECREF(module);
1614 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001615
Guido van Rossum60456fd1997-04-09 17:36:32 +00001616 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001617}
1618
Guido van Rossum60456fd1997-04-09 17:36:32 +00001619static int
1620save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1621 PyObject *pid = 0;
1622 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001623
Guido van Rossum60456fd1997-04-09 17:36:32 +00001624 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001625
Guido van Rossum053b8df1998-11-25 16:18:00 +00001626 Py_INCREF(args);
1627 ARG_TUP(self, args);
1628 if (self->arg) {
1629 pid = PyObject_CallObject(f, self->arg);
1630 FREE_ARG_TUP(self);
1631 }
1632 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001633
Guido van Rossum60456fd1997-04-09 17:36:32 +00001634 if (pid != Py_None) {
1635 if (!self->bin) {
1636 if (!PyString_Check(pid)) {
1637 PyErr_SetString(PicklingError,
1638 "persistent id must be string");
1639 goto finally;
1640 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001641
Guido van Rossum60456fd1997-04-09 17:36:32 +00001642 if ((*self->write_func)(self, &persid, 1) < 0)
1643 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001644
Guido van Rossum60456fd1997-04-09 17:36:32 +00001645 if ((size = PyString_Size(pid)) < 0)
1646 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001647
Guido van Rossum60456fd1997-04-09 17:36:32 +00001648 if ((*self->write_func)(self,
1649 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1650 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001651
Guido van Rossum60456fd1997-04-09 17:36:32 +00001652 if ((*self->write_func)(self, "\n", 1) < 0)
1653 goto finally;
1654
1655 res = 1;
1656 goto finally;
1657 }
1658 else if (save(self, pid, 1) >= 0) {
1659 if ((*self->write_func)(self, &binpersid, 1) < 0)
1660 res = -1;
1661 else
1662 res = 1;
1663 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001664
Guido van Rossum60456fd1997-04-09 17:36:32 +00001665 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001666 }
1667
Guido van Rossum60456fd1997-04-09 17:36:32 +00001668 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001669
Guido van Rossum60456fd1997-04-09 17:36:32 +00001670finally:
1671 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001672
Guido van Rossum60456fd1997-04-09 17:36:32 +00001673 return res;
1674}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001675
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001676
Guido van Rossum60456fd1997-04-09 17:36:32 +00001677static int
1678save_reduce(Picklerobject *self, PyObject *callable,
1679 PyObject *tup, PyObject *state, PyObject *ob) {
1680 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001681
Guido van Rossum60456fd1997-04-09 17:36:32 +00001682 if (save(self, callable, 0) < 0)
1683 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001684
Guido van Rossum60456fd1997-04-09 17:36:32 +00001685 if (save(self, tup, 0) < 0)
1686 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001687
Guido van Rossum60456fd1997-04-09 17:36:32 +00001688 if ((*self->write_func)(self, &reduce, 1) < 0)
1689 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001690
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001691 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001692 if (state && !PyDict_Check(state)) {
1693 if (put2(self, ob) < 0)
1694 return -1;
1695 }
1696 else {
1697 if (put(self, ob) < 0)
1698 return -1;
1699 }
1700 }
1701
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001702 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001703 if (save(self, state, 0) < 0)
1704 return -1;
1705
1706 if ((*self->write_func)(self, &build, 1) < 0)
1707 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001708 }
1709
Guido van Rossum60456fd1997-04-09 17:36:32 +00001710 return 0;
1711}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001712
Guido van Rossum60456fd1997-04-09 17:36:32 +00001713static int
1714save(Picklerobject *self, PyObject *args, int pers_save) {
1715 PyTypeObject *type;
1716 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001717 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001718 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001719
Guido van Rossum60456fd1997-04-09 17:36:32 +00001720 if (!pers_save && self->pers_func) {
1721 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1722 res = tmp;
1723 goto finally;
1724 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001725 }
1726
Guido van Rossum60456fd1997-04-09 17:36:32 +00001727 if (args == Py_None) {
1728 res = save_none(self, args);
1729 goto finally;
1730 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001731
Guido van Rossum60456fd1997-04-09 17:36:32 +00001732 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001733
Guido van Rossum60456fd1997-04-09 17:36:32 +00001734 switch (type->tp_name[0]) {
1735 case 'i':
1736 if (type == &PyInt_Type) {
1737 res = save_int(self, args);
1738 goto finally;
1739 }
1740 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001741
Guido van Rossum60456fd1997-04-09 17:36:32 +00001742 case 'l':
1743 if (type == &PyLong_Type) {
1744 res = save_long(self, args);
1745 goto finally;
1746 }
1747 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001748
Guido van Rossum60456fd1997-04-09 17:36:32 +00001749 case 'f':
1750 if (type == &PyFloat_Type) {
1751 res = save_float(self, args);
1752 goto finally;
1753 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001754 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001755
Guido van Rossum60456fd1997-04-09 17:36:32 +00001756 case 't':
1757 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001758 if (self->bin) res = save_empty_tuple(self, args);
1759 else res = save_tuple(self, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001760 goto finally;
1761 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001762 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001763
Guido van Rossum60456fd1997-04-09 17:36:32 +00001764 case 's':
Guido van Rossum053b8df1998-11-25 16:18:00 +00001765 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001766 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001767 goto finally;
1768 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001769
1770 case 'u':
1771 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
1772 res = save_unicode(self, args, 0);
1773 goto finally;
1774 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001775 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001776
Guido van Rossum60456fd1997-04-09 17:36:32 +00001777 if (args->ob_refcnt > 1) {
1778 long ob_id;
1779 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001780
Guido van Rossum60456fd1997-04-09 17:36:32 +00001781 ob_id = (long)args;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001782
Guido van Rossum053b8df1998-11-25 16:18:00 +00001783 UNLESS (py_ob_id = PyInt_FromLong(ob_id))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001784 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001785
Guido van Rossum60456fd1997-04-09 17:36:32 +00001786 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1787 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001788
Guido van Rossum60456fd1997-04-09 17:36:32 +00001789 if (has_key) {
1790 if (get(self, py_ob_id) < 0)
1791 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001792
Guido van Rossum60456fd1997-04-09 17:36:32 +00001793 res = 0;
1794 goto finally;
1795 }
1796 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001797
Guido van Rossum60456fd1997-04-09 17:36:32 +00001798 switch (type->tp_name[0]) {
1799 case 's':
1800 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001801 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001802 goto finally;
1803 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001804 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001805
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001806 case 'u':
1807 if (type == &PyUnicode_Type) {
1808 res = save_unicode(self, args, 1);
1809 goto finally;
1810 }
1811 break;
1812
Guido van Rossum60456fd1997-04-09 17:36:32 +00001813 case 't':
1814 if (type == &PyTuple_Type) {
1815 res = save_tuple(self, args);
1816 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001817 }
1818 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001819
Guido van Rossum60456fd1997-04-09 17:36:32 +00001820 case 'l':
1821 if (type == &PyList_Type) {
1822 res = save_list(self, args);
1823 goto finally;
1824 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001825 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001826
1827 case 'd':
1828 if (type == &PyDict_Type) {
1829 res = save_dict(self, args);
1830 goto finally;
1831 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001832 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001833
1834 case 'i':
1835 if (type == &PyInstance_Type) {
1836 res = save_inst(self, args);
1837 goto finally;
1838 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001839 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001840
1841 case 'c':
1842 if (type == &PyClass_Type) {
1843 res = save_global(self, args, NULL);
1844 goto finally;
1845 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001846 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001847
1848 case 'f':
1849 if (type == &PyFunction_Type) {
1850 res = save_global(self, args, NULL);
1851 goto finally;
1852 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001853 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001854
1855 case 'b':
1856 if (type == &PyCFunction_Type) {
1857 res = save_global(self, args, NULL);
1858 goto finally;
1859 }
1860 }
1861
1862 if (!pers_save && self->inst_pers_func) {
1863 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1864 res = tmp;
1865 goto finally;
1866 }
1867 }
1868
Guido van Rossum142eeb81997-08-13 03:14:41 +00001869 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001870 Py_INCREF(__reduce__);
1871
Guido van Rossum60456fd1997-04-09 17:36:32 +00001872 Py_INCREF(args);
Guido van Rossum053b8df1998-11-25 16:18:00 +00001873 ARG_TUP(self, args);
1874 if (self->arg) {
1875 t = PyObject_CallObject(__reduce__, self->arg);
1876 FREE_ARG_TUP(self);
1877 }
1878 if (! t) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001879 }
1880 else {
1881 PyErr_Clear();
1882
Guido van Rossum142eeb81997-08-13 03:14:41 +00001883 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001884 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001885 goto finally;
1886 }
1887 else {
1888 PyErr_Clear();
1889 }
1890 }
1891
1892 if (t) {
1893 if (PyString_Check(t)) {
1894 res = save_global(self, args, t);
1895 goto finally;
1896 }
1897
1898 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001899 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001900 "be a tuple", "O", __reduce__);
1901 goto finally;
1902 }
1903
1904 size = PyTuple_Size(t);
1905
1906 if ((size != 3) && (size != 2)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001907 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001908 "contain only two or three elements", "O", __reduce__);
1909 goto finally;
1910 }
1911
1912 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001913
Guido van Rossum60456fd1997-04-09 17:36:32 +00001914 arg_tup = PyTuple_GET_ITEM(t, 1);
1915
1916 if (size > 2) {
1917 state = PyTuple_GET_ITEM(t, 2);
1918 }
1919
Guido van Rossum053b8df1998-11-25 16:18:00 +00001920 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001921 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001922 "returned by %s must be a tuple", "O", __reduce__);
1923 goto finally;
1924 }
1925
1926 res = save_reduce(self, callable, arg_tup, state, args);
1927 goto finally;
1928 }
1929
Guido van Rossumc03158b1999-06-09 15:23:31 +00001930 PyErr_SetObject(UnpickleableError, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001931
1932finally:
1933 Py_XDECREF(py_ob_id);
1934 Py_XDECREF(__reduce__);
1935 Py_XDECREF(t);
1936
1937 return res;
1938}
1939
1940
1941static int
1942dump(Picklerobject *self, PyObject *args) {
1943 static char stop = STOP;
1944
1945 if (save(self, args, 0) < 0)
1946 return -1;
1947
1948 if ((*self->write_func)(self, &stop, 1) < 0)
1949 return -1;
1950
1951 if ((*self->write_func)(self, NULL, 0) < 0)
1952 return -1;
1953
1954 return 0;
1955}
1956
1957static PyObject *
Guido van Rossum053b8df1998-11-25 16:18:00 +00001958Pickle_clear_memo(Picklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00001959 if (args && ! PyArg_ParseTuple(args,":clear_memo")) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001960 if (self->memo) PyDict_Clear(self->memo);
1961 Py_INCREF(Py_None);
1962 return Py_None;
1963}
1964
1965static PyObject *
1966Pickle_getvalue(Picklerobject *self, PyObject *args) {
1967 int l, i, rsize, ssize, clear=1, lm;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001968 long ik;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001969 PyObject *k, *r;
1970 char *s, *p, *have_get;
1971 Pdata *data;
1972
Guido van Rossum43713e52000-02-29 13:59:29 +00001973 if (args && ! PyArg_ParseTuple(args,"|i:getvalue",&clear)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001974
1975 /* Check to make sure we are based on a list */
1976 if (! Pdata_Check(self->file)) {
1977 PyErr_SetString(PicklingError,
1978 "Attempt to getvalue a non-list-based pickler");
1979 return NULL;
1980 }
1981
1982 /* flush write buffer */
1983 if (write_other(self, NULL, 0) < 0) return NULL;
1984
1985 data=(Pdata*)self->file;
1986 l=data->length;
1987
1988 /* set up an array to hold get/put status */
1989 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
1990 lm++;
1991 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
1992 memset(have_get,0,lm);
1993
1994 /* Scan for gets. */
1995 for (rsize=0, i=l; --i >= 0; ) {
1996 k=data->data[i];
1997
1998 if (PyString_Check(k)) {
1999 rsize += PyString_GET_SIZE(k);
2000 }
2001
2002 else if (PyInt_Check(k)) { /* put */
2003 ik=PyInt_AS_LONG((PyIntObject*)k);
2004 if (ik >= lm || ik==0) {
2005 PyErr_SetString(PicklingError,
2006 "Invalid get data");
2007 return NULL;
2008 }
2009 if (have_get[ik]) { /* with matching get */
2010 if (ik < 256) rsize += 2;
2011 else rsize+=5;
2012 }
2013 }
2014
2015 else if (! (PyTuple_Check(k) &&
2016 PyTuple_GET_SIZE(k) == 2 &&
2017 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
2018 ) {
2019 PyErr_SetString(PicklingError,
2020 "Unexpected data in internal list");
2021 return NULL;
2022 }
2023
2024 else { /* put */
2025 ik=PyInt_AS_LONG((PyIntObject*)k);
2026 if (ik >= lm || ik==0) {
2027 PyErr_SetString(PicklingError,
2028 "Invalid get data");
2029 return NULL;
2030 }
2031 have_get[ik]=1;
2032 if (ik < 256) rsize += 2;
2033 else rsize+=5;
2034 }
2035
2036 }
2037
2038 /* Now generate the result */
2039 UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
2040 s=PyString_AS_STRING((PyStringObject*)r);
2041
2042 for (i=0; i<l; i++) {
2043 k=data->data[i];
2044
2045 if (PyString_Check(k)) {
2046 ssize=PyString_GET_SIZE(k);
2047 if (ssize) {
2048 p=PyString_AS_STRING((PyStringObject*)k);
2049 while (--ssize >= 0) *s++=*p++;
2050 }
2051 }
2052
2053 else if (PyTuple_Check(k)) { /* get */
2054 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
2055 if (ik < 256) {
2056 *s++ = BINGET;
2057 *s++ = (int)(ik & 0xff);
2058 }
2059 else {
2060 *s++ = LONG_BINGET;
2061 *s++ = (int)(ik & 0xff);
2062 *s++ = (int)((ik >> 8) & 0xff);
2063 *s++ = (int)((ik >> 16) & 0xff);
2064 *s++ = (int)((ik >> 24) & 0xff);
2065 }
2066 }
2067
2068 else { /* put */
2069 ik=PyInt_AS_LONG((PyIntObject*)k);
2070
2071 if (have_get[ik]) { /* with matching get */
2072 if (ik < 256) {
2073 *s++ = BINPUT;
2074 *s++ = (int)(ik & 0xff);
2075 }
2076 else {
2077 *s++ = LONG_BINPUT;
2078 *s++ = (int)(ik & 0xff);
2079 *s++ = (int)((ik >> 8) & 0xff);
2080 *s++ = (int)((ik >> 16) & 0xff);
2081 *s++ = (int)((ik >> 24) & 0xff);
2082 }
2083 }
2084 }
2085
2086 }
2087
2088 if (clear) {
2089 PyDict_Clear(self->memo);
2090 Pdata_clear(data,0);
2091 }
2092
2093 free(have_get);
2094 return r;
2095err:
2096 free(have_get);
2097 return NULL;
2098}
2099
2100static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002101Pickler_dump(Picklerobject *self, PyObject *args) {
2102 PyObject *ob;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002103 int get=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002104
Guido van Rossum43713e52000-02-29 13:59:29 +00002105 UNLESS (PyArg_ParseTuple(args, "O|i:dump", &ob, &get))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002106 return NULL;
2107
2108 if (dump(self, ob) < 0)
2109 return NULL;
2110
Guido van Rossum053b8df1998-11-25 16:18:00 +00002111 if (get) return Pickle_getvalue(self, NULL);
2112
2113 Py_INCREF(self);
2114 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002115}
2116
2117
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002118static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00002119 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002120 "dump(object) --"
2121 "Write an object in pickle format to the object's pickle stream\n"
2122 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00002123 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002124 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum053b8df1998-11-25 16:18:00 +00002125 {"getvalue", (PyCFunction)Pickle_getvalue, 1,
2126 "getvalue() -- Finish picking a list-based pickle"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002127 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002128};
2129
2130
2131static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002132newPicklerobject(PyObject *file, int bin) {
2133 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002134
Guido van Rossum053b8df1998-11-25 16:18:00 +00002135 UNLESS (self = PyObject_NEW(Picklerobject, &Picklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002136 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002137
2138 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002139 self->write = NULL;
2140 self->memo = NULL;
2141 self->arg = NULL;
2142 self->pers_func = NULL;
2143 self->inst_pers_func = NULL;
2144 self->write_buf = NULL;
2145 self->bin = bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002146 self->fast = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002147 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002148 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002149
Guido van Rossum053b8df1998-11-25 16:18:00 +00002150 if (file)
2151 Py_INCREF(file);
2152 else
Guido van Rossum50f385c1998-12-04 18:48:44 +00002153 file=Pdata_New();
Guido van Rossum83addc72000-04-21 20:49:36 +00002154
2155 UNLESS (self->file = file)
2156 goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002157
Guido van Rossum83addc72000-04-21 20:49:36 +00002158 UNLESS (self->memo = PyDict_New())
2159 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002160
Guido van Rossum60456fd1997-04-09 17:36:32 +00002161 if (PyFile_Check(file)) {
2162 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002163 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00002164 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
2165 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002166 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002167 self->write_func = write_file;
2168 }
2169 else if (PycStringIO_OutputCheck(file)) {
2170 self->write_func = write_cStringIO;
2171 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002172 else if (file == Py_None) {
2173 self->write_func = write_none;
2174 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002175 else {
2176 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002177
Guido van Rossum053b8df1998-11-25 16:18:00 +00002178 if (! Pdata_Check(file)) {
2179 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002180 PyErr_Clear();
2181 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002182 "attribute");
2183 goto err;
2184 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002185 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002186
Guido van Rossum053b8df1998-11-25 16:18:00 +00002187 UNLESS (self->write_buf =
Guido van Rossum60456fd1997-04-09 17:36:32 +00002188 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
2189 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002190 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002191 }
2192 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002193
Guido van Rossum053b8df1998-11-25 16:18:00 +00002194 if (PyEval_GetRestricted()) {
2195 /* Restricted execution, get private tables */
2196 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002197
Guido van Rossum053b8df1998-11-25 16:18:00 +00002198 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2199 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2200 Py_DECREF(m);
2201 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002202 }
2203 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002204 self->dispatch_table=dispatch_table;
2205 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002206 }
2207
Guido van Rossum60456fd1997-04-09 17:36:32 +00002208 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002209
2210err:
2211 Py_DECREF((PyObject *)self);
2212 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002213}
2214
2215
2216static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002217get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002218 PyObject *file=NULL;
2219 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002220
Guido van Rossum053b8df1998-11-25 16:18:00 +00002221 bin=1;
Guido van Rossum43713e52000-02-29 13:59:29 +00002222 if (! PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002223 PyErr_Clear();
2224 bin=0;
Guido van Rossum43713e52000-02-29 13:59:29 +00002225 if (! PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002226 return NULL;
2227 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002228 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002229}
2230
2231
2232static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002233Pickler_dealloc(Picklerobject *self) {
2234 Py_XDECREF(self->write);
2235 Py_XDECREF(self->memo);
2236 Py_XDECREF(self->arg);
2237 Py_XDECREF(self->file);
2238 Py_XDECREF(self->pers_func);
2239 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002240 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002241
2242 if (self->write_buf) {
2243 free(self->write_buf);
2244 }
2245
2246 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002247}
2248
2249
2250static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002251Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002252
2253 switch (*name) {
2254 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002255 if (strcmp(name, "persistent_id") == 0) {
2256 if (!self->pers_func) {
2257 PyErr_SetString(PyExc_AttributeError, name);
2258 return NULL;
2259 }
2260
2261 Py_INCREF(self->pers_func);
2262 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002263 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002264 break;
2265 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002266 if (strcmp(name, "memo") == 0) {
2267 if (!self->memo) {
2268 PyErr_SetString(PyExc_AttributeError, name);
2269 return NULL;
2270 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002271
Guido van Rossum60456fd1997-04-09 17:36:32 +00002272 Py_INCREF(self->memo);
2273 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002274 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002275 break;
2276 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002277 if (strcmp(name, "PicklingError") == 0) {
2278 Py_INCREF(PicklingError);
2279 return PicklingError;
2280 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002281 break;
2282 case 'b':
2283 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002284 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002285 break;
2286 case 'f':
2287 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002288 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002289 break;
2290 case 'g':
2291 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2292 PyErr_SetString(PyExc_AttributeError, name);
2293 return NULL;
2294 }
2295 break;
2296 }
2297 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002298}
2299
2300
2301int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002302Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002303
Guido van Rossum053b8df1998-11-25 16:18:00 +00002304 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002305 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002306 "attribute deletion is not supported");
2307 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002308 }
2309
Guido van Rossum60456fd1997-04-09 17:36:32 +00002310 if (strcmp(name, "persistent_id") == 0) {
2311 Py_XDECREF(self->pers_func);
2312 self->pers_func = value;
2313 Py_INCREF(value);
2314 return 0;
2315 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002316
Guido van Rossum60456fd1997-04-09 17:36:32 +00002317 if (strcmp(name, "inst_persistent_id") == 0) {
2318 Py_XDECREF(self->inst_pers_func);
2319 self->inst_pers_func = value;
2320 Py_INCREF(value);
2321 return 0;
2322 }
2323
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002324 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002325 if (! PyDict_Check(value)) {
2326 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2327 return -1;
2328 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002329 Py_XDECREF(self->memo);
2330 self->memo = value;
2331 Py_INCREF(value);
2332 return 0;
2333 }
2334
Guido van Rossum053b8df1998-11-25 16:18:00 +00002335 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002336 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002337 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002338 }
2339
Guido van Rossum053b8df1998-11-25 16:18:00 +00002340 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002341 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002342 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002343 }
2344
Guido van Rossum60456fd1997-04-09 17:36:32 +00002345 PyErr_SetString(PyExc_AttributeError, name);
2346 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002347}
2348
2349
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002350static char Picklertype__doc__[] =
2351"Objects that know how to pickle objects\n"
2352;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002353
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002354static PyTypeObject Picklertype = {
2355 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002356 0, /*ob_size*/
2357 "Pickler", /*tp_name*/
2358 sizeof(Picklerobject), /*tp_basicsize*/
2359 0, /*tp_itemsize*/
2360 /* methods */
2361 (destructor)Pickler_dealloc, /*tp_dealloc*/
2362 (printfunc)0, /*tp_print*/
2363 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2364 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2365 (cmpfunc)0, /*tp_compare*/
2366 (reprfunc)0, /*tp_repr*/
2367 0, /*tp_as_number*/
2368 0, /*tp_as_sequence*/
2369 0, /*tp_as_mapping*/
2370 (hashfunc)0, /*tp_hash*/
2371 (ternaryfunc)0, /*tp_call*/
2372 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002373
Guido van Rossum60456fd1997-04-09 17:36:32 +00002374 /* Space for future expansion */
2375 0L,0L,0L,0L,
2376 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002377};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002378
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002379static PyObject *
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002380find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002381 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002382
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002383 if (fc) {
2384 if (fc==Py_None) {
2385 PyErr_SetString(UnpicklingError,
2386 "Global and instance pickles are not supported.");
2387 return NULL;
2388 }
2389 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2390 }
2391
Jeremy Hyltond1055231998-08-11 19:52:51 +00002392 module = PySys_GetObject("modules");
2393 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002394 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002395
2396 module = PyDict_GetItem(module, py_module_name);
2397 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002398 module = PyImport_Import(py_module_name);
2399 if (!module)
2400 return NULL;
2401 global = PyObject_GetAttr(module, py_global_name);
2402 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002403 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002404 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002405 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002406 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002407 char buf[256 + 37];
2408 sprintf(buf, "Failed to import class %.128s from module %.128s",
2409 PyString_AS_STRING((PyStringObject*)py_global_name),
2410 PyString_AS_STRING((PyStringObject*)py_module_name));
2411 PyErr_SetString(PyExc_SystemError, buf);
2412 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002413 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002414 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002415}
2416
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002417static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002418marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002419 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002420 PyErr_SetString(UnpicklingError, "could not find MARK");
2421 return -1;
2422 }
2423
2424 return self->marks[--self->num_marks];
2425}
2426
2427
2428static int
2429load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002430 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002431 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002432}
2433
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002434static int
2435bad_readline() {
2436 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2437 return -1;
2438}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002439
2440static int
2441load_int(Unpicklerobject *self) {
2442 PyObject *py_int = 0;
2443 char *endptr, *s;
2444 int len, res = -1;
2445 long l;
2446
2447 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002448 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002449 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002450
2451 errno = 0;
2452 l = strtol(s, &endptr, 0);
2453
2454 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2455 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002456 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002457 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002458 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002459
Guido van Rossum053b8df1998-11-25 16:18:00 +00002460 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2461 PyErr_SetString(PyExc_ValueError,
2462 "could not convert string to int");
2463 goto finally;
2464 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002465 }
2466 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002467 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002468 }
2469
Guido van Rossum053b8df1998-11-25 16:18:00 +00002470 free(s);
2471 PDATA_PUSH(self->stack, py_int, -1);
2472 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002473
2474finally:
2475 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002476
2477 return res;
2478}
2479
2480
2481static long
2482calc_binint(char *s, int x) {
2483 unsigned char c;
2484 int i;
2485 long l;
2486
2487 for (i = 0, l = 0L; i < x; i++) {
2488 c = (unsigned char)s[i];
2489 l |= (long)c << (i * 8);
2490 }
2491
2492 return l;
2493}
2494
2495
2496static int
2497load_binintx(Unpicklerobject *self, char *s, int x) {
2498 PyObject *py_int = 0;
2499 long l;
2500
2501 l = calc_binint(s, x);
2502
Guido van Rossum053b8df1998-11-25 16:18:00 +00002503 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002504 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002505
Guido van Rossum053b8df1998-11-25 16:18:00 +00002506 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002507 return 0;
2508}
2509
2510
2511static int
2512load_binint(Unpicklerobject *self) {
2513 char *s;
2514
2515 if ((*self->read_func)(self, &s, 4) < 0)
2516 return -1;
2517
2518 return load_binintx(self, s, 4);
2519}
2520
2521
2522static int
2523load_binint1(Unpicklerobject *self) {
2524 char *s;
2525
2526 if ((*self->read_func)(self, &s, 1) < 0)
2527 return -1;
2528
2529 return load_binintx(self, s, 1);
2530}
2531
2532
2533static int
2534load_binint2(Unpicklerobject *self) {
2535 char *s;
2536
2537 if ((*self->read_func)(self, &s, 2) < 0)
2538 return -1;
2539
2540 return load_binintx(self, s, 2);
2541}
2542
2543static int
2544load_long(Unpicklerobject *self) {
2545 PyObject *l = 0;
2546 char *end, *s;
2547 int len, res = -1;
2548
Guido van Rossum60456fd1997-04-09 17:36:32 +00002549 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002550 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002551 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002552
Guido van Rossum053b8df1998-11-25 16:18:00 +00002553 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002554 goto finally;
2555
Guido van Rossum053b8df1998-11-25 16:18:00 +00002556 free(s);
2557 PDATA_PUSH(self->stack, l, -1);
2558 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002559
2560finally:
2561 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002562
2563 return res;
2564}
2565
2566
2567static int
2568load_float(Unpicklerobject *self) {
2569 PyObject *py_float = 0;
2570 char *endptr, *s;
2571 int len, res = -1;
2572 double d;
2573
2574 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002575 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002576 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002577
2578 errno = 0;
2579 d = strtod(s, &endptr);
2580
2581 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2582 PyErr_SetString(PyExc_ValueError,
2583 "could not convert string to float");
2584 goto finally;
2585 }
2586
Guido van Rossum053b8df1998-11-25 16:18:00 +00002587 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002588 goto finally;
2589
Guido van Rossum053b8df1998-11-25 16:18:00 +00002590 free(s);
2591 PDATA_PUSH(self->stack, py_float, -1);
2592 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002593
2594finally:
2595 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002596
2597 return res;
2598}
2599
Guido van Rossum60456fd1997-04-09 17:36:32 +00002600static int
2601load_binfloat(Unpicklerobject *self) {
2602 PyObject *py_float = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002603 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002604 long fhi, flo;
2605 double x;
2606 char *p;
2607
2608 if ((*self->read_func)(self, &p, 8) < 0)
2609 return -1;
2610
2611 /* First byte */
2612 s = (*p>>7) & 1;
2613 e = (*p & 0x7F) << 4;
2614 p++;
2615
2616 /* Second byte */
2617 e |= (*p>>4) & 0xF;
2618 fhi = (*p & 0xF) << 24;
2619 p++;
2620
2621 /* Third byte */
2622 fhi |= (*p & 0xFF) << 16;
2623 p++;
2624
2625 /* Fourth byte */
2626 fhi |= (*p & 0xFF) << 8;
2627 p++;
2628
2629 /* Fifth byte */
2630 fhi |= *p & 0xFF;
2631 p++;
2632
2633 /* Sixth byte */
2634 flo = (*p & 0xFF) << 16;
2635 p++;
2636
2637 /* Seventh byte */
2638 flo |= (*p & 0xFF) << 8;
2639 p++;
2640
2641 /* Eighth byte */
2642 flo |= *p & 0xFF;
2643
2644 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2645 x /= 268435456.0; /* 2**28 */
2646
2647 /* XXX This sadly ignores Inf/NaN */
2648 if (e == 0)
2649 e = -1022;
2650 else {
2651 x += 1.0;
2652 e -= 1023;
2653 }
2654 x = ldexp(x, e);
2655
2656 if (s)
2657 x = -x;
2658
Guido van Rossum053b8df1998-11-25 16:18:00 +00002659 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002660
Guido van Rossum053b8df1998-11-25 16:18:00 +00002661 PDATA_PUSH(self->stack, py_float, -1);
2662 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002663}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002664
2665static int
2666load_string(Unpicklerobject *self) {
2667 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002668 int len, res = -1, nslash;
2669 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002670
2671 static PyObject *eval_dict = 0;
2672
2673 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002674 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002675 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002676
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002677 /* Check for unquoted quotes (evil strings) */
2678 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002679 if (q != '"' && q != '\'') goto insecure;
2680 for (p=s+1, nslash=0; *p; p++) {
2681 if (*p==q && nslash%2==0) break;
2682 if (*p=='\\') nslash++;
2683 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002684 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002685 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002686 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002687 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002688 }
2689 else goto insecure;
2690 /********************************************/
2691
Guido van Rossum053b8df1998-11-25 16:18:00 +00002692 UNLESS (eval_dict)
2693 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002694 goto finally;
2695
Guido van Rossum053b8df1998-11-25 16:18:00 +00002696 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002697 goto finally;
2698
Guido van Rossum053b8df1998-11-25 16:18:00 +00002699 free(s);
2700 PDATA_PUSH(self->stack, str, -1);
2701 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002702
2703finally:
2704 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002705
2706 return res;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002707
2708insecure:
2709 free(s);
2710 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2711 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002712}
2713
2714
2715static int
2716load_binstring(Unpicklerobject *self) {
2717 PyObject *py_string = 0;
2718 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002719 char *s;
2720
Guido van Rossum053b8df1998-11-25 16:18:00 +00002721 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002722
2723 l = calc_binint(s, 4);
2724
2725 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002726 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002727
Guido van Rossum053b8df1998-11-25 16:18:00 +00002728 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2729 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002730
Guido van Rossum053b8df1998-11-25 16:18:00 +00002731 PDATA_PUSH(self->stack, py_string, -1);
2732 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002733}
2734
2735
2736static int
2737load_short_binstring(Unpicklerobject *self) {
2738 PyObject *py_string = 0;
2739 unsigned char l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002740 char *s;
2741
2742 if ((*self->read_func)(self, &s, 1) < 0)
2743 return -1;
2744
2745 l = (unsigned char)s[0];
2746
Guido van Rossum053b8df1998-11-25 16:18:00 +00002747 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002748
Guido van Rossum053b8df1998-11-25 16:18:00 +00002749 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002750
Guido van Rossum053b8df1998-11-25 16:18:00 +00002751 PDATA_PUSH(self->stack, py_string, -1);
2752 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002753}
2754
2755
2756static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002757load_unicode(Unpicklerobject *self) {
2758 PyObject *str = 0;
2759 int len, res = -1;
2760 char *s;
2761
2762 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2763 if (len < 2) return bad_readline();
2764
2765 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
2766 goto finally;
2767
2768 PDATA_PUSH(self->stack, str, -1);
2769 return 0;
2770
2771finally:
2772 return res;
2773}
2774
2775
2776static int
2777load_binunicode(Unpicklerobject *self) {
2778 PyObject *unicode;
2779 long l;
2780 char *s;
2781
2782 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2783
2784 l = calc_binint(s, 4);
2785
2786 if ((*self->read_func)(self, &s, l) < 0)
2787 return -1;
2788
2789 UNLESS (unicode = PyUnicode_DecodeUTF8(s, l, NULL))
2790 return -1;
2791
2792 PDATA_PUSH(self->stack, unicode, -1);
2793 return 0;
2794}
2795
2796
2797static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002798load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002799 PyObject *tup;
2800 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002801
Guido van Rossum053b8df1998-11-25 16:18:00 +00002802 if ((i = marker(self)) < 0) return -1;
2803 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2804 PDATA_PUSH(self->stack, tup, -1);
2805 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002806}
2807
2808static int
2809load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002810 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002811
Guido van Rossum053b8df1998-11-25 16:18:00 +00002812 UNLESS (tup=PyTuple_New(0)) return -1;
2813 PDATA_PUSH(self->stack, tup, -1);
2814 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002815}
2816
2817static int
2818load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002819 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002820
Guido van Rossum053b8df1998-11-25 16:18:00 +00002821 UNLESS (list=PyList_New(0)) return -1;
2822 PDATA_PUSH(self->stack, list, -1);
2823 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002824}
2825
2826static int
2827load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002828 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002829
Guido van Rossum053b8df1998-11-25 16:18:00 +00002830 UNLESS (dict=PyDict_New()) return -1;
2831 PDATA_PUSH(self->stack, dict, -1);
2832 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002833}
2834
2835
2836static int
2837load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002838 PyObject *list = 0;
2839 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002840
Guido van Rossum053b8df1998-11-25 16:18:00 +00002841 if ((i = marker(self)) < 0) return -1;
2842 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2843 PDATA_PUSH(self->stack, list, -1);
2844 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002845}
2846
2847static int
2848load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002849 PyObject *dict, *key, *value;
2850 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002851
Guido van Rossum053b8df1998-11-25 16:18:00 +00002852 if ((i = marker(self)) < 0) return -1;
2853 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002854
Guido van Rossum053b8df1998-11-25 16:18:00 +00002855 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002856
Guido van Rossum053b8df1998-11-25 16:18:00 +00002857 for (k = i+1; k < j; k += 2) {
2858 key =self->stack->data[k-1];
2859 value=self->stack->data[k ];
2860 if (PyDict_SetItem(dict, key, value) < 0) {
2861 Py_DECREF(dict);
2862 return -1;
2863 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002864 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002865 Pdata_clear(self->stack, i);
2866 PDATA_PUSH(self->stack, dict, -1);
2867 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002868}
2869
2870static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002871Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002872 int has_key;
2873 PyObject *safe=0, *r=0;
2874
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002875 if (PyClass_Check(cls)) {
2876 int l;
2877
Guido van Rossum053b8df1998-11-25 16:18:00 +00002878 if ((l=PyObject_Length(args)) < 0) goto err;
2879 UNLESS (l) {
2880 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002881
Guido van Rossum053b8df1998-11-25 16:18:00 +00002882 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2883 /* We have a class with no __getinitargs__, so bypass usual
2884 construction */
2885 PyInstanceObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002886
Guido van Rossum053b8df1998-11-25 16:18:00 +00002887 PyErr_Clear();
2888 UNLESS (inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
2889 goto err;
2890 inst->in_class=(PyClassObject*)cls;
2891 Py_INCREF(cls);
2892 UNLESS (inst->in_dict=PyDict_New()) {
2893 Py_DECREF(inst);
2894 goto err;
2895 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002896
Guido van Rossum053b8df1998-11-25 16:18:00 +00002897 return (PyObject *)inst;
2898 }
2899 Py_DECREF(__getinitargs__);
2900 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002901
Guido van Rossum053b8df1998-11-25 16:18:00 +00002902 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002903 else goto err;
2904 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002905
2906
2907 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2908 goto err;
2909
2910 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002911 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002912 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002913 cPickle_ErrFormat(UnpicklingError,
2914 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002915 Py_XDECREF(safe);
2916 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002917 }
2918
Guido van Rossum053b8df1998-11-25 16:18:00 +00002919 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002920 /* Special case, call cls.__basicnew__() */
2921 PyObject *basicnew;
2922
Guido van Rossum053b8df1998-11-25 16:18:00 +00002923 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002924 r=PyObject_CallObject(basicnew, NULL);
2925 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002926 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002927 }
2928
Guido van Rossum053b8df1998-11-25 16:18:00 +00002929 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002930
Guido van Rossum60456fd1997-04-09 17:36:32 +00002931err:
2932 {
2933 PyObject *tp, *v, *tb;
2934
2935 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002936 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2937 Py_XDECREF(v);
2938 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002939 }
2940 PyErr_Restore(tp,v,tb);
2941 }
2942 return NULL;
2943}
2944
2945
2946static int
2947load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002948 PyObject *class, *tup, *obj=0;
2949 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002950
Guido van Rossum053b8df1998-11-25 16:18:00 +00002951 if ((i = marker(self)) < 0) return -1;
2952 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
2953 PDATA_POP(self->stack, class);
2954 if (class) {
2955 obj = Instance_New(class, tup);
2956 Py_DECREF(class);
2957 }
2958 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002959
Guido van Rossum053b8df1998-11-25 16:18:00 +00002960 if (! obj) return -1;
2961 PDATA_PUSH(self->stack, obj, -1);
2962 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002963}
2964
2965
2966static int
2967load_inst(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002968 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002969 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002970 char *s;
2971
Guido van Rossum053b8df1998-11-25 16:18:00 +00002972 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002973
Guido van Rossum053b8df1998-11-25 16:18:00 +00002974 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002975 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002976 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002977
Guido van Rossum053b8df1998-11-25 16:18:00 +00002978 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002979 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002980 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002981 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002982 Py_DECREF(class_name);
2983 }
2984 }
2985 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002986
Guido van Rossum053b8df1998-11-25 16:18:00 +00002987 if (! class) return -1;
2988
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002989 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002990 obj = Instance_New(class, tup);
2991 Py_DECREF(tup);
2992 }
2993 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002994
Guido van Rossum053b8df1998-11-25 16:18:00 +00002995 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002996
Guido van Rossum053b8df1998-11-25 16:18:00 +00002997 PDATA_PUSH(self->stack, obj, -1);
2998 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002999}
3000
3001
3002static int
3003load_global(Unpicklerobject *self) {
3004 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003005 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003006 char *s;
3007
Guido van Rossum053b8df1998-11-25 16:18:00 +00003008 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003009 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003010 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003011
Guido van Rossum053b8df1998-11-25 16:18:00 +00003012 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003013 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003014 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003015 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003016 Py_DECREF(class_name);
3017 }
3018 }
3019 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003020
Guido van Rossum053b8df1998-11-25 16:18:00 +00003021 if (! class) return -1;
3022 PDATA_PUSH(self->stack, class, -1);
3023 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003024}
3025
3026
3027static int
3028load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003029 PyObject *pid = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003030 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003031 char *s;
3032
3033 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003034 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003035 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003036
Guido van Rossum053b8df1998-11-25 16:18:00 +00003037 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003038
Guido van Rossum053b8df1998-11-25 16:18:00 +00003039 if (PyList_Check(self->pers_func)) {
3040 if (PyList_Append(self->pers_func, pid) < 0) {
3041 Py_DECREF(pid);
3042 return -1;
3043 }
3044 }
3045 else {
3046 ARG_TUP(self, pid);
3047 if (self->arg) {
3048 pid = PyObject_CallObject(self->pers_func, self->arg);
3049 FREE_ARG_TUP(self);
3050 }
3051 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003052
Guido van Rossum053b8df1998-11-25 16:18:00 +00003053 if (! pid) return -1;
3054
3055 PDATA_PUSH(self->stack, pid, -1);
3056 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003057 }
3058 else {
3059 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003060 "A load persistent id instruction was encountered,\n"
3061 "but no persistent_load function was specified.");
3062 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003063 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003064}
3065
Guido van Rossum60456fd1997-04-09 17:36:32 +00003066static int
3067load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003068 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003069
3070 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003071 PDATA_POP(self->stack, pid);
3072 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003073
Guido van Rossum053b8df1998-11-25 16:18:00 +00003074 if (PyList_Check(self->pers_func)) {
3075 if (PyList_Append(self->pers_func, pid) < 0) {
3076 Py_DECREF(pid);
3077 return -1;
3078 }
3079 }
3080 else {
3081 ARG_TUP(self, pid);
3082 if (self->arg) {
3083 pid = PyObject_CallObject(self->pers_func, self->arg);
3084 FREE_ARG_TUP(self);
3085 }
3086 if (! pid) return -1;
3087 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003088
Guido van Rossum053b8df1998-11-25 16:18:00 +00003089 PDATA_PUSH(self->stack, pid, -1);
3090 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003091 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003092 else {
3093 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003094 "A load persistent id instruction was encountered,\n"
3095 "but no persistent_load function was specified.");
3096 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003097 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003098}
3099
3100
3101static int
3102load_pop(Unpicklerobject *self) {
3103 int len;
3104
Guido van Rossum053b8df1998-11-25 16:18:00 +00003105 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003106
3107 if ((self->num_marks > 0) &&
3108 (self->marks[self->num_marks - 1] == len))
3109 self->num_marks--;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003110 else
3111 Py_DECREF(self->stack->data[--(self->stack->length)]);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003112
3113 return 0;
3114}
3115
3116
3117static int
3118load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003119 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003120
3121 if ((i = marker(self)) < 0)
3122 return -1;
3123
Guido van Rossum053b8df1998-11-25 16:18:00 +00003124 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003125
3126 return 0;
3127}
3128
3129
3130static int
3131load_dup(Unpicklerobject *self) {
3132 PyObject *last;
3133 int len;
3134
Guido van Rossum053b8df1998-11-25 16:18:00 +00003135 if ((len = self->stack->length) <= 0) return stackUnderflow();
3136 last=self->stack->data[len-1];
3137 Py_INCREF(last);
3138 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003139 return 0;
3140}
3141
3142
3143static int
3144load_get(Unpicklerobject *self) {
3145 PyObject *py_str = 0, *value = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003146 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003147 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003148 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003149
Guido van Rossum053b8df1998-11-25 16:18:00 +00003150 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003151 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003152
Guido van Rossum053b8df1998-11-25 16:18:00 +00003153 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3154
3155 value = PyDict_GetItem(self->memo, py_str);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003156 if (! value) {
3157 PyErr_SetObject(BadPickleGet, py_str);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003158 rc = -1;
3159 } else {
3160 PDATA_APPEND(self->stack, value, -1);
3161 rc = 0;
3162 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003163
Guido van Rossum2f80d961999-07-13 15:18:58 +00003164 Py_DECREF(py_str);
3165 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003166}
3167
3168
3169static int
3170load_binget(Unpicklerobject *self) {
3171 PyObject *py_key = 0, *value = 0;
3172 unsigned char key;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003173 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003174 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003175
Guido van Rossum053b8df1998-11-25 16:18:00 +00003176 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003177
3178 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003179 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3180
3181 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003182 if (! value) {
3183 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003184 rc = -1;
3185 } else {
3186 PDATA_APPEND(self->stack, value, -1);
3187 rc = 0;
3188 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003189
Guido van Rossum2f80d961999-07-13 15:18:58 +00003190 Py_DECREF(py_key);
3191 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003192}
3193
3194
3195static int
3196load_long_binget(Unpicklerobject *self) {
3197 PyObject *py_key = 0, *value = 0;
3198 unsigned char c, *s;
3199 long key;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003200 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003201
Guido van Rossum053b8df1998-11-25 16:18:00 +00003202 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003203
3204 c = (unsigned char)s[0];
3205 key = (long)c;
3206 c = (unsigned char)s[1];
3207 key |= (long)c << 8;
3208 c = (unsigned char)s[2];
3209 key |= (long)c << 16;
3210 c = (unsigned char)s[3];
3211 key |= (long)c << 24;
3212
Guido van Rossum053b8df1998-11-25 16:18:00 +00003213 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3214
3215 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003216 if (! value) {
3217 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003218 rc = -1;
3219 } else {
3220 PDATA_APPEND(self->stack, value, -1);
3221 rc = 0;
3222 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003223
Guido van Rossum2f80d961999-07-13 15:18:58 +00003224 Py_DECREF(py_key);
3225 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003226}
3227
3228
3229static int
3230load_put(Unpicklerobject *self) {
3231 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003232 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003233 char *s;
3234
Guido van Rossum053b8df1998-11-25 16:18:00 +00003235 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003236 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003237 UNLESS (len=self->stack->length) return stackUnderflow();
3238 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3239 value=self->stack->data[len-1];
3240 l=PyDict_SetItem(self->memo, py_str, value);
3241 Py_DECREF(py_str);
3242 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003243}
3244
3245
3246static int
3247load_binput(Unpicklerobject *self) {
3248 PyObject *py_key = 0, *value = 0;
3249 unsigned char key, *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003250 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003251
Guido van Rossum053b8df1998-11-25 16:18:00 +00003252 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3253 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003254
3255 key = (unsigned char)s[0];
3256
Guido van Rossum053b8df1998-11-25 16:18:00 +00003257 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3258 value=self->stack->data[len-1];
3259 len=PyDict_SetItem(self->memo, py_key, value);
3260 Py_DECREF(py_key);
3261 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003262}
3263
3264
3265static int
3266load_long_binput(Unpicklerobject *self) {
3267 PyObject *py_key = 0, *value = 0;
3268 long key;
3269 unsigned char c, *s;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003270 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003271
Guido van Rossum053b8df1998-11-25 16:18:00 +00003272 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3273 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003274
3275 c = (unsigned char)s[0];
3276 key = (long)c;
3277 c = (unsigned char)s[1];
3278 key |= (long)c << 8;
3279 c = (unsigned char)s[2];
3280 key |= (long)c << 16;
3281 c = (unsigned char)s[3];
3282 key |= (long)c << 24;
3283
Guido van Rossum053b8df1998-11-25 16:18:00 +00003284 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3285 value=self->stack->data[len-1];
3286 len=PyDict_SetItem(self->memo, py_key, value);
3287 Py_DECREF(py_key);
3288 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003289}
3290
3291
3292static int
3293do_append(Unpicklerobject *self, int x) {
3294 PyObject *value = 0, *list = 0, *append_method = 0;
3295 int len, i;
3296
Guido van Rossum053b8df1998-11-25 16:18:00 +00003297 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3298 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003299
Guido van Rossum053b8df1998-11-25 16:18:00 +00003300 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003301
3302 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003303 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003304 int list_len;
3305
Guido van Rossum053b8df1998-11-25 16:18:00 +00003306 slice=Pdata_popList(self->stack, x);
3307 list_len = PyList_GET_SIZE(list);
3308 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003309 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003310 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003311 }
3312 else {
3313
Guido van Rossum053b8df1998-11-25 16:18:00 +00003314 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003315 return -1;
3316
3317 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003318 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003319
Guido van Rossum053b8df1998-11-25 16:18:00 +00003320 value=self->stack->data[i];
3321 junk=0;
3322 ARG_TUP(self, value);
3323 if (self->arg) {
3324 junk = PyObject_CallObject(append_method, self->arg);
3325 FREE_ARG_TUP(self);
3326 }
3327 if (! junk) {
3328 Pdata_clear(self->stack, i+1);
3329 self->stack->length=x;
3330 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003331 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003332 }
3333 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003334 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003335 self->stack->length=x;
3336 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003337 }
3338
Guido van Rossum60456fd1997-04-09 17:36:32 +00003339 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003340}
3341
3342
3343static int
3344load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003345 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003346}
3347
3348
3349static int
3350load_appends(Unpicklerobject *self) {
3351 return do_append(self, marker(self));
3352}
3353
3354
3355static int
3356do_setitems(Unpicklerobject *self, int x) {
3357 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003358 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003359
Guido van Rossum053b8df1998-11-25 16:18:00 +00003360 UNLESS ((len=self->stack->length) >= x
3361 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003362
Guido van Rossum053b8df1998-11-25 16:18:00 +00003363 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003364
Guido van Rossum053b8df1998-11-25 16:18:00 +00003365 for (i = x+1; i < len; i += 2) {
3366 key =self->stack->data[i-1];
3367 value=self->stack->data[i ];
3368 if (PyObject_SetItem(dict, key, value) < 0) {
3369 r=-1;
3370 break;
3371 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003372 }
3373
Guido van Rossum053b8df1998-11-25 16:18:00 +00003374 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003375
Guido van Rossum053b8df1998-11-25 16:18:00 +00003376 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003377}
3378
3379
3380static int
3381load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003382 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003383}
3384
Guido van Rossum60456fd1997-04-09 17:36:32 +00003385static int
3386load_setitems(Unpicklerobject *self) {
3387 return do_setitems(self, marker(self));
3388}
3389
3390
3391static int
3392load_build(Unpicklerobject *self) {
3393 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3394 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003395 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003396
Guido van Rossum053b8df1998-11-25 16:18:00 +00003397 if (self->stack->length < 2) return stackUnderflow();
3398 PDATA_POP(self->stack, value);
3399 if (! value) return -1;
3400 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003401
Guido van Rossum053b8df1998-11-25 16:18:00 +00003402 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3403 ARG_TUP(self, value);
3404 if (self->arg) {
3405 junk = PyObject_CallObject(__setstate__, self->arg);
3406 FREE_ARG_TUP(self);
3407 }
3408 Py_DECREF(__setstate__);
3409 if (! junk) return -1;
3410 Py_DECREF(junk);
3411 return 0;
3412 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003413
Guido van Rossum053b8df1998-11-25 16:18:00 +00003414 PyErr_Clear();
3415 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003416 i = 0;
3417 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003418 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3419 r=-1;
3420 break;
3421 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003422 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003423 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003424 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003425 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003426
Guido van Rossum053b8df1998-11-25 16:18:00 +00003427 Py_XDECREF(value);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003428
Guido van Rossum053b8df1998-11-25 16:18:00 +00003429 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003430}
3431
3432
3433static int
3434load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003435 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003436
Guido van Rossum053b8df1998-11-25 16:18:00 +00003437 if ((self->num_marks + 1) >= self->marks_size) {
3438 s=self->marks_size+20;
3439 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003440 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003441 self->marks=(int *)malloc(s * sizeof(int));
3442 else
3443 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003444 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003445 PyErr_NoMemory();
3446 return -1;
3447 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003448 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003449 }
3450
Guido van Rossum053b8df1998-11-25 16:18:00 +00003451 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003452
3453 return 0;
3454}
3455
3456static int
3457load_reduce(Unpicklerobject *self) {
3458 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003459
Guido van Rossum053b8df1998-11-25 16:18:00 +00003460 PDATA_POP(self->stack, arg_tup);
3461 if (! arg_tup) return -1;
3462 PDATA_POP(self->stack, callable);
3463 if (callable) {
3464 ob = Instance_New(callable, arg_tup);
3465 Py_DECREF(callable);
3466 }
3467 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003468
Guido van Rossum053b8df1998-11-25 16:18:00 +00003469 if (! ob) return -1;
3470
3471 PDATA_PUSH(self->stack, ob, -1);
3472 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003473}
3474
3475static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003476load(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003477 PyObject *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003478 char *s;
3479
Guido van Rossum60456fd1997-04-09 17:36:32 +00003480 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003481 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003482
3483 while (1) {
3484 if ((*self->read_func)(self, &s, 1) < 0)
3485 break;
3486
3487 switch (s[0]) {
3488 case NONE:
3489 if (load_none(self) < 0)
3490 break;
3491 continue;
3492
3493 case BININT:
3494 if (load_binint(self) < 0)
3495 break;
3496 continue;
3497
3498 case BININT1:
3499 if (load_binint1(self) < 0)
3500 break;
3501 continue;
3502
3503 case BININT2:
3504 if (load_binint2(self) < 0)
3505 break;
3506 continue;
3507
3508 case INT:
3509 if (load_int(self) < 0)
3510 break;
3511 continue;
3512
3513 case LONG:
3514 if (load_long(self) < 0)
3515 break;
3516 continue;
3517
3518 case FLOAT:
3519 if (load_float(self) < 0)
3520 break;
3521 continue;
3522
Guido van Rossum60456fd1997-04-09 17:36:32 +00003523 case BINFLOAT:
3524 if (load_binfloat(self) < 0)
3525 break;
3526 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003527
3528 case BINSTRING:
3529 if (load_binstring(self) < 0)
3530 break;
3531 continue;
3532
3533 case SHORT_BINSTRING:
3534 if (load_short_binstring(self) < 0)
3535 break;
3536 continue;
3537
3538 case STRING:
3539 if (load_string(self) < 0)
3540 break;
3541 continue;
3542
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003543 case UNICODE:
3544 if (load_unicode(self) < 0)
3545 break;
3546 continue;
3547
3548 case BINUNICODE:
3549 if (load_binunicode(self) < 0)
3550 break;
3551 continue;
3552
Guido van Rossum60456fd1997-04-09 17:36:32 +00003553 case EMPTY_TUPLE:
3554 if (load_empty_tuple(self) < 0)
3555 break;
3556 continue;
3557
3558 case TUPLE:
3559 if (load_tuple(self) < 0)
3560 break;
3561 continue;
3562
3563 case EMPTY_LIST:
3564 if (load_empty_list(self) < 0)
3565 break;
3566 continue;
3567
3568 case LIST:
3569 if (load_list(self) < 0)
3570 break;
3571 continue;
3572
3573 case EMPTY_DICT:
3574 if (load_empty_dict(self) < 0)
3575 break;
3576 continue;
3577
3578 case DICT:
3579 if (load_dict(self) < 0)
3580 break;
3581 continue;
3582
3583 case OBJ:
3584 if (load_obj(self) < 0)
3585 break;
3586 continue;
3587
3588 case INST:
3589 if (load_inst(self) < 0)
3590 break;
3591 continue;
3592
3593 case GLOBAL:
3594 if (load_global(self) < 0)
3595 break;
3596 continue;
3597
3598 case APPEND:
3599 if (load_append(self) < 0)
3600 break;
3601 continue;
3602
3603 case APPENDS:
3604 if (load_appends(self) < 0)
3605 break;
3606 continue;
3607
3608 case BUILD:
3609 if (load_build(self) < 0)
3610 break;
3611 continue;
3612
3613 case DUP:
3614 if (load_dup(self) < 0)
3615 break;
3616 continue;
3617
3618 case BINGET:
3619 if (load_binget(self) < 0)
3620 break;
3621 continue;
3622
3623 case LONG_BINGET:
3624 if (load_long_binget(self) < 0)
3625 break;
3626 continue;
3627
3628 case GET:
3629 if (load_get(self) < 0)
3630 break;
3631 continue;
3632
3633 case MARK:
3634 if (load_mark(self) < 0)
3635 break;
3636 continue;
3637
3638 case BINPUT:
3639 if (load_binput(self) < 0)
3640 break;
3641 continue;
3642
3643 case LONG_BINPUT:
3644 if (load_long_binput(self) < 0)
3645 break;
3646 continue;
3647
3648 case PUT:
3649 if (load_put(self) < 0)
3650 break;
3651 continue;
3652
3653 case POP:
3654 if (load_pop(self) < 0)
3655 break;
3656 continue;
3657
3658 case POP_MARK:
3659 if (load_pop_mark(self) < 0)
3660 break;
3661 continue;
3662
3663 case SETITEM:
3664 if (load_setitem(self) < 0)
3665 break;
3666 continue;
3667
3668 case SETITEMS:
3669 if (load_setitems(self) < 0)
3670 break;
3671 continue;
3672
3673 case STOP:
3674 break;
3675
3676 case PERSID:
3677 if (load_persid(self) < 0)
3678 break;
3679 continue;
3680
3681 case BINPERSID:
3682 if (load_binpersid(self) < 0)
3683 break;
3684 continue;
3685
3686 case REDUCE:
3687 if (load_reduce(self) < 0)
3688 break;
3689 continue;
3690
3691 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003692 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003693 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003694 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003695 }
3696
3697 break;
3698 }
3699
Guido van Rossum053b8df1998-11-25 16:18:00 +00003700 if ((err = PyErr_Occurred())) {
3701 if (err == PyExc_EOFError) {
3702 PyErr_SetNone(PyExc_EOFError);
3703 }
3704 return NULL;
3705 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003706
Guido van Rossum053b8df1998-11-25 16:18:00 +00003707 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003708 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003709}
3710
3711
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003712/* No-load functions to support noload, which is used to
3713 find persistent references. */
3714
3715static int
3716noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003717 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003718
3719 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003720 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003721}
3722
3723
3724static int
3725noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003726 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003727 char *s;
3728
3729 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003730 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003731 if ((*self->readline_func)(self, &s) < 0) return -1;
3732 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003733 PDATA_APPEND(self->stack, Py_None,-1);
3734 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003735}
3736
3737static int
3738noload_global(Unpicklerobject *self) {
3739 char *s;
3740
3741 if ((*self->readline_func)(self, &s) < 0) return -1;
3742 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003743 PDATA_APPEND(self->stack, Py_None,-1);
3744 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003745}
3746
3747static int
3748noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003749
Guido van Rossum053b8df1998-11-25 16:18:00 +00003750 if (self->stack->length < 2) return stackUnderflow();
3751 Pdata_clear(self->stack, self->stack->length-2);
3752 PDATA_APPEND(self->stack, Py_None,-1);
3753 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003754}
3755
3756static int
3757noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003758
Guido van Rossum053b8df1998-11-25 16:18:00 +00003759 if (self->stack->length < 1) return stackUnderflow();
3760 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003761 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003762}
3763
3764
3765static PyObject *
3766noload(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003767 PyObject *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003768 char *s;
3769
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003770 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003771 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003772
3773 while (1) {
3774 if ((*self->read_func)(self, &s, 1) < 0)
3775 break;
3776
3777 switch (s[0]) {
3778 case NONE:
3779 if (load_none(self) < 0)
3780 break;
3781 continue;
3782
3783 case BININT:
3784 if (load_binint(self) < 0)
3785 break;
3786 continue;
3787
3788 case BININT1:
3789 if (load_binint1(self) < 0)
3790 break;
3791 continue;
3792
3793 case BININT2:
3794 if (load_binint2(self) < 0)
3795 break;
3796 continue;
3797
3798 case INT:
3799 if (load_int(self) < 0)
3800 break;
3801 continue;
3802
3803 case LONG:
3804 if (load_long(self) < 0)
3805 break;
3806 continue;
3807
3808 case FLOAT:
3809 if (load_float(self) < 0)
3810 break;
3811 continue;
3812
3813 case BINFLOAT:
3814 if (load_binfloat(self) < 0)
3815 break;
3816 continue;
3817
3818 case BINSTRING:
3819 if (load_binstring(self) < 0)
3820 break;
3821 continue;
3822
3823 case SHORT_BINSTRING:
3824 if (load_short_binstring(self) < 0)
3825 break;
3826 continue;
3827
3828 case STRING:
3829 if (load_string(self) < 0)
3830 break;
3831 continue;
3832
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003833 case UNICODE:
3834 if (load_unicode(self) < 0)
3835 break;
3836 continue;
3837
3838 case BINUNICODE:
3839 if (load_binunicode(self) < 0)
3840 break;
3841 continue;
3842
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003843 case EMPTY_TUPLE:
3844 if (load_empty_tuple(self) < 0)
3845 break;
3846 continue;
3847
3848 case TUPLE:
3849 if (load_tuple(self) < 0)
3850 break;
3851 continue;
3852
3853 case EMPTY_LIST:
3854 if (load_empty_list(self) < 0)
3855 break;
3856 continue;
3857
3858 case LIST:
3859 if (load_list(self) < 0)
3860 break;
3861 continue;
3862
3863 case EMPTY_DICT:
3864 if (load_empty_dict(self) < 0)
3865 break;
3866 continue;
3867
3868 case DICT:
3869 if (load_dict(self) < 0)
3870 break;
3871 continue;
3872
3873 case OBJ:
3874 if (noload_obj(self) < 0)
3875 break;
3876 continue;
3877
3878 case INST:
3879 if (noload_inst(self) < 0)
3880 break;
3881 continue;
3882
3883 case GLOBAL:
3884 if (noload_global(self) < 0)
3885 break;
3886 continue;
3887
3888 case APPEND:
3889 if (load_append(self) < 0)
3890 break;
3891 continue;
3892
3893 case APPENDS:
3894 if (load_appends(self) < 0)
3895 break;
3896 continue;
3897
3898 case BUILD:
3899 if (noload_build(self) < 0)
3900 break;
3901 continue;
3902
3903 case DUP:
3904 if (load_dup(self) < 0)
3905 break;
3906 continue;
3907
3908 case BINGET:
3909 if (load_binget(self) < 0)
3910 break;
3911 continue;
3912
3913 case LONG_BINGET:
3914 if (load_long_binget(self) < 0)
3915 break;
3916 continue;
3917
3918 case GET:
3919 if (load_get(self) < 0)
3920 break;
3921 continue;
3922
3923 case MARK:
3924 if (load_mark(self) < 0)
3925 break;
3926 continue;
3927
3928 case BINPUT:
3929 if (load_binput(self) < 0)
3930 break;
3931 continue;
3932
3933 case LONG_BINPUT:
3934 if (load_long_binput(self) < 0)
3935 break;
3936 continue;
3937
3938 case PUT:
3939 if (load_put(self) < 0)
3940 break;
3941 continue;
3942
3943 case POP:
3944 if (load_pop(self) < 0)
3945 break;
3946 continue;
3947
3948 case POP_MARK:
3949 if (load_pop_mark(self) < 0)
3950 break;
3951 continue;
3952
3953 case SETITEM:
3954 if (load_setitem(self) < 0)
3955 break;
3956 continue;
3957
3958 case SETITEMS:
3959 if (load_setitems(self) < 0)
3960 break;
3961 continue;
3962
3963 case STOP:
3964 break;
3965
3966 case PERSID:
3967 if (load_persid(self) < 0)
3968 break;
3969 continue;
3970
3971 case BINPERSID:
3972 if (load_binpersid(self) < 0)
3973 break;
3974 continue;
3975
3976 case REDUCE:
3977 if (noload_reduce(self) < 0)
3978 break;
3979 continue;
3980
3981 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003982 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003983 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003984 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003985 }
3986
3987 break;
3988 }
3989
Guido van Rossum053b8df1998-11-25 16:18:00 +00003990 if ((err = PyErr_Occurred())) {
3991 if (err == PyExc_EOFError) {
3992 PyErr_SetNone(PyExc_EOFError);
3993 }
3994 return NULL;
3995 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003996
Guido van Rossum053b8df1998-11-25 16:18:00 +00003997 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003998 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003999}
4000
4001
Guido van Rossum60456fd1997-04-09 17:36:32 +00004002static PyObject *
4003Unpickler_load(Unpicklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00004004 UNLESS (PyArg_ParseTuple(args, ":load"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004005 return NULL;
4006
4007 return load(self);
4008}
4009
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004010static PyObject *
4011Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00004012 UNLESS (PyArg_ParseTuple(args, ":noload"))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004013 return NULL;
4014
4015 return noload(self);
4016}
4017
Guido van Rossum60456fd1997-04-09 17:36:32 +00004018
4019static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004020 {"load", (PyCFunction)Unpickler_load, 1,
4021 "load() -- Load a pickle"
4022 },
4023 {"noload", (PyCFunction)Unpickler_noload, 1,
4024 "noload() -- not load a pickle, but go through most of the motions\n"
4025 "\n"
4026 "This function can be used to read past a pickle without instantiating\n"
4027 "any objects or importing any modules. It can also be used to find all\n"
4028 "persistent references without instantiating any objects or importing\n"
4029 "any modules.\n"
4030 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004031 {NULL, NULL} /* sentinel */
4032};
4033
4034
4035static Unpicklerobject *
4036newUnpicklerobject(PyObject *f) {
4037 Unpicklerobject *self;
4038
Guido van Rossum053b8df1998-11-25 16:18:00 +00004039 UNLESS (self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004040 return NULL;
4041
4042 self->file = NULL;
4043 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004044 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004045 self->pers_func = NULL;
4046 self->last_string = NULL;
4047 self->marks = NULL;
4048 self->num_marks = 0;
4049 self->marks_size = 0;
4050 self->buf_size = 0;
4051 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00004052 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00004053 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004054 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004055
Guido van Rossum83addc72000-04-21 20:49:36 +00004056 UNLESS (self->memo = PyDict_New())
4057 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004058
4059 Py_INCREF(f);
4060 self->file = f;
4061
4062 /* Set read, readline based on type of f */
4063 if (PyFile_Check(f)) {
4064 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004065 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00004066 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
4067 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004068 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004069 self->read_func = read_file;
4070 self->readline_func = readline_file;
4071 }
4072 else if (PycStringIO_InputCheck(f)) {
4073 self->fp = NULL;
4074 self->read_func = read_cStringIO;
4075 self->readline_func = readline_cStringIO;
4076 }
4077 else {
4078
4079 self->fp = NULL;
4080 self->read_func = read_other;
4081 self->readline_func = readline_other;
4082
Guido van Rossum053b8df1998-11-25 16:18:00 +00004083 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004084 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004085 PyErr_Clear();
4086 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
4087 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00004088 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004089 }
4090 }
4091
Guido van Rossum053b8df1998-11-25 16:18:00 +00004092 if (PyEval_GetRestricted()) {
4093 /* Restricted execution, get private tables */
4094 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004095
Guido van Rossum053b8df1998-11-25 16:18:00 +00004096 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
4097 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
4098 Py_DECREF(m);
4099 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004100 }
4101 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004102 self->safe_constructors=safe_constructors;
4103 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004104 }
4105
Guido van Rossum60456fd1997-04-09 17:36:32 +00004106 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004107
4108err:
4109 Py_DECREF((PyObject *)self);
4110 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004111}
4112
4113
4114static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004115get_Unpickler(PyObject *self, PyObject *args) {
4116 PyObject *file;
4117
Guido van Rossum43713e52000-02-29 13:59:29 +00004118 UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004119 return NULL;
4120 return (PyObject *)newUnpicklerobject(file);
4121}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004122
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004123
Guido van Rossum60456fd1997-04-09 17:36:32 +00004124static void
4125Unpickler_dealloc(Unpicklerobject *self) {
4126 Py_XDECREF(self->readline);
4127 Py_XDECREF(self->read);
4128 Py_XDECREF(self->file);
4129 Py_XDECREF(self->memo);
4130 Py_XDECREF(self->stack);
4131 Py_XDECREF(self->pers_func);
4132 Py_XDECREF(self->arg);
4133 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004134 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004135
Guido van Rossum60456fd1997-04-09 17:36:32 +00004136 if (self->marks) {
4137 free(self->marks);
4138 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004139
Guido van Rossum60456fd1997-04-09 17:36:32 +00004140 if (self->buf_size) {
4141 free(self->buf);
4142 }
4143
4144 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004145}
4146
4147
4148static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004149Unpickler_getattr(Unpicklerobject *self, char *name) {
4150 if (!strcmp(name, "persistent_load")) {
4151 if (!self->pers_func) {
4152 PyErr_SetString(PyExc_AttributeError, name);
4153 return NULL;
4154 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004155
Guido van Rossum60456fd1997-04-09 17:36:32 +00004156 Py_INCREF(self->pers_func);
4157 return self->pers_func;
4158 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004159
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004160 if (!strcmp(name, "find_global")) {
4161 if (!self->find_class) {
4162 PyErr_SetString(PyExc_AttributeError, name);
4163 return NULL;
4164 }
4165
4166 Py_INCREF(self->find_class);
4167 return self->find_class;
4168 }
4169
Guido van Rossum60456fd1997-04-09 17:36:32 +00004170 if (!strcmp(name, "memo")) {
4171 if (!self->memo) {
4172 PyErr_SetString(PyExc_AttributeError, name);
4173 return NULL;
4174 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004175
Guido van Rossum60456fd1997-04-09 17:36:32 +00004176 Py_INCREF(self->memo);
4177 return self->memo;
4178 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004179
Guido van Rossum60456fd1997-04-09 17:36:32 +00004180 if (!strcmp(name, "UnpicklingError")) {
4181 Py_INCREF(UnpicklingError);
4182 return UnpicklingError;
4183 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004184
Guido van Rossum60456fd1997-04-09 17:36:32 +00004185 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4186}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004187
Guido van Rossum60456fd1997-04-09 17:36:32 +00004188
4189static int
4190Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004191
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004192 if (!strcmp(name, "persistent_load")) {
4193 Py_XDECREF(self->pers_func);
4194 self->pers_func = value;
4195 Py_XINCREF(value);
4196 return 0;
4197 }
4198
4199 if (!strcmp(name, "find_global")) {
4200 Py_XDECREF(self->find_class);
4201 self->find_class = value;
4202 Py_XINCREF(value);
4203 return 0;
4204 }
4205
Guido van Rossum053b8df1998-11-25 16:18:00 +00004206 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004207 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004208 "attribute deletion is not supported");
4209 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004210 }
4211
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004212 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004213 if (! PyDict_Check(value)) {
4214 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4215 return -1;
4216 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004217 Py_XDECREF(self->memo);
4218 self->memo = value;
4219 Py_INCREF(value);
4220 return 0;
4221 }
4222
Guido van Rossum60456fd1997-04-09 17:36:32 +00004223 PyErr_SetString(PyExc_AttributeError, name);
4224 return -1;
4225}
4226
4227
4228static PyObject *
4229cpm_dump(PyObject *self, PyObject *args) {
4230 PyObject *ob, *file, *res = NULL;
4231 Picklerobject *pickler = 0;
4232 int bin = 0;
4233
Guido van Rossum053b8df1998-11-25 16:18:00 +00004234 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004235 goto finally;
4236
Guido van Rossum053b8df1998-11-25 16:18:00 +00004237 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004238 goto finally;
4239
4240 if (dump(pickler, ob) < 0)
4241 goto finally;
4242
4243 Py_INCREF(Py_None);
4244 res = Py_None;
4245
4246finally:
4247 Py_XDECREF(pickler);
4248
4249 return res;
4250}
4251
4252
4253static PyObject *
4254cpm_dumps(PyObject *self, PyObject *args) {
4255 PyObject *ob, *file = 0, *res = NULL;
4256 Picklerobject *pickler = 0;
4257 int bin = 0;
4258
Guido van Rossum43713e52000-02-29 13:59:29 +00004259 UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004260 goto finally;
4261
Guido van Rossum053b8df1998-11-25 16:18:00 +00004262 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004263 goto finally;
4264
Guido van Rossum053b8df1998-11-25 16:18:00 +00004265 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004266 goto finally;
4267
4268 if (dump(pickler, ob) < 0)
4269 goto finally;
4270
4271 res = PycStringIO->cgetvalue(file);
4272
4273finally:
4274 Py_XDECREF(pickler);
4275 Py_XDECREF(file);
4276
4277 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004278}
4279
4280
4281static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004282cpm_load(PyObject *self, PyObject *args) {
4283 Unpicklerobject *unpickler = 0;
4284 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004285
Guido van Rossum43713e52000-02-29 13:59:29 +00004286 UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004287 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004288
Guido van Rossum053b8df1998-11-25 16:18:00 +00004289 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004290 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004291
Guido van Rossum60456fd1997-04-09 17:36:32 +00004292 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004293
Guido van Rossum60456fd1997-04-09 17:36:32 +00004294finally:
4295 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004296
Guido van Rossum60456fd1997-04-09 17:36:32 +00004297 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004298}
4299
4300
4301static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004302cpm_loads(PyObject *self, PyObject *args) {
4303 PyObject *ob, *file = 0, *res = NULL;
4304 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004305
Guido van Rossum43713e52000-02-29 13:59:29 +00004306 UNLESS (PyArg_ParseTuple(args, "S:loads", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004307 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004308
Guido van Rossum053b8df1998-11-25 16:18:00 +00004309 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004310 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004311
Guido van Rossum053b8df1998-11-25 16:18:00 +00004312 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004313 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004314
Guido van Rossum60456fd1997-04-09 17:36:32 +00004315 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004316
Guido van Rossum60456fd1997-04-09 17:36:32 +00004317finally:
4318 Py_XDECREF(file);
4319 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004320
Guido van Rossum60456fd1997-04-09 17:36:32 +00004321 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004322}
4323
4324
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004325static char Unpicklertype__doc__[] =
4326"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004327
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004328static PyTypeObject Unpicklertype = {
4329 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004330 0, /*ob_size*/
4331 "Unpickler", /*tp_name*/
4332 sizeof(Unpicklerobject), /*tp_basicsize*/
4333 0, /*tp_itemsize*/
4334 /* methods */
4335 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4336 (printfunc)0, /*tp_print*/
4337 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4338 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4339 (cmpfunc)0, /*tp_compare*/
4340 (reprfunc)0, /*tp_repr*/
4341 0, /*tp_as_number*/
4342 0, /*tp_as_sequence*/
4343 0, /*tp_as_mapping*/
4344 (hashfunc)0, /*tp_hash*/
4345 (ternaryfunc)0, /*tp_call*/
4346 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004347
Guido van Rossum60456fd1997-04-09 17:36:32 +00004348 /* Space for future expansion */
4349 0L,0L,0L,0L,
4350 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004351};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004352
Guido van Rossum60456fd1997-04-09 17:36:32 +00004353static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004354 {"dump", (PyCFunction)cpm_dump, 1,
4355 "dump(object, file, [binary]) --"
4356 "Write an object in pickle format to the given file\n"
4357 "\n"
4358 "If the optional argument, binary, is provided and is true, then the\n"
4359 "pickle will be written in binary format, which is more space and\n"
4360 "computationally efficient. \n"
4361 },
4362 {"dumps", (PyCFunction)cpm_dumps, 1,
4363 "dumps(object, [binary]) --"
4364 "Return a string containing an object in pickle format\n"
4365 "\n"
4366 "If the optional argument, binary, is provided and is true, then the\n"
4367 "pickle will be written in binary format, which is more space and\n"
4368 "computationally efficient. \n"
4369 },
4370 {"load", (PyCFunction)cpm_load, 1,
4371 "load(file) -- Load a pickle from the given file"},
4372 {"loads", (PyCFunction)cpm_loads, 1,
4373 "loads(string) -- Load a pickle from the given string"},
4374 {"Pickler", (PyCFunction)get_Pickler, 1,
4375 "Pickler(file, [binary]) -- Create a pickler\n"
4376 "\n"
4377 "If the optional argument, binary, is provided and is true, then\n"
4378 "pickles will be written in binary format, which is more space and\n"
4379 "computationally efficient. \n"
4380 },
4381 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4382 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004383 { NULL, NULL }
4384};
4385
Guido van Rossum60456fd1997-04-09 17:36:32 +00004386static int
4387init_stuff(PyObject *module, PyObject *module_dict) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004388 PyObject *string, *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004389
4390#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4391
4392 INIT_STR(__class__);
4393 INIT_STR(__getinitargs__);
4394 INIT_STR(__dict__);
4395 INIT_STR(__getstate__);
4396 INIT_STR(__setstate__);
4397 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004398 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004399 INIT_STR(__reduce__);
4400 INIT_STR(write);
4401 INIT_STR(__safe_for_unpickling__);
4402 INIT_STR(append);
4403 INIT_STR(read);
4404 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004405 INIT_STR(copy_reg);
4406 INIT_STR(dispatch_table);
4407 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004408 INIT_STR(__basicnew__);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004409 UNLESS (empty_str=PyString_FromString("")) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004410
Guido van Rossum053b8df1998-11-25 16:18:00 +00004411 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004412 return -1;
4413
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004414 /* These next few are special because we want to use different
4415 ones in restricted mode. */
4416
Guido van Rossum053b8df1998-11-25 16:18:00 +00004417 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004418 return -1;
4419
Guido van Rossum053b8df1998-11-25 16:18:00 +00004420 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4421 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004422 return -1;
4423
4424 Py_DECREF(copy_reg);
4425
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004426 /* Down to here ********************************** */
4427
Guido van Rossum053b8df1998-11-25 16:18:00 +00004428 UNLESS (string = PyImport_ImportModule("string"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004429 return -1;
4430
Guido van Rossum053b8df1998-11-25 16:18:00 +00004431 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004432 return -1;
4433
4434 Py_DECREF(string);
4435
Guido van Rossum053b8df1998-11-25 16:18:00 +00004436 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004437 return -1;
4438
Guido van Rossumc03158b1999-06-09 15:23:31 +00004439 /* Ugh */
4440 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4441 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4442 return -1;
4443
4444 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004445 UNLESS (r=PyRun_String(
4446 "def __init__(self, *args): self.args=args\n\n"
4447 "def __str__(self):\n"
4448 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4449 Py_file_input,
4450 module_dict, t) ) return -1;
4451 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004452
4453 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4454 return -1;
4455
4456 Py_DECREF(t);
4457
4458
4459 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
4460 PickleError, NULL))
4461 return -1;
4462
4463 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004464 UNLESS (r=PyRun_String(
4465 "def __init__(self, *args): self.args=args\n\n"
4466 "def __str__(self):\n"
4467 " a=self.args\n"
4468 " a=a and type(a[0]) or '(what)'\n"
4469 " return 'Cannot pickle %s objects' % a\n"
4470 , Py_file_input,
4471 module_dict, t) ) return -1;
4472 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004473
4474 UNLESS (UnpickleableError = PyErr_NewException(
4475 "cPickle.UnpickleableError", PicklingError, t))
4476 return -1;
4477
4478 Py_DECREF(t);
4479
4480 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
4481 PickleError, NULL))
4482 return -1;
4483
4484 if (PyDict_SetItemString(module_dict, "PickleError",
4485 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004486 return -1;
4487
4488 if (PyDict_SetItemString(module_dict, "PicklingError",
4489 PicklingError) < 0)
4490 return -1;
4491
Guido van Rossum60456fd1997-04-09 17:36:32 +00004492 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4493 UnpicklingError) < 0)
4494 return -1;
4495
Guido van Rossumc03158b1999-06-09 15:23:31 +00004496 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4497 UnpickleableError) < 0)
4498 return -1;
4499
Guido van Rossum053b8df1998-11-25 16:18:00 +00004500 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4501 return -1;
4502
4503 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4504 BadPickleGet) < 0)
4505 return -1;
4506
Guido van Rossum60456fd1997-04-09 17:36:32 +00004507 PycString_IMPORT;
4508
4509 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004510}
4511
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004512#ifndef DL_EXPORT /* declarations for DLL import/export */
4513#define DL_EXPORT(RTYPE) RTYPE
4514#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004515DL_EXPORT(void)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004516initcPickle() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004517 PyObject *m, *d, *v;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004518 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004519 PyObject *format_version;
4520 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004521
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004522 Picklertype.ob_type = &PyType_Type;
4523 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004524 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004525
Guido van Rossum60456fd1997-04-09 17:36:32 +00004526 /* Create the module and add the functions */
4527 m = Py_InitModule4("cPickle", cPickle_methods,
4528 cPickle_module_documentation,
4529 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004530
Guido van Rossum60456fd1997-04-09 17:36:32 +00004531 /* Add some symbolic constants to the module */
4532 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004533 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004534 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004535
Guido van Rossum60456fd1997-04-09 17:36:32 +00004536 format_version = PyString_FromString("1.3");
4537 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004538
Guido van Rossum60456fd1997-04-09 17:36:32 +00004539 PyDict_SetItemString(d, "format_version", format_version);
4540 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004541 Py_XDECREF(format_version);
4542 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004543
Guido van Rossum60456fd1997-04-09 17:36:32 +00004544 init_stuff(m, d);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004545}