blob: d6a03d3b51d7fc5187de9850100088f0ad5a9721 [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 Rossum053b8df1998-11-25 16:18:00 +0000137#ifndef PyList_SET_ITEM
138#define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
139#endif
140#ifndef PyList_GET_SIZE
141#define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
142#endif
143#ifndef PyTuple_SET_ITEM
144#define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
145#endif
146#ifndef PyTuple_GET_SIZE
147#define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
148#endif
149#ifndef PyString_GET_SIZE
150#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
151#endif
152
153/*************************************************************************
154 Internal Data type for pickle data. */
155
156typedef struct {
157 PyObject_HEAD
158 int length, size;
159 PyObject **data;
160} Pdata;
161
162static void
163Pdata_dealloc(Pdata *self) {
164 int i;
165 PyObject **p;
166
167 for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
168
169 if (self->data) free(self->data);
170
Guido van Rossumb18618d2000-05-03 23:44:39 +0000171 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000172}
173
174static PyTypeObject PdataType = {
175 PyObject_HEAD_INIT(NULL) 0, "Pdata", sizeof(Pdata), 0,
176 (destructor)Pdata_dealloc,
177 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
178};
179
180#define Pdata_Check(O) ((O)->ob_type == &PdataType)
181
182static PyObject *
183Pdata_New() {
184 Pdata *self;
185
Guido van Rossumb18618d2000-05-03 23:44:39 +0000186 UNLESS (self = PyObject_New(Pdata, &PdataType)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000187 self->size=8;
188 self->length=0;
189 self->data=malloc(self->size * sizeof(PyObject*));
190 if (self->data) return (PyObject*)self;
191 Py_DECREF(self);
192 return PyErr_NoMemory();
193}
194
195static int
196stackUnderflow() {
197 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
198 return -1;
199}
200
201static int
202Pdata_clear(Pdata *self, int clearto) {
203 int i;
204 PyObject **p;
205
206 if (clearto < 0) return stackUnderflow();
207 if (clearto >= self->length) return 0;
208
209 for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
210 Py_DECREF(*p);
211 self->length=clearto;
212
213 return 0;
214}
215
216
217static int
218Pdata_grow(Pdata *self) {
219 if (! self->size) {
220 PyErr_NoMemory();
221 return -1;
222 }
223 self->size *= 2;
224 self->data = realloc(self->data, self->size*sizeof(PyObject*));
225 if (! self->data) {
226 self->size = 0;
227 PyErr_NoMemory();
228 return -1;
229 }
230 return 0;
231}
232
233#define PDATA_POP(D,V) { \
234 if ((D)->length) V=D->data[--((D)->length)]; \
235 else { \
236 PyErr_SetString(UnpicklingError, "bad pickle data"); \
237 V=NULL; \
238 } \
239}
240
241
242static PyObject *
243Pdata_popTuple(Pdata *self, int start) {
244 PyObject *r;
245 int i, j, l;
246
247 l=self->length-start;
248 UNLESS (r=PyTuple_New(l)) return NULL;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000249 for (i=start, j=0 ; j < l; i++, j++)
250 PyTuple_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000251
252 self->length=start;
253 return r;
254}
255
256static PyObject *
257Pdata_popList(Pdata *self, int start) {
258 PyObject *r;
259 int i, j, l;
260
261 l=self->length-start;
262 UNLESS (r=PyList_New(l)) return NULL;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000263 for (i=start, j=0 ; j < l; i++, j++)
264 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000265
266 self->length=start;
267 return r;
268}
269
270#define PDATA_APPEND_(D,O,ER) { \
271 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
272}
273
274#define PDATA_APPEND(D,O,ER) { \
275 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
276 Pdata_grow((Pdata*)(D)) < 0) \
277 return ER; \
278 Py_INCREF(O); \
279 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
280}
281
282#define PDATA_PUSH(D,O,ER) { \
283 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
284 Pdata_grow((Pdata*)(D)) < 0) { \
285 Py_DECREF(O); \
286 return ER; \
287 } \
288 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
289}
290
291/*************************************************************************/
292
293#define ARG_TUP(self, o) { \
294 if (self->arg || (self->arg=PyTuple_New(1))) { \
295 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
296 PyTuple_SET_ITEM(self->arg,0,o); \
297 } \
298 else { \
299 Py_DECREF(o); \
300 } \
301}
302
303#define FREE_ARG_TUP(self) { \
304 if (self->arg->ob_refcnt > 1) { \
305 Py_DECREF(self->arg); \
306 self->arg=NULL; \
307 } \
308 }
309
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000310typedef struct Picklerobject {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000311 PyObject_HEAD
312 FILE *fp;
313 PyObject *write;
314 PyObject *file;
315 PyObject *memo;
316 PyObject *arg;
317 PyObject *pers_func;
318 PyObject *inst_pers_func;
319 int bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000320 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000321 int (*write_func)(struct Picklerobject *, char *, int);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000322 char *write_buf;
323 int buf_size;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000324 PyObject *dispatch_table;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000325} Picklerobject;
326
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000327staticforward PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000328
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000329typedef struct Unpicklerobject {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000330 PyObject_HEAD
331 FILE *fp;
332 PyObject *file;
333 PyObject *readline;
334 PyObject *read;
335 PyObject *memo;
336 PyObject *arg;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000337 Pdata *stack;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000338 PyObject *mark;
339 PyObject *pers_func;
340 PyObject *last_string;
341 int *marks;
342 int num_marks;
343 int marks_size;
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000344 int (*read_func)(struct Unpicklerobject *, char **, int);
345 int (*readline_func)(struct Unpicklerobject *, char **);
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000346 int buf_size;
347 char *buf;
348 PyObject *safe_constructors;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +0000349 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000350} Unpicklerobject;
351
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000352staticforward PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000353
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000354/* Forward decls that need the above structs */
355static int save(Picklerobject *, PyObject *, int);
356static int put2(Picklerobject *, PyObject *);
357
Guido van Rossum60456fd1997-04-09 17:36:32 +0000358int
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000359cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000360 PyObject *v;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000361
Guido van Rossum053b8df1998-11-25 16:18:00 +0000362 if ((v = PyObject_GetItem(o,key))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000363 Py_DECREF(v);
364 return 1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000365 }
366
Guido van Rossum60456fd1997-04-09 17:36:32 +0000367 PyErr_Clear();
368 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000369}
370
Guido van Rossumd385d591997-04-09 17:47:47 +0000371static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000372PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000373cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
374{
Guido van Rossum60456fd1997-04-09 17:36:32 +0000375 va_list va;
376 PyObject *args=0, *retval=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000377 va_start(va, format);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000378
Guido van Rossum053b8df1998-11-25 16:18:00 +0000379 if (format) args = Py_VaBuildValue(format, va);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000380 va_end(va);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000381 if (format && ! args) return NULL;
382 if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000383
Guido van Rossum053b8df1998-11-25 16:18:00 +0000384 if (retval) {
385 if (args) {
386 PyObject *v;
387 v=PyString_Format(retval, args);
388 Py_DECREF(retval);
389 Py_DECREF(args);
390 if (! v) return NULL;
391 retval=v;
392 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000393 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000394 else
Guido van Rossum053b8df1998-11-25 16:18:00 +0000395 if (args) retval=args;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000396 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000397 PyErr_SetObject(ErrType,Py_None);
398 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000399 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000400 PyErr_SetObject(ErrType,retval);
401 Py_DECREF(retval);
402 return NULL;
403}
404
405static int
406write_file(Picklerobject *self, char *s, int n) {
407 if (s == NULL) {
408 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000409 }
410
Guido van Rossum60456fd1997-04-09 17:36:32 +0000411 if ((int)fwrite(s, sizeof(char), n, self->fp) != n) {
412 PyErr_SetFromErrno(PyExc_IOError);
413 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000414 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000415
416 return n;
417}
418
Guido van Rossum60456fd1997-04-09 17:36:32 +0000419static int
420write_cStringIO(Picklerobject *self, char *s, int n) {
421 if (s == NULL) {
422 return 0;
423 }
424
425 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
426 return -1;
427 }
428
429 return n;
430}
431
Guido van Rossum60456fd1997-04-09 17:36:32 +0000432static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000433write_none(Picklerobject *self, char *s, int n) {
434 if (s == NULL) return 0;
435 return n;
436}
437
Guido van Rossum142eeb81997-08-13 03:14:41 +0000438static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000439write_other(Picklerobject *self, char *s, int n) {
440 PyObject *py_str = 0, *junk = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000441
442 if (s == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000443 UNLESS (self->buf_size) return 0;
444 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000445 PyString_FromStringAndSize(self->write_buf, self->buf_size))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000446 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000447 }
448 else {
449 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
450 if (write_other(self, NULL, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000451 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000452 }
453
454 if (n > WRITE_BUF_SIZE) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000455 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000456 PyString_FromStringAndSize(s, n))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000457 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000458 }
459 else {
460 memcpy(self->write_buf + self->buf_size, s, n);
461 self->buf_size += n;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000462 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000463 }
464 }
465
Guido van Rossum053b8df1998-11-25 16:18:00 +0000466 if (self->write) {
467 /* object with write method */
468 ARG_TUP(self, py_str);
469 if (self->arg) {
470 junk = PyObject_CallObject(self->write, self->arg);
471 FREE_ARG_TUP(self);
472 }
473 if (junk) Py_DECREF(junk);
474 else return -1;
475 }
476 else
477 PDATA_PUSH(self->file, py_str, -1);
478
479 self->buf_size = 0;
480 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000481}
482
483
484static int
485read_file(Unpicklerobject *self, char **s, int n) {
486
487 if (self->buf_size == 0) {
488 int size;
489
490 size = ((n < 32) ? 32 : n);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000491 UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000492 PyErr_NoMemory();
493 return -1;
494 }
495
496 self->buf_size = size;
497 }
498 else if (n > self->buf_size) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000499 UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000500 PyErr_NoMemory();
501 return -1;
502 }
503
504 self->buf_size = n;
505 }
506
507 if ((int)fread(self->buf, sizeof(char), n, self->fp) != n) {
508 if (feof(self->fp)) {
509 PyErr_SetNone(PyExc_EOFError);
510 return -1;
511 }
512
513 PyErr_SetFromErrno(PyExc_IOError);
514 return -1;
515 }
516
517 *s = self->buf;
518
519 return n;
520}
521
522
523static int
524readline_file(Unpicklerobject *self, char **s) {
525 int i;
526
527 if (self->buf_size == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000528 UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000529 PyErr_NoMemory();
530 return -1;
531 }
532
533 self->buf_size = 40;
534 }
535
536 i = 0;
537 while (1) {
538 for (; i < (self->buf_size - 1); i++) {
539 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
540 self->buf[i + 1] = '\0';
541 *s = self->buf;
542 return i + 1;
543 }
544 }
545
Guido van Rossum053b8df1998-11-25 16:18:00 +0000546 UNLESS (self->buf = (char *)realloc(self->buf,
Guido van Rossum60456fd1997-04-09 17:36:32 +0000547 (self->buf_size * 2) * sizeof(char))) {
548 PyErr_NoMemory();
549 return -1;
550 }
551
552 self->buf_size *= 2;
553 }
554
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000555}
556
557
558static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000559read_cStringIO(Unpicklerobject *self, char **s, int n) {
560 char *ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000561
Guido van Rossum60456fd1997-04-09 17:36:32 +0000562 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
563 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000564 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000565 }
566
Guido van Rossum60456fd1997-04-09 17:36:32 +0000567 *s = ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000568
Guido van Rossum60456fd1997-04-09 17:36:32 +0000569 return n;
570}
571
572
573static int
574readline_cStringIO(Unpicklerobject *self, char **s) {
575 int n;
576 char *ptr;
577
578 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
579 return -1;
580 }
581
582 *s = ptr;
583
584 return n;
585}
586
587
588static int
589read_other(Unpicklerobject *self, char **s, int n) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000590 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000591
Guido van Rossum053b8df1998-11-25 16:18:00 +0000592 UNLESS (bytes = PyInt_FromLong(n)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000593
Guido van Rossum053b8df1998-11-25 16:18:00 +0000594 ARG_TUP(self, bytes);
595 if (self->arg) {
596 str = PyObject_CallObject(self->read, self->arg);
597 FREE_ARG_TUP(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000598 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000599 if (! str) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000600
601 Py_XDECREF(self->last_string);
602 self->last_string = str;
603
Guido van Rossum053b8df1998-11-25 16:18:00 +0000604 if (! (*s = PyString_AsString(str))) return -1;
605 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000606}
607
608
609static int
610readline_other(Unpicklerobject *self, char **s) {
611 PyObject *str;
612 int str_size;
613
Guido van Rossum053b8df1998-11-25 16:18:00 +0000614 UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000615 return -1;
616 }
617
Guido van Rossum053b8df1998-11-25 16:18:00 +0000618 if ((str_size = PyString_Size(str)) < 0)
619 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000620
621 Py_XDECREF(self->last_string);
622 self->last_string = str;
623
Guido van Rossum053b8df1998-11-25 16:18:00 +0000624 if (! (*s = PyString_AsString(str)))
625 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000626
627 return str_size;
628}
629
630
631static char *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000632pystrndup(char *s, int l) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000633 char *r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000634 UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
Guido van Rossum60456fd1997-04-09 17:36:32 +0000635 memcpy(r,s,l);
636 r[l]=0;
637 return r;
638}
639
640
641static int
642get(Picklerobject *self, PyObject *id) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000643 PyObject *value, *mv;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000644 long c_value;
645 char s[30];
Guido van Rossum534b7c52000-06-28 22:23:56 +0000646 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000647
Guido van Rossum053b8df1998-11-25 16:18:00 +0000648 UNLESS (mv = PyDict_GetItem(self->memo, id)) {
649 PyErr_SetObject(PyExc_KeyError, id);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000650 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000651 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000652
Guido van Rossum053b8df1998-11-25 16:18:00 +0000653 UNLESS (value = PyTuple_GetItem(mv, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000654 return -1;
655
Guido van Rossum053b8df1998-11-25 16:18:00 +0000656 UNLESS (PyInt_Check(value)) {
657 PyErr_SetString(PicklingError, "no int where int expected in memo");
658 return -1;
659 }
660 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000661
662 if (!self->bin) {
663 s[0] = GET;
664 sprintf(s + 1, "%ld\n", c_value);
665 len = strlen(s);
666 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000667 else if (Pdata_Check(self->file)) {
668 if (write_other(self, NULL, 0) < 0) return -1;
669 PDATA_APPEND(self->file, mv, -1);
670 return 0;
671 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000672 else {
673 if (c_value < 256) {
674 s[0] = BINGET;
675 s[1] = (int)(c_value & 0xff);
676 len = 2;
677 }
678 else {
679 s[0] = LONG_BINGET;
680 s[1] = (int)(c_value & 0xff);
681 s[2] = (int)((c_value >> 8) & 0xff);
682 s[3] = (int)((c_value >> 16) & 0xff);
683 s[4] = (int)((c_value >> 24) & 0xff);
684 len = 5;
685 }
686 }
687
688 if ((*self->write_func)(self, s, len) < 0)
689 return -1;
690
691 return 0;
692}
693
694
695static int
696put(Picklerobject *self, PyObject *ob) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000697 if (ob->ob_refcnt < 2 || self->fast)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000698 return 0;
699
700 return put2(self, ob);
701}
702
703
704static int
705put2(Picklerobject *self, PyObject *ob) {
706 char c_str[30];
Guido van Rossum534b7c52000-06-28 22:23:56 +0000707 int p;
708 size_t len;
709 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000710 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000711
712 if (self->fast) return 0;
713
Guido van Rossum60456fd1997-04-09 17:36:32 +0000714 if ((p = PyDict_Size(self->memo)) < 0)
715 goto finally;
716
Guido van Rossum053b8df1998-11-25 16:18:00 +0000717 p++; /* Make sure memo keys are positive! */
718
Guido van Rossum534b7c52000-06-28 22:23:56 +0000719 UNLESS (py_ob_id = PyLong_FromVoidPtr(ob))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000720 goto finally;
721
722 UNLESS (memo_len = PyInt_FromLong(p))
723 goto finally;
724
725 UNLESS (t = PyTuple_New(2))
726 goto finally;
727
728 PyTuple_SET_ITEM(t, 0, memo_len);
729 Py_INCREF(memo_len);
730 PyTuple_SET_ITEM(t, 1, ob);
731 Py_INCREF(ob);
732
733 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
734 goto finally;
735
Guido van Rossum60456fd1997-04-09 17:36:32 +0000736 if (!self->bin) {
737 c_str[0] = PUT;
738 sprintf(c_str + 1, "%d\n", p);
739 len = strlen(c_str);
740 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000741 else if (Pdata_Check(self->file)) {
742 if (write_other(self, NULL, 0) < 0) return -1;
743 PDATA_APPEND(self->file, memo_len, -1);
744 res=0; /* Job well done ;) */
745 goto finally;
746 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000747 else {
748 if (p >= 256) {
749 c_str[0] = LONG_BINPUT;
750 c_str[1] = (int)(p & 0xff);
751 c_str[2] = (int)((p >> 8) & 0xff);
752 c_str[3] = (int)((p >> 16) & 0xff);
753 c_str[4] = (int)((p >> 24) & 0xff);
754 len = 5;
755 }
756 else {
757 c_str[0] = BINPUT;
758 c_str[1] = p;
759 len = 2;
760 }
761 }
762
763 if ((*self->write_func)(self, c_str, len) < 0)
764 goto finally;
765
Guido van Rossum60456fd1997-04-09 17:36:32 +0000766 res = 0;
767
768finally:
769 Py_XDECREF(py_ob_id);
770 Py_XDECREF(memo_len);
771 Py_XDECREF(t);
772
773 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000774}
775
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000776#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000777
778static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000779PyImport_Import(PyObject *module_name) {
780 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
781 static PyObject *standard_builtins=0;
782 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
783
Guido van Rossum053b8df1998-11-25 16:18:00 +0000784 UNLESS (silly_list) {
785 UNLESS (__import___str=PyString_FromString("__import__"))
786 return NULL;
787 UNLESS (__builtins___str=PyString_FromString("__builtins__"))
788 return NULL;
789 UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
790 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000791 }
792
Guido van Rossum053b8df1998-11-25 16:18:00 +0000793 if ((globals=PyEval_GetGlobals())) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000794 Py_INCREF(globals);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000795 UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
796 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000797 }
798 else {
799 PyErr_Clear();
800
Guido van Rossum053b8df1998-11-25 16:18:00 +0000801 UNLESS (standard_builtins ||
802 (standard_builtins=PyImport_ImportModule("__builtin__")))
803 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000804
805 __builtins__=standard_builtins;
806 Py_INCREF(__builtins__);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000807 UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
808 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000809 }
810
Guido van Rossum053b8df1998-11-25 16:18:00 +0000811 if (PyDict_Check(__builtins__)) {
812 UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000813 }
814 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000815 UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000816 }
817
Guido van Rossum053b8df1998-11-25 16:18:00 +0000818 UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
819 module_name, globals, globals, silly_list))
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000820 goto err;
821
822 Py_DECREF(globals);
823 Py_DECREF(__builtins__);
824 Py_DECREF(__import__);
825
826 return r;
827err:
828 Py_XDECREF(globals);
829 Py_XDECREF(__builtins__);
830 Py_XDECREF(__import__);
831 return NULL;
832}
833
834static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +0000835whichmodule(PyObject *global, PyObject *global_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000836 int i, j;
837 PyObject *module = 0, *modules_dict = 0,
838 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000839
Guido van Rossum45188231997-09-28 05:38:51 +0000840 module = PyObject_GetAttrString(global, "__module__");
841 if (module) return module;
842 PyErr_Clear();
843
Guido van Rossum053b8df1998-11-25 16:18:00 +0000844 UNLESS (modules_dict = PySys_GetObject("modules"))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000845 return NULL;
846
Guido van Rossum60456fd1997-04-09 17:36:32 +0000847 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000848 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
849
Guido van Rossum053b8df1998-11-25 16:18:00 +0000850 if (PyObject_Compare(name, __main___str)==0) continue;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000851
Guido van Rossum053b8df1998-11-25 16:18:00 +0000852 UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000853 PyErr_Clear();
854 continue;
855 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000856
Guido van Rossum60456fd1997-04-09 17:36:32 +0000857 if (global_name_attr != global) {
858 Py_DECREF(global_name_attr);
859 continue;
860 }
861
862 Py_DECREF(global_name_attr);
863
864 break;
865 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000866
867 /* The following implements the rule in pickle.py added in 1.5
868 that used __main__ if no module is found. I don't actually
869 like this rule. jlf
870 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000871 if (!j) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000872 j=1;
873 name=__main___str;
874 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000875
876 Py_INCREF(name);
877 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000878}
879
880
Guido van Rossum60456fd1997-04-09 17:36:32 +0000881static int
882save_none(Picklerobject *self, PyObject *args) {
883 static char none = NONE;
884 if ((*self->write_func)(self, &none, 1) < 0)
885 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000886
Guido van Rossum60456fd1997-04-09 17:36:32 +0000887 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000888}
889
890
Guido van Rossum60456fd1997-04-09 17:36:32 +0000891static int
892save_int(Picklerobject *self, PyObject *args) {
893 char c_str[32];
894 long l = PyInt_AS_LONG((PyIntObject *)args);
895 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000896
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000897 if (!self->bin
898#if SIZEOF_LONG > 4
Guido van Rossum053b8df1998-11-25 16:18:00 +0000899 || (l >> 32)
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000900#endif
Guido van Rossum053b8df1998-11-25 16:18:00 +0000901 ) {
902 /* Save extra-long ints in non-binary mode, so that
903 we can use python long parsing code to restore,
904 if necessary. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000905 c_str[0] = INT;
906 sprintf(c_str + 1, "%ld\n", l);
907 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
908 return -1;
909 }
910 else {
911 c_str[1] = (int)( l & 0xff);
912 c_str[2] = (int)((l >> 8) & 0xff);
913 c_str[3] = (int)((l >> 16) & 0xff);
914 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000915
Guido van Rossum60456fd1997-04-09 17:36:32 +0000916 if ((c_str[4] == 0) && (c_str[3] == 0)) {
917 if (c_str[2] == 0) {
918 c_str[0] = BININT1;
919 len = 2;
920 }
921 else {
922 c_str[0] = BININT2;
923 len = 3;
924 }
925 }
926 else {
927 c_str[0] = BININT;
928 len = 5;
929 }
930
931 if ((*self->write_func)(self, c_str, len) < 0)
932 return -1;
933 }
934
935 return 0;
936}
937
938
939static int
940save_long(Picklerobject *self, PyObject *args) {
941 int size, res = -1;
942 PyObject *repr = 0;
943
944 static char l = LONG;
945
Guido van Rossum053b8df1998-11-25 16:18:00 +0000946 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000947 goto finally;
948
949 if ((size = PyString_Size(repr)) < 0)
950 goto finally;
951
952 if ((*self->write_func)(self, &l, 1) < 0)
953 goto finally;
954
955 if ((*self->write_func)(self,
956 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
957 goto finally;
958
959 if ((*self->write_func)(self, "\n", 1) < 0)
960 goto finally;
961
962 res = 0;
963
964finally:
965 Py_XDECREF(repr);
966
967 return res;
968}
969
970
971static int
972save_float(Picklerobject *self, PyObject *args) {
973 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
974
Guido van Rossum60456fd1997-04-09 17:36:32 +0000975 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000976 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000977 double f;
978 long fhi, flo;
979 char str[9], *p = str;
980
981 *p = BINFLOAT;
982 p++;
983
984 if (x < 0) {
985 s = 1;
986 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000987 }
988 else
Guido van Rossum60456fd1997-04-09 17:36:32 +0000989 s = 0;
990
991 f = frexp(x, &e);
992
993 /* Normalize f to be in the range [1.0, 2.0) */
994 if (0.5 <= f && f < 1.0) {
995 f *= 2.0;
996 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000997 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000998 else if (f == 0.0) {
999 e = 0;
1000 }
1001 else {
1002 PyErr_SetString(PyExc_SystemError,
1003 "frexp() result out of range");
1004 return -1;
1005 }
1006
1007 if (e >= 1024) {
1008 /* XXX 1024 itself is reserved for Inf/NaN */
1009 PyErr_SetString(PyExc_OverflowError,
1010 "float too large to pack with d format");
1011 return -1;
1012 }
1013 else if (e < -1022) {
1014 /* Gradual underflow */
1015 f = ldexp(f, 1022 + e);
1016 e = 0;
1017 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001018 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001019 e += 1023;
1020 f -= 1.0; /* Get rid of leading 1 */
1021 }
1022
1023 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1024 f *= 268435456.0; /* 2**28 */
1025 fhi = (long) floor(f); /* Truncate */
1026 f -= (double)fhi;
1027 f *= 16777216.0; /* 2**24 */
1028 flo = (long) floor(f + 0.5); /* Round */
1029
1030 /* First byte */
1031 *p = (s<<7) | (e>>4);
1032 p++;
1033
1034 /* Second byte */
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001035 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum60456fd1997-04-09 17:36:32 +00001036 p++;
1037
1038 /* Third byte */
1039 *p = (fhi>>16) & 0xFF;
1040 p++;
1041
1042 /* Fourth byte */
1043 *p = (fhi>>8) & 0xFF;
1044 p++;
1045
1046 /* Fifth byte */
1047 *p = fhi & 0xFF;
1048 p++;
1049
1050 /* Sixth byte */
1051 *p = (flo>>16) & 0xFF;
1052 p++;
1053
1054 /* Seventh byte */
1055 *p = (flo>>8) & 0xFF;
1056 p++;
1057
1058 /* Eighth byte */
1059 *p = flo & 0xFF;
1060
1061 if ((*self->write_func)(self, str, 9) < 0)
1062 return -1;
1063 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001064 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001065 char c_str[250];
1066 c_str[0] = FLOAT;
Guido van Rossum104be4a1998-04-03 21:13:02 +00001067 sprintf(c_str + 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001068
1069 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
1070 return -1;
1071 }
1072
1073 return 0;
1074}
1075
1076
1077static int
Guido van Rossum142eeb81997-08-13 03:14:41 +00001078save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001079 int size, len;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001080 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001081
Guido van Rossum053b8df1998-11-25 16:18:00 +00001082 if ((size = PyString_Size(args)) < 0)
1083 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001084
1085 if (!self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001086 char *repr_str;
1087
1088 static char string = STRING;
1089
Guido van Rossum053b8df1998-11-25 16:18:00 +00001090 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001091 return -1;
1092
Guido van Rossum053b8df1998-11-25 16:18:00 +00001093 if ((len = PyString_Size(repr)) < 0)
1094 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001095 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001096
1097 if ((*self->write_func)(self, &string, 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001098 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001099
1100 if ((*self->write_func)(self, repr_str, len) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001101 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001102
1103 if ((*self->write_func)(self, "\n", 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001104 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001105
1106 Py_XDECREF(repr);
1107 }
1108 else {
1109 int i;
1110 char c_str[5];
1111
Guido van Rossum053b8df1998-11-25 16:18:00 +00001112 if ((size = PyString_Size(args)) < 0)
1113 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001114
1115 if (size < 256) {
1116 c_str[0] = SHORT_BINSTRING;
1117 c_str[1] = size;
1118 len = 2;
1119 }
1120 else {
1121 c_str[0] = BINSTRING;
1122 for (i = 1; i < 5; i++)
1123 c_str[i] = (int)(size >> ((i - 1) * 8));
1124 len = 5;
1125 }
1126
1127 if ((*self->write_func)(self, c_str, len) < 0)
1128 return -1;
1129
Guido van Rossum053b8df1998-11-25 16:18:00 +00001130 if (size > 128 && Pdata_Check(self->file)) {
1131 if (write_other(self, NULL, 0) < 0) return -1;
1132 PDATA_APPEND(self->file, args, -1);
1133 }
1134 else {
1135 if ((*self->write_func)(self,
1136 PyString_AS_STRING((PyStringObject *)args), size) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001137 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001138 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001139 }
1140
Guido van Rossum142eeb81997-08-13 03:14:41 +00001141 if (doput)
1142 if (put(self, args) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001143 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001144
1145 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001146
1147err:
1148 Py_XDECREF(repr);
1149 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001150}
1151
1152
1153static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001154save_unicode(Picklerobject *self, PyObject *args, int doput) {
1155 int size, len;
1156 PyObject *repr=0;
1157
1158 if (!PyUnicode_Check(args))
1159 return -1;
1160
1161 if (!self->bin) {
1162 char *repr_str;
1163 static char string = UNICODE;
1164
1165 UNLESS (repr = PyUnicode_AsRawUnicodeEscapeString(args))
1166 return -1;
1167
1168 if ((len = PyString_Size(repr)) < 0)
1169 goto err;
1170 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1171
1172 if ((*self->write_func)(self, &string, 1) < 0)
1173 goto err;
1174
1175 if ((*self->write_func)(self, repr_str, len) < 0)
1176 goto err;
1177
1178 if ((*self->write_func)(self, "\n", 1) < 0)
1179 goto err;
1180
1181 Py_XDECREF(repr);
1182 }
1183 else {
1184 int i;
1185 char c_str[5];
1186
1187 UNLESS (repr = PyUnicode_AsUTF8String(args))
1188 return -1;
1189
1190 if ((size = PyString_Size(repr)) < 0)
1191 goto err;
1192
1193 c_str[0] = BINUNICODE;
1194 for (i = 1; i < 5; i++)
1195 c_str[i] = (int)(size >> ((i - 1) * 8));
1196 len = 5;
1197
1198 if ((*self->write_func)(self, c_str, len) < 0)
1199 goto err;
1200
1201 if (size > 128 && Pdata_Check(self->file)) {
1202 if (write_other(self, NULL, 0) < 0)
1203 goto err;
1204 PDATA_APPEND(self->file, repr, -1);
1205 }
1206 else {
1207 if ((*self->write_func)(self, PyString_AS_STRING(repr), size) < 0)
1208 goto err;
1209 }
1210
1211 Py_DECREF(repr);
1212 }
1213
1214 if (doput)
1215 if (put(self, args) < 0)
1216 return -1;
1217
1218 return 0;
1219
1220err:
1221 Py_XDECREF(repr);
1222 return -1;
1223}
1224
1225
1226static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001227save_tuple(Picklerobject *self, PyObject *args) {
1228 PyObject *element = 0, *py_tuple_id = 0;
1229 int len, i, has_key, res = -1;
1230
1231 static char tuple = TUPLE;
1232
1233 if ((*self->write_func)(self, &MARKv, 1) < 0)
1234 goto finally;
1235
1236 if ((len = PyTuple_Size(args)) < 0)
1237 goto finally;
1238
1239 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001240 UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001241 goto finally;
1242
1243 if (save(self, element, 0) < 0)
1244 goto finally;
1245 }
1246
Guido van Rossum534b7c52000-06-28 22:23:56 +00001247 UNLESS (py_tuple_id = PyLong_FromVoidPtr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001248 goto finally;
1249
1250 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001251 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001252 goto finally;
1253
1254 if (has_key) {
1255 if (self->bin) {
1256 static char pop_mark = POP_MARK;
1257
1258 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1259 goto finally;
1260 }
1261 else {
1262 static char pop = POP;
1263
1264 for (i = 0; i <= len; i++) {
1265 if ((*self->write_func)(self, &pop, 1) < 0)
1266 goto finally;
1267 }
1268 }
1269
1270 if (get(self, py_tuple_id) < 0)
1271 goto finally;
1272
1273 res = 0;
1274 goto finally;
1275 }
1276 }
1277
1278 if ((*self->write_func)(self, &tuple, 1) < 0) {
1279 goto finally;
1280 }
1281
1282 if (put(self, args) < 0)
1283 goto finally;
1284
1285 res = 0;
1286
1287finally:
1288 Py_XDECREF(py_tuple_id);
1289
1290 return res;
1291}
1292
1293static int
1294save_empty_tuple(Picklerobject *self, PyObject *args) {
1295 static char tuple = EMPTY_TUPLE;
1296
1297 return (*self->write_func)(self, &tuple, 1);
1298}
1299
1300
1301static int
1302save_list(Picklerobject *self, PyObject *args) {
1303 PyObject *element = 0;
1304 int s_len, len, i, using_appends, res = -1;
1305 char s[3];
1306
1307 static char append = APPEND, appends = APPENDS;
1308
Guido van Rossum053b8df1998-11-25 16:18:00 +00001309 if (self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001310 s[0] = EMPTY_LIST;
1311 s_len = 1;
1312 }
1313 else {
1314 s[0] = MARK;
1315 s[1] = LIST;
1316 s_len = 2;
1317 }
1318
1319 if ((len = PyList_Size(args)) < 0)
1320 goto finally;
1321
1322 if ((*self->write_func)(self, s, s_len) < 0)
1323 goto finally;
1324
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001325 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001326 if (put(self, args) < 0)
1327 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001328 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001329 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001330 if (put2(self, args) < 0)
1331 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001332 }
1333
Guido van Rossum142eeb81997-08-13 03:14:41 +00001334 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001335 if ((*self->write_func)(self, &MARKv, 1) < 0)
1336 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001337
Guido van Rossum60456fd1997-04-09 17:36:32 +00001338 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001339 UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001340 goto finally;
1341
1342 if (save(self, element, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001343 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001344
1345 if (!using_appends) {
1346 if ((*self->write_func)(self, &append, 1) < 0)
1347 goto finally;
1348 }
1349 }
1350
1351 if (using_appends) {
1352 if ((*self->write_func)(self, &appends, 1) < 0)
1353 goto finally;
1354 }
1355
1356 res = 0;
1357
1358finally:
1359
1360 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001361}
1362
1363
Guido van Rossum60456fd1997-04-09 17:36:32 +00001364static int
1365save_dict(Picklerobject *self, PyObject *args) {
1366 PyObject *key = 0, *value = 0;
1367 int i, len, res = -1, using_setitems;
1368 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001369
Guido van Rossum60456fd1997-04-09 17:36:32 +00001370 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001371
Guido van Rossum60456fd1997-04-09 17:36:32 +00001372 if (self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001373 s[0] = EMPTY_DICT;
1374 len = 1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001375 }
1376 else {
1377 s[0] = MARK;
1378 s[1] = DICT;
1379 len = 2;
1380 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001381
Guido van Rossum60456fd1997-04-09 17:36:32 +00001382 if ((*self->write_func)(self, s, len) < 0)
1383 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001384
Guido van Rossum60456fd1997-04-09 17:36:32 +00001385 if ((len = PyDict_Size(args)) < 0)
1386 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001387
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001388 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001389 if (put(self, args) < 0)
1390 goto finally;
1391 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001392 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001393 if (put2(self, args) < 0)
1394 goto finally;
1395 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001396
Guido van Rossum142eeb81997-08-13 03:14:41 +00001397 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001398 if ((*self->write_func)(self, &MARKv, 1) < 0)
1399 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001400
Guido van Rossum60456fd1997-04-09 17:36:32 +00001401 i = 0;
1402 while (PyDict_Next(args, &i, &key, &value)) {
1403 if (save(self, key, 0) < 0)
1404 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001405
Guido van Rossum60456fd1997-04-09 17:36:32 +00001406 if (save(self, value, 0) < 0)
1407 goto finally;
1408
1409 if (!using_setitems) {
1410 if ((*self->write_func)(self, &setitem, 1) < 0)
1411 goto finally;
1412 }
1413 }
1414
1415 if (using_setitems) {
1416 if ((*self->write_func)(self, &setitems, 1) < 0)
1417 goto finally;
1418 }
1419
1420 res = 0;
1421
1422finally:
1423
1424 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001425}
1426
1427
Guido van Rossum60456fd1997-04-09 17:36:32 +00001428static int
1429save_inst(Picklerobject *self, PyObject *args) {
1430 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1431 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1432 char *module_str, *name_str;
1433 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001434
Guido van Rossum60456fd1997-04-09 17:36:32 +00001435 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001436
Guido van Rossum60456fd1997-04-09 17:36:32 +00001437 if ((*self->write_func)(self, &MARKv, 1) < 0)
1438 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001439
Guido van Rossum053b8df1998-11-25 16:18:00 +00001440 UNLESS (class = PyObject_GetAttr(args, __class___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001441 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001442
Guido van Rossum60456fd1997-04-09 17:36:32 +00001443 if (self->bin) {
1444 if (save(self, class, 0) < 0)
1445 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001446 }
1447
Guido van Rossum142eeb81997-08-13 03:14:41 +00001448 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001449 PyObject *element = 0;
1450 int i, len;
1451
Guido van Rossum053b8df1998-11-25 16:18:00 +00001452 UNLESS (class_args =
Guido van Rossum60456fd1997-04-09 17:36:32 +00001453 PyObject_CallObject(getinitargs_func, empty_tuple))
1454 goto finally;
1455
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001456 if ((len = PyObject_Size(class_args)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001457 goto finally;
1458
1459 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001460 UNLESS (element = PySequence_GetItem(class_args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001461 goto finally;
1462
1463 if (save(self, element, 0) < 0) {
1464 Py_DECREF(element);
1465 goto finally;
1466 }
1467
1468 Py_DECREF(element);
1469 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001470 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001471 else {
1472 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001473 }
1474
Guido van Rossum60456fd1997-04-09 17:36:32 +00001475 if (!self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001476 UNLESS (name = ((PyClassObject *)class)->cl_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001477 PyErr_SetString(PicklingError, "class has no name");
1478 goto finally;
1479 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001480
Guido van Rossum053b8df1998-11-25 16:18:00 +00001481 UNLESS (module = whichmodule(class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001482 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001483
1484
1485 if ((module_size = PyString_Size(module)) < 0 ||
1486 (name_size = PyString_Size(name)) < 0)
1487 goto finally;
1488
Guido van Rossum60456fd1997-04-09 17:36:32 +00001489 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001490 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001491
Guido van Rossum60456fd1997-04-09 17:36:32 +00001492 if ((*self->write_func)(self, &inst, 1) < 0)
1493 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001494
Guido van Rossum60456fd1997-04-09 17:36:32 +00001495 if ((*self->write_func)(self, module_str, module_size) < 0)
1496 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001497
Guido van Rossum60456fd1997-04-09 17:36:32 +00001498 if ((*self->write_func)(self, "\n", 1) < 0)
1499 goto finally;
1500
1501 if ((*self->write_func)(self, name_str, name_size) < 0)
1502 goto finally;
1503
1504 if ((*self->write_func)(self, "\n", 1) < 0)
1505 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001506 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001507 else if ((*self->write_func)(self, &obj, 1) < 0) {
1508 goto finally;
1509 }
1510
Guido van Rossum142eeb81997-08-13 03:14:41 +00001511 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001512 UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001513 goto finally;
1514 }
1515 else {
1516 PyErr_Clear();
1517
Guido van Rossum053b8df1998-11-25 16:18:00 +00001518 UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001519 PyErr_Clear();
1520 res = 0;
1521 goto finally;
1522 }
1523 }
1524
1525 if (!PyDict_Check(state)) {
1526 if (put2(self, args) < 0)
1527 goto finally;
1528 }
1529 else {
1530 if (put(self, args) < 0)
1531 goto finally;
1532 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001533
Guido van Rossum60456fd1997-04-09 17:36:32 +00001534 if (save(self, state, 0) < 0)
1535 goto finally;
1536
1537 if ((*self->write_func)(self, &build, 1) < 0)
1538 goto finally;
1539
1540 res = 0;
1541
1542finally:
1543 Py_XDECREF(module);
1544 Py_XDECREF(class);
1545 Py_XDECREF(state);
1546 Py_XDECREF(getinitargs_func);
1547 Py_XDECREF(getstate_func);
1548 Py_XDECREF(class_args);
1549
1550 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001551}
1552
1553
Guido van Rossum60456fd1997-04-09 17:36:32 +00001554static int
1555save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1556 PyObject *global_name = 0, *module = 0;
1557 char *name_str, *module_str;
1558 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001559
Guido van Rossum60456fd1997-04-09 17:36:32 +00001560 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001561
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001562 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001563 global_name = name;
1564 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001565 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001566 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001567 UNLESS (global_name = PyObject_GetAttr(args, __name___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001568 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001569 }
1570
Guido van Rossum053b8df1998-11-25 16:18:00 +00001571 UNLESS (module = whichmodule(args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001572 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001573
Guido van Rossum053b8df1998-11-25 16:18:00 +00001574 if ((module_size = PyString_Size(module)) < 0 ||
1575 (name_size = PyString_Size(global_name)) < 0)
1576 goto finally;
1577
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001578 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001579 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001580
Guido van Rossum60456fd1997-04-09 17:36:32 +00001581 if ((*self->write_func)(self, &global, 1) < 0)
1582 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001583
Guido van Rossum60456fd1997-04-09 17:36:32 +00001584 if ((*self->write_func)(self, module_str, module_size) < 0)
1585 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001586
Guido van Rossum60456fd1997-04-09 17:36:32 +00001587 if ((*self->write_func)(self, "\n", 1) < 0)
1588 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001589
Guido van Rossum60456fd1997-04-09 17:36:32 +00001590 if ((*self->write_func)(self, name_str, name_size) < 0)
1591 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001592
Guido van Rossum60456fd1997-04-09 17:36:32 +00001593 if ((*self->write_func)(self, "\n", 1) < 0)
1594 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001595
Guido van Rossum60456fd1997-04-09 17:36:32 +00001596 if (put(self, args) < 0)
1597 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001598
Guido van Rossum60456fd1997-04-09 17:36:32 +00001599 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001600
Guido van Rossum60456fd1997-04-09 17:36:32 +00001601finally:
1602 Py_XDECREF(module);
1603 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001604
Guido van Rossum60456fd1997-04-09 17:36:32 +00001605 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001606}
1607
Guido van Rossum60456fd1997-04-09 17:36:32 +00001608static int
1609save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1610 PyObject *pid = 0;
1611 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001612
Guido van Rossum60456fd1997-04-09 17:36:32 +00001613 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001614
Guido van Rossum053b8df1998-11-25 16:18:00 +00001615 Py_INCREF(args);
1616 ARG_TUP(self, args);
1617 if (self->arg) {
1618 pid = PyObject_CallObject(f, self->arg);
1619 FREE_ARG_TUP(self);
1620 }
1621 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001622
Guido van Rossum60456fd1997-04-09 17:36:32 +00001623 if (pid != Py_None) {
1624 if (!self->bin) {
1625 if (!PyString_Check(pid)) {
1626 PyErr_SetString(PicklingError,
1627 "persistent id must be string");
1628 goto finally;
1629 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001630
Guido van Rossum60456fd1997-04-09 17:36:32 +00001631 if ((*self->write_func)(self, &persid, 1) < 0)
1632 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001633
Guido van Rossum60456fd1997-04-09 17:36:32 +00001634 if ((size = PyString_Size(pid)) < 0)
1635 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001636
Guido van Rossum60456fd1997-04-09 17:36:32 +00001637 if ((*self->write_func)(self,
1638 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1639 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001640
Guido van Rossum60456fd1997-04-09 17:36:32 +00001641 if ((*self->write_func)(self, "\n", 1) < 0)
1642 goto finally;
1643
1644 res = 1;
1645 goto finally;
1646 }
1647 else if (save(self, pid, 1) >= 0) {
1648 if ((*self->write_func)(self, &binpersid, 1) < 0)
1649 res = -1;
1650 else
1651 res = 1;
1652 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001653
Guido van Rossum60456fd1997-04-09 17:36:32 +00001654 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001655 }
1656
Guido van Rossum60456fd1997-04-09 17:36:32 +00001657 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001658
Guido van Rossum60456fd1997-04-09 17:36:32 +00001659finally:
1660 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001661
Guido van Rossum60456fd1997-04-09 17:36:32 +00001662 return res;
1663}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001664
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001665
Guido van Rossum60456fd1997-04-09 17:36:32 +00001666static int
1667save_reduce(Picklerobject *self, PyObject *callable,
1668 PyObject *tup, PyObject *state, PyObject *ob) {
1669 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001670
Guido van Rossum60456fd1997-04-09 17:36:32 +00001671 if (save(self, callable, 0) < 0)
1672 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001673
Guido van Rossum60456fd1997-04-09 17:36:32 +00001674 if (save(self, tup, 0) < 0)
1675 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001676
Guido van Rossum60456fd1997-04-09 17:36:32 +00001677 if ((*self->write_func)(self, &reduce, 1) < 0)
1678 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001679
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001680 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001681 if (state && !PyDict_Check(state)) {
1682 if (put2(self, ob) < 0)
1683 return -1;
1684 }
1685 else {
1686 if (put(self, ob) < 0)
1687 return -1;
1688 }
1689 }
1690
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001691 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001692 if (save(self, state, 0) < 0)
1693 return -1;
1694
1695 if ((*self->write_func)(self, &build, 1) < 0)
1696 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001697 }
1698
Guido van Rossum60456fd1997-04-09 17:36:32 +00001699 return 0;
1700}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001701
Guido van Rossum60456fd1997-04-09 17:36:32 +00001702static int
1703save(Picklerobject *self, PyObject *args, int pers_save) {
1704 PyTypeObject *type;
1705 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001706 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001707 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001708
Guido van Rossum60456fd1997-04-09 17:36:32 +00001709 if (!pers_save && self->pers_func) {
1710 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1711 res = tmp;
1712 goto finally;
1713 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001714 }
1715
Guido van Rossum60456fd1997-04-09 17:36:32 +00001716 if (args == Py_None) {
1717 res = save_none(self, args);
1718 goto finally;
1719 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001720
Guido van Rossum60456fd1997-04-09 17:36:32 +00001721 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001722
Guido van Rossum60456fd1997-04-09 17:36:32 +00001723 switch (type->tp_name[0]) {
1724 case 'i':
1725 if (type == &PyInt_Type) {
1726 res = save_int(self, args);
1727 goto finally;
1728 }
1729 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001730
Guido van Rossum60456fd1997-04-09 17:36:32 +00001731 case 'l':
1732 if (type == &PyLong_Type) {
1733 res = save_long(self, args);
1734 goto finally;
1735 }
1736 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001737
Guido van Rossum60456fd1997-04-09 17:36:32 +00001738 case 'f':
1739 if (type == &PyFloat_Type) {
1740 res = save_float(self, args);
1741 goto finally;
1742 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001743 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001744
Guido van Rossum60456fd1997-04-09 17:36:32 +00001745 case 't':
1746 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001747 if (self->bin) res = save_empty_tuple(self, args);
1748 else res = save_tuple(self, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001749 goto finally;
1750 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001751 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001752
Guido van Rossum60456fd1997-04-09 17:36:32 +00001753 case 's':
Guido van Rossum053b8df1998-11-25 16:18:00 +00001754 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001755 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001756 goto finally;
1757 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001758
1759 case 'u':
1760 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
1761 res = save_unicode(self, args, 0);
1762 goto finally;
1763 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001764 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001765
Guido van Rossum60456fd1997-04-09 17:36:32 +00001766 if (args->ob_refcnt > 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001767 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001768
Guido van Rossum534b7c52000-06-28 22:23:56 +00001769 UNLESS (py_ob_id = PyLong_FromVoidPtr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001770 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001771
Guido van Rossum60456fd1997-04-09 17:36:32 +00001772 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1773 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001774
Guido van Rossum60456fd1997-04-09 17:36:32 +00001775 if (has_key) {
1776 if (get(self, py_ob_id) < 0)
1777 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001778
Guido van Rossum60456fd1997-04-09 17:36:32 +00001779 res = 0;
1780 goto finally;
1781 }
1782 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001783
Guido van Rossum60456fd1997-04-09 17:36:32 +00001784 switch (type->tp_name[0]) {
1785 case 's':
1786 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001787 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001788 goto finally;
1789 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001790 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001791
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001792 case 'u':
1793 if (type == &PyUnicode_Type) {
1794 res = save_unicode(self, args, 1);
1795 goto finally;
1796 }
1797 break;
1798
Guido van Rossum60456fd1997-04-09 17:36:32 +00001799 case 't':
1800 if (type == &PyTuple_Type) {
1801 res = save_tuple(self, args);
1802 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001803 }
1804 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001805
Guido van Rossum60456fd1997-04-09 17:36:32 +00001806 case 'l':
1807 if (type == &PyList_Type) {
1808 res = save_list(self, args);
1809 goto finally;
1810 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001811 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001812
1813 case 'd':
1814 if (type == &PyDict_Type) {
1815 res = save_dict(self, args);
1816 goto finally;
1817 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001818 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001819
1820 case 'i':
1821 if (type == &PyInstance_Type) {
1822 res = save_inst(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 'c':
1828 if (type == &PyClass_Type) {
1829 res = save_global(self, args, NULL);
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 'f':
1835 if (type == &PyFunction_Type) {
1836 res = save_global(self, args, NULL);
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 'b':
1842 if (type == &PyCFunction_Type) {
1843 res = save_global(self, args, NULL);
1844 goto finally;
1845 }
1846 }
1847
1848 if (!pers_save && self->inst_pers_func) {
1849 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1850 res = tmp;
1851 goto finally;
1852 }
1853 }
1854
Guido van Rossum142eeb81997-08-13 03:14:41 +00001855 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001856 Py_INCREF(__reduce__);
1857
Guido van Rossum60456fd1997-04-09 17:36:32 +00001858 Py_INCREF(args);
Guido van Rossum053b8df1998-11-25 16:18:00 +00001859 ARG_TUP(self, args);
1860 if (self->arg) {
1861 t = PyObject_CallObject(__reduce__, self->arg);
1862 FREE_ARG_TUP(self);
1863 }
1864 if (! t) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001865 }
1866 else {
1867 PyErr_Clear();
1868
Guido van Rossum142eeb81997-08-13 03:14:41 +00001869 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001870 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001871 goto finally;
1872 }
1873 else {
1874 PyErr_Clear();
1875 }
1876 }
1877
1878 if (t) {
1879 if (PyString_Check(t)) {
1880 res = save_global(self, args, t);
1881 goto finally;
1882 }
1883
1884 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001885 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001886 "be a tuple", "O", __reduce__);
1887 goto finally;
1888 }
1889
1890 size = PyTuple_Size(t);
1891
1892 if ((size != 3) && (size != 2)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001893 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001894 "contain only two or three elements", "O", __reduce__);
1895 goto finally;
1896 }
1897
1898 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001899
Guido van Rossum60456fd1997-04-09 17:36:32 +00001900 arg_tup = PyTuple_GET_ITEM(t, 1);
1901
1902 if (size > 2) {
1903 state = PyTuple_GET_ITEM(t, 2);
1904 }
1905
Guido van Rossum053b8df1998-11-25 16:18:00 +00001906 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001907 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001908 "returned by %s must be a tuple", "O", __reduce__);
1909 goto finally;
1910 }
1911
1912 res = save_reduce(self, callable, arg_tup, state, args);
1913 goto finally;
1914 }
1915
Guido van Rossumc03158b1999-06-09 15:23:31 +00001916 PyErr_SetObject(UnpickleableError, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001917
1918finally:
1919 Py_XDECREF(py_ob_id);
1920 Py_XDECREF(__reduce__);
1921 Py_XDECREF(t);
1922
1923 return res;
1924}
1925
1926
1927static int
1928dump(Picklerobject *self, PyObject *args) {
1929 static char stop = STOP;
1930
1931 if (save(self, args, 0) < 0)
1932 return -1;
1933
1934 if ((*self->write_func)(self, &stop, 1) < 0)
1935 return -1;
1936
1937 if ((*self->write_func)(self, NULL, 0) < 0)
1938 return -1;
1939
1940 return 0;
1941}
1942
1943static PyObject *
Guido van Rossum053b8df1998-11-25 16:18:00 +00001944Pickle_clear_memo(Picklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00001945 if (args && ! PyArg_ParseTuple(args,":clear_memo")) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001946 if (self->memo) PyDict_Clear(self->memo);
1947 Py_INCREF(Py_None);
1948 return Py_None;
1949}
1950
1951static PyObject *
1952Pickle_getvalue(Picklerobject *self, PyObject *args) {
1953 int l, i, rsize, ssize, clear=1, lm;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001954 long ik;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001955 PyObject *k, *r;
1956 char *s, *p, *have_get;
1957 Pdata *data;
1958
Guido van Rossum43713e52000-02-29 13:59:29 +00001959 if (args && ! PyArg_ParseTuple(args,"|i:getvalue",&clear)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001960
1961 /* Check to make sure we are based on a list */
1962 if (! Pdata_Check(self->file)) {
1963 PyErr_SetString(PicklingError,
1964 "Attempt to getvalue a non-list-based pickler");
1965 return NULL;
1966 }
1967
1968 /* flush write buffer */
1969 if (write_other(self, NULL, 0) < 0) return NULL;
1970
1971 data=(Pdata*)self->file;
1972 l=data->length;
1973
1974 /* set up an array to hold get/put status */
1975 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
1976 lm++;
1977 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
1978 memset(have_get,0,lm);
1979
1980 /* Scan for gets. */
1981 for (rsize=0, i=l; --i >= 0; ) {
1982 k=data->data[i];
1983
1984 if (PyString_Check(k)) {
1985 rsize += PyString_GET_SIZE(k);
1986 }
1987
1988 else if (PyInt_Check(k)) { /* put */
1989 ik=PyInt_AS_LONG((PyIntObject*)k);
1990 if (ik >= lm || ik==0) {
1991 PyErr_SetString(PicklingError,
1992 "Invalid get data");
1993 return NULL;
1994 }
1995 if (have_get[ik]) { /* with matching get */
1996 if (ik < 256) rsize += 2;
1997 else rsize+=5;
1998 }
1999 }
2000
2001 else if (! (PyTuple_Check(k) &&
2002 PyTuple_GET_SIZE(k) == 2 &&
2003 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
2004 ) {
2005 PyErr_SetString(PicklingError,
2006 "Unexpected data in internal list");
2007 return NULL;
2008 }
2009
2010 else { /* put */
2011 ik=PyInt_AS_LONG((PyIntObject*)k);
2012 if (ik >= lm || ik==0) {
2013 PyErr_SetString(PicklingError,
2014 "Invalid get data");
2015 return NULL;
2016 }
2017 have_get[ik]=1;
2018 if (ik < 256) rsize += 2;
2019 else rsize+=5;
2020 }
2021
2022 }
2023
2024 /* Now generate the result */
2025 UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
2026 s=PyString_AS_STRING((PyStringObject*)r);
2027
2028 for (i=0; i<l; i++) {
2029 k=data->data[i];
2030
2031 if (PyString_Check(k)) {
2032 ssize=PyString_GET_SIZE(k);
2033 if (ssize) {
2034 p=PyString_AS_STRING((PyStringObject*)k);
2035 while (--ssize >= 0) *s++=*p++;
2036 }
2037 }
2038
2039 else if (PyTuple_Check(k)) { /* get */
2040 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
2041 if (ik < 256) {
2042 *s++ = BINGET;
2043 *s++ = (int)(ik & 0xff);
2044 }
2045 else {
2046 *s++ = LONG_BINGET;
2047 *s++ = (int)(ik & 0xff);
2048 *s++ = (int)((ik >> 8) & 0xff);
2049 *s++ = (int)((ik >> 16) & 0xff);
2050 *s++ = (int)((ik >> 24) & 0xff);
2051 }
2052 }
2053
2054 else { /* put */
2055 ik=PyInt_AS_LONG((PyIntObject*)k);
2056
2057 if (have_get[ik]) { /* with matching get */
2058 if (ik < 256) {
2059 *s++ = BINPUT;
2060 *s++ = (int)(ik & 0xff);
2061 }
2062 else {
2063 *s++ = LONG_BINPUT;
2064 *s++ = (int)(ik & 0xff);
2065 *s++ = (int)((ik >> 8) & 0xff);
2066 *s++ = (int)((ik >> 16) & 0xff);
2067 *s++ = (int)((ik >> 24) & 0xff);
2068 }
2069 }
2070 }
2071
2072 }
2073
2074 if (clear) {
2075 PyDict_Clear(self->memo);
2076 Pdata_clear(data,0);
2077 }
2078
2079 free(have_get);
2080 return r;
2081err:
2082 free(have_get);
2083 return NULL;
2084}
2085
2086static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002087Pickler_dump(Picklerobject *self, PyObject *args) {
2088 PyObject *ob;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002089 int get=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002090
Guido van Rossum43713e52000-02-29 13:59:29 +00002091 UNLESS (PyArg_ParseTuple(args, "O|i:dump", &ob, &get))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002092 return NULL;
2093
2094 if (dump(self, ob) < 0)
2095 return NULL;
2096
Guido van Rossum053b8df1998-11-25 16:18:00 +00002097 if (get) return Pickle_getvalue(self, NULL);
2098
2099 Py_INCREF(self);
2100 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002101}
2102
2103
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002104static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00002105 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002106 "dump(object) --"
2107 "Write an object in pickle format to the object's pickle stream\n"
2108 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00002109 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002110 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum053b8df1998-11-25 16:18:00 +00002111 {"getvalue", (PyCFunction)Pickle_getvalue, 1,
2112 "getvalue() -- Finish picking a list-based pickle"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002113 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002114};
2115
2116
2117static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002118newPicklerobject(PyObject *file, int bin) {
2119 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002120
Guido van Rossumb18618d2000-05-03 23:44:39 +00002121 UNLESS (self = PyObject_New(Picklerobject, &Picklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002122 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002123
2124 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002125 self->write = NULL;
2126 self->memo = NULL;
2127 self->arg = NULL;
2128 self->pers_func = NULL;
2129 self->inst_pers_func = NULL;
2130 self->write_buf = NULL;
2131 self->bin = bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002132 self->fast = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002133 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002134 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002135
Guido van Rossum053b8df1998-11-25 16:18:00 +00002136 if (file)
2137 Py_INCREF(file);
2138 else
Guido van Rossum50f385c1998-12-04 18:48:44 +00002139 file=Pdata_New();
Guido van Rossum83addc72000-04-21 20:49:36 +00002140
2141 UNLESS (self->file = file)
2142 goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002143
Guido van Rossum83addc72000-04-21 20:49:36 +00002144 UNLESS (self->memo = PyDict_New())
2145 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002146
Guido van Rossum60456fd1997-04-09 17:36:32 +00002147 if (PyFile_Check(file)) {
2148 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002149 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00002150 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
2151 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002152 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002153 self->write_func = write_file;
2154 }
2155 else if (PycStringIO_OutputCheck(file)) {
2156 self->write_func = write_cStringIO;
2157 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002158 else if (file == Py_None) {
2159 self->write_func = write_none;
2160 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002161 else {
2162 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002163
Guido van Rossum053b8df1998-11-25 16:18:00 +00002164 if (! Pdata_Check(file)) {
2165 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002166 PyErr_Clear();
2167 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002168 "attribute");
2169 goto err;
2170 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002171 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002172
Guido van Rossum053b8df1998-11-25 16:18:00 +00002173 UNLESS (self->write_buf =
Guido van Rossum60456fd1997-04-09 17:36:32 +00002174 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
2175 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002176 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002177 }
2178 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002179
Guido van Rossum053b8df1998-11-25 16:18:00 +00002180 if (PyEval_GetRestricted()) {
2181 /* Restricted execution, get private tables */
2182 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002183
Guido van Rossum053b8df1998-11-25 16:18:00 +00002184 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2185 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2186 Py_DECREF(m);
2187 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002188 }
2189 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002190 self->dispatch_table=dispatch_table;
2191 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002192 }
2193
Guido van Rossum60456fd1997-04-09 17:36:32 +00002194 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002195
2196err:
2197 Py_DECREF((PyObject *)self);
2198 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002199}
2200
2201
2202static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002203get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002204 PyObject *file=NULL;
2205 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002206
Guido van Rossum053b8df1998-11-25 16:18:00 +00002207 bin=1;
Guido van Rossum43713e52000-02-29 13:59:29 +00002208 if (! PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002209 PyErr_Clear();
2210 bin=0;
Guido van Rossum43713e52000-02-29 13:59:29 +00002211 if (! PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002212 return NULL;
2213 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002214 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002215}
2216
2217
2218static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002219Pickler_dealloc(Picklerobject *self) {
2220 Py_XDECREF(self->write);
2221 Py_XDECREF(self->memo);
2222 Py_XDECREF(self->arg);
2223 Py_XDECREF(self->file);
2224 Py_XDECREF(self->pers_func);
2225 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002226 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002227
2228 if (self->write_buf) {
2229 free(self->write_buf);
2230 }
2231
Guido van Rossumb18618d2000-05-03 23:44:39 +00002232 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002233}
2234
2235
2236static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002237Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002238
2239 switch (*name) {
2240 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002241 if (strcmp(name, "persistent_id") == 0) {
2242 if (!self->pers_func) {
2243 PyErr_SetString(PyExc_AttributeError, name);
2244 return NULL;
2245 }
2246
2247 Py_INCREF(self->pers_func);
2248 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002249 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002250 break;
2251 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002252 if (strcmp(name, "memo") == 0) {
2253 if (!self->memo) {
2254 PyErr_SetString(PyExc_AttributeError, name);
2255 return NULL;
2256 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002257
Guido van Rossum60456fd1997-04-09 17:36:32 +00002258 Py_INCREF(self->memo);
2259 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002260 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002261 break;
2262 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002263 if (strcmp(name, "PicklingError") == 0) {
2264 Py_INCREF(PicklingError);
2265 return PicklingError;
2266 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002267 break;
2268 case 'b':
2269 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002270 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002271 break;
2272 case 'f':
2273 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002274 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002275 break;
2276 case 'g':
2277 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2278 PyErr_SetString(PyExc_AttributeError, name);
2279 return NULL;
2280 }
2281 break;
2282 }
2283 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002284}
2285
2286
2287int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002288Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002289
Guido van Rossum053b8df1998-11-25 16:18:00 +00002290 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002291 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002292 "attribute deletion is not supported");
2293 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002294 }
2295
Guido van Rossum60456fd1997-04-09 17:36:32 +00002296 if (strcmp(name, "persistent_id") == 0) {
2297 Py_XDECREF(self->pers_func);
2298 self->pers_func = value;
2299 Py_INCREF(value);
2300 return 0;
2301 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002302
Guido van Rossum60456fd1997-04-09 17:36:32 +00002303 if (strcmp(name, "inst_persistent_id") == 0) {
2304 Py_XDECREF(self->inst_pers_func);
2305 self->inst_pers_func = value;
2306 Py_INCREF(value);
2307 return 0;
2308 }
2309
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002310 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002311 if (! PyDict_Check(value)) {
2312 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2313 return -1;
2314 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002315 Py_XDECREF(self->memo);
2316 self->memo = value;
2317 Py_INCREF(value);
2318 return 0;
2319 }
2320
Guido van Rossum053b8df1998-11-25 16:18:00 +00002321 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002322 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002323 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002324 }
2325
Guido van Rossum053b8df1998-11-25 16:18:00 +00002326 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002327 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002328 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002329 }
2330
Guido van Rossum60456fd1997-04-09 17:36:32 +00002331 PyErr_SetString(PyExc_AttributeError, name);
2332 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002333}
2334
2335
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002336static char Picklertype__doc__[] =
2337"Objects that know how to pickle objects\n"
2338;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002339
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002340static PyTypeObject Picklertype = {
2341 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002342 0, /*ob_size*/
2343 "Pickler", /*tp_name*/
2344 sizeof(Picklerobject), /*tp_basicsize*/
2345 0, /*tp_itemsize*/
2346 /* methods */
2347 (destructor)Pickler_dealloc, /*tp_dealloc*/
2348 (printfunc)0, /*tp_print*/
2349 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2350 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2351 (cmpfunc)0, /*tp_compare*/
2352 (reprfunc)0, /*tp_repr*/
2353 0, /*tp_as_number*/
2354 0, /*tp_as_sequence*/
2355 0, /*tp_as_mapping*/
2356 (hashfunc)0, /*tp_hash*/
2357 (ternaryfunc)0, /*tp_call*/
2358 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002359
Guido van Rossum60456fd1997-04-09 17:36:32 +00002360 /* Space for future expansion */
2361 0L,0L,0L,0L,
2362 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002363};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002364
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002365static PyObject *
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002366find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002367 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002368
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002369 if (fc) {
2370 if (fc==Py_None) {
2371 PyErr_SetString(UnpicklingError,
2372 "Global and instance pickles are not supported.");
2373 return NULL;
2374 }
2375 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2376 }
2377
Jeremy Hyltond1055231998-08-11 19:52:51 +00002378 module = PySys_GetObject("modules");
2379 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002380 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002381
2382 module = PyDict_GetItem(module, py_module_name);
2383 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002384 module = PyImport_Import(py_module_name);
2385 if (!module)
2386 return NULL;
2387 global = PyObject_GetAttr(module, py_global_name);
2388 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002389 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002390 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002391 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002392 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002393 char buf[256 + 37];
2394 sprintf(buf, "Failed to import class %.128s from module %.128s",
2395 PyString_AS_STRING((PyStringObject*)py_global_name),
2396 PyString_AS_STRING((PyStringObject*)py_module_name));
2397 PyErr_SetString(PyExc_SystemError, buf);
2398 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002399 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002400 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002401}
2402
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002403static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002404marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002405 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002406 PyErr_SetString(UnpicklingError, "could not find MARK");
2407 return -1;
2408 }
2409
2410 return self->marks[--self->num_marks];
2411}
2412
2413
2414static int
2415load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002416 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002417 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002418}
2419
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002420static int
2421bad_readline() {
2422 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2423 return -1;
2424}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002425
2426static int
2427load_int(Unpicklerobject *self) {
2428 PyObject *py_int = 0;
2429 char *endptr, *s;
2430 int len, res = -1;
2431 long l;
2432
2433 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002434 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002435 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002436
2437 errno = 0;
2438 l = strtol(s, &endptr, 0);
2439
2440 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2441 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002442 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002443 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002444 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002445
Guido van Rossum053b8df1998-11-25 16:18:00 +00002446 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2447 PyErr_SetString(PyExc_ValueError,
2448 "could not convert string to int");
2449 goto finally;
2450 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002451 }
2452 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002453 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002454 }
2455
Guido van Rossum053b8df1998-11-25 16:18:00 +00002456 free(s);
2457 PDATA_PUSH(self->stack, py_int, -1);
2458 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002459
2460finally:
2461 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002462
2463 return res;
2464}
2465
2466
2467static long
2468calc_binint(char *s, int x) {
2469 unsigned char c;
2470 int i;
2471 long l;
2472
2473 for (i = 0, l = 0L; i < x; i++) {
2474 c = (unsigned char)s[i];
2475 l |= (long)c << (i * 8);
2476 }
2477
2478 return l;
2479}
2480
2481
2482static int
2483load_binintx(Unpicklerobject *self, char *s, int x) {
2484 PyObject *py_int = 0;
2485 long l;
2486
2487 l = calc_binint(s, x);
2488
Guido van Rossum053b8df1998-11-25 16:18:00 +00002489 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002490 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002491
Guido van Rossum053b8df1998-11-25 16:18:00 +00002492 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002493 return 0;
2494}
2495
2496
2497static int
2498load_binint(Unpicklerobject *self) {
2499 char *s;
2500
2501 if ((*self->read_func)(self, &s, 4) < 0)
2502 return -1;
2503
2504 return load_binintx(self, s, 4);
2505}
2506
2507
2508static int
2509load_binint1(Unpicklerobject *self) {
2510 char *s;
2511
2512 if ((*self->read_func)(self, &s, 1) < 0)
2513 return -1;
2514
2515 return load_binintx(self, s, 1);
2516}
2517
2518
2519static int
2520load_binint2(Unpicklerobject *self) {
2521 char *s;
2522
2523 if ((*self->read_func)(self, &s, 2) < 0)
2524 return -1;
2525
2526 return load_binintx(self, s, 2);
2527}
2528
2529static int
2530load_long(Unpicklerobject *self) {
2531 PyObject *l = 0;
2532 char *end, *s;
2533 int len, res = -1;
2534
Guido van Rossum60456fd1997-04-09 17:36:32 +00002535 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002536 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002537 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002538
Guido van Rossum053b8df1998-11-25 16:18:00 +00002539 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002540 goto finally;
2541
Guido van Rossum053b8df1998-11-25 16:18:00 +00002542 free(s);
2543 PDATA_PUSH(self->stack, l, -1);
2544 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002545
2546finally:
2547 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002548
2549 return res;
2550}
2551
2552
2553static int
2554load_float(Unpicklerobject *self) {
2555 PyObject *py_float = 0;
2556 char *endptr, *s;
2557 int len, res = -1;
2558 double d;
2559
2560 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002561 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002562 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002563
2564 errno = 0;
2565 d = strtod(s, &endptr);
2566
2567 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2568 PyErr_SetString(PyExc_ValueError,
2569 "could not convert string to float");
2570 goto finally;
2571 }
2572
Guido van Rossum053b8df1998-11-25 16:18:00 +00002573 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002574 goto finally;
2575
Guido van Rossum053b8df1998-11-25 16:18:00 +00002576 free(s);
2577 PDATA_PUSH(self->stack, py_float, -1);
2578 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002579
2580finally:
2581 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002582
2583 return res;
2584}
2585
Guido van Rossum60456fd1997-04-09 17:36:32 +00002586static int
2587load_binfloat(Unpicklerobject *self) {
2588 PyObject *py_float = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002589 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002590 long fhi, flo;
2591 double x;
2592 char *p;
2593
2594 if ((*self->read_func)(self, &p, 8) < 0)
2595 return -1;
2596
2597 /* First byte */
2598 s = (*p>>7) & 1;
2599 e = (*p & 0x7F) << 4;
2600 p++;
2601
2602 /* Second byte */
2603 e |= (*p>>4) & 0xF;
2604 fhi = (*p & 0xF) << 24;
2605 p++;
2606
2607 /* Third byte */
2608 fhi |= (*p & 0xFF) << 16;
2609 p++;
2610
2611 /* Fourth byte */
2612 fhi |= (*p & 0xFF) << 8;
2613 p++;
2614
2615 /* Fifth byte */
2616 fhi |= *p & 0xFF;
2617 p++;
2618
2619 /* Sixth byte */
2620 flo = (*p & 0xFF) << 16;
2621 p++;
2622
2623 /* Seventh byte */
2624 flo |= (*p & 0xFF) << 8;
2625 p++;
2626
2627 /* Eighth byte */
2628 flo |= *p & 0xFF;
2629
2630 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2631 x /= 268435456.0; /* 2**28 */
2632
2633 /* XXX This sadly ignores Inf/NaN */
2634 if (e == 0)
2635 e = -1022;
2636 else {
2637 x += 1.0;
2638 e -= 1023;
2639 }
2640 x = ldexp(x, e);
2641
2642 if (s)
2643 x = -x;
2644
Guido van Rossum053b8df1998-11-25 16:18:00 +00002645 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002646
Guido van Rossum053b8df1998-11-25 16:18:00 +00002647 PDATA_PUSH(self->stack, py_float, -1);
2648 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002649}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002650
2651static int
2652load_string(Unpicklerobject *self) {
2653 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002654 int len, res = -1, nslash;
2655 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002656
2657 static PyObject *eval_dict = 0;
2658
2659 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002660 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002661 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002662
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002663 /* Check for unquoted quotes (evil strings) */
2664 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002665 if (q != '"' && q != '\'') goto insecure;
2666 for (p=s+1, nslash=0; *p; p++) {
2667 if (*p==q && nslash%2==0) break;
2668 if (*p=='\\') nslash++;
2669 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002670 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002671 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002672 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002673 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002674 }
2675 else goto insecure;
2676 /********************************************/
2677
Guido van Rossum053b8df1998-11-25 16:18:00 +00002678 UNLESS (eval_dict)
2679 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002680 goto finally;
2681
Guido van Rossum053b8df1998-11-25 16:18:00 +00002682 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002683 goto finally;
2684
Guido van Rossum053b8df1998-11-25 16:18:00 +00002685 free(s);
2686 PDATA_PUSH(self->stack, str, -1);
2687 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002688
2689finally:
2690 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002691
2692 return res;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002693
2694insecure:
2695 free(s);
2696 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2697 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002698}
2699
2700
2701static int
2702load_binstring(Unpicklerobject *self) {
2703 PyObject *py_string = 0;
2704 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002705 char *s;
2706
Guido van Rossum053b8df1998-11-25 16:18:00 +00002707 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002708
2709 l = calc_binint(s, 4);
2710
2711 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002712 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002713
Guido van Rossum053b8df1998-11-25 16:18:00 +00002714 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2715 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002716
Guido van Rossum053b8df1998-11-25 16:18:00 +00002717 PDATA_PUSH(self->stack, py_string, -1);
2718 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002719}
2720
2721
2722static int
2723load_short_binstring(Unpicklerobject *self) {
2724 PyObject *py_string = 0;
2725 unsigned char l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002726 char *s;
2727
2728 if ((*self->read_func)(self, &s, 1) < 0)
2729 return -1;
2730
2731 l = (unsigned char)s[0];
2732
Guido van Rossum053b8df1998-11-25 16:18:00 +00002733 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002734
Guido van Rossum053b8df1998-11-25 16:18:00 +00002735 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002736
Guido van Rossum053b8df1998-11-25 16:18:00 +00002737 PDATA_PUSH(self->stack, py_string, -1);
2738 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002739}
2740
2741
2742static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002743load_unicode(Unpicklerobject *self) {
2744 PyObject *str = 0;
2745 int len, res = -1;
2746 char *s;
2747
2748 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2749 if (len < 2) return bad_readline();
2750
2751 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
2752 goto finally;
2753
2754 PDATA_PUSH(self->stack, str, -1);
2755 return 0;
2756
2757finally:
2758 return res;
2759}
2760
2761
2762static int
2763load_binunicode(Unpicklerobject *self) {
2764 PyObject *unicode;
2765 long l;
2766 char *s;
2767
2768 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2769
2770 l = calc_binint(s, 4);
2771
2772 if ((*self->read_func)(self, &s, l) < 0)
2773 return -1;
2774
2775 UNLESS (unicode = PyUnicode_DecodeUTF8(s, l, NULL))
2776 return -1;
2777
2778 PDATA_PUSH(self->stack, unicode, -1);
2779 return 0;
2780}
2781
2782
2783static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002784load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002785 PyObject *tup;
2786 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002787
Guido van Rossum053b8df1998-11-25 16:18:00 +00002788 if ((i = marker(self)) < 0) return -1;
2789 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2790 PDATA_PUSH(self->stack, tup, -1);
2791 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002792}
2793
2794static int
2795load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002796 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002797
Guido van Rossum053b8df1998-11-25 16:18:00 +00002798 UNLESS (tup=PyTuple_New(0)) return -1;
2799 PDATA_PUSH(self->stack, tup, -1);
2800 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002801}
2802
2803static int
2804load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002805 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002806
Guido van Rossum053b8df1998-11-25 16:18:00 +00002807 UNLESS (list=PyList_New(0)) return -1;
2808 PDATA_PUSH(self->stack, list, -1);
2809 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002810}
2811
2812static int
2813load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002814 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002815
Guido van Rossum053b8df1998-11-25 16:18:00 +00002816 UNLESS (dict=PyDict_New()) return -1;
2817 PDATA_PUSH(self->stack, dict, -1);
2818 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002819}
2820
2821
2822static int
2823load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002824 PyObject *list = 0;
2825 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002826
Guido van Rossum053b8df1998-11-25 16:18:00 +00002827 if ((i = marker(self)) < 0) return -1;
2828 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2829 PDATA_PUSH(self->stack, list, -1);
2830 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002831}
2832
2833static int
2834load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002835 PyObject *dict, *key, *value;
2836 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002837
Guido van Rossum053b8df1998-11-25 16:18:00 +00002838 if ((i = marker(self)) < 0) return -1;
2839 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002840
Guido van Rossum053b8df1998-11-25 16:18:00 +00002841 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002842
Guido van Rossum053b8df1998-11-25 16:18:00 +00002843 for (k = i+1; k < j; k += 2) {
2844 key =self->stack->data[k-1];
2845 value=self->stack->data[k ];
2846 if (PyDict_SetItem(dict, key, value) < 0) {
2847 Py_DECREF(dict);
2848 return -1;
2849 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002850 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002851 Pdata_clear(self->stack, i);
2852 PDATA_PUSH(self->stack, dict, -1);
2853 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002854}
2855
2856static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002857Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002858 int has_key;
2859 PyObject *safe=0, *r=0;
2860
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002861 if (PyClass_Check(cls)) {
2862 int l;
2863
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002864 if ((l=PyObject_Size(args)) < 0) goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002865 UNLESS (l) {
2866 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002867
Guido van Rossum053b8df1998-11-25 16:18:00 +00002868 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2869 /* We have a class with no __getinitargs__, so bypass usual
2870 construction */
2871 PyInstanceObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002872
Guido van Rossum053b8df1998-11-25 16:18:00 +00002873 PyErr_Clear();
Guido van Rossumb18618d2000-05-03 23:44:39 +00002874 UNLESS (inst=PyObject_New(PyInstanceObject, &PyInstance_Type))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002875 goto err;
2876 inst->in_class=(PyClassObject*)cls;
2877 Py_INCREF(cls);
2878 UNLESS (inst->in_dict=PyDict_New()) {
2879 Py_DECREF(inst);
2880 goto err;
2881 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002882 PyObject_GC_Init(inst);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002883 return (PyObject *)inst;
2884 }
2885 Py_DECREF(__getinitargs__);
2886 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002887
Guido van Rossum053b8df1998-11-25 16:18:00 +00002888 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002889 else goto err;
2890 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002891
2892
2893 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2894 goto err;
2895
2896 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002897 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002898 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002899 cPickle_ErrFormat(UnpicklingError,
2900 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002901 Py_XDECREF(safe);
2902 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002903 }
2904
Guido van Rossum053b8df1998-11-25 16:18:00 +00002905 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002906 /* Special case, call cls.__basicnew__() */
2907 PyObject *basicnew;
2908
Guido van Rossum053b8df1998-11-25 16:18:00 +00002909 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002910 r=PyObject_CallObject(basicnew, NULL);
2911 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002912 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002913 }
2914
Guido van Rossum053b8df1998-11-25 16:18:00 +00002915 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002916
Guido van Rossum60456fd1997-04-09 17:36:32 +00002917err:
2918 {
2919 PyObject *tp, *v, *tb;
2920
2921 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002922 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2923 Py_XDECREF(v);
2924 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002925 }
2926 PyErr_Restore(tp,v,tb);
2927 }
2928 return NULL;
2929}
2930
2931
2932static int
2933load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002934 PyObject *class, *tup, *obj=0;
2935 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002936
Guido van Rossum053b8df1998-11-25 16:18:00 +00002937 if ((i = marker(self)) < 0) return -1;
2938 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
2939 PDATA_POP(self->stack, class);
2940 if (class) {
2941 obj = Instance_New(class, tup);
2942 Py_DECREF(class);
2943 }
2944 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002945
Guido van Rossum053b8df1998-11-25 16:18:00 +00002946 if (! obj) return -1;
2947 PDATA_PUSH(self->stack, obj, -1);
2948 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002949}
2950
2951
2952static int
2953load_inst(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002954 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002955 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002956 char *s;
2957
Guido van Rossum053b8df1998-11-25 16:18:00 +00002958 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002959
Guido van Rossum053b8df1998-11-25 16:18:00 +00002960 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002961 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002962 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002963
Guido van Rossum053b8df1998-11-25 16:18:00 +00002964 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002965 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002966 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002967 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002968 Py_DECREF(class_name);
2969 }
2970 }
2971 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002972
Guido van Rossum053b8df1998-11-25 16:18:00 +00002973 if (! class) return -1;
2974
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002975 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002976 obj = Instance_New(class, tup);
2977 Py_DECREF(tup);
2978 }
2979 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002980
Guido van Rossum053b8df1998-11-25 16:18:00 +00002981 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002982
Guido van Rossum053b8df1998-11-25 16:18:00 +00002983 PDATA_PUSH(self->stack, obj, -1);
2984 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002985}
2986
2987
2988static int
2989load_global(Unpicklerobject *self) {
2990 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002991 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002992 char *s;
2993
Guido van Rossum053b8df1998-11-25 16:18:00 +00002994 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002995 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002996 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002997
Guido van Rossum053b8df1998-11-25 16:18:00 +00002998 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002999 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003000 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003001 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003002 Py_DECREF(class_name);
3003 }
3004 }
3005 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003006
Guido van Rossum053b8df1998-11-25 16:18:00 +00003007 if (! class) return -1;
3008 PDATA_PUSH(self->stack, class, -1);
3009 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003010}
3011
3012
3013static int
3014load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003015 PyObject *pid = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003016 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003017 char *s;
3018
3019 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003020 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003021 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003022
Guido van Rossum053b8df1998-11-25 16:18:00 +00003023 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003024
Guido van Rossum053b8df1998-11-25 16:18:00 +00003025 if (PyList_Check(self->pers_func)) {
3026 if (PyList_Append(self->pers_func, pid) < 0) {
3027 Py_DECREF(pid);
3028 return -1;
3029 }
3030 }
3031 else {
3032 ARG_TUP(self, pid);
3033 if (self->arg) {
3034 pid = PyObject_CallObject(self->pers_func, self->arg);
3035 FREE_ARG_TUP(self);
3036 }
3037 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003038
Guido van Rossum053b8df1998-11-25 16:18:00 +00003039 if (! pid) return -1;
3040
3041 PDATA_PUSH(self->stack, pid, -1);
3042 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003043 }
3044 else {
3045 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003046 "A load persistent id instruction was encountered,\n"
3047 "but no persistent_load function was specified.");
3048 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003049 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003050}
3051
Guido van Rossum60456fd1997-04-09 17:36:32 +00003052static int
3053load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003054 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003055
3056 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003057 PDATA_POP(self->stack, pid);
3058 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003059
Guido van Rossum053b8df1998-11-25 16:18:00 +00003060 if (PyList_Check(self->pers_func)) {
3061 if (PyList_Append(self->pers_func, pid) < 0) {
3062 Py_DECREF(pid);
3063 return -1;
3064 }
3065 }
3066 else {
3067 ARG_TUP(self, pid);
3068 if (self->arg) {
3069 pid = PyObject_CallObject(self->pers_func, self->arg);
3070 FREE_ARG_TUP(self);
3071 }
3072 if (! pid) return -1;
3073 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003074
Guido van Rossum053b8df1998-11-25 16:18:00 +00003075 PDATA_PUSH(self->stack, pid, -1);
3076 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003077 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003078 else {
3079 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003080 "A load persistent id instruction was encountered,\n"
3081 "but no persistent_load function was specified.");
3082 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003083 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003084}
3085
3086
3087static int
3088load_pop(Unpicklerobject *self) {
3089 int len;
3090
Guido van Rossum053b8df1998-11-25 16:18:00 +00003091 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003092
Guido van Rossumea2b7152000-05-09 18:14:50 +00003093 /* Note that we split the (pickle.py) stack into two stacks,
3094 an object stack and a mark stack. We have to be clever and
3095 pop the right one. We do this by looking at the top of the
3096 mark stack.
3097 */
3098
Guido van Rossum60456fd1997-04-09 17:36:32 +00003099 if ((self->num_marks > 0) &&
3100 (self->marks[self->num_marks - 1] == len))
3101 self->num_marks--;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003102 else {
3103 len--;
3104 Py_DECREF(self->stack->data[len]);
3105 self->stack->length=len;
3106 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003107
3108 return 0;
3109}
3110
3111
3112static int
3113load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003114 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003115
3116 if ((i = marker(self)) < 0)
3117 return -1;
3118
Guido van Rossum053b8df1998-11-25 16:18:00 +00003119 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003120
3121 return 0;
3122}
3123
3124
3125static int
3126load_dup(Unpicklerobject *self) {
3127 PyObject *last;
3128 int len;
3129
Guido van Rossum053b8df1998-11-25 16:18:00 +00003130 if ((len = self->stack->length) <= 0) return stackUnderflow();
3131 last=self->stack->data[len-1];
3132 Py_INCREF(last);
3133 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003134 return 0;
3135}
3136
3137
3138static int
3139load_get(Unpicklerobject *self) {
3140 PyObject *py_str = 0, *value = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003141 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003142 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003143 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003144
Guido van Rossum053b8df1998-11-25 16:18:00 +00003145 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003146 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003147
Guido van Rossum053b8df1998-11-25 16:18:00 +00003148 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3149
3150 value = PyDict_GetItem(self->memo, py_str);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003151 if (! value) {
3152 PyErr_SetObject(BadPickleGet, py_str);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003153 rc = -1;
3154 } else {
3155 PDATA_APPEND(self->stack, value, -1);
3156 rc = 0;
3157 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003158
Guido van Rossum2f80d961999-07-13 15:18:58 +00003159 Py_DECREF(py_str);
3160 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003161}
3162
3163
3164static int
3165load_binget(Unpicklerobject *self) {
3166 PyObject *py_key = 0, *value = 0;
3167 unsigned char key;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003168 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003169 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003170
Guido van Rossum053b8df1998-11-25 16:18:00 +00003171 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003172
3173 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003174 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3175
3176 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003177 if (! value) {
3178 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003179 rc = -1;
3180 } else {
3181 PDATA_APPEND(self->stack, value, -1);
3182 rc = 0;
3183 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003184
Guido van Rossum2f80d961999-07-13 15:18:58 +00003185 Py_DECREF(py_key);
3186 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003187}
3188
3189
3190static int
3191load_long_binget(Unpicklerobject *self) {
3192 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003193 unsigned char c;
3194 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003195 long key;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003196 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003197
Guido van Rossum053b8df1998-11-25 16:18:00 +00003198 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003199
3200 c = (unsigned char)s[0];
3201 key = (long)c;
3202 c = (unsigned char)s[1];
3203 key |= (long)c << 8;
3204 c = (unsigned char)s[2];
3205 key |= (long)c << 16;
3206 c = (unsigned char)s[3];
3207 key |= (long)c << 24;
3208
Guido van Rossum053b8df1998-11-25 16:18:00 +00003209 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3210
3211 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003212 if (! value) {
3213 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003214 rc = -1;
3215 } else {
3216 PDATA_APPEND(self->stack, value, -1);
3217 rc = 0;
3218 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003219
Guido van Rossum2f80d961999-07-13 15:18:58 +00003220 Py_DECREF(py_key);
3221 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003222}
3223
3224
3225static int
3226load_put(Unpicklerobject *self) {
3227 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003228 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003229 char *s;
3230
Guido van Rossum053b8df1998-11-25 16:18:00 +00003231 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003232 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003233 UNLESS (len=self->stack->length) return stackUnderflow();
3234 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3235 value=self->stack->data[len-1];
3236 l=PyDict_SetItem(self->memo, py_str, value);
3237 Py_DECREF(py_str);
3238 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003239}
3240
3241
3242static int
3243load_binput(Unpicklerobject *self) {
3244 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003245 unsigned char key;
3246 char *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003247 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003248
Guido van Rossum053b8df1998-11-25 16:18:00 +00003249 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3250 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003251
3252 key = (unsigned char)s[0];
3253
Guido van Rossum053b8df1998-11-25 16:18:00 +00003254 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3255 value=self->stack->data[len-1];
3256 len=PyDict_SetItem(self->memo, py_key, value);
3257 Py_DECREF(py_key);
3258 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003259}
3260
3261
3262static int
3263load_long_binput(Unpicklerobject *self) {
3264 PyObject *py_key = 0, *value = 0;
3265 long key;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003266 unsigned char c;
3267 char *s;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003268 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003269
Guido van Rossum053b8df1998-11-25 16:18:00 +00003270 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3271 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003272
3273 c = (unsigned char)s[0];
3274 key = (long)c;
3275 c = (unsigned char)s[1];
3276 key |= (long)c << 8;
3277 c = (unsigned char)s[2];
3278 key |= (long)c << 16;
3279 c = (unsigned char)s[3];
3280 key |= (long)c << 24;
3281
Guido van Rossum053b8df1998-11-25 16:18:00 +00003282 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3283 value=self->stack->data[len-1];
3284 len=PyDict_SetItem(self->memo, py_key, value);
3285 Py_DECREF(py_key);
3286 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003287}
3288
3289
3290static int
3291do_append(Unpicklerobject *self, int x) {
3292 PyObject *value = 0, *list = 0, *append_method = 0;
3293 int len, i;
3294
Guido van Rossum053b8df1998-11-25 16:18:00 +00003295 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3296 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003297
Guido van Rossum053b8df1998-11-25 16:18:00 +00003298 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003299
3300 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003301 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003302 int list_len;
3303
Guido van Rossum053b8df1998-11-25 16:18:00 +00003304 slice=Pdata_popList(self->stack, x);
3305 list_len = PyList_GET_SIZE(list);
3306 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003307 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003308 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003309 }
3310 else {
3311
Guido van Rossum053b8df1998-11-25 16:18:00 +00003312 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003313 return -1;
3314
3315 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003316 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003317
Guido van Rossum053b8df1998-11-25 16:18:00 +00003318 value=self->stack->data[i];
3319 junk=0;
3320 ARG_TUP(self, value);
3321 if (self->arg) {
3322 junk = PyObject_CallObject(append_method, self->arg);
3323 FREE_ARG_TUP(self);
3324 }
3325 if (! junk) {
3326 Pdata_clear(self->stack, i+1);
3327 self->stack->length=x;
3328 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003329 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003330 }
3331 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003332 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003333 self->stack->length=x;
3334 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003335 }
3336
Guido van Rossum60456fd1997-04-09 17:36:32 +00003337 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003338}
3339
3340
3341static int
3342load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003343 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003344}
3345
3346
3347static int
3348load_appends(Unpicklerobject *self) {
3349 return do_append(self, marker(self));
3350}
3351
3352
3353static int
3354do_setitems(Unpicklerobject *self, int x) {
3355 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003356 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003357
Guido van Rossum053b8df1998-11-25 16:18:00 +00003358 UNLESS ((len=self->stack->length) >= x
3359 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003360
Guido van Rossum053b8df1998-11-25 16:18:00 +00003361 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003362
Guido van Rossum053b8df1998-11-25 16:18:00 +00003363 for (i = x+1; i < len; i += 2) {
3364 key =self->stack->data[i-1];
3365 value=self->stack->data[i ];
3366 if (PyObject_SetItem(dict, key, value) < 0) {
3367 r=-1;
3368 break;
3369 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003370 }
3371
Guido van Rossum053b8df1998-11-25 16:18:00 +00003372 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003373
Guido van Rossum053b8df1998-11-25 16:18:00 +00003374 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003375}
3376
3377
3378static int
3379load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003380 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003381}
3382
Guido van Rossum60456fd1997-04-09 17:36:32 +00003383static int
3384load_setitems(Unpicklerobject *self) {
3385 return do_setitems(self, marker(self));
3386}
3387
3388
3389static int
3390load_build(Unpicklerobject *self) {
3391 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3392 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003393 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003394
Guido van Rossum053b8df1998-11-25 16:18:00 +00003395 if (self->stack->length < 2) return stackUnderflow();
3396 PDATA_POP(self->stack, value);
3397 if (! value) return -1;
3398 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003399
Guido van Rossum053b8df1998-11-25 16:18:00 +00003400 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3401 ARG_TUP(self, value);
3402 if (self->arg) {
3403 junk = PyObject_CallObject(__setstate__, self->arg);
3404 FREE_ARG_TUP(self);
3405 }
3406 Py_DECREF(__setstate__);
3407 if (! junk) return -1;
3408 Py_DECREF(junk);
3409 return 0;
3410 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003411
Guido van Rossum053b8df1998-11-25 16:18:00 +00003412 PyErr_Clear();
3413 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003414 i = 0;
3415 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003416 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3417 r=-1;
3418 break;
3419 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003420 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003421 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003422 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003423 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003424
Guido van Rossum053b8df1998-11-25 16:18:00 +00003425 Py_XDECREF(value);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003426
Guido van Rossum053b8df1998-11-25 16:18:00 +00003427 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003428}
3429
3430
3431static int
3432load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003433 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003434
Guido van Rossumea2b7152000-05-09 18:14:50 +00003435 /* Note that we split the (pickle.py) stack into two stacks, an
3436 object stack and a mark stack. Here we push a mark onto the
3437 mark stack.
3438 */
3439
Guido van Rossum053b8df1998-11-25 16:18:00 +00003440 if ((self->num_marks + 1) >= self->marks_size) {
3441 s=self->marks_size+20;
3442 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003443 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003444 self->marks=(int *)malloc(s * sizeof(int));
3445 else
3446 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003447 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003448 PyErr_NoMemory();
3449 return -1;
3450 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003451 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003452 }
3453
Guido van Rossum053b8df1998-11-25 16:18:00 +00003454 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003455
3456 return 0;
3457}
3458
3459static int
3460load_reduce(Unpicklerobject *self) {
3461 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003462
Guido van Rossum053b8df1998-11-25 16:18:00 +00003463 PDATA_POP(self->stack, arg_tup);
3464 if (! arg_tup) return -1;
3465 PDATA_POP(self->stack, callable);
3466 if (callable) {
3467 ob = Instance_New(callable, arg_tup);
3468 Py_DECREF(callable);
3469 }
3470 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003471
Guido van Rossum053b8df1998-11-25 16:18:00 +00003472 if (! ob) return -1;
3473
3474 PDATA_PUSH(self->stack, ob, -1);
3475 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003476}
3477
3478static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003479load(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003480 PyObject *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003481 char *s;
3482
Guido van Rossum60456fd1997-04-09 17:36:32 +00003483 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003484 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003485
3486 while (1) {
3487 if ((*self->read_func)(self, &s, 1) < 0)
3488 break;
3489
3490 switch (s[0]) {
3491 case NONE:
3492 if (load_none(self) < 0)
3493 break;
3494 continue;
3495
3496 case BININT:
3497 if (load_binint(self) < 0)
3498 break;
3499 continue;
3500
3501 case BININT1:
3502 if (load_binint1(self) < 0)
3503 break;
3504 continue;
3505
3506 case BININT2:
3507 if (load_binint2(self) < 0)
3508 break;
3509 continue;
3510
3511 case INT:
3512 if (load_int(self) < 0)
3513 break;
3514 continue;
3515
3516 case LONG:
3517 if (load_long(self) < 0)
3518 break;
3519 continue;
3520
3521 case FLOAT:
3522 if (load_float(self) < 0)
3523 break;
3524 continue;
3525
Guido van Rossum60456fd1997-04-09 17:36:32 +00003526 case BINFLOAT:
3527 if (load_binfloat(self) < 0)
3528 break;
3529 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003530
3531 case BINSTRING:
3532 if (load_binstring(self) < 0)
3533 break;
3534 continue;
3535
3536 case SHORT_BINSTRING:
3537 if (load_short_binstring(self) < 0)
3538 break;
3539 continue;
3540
3541 case STRING:
3542 if (load_string(self) < 0)
3543 break;
3544 continue;
3545
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003546 case UNICODE:
3547 if (load_unicode(self) < 0)
3548 break;
3549 continue;
3550
3551 case BINUNICODE:
3552 if (load_binunicode(self) < 0)
3553 break;
3554 continue;
3555
Guido van Rossum60456fd1997-04-09 17:36:32 +00003556 case EMPTY_TUPLE:
3557 if (load_empty_tuple(self) < 0)
3558 break;
3559 continue;
3560
3561 case TUPLE:
3562 if (load_tuple(self) < 0)
3563 break;
3564 continue;
3565
3566 case EMPTY_LIST:
3567 if (load_empty_list(self) < 0)
3568 break;
3569 continue;
3570
3571 case LIST:
3572 if (load_list(self) < 0)
3573 break;
3574 continue;
3575
3576 case EMPTY_DICT:
3577 if (load_empty_dict(self) < 0)
3578 break;
3579 continue;
3580
3581 case DICT:
3582 if (load_dict(self) < 0)
3583 break;
3584 continue;
3585
3586 case OBJ:
3587 if (load_obj(self) < 0)
3588 break;
3589 continue;
3590
3591 case INST:
3592 if (load_inst(self) < 0)
3593 break;
3594 continue;
3595
3596 case GLOBAL:
3597 if (load_global(self) < 0)
3598 break;
3599 continue;
3600
3601 case APPEND:
3602 if (load_append(self) < 0)
3603 break;
3604 continue;
3605
3606 case APPENDS:
3607 if (load_appends(self) < 0)
3608 break;
3609 continue;
3610
3611 case BUILD:
3612 if (load_build(self) < 0)
3613 break;
3614 continue;
3615
3616 case DUP:
3617 if (load_dup(self) < 0)
3618 break;
3619 continue;
3620
3621 case BINGET:
3622 if (load_binget(self) < 0)
3623 break;
3624 continue;
3625
3626 case LONG_BINGET:
3627 if (load_long_binget(self) < 0)
3628 break;
3629 continue;
3630
3631 case GET:
3632 if (load_get(self) < 0)
3633 break;
3634 continue;
3635
3636 case MARK:
3637 if (load_mark(self) < 0)
3638 break;
3639 continue;
3640
3641 case BINPUT:
3642 if (load_binput(self) < 0)
3643 break;
3644 continue;
3645
3646 case LONG_BINPUT:
3647 if (load_long_binput(self) < 0)
3648 break;
3649 continue;
3650
3651 case PUT:
3652 if (load_put(self) < 0)
3653 break;
3654 continue;
3655
3656 case POP:
3657 if (load_pop(self) < 0)
3658 break;
3659 continue;
3660
3661 case POP_MARK:
3662 if (load_pop_mark(self) < 0)
3663 break;
3664 continue;
3665
3666 case SETITEM:
3667 if (load_setitem(self) < 0)
3668 break;
3669 continue;
3670
3671 case SETITEMS:
3672 if (load_setitems(self) < 0)
3673 break;
3674 continue;
3675
3676 case STOP:
3677 break;
3678
3679 case PERSID:
3680 if (load_persid(self) < 0)
3681 break;
3682 continue;
3683
3684 case BINPERSID:
3685 if (load_binpersid(self) < 0)
3686 break;
3687 continue;
3688
3689 case REDUCE:
3690 if (load_reduce(self) < 0)
3691 break;
3692 continue;
3693
3694 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003695 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003696 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003697 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003698 }
3699
3700 break;
3701 }
3702
Guido van Rossum053b8df1998-11-25 16:18:00 +00003703 if ((err = PyErr_Occurred())) {
3704 if (err == PyExc_EOFError) {
3705 PyErr_SetNone(PyExc_EOFError);
3706 }
3707 return NULL;
3708 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003709
Guido van Rossum053b8df1998-11-25 16:18:00 +00003710 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003711 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003712}
3713
3714
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003715/* No-load functions to support noload, which is used to
3716 find persistent references. */
3717
3718static int
3719noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003720 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003721
3722 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003723 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003724}
3725
3726
3727static int
3728noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003729 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003730 char *s;
3731
3732 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003733 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003734 if ((*self->readline_func)(self, &s) < 0) return -1;
3735 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003736 PDATA_APPEND(self->stack, Py_None,-1);
3737 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003738}
3739
3740static int
3741noload_global(Unpicklerobject *self) {
3742 char *s;
3743
3744 if ((*self->readline_func)(self, &s) < 0) return -1;
3745 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003746 PDATA_APPEND(self->stack, Py_None,-1);
3747 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003748}
3749
3750static int
3751noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003752
Guido van Rossum053b8df1998-11-25 16:18:00 +00003753 if (self->stack->length < 2) return stackUnderflow();
3754 Pdata_clear(self->stack, self->stack->length-2);
3755 PDATA_APPEND(self->stack, Py_None,-1);
3756 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003757}
3758
3759static int
3760noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003761
Guido van Rossum053b8df1998-11-25 16:18:00 +00003762 if (self->stack->length < 1) return stackUnderflow();
3763 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003764 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003765}
3766
3767
3768static PyObject *
3769noload(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003770 PyObject *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003771 char *s;
3772
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003773 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003774 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003775
3776 while (1) {
3777 if ((*self->read_func)(self, &s, 1) < 0)
3778 break;
3779
3780 switch (s[0]) {
3781 case NONE:
3782 if (load_none(self) < 0)
3783 break;
3784 continue;
3785
3786 case BININT:
3787 if (load_binint(self) < 0)
3788 break;
3789 continue;
3790
3791 case BININT1:
3792 if (load_binint1(self) < 0)
3793 break;
3794 continue;
3795
3796 case BININT2:
3797 if (load_binint2(self) < 0)
3798 break;
3799 continue;
3800
3801 case INT:
3802 if (load_int(self) < 0)
3803 break;
3804 continue;
3805
3806 case LONG:
3807 if (load_long(self) < 0)
3808 break;
3809 continue;
3810
3811 case FLOAT:
3812 if (load_float(self) < 0)
3813 break;
3814 continue;
3815
3816 case BINFLOAT:
3817 if (load_binfloat(self) < 0)
3818 break;
3819 continue;
3820
3821 case BINSTRING:
3822 if (load_binstring(self) < 0)
3823 break;
3824 continue;
3825
3826 case SHORT_BINSTRING:
3827 if (load_short_binstring(self) < 0)
3828 break;
3829 continue;
3830
3831 case STRING:
3832 if (load_string(self) < 0)
3833 break;
3834 continue;
3835
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003836 case UNICODE:
3837 if (load_unicode(self) < 0)
3838 break;
3839 continue;
3840
3841 case BINUNICODE:
3842 if (load_binunicode(self) < 0)
3843 break;
3844 continue;
3845
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003846 case EMPTY_TUPLE:
3847 if (load_empty_tuple(self) < 0)
3848 break;
3849 continue;
3850
3851 case TUPLE:
3852 if (load_tuple(self) < 0)
3853 break;
3854 continue;
3855
3856 case EMPTY_LIST:
3857 if (load_empty_list(self) < 0)
3858 break;
3859 continue;
3860
3861 case LIST:
3862 if (load_list(self) < 0)
3863 break;
3864 continue;
3865
3866 case EMPTY_DICT:
3867 if (load_empty_dict(self) < 0)
3868 break;
3869 continue;
3870
3871 case DICT:
3872 if (load_dict(self) < 0)
3873 break;
3874 continue;
3875
3876 case OBJ:
3877 if (noload_obj(self) < 0)
3878 break;
3879 continue;
3880
3881 case INST:
3882 if (noload_inst(self) < 0)
3883 break;
3884 continue;
3885
3886 case GLOBAL:
3887 if (noload_global(self) < 0)
3888 break;
3889 continue;
3890
3891 case APPEND:
3892 if (load_append(self) < 0)
3893 break;
3894 continue;
3895
3896 case APPENDS:
3897 if (load_appends(self) < 0)
3898 break;
3899 continue;
3900
3901 case BUILD:
3902 if (noload_build(self) < 0)
3903 break;
3904 continue;
3905
3906 case DUP:
3907 if (load_dup(self) < 0)
3908 break;
3909 continue;
3910
3911 case BINGET:
3912 if (load_binget(self) < 0)
3913 break;
3914 continue;
3915
3916 case LONG_BINGET:
3917 if (load_long_binget(self) < 0)
3918 break;
3919 continue;
3920
3921 case GET:
3922 if (load_get(self) < 0)
3923 break;
3924 continue;
3925
3926 case MARK:
3927 if (load_mark(self) < 0)
3928 break;
3929 continue;
3930
3931 case BINPUT:
3932 if (load_binput(self) < 0)
3933 break;
3934 continue;
3935
3936 case LONG_BINPUT:
3937 if (load_long_binput(self) < 0)
3938 break;
3939 continue;
3940
3941 case PUT:
3942 if (load_put(self) < 0)
3943 break;
3944 continue;
3945
3946 case POP:
3947 if (load_pop(self) < 0)
3948 break;
3949 continue;
3950
3951 case POP_MARK:
3952 if (load_pop_mark(self) < 0)
3953 break;
3954 continue;
3955
3956 case SETITEM:
3957 if (load_setitem(self) < 0)
3958 break;
3959 continue;
3960
3961 case SETITEMS:
3962 if (load_setitems(self) < 0)
3963 break;
3964 continue;
3965
3966 case STOP:
3967 break;
3968
3969 case PERSID:
3970 if (load_persid(self) < 0)
3971 break;
3972 continue;
3973
3974 case BINPERSID:
3975 if (load_binpersid(self) < 0)
3976 break;
3977 continue;
3978
3979 case REDUCE:
3980 if (noload_reduce(self) < 0)
3981 break;
3982 continue;
3983
3984 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003985 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003986 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003987 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003988 }
3989
3990 break;
3991 }
3992
Guido van Rossum053b8df1998-11-25 16:18:00 +00003993 if ((err = PyErr_Occurred())) {
3994 if (err == PyExc_EOFError) {
3995 PyErr_SetNone(PyExc_EOFError);
3996 }
3997 return NULL;
3998 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003999
Guido van Rossum053b8df1998-11-25 16:18:00 +00004000 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004001 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004002}
4003
4004
Guido van Rossum60456fd1997-04-09 17:36:32 +00004005static PyObject *
4006Unpickler_load(Unpicklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00004007 UNLESS (PyArg_ParseTuple(args, ":load"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004008 return NULL;
4009
4010 return load(self);
4011}
4012
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004013static PyObject *
4014Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00004015 UNLESS (PyArg_ParseTuple(args, ":noload"))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004016 return NULL;
4017
4018 return noload(self);
4019}
4020
Guido van Rossum60456fd1997-04-09 17:36:32 +00004021
4022static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004023 {"load", (PyCFunction)Unpickler_load, 1,
4024 "load() -- Load a pickle"
4025 },
4026 {"noload", (PyCFunction)Unpickler_noload, 1,
4027 "noload() -- not load a pickle, but go through most of the motions\n"
4028 "\n"
4029 "This function can be used to read past a pickle without instantiating\n"
4030 "any objects or importing any modules. It can also be used to find all\n"
4031 "persistent references without instantiating any objects or importing\n"
4032 "any modules.\n"
4033 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004034 {NULL, NULL} /* sentinel */
4035};
4036
4037
4038static Unpicklerobject *
4039newUnpicklerobject(PyObject *f) {
4040 Unpicklerobject *self;
4041
Guido van Rossumb18618d2000-05-03 23:44:39 +00004042 UNLESS (self = PyObject_New(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004043 return NULL;
4044
4045 self->file = NULL;
4046 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004047 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004048 self->pers_func = NULL;
4049 self->last_string = NULL;
4050 self->marks = NULL;
4051 self->num_marks = 0;
4052 self->marks_size = 0;
4053 self->buf_size = 0;
4054 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00004055 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00004056 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004057 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004058
Guido van Rossum83addc72000-04-21 20:49:36 +00004059 UNLESS (self->memo = PyDict_New())
4060 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004061
4062 Py_INCREF(f);
4063 self->file = f;
4064
4065 /* Set read, readline based on type of f */
4066 if (PyFile_Check(f)) {
4067 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004068 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00004069 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
4070 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004071 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004072 self->read_func = read_file;
4073 self->readline_func = readline_file;
4074 }
4075 else if (PycStringIO_InputCheck(f)) {
4076 self->fp = NULL;
4077 self->read_func = read_cStringIO;
4078 self->readline_func = readline_cStringIO;
4079 }
4080 else {
4081
4082 self->fp = NULL;
4083 self->read_func = read_other;
4084 self->readline_func = readline_other;
4085
Guido van Rossum053b8df1998-11-25 16:18:00 +00004086 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004087 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004088 PyErr_Clear();
4089 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
4090 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00004091 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004092 }
4093 }
4094
Guido van Rossum053b8df1998-11-25 16:18:00 +00004095 if (PyEval_GetRestricted()) {
4096 /* Restricted execution, get private tables */
4097 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004098
Guido van Rossum053b8df1998-11-25 16:18:00 +00004099 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
4100 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
4101 Py_DECREF(m);
4102 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004103 }
4104 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004105 self->safe_constructors=safe_constructors;
4106 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004107 }
4108
Guido van Rossum60456fd1997-04-09 17:36:32 +00004109 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004110
4111err:
4112 Py_DECREF((PyObject *)self);
4113 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004114}
4115
4116
4117static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004118get_Unpickler(PyObject *self, PyObject *args) {
4119 PyObject *file;
4120
Guido van Rossum43713e52000-02-29 13:59:29 +00004121 UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004122 return NULL;
4123 return (PyObject *)newUnpicklerobject(file);
4124}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004125
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004126
Guido van Rossum60456fd1997-04-09 17:36:32 +00004127static void
4128Unpickler_dealloc(Unpicklerobject *self) {
4129 Py_XDECREF(self->readline);
4130 Py_XDECREF(self->read);
4131 Py_XDECREF(self->file);
4132 Py_XDECREF(self->memo);
4133 Py_XDECREF(self->stack);
4134 Py_XDECREF(self->pers_func);
4135 Py_XDECREF(self->arg);
4136 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004137 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004138
Guido van Rossum60456fd1997-04-09 17:36:32 +00004139 if (self->marks) {
4140 free(self->marks);
4141 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004142
Guido van Rossum60456fd1997-04-09 17:36:32 +00004143 if (self->buf_size) {
4144 free(self->buf);
4145 }
4146
Guido van Rossumb18618d2000-05-03 23:44:39 +00004147 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004148}
4149
4150
4151static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004152Unpickler_getattr(Unpicklerobject *self, char *name) {
4153 if (!strcmp(name, "persistent_load")) {
4154 if (!self->pers_func) {
4155 PyErr_SetString(PyExc_AttributeError, name);
4156 return NULL;
4157 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004158
Guido van Rossum60456fd1997-04-09 17:36:32 +00004159 Py_INCREF(self->pers_func);
4160 return self->pers_func;
4161 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004162
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004163 if (!strcmp(name, "find_global")) {
4164 if (!self->find_class) {
4165 PyErr_SetString(PyExc_AttributeError, name);
4166 return NULL;
4167 }
4168
4169 Py_INCREF(self->find_class);
4170 return self->find_class;
4171 }
4172
Guido van Rossum60456fd1997-04-09 17:36:32 +00004173 if (!strcmp(name, "memo")) {
4174 if (!self->memo) {
4175 PyErr_SetString(PyExc_AttributeError, name);
4176 return NULL;
4177 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004178
Guido van Rossum60456fd1997-04-09 17:36:32 +00004179 Py_INCREF(self->memo);
4180 return self->memo;
4181 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004182
Guido van Rossum60456fd1997-04-09 17:36:32 +00004183 if (!strcmp(name, "UnpicklingError")) {
4184 Py_INCREF(UnpicklingError);
4185 return UnpicklingError;
4186 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004187
Guido van Rossum60456fd1997-04-09 17:36:32 +00004188 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4189}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004190
Guido van Rossum60456fd1997-04-09 17:36:32 +00004191
4192static int
4193Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004194
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004195 if (!strcmp(name, "persistent_load")) {
4196 Py_XDECREF(self->pers_func);
4197 self->pers_func = value;
4198 Py_XINCREF(value);
4199 return 0;
4200 }
4201
4202 if (!strcmp(name, "find_global")) {
4203 Py_XDECREF(self->find_class);
4204 self->find_class = value;
4205 Py_XINCREF(value);
4206 return 0;
4207 }
4208
Guido van Rossum053b8df1998-11-25 16:18:00 +00004209 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004210 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004211 "attribute deletion is not supported");
4212 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004213 }
4214
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004215 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004216 if (! PyDict_Check(value)) {
4217 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4218 return -1;
4219 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004220 Py_XDECREF(self->memo);
4221 self->memo = value;
4222 Py_INCREF(value);
4223 return 0;
4224 }
4225
Guido van Rossum60456fd1997-04-09 17:36:32 +00004226 PyErr_SetString(PyExc_AttributeError, name);
4227 return -1;
4228}
4229
4230
4231static PyObject *
4232cpm_dump(PyObject *self, PyObject *args) {
4233 PyObject *ob, *file, *res = NULL;
4234 Picklerobject *pickler = 0;
4235 int bin = 0;
4236
Guido van Rossum053b8df1998-11-25 16:18:00 +00004237 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004238 goto finally;
4239
Guido van Rossum053b8df1998-11-25 16:18:00 +00004240 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004241 goto finally;
4242
4243 if (dump(pickler, ob) < 0)
4244 goto finally;
4245
4246 Py_INCREF(Py_None);
4247 res = Py_None;
4248
4249finally:
4250 Py_XDECREF(pickler);
4251
4252 return res;
4253}
4254
4255
4256static PyObject *
4257cpm_dumps(PyObject *self, PyObject *args) {
4258 PyObject *ob, *file = 0, *res = NULL;
4259 Picklerobject *pickler = 0;
4260 int bin = 0;
4261
Guido van Rossum43713e52000-02-29 13:59:29 +00004262 UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004263 goto finally;
4264
Guido van Rossum053b8df1998-11-25 16:18:00 +00004265 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004266 goto finally;
4267
Guido van Rossum053b8df1998-11-25 16:18:00 +00004268 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004269 goto finally;
4270
4271 if (dump(pickler, ob) < 0)
4272 goto finally;
4273
4274 res = PycStringIO->cgetvalue(file);
4275
4276finally:
4277 Py_XDECREF(pickler);
4278 Py_XDECREF(file);
4279
4280 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004281}
4282
4283
4284static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004285cpm_load(PyObject *self, PyObject *args) {
4286 Unpicklerobject *unpickler = 0;
4287 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004288
Guido van Rossum43713e52000-02-29 13:59:29 +00004289 UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004290 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004291
Guido van Rossum053b8df1998-11-25 16:18:00 +00004292 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004293 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004294
Guido van Rossum60456fd1997-04-09 17:36:32 +00004295 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004296
Guido van Rossum60456fd1997-04-09 17:36:32 +00004297finally:
4298 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004299
Guido van Rossum60456fd1997-04-09 17:36:32 +00004300 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004301}
4302
4303
4304static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004305cpm_loads(PyObject *self, PyObject *args) {
4306 PyObject *ob, *file = 0, *res = NULL;
4307 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004308
Guido van Rossum43713e52000-02-29 13:59:29 +00004309 UNLESS (PyArg_ParseTuple(args, "S:loads", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004310 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004311
Guido van Rossum053b8df1998-11-25 16:18:00 +00004312 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004313 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004314
Guido van Rossum053b8df1998-11-25 16:18:00 +00004315 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004316 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004317
Guido van Rossum60456fd1997-04-09 17:36:32 +00004318 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004319
Guido van Rossum60456fd1997-04-09 17:36:32 +00004320finally:
4321 Py_XDECREF(file);
4322 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004323
Guido van Rossum60456fd1997-04-09 17:36:32 +00004324 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004325}
4326
4327
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004328static char Unpicklertype__doc__[] =
4329"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004330
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004331static PyTypeObject Unpicklertype = {
4332 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004333 0, /*ob_size*/
4334 "Unpickler", /*tp_name*/
4335 sizeof(Unpicklerobject), /*tp_basicsize*/
4336 0, /*tp_itemsize*/
4337 /* methods */
4338 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4339 (printfunc)0, /*tp_print*/
4340 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4341 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4342 (cmpfunc)0, /*tp_compare*/
4343 (reprfunc)0, /*tp_repr*/
4344 0, /*tp_as_number*/
4345 0, /*tp_as_sequence*/
4346 0, /*tp_as_mapping*/
4347 (hashfunc)0, /*tp_hash*/
4348 (ternaryfunc)0, /*tp_call*/
4349 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004350
Guido van Rossum60456fd1997-04-09 17:36:32 +00004351 /* Space for future expansion */
4352 0L,0L,0L,0L,
4353 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004354};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004355
Guido van Rossum60456fd1997-04-09 17:36:32 +00004356static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004357 {"dump", (PyCFunction)cpm_dump, 1,
4358 "dump(object, file, [binary]) --"
4359 "Write an object in pickle format to the given file\n"
4360 "\n"
4361 "If the optional argument, binary, is provided and is true, then the\n"
4362 "pickle will be written in binary format, which is more space and\n"
4363 "computationally efficient. \n"
4364 },
4365 {"dumps", (PyCFunction)cpm_dumps, 1,
4366 "dumps(object, [binary]) --"
4367 "Return a string containing an object in pickle format\n"
4368 "\n"
4369 "If the optional argument, binary, is provided and is true, then the\n"
4370 "pickle will be written in binary format, which is more space and\n"
4371 "computationally efficient. \n"
4372 },
4373 {"load", (PyCFunction)cpm_load, 1,
4374 "load(file) -- Load a pickle from the given file"},
4375 {"loads", (PyCFunction)cpm_loads, 1,
4376 "loads(string) -- Load a pickle from the given string"},
4377 {"Pickler", (PyCFunction)get_Pickler, 1,
4378 "Pickler(file, [binary]) -- Create a pickler\n"
4379 "\n"
4380 "If the optional argument, binary, is provided and is true, then\n"
4381 "pickles will be written in binary format, which is more space and\n"
4382 "computationally efficient. \n"
4383 },
4384 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4385 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004386 { NULL, NULL }
4387};
4388
Guido van Rossum60456fd1997-04-09 17:36:32 +00004389static int
4390init_stuff(PyObject *module, PyObject *module_dict) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004391 PyObject *string, *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004392
4393#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4394
4395 INIT_STR(__class__);
4396 INIT_STR(__getinitargs__);
4397 INIT_STR(__dict__);
4398 INIT_STR(__getstate__);
4399 INIT_STR(__setstate__);
4400 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004401 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004402 INIT_STR(__reduce__);
4403 INIT_STR(write);
4404 INIT_STR(__safe_for_unpickling__);
4405 INIT_STR(append);
4406 INIT_STR(read);
4407 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004408 INIT_STR(copy_reg);
4409 INIT_STR(dispatch_table);
4410 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004411 INIT_STR(__basicnew__);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004412 UNLESS (empty_str=PyString_FromString("")) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004413
Guido van Rossum053b8df1998-11-25 16:18:00 +00004414 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004415 return -1;
4416
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004417 /* These next few are special because we want to use different
4418 ones in restricted mode. */
4419
Guido van Rossum053b8df1998-11-25 16:18:00 +00004420 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004421 return -1;
4422
Guido van Rossum053b8df1998-11-25 16:18:00 +00004423 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4424 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004425 return -1;
4426
4427 Py_DECREF(copy_reg);
4428
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004429 /* Down to here ********************************** */
4430
Guido van Rossum053b8df1998-11-25 16:18:00 +00004431 UNLESS (string = PyImport_ImportModule("string"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004432 return -1;
4433
Guido van Rossum053b8df1998-11-25 16:18:00 +00004434 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004435 return -1;
4436
4437 Py_DECREF(string);
4438
Guido van Rossum053b8df1998-11-25 16:18:00 +00004439 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004440 return -1;
4441
Guido van Rossumc03158b1999-06-09 15:23:31 +00004442 /* Ugh */
4443 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4444 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4445 return -1;
4446
4447 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004448 UNLESS (r=PyRun_String(
4449 "def __init__(self, *args): self.args=args\n\n"
4450 "def __str__(self):\n"
4451 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4452 Py_file_input,
4453 module_dict, t) ) return -1;
4454 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004455
4456 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4457 return -1;
4458
4459 Py_DECREF(t);
4460
4461
4462 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
4463 PickleError, NULL))
4464 return -1;
4465
4466 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004467 UNLESS (r=PyRun_String(
4468 "def __init__(self, *args): self.args=args\n\n"
4469 "def __str__(self):\n"
4470 " a=self.args\n"
4471 " a=a and type(a[0]) or '(what)'\n"
4472 " return 'Cannot pickle %s objects' % a\n"
4473 , Py_file_input,
4474 module_dict, t) ) return -1;
4475 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004476
4477 UNLESS (UnpickleableError = PyErr_NewException(
4478 "cPickle.UnpickleableError", PicklingError, t))
4479 return -1;
4480
4481 Py_DECREF(t);
4482
4483 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
4484 PickleError, NULL))
4485 return -1;
4486
4487 if (PyDict_SetItemString(module_dict, "PickleError",
4488 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004489 return -1;
4490
4491 if (PyDict_SetItemString(module_dict, "PicklingError",
4492 PicklingError) < 0)
4493 return -1;
4494
Guido van Rossum60456fd1997-04-09 17:36:32 +00004495 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4496 UnpicklingError) < 0)
4497 return -1;
4498
Guido van Rossumc03158b1999-06-09 15:23:31 +00004499 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4500 UnpickleableError) < 0)
4501 return -1;
4502
Guido van Rossum053b8df1998-11-25 16:18:00 +00004503 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4504 return -1;
4505
4506 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4507 BadPickleGet) < 0)
4508 return -1;
4509
Guido van Rossum60456fd1997-04-09 17:36:32 +00004510 PycString_IMPORT;
4511
4512 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004513}
4514
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004515#ifndef DL_EXPORT /* declarations for DLL import/export */
4516#define DL_EXPORT(RTYPE) RTYPE
4517#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004518DL_EXPORT(void)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004519initcPickle() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004520 PyObject *m, *d, *v;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004521 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004522 PyObject *format_version;
4523 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004524
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004525 Picklertype.ob_type = &PyType_Type;
4526 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004527 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004528
Guido van Rossum60456fd1997-04-09 17:36:32 +00004529 /* Create the module and add the functions */
4530 m = Py_InitModule4("cPickle", cPickle_methods,
4531 cPickle_module_documentation,
4532 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004533
Guido van Rossum60456fd1997-04-09 17:36:32 +00004534 /* Add some symbolic constants to the module */
4535 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004536 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004537 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004538
Guido van Rossum60456fd1997-04-09 17:36:32 +00004539 format_version = PyString_FromString("1.3");
4540 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004541
Guido van Rossum60456fd1997-04-09 17:36:32 +00004542 PyDict_SetItemString(d, "format_version", format_version);
4543 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004544 Py_XDECREF(format_version);
4545 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004546
Guido van Rossum60456fd1997-04-09 17:36:32 +00004547 init_stuff(m, d);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004548}