blob: 5b02c2aa7d72403b9ba8926f14f87a82c6a27eb5 [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 Rossum2f4caa41997-01-06 22:59:08 +000057
Guido van Rossum142eeb81997-08-13 03:14:41 +000058#ifndef Py_eval_input
59#include <graminit.h>
60#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000061#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000062
Guido van Rossum2f4caa41997-01-06 22:59:08 +000063#include <errno.h>
64
Guido van Rossum2f4caa41997-01-06 22:59:08 +000065#define UNLESS(E) if (!(E))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000066
Guido van Rossum60456fd1997-04-09 17:36:32 +000067#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000068
Guido van Rossum60456fd1997-04-09 17:36:32 +000069#define WRITE_BUF_SIZE 256
70
71
72#define MARK '('
73#define STOP '.'
74#define POP '0'
75#define POP_MARK '1'
76#define DUP '2'
77#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000078#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000079#define INT 'I'
80#define BININT 'J'
81#define BININT1 'K'
82#define LONG 'L'
83#define BININT2 'M'
84#define NONE 'N'
85#define PERSID 'P'
86#define BINPERSID 'Q'
87#define REDUCE 'R'
88#define STRING 'S'
89#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000090#define SHORT_BINSTRING 'U'
Guido van Rossum5fccb7c2000-03-10 23:11:40 +000091#define UNICODE 'V'
92#define BINUNICODE 'X'
Guido van Rossum60456fd1997-04-09 17:36:32 +000093#define APPEND 'a'
94#define BUILD 'b'
95#define GLOBAL 'c'
96#define DICT 'd'
97#define EMPTY_DICT '}'
98#define APPENDS 'e'
99#define GET 'g'
100#define BINGET 'h'
101#define INST 'i'
102#define LONG_BINGET 'j'
103#define LIST 'l'
104#define EMPTY_LIST ']'
105#define OBJ 'o'
106#define PUT 'p'
107#define BINPUT 'q'
108#define LONG_BINPUT 'r'
109#define SETITEM 's'
110#define TUPLE 't'
111#define EMPTY_TUPLE ')'
112#define SETITEMS 'u'
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000113
Guido van Rossum60456fd1997-04-09 17:36:32 +0000114static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000115
116/* atol function from string module */
117static PyObject *atol_func;
118
Guido van Rossumc03158b1999-06-09 15:23:31 +0000119static PyObject *PickleError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000120static PyObject *PicklingError;
Guido van Rossumc03158b1999-06-09 15:23:31 +0000121static PyObject *UnpickleableError;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000122static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000123static PyObject *BadPickleGet;
124
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000125
Guido van Rossum60456fd1997-04-09 17:36:32 +0000126static PyObject *dispatch_table;
127static PyObject *safe_constructors;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000128static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000129
Guido van Rossum60456fd1997-04-09 17:36:32 +0000130static PyObject *__class___str, *__getinitargs___str, *__dict___str,
131 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
132 *write_str, *__safe_for_unpickling___str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000133 *read_str, *readline_str, *__main___str, *__basicnew___str,
Guido van Rossum053b8df1998-11-25 16:18:00 +0000134 *copy_reg_str, *dispatch_table_str, *safe_constructors_str, *empty_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000135
Guido van Rossum053b8df1998-11-25 16:18:00 +0000136#ifndef PyList_SET_ITEM
137#define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
138#endif
139#ifndef PyList_GET_SIZE
140#define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
141#endif
142#ifndef PyTuple_SET_ITEM
143#define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
144#endif
145#ifndef PyTuple_GET_SIZE
146#define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
147#endif
148#ifndef PyString_GET_SIZE
149#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
150#endif
151
152/*************************************************************************
153 Internal Data type for pickle data. */
154
155typedef struct {
156 PyObject_HEAD
157 int length, size;
158 PyObject **data;
159} Pdata;
160
161static void
162Pdata_dealloc(Pdata *self) {
163 int i;
164 PyObject **p;
165
166 for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
167
168 if (self->data) free(self->data);
169
Guido van Rossumb18618d2000-05-03 23:44:39 +0000170 PyObject_Del(self);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000171}
172
173static PyTypeObject PdataType = {
174 PyObject_HEAD_INIT(NULL) 0, "Pdata", sizeof(Pdata), 0,
175 (destructor)Pdata_dealloc,
176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
177};
178
179#define Pdata_Check(O) ((O)->ob_type == &PdataType)
180
181static PyObject *
Thomas Wouters58d05102000-07-24 14:43:35 +0000182Pdata_New(void) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000183 Pdata *self;
184
Guido van Rossumb18618d2000-05-03 23:44:39 +0000185 UNLESS (self = PyObject_New(Pdata, &PdataType)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000186 self->size=8;
187 self->length=0;
188 self->data=malloc(self->size * sizeof(PyObject*));
189 if (self->data) return (PyObject*)self;
190 Py_DECREF(self);
191 return PyErr_NoMemory();
192}
193
194static int
Thomas Wouters58d05102000-07-24 14:43:35 +0000195stackUnderflow(void) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000196 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
197 return -1;
198}
199
200static int
201Pdata_clear(Pdata *self, int clearto) {
202 int i;
203 PyObject **p;
204
205 if (clearto < 0) return stackUnderflow();
206 if (clearto >= self->length) return 0;
207
208 for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
209 Py_DECREF(*p);
210 self->length=clearto;
211
212 return 0;
213}
214
215
216static int
217Pdata_grow(Pdata *self) {
218 if (! self->size) {
219 PyErr_NoMemory();
220 return -1;
221 }
222 self->size *= 2;
223 self->data = realloc(self->data, self->size*sizeof(PyObject*));
224 if (! self->data) {
225 self->size = 0;
226 PyErr_NoMemory();
227 return -1;
228 }
229 return 0;
230}
231
232#define PDATA_POP(D,V) { \
233 if ((D)->length) V=D->data[--((D)->length)]; \
234 else { \
235 PyErr_SetString(UnpicklingError, "bad pickle data"); \
236 V=NULL; \
237 } \
238}
239
240
241static PyObject *
242Pdata_popTuple(Pdata *self, int start) {
243 PyObject *r;
244 int i, j, l;
245
246 l=self->length-start;
247 UNLESS (r=PyTuple_New(l)) return NULL;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000248 for (i=start, j=0 ; j < l; i++, j++)
249 PyTuple_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000250
251 self->length=start;
252 return r;
253}
254
255static PyObject *
256Pdata_popList(Pdata *self, int start) {
257 PyObject *r;
258 int i, j, l;
259
260 l=self->length-start;
261 UNLESS (r=PyList_New(l)) return NULL;
Guido van Rossumea2b7152000-05-09 18:14:50 +0000262 for (i=start, j=0 ; j < l; i++, j++)
263 PyList_SET_ITEM(r, j, self->data[i]);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000264
265 self->length=start;
266 return r;
267}
268
269#define PDATA_APPEND_(D,O,ER) { \
270 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
271}
272
273#define PDATA_APPEND(D,O,ER) { \
274 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
275 Pdata_grow((Pdata*)(D)) < 0) \
276 return ER; \
277 Py_INCREF(O); \
278 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
279}
280
281#define PDATA_PUSH(D,O,ER) { \
282 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
283 Pdata_grow((Pdata*)(D)) < 0) { \
284 Py_DECREF(O); \
285 return ER; \
286 } \
287 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
288}
289
290/*************************************************************************/
291
292#define ARG_TUP(self, o) { \
293 if (self->arg || (self->arg=PyTuple_New(1))) { \
294 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
295 PyTuple_SET_ITEM(self->arg,0,o); \
296 } \
297 else { \
298 Py_DECREF(o); \
299 } \
300}
301
302#define FREE_ARG_TUP(self) { \
303 if (self->arg->ob_refcnt > 1) { \
304 Py_DECREF(self->arg); \
305 self->arg=NULL; \
306 } \
307 }
308
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000309typedef struct Picklerobject {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000310 PyObject_HEAD
311 FILE *fp;
312 PyObject *write;
313 PyObject *file;
314 PyObject *memo;
315 PyObject *arg;
316 PyObject *pers_func;
317 PyObject *inst_pers_func;
318 int bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000319 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000320 int (*write_func)(struct Picklerobject *, char *, int);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000321 char *write_buf;
322 int buf_size;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000323 PyObject *dispatch_table;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000324} Picklerobject;
325
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000326staticforward PyTypeObject Picklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000327
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000328typedef struct Unpicklerobject {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000329 PyObject_HEAD
330 FILE *fp;
331 PyObject *file;
332 PyObject *readline;
333 PyObject *read;
334 PyObject *memo;
335 PyObject *arg;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000336 Pdata *stack;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000337 PyObject *mark;
338 PyObject *pers_func;
339 PyObject *last_string;
340 int *marks;
341 int num_marks;
342 int marks_size;
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000343 int (*read_func)(struct Unpicklerobject *, char **, int);
344 int (*readline_func)(struct Unpicklerobject *, char **);
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000345 int buf_size;
346 char *buf;
347 PyObject *safe_constructors;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +0000348 PyObject *find_class;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000349} Unpicklerobject;
350
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000351staticforward PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000352
Thomas Wouters4789b3a2000-07-24 11:36:47 +0000353/* Forward decls that need the above structs */
354static int save(Picklerobject *, PyObject *, int);
355static int put2(Picklerobject *, PyObject *);
356
Guido van Rossum60456fd1997-04-09 17:36:32 +0000357int
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000358cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000359 PyObject *v;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000360
Guido van Rossum053b8df1998-11-25 16:18:00 +0000361 if ((v = PyObject_GetItem(o,key))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000362 Py_DECREF(v);
363 return 1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000364 }
365
Guido van Rossum60456fd1997-04-09 17:36:32 +0000366 PyErr_Clear();
367 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000368}
369
Guido van Rossumd385d591997-04-09 17:47:47 +0000370static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000371PyObject *
Thomas Wouters3b6448f2000-07-22 23:56:07 +0000372cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
373{
Guido van Rossum60456fd1997-04-09 17:36:32 +0000374 va_list va;
375 PyObject *args=0, *retval=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000376 va_start(va, format);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000377
Guido van Rossum053b8df1998-11-25 16:18:00 +0000378 if (format) args = Py_VaBuildValue(format, va);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000379 va_end(va);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000380 if (format && ! args) return NULL;
381 if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000382
Guido van Rossum053b8df1998-11-25 16:18:00 +0000383 if (retval) {
384 if (args) {
385 PyObject *v;
386 v=PyString_Format(retval, args);
387 Py_DECREF(retval);
388 Py_DECREF(args);
389 if (! v) return NULL;
390 retval=v;
391 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000392 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000393 else
Guido van Rossum053b8df1998-11-25 16:18:00 +0000394 if (args) retval=args;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000395 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000396 PyErr_SetObject(ErrType,Py_None);
397 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000398 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000399 PyErr_SetObject(ErrType,retval);
400 Py_DECREF(retval);
401 return NULL;
402}
403
404static int
405write_file(Picklerobject *self, char *s, int n) {
406 if (s == NULL) {
407 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000408 }
409
Trent Mick6c116dd2000-08-12 20:58:11 +0000410 if (fwrite(s, sizeof(char), n, self->fp) != (size_t)n) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000411 PyErr_SetFromErrno(PyExc_IOError);
412 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000413 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000414
415 return n;
416}
417
Guido van Rossum60456fd1997-04-09 17:36:32 +0000418static int
419write_cStringIO(Picklerobject *self, char *s, int n) {
420 if (s == NULL) {
421 return 0;
422 }
423
424 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
425 return -1;
426 }
427
428 return n;
429}
430
Guido van Rossum60456fd1997-04-09 17:36:32 +0000431static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000432write_none(Picklerobject *self, char *s, int n) {
433 if (s == NULL) return 0;
434 return n;
435}
436
Guido van Rossum142eeb81997-08-13 03:14:41 +0000437static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000438write_other(Picklerobject *self, char *s, int n) {
439 PyObject *py_str = 0, *junk = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000440
441 if (s == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000442 UNLESS (self->buf_size) return 0;
443 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000444 PyString_FromStringAndSize(self->write_buf, self->buf_size))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000445 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000446 }
447 else {
448 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
449 if (write_other(self, NULL, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000450 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000451 }
452
453 if (n > WRITE_BUF_SIZE) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000454 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000455 PyString_FromStringAndSize(s, n))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000456 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000457 }
458 else {
459 memcpy(self->write_buf + self->buf_size, s, n);
460 self->buf_size += n;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000461 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000462 }
463 }
464
Guido van Rossum053b8df1998-11-25 16:18:00 +0000465 if (self->write) {
466 /* object with write method */
467 ARG_TUP(self, py_str);
468 if (self->arg) {
469 junk = PyObject_CallObject(self->write, self->arg);
470 FREE_ARG_TUP(self);
471 }
472 if (junk) Py_DECREF(junk);
473 else return -1;
474 }
475 else
476 PDATA_PUSH(self->file, py_str, -1);
477
478 self->buf_size = 0;
479 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000480}
481
482
483static int
484read_file(Unpicklerobject *self, char **s, int n) {
485
486 if (self->buf_size == 0) {
487 int size;
488
489 size = ((n < 32) ? 32 : n);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000490 UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000491 PyErr_NoMemory();
492 return -1;
493 }
494
495 self->buf_size = size;
496 }
497 else if (n > self->buf_size) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000498 UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000499 PyErr_NoMemory();
500 return -1;
501 }
502
503 self->buf_size = n;
504 }
505
Trent Mick6c116dd2000-08-12 20:58:11 +0000506 if (fread(self->buf, sizeof(char), n, self->fp) != (size_t)n) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000507 if (feof(self->fp)) {
508 PyErr_SetNone(PyExc_EOFError);
509 return -1;
510 }
511
512 PyErr_SetFromErrno(PyExc_IOError);
513 return -1;
514 }
515
516 *s = self->buf;
517
518 return n;
519}
520
521
522static int
523readline_file(Unpicklerobject *self, char **s) {
524 int i;
525
526 if (self->buf_size == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000527 UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000528 PyErr_NoMemory();
529 return -1;
530 }
531
532 self->buf_size = 40;
533 }
534
535 i = 0;
536 while (1) {
537 for (; i < (self->buf_size - 1); i++) {
538 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
539 self->buf[i + 1] = '\0';
540 *s = self->buf;
541 return i + 1;
542 }
543 }
544
Guido van Rossum053b8df1998-11-25 16:18:00 +0000545 UNLESS (self->buf = (char *)realloc(self->buf,
Guido van Rossum60456fd1997-04-09 17:36:32 +0000546 (self->buf_size * 2) * sizeof(char))) {
547 PyErr_NoMemory();
548 return -1;
549 }
550
551 self->buf_size *= 2;
552 }
553
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000554}
555
556
557static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000558read_cStringIO(Unpicklerobject *self, char **s, int n) {
559 char *ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000560
Guido van Rossum60456fd1997-04-09 17:36:32 +0000561 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
562 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000563 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000564 }
565
Guido van Rossum60456fd1997-04-09 17:36:32 +0000566 *s = ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000567
Guido van Rossum60456fd1997-04-09 17:36:32 +0000568 return n;
569}
570
571
572static int
573readline_cStringIO(Unpicklerobject *self, char **s) {
574 int n;
575 char *ptr;
576
577 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
578 return -1;
579 }
580
581 *s = ptr;
582
583 return n;
584}
585
586
587static int
588read_other(Unpicklerobject *self, char **s, int n) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000589 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000590
Guido van Rossum053b8df1998-11-25 16:18:00 +0000591 UNLESS (bytes = PyInt_FromLong(n)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000592
Guido van Rossum053b8df1998-11-25 16:18:00 +0000593 ARG_TUP(self, bytes);
594 if (self->arg) {
595 str = PyObject_CallObject(self->read, self->arg);
596 FREE_ARG_TUP(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000597 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000598 if (! str) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000599
600 Py_XDECREF(self->last_string);
601 self->last_string = str;
602
Guido van Rossum053b8df1998-11-25 16:18:00 +0000603 if (! (*s = PyString_AsString(str))) return -1;
604 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000605}
606
607
608static int
609readline_other(Unpicklerobject *self, char **s) {
610 PyObject *str;
611 int str_size;
612
Guido van Rossum053b8df1998-11-25 16:18:00 +0000613 UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000614 return -1;
615 }
616
Guido van Rossum053b8df1998-11-25 16:18:00 +0000617 if ((str_size = PyString_Size(str)) < 0)
618 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000619
620 Py_XDECREF(self->last_string);
621 self->last_string = str;
622
Guido van Rossum053b8df1998-11-25 16:18:00 +0000623 if (! (*s = PyString_AsString(str)))
624 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000625
626 return str_size;
627}
628
629
630static char *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000631pystrndup(char *s, int l) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000632 char *r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000633 UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
Guido van Rossum60456fd1997-04-09 17:36:32 +0000634 memcpy(r,s,l);
635 r[l]=0;
636 return r;
637}
638
639
640static int
641get(Picklerobject *self, PyObject *id) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000642 PyObject *value, *mv;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000643 long c_value;
644 char s[30];
Guido van Rossum534b7c52000-06-28 22:23:56 +0000645 size_t len;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000646
Guido van Rossum053b8df1998-11-25 16:18:00 +0000647 UNLESS (mv = PyDict_GetItem(self->memo, id)) {
648 PyErr_SetObject(PyExc_KeyError, id);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000649 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000650 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000651
Guido van Rossum053b8df1998-11-25 16:18:00 +0000652 UNLESS (value = PyTuple_GetItem(mv, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000653 return -1;
654
Guido van Rossum053b8df1998-11-25 16:18:00 +0000655 UNLESS (PyInt_Check(value)) {
656 PyErr_SetString(PicklingError, "no int where int expected in memo");
657 return -1;
658 }
659 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000660
661 if (!self->bin) {
662 s[0] = GET;
663 sprintf(s + 1, "%ld\n", c_value);
664 len = strlen(s);
665 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000666 else if (Pdata_Check(self->file)) {
667 if (write_other(self, NULL, 0) < 0) return -1;
668 PDATA_APPEND(self->file, mv, -1);
669 return 0;
670 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000671 else {
672 if (c_value < 256) {
673 s[0] = BINGET;
674 s[1] = (int)(c_value & 0xff);
675 len = 2;
676 }
677 else {
678 s[0] = LONG_BINGET;
679 s[1] = (int)(c_value & 0xff);
680 s[2] = (int)((c_value >> 8) & 0xff);
681 s[3] = (int)((c_value >> 16) & 0xff);
682 s[4] = (int)((c_value >> 24) & 0xff);
683 len = 5;
684 }
685 }
686
687 if ((*self->write_func)(self, s, len) < 0)
688 return -1;
689
690 return 0;
691}
692
693
694static int
695put(Picklerobject *self, PyObject *ob) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000696 if (ob->ob_refcnt < 2 || self->fast)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000697 return 0;
698
699 return put2(self, ob);
700}
701
702
703static int
704put2(Picklerobject *self, PyObject *ob) {
705 char c_str[30];
Guido van Rossum534b7c52000-06-28 22:23:56 +0000706 int p;
707 size_t len;
708 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000709 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000710
711 if (self->fast) return 0;
712
Guido van Rossum60456fd1997-04-09 17:36:32 +0000713 if ((p = PyDict_Size(self->memo)) < 0)
714 goto finally;
715
Guido van Rossum053b8df1998-11-25 16:18:00 +0000716 p++; /* Make sure memo keys are positive! */
717
Guido van Rossum534b7c52000-06-28 22:23:56 +0000718 UNLESS (py_ob_id = PyLong_FromVoidPtr(ob))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000719 goto finally;
720
721 UNLESS (memo_len = PyInt_FromLong(p))
722 goto finally;
723
724 UNLESS (t = PyTuple_New(2))
725 goto finally;
726
727 PyTuple_SET_ITEM(t, 0, memo_len);
728 Py_INCREF(memo_len);
729 PyTuple_SET_ITEM(t, 1, ob);
730 Py_INCREF(ob);
731
732 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
733 goto finally;
734
Guido van Rossum60456fd1997-04-09 17:36:32 +0000735 if (!self->bin) {
736 c_str[0] = PUT;
737 sprintf(c_str + 1, "%d\n", p);
738 len = strlen(c_str);
739 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000740 else if (Pdata_Check(self->file)) {
741 if (write_other(self, NULL, 0) < 0) return -1;
742 PDATA_APPEND(self->file, memo_len, -1);
743 res=0; /* Job well done ;) */
744 goto finally;
745 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000746 else {
747 if (p >= 256) {
748 c_str[0] = LONG_BINPUT;
749 c_str[1] = (int)(p & 0xff);
750 c_str[2] = (int)((p >> 8) & 0xff);
751 c_str[3] = (int)((p >> 16) & 0xff);
752 c_str[4] = (int)((p >> 24) & 0xff);
753 len = 5;
754 }
755 else {
756 c_str[0] = BINPUT;
757 c_str[1] = p;
758 len = 2;
759 }
760 }
761
762 if ((*self->write_func)(self, c_str, len) < 0)
763 goto finally;
764
Guido van Rossum60456fd1997-04-09 17:36:32 +0000765 res = 0;
766
767finally:
768 Py_XDECREF(py_ob_id);
769 Py_XDECREF(memo_len);
770 Py_XDECREF(t);
771
772 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000773}
774
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000775#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000776
777static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000778PyImport_Import(PyObject *module_name) {
779 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
780 static PyObject *standard_builtins=0;
781 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
782
Guido van Rossum053b8df1998-11-25 16:18:00 +0000783 UNLESS (silly_list) {
784 UNLESS (__import___str=PyString_FromString("__import__"))
785 return NULL;
786 UNLESS (__builtins___str=PyString_FromString("__builtins__"))
787 return NULL;
788 UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
789 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000790 }
791
Guido van Rossum053b8df1998-11-25 16:18:00 +0000792 if ((globals=PyEval_GetGlobals())) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000793 Py_INCREF(globals);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000794 UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
795 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000796 }
797 else {
798 PyErr_Clear();
799
Guido van Rossum053b8df1998-11-25 16:18:00 +0000800 UNLESS (standard_builtins ||
801 (standard_builtins=PyImport_ImportModule("__builtin__")))
802 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000803
804 __builtins__=standard_builtins;
805 Py_INCREF(__builtins__);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000806 UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
807 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000808 }
809
Guido van Rossum053b8df1998-11-25 16:18:00 +0000810 if (PyDict_Check(__builtins__)) {
811 UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000812 }
813 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000814 UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000815 }
816
Guido van Rossum053b8df1998-11-25 16:18:00 +0000817 UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
818 module_name, globals, globals, silly_list))
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000819 goto err;
820
821 Py_DECREF(globals);
822 Py_DECREF(__builtins__);
823 Py_DECREF(__import__);
824
825 return r;
826err:
827 Py_XDECREF(globals);
828 Py_XDECREF(__builtins__);
829 Py_XDECREF(__import__);
830 return NULL;
831}
832
833static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +0000834whichmodule(PyObject *global, PyObject *global_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000835 int i, j;
836 PyObject *module = 0, *modules_dict = 0,
837 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000838
Guido van Rossum45188231997-09-28 05:38:51 +0000839 module = PyObject_GetAttrString(global, "__module__");
840 if (module) return module;
841 PyErr_Clear();
842
Guido van Rossum053b8df1998-11-25 16:18:00 +0000843 UNLESS (modules_dict = PySys_GetObject("modules"))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000844 return NULL;
845
Guido van Rossum60456fd1997-04-09 17:36:32 +0000846 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000847 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
848
Guido van Rossum053b8df1998-11-25 16:18:00 +0000849 if (PyObject_Compare(name, __main___str)==0) continue;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000850
Guido van Rossum053b8df1998-11-25 16:18:00 +0000851 UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000852 PyErr_Clear();
853 continue;
854 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000855
Guido van Rossum60456fd1997-04-09 17:36:32 +0000856 if (global_name_attr != global) {
857 Py_DECREF(global_name_attr);
858 continue;
859 }
860
861 Py_DECREF(global_name_attr);
862
863 break;
864 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000865
866 /* The following implements the rule in pickle.py added in 1.5
867 that used __main__ if no module is found. I don't actually
868 like this rule. jlf
869 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000870 if (!j) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000871 j=1;
872 name=__main___str;
873 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000874
875 Py_INCREF(name);
876 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000877}
878
879
Guido van Rossum60456fd1997-04-09 17:36:32 +0000880static int
881save_none(Picklerobject *self, PyObject *args) {
882 static char none = NONE;
883 if ((*self->write_func)(self, &none, 1) < 0)
884 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000885
Guido van Rossum60456fd1997-04-09 17:36:32 +0000886 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000887}
888
889
Guido van Rossum60456fd1997-04-09 17:36:32 +0000890static int
891save_int(Picklerobject *self, PyObject *args) {
892 char c_str[32];
893 long l = PyInt_AS_LONG((PyIntObject *)args);
894 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000895
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000896 if (!self->bin
897#if SIZEOF_LONG > 4
Guido van Rossum053b8df1998-11-25 16:18:00 +0000898 || (l >> 32)
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000899#endif
Guido van Rossum053b8df1998-11-25 16:18:00 +0000900 ) {
901 /* Save extra-long ints in non-binary mode, so that
902 we can use python long parsing code to restore,
903 if necessary. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000904 c_str[0] = INT;
905 sprintf(c_str + 1, "%ld\n", l);
906 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
907 return -1;
908 }
909 else {
910 c_str[1] = (int)( l & 0xff);
911 c_str[2] = (int)((l >> 8) & 0xff);
912 c_str[3] = (int)((l >> 16) & 0xff);
913 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000914
Guido van Rossum60456fd1997-04-09 17:36:32 +0000915 if ((c_str[4] == 0) && (c_str[3] == 0)) {
916 if (c_str[2] == 0) {
917 c_str[0] = BININT1;
918 len = 2;
919 }
920 else {
921 c_str[0] = BININT2;
922 len = 3;
923 }
924 }
925 else {
926 c_str[0] = BININT;
927 len = 5;
928 }
929
930 if ((*self->write_func)(self, c_str, len) < 0)
931 return -1;
932 }
933
934 return 0;
935}
936
937
938static int
939save_long(Picklerobject *self, PyObject *args) {
940 int size, res = -1;
941 PyObject *repr = 0;
942
943 static char l = LONG;
944
Guido van Rossum053b8df1998-11-25 16:18:00 +0000945 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000946 goto finally;
947
948 if ((size = PyString_Size(repr)) < 0)
949 goto finally;
950
951 if ((*self->write_func)(self, &l, 1) < 0)
952 goto finally;
953
954 if ((*self->write_func)(self,
955 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
956 goto finally;
957
958 if ((*self->write_func)(self, "\n", 1) < 0)
959 goto finally;
960
961 res = 0;
962
963finally:
964 Py_XDECREF(repr);
965
966 return res;
967}
968
969
970static int
971save_float(Picklerobject *self, PyObject *args) {
972 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
973
Guido van Rossum60456fd1997-04-09 17:36:32 +0000974 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000975 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000976 double f;
977 long fhi, flo;
978 char str[9], *p = str;
979
980 *p = BINFLOAT;
981 p++;
982
983 if (x < 0) {
984 s = 1;
985 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000986 }
987 else
Guido van Rossum60456fd1997-04-09 17:36:32 +0000988 s = 0;
989
990 f = frexp(x, &e);
991
992 /* Normalize f to be in the range [1.0, 2.0) */
993 if (0.5 <= f && f < 1.0) {
994 f *= 2.0;
995 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000996 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000997 else if (f == 0.0) {
998 e = 0;
999 }
1000 else {
1001 PyErr_SetString(PyExc_SystemError,
1002 "frexp() result out of range");
1003 return -1;
1004 }
1005
1006 if (e >= 1024) {
1007 /* XXX 1024 itself is reserved for Inf/NaN */
1008 PyErr_SetString(PyExc_OverflowError,
1009 "float too large to pack with d format");
1010 return -1;
1011 }
1012 else if (e < -1022) {
1013 /* Gradual underflow */
1014 f = ldexp(f, 1022 + e);
1015 e = 0;
1016 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001017 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001018 e += 1023;
1019 f -= 1.0; /* Get rid of leading 1 */
1020 }
1021
1022 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1023 f *= 268435456.0; /* 2**28 */
1024 fhi = (long) floor(f); /* Truncate */
1025 f -= (double)fhi;
1026 f *= 16777216.0; /* 2**24 */
1027 flo = (long) floor(f + 0.5); /* Round */
1028
1029 /* First byte */
1030 *p = (s<<7) | (e>>4);
1031 p++;
1032
1033 /* Second byte */
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001034 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum60456fd1997-04-09 17:36:32 +00001035 p++;
1036
1037 /* Third byte */
1038 *p = (fhi>>16) & 0xFF;
1039 p++;
1040
1041 /* Fourth byte */
1042 *p = (fhi>>8) & 0xFF;
1043 p++;
1044
1045 /* Fifth byte */
1046 *p = fhi & 0xFF;
1047 p++;
1048
1049 /* Sixth byte */
1050 *p = (flo>>16) & 0xFF;
1051 p++;
1052
1053 /* Seventh byte */
1054 *p = (flo>>8) & 0xFF;
1055 p++;
1056
1057 /* Eighth byte */
1058 *p = flo & 0xFF;
1059
1060 if ((*self->write_func)(self, str, 9) < 0)
1061 return -1;
1062 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001063 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001064 char c_str[250];
1065 c_str[0] = FLOAT;
Guido van Rossum104be4a1998-04-03 21:13:02 +00001066 sprintf(c_str + 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001067
1068 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
1069 return -1;
1070 }
1071
1072 return 0;
1073}
1074
1075
1076static int
Guido van Rossum142eeb81997-08-13 03:14:41 +00001077save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001078 int size, len;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001079 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001080
Guido van Rossum053b8df1998-11-25 16:18:00 +00001081 if ((size = PyString_Size(args)) < 0)
1082 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001083
1084 if (!self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001085 char *repr_str;
1086
1087 static char string = STRING;
1088
Guido van Rossum053b8df1998-11-25 16:18:00 +00001089 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001090 return -1;
1091
Guido van Rossum053b8df1998-11-25 16:18:00 +00001092 if ((len = PyString_Size(repr)) < 0)
1093 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001094 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001095
1096 if ((*self->write_func)(self, &string, 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001097 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001098
1099 if ((*self->write_func)(self, repr_str, len) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001100 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001101
1102 if ((*self->write_func)(self, "\n", 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001103 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001104
1105 Py_XDECREF(repr);
1106 }
1107 else {
1108 int i;
1109 char c_str[5];
1110
Guido van Rossum053b8df1998-11-25 16:18:00 +00001111 if ((size = PyString_Size(args)) < 0)
1112 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001113
1114 if (size < 256) {
1115 c_str[0] = SHORT_BINSTRING;
1116 c_str[1] = size;
1117 len = 2;
1118 }
1119 else {
1120 c_str[0] = BINSTRING;
1121 for (i = 1; i < 5; i++)
1122 c_str[i] = (int)(size >> ((i - 1) * 8));
1123 len = 5;
1124 }
1125
1126 if ((*self->write_func)(self, c_str, len) < 0)
1127 return -1;
1128
Guido van Rossum053b8df1998-11-25 16:18:00 +00001129 if (size > 128 && Pdata_Check(self->file)) {
1130 if (write_other(self, NULL, 0) < 0) return -1;
1131 PDATA_APPEND(self->file, args, -1);
1132 }
1133 else {
1134 if ((*self->write_func)(self,
1135 PyString_AS_STRING((PyStringObject *)args), size) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001136 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001137 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001138 }
1139
Guido van Rossum142eeb81997-08-13 03:14:41 +00001140 if (doput)
1141 if (put(self, args) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001142 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001143
1144 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001145
1146err:
1147 Py_XDECREF(repr);
1148 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001149}
1150
1151
1152static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001153save_unicode(Picklerobject *self, PyObject *args, int doput) {
1154 int size, len;
1155 PyObject *repr=0;
1156
1157 if (!PyUnicode_Check(args))
1158 return -1;
1159
1160 if (!self->bin) {
1161 char *repr_str;
1162 static char string = UNICODE;
1163
1164 UNLESS (repr = PyUnicode_AsRawUnicodeEscapeString(args))
1165 return -1;
1166
1167 if ((len = PyString_Size(repr)) < 0)
1168 goto err;
1169 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1170
1171 if ((*self->write_func)(self, &string, 1) < 0)
1172 goto err;
1173
1174 if ((*self->write_func)(self, repr_str, len) < 0)
1175 goto err;
1176
1177 if ((*self->write_func)(self, "\n", 1) < 0)
1178 goto err;
1179
1180 Py_XDECREF(repr);
1181 }
1182 else {
1183 int i;
1184 char c_str[5];
1185
1186 UNLESS (repr = PyUnicode_AsUTF8String(args))
1187 return -1;
1188
1189 if ((size = PyString_Size(repr)) < 0)
1190 goto err;
1191
1192 c_str[0] = BINUNICODE;
1193 for (i = 1; i < 5; i++)
1194 c_str[i] = (int)(size >> ((i - 1) * 8));
1195 len = 5;
1196
1197 if ((*self->write_func)(self, c_str, len) < 0)
1198 goto err;
1199
1200 if (size > 128 && Pdata_Check(self->file)) {
1201 if (write_other(self, NULL, 0) < 0)
1202 goto err;
1203 PDATA_APPEND(self->file, repr, -1);
1204 }
1205 else {
1206 if ((*self->write_func)(self, PyString_AS_STRING(repr), size) < 0)
1207 goto err;
1208 }
1209
1210 Py_DECREF(repr);
1211 }
1212
1213 if (doput)
1214 if (put(self, args) < 0)
1215 return -1;
1216
1217 return 0;
1218
1219err:
1220 Py_XDECREF(repr);
1221 return -1;
1222}
1223
1224
1225static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00001226save_tuple(Picklerobject *self, PyObject *args) {
1227 PyObject *element = 0, *py_tuple_id = 0;
1228 int len, i, has_key, res = -1;
1229
1230 static char tuple = TUPLE;
1231
1232 if ((*self->write_func)(self, &MARKv, 1) < 0)
1233 goto finally;
1234
1235 if ((len = PyTuple_Size(args)) < 0)
1236 goto finally;
1237
1238 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001239 UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001240 goto finally;
1241
1242 if (save(self, element, 0) < 0)
1243 goto finally;
1244 }
1245
Guido van Rossum534b7c52000-06-28 22:23:56 +00001246 UNLESS (py_tuple_id = PyLong_FromVoidPtr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001247 goto finally;
1248
1249 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001250 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001251 goto finally;
1252
1253 if (has_key) {
1254 if (self->bin) {
1255 static char pop_mark = POP_MARK;
1256
1257 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1258 goto finally;
1259 }
1260 else {
1261 static char pop = POP;
1262
1263 for (i = 0; i <= len; i++) {
1264 if ((*self->write_func)(self, &pop, 1) < 0)
1265 goto finally;
1266 }
1267 }
1268
1269 if (get(self, py_tuple_id) < 0)
1270 goto finally;
1271
1272 res = 0;
1273 goto finally;
1274 }
1275 }
1276
1277 if ((*self->write_func)(self, &tuple, 1) < 0) {
1278 goto finally;
1279 }
1280
1281 if (put(self, args) < 0)
1282 goto finally;
1283
1284 res = 0;
1285
1286finally:
1287 Py_XDECREF(py_tuple_id);
1288
1289 return res;
1290}
1291
1292static int
1293save_empty_tuple(Picklerobject *self, PyObject *args) {
1294 static char tuple = EMPTY_TUPLE;
1295
1296 return (*self->write_func)(self, &tuple, 1);
1297}
1298
1299
1300static int
1301save_list(Picklerobject *self, PyObject *args) {
1302 PyObject *element = 0;
1303 int s_len, len, i, using_appends, res = -1;
1304 char s[3];
1305
1306 static char append = APPEND, appends = APPENDS;
1307
Guido van Rossum053b8df1998-11-25 16:18:00 +00001308 if (self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001309 s[0] = EMPTY_LIST;
1310 s_len = 1;
1311 }
1312 else {
1313 s[0] = MARK;
1314 s[1] = LIST;
1315 s_len = 2;
1316 }
1317
1318 if ((len = PyList_Size(args)) < 0)
1319 goto finally;
1320
1321 if ((*self->write_func)(self, s, s_len) < 0)
1322 goto finally;
1323
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001324 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001325 if (put(self, args) < 0)
1326 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001327 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001328 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001329 if (put2(self, args) < 0)
1330 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001331 }
1332
Guido van Rossum142eeb81997-08-13 03:14:41 +00001333 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001334 if ((*self->write_func)(self, &MARKv, 1) < 0)
1335 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001336
Guido van Rossum60456fd1997-04-09 17:36:32 +00001337 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001338 UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001339 goto finally;
1340
1341 if (save(self, element, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001342 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001343
1344 if (!using_appends) {
1345 if ((*self->write_func)(self, &append, 1) < 0)
1346 goto finally;
1347 }
1348 }
1349
1350 if (using_appends) {
1351 if ((*self->write_func)(self, &appends, 1) < 0)
1352 goto finally;
1353 }
1354
1355 res = 0;
1356
1357finally:
1358
1359 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001360}
1361
1362
Guido van Rossum60456fd1997-04-09 17:36:32 +00001363static int
1364save_dict(Picklerobject *self, PyObject *args) {
1365 PyObject *key = 0, *value = 0;
1366 int i, len, res = -1, using_setitems;
1367 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001368
Guido van Rossum60456fd1997-04-09 17:36:32 +00001369 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001370
Guido van Rossum60456fd1997-04-09 17:36:32 +00001371 if (self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001372 s[0] = EMPTY_DICT;
1373 len = 1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001374 }
1375 else {
1376 s[0] = MARK;
1377 s[1] = DICT;
1378 len = 2;
1379 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001380
Guido van Rossum60456fd1997-04-09 17:36:32 +00001381 if ((*self->write_func)(self, s, len) < 0)
1382 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001383
Guido van Rossum60456fd1997-04-09 17:36:32 +00001384 if ((len = PyDict_Size(args)) < 0)
1385 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001386
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001387 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001388 if (put(self, args) < 0)
1389 goto finally;
1390 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001391 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001392 if (put2(self, args) < 0)
1393 goto finally;
1394 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001395
Guido van Rossum142eeb81997-08-13 03:14:41 +00001396 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001397 if ((*self->write_func)(self, &MARKv, 1) < 0)
1398 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001399
Guido van Rossum60456fd1997-04-09 17:36:32 +00001400 i = 0;
1401 while (PyDict_Next(args, &i, &key, &value)) {
1402 if (save(self, key, 0) < 0)
1403 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001404
Guido van Rossum60456fd1997-04-09 17:36:32 +00001405 if (save(self, value, 0) < 0)
1406 goto finally;
1407
1408 if (!using_setitems) {
1409 if ((*self->write_func)(self, &setitem, 1) < 0)
1410 goto finally;
1411 }
1412 }
1413
1414 if (using_setitems) {
1415 if ((*self->write_func)(self, &setitems, 1) < 0)
1416 goto finally;
1417 }
1418
1419 res = 0;
1420
1421finally:
1422
1423 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001424}
1425
1426
Guido van Rossum60456fd1997-04-09 17:36:32 +00001427static int
1428save_inst(Picklerobject *self, PyObject *args) {
1429 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1430 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1431 char *module_str, *name_str;
1432 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001433
Guido van Rossum60456fd1997-04-09 17:36:32 +00001434 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001435
Guido van Rossum60456fd1997-04-09 17:36:32 +00001436 if ((*self->write_func)(self, &MARKv, 1) < 0)
1437 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001438
Guido van Rossum053b8df1998-11-25 16:18:00 +00001439 UNLESS (class = PyObject_GetAttr(args, __class___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001440 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001441
Guido van Rossum60456fd1997-04-09 17:36:32 +00001442 if (self->bin) {
1443 if (save(self, class, 0) < 0)
1444 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001445 }
1446
Guido van Rossum142eeb81997-08-13 03:14:41 +00001447 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001448 PyObject *element = 0;
1449 int i, len;
1450
Guido van Rossum053b8df1998-11-25 16:18:00 +00001451 UNLESS (class_args =
Guido van Rossum60456fd1997-04-09 17:36:32 +00001452 PyObject_CallObject(getinitargs_func, empty_tuple))
1453 goto finally;
1454
Jeremy Hylton03657cf2000-07-12 13:05:33 +00001455 if ((len = PyObject_Size(class_args)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001456 goto finally;
1457
1458 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001459 UNLESS (element = PySequence_GetItem(class_args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001460 goto finally;
1461
1462 if (save(self, element, 0) < 0) {
1463 Py_DECREF(element);
1464 goto finally;
1465 }
1466
1467 Py_DECREF(element);
1468 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001469 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001470 else {
1471 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001472 }
1473
Guido van Rossum60456fd1997-04-09 17:36:32 +00001474 if (!self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001475 UNLESS (name = ((PyClassObject *)class)->cl_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001476 PyErr_SetString(PicklingError, "class has no name");
1477 goto finally;
1478 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001479
Guido van Rossum053b8df1998-11-25 16:18:00 +00001480 UNLESS (module = whichmodule(class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001481 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001482
1483
1484 if ((module_size = PyString_Size(module)) < 0 ||
1485 (name_size = PyString_Size(name)) < 0)
1486 goto finally;
1487
Guido van Rossum60456fd1997-04-09 17:36:32 +00001488 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001489 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001490
Guido van Rossum60456fd1997-04-09 17:36:32 +00001491 if ((*self->write_func)(self, &inst, 1) < 0)
1492 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001493
Guido van Rossum60456fd1997-04-09 17:36:32 +00001494 if ((*self->write_func)(self, module_str, module_size) < 0)
1495 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001496
Guido van Rossum60456fd1997-04-09 17:36:32 +00001497 if ((*self->write_func)(self, "\n", 1) < 0)
1498 goto finally;
1499
1500 if ((*self->write_func)(self, name_str, name_size) < 0)
1501 goto finally;
1502
1503 if ((*self->write_func)(self, "\n", 1) < 0)
1504 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001505 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001506 else if ((*self->write_func)(self, &obj, 1) < 0) {
1507 goto finally;
1508 }
1509
Guido van Rossum142eeb81997-08-13 03:14:41 +00001510 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001511 UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001512 goto finally;
1513 }
1514 else {
1515 PyErr_Clear();
1516
Guido van Rossum053b8df1998-11-25 16:18:00 +00001517 UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001518 PyErr_Clear();
1519 res = 0;
1520 goto finally;
1521 }
1522 }
1523
1524 if (!PyDict_Check(state)) {
1525 if (put2(self, args) < 0)
1526 goto finally;
1527 }
1528 else {
1529 if (put(self, args) < 0)
1530 goto finally;
1531 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001532
Guido van Rossum60456fd1997-04-09 17:36:32 +00001533 if (save(self, state, 0) < 0)
1534 goto finally;
1535
1536 if ((*self->write_func)(self, &build, 1) < 0)
1537 goto finally;
1538
1539 res = 0;
1540
1541finally:
1542 Py_XDECREF(module);
1543 Py_XDECREF(class);
1544 Py_XDECREF(state);
1545 Py_XDECREF(getinitargs_func);
1546 Py_XDECREF(getstate_func);
1547 Py_XDECREF(class_args);
1548
1549 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001550}
1551
1552
Guido van Rossum60456fd1997-04-09 17:36:32 +00001553static int
1554save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1555 PyObject *global_name = 0, *module = 0;
1556 char *name_str, *module_str;
1557 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001558
Guido van Rossum60456fd1997-04-09 17:36:32 +00001559 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001560
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001561 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001562 global_name = name;
1563 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001564 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001565 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001566 UNLESS (global_name = PyObject_GetAttr(args, __name___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001567 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001568 }
1569
Guido van Rossum053b8df1998-11-25 16:18:00 +00001570 UNLESS (module = whichmodule(args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001571 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001572
Guido van Rossum053b8df1998-11-25 16:18:00 +00001573 if ((module_size = PyString_Size(module)) < 0 ||
1574 (name_size = PyString_Size(global_name)) < 0)
1575 goto finally;
1576
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001577 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001578 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001579
Guido van Rossum60456fd1997-04-09 17:36:32 +00001580 if ((*self->write_func)(self, &global, 1) < 0)
1581 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001582
Guido van Rossum60456fd1997-04-09 17:36:32 +00001583 if ((*self->write_func)(self, module_str, module_size) < 0)
1584 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001585
Guido van Rossum60456fd1997-04-09 17:36:32 +00001586 if ((*self->write_func)(self, "\n", 1) < 0)
1587 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001588
Guido van Rossum60456fd1997-04-09 17:36:32 +00001589 if ((*self->write_func)(self, name_str, name_size) < 0)
1590 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001591
Guido van Rossum60456fd1997-04-09 17:36:32 +00001592 if ((*self->write_func)(self, "\n", 1) < 0)
1593 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001594
Guido van Rossum60456fd1997-04-09 17:36:32 +00001595 if (put(self, args) < 0)
1596 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001597
Guido van Rossum60456fd1997-04-09 17:36:32 +00001598 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001599
Guido van Rossum60456fd1997-04-09 17:36:32 +00001600finally:
1601 Py_XDECREF(module);
1602 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001603
Guido van Rossum60456fd1997-04-09 17:36:32 +00001604 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001605}
1606
Guido van Rossum60456fd1997-04-09 17:36:32 +00001607static int
1608save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1609 PyObject *pid = 0;
1610 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001611
Guido van Rossum60456fd1997-04-09 17:36:32 +00001612 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001613
Guido van Rossum053b8df1998-11-25 16:18:00 +00001614 Py_INCREF(args);
1615 ARG_TUP(self, args);
1616 if (self->arg) {
1617 pid = PyObject_CallObject(f, self->arg);
1618 FREE_ARG_TUP(self);
1619 }
1620 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001621
Guido van Rossum60456fd1997-04-09 17:36:32 +00001622 if (pid != Py_None) {
1623 if (!self->bin) {
1624 if (!PyString_Check(pid)) {
1625 PyErr_SetString(PicklingError,
1626 "persistent id must be string");
1627 goto finally;
1628 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001629
Guido van Rossum60456fd1997-04-09 17:36:32 +00001630 if ((*self->write_func)(self, &persid, 1) < 0)
1631 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001632
Guido van Rossum60456fd1997-04-09 17:36:32 +00001633 if ((size = PyString_Size(pid)) < 0)
1634 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001635
Guido van Rossum60456fd1997-04-09 17:36:32 +00001636 if ((*self->write_func)(self,
1637 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1638 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001639
Guido van Rossum60456fd1997-04-09 17:36:32 +00001640 if ((*self->write_func)(self, "\n", 1) < 0)
1641 goto finally;
1642
1643 res = 1;
1644 goto finally;
1645 }
1646 else if (save(self, pid, 1) >= 0) {
1647 if ((*self->write_func)(self, &binpersid, 1) < 0)
1648 res = -1;
1649 else
1650 res = 1;
1651 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001652
Guido van Rossum60456fd1997-04-09 17:36:32 +00001653 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001654 }
1655
Guido van Rossum60456fd1997-04-09 17:36:32 +00001656 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001657
Guido van Rossum60456fd1997-04-09 17:36:32 +00001658finally:
1659 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001660
Guido van Rossum60456fd1997-04-09 17:36:32 +00001661 return res;
1662}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001663
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001664
Guido van Rossum60456fd1997-04-09 17:36:32 +00001665static int
1666save_reduce(Picklerobject *self, PyObject *callable,
1667 PyObject *tup, PyObject *state, PyObject *ob) {
1668 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001669
Guido van Rossum60456fd1997-04-09 17:36:32 +00001670 if (save(self, callable, 0) < 0)
1671 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001672
Guido van Rossum60456fd1997-04-09 17:36:32 +00001673 if (save(self, tup, 0) < 0)
1674 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001675
Guido van Rossum60456fd1997-04-09 17:36:32 +00001676 if ((*self->write_func)(self, &reduce, 1) < 0)
1677 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001678
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001679 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001680 if (state && !PyDict_Check(state)) {
1681 if (put2(self, ob) < 0)
1682 return -1;
1683 }
1684 else {
1685 if (put(self, ob) < 0)
1686 return -1;
1687 }
1688 }
1689
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001690 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001691 if (save(self, state, 0) < 0)
1692 return -1;
1693
1694 if ((*self->write_func)(self, &build, 1) < 0)
1695 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001696 }
1697
Guido van Rossum60456fd1997-04-09 17:36:32 +00001698 return 0;
1699}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001700
Guido van Rossum60456fd1997-04-09 17:36:32 +00001701static int
1702save(Picklerobject *self, PyObject *args, int pers_save) {
1703 PyTypeObject *type;
1704 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001705 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001706 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001707
Guido van Rossum60456fd1997-04-09 17:36:32 +00001708 if (!pers_save && self->pers_func) {
1709 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1710 res = tmp;
1711 goto finally;
1712 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001713 }
1714
Guido van Rossum60456fd1997-04-09 17:36:32 +00001715 if (args == Py_None) {
1716 res = save_none(self, args);
1717 goto finally;
1718 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001719
Guido van Rossum60456fd1997-04-09 17:36:32 +00001720 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001721
Guido van Rossum60456fd1997-04-09 17:36:32 +00001722 switch (type->tp_name[0]) {
1723 case 'i':
1724 if (type == &PyInt_Type) {
1725 res = save_int(self, args);
1726 goto finally;
1727 }
1728 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001729
Guido van Rossum60456fd1997-04-09 17:36:32 +00001730 case 'l':
1731 if (type == &PyLong_Type) {
1732 res = save_long(self, args);
1733 goto finally;
1734 }
1735 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001736
Guido van Rossum60456fd1997-04-09 17:36:32 +00001737 case 'f':
1738 if (type == &PyFloat_Type) {
1739 res = save_float(self, args);
1740 goto finally;
1741 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001742 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001743
Guido van Rossum60456fd1997-04-09 17:36:32 +00001744 case 't':
1745 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001746 if (self->bin) res = save_empty_tuple(self, args);
1747 else res = save_tuple(self, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001748 goto finally;
1749 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001750 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001751
Guido van Rossum60456fd1997-04-09 17:36:32 +00001752 case 's':
Guido van Rossum053b8df1998-11-25 16:18:00 +00001753 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001754 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001755 goto finally;
1756 }
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001757
1758 case 'u':
1759 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
1760 res = save_unicode(self, args, 0);
1761 goto finally;
1762 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001763 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001764
Guido van Rossum60456fd1997-04-09 17:36:32 +00001765 if (args->ob_refcnt > 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001766 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001767
Guido van Rossum534b7c52000-06-28 22:23:56 +00001768 UNLESS (py_ob_id = PyLong_FromVoidPtr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001769 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001770
Guido van Rossum60456fd1997-04-09 17:36:32 +00001771 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1772 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001773
Guido van Rossum60456fd1997-04-09 17:36:32 +00001774 if (has_key) {
1775 if (get(self, py_ob_id) < 0)
1776 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001777
Guido van Rossum60456fd1997-04-09 17:36:32 +00001778 res = 0;
1779 goto finally;
1780 }
1781 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001782
Guido van Rossum60456fd1997-04-09 17:36:32 +00001783 switch (type->tp_name[0]) {
1784 case 's':
1785 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001786 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001787 goto finally;
1788 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001789 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001790
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00001791 case 'u':
1792 if (type == &PyUnicode_Type) {
1793 res = save_unicode(self, args, 1);
1794 goto finally;
1795 }
1796 break;
1797
Guido van Rossum60456fd1997-04-09 17:36:32 +00001798 case 't':
1799 if (type == &PyTuple_Type) {
1800 res = save_tuple(self, args);
1801 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001802 }
1803 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001804
Guido van Rossum60456fd1997-04-09 17:36:32 +00001805 case 'l':
1806 if (type == &PyList_Type) {
1807 res = save_list(self, args);
1808 goto finally;
1809 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001810 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001811
1812 case 'd':
1813 if (type == &PyDict_Type) {
1814 res = save_dict(self, args);
1815 goto finally;
1816 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001817 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001818
1819 case 'i':
1820 if (type == &PyInstance_Type) {
1821 res = save_inst(self, args);
1822 goto finally;
1823 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001824 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001825
1826 case 'c':
1827 if (type == &PyClass_Type) {
1828 res = save_global(self, args, NULL);
1829 goto finally;
1830 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001831 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001832
1833 case 'f':
1834 if (type == &PyFunction_Type) {
1835 res = save_global(self, args, NULL);
1836 goto finally;
1837 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001838 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001839
1840 case 'b':
1841 if (type == &PyCFunction_Type) {
1842 res = save_global(self, args, NULL);
1843 goto finally;
1844 }
1845 }
1846
1847 if (!pers_save && self->inst_pers_func) {
1848 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1849 res = tmp;
1850 goto finally;
1851 }
1852 }
1853
Guido van Rossum142eeb81997-08-13 03:14:41 +00001854 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001855 Py_INCREF(__reduce__);
1856
Guido van Rossum60456fd1997-04-09 17:36:32 +00001857 Py_INCREF(args);
Guido van Rossum053b8df1998-11-25 16:18:00 +00001858 ARG_TUP(self, args);
1859 if (self->arg) {
1860 t = PyObject_CallObject(__reduce__, self->arg);
1861 FREE_ARG_TUP(self);
1862 }
1863 if (! t) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001864 }
1865 else {
1866 PyErr_Clear();
1867
Guido van Rossum142eeb81997-08-13 03:14:41 +00001868 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001869 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001870 goto finally;
1871 }
1872 else {
1873 PyErr_Clear();
1874 }
1875 }
1876
1877 if (t) {
1878 if (PyString_Check(t)) {
1879 res = save_global(self, args, t);
1880 goto finally;
1881 }
1882
1883 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001884 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001885 "be a tuple", "O", __reduce__);
1886 goto finally;
1887 }
1888
1889 size = PyTuple_Size(t);
1890
1891 if ((size != 3) && (size != 2)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001892 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001893 "contain only two or three elements", "O", __reduce__);
1894 goto finally;
1895 }
1896
1897 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001898
Guido van Rossum60456fd1997-04-09 17:36:32 +00001899 arg_tup = PyTuple_GET_ITEM(t, 1);
1900
1901 if (size > 2) {
1902 state = PyTuple_GET_ITEM(t, 2);
1903 }
1904
Guido van Rossum053b8df1998-11-25 16:18:00 +00001905 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001906 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001907 "returned by %s must be a tuple", "O", __reduce__);
1908 goto finally;
1909 }
1910
1911 res = save_reduce(self, callable, arg_tup, state, args);
1912 goto finally;
1913 }
1914
Guido van Rossumc03158b1999-06-09 15:23:31 +00001915 PyErr_SetObject(UnpickleableError, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001916
1917finally:
1918 Py_XDECREF(py_ob_id);
1919 Py_XDECREF(__reduce__);
1920 Py_XDECREF(t);
1921
1922 return res;
1923}
1924
1925
1926static int
1927dump(Picklerobject *self, PyObject *args) {
1928 static char stop = STOP;
1929
1930 if (save(self, args, 0) < 0)
1931 return -1;
1932
1933 if ((*self->write_func)(self, &stop, 1) < 0)
1934 return -1;
1935
1936 if ((*self->write_func)(self, NULL, 0) < 0)
1937 return -1;
1938
1939 return 0;
1940}
1941
1942static PyObject *
Guido van Rossum053b8df1998-11-25 16:18:00 +00001943Pickle_clear_memo(Picklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00001944 if (args && ! PyArg_ParseTuple(args,":clear_memo")) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001945 if (self->memo) PyDict_Clear(self->memo);
1946 Py_INCREF(Py_None);
1947 return Py_None;
1948}
1949
1950static PyObject *
1951Pickle_getvalue(Picklerobject *self, PyObject *args) {
1952 int l, i, rsize, ssize, clear=1, lm;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001953 long ik;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001954 PyObject *k, *r;
1955 char *s, *p, *have_get;
1956 Pdata *data;
1957
Guido van Rossum43713e52000-02-29 13:59:29 +00001958 if (args && ! PyArg_ParseTuple(args,"|i:getvalue",&clear)) return NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001959
1960 /* Check to make sure we are based on a list */
1961 if (! Pdata_Check(self->file)) {
1962 PyErr_SetString(PicklingError,
1963 "Attempt to getvalue a non-list-based pickler");
1964 return NULL;
1965 }
1966
1967 /* flush write buffer */
1968 if (write_other(self, NULL, 0) < 0) return NULL;
1969
1970 data=(Pdata*)self->file;
1971 l=data->length;
1972
1973 /* set up an array to hold get/put status */
1974 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
1975 lm++;
1976 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
1977 memset(have_get,0,lm);
1978
1979 /* Scan for gets. */
1980 for (rsize=0, i=l; --i >= 0; ) {
1981 k=data->data[i];
1982
1983 if (PyString_Check(k)) {
1984 rsize += PyString_GET_SIZE(k);
1985 }
1986
1987 else if (PyInt_Check(k)) { /* put */
1988 ik=PyInt_AS_LONG((PyIntObject*)k);
1989 if (ik >= lm || ik==0) {
1990 PyErr_SetString(PicklingError,
1991 "Invalid get data");
1992 return NULL;
1993 }
1994 if (have_get[ik]) { /* with matching get */
1995 if (ik < 256) rsize += 2;
1996 else rsize+=5;
1997 }
1998 }
1999
2000 else if (! (PyTuple_Check(k) &&
2001 PyTuple_GET_SIZE(k) == 2 &&
2002 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
2003 ) {
2004 PyErr_SetString(PicklingError,
2005 "Unexpected data in internal list");
2006 return NULL;
2007 }
2008
2009 else { /* put */
2010 ik=PyInt_AS_LONG((PyIntObject*)k);
2011 if (ik >= lm || ik==0) {
2012 PyErr_SetString(PicklingError,
2013 "Invalid get data");
2014 return NULL;
2015 }
2016 have_get[ik]=1;
2017 if (ik < 256) rsize += 2;
2018 else rsize+=5;
2019 }
2020
2021 }
2022
2023 /* Now generate the result */
2024 UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
2025 s=PyString_AS_STRING((PyStringObject*)r);
2026
2027 for (i=0; i<l; i++) {
2028 k=data->data[i];
2029
2030 if (PyString_Check(k)) {
2031 ssize=PyString_GET_SIZE(k);
2032 if (ssize) {
2033 p=PyString_AS_STRING((PyStringObject*)k);
2034 while (--ssize >= 0) *s++=*p++;
2035 }
2036 }
2037
2038 else if (PyTuple_Check(k)) { /* get */
2039 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
2040 if (ik < 256) {
2041 *s++ = BINGET;
2042 *s++ = (int)(ik & 0xff);
2043 }
2044 else {
2045 *s++ = LONG_BINGET;
2046 *s++ = (int)(ik & 0xff);
2047 *s++ = (int)((ik >> 8) & 0xff);
2048 *s++ = (int)((ik >> 16) & 0xff);
2049 *s++ = (int)((ik >> 24) & 0xff);
2050 }
2051 }
2052
2053 else { /* put */
2054 ik=PyInt_AS_LONG((PyIntObject*)k);
2055
2056 if (have_get[ik]) { /* with matching get */
2057 if (ik < 256) {
2058 *s++ = BINPUT;
2059 *s++ = (int)(ik & 0xff);
2060 }
2061 else {
2062 *s++ = LONG_BINPUT;
2063 *s++ = (int)(ik & 0xff);
2064 *s++ = (int)((ik >> 8) & 0xff);
2065 *s++ = (int)((ik >> 16) & 0xff);
2066 *s++ = (int)((ik >> 24) & 0xff);
2067 }
2068 }
2069 }
2070
2071 }
2072
2073 if (clear) {
2074 PyDict_Clear(self->memo);
2075 Pdata_clear(data,0);
2076 }
2077
2078 free(have_get);
2079 return r;
2080err:
2081 free(have_get);
2082 return NULL;
2083}
2084
2085static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002086Pickler_dump(Picklerobject *self, PyObject *args) {
2087 PyObject *ob;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002088 int get=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002089
Guido van Rossum43713e52000-02-29 13:59:29 +00002090 UNLESS (PyArg_ParseTuple(args, "O|i:dump", &ob, &get))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002091 return NULL;
2092
2093 if (dump(self, ob) < 0)
2094 return NULL;
2095
Guido van Rossum053b8df1998-11-25 16:18:00 +00002096 if (get) return Pickle_getvalue(self, NULL);
2097
2098 Py_INCREF(self);
2099 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002100}
2101
2102
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002103static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00002104 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002105 "dump(object) --"
2106 "Write an object in pickle format to the object's pickle stream\n"
2107 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00002108 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002109 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum053b8df1998-11-25 16:18:00 +00002110 {"getvalue", (PyCFunction)Pickle_getvalue, 1,
2111 "getvalue() -- Finish picking a list-based pickle"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002112 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002113};
2114
2115
2116static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002117newPicklerobject(PyObject *file, int bin) {
2118 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002119
Guido van Rossumb18618d2000-05-03 23:44:39 +00002120 UNLESS (self = PyObject_New(Picklerobject, &Picklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002121 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002122
2123 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002124 self->write = NULL;
2125 self->memo = NULL;
2126 self->arg = NULL;
2127 self->pers_func = NULL;
2128 self->inst_pers_func = NULL;
2129 self->write_buf = NULL;
2130 self->bin = bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002131 self->fast = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002132 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002133 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002134
Guido van Rossum053b8df1998-11-25 16:18:00 +00002135 if (file)
2136 Py_INCREF(file);
2137 else
Guido van Rossum50f385c1998-12-04 18:48:44 +00002138 file=Pdata_New();
Guido van Rossum83addc72000-04-21 20:49:36 +00002139
2140 UNLESS (self->file = file)
2141 goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002142
Guido van Rossum83addc72000-04-21 20:49:36 +00002143 UNLESS (self->memo = PyDict_New())
2144 goto err;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002145
Guido van Rossum60456fd1997-04-09 17:36:32 +00002146 if (PyFile_Check(file)) {
2147 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002148 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00002149 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
2150 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002151 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002152 self->write_func = write_file;
2153 }
2154 else if (PycStringIO_OutputCheck(file)) {
2155 self->write_func = write_cStringIO;
2156 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002157 else if (file == Py_None) {
2158 self->write_func = write_none;
2159 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002160 else {
2161 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002162
Guido van Rossum053b8df1998-11-25 16:18:00 +00002163 if (! Pdata_Check(file)) {
2164 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002165 PyErr_Clear();
2166 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002167 "attribute");
2168 goto err;
2169 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002170 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002171
Guido van Rossum053b8df1998-11-25 16:18:00 +00002172 UNLESS (self->write_buf =
Guido van Rossum60456fd1997-04-09 17:36:32 +00002173 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
2174 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002175 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002176 }
2177 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002178
Guido van Rossum053b8df1998-11-25 16:18:00 +00002179 if (PyEval_GetRestricted()) {
2180 /* Restricted execution, get private tables */
2181 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002182
Guido van Rossum053b8df1998-11-25 16:18:00 +00002183 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2184 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2185 Py_DECREF(m);
2186 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002187 }
2188 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002189 self->dispatch_table=dispatch_table;
2190 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002191 }
2192
Guido van Rossum60456fd1997-04-09 17:36:32 +00002193 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002194
2195err:
2196 Py_DECREF((PyObject *)self);
2197 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002198}
2199
2200
2201static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002202get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002203 PyObject *file=NULL;
2204 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002205
Guido van Rossum053b8df1998-11-25 16:18:00 +00002206 bin=1;
Guido van Rossum43713e52000-02-29 13:59:29 +00002207 if (! PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002208 PyErr_Clear();
2209 bin=0;
Guido van Rossum43713e52000-02-29 13:59:29 +00002210 if (! PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002211 return NULL;
2212 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002213 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002214}
2215
2216
2217static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002218Pickler_dealloc(Picklerobject *self) {
2219 Py_XDECREF(self->write);
2220 Py_XDECREF(self->memo);
2221 Py_XDECREF(self->arg);
2222 Py_XDECREF(self->file);
2223 Py_XDECREF(self->pers_func);
2224 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002225 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002226
2227 if (self->write_buf) {
2228 free(self->write_buf);
2229 }
2230
Guido van Rossumb18618d2000-05-03 23:44:39 +00002231 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002232}
2233
2234
2235static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002236Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002237
2238 switch (*name) {
2239 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002240 if (strcmp(name, "persistent_id") == 0) {
2241 if (!self->pers_func) {
2242 PyErr_SetString(PyExc_AttributeError, name);
2243 return NULL;
2244 }
2245
2246 Py_INCREF(self->pers_func);
2247 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002248 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002249 break;
2250 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002251 if (strcmp(name, "memo") == 0) {
2252 if (!self->memo) {
2253 PyErr_SetString(PyExc_AttributeError, name);
2254 return NULL;
2255 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002256
Guido van Rossum60456fd1997-04-09 17:36:32 +00002257 Py_INCREF(self->memo);
2258 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002259 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002260 break;
2261 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002262 if (strcmp(name, "PicklingError") == 0) {
2263 Py_INCREF(PicklingError);
2264 return PicklingError;
2265 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002266 break;
2267 case 'b':
2268 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002269 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002270 break;
2271 case 'f':
2272 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002273 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002274 break;
2275 case 'g':
2276 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2277 PyErr_SetString(PyExc_AttributeError, name);
2278 return NULL;
2279 }
2280 break;
2281 }
2282 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002283}
2284
2285
2286int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002287Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002288
Guido van Rossum053b8df1998-11-25 16:18:00 +00002289 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002290 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002291 "attribute deletion is not supported");
2292 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002293 }
2294
Guido van Rossum60456fd1997-04-09 17:36:32 +00002295 if (strcmp(name, "persistent_id") == 0) {
2296 Py_XDECREF(self->pers_func);
2297 self->pers_func = value;
2298 Py_INCREF(value);
2299 return 0;
2300 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002301
Guido van Rossum60456fd1997-04-09 17:36:32 +00002302 if (strcmp(name, "inst_persistent_id") == 0) {
2303 Py_XDECREF(self->inst_pers_func);
2304 self->inst_pers_func = value;
2305 Py_INCREF(value);
2306 return 0;
2307 }
2308
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002309 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002310 if (! PyDict_Check(value)) {
2311 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2312 return -1;
2313 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002314 Py_XDECREF(self->memo);
2315 self->memo = value;
2316 Py_INCREF(value);
2317 return 0;
2318 }
2319
Guido van Rossum053b8df1998-11-25 16:18:00 +00002320 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002321 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002322 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002323 }
2324
Guido van Rossum053b8df1998-11-25 16:18:00 +00002325 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002326 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002327 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002328 }
2329
Guido van Rossum60456fd1997-04-09 17:36:32 +00002330 PyErr_SetString(PyExc_AttributeError, name);
2331 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002332}
2333
2334
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002335static char Picklertype__doc__[] =
2336"Objects that know how to pickle objects\n"
2337;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002338
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002339static PyTypeObject Picklertype = {
2340 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002341 0, /*ob_size*/
2342 "Pickler", /*tp_name*/
2343 sizeof(Picklerobject), /*tp_basicsize*/
2344 0, /*tp_itemsize*/
2345 /* methods */
2346 (destructor)Pickler_dealloc, /*tp_dealloc*/
2347 (printfunc)0, /*tp_print*/
2348 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2349 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2350 (cmpfunc)0, /*tp_compare*/
2351 (reprfunc)0, /*tp_repr*/
2352 0, /*tp_as_number*/
2353 0, /*tp_as_sequence*/
2354 0, /*tp_as_mapping*/
2355 (hashfunc)0, /*tp_hash*/
2356 (ternaryfunc)0, /*tp_call*/
2357 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002358
Guido van Rossum60456fd1997-04-09 17:36:32 +00002359 /* Space for future expansion */
2360 0L,0L,0L,0L,
2361 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002362};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002363
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002364static PyObject *
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002365find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002366 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002367
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002368 if (fc) {
2369 if (fc==Py_None) {
2370 PyErr_SetString(UnpicklingError,
2371 "Global and instance pickles are not supported.");
2372 return NULL;
2373 }
2374 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2375 }
2376
Jeremy Hyltond1055231998-08-11 19:52:51 +00002377 module = PySys_GetObject("modules");
2378 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002379 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002380
2381 module = PyDict_GetItem(module, py_module_name);
2382 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002383 module = PyImport_Import(py_module_name);
2384 if (!module)
2385 return NULL;
2386 global = PyObject_GetAttr(module, py_global_name);
2387 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002388 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002389 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002390 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002391 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002392 char buf[256 + 37];
2393 sprintf(buf, "Failed to import class %.128s from module %.128s",
2394 PyString_AS_STRING((PyStringObject*)py_global_name),
2395 PyString_AS_STRING((PyStringObject*)py_module_name));
2396 PyErr_SetString(PyExc_SystemError, buf);
2397 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002398 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002399 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002400}
2401
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002402static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002403marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002404 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002405 PyErr_SetString(UnpicklingError, "could not find MARK");
2406 return -1;
2407 }
2408
2409 return self->marks[--self->num_marks];
2410}
2411
2412
2413static int
2414load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002415 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002416 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002417}
2418
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002419static int
Thomas Wouters58d05102000-07-24 14:43:35 +00002420bad_readline(void) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002421 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2422 return -1;
2423}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002424
2425static int
2426load_int(Unpicklerobject *self) {
2427 PyObject *py_int = 0;
2428 char *endptr, *s;
2429 int len, res = -1;
2430 long l;
2431
2432 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002433 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002434 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002435
2436 errno = 0;
2437 l = strtol(s, &endptr, 0);
2438
2439 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2440 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002441 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002442 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002443 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002444
Guido van Rossum053b8df1998-11-25 16:18:00 +00002445 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2446 PyErr_SetString(PyExc_ValueError,
2447 "could not convert string to int");
2448 goto finally;
2449 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002450 }
2451 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002452 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002453 }
2454
Guido van Rossum053b8df1998-11-25 16:18:00 +00002455 free(s);
2456 PDATA_PUSH(self->stack, py_int, -1);
2457 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002458
2459finally:
2460 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002461
2462 return res;
2463}
2464
2465
2466static long
2467calc_binint(char *s, int x) {
2468 unsigned char c;
2469 int i;
2470 long l;
2471
2472 for (i = 0, l = 0L; i < x; i++) {
2473 c = (unsigned char)s[i];
2474 l |= (long)c << (i * 8);
2475 }
2476
2477 return l;
2478}
2479
2480
2481static int
2482load_binintx(Unpicklerobject *self, char *s, int x) {
2483 PyObject *py_int = 0;
2484 long l;
2485
2486 l = calc_binint(s, x);
2487
Guido van Rossum053b8df1998-11-25 16:18:00 +00002488 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002489 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002490
Guido van Rossum053b8df1998-11-25 16:18:00 +00002491 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002492 return 0;
2493}
2494
2495
2496static int
2497load_binint(Unpicklerobject *self) {
2498 char *s;
2499
2500 if ((*self->read_func)(self, &s, 4) < 0)
2501 return -1;
2502
2503 return load_binintx(self, s, 4);
2504}
2505
2506
2507static int
2508load_binint1(Unpicklerobject *self) {
2509 char *s;
2510
2511 if ((*self->read_func)(self, &s, 1) < 0)
2512 return -1;
2513
2514 return load_binintx(self, s, 1);
2515}
2516
2517
2518static int
2519load_binint2(Unpicklerobject *self) {
2520 char *s;
2521
2522 if ((*self->read_func)(self, &s, 2) < 0)
2523 return -1;
2524
2525 return load_binintx(self, s, 2);
2526}
2527
2528static int
2529load_long(Unpicklerobject *self) {
2530 PyObject *l = 0;
2531 char *end, *s;
2532 int len, res = -1;
2533
Guido van Rossum60456fd1997-04-09 17:36:32 +00002534 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002535 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002536 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002537
Guido van Rossum053b8df1998-11-25 16:18:00 +00002538 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002539 goto finally;
2540
Guido van Rossum053b8df1998-11-25 16:18:00 +00002541 free(s);
2542 PDATA_PUSH(self->stack, l, -1);
2543 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002544
2545finally:
2546 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002547
2548 return res;
2549}
2550
2551
2552static int
2553load_float(Unpicklerobject *self) {
2554 PyObject *py_float = 0;
2555 char *endptr, *s;
2556 int len, res = -1;
2557 double d;
2558
2559 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002560 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002561 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002562
2563 errno = 0;
2564 d = strtod(s, &endptr);
2565
2566 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2567 PyErr_SetString(PyExc_ValueError,
2568 "could not convert string to float");
2569 goto finally;
2570 }
2571
Guido van Rossum053b8df1998-11-25 16:18:00 +00002572 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002573 goto finally;
2574
Guido van Rossum053b8df1998-11-25 16:18:00 +00002575 free(s);
2576 PDATA_PUSH(self->stack, py_float, -1);
2577 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002578
2579finally:
2580 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002581
2582 return res;
2583}
2584
Guido van Rossum60456fd1997-04-09 17:36:32 +00002585static int
2586load_binfloat(Unpicklerobject *self) {
2587 PyObject *py_float = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002588 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002589 long fhi, flo;
2590 double x;
2591 char *p;
2592
2593 if ((*self->read_func)(self, &p, 8) < 0)
2594 return -1;
2595
2596 /* First byte */
2597 s = (*p>>7) & 1;
2598 e = (*p & 0x7F) << 4;
2599 p++;
2600
2601 /* Second byte */
2602 e |= (*p>>4) & 0xF;
2603 fhi = (*p & 0xF) << 24;
2604 p++;
2605
2606 /* Third byte */
2607 fhi |= (*p & 0xFF) << 16;
2608 p++;
2609
2610 /* Fourth byte */
2611 fhi |= (*p & 0xFF) << 8;
2612 p++;
2613
2614 /* Fifth byte */
2615 fhi |= *p & 0xFF;
2616 p++;
2617
2618 /* Sixth byte */
2619 flo = (*p & 0xFF) << 16;
2620 p++;
2621
2622 /* Seventh byte */
2623 flo |= (*p & 0xFF) << 8;
2624 p++;
2625
2626 /* Eighth byte */
2627 flo |= *p & 0xFF;
2628
2629 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2630 x /= 268435456.0; /* 2**28 */
2631
2632 /* XXX This sadly ignores Inf/NaN */
2633 if (e == 0)
2634 e = -1022;
2635 else {
2636 x += 1.0;
2637 e -= 1023;
2638 }
2639 x = ldexp(x, e);
2640
2641 if (s)
2642 x = -x;
2643
Guido van Rossum053b8df1998-11-25 16:18:00 +00002644 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002645
Guido van Rossum053b8df1998-11-25 16:18:00 +00002646 PDATA_PUSH(self->stack, py_float, -1);
2647 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002648}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002649
2650static int
2651load_string(Unpicklerobject *self) {
2652 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002653 int len, res = -1, nslash;
2654 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002655
2656 static PyObject *eval_dict = 0;
2657
2658 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002659 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002660 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002661
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002662 /* Check for unquoted quotes (evil strings) */
2663 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002664 if (q != '"' && q != '\'') goto insecure;
2665 for (p=s+1, nslash=0; *p; p++) {
2666 if (*p==q && nslash%2==0) break;
2667 if (*p=='\\') nslash++;
2668 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002669 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002670 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002671 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002672 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002673 }
2674 else goto insecure;
2675 /********************************************/
2676
Guido van Rossum053b8df1998-11-25 16:18:00 +00002677 UNLESS (eval_dict)
2678 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002679 goto finally;
2680
Guido van Rossum053b8df1998-11-25 16:18:00 +00002681 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002682 goto finally;
2683
Guido van Rossum053b8df1998-11-25 16:18:00 +00002684 free(s);
2685 PDATA_PUSH(self->stack, str, -1);
2686 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002687
2688finally:
2689 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002690
2691 return res;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002692
2693insecure:
2694 free(s);
2695 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2696 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002697}
2698
2699
2700static int
2701load_binstring(Unpicklerobject *self) {
2702 PyObject *py_string = 0;
2703 long l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002704 char *s;
2705
Guido van Rossum053b8df1998-11-25 16:18:00 +00002706 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002707
2708 l = calc_binint(s, 4);
2709
2710 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002711 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002712
Guido van Rossum053b8df1998-11-25 16:18:00 +00002713 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2714 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002715
Guido van Rossum053b8df1998-11-25 16:18:00 +00002716 PDATA_PUSH(self->stack, py_string, -1);
2717 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002718}
2719
2720
2721static int
2722load_short_binstring(Unpicklerobject *self) {
2723 PyObject *py_string = 0;
2724 unsigned char l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002725 char *s;
2726
2727 if ((*self->read_func)(self, &s, 1) < 0)
2728 return -1;
2729
2730 l = (unsigned char)s[0];
2731
Guido van Rossum053b8df1998-11-25 16:18:00 +00002732 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002733
Guido van Rossum053b8df1998-11-25 16:18:00 +00002734 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002735
Guido van Rossum053b8df1998-11-25 16:18:00 +00002736 PDATA_PUSH(self->stack, py_string, -1);
2737 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002738}
2739
2740
2741static int
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00002742load_unicode(Unpicklerobject *self) {
2743 PyObject *str = 0;
2744 int len, res = -1;
2745 char *s;
2746
2747 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2748 if (len < 2) return bad_readline();
2749
2750 UNLESS (str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL))
2751 goto finally;
2752
2753 PDATA_PUSH(self->stack, str, -1);
2754 return 0;
2755
2756finally:
2757 return res;
2758}
2759
2760
2761static int
2762load_binunicode(Unpicklerobject *self) {
2763 PyObject *unicode;
2764 long l;
2765 char *s;
2766
2767 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2768
2769 l = calc_binint(s, 4);
2770
2771 if ((*self->read_func)(self, &s, l) < 0)
2772 return -1;
2773
2774 UNLESS (unicode = PyUnicode_DecodeUTF8(s, l, NULL))
2775 return -1;
2776
2777 PDATA_PUSH(self->stack, unicode, -1);
2778 return 0;
2779}
2780
2781
2782static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002783load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002784 PyObject *tup;
2785 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002786
Guido van Rossum053b8df1998-11-25 16:18:00 +00002787 if ((i = marker(self)) < 0) return -1;
2788 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2789 PDATA_PUSH(self->stack, tup, -1);
2790 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002791}
2792
2793static int
2794load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002795 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002796
Guido van Rossum053b8df1998-11-25 16:18:00 +00002797 UNLESS (tup=PyTuple_New(0)) return -1;
2798 PDATA_PUSH(self->stack, tup, -1);
2799 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002800}
2801
2802static int
2803load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002804 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002805
Guido van Rossum053b8df1998-11-25 16:18:00 +00002806 UNLESS (list=PyList_New(0)) return -1;
2807 PDATA_PUSH(self->stack, list, -1);
2808 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002809}
2810
2811static int
2812load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002813 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002814
Guido van Rossum053b8df1998-11-25 16:18:00 +00002815 UNLESS (dict=PyDict_New()) return -1;
2816 PDATA_PUSH(self->stack, dict, -1);
2817 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002818}
2819
2820
2821static int
2822load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002823 PyObject *list = 0;
2824 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002825
Guido van Rossum053b8df1998-11-25 16:18:00 +00002826 if ((i = marker(self)) < 0) return -1;
2827 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2828 PDATA_PUSH(self->stack, list, -1);
2829 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002830}
2831
2832static int
2833load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002834 PyObject *dict, *key, *value;
2835 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002836
Guido van Rossum053b8df1998-11-25 16:18:00 +00002837 if ((i = marker(self)) < 0) return -1;
2838 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002839
Guido van Rossum053b8df1998-11-25 16:18:00 +00002840 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002841
Guido van Rossum053b8df1998-11-25 16:18:00 +00002842 for (k = i+1; k < j; k += 2) {
2843 key =self->stack->data[k-1];
2844 value=self->stack->data[k ];
2845 if (PyDict_SetItem(dict, key, value) < 0) {
2846 Py_DECREF(dict);
2847 return -1;
2848 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002849 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002850 Pdata_clear(self->stack, i);
2851 PDATA_PUSH(self->stack, dict, -1);
2852 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002853}
2854
2855static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002856Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002857 int has_key;
2858 PyObject *safe=0, *r=0;
2859
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002860 if (PyClass_Check(cls)) {
2861 int l;
2862
Jeremy Hylton03657cf2000-07-12 13:05:33 +00002863 if ((l=PyObject_Size(args)) < 0) goto err;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002864 UNLESS (l) {
2865 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002866
Guido van Rossum053b8df1998-11-25 16:18:00 +00002867 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2868 /* We have a class with no __getinitargs__, so bypass usual
2869 construction */
2870 PyInstanceObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002871
Guido van Rossum053b8df1998-11-25 16:18:00 +00002872 PyErr_Clear();
Guido van Rossumb18618d2000-05-03 23:44:39 +00002873 UNLESS (inst=PyObject_New(PyInstanceObject, &PyInstance_Type))
Guido van Rossum053b8df1998-11-25 16:18:00 +00002874 goto err;
2875 inst->in_class=(PyClassObject*)cls;
2876 Py_INCREF(cls);
2877 UNLESS (inst->in_dict=PyDict_New()) {
Neil Schemenauer5196c582000-10-04 16:22:26 +00002878 inst = (PyInstanceObject *) PyObject_AS_GC(inst);
2879 PyObject_DEL(inst);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002880 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
Guido van Rossumebba4202000-09-07 14:35:37 +00004390init_stuff(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)
Thomas Wouters58d05102000-07-24 14:43:35 +00004519initcPickle(void) {
Guido van Rossumebba4202000-09-07 14:35:37 +00004520 PyObject *m, *d, *di, *v, *k;
4521 int i;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004522 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004523 PyObject *format_version;
4524 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004525
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004526 Picklertype.ob_type = &PyType_Type;
4527 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004528 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004529
Guido van Rossumebba4202000-09-07 14:35:37 +00004530 /* Initialize some pieces. We need to do this before module creation,
4531 so we're forced to use a temporary dictionary. :(
4532 */
4533 di=PyDict_New();
4534 if (!di) return;
4535 if (init_stuff(di) < 0) return;
4536
Guido van Rossum60456fd1997-04-09 17:36:32 +00004537 /* Create the module and add the functions */
4538 m = Py_InitModule4("cPickle", cPickle_methods,
4539 cPickle_module_documentation,
4540 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004541
Guido van Rossum60456fd1997-04-09 17:36:32 +00004542 /* Add some symbolic constants to the module */
4543 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004544 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004545 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004546
Guido van Rossumebba4202000-09-07 14:35:37 +00004547 /* Copy data from di. Waaa. */
4548 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
4549 if (PyObject_SetItem(d, k, v) < 0) {
4550 Py_DECREF(di);
4551 return;
4552 }
4553 }
4554 Py_DECREF(di);
4555
Guido van Rossum60456fd1997-04-09 17:36:32 +00004556 format_version = PyString_FromString("1.3");
4557 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004558
Guido van Rossum60456fd1997-04-09 17:36:32 +00004559 PyDict_SetItemString(d, "format_version", format_version);
4560 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004561 Py_XDECREF(format_version);
4562 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004563}