blob: ecf941d195f0fe5c01e84da7d8a76bcdedbd457a [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 Rossum053b8df1998-11-25 16:18:00 +00002154
Guido van Rossum60456fd1997-04-09 17:36:32 +00002155 self->file = file;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002156
Guido van Rossum053b8df1998-11-25 16:18:00 +00002157 UNLESS (self->memo = PyDict_New()) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002158 Py_XDECREF((PyObject *)self);
2159 return NULL;
2160 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002161
Guido van Rossum60456fd1997-04-09 17:36:32 +00002162 if (PyFile_Check(file)) {
2163 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002164 if (self->fp == NULL) {
2165 PyErr_SetString(PyExc_IOError, "output file closed");
2166 return NULL;
2167 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002168 self->write_func = write_file;
2169 }
2170 else if (PycStringIO_OutputCheck(file)) {
2171 self->write_func = write_cStringIO;
2172 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002173 else if (file == Py_None) {
2174 self->write_func = write_none;
2175 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002176 else {
2177 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002178
Guido van Rossum053b8df1998-11-25 16:18:00 +00002179 if (! Pdata_Check(file)) {
2180 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002181 PyErr_Clear();
2182 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002183 "attribute");
2184 goto err;
2185 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002186 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002187
Guido van Rossum053b8df1998-11-25 16:18:00 +00002188 UNLESS (self->write_buf =
Guido van Rossum60456fd1997-04-09 17:36:32 +00002189 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
2190 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002191 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002192 }
2193 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002194
Guido van Rossum053b8df1998-11-25 16:18:00 +00002195 if (PyEval_GetRestricted()) {
2196 /* Restricted execution, get private tables */
2197 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002198
Guido van Rossum053b8df1998-11-25 16:18:00 +00002199 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2200 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2201 Py_DECREF(m);
2202 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002203 }
2204 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002205 self->dispatch_table=dispatch_table;
2206 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002207 }
2208
Guido van Rossum60456fd1997-04-09 17:36:32 +00002209 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002210
2211err:
2212 Py_DECREF((PyObject *)self);
2213 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002214}
2215
2216
2217static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002218get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002219 PyObject *file=NULL;
2220 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002221
Guido van Rossum053b8df1998-11-25 16:18:00 +00002222 bin=1;
Guido van Rossum43713e52000-02-29 13:59:29 +00002223 if (! PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002224 PyErr_Clear();
2225 bin=0;
Guido van Rossum43713e52000-02-29 13:59:29 +00002226 if (! PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002227 return NULL;
2228 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002229 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002230}
2231
2232
2233static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002234Pickler_dealloc(Picklerobject *self) {
2235 Py_XDECREF(self->write);
2236 Py_XDECREF(self->memo);
2237 Py_XDECREF(self->arg);
2238 Py_XDECREF(self->file);
2239 Py_XDECREF(self->pers_func);
2240 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002241 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002242
2243 if (self->write_buf) {
2244 free(self->write_buf);
2245 }
2246
2247 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002248}
2249
2250
2251static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002252Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002253
2254 switch (*name) {
2255 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002256 if (strcmp(name, "persistent_id") == 0) {
2257 if (!self->pers_func) {
2258 PyErr_SetString(PyExc_AttributeError, name);
2259 return NULL;
2260 }
2261
2262 Py_INCREF(self->pers_func);
2263 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002264 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002265 break;
2266 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002267 if (strcmp(name, "memo") == 0) {
2268 if (!self->memo) {
2269 PyErr_SetString(PyExc_AttributeError, name);
2270 return NULL;
2271 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002272
Guido van Rossum60456fd1997-04-09 17:36:32 +00002273 Py_INCREF(self->memo);
2274 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002275 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002276 break;
2277 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002278 if (strcmp(name, "PicklingError") == 0) {
2279 Py_INCREF(PicklingError);
2280 return PicklingError;
2281 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002282 break;
2283 case 'b':
2284 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002285 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002286 break;
2287 case 'f':
2288 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002289 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002290 break;
2291 case 'g':
2292 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2293 PyErr_SetString(PyExc_AttributeError, name);
2294 return NULL;
2295 }
2296 break;
2297 }
2298 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002299}
2300
2301
2302int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002303Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002304
Guido van Rossum053b8df1998-11-25 16:18:00 +00002305 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002306 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002307 "attribute deletion is not supported");
2308 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002309 }
2310
Guido van Rossum60456fd1997-04-09 17:36:32 +00002311 if (strcmp(name, "persistent_id") == 0) {
2312 Py_XDECREF(self->pers_func);
2313 self->pers_func = value;
2314 Py_INCREF(value);
2315 return 0;
2316 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002317
Guido van Rossum60456fd1997-04-09 17:36:32 +00002318 if (strcmp(name, "inst_persistent_id") == 0) {
2319 Py_XDECREF(self->inst_pers_func);
2320 self->inst_pers_func = value;
2321 Py_INCREF(value);
2322 return 0;
2323 }
2324
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002325 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002326 if (! PyDict_Check(value)) {
2327 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2328 return -1;
2329 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002330 Py_XDECREF(self->memo);
2331 self->memo = value;
2332 Py_INCREF(value);
2333 return 0;
2334 }
2335
Guido van Rossum053b8df1998-11-25 16:18:00 +00002336 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002337 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002338 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002339 }
2340
Guido van Rossum053b8df1998-11-25 16:18:00 +00002341 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002342 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002343 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002344 }
2345
Guido van Rossum60456fd1997-04-09 17:36:32 +00002346 PyErr_SetString(PyExc_AttributeError, name);
2347 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002348}
2349
2350
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002351static char Picklertype__doc__[] =
2352"Objects that know how to pickle objects\n"
2353;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002354
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002355static PyTypeObject Picklertype = {
2356 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002357 0, /*ob_size*/
2358 "Pickler", /*tp_name*/
2359 sizeof(Picklerobject), /*tp_basicsize*/
2360 0, /*tp_itemsize*/
2361 /* methods */
2362 (destructor)Pickler_dealloc, /*tp_dealloc*/
2363 (printfunc)0, /*tp_print*/
2364 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2365 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2366 (cmpfunc)0, /*tp_compare*/
2367 (reprfunc)0, /*tp_repr*/
2368 0, /*tp_as_number*/
2369 0, /*tp_as_sequence*/
2370 0, /*tp_as_mapping*/
2371 (hashfunc)0, /*tp_hash*/
2372 (ternaryfunc)0, /*tp_call*/
2373 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002374
Guido van Rossum60456fd1997-04-09 17:36:32 +00002375 /* Space for future expansion */
2376 0L,0L,0L,0L,
2377 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002378};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002379
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002380static PyObject *
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002381find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002382 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002383
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002384 if (fc) {
2385 if (fc==Py_None) {
2386 PyErr_SetString(UnpicklingError,
2387 "Global and instance pickles are not supported.");
2388 return NULL;
2389 }
2390 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2391 }
2392
Jeremy Hyltond1055231998-08-11 19:52:51 +00002393 module = PySys_GetObject("modules");
2394 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002395 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002396
2397 module = PyDict_GetItem(module, py_module_name);
2398 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002399 module = PyImport_Import(py_module_name);
2400 if (!module)
2401 return NULL;
2402 global = PyObject_GetAttr(module, py_global_name);
2403 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002404 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002405 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002406 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002407 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002408 char buf[256 + 37];
2409 sprintf(buf, "Failed to import class %.128s from module %.128s",
2410 PyString_AS_STRING((PyStringObject*)py_global_name),
2411 PyString_AS_STRING((PyStringObject*)py_module_name));
2412 PyErr_SetString(PyExc_SystemError, buf);
2413 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002414 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002415 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002416}
2417
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002418static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002419marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002420 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002421 PyErr_SetString(UnpicklingError, "could not find MARK");
2422 return -1;
2423 }
2424
2425 return self->marks[--self->num_marks];
2426}
2427
2428
2429static int
2430load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002431 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002432 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002433}
2434
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002435static int
2436bad_readline() {
2437 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2438 return -1;
2439}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002440
2441static int
2442load_int(Unpicklerobject *self) {
2443 PyObject *py_int = 0;
2444 char *endptr, *s;
2445 int len, res = -1;
2446 long l;
2447
2448 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002449 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002450 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002451
2452 errno = 0;
2453 l = strtol(s, &endptr, 0);
2454
2455 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2456 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002457 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002458 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002459 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002460
Guido van Rossum053b8df1998-11-25 16:18:00 +00002461 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2462 PyErr_SetString(PyExc_ValueError,
2463 "could not convert string to int");
2464 goto finally;
2465 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002466 }
2467 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002468 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002469 }
2470
Guido van Rossum053b8df1998-11-25 16:18:00 +00002471 free(s);
2472 PDATA_PUSH(self->stack, py_int, -1);
2473 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002474
2475finally:
2476 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002477
2478 return res;
2479}
2480
2481
2482static long
2483calc_binint(char *s, int x) {
2484 unsigned char c;
2485 int i;
2486 long l;
2487
2488 for (i = 0, l = 0L; i < x; i++) {
2489 c = (unsigned char)s[i];
2490 l |= (long)c << (i * 8);
2491 }
2492
2493 return l;
2494}
2495
2496
2497static int
2498load_binintx(Unpicklerobject *self, char *s, int x) {
2499 PyObject *py_int = 0;
2500 long l;
2501
2502 l = calc_binint(s, x);
2503
Guido van Rossum053b8df1998-11-25 16:18:00 +00002504 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002505 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002506
Guido van Rossum053b8df1998-11-25 16:18:00 +00002507 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002508 return 0;
2509}
2510
2511
2512static int
2513load_binint(Unpicklerobject *self) {
2514 char *s;
2515
2516 if ((*self->read_func)(self, &s, 4) < 0)
2517 return -1;
2518
2519 return load_binintx(self, s, 4);
2520}
2521
2522
2523static int
2524load_binint1(Unpicklerobject *self) {
2525 char *s;
2526
2527 if ((*self->read_func)(self, &s, 1) < 0)
2528 return -1;
2529
2530 return load_binintx(self, s, 1);
2531}
2532
2533
2534static int
2535load_binint2(Unpicklerobject *self) {
2536 char *s;
2537
2538 if ((*self->read_func)(self, &s, 2) < 0)
2539 return -1;
2540
2541 return load_binintx(self, s, 2);
2542}
2543
2544static int
2545load_long(Unpicklerobject *self) {
2546 PyObject *l = 0;
2547 char *end, *s;
2548 int len, res = -1;
2549
Guido van Rossum60456fd1997-04-09 17:36:32 +00002550 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002551 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002552 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002553
Guido van Rossum053b8df1998-11-25 16:18:00 +00002554 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002555 goto finally;
2556
Guido van Rossum053b8df1998-11-25 16:18:00 +00002557 free(s);
2558 PDATA_PUSH(self->stack, l, -1);
2559 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002560
2561finally:
2562 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002563
2564 return res;
2565}
2566
2567
2568static int
2569load_float(Unpicklerobject *self) {
2570 PyObject *py_float = 0;
2571 char *endptr, *s;
2572 int len, res = -1;
2573 double d;
2574
2575 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002576 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002577 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002578
2579 errno = 0;
2580 d = strtod(s, &endptr);
2581
2582 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2583 PyErr_SetString(PyExc_ValueError,
2584 "could not convert string to float");
2585 goto finally;
2586 }
2587
Guido van Rossum053b8df1998-11-25 16:18:00 +00002588 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002589 goto finally;
2590
Guido van Rossum053b8df1998-11-25 16:18:00 +00002591 free(s);
2592 PDATA_PUSH(self->stack, py_float, -1);
2593 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002594
2595finally:
2596 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002597
2598 return res;
2599}
2600
Guido van Rossum60456fd1997-04-09 17:36:32 +00002601static int
2602load_binfloat(Unpicklerobject *self) {
2603 PyObject *py_float = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002604 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002605 long fhi, flo;
2606 double x;
2607 char *p;
2608
2609 if ((*self->read_func)(self, &p, 8) < 0)
2610 return -1;
2611
2612 /* First byte */
2613 s = (*p>>7) & 1;
2614 e = (*p & 0x7F) << 4;
2615 p++;
2616
2617 /* Second byte */
2618 e |= (*p>>4) & 0xF;
2619 fhi = (*p & 0xF) << 24;
2620 p++;
2621
2622 /* Third byte */
2623 fhi |= (*p & 0xFF) << 16;
2624 p++;
2625
2626 /* Fourth byte */
2627 fhi |= (*p & 0xFF) << 8;
2628 p++;
2629
2630 /* Fifth byte */
2631 fhi |= *p & 0xFF;
2632 p++;
2633
2634 /* Sixth byte */
2635 flo = (*p & 0xFF) << 16;
2636 p++;
2637
2638 /* Seventh byte */
2639 flo |= (*p & 0xFF) << 8;
2640 p++;
2641
2642 /* Eighth byte */
2643 flo |= *p & 0xFF;
2644
2645 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2646 x /= 268435456.0; /* 2**28 */
2647
2648 /* XXX This sadly ignores Inf/NaN */
2649 if (e == 0)
2650 e = -1022;
2651 else {
2652 x += 1.0;
2653 e -= 1023;
2654 }
2655 x = ldexp(x, e);
2656
2657 if (s)
2658 x = -x;
2659
Guido van Rossum053b8df1998-11-25 16:18:00 +00002660 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002661
Guido van Rossum053b8df1998-11-25 16:18:00 +00002662 PDATA_PUSH(self->stack, py_float, -1);
2663 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002664}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002665
2666static int
2667load_string(Unpicklerobject *self) {
2668 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002669 int len, res = -1, nslash;
2670 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002671
2672 static PyObject *eval_dict = 0;
2673
2674 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002675 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002676 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002677
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002678 /* Check for unquoted quotes (evil strings) */
2679 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002680 if (q != '"' && q != '\'') goto insecure;
2681 for (p=s+1, nslash=0; *p; p++) {
2682 if (*p==q && nslash%2==0) break;
2683 if (*p=='\\') nslash++;
2684 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002685 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002686 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002687 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002688 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002689 }
2690 else goto insecure;
2691 /********************************************/
2692
Guido van Rossum053b8df1998-11-25 16:18:00 +00002693 UNLESS (eval_dict)
2694 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002695 goto finally;
2696
Guido van Rossum053b8df1998-11-25 16:18:00 +00002697 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002698 goto finally;
2699
Guido van Rossum053b8df1998-11-25 16:18:00 +00002700 free(s);
2701 PDATA_PUSH(self->stack, str, -1);
2702 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002703
2704finally:
2705 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002706
2707 return res;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002708
2709insecure:
2710 free(s);
2711 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2712 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002713}
2714
2715
2716static int
2717load_binstring(Unpicklerobject *self) {
2718 PyObject *py_string = 0;
2719 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002720 char *s;
2721
Guido van Rossum053b8df1998-11-25 16:18:00 +00002722 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002723
2724 l = calc_binint(s, 4);
2725
2726 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002727 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002728
Guido van Rossum053b8df1998-11-25 16:18:00 +00002729 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2730 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002731
Guido van Rossum053b8df1998-11-25 16:18:00 +00002732 PDATA_PUSH(self->stack, py_string, -1);
2733 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002734}
2735
2736
2737static int
2738load_short_binstring(Unpicklerobject *self) {
2739 PyObject *py_string = 0;
2740 unsigned char l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002741 char *s;
2742
2743 if ((*self->read_func)(self, &s, 1) < 0)
2744 return -1;
2745
2746 l = (unsigned char)s[0];
2747
Guido van Rossum053b8df1998-11-25 16:18:00 +00002748 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002749
Guido van Rossum053b8df1998-11-25 16:18:00 +00002750 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002751
Guido van Rossum053b8df1998-11-25 16:18:00 +00002752 PDATA_PUSH(self->stack, py_string, -1);
2753 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002754}
2755
2756
2757static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002758load_unicode(Unpicklerobject *self) {
2759 PyObject *str = 0;
2760 int len, res = -1;
2761 char *s;
2762
2763 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2764 if (len < 2) return bad_readline();
2765
2766 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
2767 goto finally;
2768
2769 PDATA_PUSH(self->stack, str, -1);
2770 return 0;
2771
2772finally:
2773 return res;
2774}
2775
2776
2777static int
2778load_binunicode(Unpicklerobject *self) {
2779 PyObject *unicode;
2780 long l;
2781 char *s;
2782
2783 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2784
2785 l = calc_binint(s, 4);
2786
2787 if ((*self->read_func)(self, &s, l) < 0)
2788 return -1;
2789
2790 UNLESS (unicode = PyUnicode_DecodeUTF8(s, l, NULL))
2791 return -1;
2792
2793 PDATA_PUSH(self->stack, unicode, -1);
2794 return 0;
2795}
2796
2797
2798static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002799load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002800 PyObject *tup;
2801 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002802
Guido van Rossum053b8df1998-11-25 16:18:00 +00002803 if ((i = marker(self)) < 0) return -1;
2804 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2805 PDATA_PUSH(self->stack, tup, -1);
2806 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002807}
2808
2809static int
2810load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002811 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002812
Guido van Rossum053b8df1998-11-25 16:18:00 +00002813 UNLESS (tup=PyTuple_New(0)) return -1;
2814 PDATA_PUSH(self->stack, tup, -1);
2815 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002816}
2817
2818static int
2819load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002820 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002821
Guido van Rossum053b8df1998-11-25 16:18:00 +00002822 UNLESS (list=PyList_New(0)) return -1;
2823 PDATA_PUSH(self->stack, list, -1);
2824 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002825}
2826
2827static int
2828load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002829 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002830
Guido van Rossum053b8df1998-11-25 16:18:00 +00002831 UNLESS (dict=PyDict_New()) return -1;
2832 PDATA_PUSH(self->stack, dict, -1);
2833 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002834}
2835
2836
2837static int
2838load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002839 PyObject *list = 0;
2840 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002841
Guido van Rossum053b8df1998-11-25 16:18:00 +00002842 if ((i = marker(self)) < 0) return -1;
2843 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2844 PDATA_PUSH(self->stack, list, -1);
2845 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002846}
2847
2848static int
2849load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002850 PyObject *dict, *key, *value;
2851 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002852
Guido van Rossum053b8df1998-11-25 16:18:00 +00002853 if ((i = marker(self)) < 0) return -1;
2854 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002855
Guido van Rossum053b8df1998-11-25 16:18:00 +00002856 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002857
Guido van Rossum053b8df1998-11-25 16:18:00 +00002858 for (k = i+1; k < j; k += 2) {
2859 key =self->stack->data[k-1];
2860 value=self->stack->data[k ];
2861 if (PyDict_SetItem(dict, key, value) < 0) {
2862 Py_DECREF(dict);
2863 return -1;
2864 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002865 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002866 Pdata_clear(self->stack, i);
2867 PDATA_PUSH(self->stack, dict, -1);
2868 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002869}
2870
2871static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002872Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002873 int has_key;
2874 PyObject *safe=0, *r=0;
2875
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002876 if (PyClass_Check(cls)) {
2877 int l;
2878
Guido van Rossum053b8df1998-11-25 16:18:00 +00002879 if ((l=PyObject_Length(args)) < 0) goto err;
2880 UNLESS (l) {
2881 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002882
Guido van Rossum053b8df1998-11-25 16:18:00 +00002883 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2884 /* We have a class with no __getinitargs__, so bypass usual
2885 construction */
2886 PyInstanceObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002887
Guido van Rossum053b8df1998-11-25 16:18:00 +00002888 PyErr_Clear();
2889 UNLESS (inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
2890 goto err;
2891 inst->in_class=(PyClassObject*)cls;
2892 Py_INCREF(cls);
2893 UNLESS (inst->in_dict=PyDict_New()) {
2894 Py_DECREF(inst);
2895 goto err;
2896 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002897
Guido van Rossum053b8df1998-11-25 16:18:00 +00002898 return (PyObject *)inst;
2899 }
2900 Py_DECREF(__getinitargs__);
2901 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002902
Guido van Rossum053b8df1998-11-25 16:18:00 +00002903 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002904 else goto err;
2905 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002906
2907
2908 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2909 goto err;
2910
2911 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002912 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002913 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002914 cPickle_ErrFormat(UnpicklingError,
2915 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002916 Py_XDECREF(safe);
2917 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002918 }
2919
Guido van Rossum053b8df1998-11-25 16:18:00 +00002920 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002921 /* Special case, call cls.__basicnew__() */
2922 PyObject *basicnew;
2923
Guido van Rossum053b8df1998-11-25 16:18:00 +00002924 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002925 r=PyObject_CallObject(basicnew, NULL);
2926 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002927 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002928 }
2929
Guido van Rossum053b8df1998-11-25 16:18:00 +00002930 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002931
Guido van Rossum60456fd1997-04-09 17:36:32 +00002932err:
2933 {
2934 PyObject *tp, *v, *tb;
2935
2936 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002937 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2938 Py_XDECREF(v);
2939 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002940 }
2941 PyErr_Restore(tp,v,tb);
2942 }
2943 return NULL;
2944}
2945
2946
2947static int
2948load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002949 PyObject *class, *tup, *obj=0;
2950 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002951
Guido van Rossum053b8df1998-11-25 16:18:00 +00002952 if ((i = marker(self)) < 0) return -1;
2953 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
2954 PDATA_POP(self->stack, class);
2955 if (class) {
2956 obj = Instance_New(class, tup);
2957 Py_DECREF(class);
2958 }
2959 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002960
Guido van Rossum053b8df1998-11-25 16:18:00 +00002961 if (! obj) return -1;
2962 PDATA_PUSH(self->stack, obj, -1);
2963 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002964}
2965
2966
2967static int
2968load_inst(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002969 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002970 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002971 char *s;
2972
Guido van Rossum053b8df1998-11-25 16:18:00 +00002973 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002974
Guido van Rossum053b8df1998-11-25 16:18:00 +00002975 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002976 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002977 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002978
Guido van Rossum053b8df1998-11-25 16:18:00 +00002979 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002980 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002981 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002982 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002983 Py_DECREF(class_name);
2984 }
2985 }
2986 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002987
Guido van Rossum053b8df1998-11-25 16:18:00 +00002988 if (! class) return -1;
2989
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002990 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002991 obj = Instance_New(class, tup);
2992 Py_DECREF(tup);
2993 }
2994 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002995
Guido van Rossum053b8df1998-11-25 16:18:00 +00002996 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002997
Guido van Rossum053b8df1998-11-25 16:18:00 +00002998 PDATA_PUSH(self->stack, obj, -1);
2999 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003000}
3001
3002
3003static int
3004load_global(Unpicklerobject *self) {
3005 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003006 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003007 char *s;
3008
Guido van Rossum053b8df1998-11-25 16:18:00 +00003009 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003010 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003011 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003012
Guido van Rossum053b8df1998-11-25 16:18:00 +00003013 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003014 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003015 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003016 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003017 Py_DECREF(class_name);
3018 }
3019 }
3020 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003021
Guido van Rossum053b8df1998-11-25 16:18:00 +00003022 if (! class) return -1;
3023 PDATA_PUSH(self->stack, class, -1);
3024 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003025}
3026
3027
3028static int
3029load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003030 PyObject *pid = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003031 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003032 char *s;
3033
3034 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003035 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003036 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003037
Guido van Rossum053b8df1998-11-25 16:18:00 +00003038 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003039
Guido van Rossum053b8df1998-11-25 16:18:00 +00003040 if (PyList_Check(self->pers_func)) {
3041 if (PyList_Append(self->pers_func, pid) < 0) {
3042 Py_DECREF(pid);
3043 return -1;
3044 }
3045 }
3046 else {
3047 ARG_TUP(self, pid);
3048 if (self->arg) {
3049 pid = PyObject_CallObject(self->pers_func, self->arg);
3050 FREE_ARG_TUP(self);
3051 }
3052 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003053
Guido van Rossum053b8df1998-11-25 16:18:00 +00003054 if (! pid) return -1;
3055
3056 PDATA_PUSH(self->stack, pid, -1);
3057 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003058 }
3059 else {
3060 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003061 "A load persistent id instruction was encountered,\n"
3062 "but no persistent_load function was specified.");
3063 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003064 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003065}
3066
Guido van Rossum60456fd1997-04-09 17:36:32 +00003067static int
3068load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003069 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003070
3071 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003072 PDATA_POP(self->stack, pid);
3073 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003074
Guido van Rossum053b8df1998-11-25 16:18:00 +00003075 if (PyList_Check(self->pers_func)) {
3076 if (PyList_Append(self->pers_func, pid) < 0) {
3077 Py_DECREF(pid);
3078 return -1;
3079 }
3080 }
3081 else {
3082 ARG_TUP(self, pid);
3083 if (self->arg) {
3084 pid = PyObject_CallObject(self->pers_func, self->arg);
3085 FREE_ARG_TUP(self);
3086 }
3087 if (! pid) return -1;
3088 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003089
Guido van Rossum053b8df1998-11-25 16:18:00 +00003090 PDATA_PUSH(self->stack, pid, -1);
3091 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003092 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003093 else {
3094 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003095 "A load persistent id instruction was encountered,\n"
3096 "but no persistent_load function was specified.");
3097 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003098 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003099}
3100
3101
3102static int
3103load_pop(Unpicklerobject *self) {
3104 int len;
3105
Guido van Rossum053b8df1998-11-25 16:18:00 +00003106 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003107
3108 if ((self->num_marks > 0) &&
3109 (self->marks[self->num_marks - 1] == len))
3110 self->num_marks--;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003111 else
3112 Py_DECREF(self->stack->data[--(self->stack->length)]);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003113
3114 return 0;
3115}
3116
3117
3118static int
3119load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003120 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003121
3122 if ((i = marker(self)) < 0)
3123 return -1;
3124
Guido van Rossum053b8df1998-11-25 16:18:00 +00003125 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003126
3127 return 0;
3128}
3129
3130
3131static int
3132load_dup(Unpicklerobject *self) {
3133 PyObject *last;
3134 int len;
3135
Guido van Rossum053b8df1998-11-25 16:18:00 +00003136 if ((len = self->stack->length) <= 0) return stackUnderflow();
3137 last=self->stack->data[len-1];
3138 Py_INCREF(last);
3139 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003140 return 0;
3141}
3142
3143
3144static int
3145load_get(Unpicklerobject *self) {
3146 PyObject *py_str = 0, *value = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003147 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003148 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003149 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003150
Guido van Rossum053b8df1998-11-25 16:18:00 +00003151 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003152 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003153
Guido van Rossum053b8df1998-11-25 16:18:00 +00003154 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3155
3156 value = PyDict_GetItem(self->memo, py_str);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003157 if (! value) {
3158 PyErr_SetObject(BadPickleGet, py_str);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003159 rc = -1;
3160 } else {
3161 PDATA_APPEND(self->stack, value, -1);
3162 rc = 0;
3163 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003164
Guido van Rossum2f80d961999-07-13 15:18:58 +00003165 Py_DECREF(py_str);
3166 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003167}
3168
3169
3170static int
3171load_binget(Unpicklerobject *self) {
3172 PyObject *py_key = 0, *value = 0;
3173 unsigned char key;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003174 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003175 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003176
Guido van Rossum053b8df1998-11-25 16:18:00 +00003177 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003178
3179 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003180 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3181
3182 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003183 if (! value) {
3184 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003185 rc = -1;
3186 } else {
3187 PDATA_APPEND(self->stack, value, -1);
3188 rc = 0;
3189 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003190
Guido van Rossum2f80d961999-07-13 15:18:58 +00003191 Py_DECREF(py_key);
3192 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003193}
3194
3195
3196static int
3197load_long_binget(Unpicklerobject *self) {
3198 PyObject *py_key = 0, *value = 0;
3199 unsigned char c, *s;
3200 long key;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003201 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003202
Guido van Rossum053b8df1998-11-25 16:18:00 +00003203 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003204
3205 c = (unsigned char)s[0];
3206 key = (long)c;
3207 c = (unsigned char)s[1];
3208 key |= (long)c << 8;
3209 c = (unsigned char)s[2];
3210 key |= (long)c << 16;
3211 c = (unsigned char)s[3];
3212 key |= (long)c << 24;
3213
Guido van Rossum053b8df1998-11-25 16:18:00 +00003214 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3215
3216 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003217 if (! value) {
3218 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003219 rc = -1;
3220 } else {
3221 PDATA_APPEND(self->stack, value, -1);
3222 rc = 0;
3223 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003224
Guido van Rossum2f80d961999-07-13 15:18:58 +00003225 Py_DECREF(py_key);
3226 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003227}
3228
3229
3230static int
3231load_put(Unpicklerobject *self) {
3232 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003233 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003234 char *s;
3235
Guido van Rossum053b8df1998-11-25 16:18:00 +00003236 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003237 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003238 UNLESS (len=self->stack->length) return stackUnderflow();
3239 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3240 value=self->stack->data[len-1];
3241 l=PyDict_SetItem(self->memo, py_str, value);
3242 Py_DECREF(py_str);
3243 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003244}
3245
3246
3247static int
3248load_binput(Unpicklerobject *self) {
3249 PyObject *py_key = 0, *value = 0;
3250 unsigned char key, *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003251 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003252
Guido van Rossum053b8df1998-11-25 16:18:00 +00003253 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3254 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003255
3256 key = (unsigned char)s[0];
3257
Guido van Rossum053b8df1998-11-25 16:18:00 +00003258 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3259 value=self->stack->data[len-1];
3260 len=PyDict_SetItem(self->memo, py_key, value);
3261 Py_DECREF(py_key);
3262 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003263}
3264
3265
3266static int
3267load_long_binput(Unpicklerobject *self) {
3268 PyObject *py_key = 0, *value = 0;
3269 long key;
3270 unsigned char c, *s;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003271 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003272
Guido van Rossum053b8df1998-11-25 16:18:00 +00003273 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3274 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003275
3276 c = (unsigned char)s[0];
3277 key = (long)c;
3278 c = (unsigned char)s[1];
3279 key |= (long)c << 8;
3280 c = (unsigned char)s[2];
3281 key |= (long)c << 16;
3282 c = (unsigned char)s[3];
3283 key |= (long)c << 24;
3284
Guido van Rossum053b8df1998-11-25 16:18:00 +00003285 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3286 value=self->stack->data[len-1];
3287 len=PyDict_SetItem(self->memo, py_key, value);
3288 Py_DECREF(py_key);
3289 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003290}
3291
3292
3293static int
3294do_append(Unpicklerobject *self, int x) {
3295 PyObject *value = 0, *list = 0, *append_method = 0;
3296 int len, i;
3297
Guido van Rossum053b8df1998-11-25 16:18:00 +00003298 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3299 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003300
Guido van Rossum053b8df1998-11-25 16:18:00 +00003301 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003302
3303 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003304 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003305 int list_len;
3306
Guido van Rossum053b8df1998-11-25 16:18:00 +00003307 slice=Pdata_popList(self->stack, x);
3308 list_len = PyList_GET_SIZE(list);
3309 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003310 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003311 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003312 }
3313 else {
3314
Guido van Rossum053b8df1998-11-25 16:18:00 +00003315 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003316 return -1;
3317
3318 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003319 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003320
Guido van Rossum053b8df1998-11-25 16:18:00 +00003321 value=self->stack->data[i];
3322 junk=0;
3323 ARG_TUP(self, value);
3324 if (self->arg) {
3325 junk = PyObject_CallObject(append_method, self->arg);
3326 FREE_ARG_TUP(self);
3327 }
3328 if (! junk) {
3329 Pdata_clear(self->stack, i+1);
3330 self->stack->length=x;
3331 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003332 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003333 }
3334 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003335 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003336 self->stack->length=x;
3337 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003338 }
3339
Guido van Rossum60456fd1997-04-09 17:36:32 +00003340 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003341}
3342
3343
3344static int
3345load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003346 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003347}
3348
3349
3350static int
3351load_appends(Unpicklerobject *self) {
3352 return do_append(self, marker(self));
3353}
3354
3355
3356static int
3357do_setitems(Unpicklerobject *self, int x) {
3358 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003359 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003360
Guido van Rossum053b8df1998-11-25 16:18:00 +00003361 UNLESS ((len=self->stack->length) >= x
3362 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003363
Guido van Rossum053b8df1998-11-25 16:18:00 +00003364 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003365
Guido van Rossum053b8df1998-11-25 16:18:00 +00003366 for (i = x+1; i < len; i += 2) {
3367 key =self->stack->data[i-1];
3368 value=self->stack->data[i ];
3369 if (PyObject_SetItem(dict, key, value) < 0) {
3370 r=-1;
3371 break;
3372 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003373 }
3374
Guido van Rossum053b8df1998-11-25 16:18:00 +00003375 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003376
Guido van Rossum053b8df1998-11-25 16:18:00 +00003377 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003378}
3379
3380
3381static int
3382load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003383 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003384}
3385
Guido van Rossum60456fd1997-04-09 17:36:32 +00003386static int
3387load_setitems(Unpicklerobject *self) {
3388 return do_setitems(self, marker(self));
3389}
3390
3391
3392static int
3393load_build(Unpicklerobject *self) {
3394 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3395 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003396 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003397
Guido van Rossum053b8df1998-11-25 16:18:00 +00003398 if (self->stack->length < 2) return stackUnderflow();
3399 PDATA_POP(self->stack, value);
3400 if (! value) return -1;
3401 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003402
Guido van Rossum053b8df1998-11-25 16:18:00 +00003403 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3404 ARG_TUP(self, value);
3405 if (self->arg) {
3406 junk = PyObject_CallObject(__setstate__, self->arg);
3407 FREE_ARG_TUP(self);
3408 }
3409 Py_DECREF(__setstate__);
3410 if (! junk) return -1;
3411 Py_DECREF(junk);
3412 return 0;
3413 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003414
Guido van Rossum053b8df1998-11-25 16:18:00 +00003415 PyErr_Clear();
3416 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003417 i = 0;
3418 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003419 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3420 r=-1;
3421 break;
3422 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003423 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003424 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003425 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003426 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003427
Guido van Rossum053b8df1998-11-25 16:18:00 +00003428 Py_XDECREF(value);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003429
Guido van Rossum053b8df1998-11-25 16:18:00 +00003430 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003431}
3432
3433
3434static int
3435load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003436 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003437
Guido van Rossum053b8df1998-11-25 16:18:00 +00003438 if ((self->num_marks + 1) >= self->marks_size) {
3439 s=self->marks_size+20;
3440 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003441 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003442 self->marks=(int *)malloc(s * sizeof(int));
3443 else
3444 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003445 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003446 PyErr_NoMemory();
3447 return -1;
3448 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003449 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003450 }
3451
Guido van Rossum053b8df1998-11-25 16:18:00 +00003452 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003453
3454 return 0;
3455}
3456
3457static int
3458load_reduce(Unpicklerobject *self) {
3459 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003460
Guido van Rossum053b8df1998-11-25 16:18:00 +00003461 PDATA_POP(self->stack, arg_tup);
3462 if (! arg_tup) return -1;
3463 PDATA_POP(self->stack, callable);
3464 if (callable) {
3465 ob = Instance_New(callable, arg_tup);
3466 Py_DECREF(callable);
3467 }
3468 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003469
Guido van Rossum053b8df1998-11-25 16:18:00 +00003470 if (! ob) return -1;
3471
3472 PDATA_PUSH(self->stack, ob, -1);
3473 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003474}
3475
3476static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003477load(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003478 PyObject *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003479 char *s;
3480
Guido van Rossum60456fd1997-04-09 17:36:32 +00003481 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003482 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003483
3484 while (1) {
3485 if ((*self->read_func)(self, &s, 1) < 0)
3486 break;
3487
3488 switch (s[0]) {
3489 case NONE:
3490 if (load_none(self) < 0)
3491 break;
3492 continue;
3493
3494 case BININT:
3495 if (load_binint(self) < 0)
3496 break;
3497 continue;
3498
3499 case BININT1:
3500 if (load_binint1(self) < 0)
3501 break;
3502 continue;
3503
3504 case BININT2:
3505 if (load_binint2(self) < 0)
3506 break;
3507 continue;
3508
3509 case INT:
3510 if (load_int(self) < 0)
3511 break;
3512 continue;
3513
3514 case LONG:
3515 if (load_long(self) < 0)
3516 break;
3517 continue;
3518
3519 case FLOAT:
3520 if (load_float(self) < 0)
3521 break;
3522 continue;
3523
Guido van Rossum60456fd1997-04-09 17:36:32 +00003524 case BINFLOAT:
3525 if (load_binfloat(self) < 0)
3526 break;
3527 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003528
3529 case BINSTRING:
3530 if (load_binstring(self) < 0)
3531 break;
3532 continue;
3533
3534 case SHORT_BINSTRING:
3535 if (load_short_binstring(self) < 0)
3536 break;
3537 continue;
3538
3539 case STRING:
3540 if (load_string(self) < 0)
3541 break;
3542 continue;
3543
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003544 case UNICODE:
3545 if (load_unicode(self) < 0)
3546 break;
3547 continue;
3548
3549 case BINUNICODE:
3550 if (load_binunicode(self) < 0)
3551 break;
3552 continue;
3553
Guido van Rossum60456fd1997-04-09 17:36:32 +00003554 case EMPTY_TUPLE:
3555 if (load_empty_tuple(self) < 0)
3556 break;
3557 continue;
3558
3559 case TUPLE:
3560 if (load_tuple(self) < 0)
3561 break;
3562 continue;
3563
3564 case EMPTY_LIST:
3565 if (load_empty_list(self) < 0)
3566 break;
3567 continue;
3568
3569 case LIST:
3570 if (load_list(self) < 0)
3571 break;
3572 continue;
3573
3574 case EMPTY_DICT:
3575 if (load_empty_dict(self) < 0)
3576 break;
3577 continue;
3578
3579 case DICT:
3580 if (load_dict(self) < 0)
3581 break;
3582 continue;
3583
3584 case OBJ:
3585 if (load_obj(self) < 0)
3586 break;
3587 continue;
3588
3589 case INST:
3590 if (load_inst(self) < 0)
3591 break;
3592 continue;
3593
3594 case GLOBAL:
3595 if (load_global(self) < 0)
3596 break;
3597 continue;
3598
3599 case APPEND:
3600 if (load_append(self) < 0)
3601 break;
3602 continue;
3603
3604 case APPENDS:
3605 if (load_appends(self) < 0)
3606 break;
3607 continue;
3608
3609 case BUILD:
3610 if (load_build(self) < 0)
3611 break;
3612 continue;
3613
3614 case DUP:
3615 if (load_dup(self) < 0)
3616 break;
3617 continue;
3618
3619 case BINGET:
3620 if (load_binget(self) < 0)
3621 break;
3622 continue;
3623
3624 case LONG_BINGET:
3625 if (load_long_binget(self) < 0)
3626 break;
3627 continue;
3628
3629 case GET:
3630 if (load_get(self) < 0)
3631 break;
3632 continue;
3633
3634 case MARK:
3635 if (load_mark(self) < 0)
3636 break;
3637 continue;
3638
3639 case BINPUT:
3640 if (load_binput(self) < 0)
3641 break;
3642 continue;
3643
3644 case LONG_BINPUT:
3645 if (load_long_binput(self) < 0)
3646 break;
3647 continue;
3648
3649 case PUT:
3650 if (load_put(self) < 0)
3651 break;
3652 continue;
3653
3654 case POP:
3655 if (load_pop(self) < 0)
3656 break;
3657 continue;
3658
3659 case POP_MARK:
3660 if (load_pop_mark(self) < 0)
3661 break;
3662 continue;
3663
3664 case SETITEM:
3665 if (load_setitem(self) < 0)
3666 break;
3667 continue;
3668
3669 case SETITEMS:
3670 if (load_setitems(self) < 0)
3671 break;
3672 continue;
3673
3674 case STOP:
3675 break;
3676
3677 case PERSID:
3678 if (load_persid(self) < 0)
3679 break;
3680 continue;
3681
3682 case BINPERSID:
3683 if (load_binpersid(self) < 0)
3684 break;
3685 continue;
3686
3687 case REDUCE:
3688 if (load_reduce(self) < 0)
3689 break;
3690 continue;
3691
3692 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003693 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003694 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003695 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003696 }
3697
3698 break;
3699 }
3700
Guido van Rossum053b8df1998-11-25 16:18:00 +00003701 if ((err = PyErr_Occurred())) {
3702 if (err == PyExc_EOFError) {
3703 PyErr_SetNone(PyExc_EOFError);
3704 }
3705 return NULL;
3706 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003707
Guido van Rossum053b8df1998-11-25 16:18:00 +00003708 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003709 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003710}
3711
3712
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003713/* No-load functions to support noload, which is used to
3714 find persistent references. */
3715
3716static int
3717noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003718 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003719
3720 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003721 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003722}
3723
3724
3725static int
3726noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003727 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003728 char *s;
3729
3730 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003731 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003732 if ((*self->readline_func)(self, &s) < 0) return -1;
3733 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003734 PDATA_APPEND(self->stack, Py_None,-1);
3735 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003736}
3737
3738static int
3739noload_global(Unpicklerobject *self) {
3740 char *s;
3741
3742 if ((*self->readline_func)(self, &s) < 0) return -1;
3743 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003744 PDATA_APPEND(self->stack, Py_None,-1);
3745 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003746}
3747
3748static int
3749noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003750
Guido van Rossum053b8df1998-11-25 16:18:00 +00003751 if (self->stack->length < 2) return stackUnderflow();
3752 Pdata_clear(self->stack, self->stack->length-2);
3753 PDATA_APPEND(self->stack, Py_None,-1);
3754 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003755}
3756
3757static int
3758noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003759
Guido van Rossum053b8df1998-11-25 16:18:00 +00003760 if (self->stack->length < 1) return stackUnderflow();
3761 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003762 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003763}
3764
3765
3766static PyObject *
3767noload(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003768 PyObject *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003769 char *s;
3770
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003771 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003772 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003773
3774 while (1) {
3775 if ((*self->read_func)(self, &s, 1) < 0)
3776 break;
3777
3778 switch (s[0]) {
3779 case NONE:
3780 if (load_none(self) < 0)
3781 break;
3782 continue;
3783
3784 case BININT:
3785 if (load_binint(self) < 0)
3786 break;
3787 continue;
3788
3789 case BININT1:
3790 if (load_binint1(self) < 0)
3791 break;
3792 continue;
3793
3794 case BININT2:
3795 if (load_binint2(self) < 0)
3796 break;
3797 continue;
3798
3799 case INT:
3800 if (load_int(self) < 0)
3801 break;
3802 continue;
3803
3804 case LONG:
3805 if (load_long(self) < 0)
3806 break;
3807 continue;
3808
3809 case FLOAT:
3810 if (load_float(self) < 0)
3811 break;
3812 continue;
3813
3814 case BINFLOAT:
3815 if (load_binfloat(self) < 0)
3816 break;
3817 continue;
3818
3819 case BINSTRING:
3820 if (load_binstring(self) < 0)
3821 break;
3822 continue;
3823
3824 case SHORT_BINSTRING:
3825 if (load_short_binstring(self) < 0)
3826 break;
3827 continue;
3828
3829 case STRING:
3830 if (load_string(self) < 0)
3831 break;
3832 continue;
3833
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003834 case UNICODE:
3835 if (load_unicode(self) < 0)
3836 break;
3837 continue;
3838
3839 case BINUNICODE:
3840 if (load_binunicode(self) < 0)
3841 break;
3842 continue;
3843
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003844 case EMPTY_TUPLE:
3845 if (load_empty_tuple(self) < 0)
3846 break;
3847 continue;
3848
3849 case TUPLE:
3850 if (load_tuple(self) < 0)
3851 break;
3852 continue;
3853
3854 case EMPTY_LIST:
3855 if (load_empty_list(self) < 0)
3856 break;
3857 continue;
3858
3859 case LIST:
3860 if (load_list(self) < 0)
3861 break;
3862 continue;
3863
3864 case EMPTY_DICT:
3865 if (load_empty_dict(self) < 0)
3866 break;
3867 continue;
3868
3869 case DICT:
3870 if (load_dict(self) < 0)
3871 break;
3872 continue;
3873
3874 case OBJ:
3875 if (noload_obj(self) < 0)
3876 break;
3877 continue;
3878
3879 case INST:
3880 if (noload_inst(self) < 0)
3881 break;
3882 continue;
3883
3884 case GLOBAL:
3885 if (noload_global(self) < 0)
3886 break;
3887 continue;
3888
3889 case APPEND:
3890 if (load_append(self) < 0)
3891 break;
3892 continue;
3893
3894 case APPENDS:
3895 if (load_appends(self) < 0)
3896 break;
3897 continue;
3898
3899 case BUILD:
3900 if (noload_build(self) < 0)
3901 break;
3902 continue;
3903
3904 case DUP:
3905 if (load_dup(self) < 0)
3906 break;
3907 continue;
3908
3909 case BINGET:
3910 if (load_binget(self) < 0)
3911 break;
3912 continue;
3913
3914 case LONG_BINGET:
3915 if (load_long_binget(self) < 0)
3916 break;
3917 continue;
3918
3919 case GET:
3920 if (load_get(self) < 0)
3921 break;
3922 continue;
3923
3924 case MARK:
3925 if (load_mark(self) < 0)
3926 break;
3927 continue;
3928
3929 case BINPUT:
3930 if (load_binput(self) < 0)
3931 break;
3932 continue;
3933
3934 case LONG_BINPUT:
3935 if (load_long_binput(self) < 0)
3936 break;
3937 continue;
3938
3939 case PUT:
3940 if (load_put(self) < 0)
3941 break;
3942 continue;
3943
3944 case POP:
3945 if (load_pop(self) < 0)
3946 break;
3947 continue;
3948
3949 case POP_MARK:
3950 if (load_pop_mark(self) < 0)
3951 break;
3952 continue;
3953
3954 case SETITEM:
3955 if (load_setitem(self) < 0)
3956 break;
3957 continue;
3958
3959 case SETITEMS:
3960 if (load_setitems(self) < 0)
3961 break;
3962 continue;
3963
3964 case STOP:
3965 break;
3966
3967 case PERSID:
3968 if (load_persid(self) < 0)
3969 break;
3970 continue;
3971
3972 case BINPERSID:
3973 if (load_binpersid(self) < 0)
3974 break;
3975 continue;
3976
3977 case REDUCE:
3978 if (noload_reduce(self) < 0)
3979 break;
3980 continue;
3981
3982 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003983 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003984 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003985 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003986 }
3987
3988 break;
3989 }
3990
Guido van Rossum053b8df1998-11-25 16:18:00 +00003991 if ((err = PyErr_Occurred())) {
3992 if (err == PyExc_EOFError) {
3993 PyErr_SetNone(PyExc_EOFError);
3994 }
3995 return NULL;
3996 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003997
Guido van Rossum053b8df1998-11-25 16:18:00 +00003998 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003999 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004000}
4001
4002
Guido van Rossum60456fd1997-04-09 17:36:32 +00004003static PyObject *
4004Unpickler_load(Unpicklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00004005 UNLESS (PyArg_ParseTuple(args, ":load"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004006 return NULL;
4007
4008 return load(self);
4009}
4010
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004011static PyObject *
4012Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00004013 UNLESS (PyArg_ParseTuple(args, ":noload"))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004014 return NULL;
4015
4016 return noload(self);
4017}
4018
Guido van Rossum60456fd1997-04-09 17:36:32 +00004019
4020static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004021 {"load", (PyCFunction)Unpickler_load, 1,
4022 "load() -- Load a pickle"
4023 },
4024 {"noload", (PyCFunction)Unpickler_noload, 1,
4025 "noload() -- not load a pickle, but go through most of the motions\n"
4026 "\n"
4027 "This function can be used to read past a pickle without instantiating\n"
4028 "any objects or importing any modules. It can also be used to find all\n"
4029 "persistent references without instantiating any objects or importing\n"
4030 "any modules.\n"
4031 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004032 {NULL, NULL} /* sentinel */
4033};
4034
4035
4036static Unpicklerobject *
4037newUnpicklerobject(PyObject *f) {
4038 Unpicklerobject *self;
4039
Guido van Rossum053b8df1998-11-25 16:18:00 +00004040 UNLESS (self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004041 return NULL;
4042
4043 self->file = NULL;
4044 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004045 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004046 self->pers_func = NULL;
4047 self->last_string = NULL;
4048 self->marks = NULL;
4049 self->num_marks = 0;
4050 self->marks_size = 0;
4051 self->buf_size = 0;
4052 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00004053 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00004054 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004055 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004056
Guido van Rossum053b8df1998-11-25 16:18:00 +00004057 UNLESS (self->memo = PyDict_New()) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004058 Py_XDECREF((PyObject *)self);
4059 return NULL;
4060 }
4061
4062 Py_INCREF(f);
4063 self->file = f;
4064
4065 /* Set read, readline based on type of f */
4066 if (PyFile_Check(f)) {
4067 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004068 if (self->fp == NULL) {
4069 PyErr_SetString(PyExc_IOError, "input file closed");
4070 return NULL;
4071 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004072 self->read_func = read_file;
4073 self->readline_func = readline_file;
4074 }
4075 else if (PycStringIO_InputCheck(f)) {
4076 self->fp = NULL;
4077 self->read_func = read_cStringIO;
4078 self->readline_func = readline_cStringIO;
4079 }
4080 else {
4081
4082 self->fp = NULL;
4083 self->read_func = read_other;
4084 self->readline_func = readline_other;
4085
Guido van Rossum053b8df1998-11-25 16:18:00 +00004086 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004087 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004088 PyErr_Clear();
4089 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
4090 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00004091 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004092 }
4093 }
4094
Guido van Rossum053b8df1998-11-25 16:18:00 +00004095 if (PyEval_GetRestricted()) {
4096 /* Restricted execution, get private tables */
4097 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004098
Guido van Rossum053b8df1998-11-25 16:18:00 +00004099 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
4100 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
4101 Py_DECREF(m);
4102 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004103 }
4104 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004105 self->safe_constructors=safe_constructors;
4106 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004107 }
4108
Guido van Rossum60456fd1997-04-09 17:36:32 +00004109 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004110
4111err:
4112 Py_DECREF((PyObject *)self);
4113 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004114}
4115
4116
4117static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004118get_Unpickler(PyObject *self, PyObject *args) {
4119 PyObject *file;
4120
Guido van Rossum43713e52000-02-29 13:59:29 +00004121 UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004122 return NULL;
4123 return (PyObject *)newUnpicklerobject(file);
4124}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004125
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004126
Guido van Rossum60456fd1997-04-09 17:36:32 +00004127static void
4128Unpickler_dealloc(Unpicklerobject *self) {
4129 Py_XDECREF(self->readline);
4130 Py_XDECREF(self->read);
4131 Py_XDECREF(self->file);
4132 Py_XDECREF(self->memo);
4133 Py_XDECREF(self->stack);
4134 Py_XDECREF(self->pers_func);
4135 Py_XDECREF(self->arg);
4136 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004137 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004138
Guido van Rossum60456fd1997-04-09 17:36:32 +00004139 if (self->marks) {
4140 free(self->marks);
4141 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004142
Guido van Rossum60456fd1997-04-09 17:36:32 +00004143 if (self->buf_size) {
4144 free(self->buf);
4145 }
4146
4147 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004148}
4149
4150
4151static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004152Unpickler_getattr(Unpicklerobject *self, char *name) {
4153 if (!strcmp(name, "persistent_load")) {
4154 if (!self->pers_func) {
4155 PyErr_SetString(PyExc_AttributeError, name);
4156 return NULL;
4157 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004158
Guido van Rossum60456fd1997-04-09 17:36:32 +00004159 Py_INCREF(self->pers_func);
4160 return self->pers_func;
4161 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004162
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004163 if (!strcmp(name, "find_global")) {
4164 if (!self->find_class) {
4165 PyErr_SetString(PyExc_AttributeError, name);
4166 return NULL;
4167 }
4168
4169 Py_INCREF(self->find_class);
4170 return self->find_class;
4171 }
4172
Guido van Rossum60456fd1997-04-09 17:36:32 +00004173 if (!strcmp(name, "memo")) {
4174 if (!self->memo) {
4175 PyErr_SetString(PyExc_AttributeError, name);
4176 return NULL;
4177 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004178
Guido van Rossum60456fd1997-04-09 17:36:32 +00004179 Py_INCREF(self->memo);
4180 return self->memo;
4181 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004182
Guido van Rossum60456fd1997-04-09 17:36:32 +00004183 if (!strcmp(name, "UnpicklingError")) {
4184 Py_INCREF(UnpicklingError);
4185 return UnpicklingError;
4186 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004187
Guido van Rossum60456fd1997-04-09 17:36:32 +00004188 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4189}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004190
Guido van Rossum60456fd1997-04-09 17:36:32 +00004191
4192static int
4193Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004194
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004195 if (!strcmp(name, "persistent_load")) {
4196 Py_XDECREF(self->pers_func);
4197 self->pers_func = value;
4198 Py_XINCREF(value);
4199 return 0;
4200 }
4201
4202 if (!strcmp(name, "find_global")) {
4203 Py_XDECREF(self->find_class);
4204 self->find_class = value;
4205 Py_XINCREF(value);
4206 return 0;
4207 }
4208
Guido van Rossum053b8df1998-11-25 16:18:00 +00004209 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004210 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004211 "attribute deletion is not supported");
4212 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004213 }
4214
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004215 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004216 if (! PyDict_Check(value)) {
4217 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4218 return -1;
4219 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004220 Py_XDECREF(self->memo);
4221 self->memo = value;
4222 Py_INCREF(value);
4223 return 0;
4224 }
4225
Guido van Rossum60456fd1997-04-09 17:36:32 +00004226 PyErr_SetString(PyExc_AttributeError, name);
4227 return -1;
4228}
4229
4230
4231static PyObject *
4232cpm_dump(PyObject *self, PyObject *args) {
4233 PyObject *ob, *file, *res = NULL;
4234 Picklerobject *pickler = 0;
4235 int bin = 0;
4236
Guido van Rossum053b8df1998-11-25 16:18:00 +00004237 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004238 goto finally;
4239
Guido van Rossum053b8df1998-11-25 16:18:00 +00004240 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004241 goto finally;
4242
4243 if (dump(pickler, ob) < 0)
4244 goto finally;
4245
4246 Py_INCREF(Py_None);
4247 res = Py_None;
4248
4249finally:
4250 Py_XDECREF(pickler);
4251
4252 return res;
4253}
4254
4255
4256static PyObject *
4257cpm_dumps(PyObject *self, PyObject *args) {
4258 PyObject *ob, *file = 0, *res = NULL;
4259 Picklerobject *pickler = 0;
4260 int bin = 0;
4261
Guido van Rossum43713e52000-02-29 13:59:29 +00004262 UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004263 goto finally;
4264
Guido van Rossum053b8df1998-11-25 16:18:00 +00004265 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004266 goto finally;
4267
Guido van Rossum053b8df1998-11-25 16:18:00 +00004268 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004269 goto finally;
4270
4271 if (dump(pickler, ob) < 0)
4272 goto finally;
4273
4274 res = PycStringIO->cgetvalue(file);
4275
4276finally:
4277 Py_XDECREF(pickler);
4278 Py_XDECREF(file);
4279
4280 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004281}
4282
4283
4284static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004285cpm_load(PyObject *self, PyObject *args) {
4286 Unpicklerobject *unpickler = 0;
4287 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004288
Guido van Rossum43713e52000-02-29 13:59:29 +00004289 UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004290 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004291
Guido van Rossum053b8df1998-11-25 16:18:00 +00004292 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004293 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004294
Guido van Rossum60456fd1997-04-09 17:36:32 +00004295 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004296
Guido van Rossum60456fd1997-04-09 17:36:32 +00004297finally:
4298 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004299
Guido van Rossum60456fd1997-04-09 17:36:32 +00004300 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004301}
4302
4303
4304static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004305cpm_loads(PyObject *self, PyObject *args) {
4306 PyObject *ob, *file = 0, *res = NULL;
4307 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004308
Guido van Rossum43713e52000-02-29 13:59:29 +00004309 UNLESS (PyArg_ParseTuple(args, "S:loads", &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 (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004313 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004314
Guido van Rossum053b8df1998-11-25 16:18:00 +00004315 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004316 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004317
Guido van Rossum60456fd1997-04-09 17:36:32 +00004318 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004319
Guido van Rossum60456fd1997-04-09 17:36:32 +00004320finally:
4321 Py_XDECREF(file);
4322 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004323
Guido van Rossum60456fd1997-04-09 17:36:32 +00004324 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004325}
4326
4327
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004328static char Unpicklertype__doc__[] =
4329"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004330
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004331static PyTypeObject Unpicklertype = {
4332 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004333 0, /*ob_size*/
4334 "Unpickler", /*tp_name*/
4335 sizeof(Unpicklerobject), /*tp_basicsize*/
4336 0, /*tp_itemsize*/
4337 /* methods */
4338 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4339 (printfunc)0, /*tp_print*/
4340 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4341 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4342 (cmpfunc)0, /*tp_compare*/
4343 (reprfunc)0, /*tp_repr*/
4344 0, /*tp_as_number*/
4345 0, /*tp_as_sequence*/
4346 0, /*tp_as_mapping*/
4347 (hashfunc)0, /*tp_hash*/
4348 (ternaryfunc)0, /*tp_call*/
4349 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004350
Guido van Rossum60456fd1997-04-09 17:36:32 +00004351 /* Space for future expansion */
4352 0L,0L,0L,0L,
4353 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004354};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004355
Guido van Rossum60456fd1997-04-09 17:36:32 +00004356static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004357 {"dump", (PyCFunction)cpm_dump, 1,
4358 "dump(object, file, [binary]) --"
4359 "Write an object in pickle format to the given file\n"
4360 "\n"
4361 "If the optional argument, binary, is provided and is true, then the\n"
4362 "pickle will be written in binary format, which is more space and\n"
4363 "computationally efficient. \n"
4364 },
4365 {"dumps", (PyCFunction)cpm_dumps, 1,
4366 "dumps(object, [binary]) --"
4367 "Return a string containing an object in pickle format\n"
4368 "\n"
4369 "If the optional argument, binary, is provided and is true, then the\n"
4370 "pickle will be written in binary format, which is more space and\n"
4371 "computationally efficient. \n"
4372 },
4373 {"load", (PyCFunction)cpm_load, 1,
4374 "load(file) -- Load a pickle from the given file"},
4375 {"loads", (PyCFunction)cpm_loads, 1,
4376 "loads(string) -- Load a pickle from the given string"},
4377 {"Pickler", (PyCFunction)get_Pickler, 1,
4378 "Pickler(file, [binary]) -- Create a pickler\n"
4379 "\n"
4380 "If the optional argument, binary, is provided and is true, then\n"
4381 "pickles will be written in binary format, which is more space and\n"
4382 "computationally efficient. \n"
4383 },
4384 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4385 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004386 { NULL, NULL }
4387};
4388
Guido van Rossum60456fd1997-04-09 17:36:32 +00004389static int
4390init_stuff(PyObject *module, PyObject *module_dict) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004391 PyObject *string, *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004392
4393#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4394
4395 INIT_STR(__class__);
4396 INIT_STR(__getinitargs__);
4397 INIT_STR(__dict__);
4398 INIT_STR(__getstate__);
4399 INIT_STR(__setstate__);
4400 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004401 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004402 INIT_STR(__reduce__);
4403 INIT_STR(write);
4404 INIT_STR(__safe_for_unpickling__);
4405 INIT_STR(append);
4406 INIT_STR(read);
4407 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004408 INIT_STR(copy_reg);
4409 INIT_STR(dispatch_table);
4410 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004411 INIT_STR(__basicnew__);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004412 UNLESS (empty_str=PyString_FromString("")) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004413
Guido van Rossum053b8df1998-11-25 16:18:00 +00004414 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004415 return -1;
4416
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004417 /* These next few are special because we want to use different
4418 ones in restricted mode. */
4419
Guido van Rossum053b8df1998-11-25 16:18:00 +00004420 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004421 return -1;
4422
Guido van Rossum053b8df1998-11-25 16:18:00 +00004423 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4424 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004425 return -1;
4426
4427 Py_DECREF(copy_reg);
4428
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004429 /* Down to here ********************************** */
4430
Guido van Rossum053b8df1998-11-25 16:18:00 +00004431 UNLESS (string = PyImport_ImportModule("string"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004432 return -1;
4433
Guido van Rossum053b8df1998-11-25 16:18:00 +00004434 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004435 return -1;
4436
4437 Py_DECREF(string);
4438
Guido van Rossum053b8df1998-11-25 16:18:00 +00004439 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004440 return -1;
4441
Guido van Rossumc03158b1999-06-09 15:23:31 +00004442 /* Ugh */
4443 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4444 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4445 return -1;
4446
4447 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004448 UNLESS (r=PyRun_String(
4449 "def __init__(self, *args): self.args=args\n\n"
4450 "def __str__(self):\n"
4451 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4452 Py_file_input,
4453 module_dict, t) ) return -1;
4454 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004455
4456 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4457 return -1;
4458
4459 Py_DECREF(t);
4460
4461
4462 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
4463 PickleError, NULL))
4464 return -1;
4465
4466 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004467 UNLESS (r=PyRun_String(
4468 "def __init__(self, *args): self.args=args\n\n"
4469 "def __str__(self):\n"
4470 " a=self.args\n"
4471 " a=a and type(a[0]) or '(what)'\n"
4472 " return 'Cannot pickle %s objects' % a\n"
4473 , Py_file_input,
4474 module_dict, t) ) return -1;
4475 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004476
4477 UNLESS (UnpickleableError = PyErr_NewException(
4478 "cPickle.UnpickleableError", PicklingError, t))
4479 return -1;
4480
4481 Py_DECREF(t);
4482
4483 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
4484 PickleError, NULL))
4485 return -1;
4486
4487 if (PyDict_SetItemString(module_dict, "PickleError",
4488 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004489 return -1;
4490
4491 if (PyDict_SetItemString(module_dict, "PicklingError",
4492 PicklingError) < 0)
4493 return -1;
4494
Guido van Rossum60456fd1997-04-09 17:36:32 +00004495 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4496 UnpicklingError) < 0)
4497 return -1;
4498
Guido van Rossumc03158b1999-06-09 15:23:31 +00004499 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4500 UnpickleableError) < 0)
4501 return -1;
4502
Guido van Rossum053b8df1998-11-25 16:18:00 +00004503 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4504 return -1;
4505
4506 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4507 BadPickleGet) < 0)
4508 return -1;
4509
Guido van Rossum60456fd1997-04-09 17:36:32 +00004510 PycString_IMPORT;
4511
4512 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004513}
4514
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004515#ifndef DL_EXPORT /* declarations for DLL import/export */
4516#define DL_EXPORT(RTYPE) RTYPE
4517#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004518DL_EXPORT(void)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004519initcPickle() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004520 PyObject *m, *d, *v;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004521 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004522 PyObject *format_version;
4523 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004524
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004525 Picklertype.ob_type = &PyType_Type;
4526 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004527 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004528
Guido van Rossum60456fd1997-04-09 17:36:32 +00004529 /* Create the module and add the functions */
4530 m = Py_InitModule4("cPickle", cPickle_methods,
4531 cPickle_module_documentation,
4532 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004533
Guido van Rossum60456fd1997-04-09 17:36:32 +00004534 /* Add some symbolic constants to the module */
4535 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004536 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004537 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004538
Guido van Rossum60456fd1997-04-09 17:36:32 +00004539 format_version = PyString_FromString("1.3");
4540 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004541
Guido van Rossum60456fd1997-04-09 17:36:32 +00004542 PyDict_SetItemString(d, "format_version", format_version);
4543 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004544 Py_XDECREF(format_version);
4545 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004546
Guido van Rossum60456fd1997-04-09 17:36:32 +00004547 init_stuff(m, d);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004548}