blob: 6eeb0a40c10e9333a0abd05de7982c35ac0a743a [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
Guido van Rossumb18618d2000-05-03 23:44:39 +0000174 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000175}
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
Guido van Rossumb18618d2000-05-03 23:44:39 +0000189 UNLESS (self = PyObject_New(Pdata, &PdataType)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000190 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;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000252 for (i=start, j=0 ; j < l; i++, j++)
253 PyTuple_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000254
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;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000266 for (i=start, j=0 ; j < l; i++, j++)
267 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000268
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 Rossumb18618d2000-05-03 23:44:39 +00002135 UNLESS (self = PyObject_New(Picklerobject, &Picklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002136 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002137
2138 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002139 self->write = NULL;
2140 self->memo = NULL;
2141 self->arg = NULL;
2142 self->pers_func = NULL;
2143 self->inst_pers_func = NULL;
2144 self->write_buf = NULL;
2145 self->bin = bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002146 self->fast = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002147 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002148 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002149
Guido van Rossum053b8df1998-11-25 16:18:00 +00002150 if (file)
2151 Py_INCREF(file);
2152 else
Guido van Rossum50f385c1998-12-04 18:48:44 +00002153 file=Pdata_New();
Guido van Rossum83addc72000-04-21 20:49:36 +00002154
2155 UNLESS (self->file = file)
2156 goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002157
Guido van Rossum83addc72000-04-21 20:49:36 +00002158 UNLESS (self->memo = PyDict_New())
2159 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002160
Guido van Rossum60456fd1997-04-09 17:36:32 +00002161 if (PyFile_Check(file)) {
2162 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002163 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00002164 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
2165 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002166 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002167 self->write_func = write_file;
2168 }
2169 else if (PycStringIO_OutputCheck(file)) {
2170 self->write_func = write_cStringIO;
2171 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002172 else if (file == Py_None) {
2173 self->write_func = write_none;
2174 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002175 else {
2176 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002177
Guido van Rossum053b8df1998-11-25 16:18:00 +00002178 if (! Pdata_Check(file)) {
2179 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002180 PyErr_Clear();
2181 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002182 "attribute");
2183 goto err;
2184 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002185 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002186
Guido van Rossum053b8df1998-11-25 16:18:00 +00002187 UNLESS (self->write_buf =
Guido van Rossum60456fd1997-04-09 17:36:32 +00002188 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
2189 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002190 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002191 }
2192 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002193
Guido van Rossum053b8df1998-11-25 16:18:00 +00002194 if (PyEval_GetRestricted()) {
2195 /* Restricted execution, get private tables */
2196 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002197
Guido van Rossum053b8df1998-11-25 16:18:00 +00002198 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2199 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2200 Py_DECREF(m);
2201 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002202 }
2203 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002204 self->dispatch_table=dispatch_table;
2205 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002206 }
2207
Guido van Rossum60456fd1997-04-09 17:36:32 +00002208 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002209
2210err:
2211 Py_DECREF((PyObject *)self);
2212 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002213}
2214
2215
2216static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002217get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002218 PyObject *file=NULL;
2219 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002220
Guido van Rossum053b8df1998-11-25 16:18:00 +00002221 bin=1;
Guido van Rossum43713e52000-02-29 13:59:29 +00002222 if (! PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002223 PyErr_Clear();
2224 bin=0;
Guido van Rossum43713e52000-02-29 13:59:29 +00002225 if (! PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002226 return NULL;
2227 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002228 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002229}
2230
2231
2232static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002233Pickler_dealloc(Picklerobject *self) {
2234 Py_XDECREF(self->write);
2235 Py_XDECREF(self->memo);
2236 Py_XDECREF(self->arg);
2237 Py_XDECREF(self->file);
2238 Py_XDECREF(self->pers_func);
2239 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002240 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002241
2242 if (self->write_buf) {
2243 free(self->write_buf);
2244 }
2245
Guido van Rossumb18618d2000-05-03 23:44:39 +00002246 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002247}
2248
2249
2250static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002251Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002252
2253 switch (*name) {
2254 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002255 if (strcmp(name, "persistent_id") == 0) {
2256 if (!self->pers_func) {
2257 PyErr_SetString(PyExc_AttributeError, name);
2258 return NULL;
2259 }
2260
2261 Py_INCREF(self->pers_func);
2262 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002263 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002264 break;
2265 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002266 if (strcmp(name, "memo") == 0) {
2267 if (!self->memo) {
2268 PyErr_SetString(PyExc_AttributeError, name);
2269 return NULL;
2270 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002271
Guido van Rossum60456fd1997-04-09 17:36:32 +00002272 Py_INCREF(self->memo);
2273 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002274 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002275 break;
2276 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002277 if (strcmp(name, "PicklingError") == 0) {
2278 Py_INCREF(PicklingError);
2279 return PicklingError;
2280 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002281 break;
2282 case 'b':
2283 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002284 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002285 break;
2286 case 'f':
2287 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002288 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002289 break;
2290 case 'g':
2291 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2292 PyErr_SetString(PyExc_AttributeError, name);
2293 return NULL;
2294 }
2295 break;
2296 }
2297 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002298}
2299
2300
2301int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002302Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002303
Guido van Rossum053b8df1998-11-25 16:18:00 +00002304 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002305 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002306 "attribute deletion is not supported");
2307 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002308 }
2309
Guido van Rossum60456fd1997-04-09 17:36:32 +00002310 if (strcmp(name, "persistent_id") == 0) {
2311 Py_XDECREF(self->pers_func);
2312 self->pers_func = value;
2313 Py_INCREF(value);
2314 return 0;
2315 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002316
Guido van Rossum60456fd1997-04-09 17:36:32 +00002317 if (strcmp(name, "inst_persistent_id") == 0) {
2318 Py_XDECREF(self->inst_pers_func);
2319 self->inst_pers_func = value;
2320 Py_INCREF(value);
2321 return 0;
2322 }
2323
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002324 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002325 if (! PyDict_Check(value)) {
2326 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2327 return -1;
2328 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002329 Py_XDECREF(self->memo);
2330 self->memo = value;
2331 Py_INCREF(value);
2332 return 0;
2333 }
2334
Guido van Rossum053b8df1998-11-25 16:18:00 +00002335 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002336 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002337 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002338 }
2339
Guido van Rossum053b8df1998-11-25 16:18:00 +00002340 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002341 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002342 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002343 }
2344
Guido van Rossum60456fd1997-04-09 17:36:32 +00002345 PyErr_SetString(PyExc_AttributeError, name);
2346 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002347}
2348
2349
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002350static char Picklertype__doc__[] =
2351"Objects that know how to pickle objects\n"
2352;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002353
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002354static PyTypeObject Picklertype = {
2355 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002356 0, /*ob_size*/
2357 "Pickler", /*tp_name*/
2358 sizeof(Picklerobject), /*tp_basicsize*/
2359 0, /*tp_itemsize*/
2360 /* methods */
2361 (destructor)Pickler_dealloc, /*tp_dealloc*/
2362 (printfunc)0, /*tp_print*/
2363 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2364 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2365 (cmpfunc)0, /*tp_compare*/
2366 (reprfunc)0, /*tp_repr*/
2367 0, /*tp_as_number*/
2368 0, /*tp_as_sequence*/
2369 0, /*tp_as_mapping*/
2370 (hashfunc)0, /*tp_hash*/
2371 (ternaryfunc)0, /*tp_call*/
2372 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002373
Guido van Rossum60456fd1997-04-09 17:36:32 +00002374 /* Space for future expansion */
2375 0L,0L,0L,0L,
2376 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002377};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002378
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002379static PyObject *
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002380find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002381 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002382
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002383 if (fc) {
2384 if (fc==Py_None) {
2385 PyErr_SetString(UnpicklingError,
2386 "Global and instance pickles are not supported.");
2387 return NULL;
2388 }
2389 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2390 }
2391
Jeremy Hyltond1055231998-08-11 19:52:51 +00002392 module = PySys_GetObject("modules");
2393 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002394 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002395
2396 module = PyDict_GetItem(module, py_module_name);
2397 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002398 module = PyImport_Import(py_module_name);
2399 if (!module)
2400 return NULL;
2401 global = PyObject_GetAttr(module, py_global_name);
2402 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002403 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002404 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002405 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002406 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002407 char buf[256 + 37];
2408 sprintf(buf, "Failed to import class %.128s from module %.128s",
2409 PyString_AS_STRING((PyStringObject*)py_global_name),
2410 PyString_AS_STRING((PyStringObject*)py_module_name));
2411 PyErr_SetString(PyExc_SystemError, buf);
2412 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002413 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002414 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002415}
2416
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002417static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002418marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002419 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002420 PyErr_SetString(UnpicklingError, "could not find MARK");
2421 return -1;
2422 }
2423
2424 return self->marks[--self->num_marks];
2425}
2426
2427
2428static int
2429load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002430 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002431 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002432}
2433
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002434static int
2435bad_readline() {
2436 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2437 return -1;
2438}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002439
2440static int
2441load_int(Unpicklerobject *self) {
2442 PyObject *py_int = 0;
2443 char *endptr, *s;
2444 int len, res = -1;
2445 long l;
2446
2447 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002448 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002449 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002450
2451 errno = 0;
2452 l = strtol(s, &endptr, 0);
2453
2454 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2455 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002456 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002457 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002458 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002459
Guido van Rossum053b8df1998-11-25 16:18:00 +00002460 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2461 PyErr_SetString(PyExc_ValueError,
2462 "could not convert string to int");
2463 goto finally;
2464 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002465 }
2466 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002467 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002468 }
2469
Guido van Rossum053b8df1998-11-25 16:18:00 +00002470 free(s);
2471 PDATA_PUSH(self->stack, py_int, -1);
2472 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002473
2474finally:
2475 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002476
2477 return res;
2478}
2479
2480
2481static long
2482calc_binint(char *s, int x) {
2483 unsigned char c;
2484 int i;
2485 long l;
2486
2487 for (i = 0, l = 0L; i < x; i++) {
2488 c = (unsigned char)s[i];
2489 l |= (long)c << (i * 8);
2490 }
2491
2492 return l;
2493}
2494
2495
2496static int
2497load_binintx(Unpicklerobject *self, char *s, int x) {
2498 PyObject *py_int = 0;
2499 long l;
2500
2501 l = calc_binint(s, x);
2502
Guido van Rossum053b8df1998-11-25 16:18:00 +00002503 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002504 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002505
Guido van Rossum053b8df1998-11-25 16:18:00 +00002506 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002507 return 0;
2508}
2509
2510
2511static int
2512load_binint(Unpicklerobject *self) {
2513 char *s;
2514
2515 if ((*self->read_func)(self, &s, 4) < 0)
2516 return -1;
2517
2518 return load_binintx(self, s, 4);
2519}
2520
2521
2522static int
2523load_binint1(Unpicklerobject *self) {
2524 char *s;
2525
2526 if ((*self->read_func)(self, &s, 1) < 0)
2527 return -1;
2528
2529 return load_binintx(self, s, 1);
2530}
2531
2532
2533static int
2534load_binint2(Unpicklerobject *self) {
2535 char *s;
2536
2537 if ((*self->read_func)(self, &s, 2) < 0)
2538 return -1;
2539
2540 return load_binintx(self, s, 2);
2541}
2542
2543static int
2544load_long(Unpicklerobject *self) {
2545 PyObject *l = 0;
2546 char *end, *s;
2547 int len, res = -1;
2548
Guido van Rossum60456fd1997-04-09 17:36:32 +00002549 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002550 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002551 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002552
Guido van Rossum053b8df1998-11-25 16:18:00 +00002553 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002554 goto finally;
2555
Guido van Rossum053b8df1998-11-25 16:18:00 +00002556 free(s);
2557 PDATA_PUSH(self->stack, l, -1);
2558 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002559
2560finally:
2561 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002562
2563 return res;
2564}
2565
2566
2567static int
2568load_float(Unpicklerobject *self) {
2569 PyObject *py_float = 0;
2570 char *endptr, *s;
2571 int len, res = -1;
2572 double d;
2573
2574 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002575 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002576 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002577
2578 errno = 0;
2579 d = strtod(s, &endptr);
2580
2581 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2582 PyErr_SetString(PyExc_ValueError,
2583 "could not convert string to float");
2584 goto finally;
2585 }
2586
Guido van Rossum053b8df1998-11-25 16:18:00 +00002587 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002588 goto finally;
2589
Guido van Rossum053b8df1998-11-25 16:18:00 +00002590 free(s);
2591 PDATA_PUSH(self->stack, py_float, -1);
2592 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002593
2594finally:
2595 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002596
2597 return res;
2598}
2599
Guido van Rossum60456fd1997-04-09 17:36:32 +00002600static int
2601load_binfloat(Unpicklerobject *self) {
2602 PyObject *py_float = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002603 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002604 long fhi, flo;
2605 double x;
2606 char *p;
2607
2608 if ((*self->read_func)(self, &p, 8) < 0)
2609 return -1;
2610
2611 /* First byte */
2612 s = (*p>>7) & 1;
2613 e = (*p & 0x7F) << 4;
2614 p++;
2615
2616 /* Second byte */
2617 e |= (*p>>4) & 0xF;
2618 fhi = (*p & 0xF) << 24;
2619 p++;
2620
2621 /* Third byte */
2622 fhi |= (*p & 0xFF) << 16;
2623 p++;
2624
2625 /* Fourth byte */
2626 fhi |= (*p & 0xFF) << 8;
2627 p++;
2628
2629 /* Fifth byte */
2630 fhi |= *p & 0xFF;
2631 p++;
2632
2633 /* Sixth byte */
2634 flo = (*p & 0xFF) << 16;
2635 p++;
2636
2637 /* Seventh byte */
2638 flo |= (*p & 0xFF) << 8;
2639 p++;
2640
2641 /* Eighth byte */
2642 flo |= *p & 0xFF;
2643
2644 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2645 x /= 268435456.0; /* 2**28 */
2646
2647 /* XXX This sadly ignores Inf/NaN */
2648 if (e == 0)
2649 e = -1022;
2650 else {
2651 x += 1.0;
2652 e -= 1023;
2653 }
2654 x = ldexp(x, e);
2655
2656 if (s)
2657 x = -x;
2658
Guido van Rossum053b8df1998-11-25 16:18:00 +00002659 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002660
Guido van Rossum053b8df1998-11-25 16:18:00 +00002661 PDATA_PUSH(self->stack, py_float, -1);
2662 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002663}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002664
2665static int
2666load_string(Unpicklerobject *self) {
2667 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002668 int len, res = -1, nslash;
2669 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002670
2671 static PyObject *eval_dict = 0;
2672
2673 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002674 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002675 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002676
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002677 /* Check for unquoted quotes (evil strings) */
2678 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002679 if (q != '"' && q != '\'') goto insecure;
2680 for (p=s+1, nslash=0; *p; p++) {
2681 if (*p==q && nslash%2==0) break;
2682 if (*p=='\\') nslash++;
2683 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002684 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002685 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002686 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002687 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002688 }
2689 else goto insecure;
2690 /********************************************/
2691
Guido van Rossum053b8df1998-11-25 16:18:00 +00002692 UNLESS (eval_dict)
2693 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002694 goto finally;
2695
Guido van Rossum053b8df1998-11-25 16:18:00 +00002696 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002697 goto finally;
2698
Guido van Rossum053b8df1998-11-25 16:18:00 +00002699 free(s);
2700 PDATA_PUSH(self->stack, str, -1);
2701 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002702
2703finally:
2704 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002705
2706 return res;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002707
2708insecure:
2709 free(s);
2710 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2711 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002712}
2713
2714
2715static int
2716load_binstring(Unpicklerobject *self) {
2717 PyObject *py_string = 0;
2718 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002719 char *s;
2720
Guido van Rossum053b8df1998-11-25 16:18:00 +00002721 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002722
2723 l = calc_binint(s, 4);
2724
2725 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002726 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002727
Guido van Rossum053b8df1998-11-25 16:18:00 +00002728 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2729 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002730
Guido van Rossum053b8df1998-11-25 16:18:00 +00002731 PDATA_PUSH(self->stack, py_string, -1);
2732 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002733}
2734
2735
2736static int
2737load_short_binstring(Unpicklerobject *self) {
2738 PyObject *py_string = 0;
2739 unsigned char l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002740 char *s;
2741
2742 if ((*self->read_func)(self, &s, 1) < 0)
2743 return -1;
2744
2745 l = (unsigned char)s[0];
2746
Guido van Rossum053b8df1998-11-25 16:18:00 +00002747 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002748
Guido van Rossum053b8df1998-11-25 16:18:00 +00002749 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002750
Guido van Rossum053b8df1998-11-25 16:18:00 +00002751 PDATA_PUSH(self->stack, py_string, -1);
2752 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002753}
2754
2755
2756static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002757load_unicode(Unpicklerobject *self) {
2758 PyObject *str = 0;
2759 int len, res = -1;
2760 char *s;
2761
2762 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2763 if (len < 2) return bad_readline();
2764
2765 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
2766 goto finally;
2767
2768 PDATA_PUSH(self->stack, str, -1);
2769 return 0;
2770
2771finally:
2772 return res;
2773}
2774
2775
2776static int
2777load_binunicode(Unpicklerobject *self) {
2778 PyObject *unicode;
2779 long l;
2780 char *s;
2781
2782 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2783
2784 l = calc_binint(s, 4);
2785
2786 if ((*self->read_func)(self, &s, l) < 0)
2787 return -1;
2788
2789 UNLESS (unicode = PyUnicode_DecodeUTF8(s, l, NULL))
2790 return -1;
2791
2792 PDATA_PUSH(self->stack, unicode, -1);
2793 return 0;
2794}
2795
2796
2797static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002798load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002799 PyObject *tup;
2800 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002801
Guido van Rossum053b8df1998-11-25 16:18:00 +00002802 if ((i = marker(self)) < 0) return -1;
2803 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2804 PDATA_PUSH(self->stack, tup, -1);
2805 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002806}
2807
2808static int
2809load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002810 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002811
Guido van Rossum053b8df1998-11-25 16:18:00 +00002812 UNLESS (tup=PyTuple_New(0)) return -1;
2813 PDATA_PUSH(self->stack, tup, -1);
2814 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002815}
2816
2817static int
2818load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002819 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002820
Guido van Rossum053b8df1998-11-25 16:18:00 +00002821 UNLESS (list=PyList_New(0)) return -1;
2822 PDATA_PUSH(self->stack, list, -1);
2823 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002824}
2825
2826static int
2827load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002828 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002829
Guido van Rossum053b8df1998-11-25 16:18:00 +00002830 UNLESS (dict=PyDict_New()) return -1;
2831 PDATA_PUSH(self->stack, dict, -1);
2832 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002833}
2834
2835
2836static int
2837load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002838 PyObject *list = 0;
2839 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002840
Guido van Rossum053b8df1998-11-25 16:18:00 +00002841 if ((i = marker(self)) < 0) return -1;
2842 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2843 PDATA_PUSH(self->stack, list, -1);
2844 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002845}
2846
2847static int
2848load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002849 PyObject *dict, *key, *value;
2850 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002851
Guido van Rossum053b8df1998-11-25 16:18:00 +00002852 if ((i = marker(self)) < 0) return -1;
2853 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002854
Guido van Rossum053b8df1998-11-25 16:18:00 +00002855 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002856
Guido van Rossum053b8df1998-11-25 16:18:00 +00002857 for (k = i+1; k < j; k += 2) {
2858 key =self->stack->data[k-1];
2859 value=self->stack->data[k ];
2860 if (PyDict_SetItem(dict, key, value) < 0) {
2861 Py_DECREF(dict);
2862 return -1;
2863 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002864 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002865 Pdata_clear(self->stack, i);
2866 PDATA_PUSH(self->stack, dict, -1);
2867 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002868}
2869
2870static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002871Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002872 int has_key;
2873 PyObject *safe=0, *r=0;
2874
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002875 if (PyClass_Check(cls)) {
2876 int l;
2877
Guido van Rossum053b8df1998-11-25 16:18:00 +00002878 if ((l=PyObject_Length(args)) < 0) goto err;
2879 UNLESS (l) {
2880 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002881
Guido van Rossum053b8df1998-11-25 16:18:00 +00002882 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2883 /* We have a class with no __getinitargs__, so bypass usual
2884 construction */
2885 PyInstanceObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002886
Guido van Rossum053b8df1998-11-25 16:18:00 +00002887 PyErr_Clear();
Guido van Rossumb18618d2000-05-03 23:44:39 +00002888 UNLESS (inst=PyObject_New(PyInstanceObject, &PyInstance_Type))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002889 goto err;
2890 inst->in_class=(PyClassObject*)cls;
2891 Py_INCREF(cls);
2892 UNLESS (inst->in_dict=PyDict_New()) {
2893 Py_DECREF(inst);
2894 goto err;
2895 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002896
Guido van Rossum053b8df1998-11-25 16:18:00 +00002897 return (PyObject *)inst;
2898 }
2899 Py_DECREF(__getinitargs__);
2900 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002901
Guido van Rossum053b8df1998-11-25 16:18:00 +00002902 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002903 else goto err;
2904 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002905
2906
2907 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2908 goto err;
2909
2910 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002911 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002912 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002913 cPickle_ErrFormat(UnpicklingError,
2914 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002915 Py_XDECREF(safe);
2916 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002917 }
2918
Guido van Rossum053b8df1998-11-25 16:18:00 +00002919 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002920 /* Special case, call cls.__basicnew__() */
2921 PyObject *basicnew;
2922
Guido van Rossum053b8df1998-11-25 16:18:00 +00002923 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002924 r=PyObject_CallObject(basicnew, NULL);
2925 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002926 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002927 }
2928
Guido van Rossum053b8df1998-11-25 16:18:00 +00002929 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002930
Guido van Rossum60456fd1997-04-09 17:36:32 +00002931err:
2932 {
2933 PyObject *tp, *v, *tb;
2934
2935 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002936 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2937 Py_XDECREF(v);
2938 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002939 }
2940 PyErr_Restore(tp,v,tb);
2941 }
2942 return NULL;
2943}
2944
2945
2946static int
2947load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002948 PyObject *class, *tup, *obj=0;
2949 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002950
Guido van Rossum053b8df1998-11-25 16:18:00 +00002951 if ((i = marker(self)) < 0) return -1;
2952 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
2953 PDATA_POP(self->stack, class);
2954 if (class) {
2955 obj = Instance_New(class, tup);
2956 Py_DECREF(class);
2957 }
2958 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002959
Guido van Rossum053b8df1998-11-25 16:18:00 +00002960 if (! obj) return -1;
2961 PDATA_PUSH(self->stack, obj, -1);
2962 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002963}
2964
2965
2966static int
2967load_inst(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002968 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002969 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002970 char *s;
2971
Guido van Rossum053b8df1998-11-25 16:18:00 +00002972 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002973
Guido van Rossum053b8df1998-11-25 16:18:00 +00002974 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002975 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002976 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002977
Guido van Rossum053b8df1998-11-25 16:18:00 +00002978 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002979 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002980 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002981 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002982 Py_DECREF(class_name);
2983 }
2984 }
2985 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002986
Guido van Rossum053b8df1998-11-25 16:18:00 +00002987 if (! class) return -1;
2988
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002989 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002990 obj = Instance_New(class, tup);
2991 Py_DECREF(tup);
2992 }
2993 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002994
Guido van Rossum053b8df1998-11-25 16:18:00 +00002995 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002996
Guido van Rossum053b8df1998-11-25 16:18:00 +00002997 PDATA_PUSH(self->stack, obj, -1);
2998 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002999}
3000
3001
3002static int
3003load_global(Unpicklerobject *self) {
3004 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003005 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003006 char *s;
3007
Guido van Rossum053b8df1998-11-25 16:18:00 +00003008 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003009 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003010 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003011
Guido van Rossum053b8df1998-11-25 16:18:00 +00003012 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003013 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003014 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003015 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003016 Py_DECREF(class_name);
3017 }
3018 }
3019 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003020
Guido van Rossum053b8df1998-11-25 16:18:00 +00003021 if (! class) return -1;
3022 PDATA_PUSH(self->stack, class, -1);
3023 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003024}
3025
3026
3027static int
3028load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003029 PyObject *pid = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003030 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003031 char *s;
3032
3033 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003034 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003035 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003036
Guido van Rossum053b8df1998-11-25 16:18:00 +00003037 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003038
Guido van Rossum053b8df1998-11-25 16:18:00 +00003039 if (PyList_Check(self->pers_func)) {
3040 if (PyList_Append(self->pers_func, pid) < 0) {
3041 Py_DECREF(pid);
3042 return -1;
3043 }
3044 }
3045 else {
3046 ARG_TUP(self, pid);
3047 if (self->arg) {
3048 pid = PyObject_CallObject(self->pers_func, self->arg);
3049 FREE_ARG_TUP(self);
3050 }
3051 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003052
Guido van Rossum053b8df1998-11-25 16:18:00 +00003053 if (! pid) return -1;
3054
3055 PDATA_PUSH(self->stack, pid, -1);
3056 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003057 }
3058 else {
3059 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003060 "A load persistent id instruction was encountered,\n"
3061 "but no persistent_load function was specified.");
3062 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003063 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003064}
3065
Guido van Rossum60456fd1997-04-09 17:36:32 +00003066static int
3067load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003068 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003069
3070 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003071 PDATA_POP(self->stack, pid);
3072 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003073
Guido van Rossum053b8df1998-11-25 16:18:00 +00003074 if (PyList_Check(self->pers_func)) {
3075 if (PyList_Append(self->pers_func, pid) < 0) {
3076 Py_DECREF(pid);
3077 return -1;
3078 }
3079 }
3080 else {
3081 ARG_TUP(self, pid);
3082 if (self->arg) {
3083 pid = PyObject_CallObject(self->pers_func, self->arg);
3084 FREE_ARG_TUP(self);
3085 }
3086 if (! pid) return -1;
3087 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003088
Guido van Rossum053b8df1998-11-25 16:18:00 +00003089 PDATA_PUSH(self->stack, pid, -1);
3090 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003091 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003092 else {
3093 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003094 "A load persistent id instruction was encountered,\n"
3095 "but no persistent_load function was specified.");
3096 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003097 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003098}
3099
3100
3101static int
3102load_pop(Unpicklerobject *self) {
3103 int len;
3104
Guido van Rossum053b8df1998-11-25 16:18:00 +00003105 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003106
Guido van Rossumea2b7152000-05-09 18:14:50 +00003107 /* Note that we split the (pickle.py) stack into two stacks,
3108 an object stack and a mark stack. We have to be clever and
3109 pop the right one. We do this by looking at the top of the
3110 mark stack.
3111 */
3112
Guido van Rossum60456fd1997-04-09 17:36:32 +00003113 if ((self->num_marks > 0) &&
3114 (self->marks[self->num_marks - 1] == len))
3115 self->num_marks--;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003116 else {
3117 len--;
3118 Py_DECREF(self->stack->data[len]);
3119 self->stack->length=len;
3120 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003121
3122 return 0;
3123}
3124
3125
3126static int
3127load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003128 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003129
3130 if ((i = marker(self)) < 0)
3131 return -1;
3132
Guido van Rossum053b8df1998-11-25 16:18:00 +00003133 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003134
3135 return 0;
3136}
3137
3138
3139static int
3140load_dup(Unpicklerobject *self) {
3141 PyObject *last;
3142 int len;
3143
Guido van Rossum053b8df1998-11-25 16:18:00 +00003144 if ((len = self->stack->length) <= 0) return stackUnderflow();
3145 last=self->stack->data[len-1];
3146 Py_INCREF(last);
3147 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003148 return 0;
3149}
3150
3151
3152static int
3153load_get(Unpicklerobject *self) {
3154 PyObject *py_str = 0, *value = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003155 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003156 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003157 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003158
Guido van Rossum053b8df1998-11-25 16:18:00 +00003159 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003160 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003161
Guido van Rossum053b8df1998-11-25 16:18:00 +00003162 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3163
3164 value = PyDict_GetItem(self->memo, py_str);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003165 if (! value) {
3166 PyErr_SetObject(BadPickleGet, py_str);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003167 rc = -1;
3168 } else {
3169 PDATA_APPEND(self->stack, value, -1);
3170 rc = 0;
3171 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003172
Guido van Rossum2f80d961999-07-13 15:18:58 +00003173 Py_DECREF(py_str);
3174 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003175}
3176
3177
3178static int
3179load_binget(Unpicklerobject *self) {
3180 PyObject *py_key = 0, *value = 0;
3181 unsigned char key;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003182 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003183 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003184
Guido van Rossum053b8df1998-11-25 16:18:00 +00003185 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003186
3187 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003188 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3189
3190 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003191 if (! value) {
3192 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003193 rc = -1;
3194 } else {
3195 PDATA_APPEND(self->stack, value, -1);
3196 rc = 0;
3197 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003198
Guido van Rossum2f80d961999-07-13 15:18:58 +00003199 Py_DECREF(py_key);
3200 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003201}
3202
3203
3204static int
3205load_long_binget(Unpicklerobject *self) {
3206 PyObject *py_key = 0, *value = 0;
3207 unsigned char c, *s;
3208 long key;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003209 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003210
Guido van Rossum053b8df1998-11-25 16:18:00 +00003211 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003212
3213 c = (unsigned char)s[0];
3214 key = (long)c;
3215 c = (unsigned char)s[1];
3216 key |= (long)c << 8;
3217 c = (unsigned char)s[2];
3218 key |= (long)c << 16;
3219 c = (unsigned char)s[3];
3220 key |= (long)c << 24;
3221
Guido van Rossum053b8df1998-11-25 16:18:00 +00003222 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3223
3224 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003225 if (! value) {
3226 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003227 rc = -1;
3228 } else {
3229 PDATA_APPEND(self->stack, value, -1);
3230 rc = 0;
3231 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003232
Guido van Rossum2f80d961999-07-13 15:18:58 +00003233 Py_DECREF(py_key);
3234 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003235}
3236
3237
3238static int
3239load_put(Unpicklerobject *self) {
3240 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003241 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003242 char *s;
3243
Guido van Rossum053b8df1998-11-25 16:18:00 +00003244 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003245 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003246 UNLESS (len=self->stack->length) return stackUnderflow();
3247 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3248 value=self->stack->data[len-1];
3249 l=PyDict_SetItem(self->memo, py_str, value);
3250 Py_DECREF(py_str);
3251 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003252}
3253
3254
3255static int
3256load_binput(Unpicklerobject *self) {
3257 PyObject *py_key = 0, *value = 0;
3258 unsigned char key, *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003259 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003260
Guido van Rossum053b8df1998-11-25 16:18:00 +00003261 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3262 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003263
3264 key = (unsigned char)s[0];
3265
Guido van Rossum053b8df1998-11-25 16:18:00 +00003266 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3267 value=self->stack->data[len-1];
3268 len=PyDict_SetItem(self->memo, py_key, value);
3269 Py_DECREF(py_key);
3270 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003271}
3272
3273
3274static int
3275load_long_binput(Unpicklerobject *self) {
3276 PyObject *py_key = 0, *value = 0;
3277 long key;
3278 unsigned char c, *s;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003279 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003280
Guido van Rossum053b8df1998-11-25 16:18:00 +00003281 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3282 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003283
3284 c = (unsigned char)s[0];
3285 key = (long)c;
3286 c = (unsigned char)s[1];
3287 key |= (long)c << 8;
3288 c = (unsigned char)s[2];
3289 key |= (long)c << 16;
3290 c = (unsigned char)s[3];
3291 key |= (long)c << 24;
3292
Guido van Rossum053b8df1998-11-25 16:18:00 +00003293 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3294 value=self->stack->data[len-1];
3295 len=PyDict_SetItem(self->memo, py_key, value);
3296 Py_DECREF(py_key);
3297 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003298}
3299
3300
3301static int
3302do_append(Unpicklerobject *self, int x) {
3303 PyObject *value = 0, *list = 0, *append_method = 0;
3304 int len, i;
3305
Guido van Rossum053b8df1998-11-25 16:18:00 +00003306 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3307 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003308
Guido van Rossum053b8df1998-11-25 16:18:00 +00003309 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003310
3311 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003312 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003313 int list_len;
3314
Guido van Rossum053b8df1998-11-25 16:18:00 +00003315 slice=Pdata_popList(self->stack, x);
3316 list_len = PyList_GET_SIZE(list);
3317 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003318 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003319 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003320 }
3321 else {
3322
Guido van Rossum053b8df1998-11-25 16:18:00 +00003323 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003324 return -1;
3325
3326 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003327 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003328
Guido van Rossum053b8df1998-11-25 16:18:00 +00003329 value=self->stack->data[i];
3330 junk=0;
3331 ARG_TUP(self, value);
3332 if (self->arg) {
3333 junk = PyObject_CallObject(append_method, self->arg);
3334 FREE_ARG_TUP(self);
3335 }
3336 if (! junk) {
3337 Pdata_clear(self->stack, i+1);
3338 self->stack->length=x;
3339 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003340 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003341 }
3342 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003343 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003344 self->stack->length=x;
3345 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003346 }
3347
Guido van Rossum60456fd1997-04-09 17:36:32 +00003348 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003349}
3350
3351
3352static int
3353load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003354 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003355}
3356
3357
3358static int
3359load_appends(Unpicklerobject *self) {
3360 return do_append(self, marker(self));
3361}
3362
3363
3364static int
3365do_setitems(Unpicklerobject *self, int x) {
3366 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003367 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003368
Guido van Rossum053b8df1998-11-25 16:18:00 +00003369 UNLESS ((len=self->stack->length) >= x
3370 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003371
Guido van Rossum053b8df1998-11-25 16:18:00 +00003372 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003373
Guido van Rossum053b8df1998-11-25 16:18:00 +00003374 for (i = x+1; i < len; i += 2) {
3375 key =self->stack->data[i-1];
3376 value=self->stack->data[i ];
3377 if (PyObject_SetItem(dict, key, value) < 0) {
3378 r=-1;
3379 break;
3380 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003381 }
3382
Guido van Rossum053b8df1998-11-25 16:18:00 +00003383 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003384
Guido van Rossum053b8df1998-11-25 16:18:00 +00003385 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003386}
3387
3388
3389static int
3390load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003391 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003392}
3393
Guido van Rossum60456fd1997-04-09 17:36:32 +00003394static int
3395load_setitems(Unpicklerobject *self) {
3396 return do_setitems(self, marker(self));
3397}
3398
3399
3400static int
3401load_build(Unpicklerobject *self) {
3402 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3403 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003404 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003405
Guido van Rossum053b8df1998-11-25 16:18:00 +00003406 if (self->stack->length < 2) return stackUnderflow();
3407 PDATA_POP(self->stack, value);
3408 if (! value) return -1;
3409 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003410
Guido van Rossum053b8df1998-11-25 16:18:00 +00003411 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3412 ARG_TUP(self, value);
3413 if (self->arg) {
3414 junk = PyObject_CallObject(__setstate__, self->arg);
3415 FREE_ARG_TUP(self);
3416 }
3417 Py_DECREF(__setstate__);
3418 if (! junk) return -1;
3419 Py_DECREF(junk);
3420 return 0;
3421 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003422
Guido van Rossum053b8df1998-11-25 16:18:00 +00003423 PyErr_Clear();
3424 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003425 i = 0;
3426 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003427 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3428 r=-1;
3429 break;
3430 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003431 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003432 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003433 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003434 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003435
Guido van Rossum053b8df1998-11-25 16:18:00 +00003436 Py_XDECREF(value);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003437
Guido van Rossum053b8df1998-11-25 16:18:00 +00003438 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003439}
3440
3441
3442static int
3443load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003444 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003445
Guido van Rossumea2b7152000-05-09 18:14:50 +00003446 /* Note that we split the (pickle.py) stack into two stacks, an
3447 object stack and a mark stack. Here we push a mark onto the
3448 mark stack.
3449 */
3450
Guido van Rossum053b8df1998-11-25 16:18:00 +00003451 if ((self->num_marks + 1) >= self->marks_size) {
3452 s=self->marks_size+20;
3453 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003454 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003455 self->marks=(int *)malloc(s * sizeof(int));
3456 else
3457 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003458 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003459 PyErr_NoMemory();
3460 return -1;
3461 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003462 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003463 }
3464
Guido van Rossum053b8df1998-11-25 16:18:00 +00003465 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003466
3467 return 0;
3468}
3469
3470static int
3471load_reduce(Unpicklerobject *self) {
3472 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003473
Guido van Rossum053b8df1998-11-25 16:18:00 +00003474 PDATA_POP(self->stack, arg_tup);
3475 if (! arg_tup) return -1;
3476 PDATA_POP(self->stack, callable);
3477 if (callable) {
3478 ob = Instance_New(callable, arg_tup);
3479 Py_DECREF(callable);
3480 }
3481 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003482
Guido van Rossum053b8df1998-11-25 16:18:00 +00003483 if (! ob) return -1;
3484
3485 PDATA_PUSH(self->stack, ob, -1);
3486 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003487}
3488
3489static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003490load(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003491 PyObject *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003492 char *s;
3493
Guido van Rossum60456fd1997-04-09 17:36:32 +00003494 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003495 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003496
3497 while (1) {
3498 if ((*self->read_func)(self, &s, 1) < 0)
3499 break;
3500
3501 switch (s[0]) {
3502 case NONE:
3503 if (load_none(self) < 0)
3504 break;
3505 continue;
3506
3507 case BININT:
3508 if (load_binint(self) < 0)
3509 break;
3510 continue;
3511
3512 case BININT1:
3513 if (load_binint1(self) < 0)
3514 break;
3515 continue;
3516
3517 case BININT2:
3518 if (load_binint2(self) < 0)
3519 break;
3520 continue;
3521
3522 case INT:
3523 if (load_int(self) < 0)
3524 break;
3525 continue;
3526
3527 case LONG:
3528 if (load_long(self) < 0)
3529 break;
3530 continue;
3531
3532 case FLOAT:
3533 if (load_float(self) < 0)
3534 break;
3535 continue;
3536
Guido van Rossum60456fd1997-04-09 17:36:32 +00003537 case BINFLOAT:
3538 if (load_binfloat(self) < 0)
3539 break;
3540 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003541
3542 case BINSTRING:
3543 if (load_binstring(self) < 0)
3544 break;
3545 continue;
3546
3547 case SHORT_BINSTRING:
3548 if (load_short_binstring(self) < 0)
3549 break;
3550 continue;
3551
3552 case STRING:
3553 if (load_string(self) < 0)
3554 break;
3555 continue;
3556
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003557 case UNICODE:
3558 if (load_unicode(self) < 0)
3559 break;
3560 continue;
3561
3562 case BINUNICODE:
3563 if (load_binunicode(self) < 0)
3564 break;
3565 continue;
3566
Guido van Rossum60456fd1997-04-09 17:36:32 +00003567 case EMPTY_TUPLE:
3568 if (load_empty_tuple(self) < 0)
3569 break;
3570 continue;
3571
3572 case TUPLE:
3573 if (load_tuple(self) < 0)
3574 break;
3575 continue;
3576
3577 case EMPTY_LIST:
3578 if (load_empty_list(self) < 0)
3579 break;
3580 continue;
3581
3582 case LIST:
3583 if (load_list(self) < 0)
3584 break;
3585 continue;
3586
3587 case EMPTY_DICT:
3588 if (load_empty_dict(self) < 0)
3589 break;
3590 continue;
3591
3592 case DICT:
3593 if (load_dict(self) < 0)
3594 break;
3595 continue;
3596
3597 case OBJ:
3598 if (load_obj(self) < 0)
3599 break;
3600 continue;
3601
3602 case INST:
3603 if (load_inst(self) < 0)
3604 break;
3605 continue;
3606
3607 case GLOBAL:
3608 if (load_global(self) < 0)
3609 break;
3610 continue;
3611
3612 case APPEND:
3613 if (load_append(self) < 0)
3614 break;
3615 continue;
3616
3617 case APPENDS:
3618 if (load_appends(self) < 0)
3619 break;
3620 continue;
3621
3622 case BUILD:
3623 if (load_build(self) < 0)
3624 break;
3625 continue;
3626
3627 case DUP:
3628 if (load_dup(self) < 0)
3629 break;
3630 continue;
3631
3632 case BINGET:
3633 if (load_binget(self) < 0)
3634 break;
3635 continue;
3636
3637 case LONG_BINGET:
3638 if (load_long_binget(self) < 0)
3639 break;
3640 continue;
3641
3642 case GET:
3643 if (load_get(self) < 0)
3644 break;
3645 continue;
3646
3647 case MARK:
3648 if (load_mark(self) < 0)
3649 break;
3650 continue;
3651
3652 case BINPUT:
3653 if (load_binput(self) < 0)
3654 break;
3655 continue;
3656
3657 case LONG_BINPUT:
3658 if (load_long_binput(self) < 0)
3659 break;
3660 continue;
3661
3662 case PUT:
3663 if (load_put(self) < 0)
3664 break;
3665 continue;
3666
3667 case POP:
3668 if (load_pop(self) < 0)
3669 break;
3670 continue;
3671
3672 case POP_MARK:
3673 if (load_pop_mark(self) < 0)
3674 break;
3675 continue;
3676
3677 case SETITEM:
3678 if (load_setitem(self) < 0)
3679 break;
3680 continue;
3681
3682 case SETITEMS:
3683 if (load_setitems(self) < 0)
3684 break;
3685 continue;
3686
3687 case STOP:
3688 break;
3689
3690 case PERSID:
3691 if (load_persid(self) < 0)
3692 break;
3693 continue;
3694
3695 case BINPERSID:
3696 if (load_binpersid(self) < 0)
3697 break;
3698 continue;
3699
3700 case REDUCE:
3701 if (load_reduce(self) < 0)
3702 break;
3703 continue;
3704
3705 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003706 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003707 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003708 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003709 }
3710
3711 break;
3712 }
3713
Guido van Rossum053b8df1998-11-25 16:18:00 +00003714 if ((err = PyErr_Occurred())) {
3715 if (err == PyExc_EOFError) {
3716 PyErr_SetNone(PyExc_EOFError);
3717 }
3718 return NULL;
3719 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003720
Guido van Rossum053b8df1998-11-25 16:18:00 +00003721 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003722 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003723}
3724
3725
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003726/* No-load functions to support noload, which is used to
3727 find persistent references. */
3728
3729static int
3730noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003731 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003732
3733 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003734 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003735}
3736
3737
3738static int
3739noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003740 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003741 char *s;
3742
3743 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003744 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003745 if ((*self->readline_func)(self, &s) < 0) return -1;
3746 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003747 PDATA_APPEND(self->stack, Py_None,-1);
3748 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003749}
3750
3751static int
3752noload_global(Unpicklerobject *self) {
3753 char *s;
3754
3755 if ((*self->readline_func)(self, &s) < 0) return -1;
3756 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003757 PDATA_APPEND(self->stack, Py_None,-1);
3758 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003759}
3760
3761static int
3762noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003763
Guido van Rossum053b8df1998-11-25 16:18:00 +00003764 if (self->stack->length < 2) return stackUnderflow();
3765 Pdata_clear(self->stack, self->stack->length-2);
3766 PDATA_APPEND(self->stack, Py_None,-1);
3767 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003768}
3769
3770static int
3771noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003772
Guido van Rossum053b8df1998-11-25 16:18:00 +00003773 if (self->stack->length < 1) return stackUnderflow();
3774 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003775 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003776}
3777
3778
3779static PyObject *
3780noload(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003781 PyObject *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003782 char *s;
3783
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003784 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003785 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003786
3787 while (1) {
3788 if ((*self->read_func)(self, &s, 1) < 0)
3789 break;
3790
3791 switch (s[0]) {
3792 case NONE:
3793 if (load_none(self) < 0)
3794 break;
3795 continue;
3796
3797 case BININT:
3798 if (load_binint(self) < 0)
3799 break;
3800 continue;
3801
3802 case BININT1:
3803 if (load_binint1(self) < 0)
3804 break;
3805 continue;
3806
3807 case BININT2:
3808 if (load_binint2(self) < 0)
3809 break;
3810 continue;
3811
3812 case INT:
3813 if (load_int(self) < 0)
3814 break;
3815 continue;
3816
3817 case LONG:
3818 if (load_long(self) < 0)
3819 break;
3820 continue;
3821
3822 case FLOAT:
3823 if (load_float(self) < 0)
3824 break;
3825 continue;
3826
3827 case BINFLOAT:
3828 if (load_binfloat(self) < 0)
3829 break;
3830 continue;
3831
3832 case BINSTRING:
3833 if (load_binstring(self) < 0)
3834 break;
3835 continue;
3836
3837 case SHORT_BINSTRING:
3838 if (load_short_binstring(self) < 0)
3839 break;
3840 continue;
3841
3842 case STRING:
3843 if (load_string(self) < 0)
3844 break;
3845 continue;
3846
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003847 case UNICODE:
3848 if (load_unicode(self) < 0)
3849 break;
3850 continue;
3851
3852 case BINUNICODE:
3853 if (load_binunicode(self) < 0)
3854 break;
3855 continue;
3856
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003857 case EMPTY_TUPLE:
3858 if (load_empty_tuple(self) < 0)
3859 break;
3860 continue;
3861
3862 case TUPLE:
3863 if (load_tuple(self) < 0)
3864 break;
3865 continue;
3866
3867 case EMPTY_LIST:
3868 if (load_empty_list(self) < 0)
3869 break;
3870 continue;
3871
3872 case LIST:
3873 if (load_list(self) < 0)
3874 break;
3875 continue;
3876
3877 case EMPTY_DICT:
3878 if (load_empty_dict(self) < 0)
3879 break;
3880 continue;
3881
3882 case DICT:
3883 if (load_dict(self) < 0)
3884 break;
3885 continue;
3886
3887 case OBJ:
3888 if (noload_obj(self) < 0)
3889 break;
3890 continue;
3891
3892 case INST:
3893 if (noload_inst(self) < 0)
3894 break;
3895 continue;
3896
3897 case GLOBAL:
3898 if (noload_global(self) < 0)
3899 break;
3900 continue;
3901
3902 case APPEND:
3903 if (load_append(self) < 0)
3904 break;
3905 continue;
3906
3907 case APPENDS:
3908 if (load_appends(self) < 0)
3909 break;
3910 continue;
3911
3912 case BUILD:
3913 if (noload_build(self) < 0)
3914 break;
3915 continue;
3916
3917 case DUP:
3918 if (load_dup(self) < 0)
3919 break;
3920 continue;
3921
3922 case BINGET:
3923 if (load_binget(self) < 0)
3924 break;
3925 continue;
3926
3927 case LONG_BINGET:
3928 if (load_long_binget(self) < 0)
3929 break;
3930 continue;
3931
3932 case GET:
3933 if (load_get(self) < 0)
3934 break;
3935 continue;
3936
3937 case MARK:
3938 if (load_mark(self) < 0)
3939 break;
3940 continue;
3941
3942 case BINPUT:
3943 if (load_binput(self) < 0)
3944 break;
3945 continue;
3946
3947 case LONG_BINPUT:
3948 if (load_long_binput(self) < 0)
3949 break;
3950 continue;
3951
3952 case PUT:
3953 if (load_put(self) < 0)
3954 break;
3955 continue;
3956
3957 case POP:
3958 if (load_pop(self) < 0)
3959 break;
3960 continue;
3961
3962 case POP_MARK:
3963 if (load_pop_mark(self) < 0)
3964 break;
3965 continue;
3966
3967 case SETITEM:
3968 if (load_setitem(self) < 0)
3969 break;
3970 continue;
3971
3972 case SETITEMS:
3973 if (load_setitems(self) < 0)
3974 break;
3975 continue;
3976
3977 case STOP:
3978 break;
3979
3980 case PERSID:
3981 if (load_persid(self) < 0)
3982 break;
3983 continue;
3984
3985 case BINPERSID:
3986 if (load_binpersid(self) < 0)
3987 break;
3988 continue;
3989
3990 case REDUCE:
3991 if (noload_reduce(self) < 0)
3992 break;
3993 continue;
3994
3995 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003996 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003997 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003998 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003999 }
4000
4001 break;
4002 }
4003
Guido van Rossum053b8df1998-11-25 16:18:00 +00004004 if ((err = PyErr_Occurred())) {
4005 if (err == PyExc_EOFError) {
4006 PyErr_SetNone(PyExc_EOFError);
4007 }
4008 return NULL;
4009 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004010
Guido van Rossum053b8df1998-11-25 16:18:00 +00004011 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004012 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004013}
4014
4015
Guido van Rossum60456fd1997-04-09 17:36:32 +00004016static PyObject *
4017Unpickler_load(Unpicklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00004018 UNLESS (PyArg_ParseTuple(args, ":load"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004019 return NULL;
4020
4021 return load(self);
4022}
4023
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004024static PyObject *
4025Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00004026 UNLESS (PyArg_ParseTuple(args, ":noload"))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004027 return NULL;
4028
4029 return noload(self);
4030}
4031
Guido van Rossum60456fd1997-04-09 17:36:32 +00004032
4033static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004034 {"load", (PyCFunction)Unpickler_load, 1,
4035 "load() -- Load a pickle"
4036 },
4037 {"noload", (PyCFunction)Unpickler_noload, 1,
4038 "noload() -- not load a pickle, but go through most of the motions\n"
4039 "\n"
4040 "This function can be used to read past a pickle without instantiating\n"
4041 "any objects or importing any modules. It can also be used to find all\n"
4042 "persistent references without instantiating any objects or importing\n"
4043 "any modules.\n"
4044 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004045 {NULL, NULL} /* sentinel */
4046};
4047
4048
4049static Unpicklerobject *
4050newUnpicklerobject(PyObject *f) {
4051 Unpicklerobject *self;
4052
Guido van Rossumb18618d2000-05-03 23:44:39 +00004053 UNLESS (self = PyObject_New(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004054 return NULL;
4055
4056 self->file = NULL;
4057 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004058 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004059 self->pers_func = NULL;
4060 self->last_string = NULL;
4061 self->marks = NULL;
4062 self->num_marks = 0;
4063 self->marks_size = 0;
4064 self->buf_size = 0;
4065 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00004066 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00004067 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004068 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004069
Guido van Rossum83addc72000-04-21 20:49:36 +00004070 UNLESS (self->memo = PyDict_New())
4071 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004072
4073 Py_INCREF(f);
4074 self->file = f;
4075
4076 /* Set read, readline based on type of f */
4077 if (PyFile_Check(f)) {
4078 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004079 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00004080 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
4081 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004082 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004083 self->read_func = read_file;
4084 self->readline_func = readline_file;
4085 }
4086 else if (PycStringIO_InputCheck(f)) {
4087 self->fp = NULL;
4088 self->read_func = read_cStringIO;
4089 self->readline_func = readline_cStringIO;
4090 }
4091 else {
4092
4093 self->fp = NULL;
4094 self->read_func = read_other;
4095 self->readline_func = readline_other;
4096
Guido van Rossum053b8df1998-11-25 16:18:00 +00004097 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004098 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004099 PyErr_Clear();
4100 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
4101 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00004102 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004103 }
4104 }
4105
Guido van Rossum053b8df1998-11-25 16:18:00 +00004106 if (PyEval_GetRestricted()) {
4107 /* Restricted execution, get private tables */
4108 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004109
Guido van Rossum053b8df1998-11-25 16:18:00 +00004110 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
4111 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
4112 Py_DECREF(m);
4113 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004114 }
4115 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004116 self->safe_constructors=safe_constructors;
4117 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004118 }
4119
Guido van Rossum60456fd1997-04-09 17:36:32 +00004120 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004121
4122err:
4123 Py_DECREF((PyObject *)self);
4124 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004125}
4126
4127
4128static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004129get_Unpickler(PyObject *self, PyObject *args) {
4130 PyObject *file;
4131
Guido van Rossum43713e52000-02-29 13:59:29 +00004132 UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004133 return NULL;
4134 return (PyObject *)newUnpicklerobject(file);
4135}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004136
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004137
Guido van Rossum60456fd1997-04-09 17:36:32 +00004138static void
4139Unpickler_dealloc(Unpicklerobject *self) {
4140 Py_XDECREF(self->readline);
4141 Py_XDECREF(self->read);
4142 Py_XDECREF(self->file);
4143 Py_XDECREF(self->memo);
4144 Py_XDECREF(self->stack);
4145 Py_XDECREF(self->pers_func);
4146 Py_XDECREF(self->arg);
4147 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004148 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004149
Guido van Rossum60456fd1997-04-09 17:36:32 +00004150 if (self->marks) {
4151 free(self->marks);
4152 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004153
Guido van Rossum60456fd1997-04-09 17:36:32 +00004154 if (self->buf_size) {
4155 free(self->buf);
4156 }
4157
Guido van Rossumb18618d2000-05-03 23:44:39 +00004158 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004159}
4160
4161
4162static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004163Unpickler_getattr(Unpicklerobject *self, char *name) {
4164 if (!strcmp(name, "persistent_load")) {
4165 if (!self->pers_func) {
4166 PyErr_SetString(PyExc_AttributeError, name);
4167 return NULL;
4168 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004169
Guido van Rossum60456fd1997-04-09 17:36:32 +00004170 Py_INCREF(self->pers_func);
4171 return self->pers_func;
4172 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004173
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004174 if (!strcmp(name, "find_global")) {
4175 if (!self->find_class) {
4176 PyErr_SetString(PyExc_AttributeError, name);
4177 return NULL;
4178 }
4179
4180 Py_INCREF(self->find_class);
4181 return self->find_class;
4182 }
4183
Guido van Rossum60456fd1997-04-09 17:36:32 +00004184 if (!strcmp(name, "memo")) {
4185 if (!self->memo) {
4186 PyErr_SetString(PyExc_AttributeError, name);
4187 return NULL;
4188 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004189
Guido van Rossum60456fd1997-04-09 17:36:32 +00004190 Py_INCREF(self->memo);
4191 return self->memo;
4192 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004193
Guido van Rossum60456fd1997-04-09 17:36:32 +00004194 if (!strcmp(name, "UnpicklingError")) {
4195 Py_INCREF(UnpicklingError);
4196 return UnpicklingError;
4197 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004198
Guido van Rossum60456fd1997-04-09 17:36:32 +00004199 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4200}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004201
Guido van Rossum60456fd1997-04-09 17:36:32 +00004202
4203static int
4204Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004205
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004206 if (!strcmp(name, "persistent_load")) {
4207 Py_XDECREF(self->pers_func);
4208 self->pers_func = value;
4209 Py_XINCREF(value);
4210 return 0;
4211 }
4212
4213 if (!strcmp(name, "find_global")) {
4214 Py_XDECREF(self->find_class);
4215 self->find_class = value;
4216 Py_XINCREF(value);
4217 return 0;
4218 }
4219
Guido van Rossum053b8df1998-11-25 16:18:00 +00004220 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004221 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004222 "attribute deletion is not supported");
4223 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004224 }
4225
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004226 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004227 if (! PyDict_Check(value)) {
4228 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4229 return -1;
4230 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004231 Py_XDECREF(self->memo);
4232 self->memo = value;
4233 Py_INCREF(value);
4234 return 0;
4235 }
4236
Guido van Rossum60456fd1997-04-09 17:36:32 +00004237 PyErr_SetString(PyExc_AttributeError, name);
4238 return -1;
4239}
4240
4241
4242static PyObject *
4243cpm_dump(PyObject *self, PyObject *args) {
4244 PyObject *ob, *file, *res = NULL;
4245 Picklerobject *pickler = 0;
4246 int bin = 0;
4247
Guido van Rossum053b8df1998-11-25 16:18:00 +00004248 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004249 goto finally;
4250
Guido van Rossum053b8df1998-11-25 16:18:00 +00004251 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004252 goto finally;
4253
4254 if (dump(pickler, ob) < 0)
4255 goto finally;
4256
4257 Py_INCREF(Py_None);
4258 res = Py_None;
4259
4260finally:
4261 Py_XDECREF(pickler);
4262
4263 return res;
4264}
4265
4266
4267static PyObject *
4268cpm_dumps(PyObject *self, PyObject *args) {
4269 PyObject *ob, *file = 0, *res = NULL;
4270 Picklerobject *pickler = 0;
4271 int bin = 0;
4272
Guido van Rossum43713e52000-02-29 13:59:29 +00004273 UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004274 goto finally;
4275
Guido van Rossum053b8df1998-11-25 16:18:00 +00004276 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004277 goto finally;
4278
Guido van Rossum053b8df1998-11-25 16:18:00 +00004279 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004280 goto finally;
4281
4282 if (dump(pickler, ob) < 0)
4283 goto finally;
4284
4285 res = PycStringIO->cgetvalue(file);
4286
4287finally:
4288 Py_XDECREF(pickler);
4289 Py_XDECREF(file);
4290
4291 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004292}
4293
4294
4295static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004296cpm_load(PyObject *self, PyObject *args) {
4297 Unpicklerobject *unpickler = 0;
4298 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004299
Guido van Rossum43713e52000-02-29 13:59:29 +00004300 UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004301 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004302
Guido van Rossum053b8df1998-11-25 16:18:00 +00004303 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004304 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004305
Guido van Rossum60456fd1997-04-09 17:36:32 +00004306 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004307
Guido van Rossum60456fd1997-04-09 17:36:32 +00004308finally:
4309 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004310
Guido van Rossum60456fd1997-04-09 17:36:32 +00004311 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004312}
4313
4314
4315static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004316cpm_loads(PyObject *self, PyObject *args) {
4317 PyObject *ob, *file = 0, *res = NULL;
4318 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004319
Guido van Rossum43713e52000-02-29 13:59:29 +00004320 UNLESS (PyArg_ParseTuple(args, "S:loads", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004321 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004322
Guido van Rossum053b8df1998-11-25 16:18:00 +00004323 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004324 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004325
Guido van Rossum053b8df1998-11-25 16:18:00 +00004326 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004327 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004328
Guido van Rossum60456fd1997-04-09 17:36:32 +00004329 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004330
Guido van Rossum60456fd1997-04-09 17:36:32 +00004331finally:
4332 Py_XDECREF(file);
4333 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004334
Guido van Rossum60456fd1997-04-09 17:36:32 +00004335 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004336}
4337
4338
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004339static char Unpicklertype__doc__[] =
4340"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004341
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004342static PyTypeObject Unpicklertype = {
4343 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004344 0, /*ob_size*/
4345 "Unpickler", /*tp_name*/
4346 sizeof(Unpicklerobject), /*tp_basicsize*/
4347 0, /*tp_itemsize*/
4348 /* methods */
4349 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4350 (printfunc)0, /*tp_print*/
4351 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4352 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4353 (cmpfunc)0, /*tp_compare*/
4354 (reprfunc)0, /*tp_repr*/
4355 0, /*tp_as_number*/
4356 0, /*tp_as_sequence*/
4357 0, /*tp_as_mapping*/
4358 (hashfunc)0, /*tp_hash*/
4359 (ternaryfunc)0, /*tp_call*/
4360 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004361
Guido van Rossum60456fd1997-04-09 17:36:32 +00004362 /* Space for future expansion */
4363 0L,0L,0L,0L,
4364 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004365};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004366
Guido van Rossum60456fd1997-04-09 17:36:32 +00004367static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004368 {"dump", (PyCFunction)cpm_dump, 1,
4369 "dump(object, file, [binary]) --"
4370 "Write an object in pickle format to the given file\n"
4371 "\n"
4372 "If the optional argument, binary, is provided and is true, then the\n"
4373 "pickle will be written in binary format, which is more space and\n"
4374 "computationally efficient. \n"
4375 },
4376 {"dumps", (PyCFunction)cpm_dumps, 1,
4377 "dumps(object, [binary]) --"
4378 "Return a string containing an object in pickle format\n"
4379 "\n"
4380 "If the optional argument, binary, is provided and is true, then the\n"
4381 "pickle will be written in binary format, which is more space and\n"
4382 "computationally efficient. \n"
4383 },
4384 {"load", (PyCFunction)cpm_load, 1,
4385 "load(file) -- Load a pickle from the given file"},
4386 {"loads", (PyCFunction)cpm_loads, 1,
4387 "loads(string) -- Load a pickle from the given string"},
4388 {"Pickler", (PyCFunction)get_Pickler, 1,
4389 "Pickler(file, [binary]) -- Create a pickler\n"
4390 "\n"
4391 "If the optional argument, binary, is provided and is true, then\n"
4392 "pickles will be written in binary format, which is more space and\n"
4393 "computationally efficient. \n"
4394 },
4395 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4396 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004397 { NULL, NULL }
4398};
4399
Guido van Rossum60456fd1997-04-09 17:36:32 +00004400static int
4401init_stuff(PyObject *module, PyObject *module_dict) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004402 PyObject *string, *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004403
4404#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4405
4406 INIT_STR(__class__);
4407 INIT_STR(__getinitargs__);
4408 INIT_STR(__dict__);
4409 INIT_STR(__getstate__);
4410 INIT_STR(__setstate__);
4411 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004412 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004413 INIT_STR(__reduce__);
4414 INIT_STR(write);
4415 INIT_STR(__safe_for_unpickling__);
4416 INIT_STR(append);
4417 INIT_STR(read);
4418 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004419 INIT_STR(copy_reg);
4420 INIT_STR(dispatch_table);
4421 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004422 INIT_STR(__basicnew__);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004423 UNLESS (empty_str=PyString_FromString("")) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004424
Guido van Rossum053b8df1998-11-25 16:18:00 +00004425 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004426 return -1;
4427
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004428 /* These next few are special because we want to use different
4429 ones in restricted mode. */
4430
Guido van Rossum053b8df1998-11-25 16:18:00 +00004431 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004432 return -1;
4433
Guido van Rossum053b8df1998-11-25 16:18:00 +00004434 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4435 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004436 return -1;
4437
4438 Py_DECREF(copy_reg);
4439
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004440 /* Down to here ********************************** */
4441
Guido van Rossum053b8df1998-11-25 16:18:00 +00004442 UNLESS (string = PyImport_ImportModule("string"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004443 return -1;
4444
Guido van Rossum053b8df1998-11-25 16:18:00 +00004445 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004446 return -1;
4447
4448 Py_DECREF(string);
4449
Guido van Rossum053b8df1998-11-25 16:18:00 +00004450 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004451 return -1;
4452
Guido van Rossumc03158b1999-06-09 15:23:31 +00004453 /* Ugh */
4454 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4455 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4456 return -1;
4457
4458 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004459 UNLESS (r=PyRun_String(
4460 "def __init__(self, *args): self.args=args\n\n"
4461 "def __str__(self):\n"
4462 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4463 Py_file_input,
4464 module_dict, t) ) return -1;
4465 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004466
4467 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4468 return -1;
4469
4470 Py_DECREF(t);
4471
4472
4473 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
4474 PickleError, NULL))
4475 return -1;
4476
4477 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004478 UNLESS (r=PyRun_String(
4479 "def __init__(self, *args): self.args=args\n\n"
4480 "def __str__(self):\n"
4481 " a=self.args\n"
4482 " a=a and type(a[0]) or '(what)'\n"
4483 " return 'Cannot pickle %s objects' % a\n"
4484 , Py_file_input,
4485 module_dict, t) ) return -1;
4486 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004487
4488 UNLESS (UnpickleableError = PyErr_NewException(
4489 "cPickle.UnpickleableError", PicklingError, t))
4490 return -1;
4491
4492 Py_DECREF(t);
4493
4494 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
4495 PickleError, NULL))
4496 return -1;
4497
4498 if (PyDict_SetItemString(module_dict, "PickleError",
4499 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004500 return -1;
4501
4502 if (PyDict_SetItemString(module_dict, "PicklingError",
4503 PicklingError) < 0)
4504 return -1;
4505
Guido van Rossum60456fd1997-04-09 17:36:32 +00004506 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4507 UnpicklingError) < 0)
4508 return -1;
4509
Guido van Rossumc03158b1999-06-09 15:23:31 +00004510 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4511 UnpickleableError) < 0)
4512 return -1;
4513
Guido van Rossum053b8df1998-11-25 16:18:00 +00004514 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4515 return -1;
4516
4517 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4518 BadPickleGet) < 0)
4519 return -1;
4520
Guido van Rossum60456fd1997-04-09 17:36:32 +00004521 PycString_IMPORT;
4522
4523 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004524}
4525
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004526#ifndef DL_EXPORT /* declarations for DLL import/export */
4527#define DL_EXPORT(RTYPE) RTYPE
4528#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004529DL_EXPORT(void)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004530initcPickle() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004531 PyObject *m, *d, *v;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004532 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004533 PyObject *format_version;
4534 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004535
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004536 Picklertype.ob_type = &PyType_Type;
4537 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004538 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004539
Guido van Rossum60456fd1997-04-09 17:36:32 +00004540 /* Create the module and add the functions */
4541 m = Py_InitModule4("cPickle", cPickle_methods,
4542 cPickle_module_documentation,
4543 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004544
Guido van Rossum60456fd1997-04-09 17:36:32 +00004545 /* Add some symbolic constants to the module */
4546 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004547 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004548 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004549
Guido van Rossum60456fd1997-04-09 17:36:32 +00004550 format_version = PyString_FromString("1.3");
4551 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004552
Guido van Rossum60456fd1997-04-09 17:36:32 +00004553 PyDict_SetItemString(d, "format_version", format_version);
4554 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004555 Py_XDECREF(format_version);
4556 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004557
Guido van Rossum60456fd1997-04-09 17:36:32 +00004558 init_stuff(m, d);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004559}