blob: b64b1f137799a8bc3f75f6aebefaffcb02a00e29 [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()) {
2878 Py_DECREF(inst);
2879 goto err;
2880 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002881 PyObject_GC_Init(inst);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002882 return (PyObject *)inst;
2883 }
2884 Py_DECREF(__getinitargs__);
2885 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002886
Guido van Rossum053b8df1998-11-25 16:18:00 +00002887 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002888 else goto err;
2889 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002890
2891
2892 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2893 goto err;
2894
2895 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002896 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002897 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002898 cPickle_ErrFormat(UnpicklingError,
2899 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002900 Py_XDECREF(safe);
2901 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002902 }
2903
Guido van Rossum053b8df1998-11-25 16:18:00 +00002904 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002905 /* Special case, call cls.__basicnew__() */
2906 PyObject *basicnew;
2907
Guido van Rossum053b8df1998-11-25 16:18:00 +00002908 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002909 r=PyObject_CallObject(basicnew, NULL);
2910 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002911 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002912 }
2913
Guido van Rossum053b8df1998-11-25 16:18:00 +00002914 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002915
Guido van Rossum60456fd1997-04-09 17:36:32 +00002916err:
2917 {
2918 PyObject *tp, *v, *tb;
2919
2920 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002921 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2922 Py_XDECREF(v);
2923 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002924 }
2925 PyErr_Restore(tp,v,tb);
2926 }
2927 return NULL;
2928}
2929
2930
2931static int
2932load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002933 PyObject *class, *tup, *obj=0;
2934 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002935
Guido van Rossum053b8df1998-11-25 16:18:00 +00002936 if ((i = marker(self)) < 0) return -1;
2937 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
2938 PDATA_POP(self->stack, class);
2939 if (class) {
2940 obj = Instance_New(class, tup);
2941 Py_DECREF(class);
2942 }
2943 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002944
Guido van Rossum053b8df1998-11-25 16:18:00 +00002945 if (! obj) return -1;
2946 PDATA_PUSH(self->stack, obj, -1);
2947 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002948}
2949
2950
2951static int
2952load_inst(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002953 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002954 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002955 char *s;
2956
Guido van Rossum053b8df1998-11-25 16:18:00 +00002957 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002958
Guido van Rossum053b8df1998-11-25 16:18:00 +00002959 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002960 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002961 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002962
Guido van Rossum053b8df1998-11-25 16:18:00 +00002963 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002964 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002965 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00002966 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002967 Py_DECREF(class_name);
2968 }
2969 }
2970 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002971
Guido van Rossum053b8df1998-11-25 16:18:00 +00002972 if (! class) return -1;
2973
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002974 if ((tup=Pdata_popTuple(self->stack, i))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002975 obj = Instance_New(class, tup);
2976 Py_DECREF(tup);
2977 }
2978 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002979
Guido van Rossum053b8df1998-11-25 16:18:00 +00002980 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002981
Guido van Rossum053b8df1998-11-25 16:18:00 +00002982 PDATA_PUSH(self->stack, obj, -1);
2983 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002984}
2985
2986
2987static int
2988load_global(Unpicklerobject *self) {
2989 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002990 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002991 char *s;
2992
Guido van Rossum053b8df1998-11-25 16:18:00 +00002993 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002994 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002995 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002996
Guido van Rossum053b8df1998-11-25 16:18:00 +00002997 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002998 if (len < 2) return bad_readline();
Guido van Rossumc3be1a31999-06-15 14:36:59 +00002999 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00003000 class = find_class(module_name, class_name, self->find_class);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003001 Py_DECREF(class_name);
3002 }
3003 }
3004 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003005
Guido van Rossum053b8df1998-11-25 16:18:00 +00003006 if (! class) return -1;
3007 PDATA_PUSH(self->stack, class, -1);
3008 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003009}
3010
3011
3012static int
3013load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003014 PyObject *pid = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003015 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003016 char *s;
3017
3018 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003019 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003020 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003021
Guido van Rossum053b8df1998-11-25 16:18:00 +00003022 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003023
Guido van Rossum053b8df1998-11-25 16:18:00 +00003024 if (PyList_Check(self->pers_func)) {
3025 if (PyList_Append(self->pers_func, pid) < 0) {
3026 Py_DECREF(pid);
3027 return -1;
3028 }
3029 }
3030 else {
3031 ARG_TUP(self, pid);
3032 if (self->arg) {
3033 pid = PyObject_CallObject(self->pers_func, self->arg);
3034 FREE_ARG_TUP(self);
3035 }
3036 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003037
Guido van Rossum053b8df1998-11-25 16:18:00 +00003038 if (! pid) return -1;
3039
3040 PDATA_PUSH(self->stack, pid, -1);
3041 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003042 }
3043 else {
3044 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003045 "A load persistent id instruction was encountered,\n"
3046 "but no persistent_load function was specified.");
3047 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003048 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003049}
3050
Guido van Rossum60456fd1997-04-09 17:36:32 +00003051static int
3052load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003053 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003054
3055 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003056 PDATA_POP(self->stack, pid);
3057 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003058
Guido van Rossum053b8df1998-11-25 16:18:00 +00003059 if (PyList_Check(self->pers_func)) {
3060 if (PyList_Append(self->pers_func, pid) < 0) {
3061 Py_DECREF(pid);
3062 return -1;
3063 }
3064 }
3065 else {
3066 ARG_TUP(self, pid);
3067 if (self->arg) {
3068 pid = PyObject_CallObject(self->pers_func, self->arg);
3069 FREE_ARG_TUP(self);
3070 }
3071 if (! pid) return -1;
3072 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003073
Guido van Rossum053b8df1998-11-25 16:18:00 +00003074 PDATA_PUSH(self->stack, pid, -1);
3075 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003076 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003077 else {
3078 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00003079 "A load persistent id instruction was encountered,\n"
3080 "but no persistent_load function was specified.");
3081 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003082 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003083}
3084
3085
3086static int
3087load_pop(Unpicklerobject *self) {
3088 int len;
3089
Guido van Rossum053b8df1998-11-25 16:18:00 +00003090 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003091
Guido van Rossumea2b7152000-05-09 18:14:50 +00003092 /* Note that we split the (pickle.py) stack into two stacks,
3093 an object stack and a mark stack. We have to be clever and
3094 pop the right one. We do this by looking at the top of the
3095 mark stack.
3096 */
3097
Guido van Rossum60456fd1997-04-09 17:36:32 +00003098 if ((self->num_marks > 0) &&
3099 (self->marks[self->num_marks - 1] == len))
3100 self->num_marks--;
Guido van Rossumea2b7152000-05-09 18:14:50 +00003101 else {
3102 len--;
3103 Py_DECREF(self->stack->data[len]);
3104 self->stack->length=len;
3105 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003106
3107 return 0;
3108}
3109
3110
3111static int
3112load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003113 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003114
3115 if ((i = marker(self)) < 0)
3116 return -1;
3117
Guido van Rossum053b8df1998-11-25 16:18:00 +00003118 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003119
3120 return 0;
3121}
3122
3123
3124static int
3125load_dup(Unpicklerobject *self) {
3126 PyObject *last;
3127 int len;
3128
Guido van Rossum053b8df1998-11-25 16:18:00 +00003129 if ((len = self->stack->length) <= 0) return stackUnderflow();
3130 last=self->stack->data[len-1];
3131 Py_INCREF(last);
3132 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003133 return 0;
3134}
3135
3136
3137static int
3138load_get(Unpicklerobject *self) {
3139 PyObject *py_str = 0, *value = 0;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003140 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003141 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003142 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003143
Guido van Rossum053b8df1998-11-25 16:18:00 +00003144 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003145 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003146
Guido van Rossum053b8df1998-11-25 16:18:00 +00003147 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3148
3149 value = PyDict_GetItem(self->memo, py_str);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003150 if (! value) {
3151 PyErr_SetObject(BadPickleGet, py_str);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003152 rc = -1;
3153 } else {
3154 PDATA_APPEND(self->stack, value, -1);
3155 rc = 0;
3156 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003157
Guido van Rossum2f80d961999-07-13 15:18:58 +00003158 Py_DECREF(py_str);
3159 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003160}
3161
3162
3163static int
3164load_binget(Unpicklerobject *self) {
3165 PyObject *py_key = 0, *value = 0;
3166 unsigned char key;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003167 char *s;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003168 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003169
Guido van Rossum053b8df1998-11-25 16:18:00 +00003170 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003171
3172 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003173 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3174
3175 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003176 if (! value) {
3177 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003178 rc = -1;
3179 } else {
3180 PDATA_APPEND(self->stack, value, -1);
3181 rc = 0;
3182 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003183
Guido van Rossum2f80d961999-07-13 15:18:58 +00003184 Py_DECREF(py_key);
3185 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003186}
3187
3188
3189static int
3190load_long_binget(Unpicklerobject *self) {
3191 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003192 unsigned char c;
3193 char *s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003194 long key;
Guido van Rossum2f80d961999-07-13 15:18:58 +00003195 int rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003196
Guido van Rossum053b8df1998-11-25 16:18:00 +00003197 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003198
3199 c = (unsigned char)s[0];
3200 key = (long)c;
3201 c = (unsigned char)s[1];
3202 key |= (long)c << 8;
3203 c = (unsigned char)s[2];
3204 key |= (long)c << 16;
3205 c = (unsigned char)s[3];
3206 key |= (long)c << 24;
3207
Guido van Rossum053b8df1998-11-25 16:18:00 +00003208 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3209
3210 value = PyDict_GetItem(self->memo, py_key);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003211 if (! value) {
3212 PyErr_SetObject(BadPickleGet, py_key);
Guido van Rossum2f80d961999-07-13 15:18:58 +00003213 rc = -1;
3214 } else {
3215 PDATA_APPEND(self->stack, value, -1);
3216 rc = 0;
3217 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003218
Guido van Rossum2f80d961999-07-13 15:18:58 +00003219 Py_DECREF(py_key);
3220 return rc;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003221}
3222
3223
3224static int
3225load_put(Unpicklerobject *self) {
3226 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003227 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003228 char *s;
3229
Guido van Rossum053b8df1998-11-25 16:18:00 +00003230 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003231 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003232 UNLESS (len=self->stack->length) return stackUnderflow();
3233 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3234 value=self->stack->data[len-1];
3235 l=PyDict_SetItem(self->memo, py_str, value);
3236 Py_DECREF(py_str);
3237 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003238}
3239
3240
3241static int
3242load_binput(Unpicklerobject *self) {
3243 PyObject *py_key = 0, *value = 0;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003244 unsigned char key;
3245 char *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003246 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003247
Guido van Rossum053b8df1998-11-25 16:18:00 +00003248 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3249 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003250
3251 key = (unsigned char)s[0];
3252
Guido van Rossum053b8df1998-11-25 16:18:00 +00003253 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3254 value=self->stack->data[len-1];
3255 len=PyDict_SetItem(self->memo, py_key, value);
3256 Py_DECREF(py_key);
3257 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003258}
3259
3260
3261static int
3262load_long_binput(Unpicklerobject *self) {
3263 PyObject *py_key = 0, *value = 0;
3264 long key;
Thomas Wouters3b6448f2000-07-22 23:56:07 +00003265 unsigned char c;
3266 char *s;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003267 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003268
Guido van Rossum053b8df1998-11-25 16:18:00 +00003269 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3270 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003271
3272 c = (unsigned char)s[0];
3273 key = (long)c;
3274 c = (unsigned char)s[1];
3275 key |= (long)c << 8;
3276 c = (unsigned char)s[2];
3277 key |= (long)c << 16;
3278 c = (unsigned char)s[3];
3279 key |= (long)c << 24;
3280
Guido van Rossum053b8df1998-11-25 16:18:00 +00003281 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3282 value=self->stack->data[len-1];
3283 len=PyDict_SetItem(self->memo, py_key, value);
3284 Py_DECREF(py_key);
3285 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003286}
3287
3288
3289static int
3290do_append(Unpicklerobject *self, int x) {
3291 PyObject *value = 0, *list = 0, *append_method = 0;
3292 int len, i;
3293
Guido van Rossum053b8df1998-11-25 16:18:00 +00003294 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3295 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003296
Guido van Rossum053b8df1998-11-25 16:18:00 +00003297 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003298
3299 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003300 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003301 int list_len;
3302
Guido van Rossum053b8df1998-11-25 16:18:00 +00003303 slice=Pdata_popList(self->stack, x);
3304 list_len = PyList_GET_SIZE(list);
3305 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003306 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003307 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003308 }
3309 else {
3310
Guido van Rossum053b8df1998-11-25 16:18:00 +00003311 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003312 return -1;
3313
3314 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003315 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003316
Guido van Rossum053b8df1998-11-25 16:18:00 +00003317 value=self->stack->data[i];
3318 junk=0;
3319 ARG_TUP(self, value);
3320 if (self->arg) {
3321 junk = PyObject_CallObject(append_method, self->arg);
3322 FREE_ARG_TUP(self);
3323 }
3324 if (! junk) {
3325 Pdata_clear(self->stack, i+1);
3326 self->stack->length=x;
3327 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003328 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003329 }
3330 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003331 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003332 self->stack->length=x;
3333 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003334 }
3335
Guido van Rossum60456fd1997-04-09 17:36:32 +00003336 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003337}
3338
3339
3340static int
3341load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003342 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003343}
3344
3345
3346static int
3347load_appends(Unpicklerobject *self) {
3348 return do_append(self, marker(self));
3349}
3350
3351
3352static int
3353do_setitems(Unpicklerobject *self, int x) {
3354 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003355 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003356
Guido van Rossum053b8df1998-11-25 16:18:00 +00003357 UNLESS ((len=self->stack->length) >= x
3358 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003359
Guido van Rossum053b8df1998-11-25 16:18:00 +00003360 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003361
Guido van Rossum053b8df1998-11-25 16:18:00 +00003362 for (i = x+1; i < len; i += 2) {
3363 key =self->stack->data[i-1];
3364 value=self->stack->data[i ];
3365 if (PyObject_SetItem(dict, key, value) < 0) {
3366 r=-1;
3367 break;
3368 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003369 }
3370
Guido van Rossum053b8df1998-11-25 16:18:00 +00003371 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003372
Guido van Rossum053b8df1998-11-25 16:18:00 +00003373 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003374}
3375
3376
3377static int
3378load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003379 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003380}
3381
Guido van Rossum60456fd1997-04-09 17:36:32 +00003382static int
3383load_setitems(Unpicklerobject *self) {
3384 return do_setitems(self, marker(self));
3385}
3386
3387
3388static int
3389load_build(Unpicklerobject *self) {
3390 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3391 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003392 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003393
Guido van Rossum053b8df1998-11-25 16:18:00 +00003394 if (self->stack->length < 2) return stackUnderflow();
3395 PDATA_POP(self->stack, value);
3396 if (! value) return -1;
3397 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003398
Guido van Rossum053b8df1998-11-25 16:18:00 +00003399 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3400 ARG_TUP(self, value);
3401 if (self->arg) {
3402 junk = PyObject_CallObject(__setstate__, self->arg);
3403 FREE_ARG_TUP(self);
3404 }
3405 Py_DECREF(__setstate__);
3406 if (! junk) return -1;
3407 Py_DECREF(junk);
3408 return 0;
3409 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003410
Guido van Rossum053b8df1998-11-25 16:18:00 +00003411 PyErr_Clear();
3412 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003413 i = 0;
3414 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003415 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3416 r=-1;
3417 break;
3418 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003419 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003420 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003421 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003422 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003423
Guido van Rossum053b8df1998-11-25 16:18:00 +00003424 Py_XDECREF(value);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003425
Guido van Rossum053b8df1998-11-25 16:18:00 +00003426 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003427}
3428
3429
3430static int
3431load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003432 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003433
Guido van Rossumea2b7152000-05-09 18:14:50 +00003434 /* Note that we split the (pickle.py) stack into two stacks, an
3435 object stack and a mark stack. Here we push a mark onto the
3436 mark stack.
3437 */
3438
Guido van Rossum053b8df1998-11-25 16:18:00 +00003439 if ((self->num_marks + 1) >= self->marks_size) {
3440 s=self->marks_size+20;
3441 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003442 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003443 self->marks=(int *)malloc(s * sizeof(int));
3444 else
3445 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003446 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003447 PyErr_NoMemory();
3448 return -1;
3449 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003450 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003451 }
3452
Guido van Rossum053b8df1998-11-25 16:18:00 +00003453 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003454
3455 return 0;
3456}
3457
3458static int
3459load_reduce(Unpicklerobject *self) {
3460 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003461
Guido van Rossum053b8df1998-11-25 16:18:00 +00003462 PDATA_POP(self->stack, arg_tup);
3463 if (! arg_tup) return -1;
3464 PDATA_POP(self->stack, callable);
3465 if (callable) {
3466 ob = Instance_New(callable, arg_tup);
3467 Py_DECREF(callable);
3468 }
3469 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003470
Guido van Rossum053b8df1998-11-25 16:18:00 +00003471 if (! ob) return -1;
3472
3473 PDATA_PUSH(self->stack, ob, -1);
3474 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003475}
3476
3477static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003478load(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003479 PyObject *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003480 char *s;
3481
Guido van Rossum60456fd1997-04-09 17:36:32 +00003482 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003483 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003484
3485 while (1) {
3486 if ((*self->read_func)(self, &s, 1) < 0)
3487 break;
3488
3489 switch (s[0]) {
3490 case NONE:
3491 if (load_none(self) < 0)
3492 break;
3493 continue;
3494
3495 case BININT:
3496 if (load_binint(self) < 0)
3497 break;
3498 continue;
3499
3500 case BININT1:
3501 if (load_binint1(self) < 0)
3502 break;
3503 continue;
3504
3505 case BININT2:
3506 if (load_binint2(self) < 0)
3507 break;
3508 continue;
3509
3510 case INT:
3511 if (load_int(self) < 0)
3512 break;
3513 continue;
3514
3515 case LONG:
3516 if (load_long(self) < 0)
3517 break;
3518 continue;
3519
3520 case FLOAT:
3521 if (load_float(self) < 0)
3522 break;
3523 continue;
3524
Guido van Rossum60456fd1997-04-09 17:36:32 +00003525 case BINFLOAT:
3526 if (load_binfloat(self) < 0)
3527 break;
3528 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003529
3530 case BINSTRING:
3531 if (load_binstring(self) < 0)
3532 break;
3533 continue;
3534
3535 case SHORT_BINSTRING:
3536 if (load_short_binstring(self) < 0)
3537 break;
3538 continue;
3539
3540 case STRING:
3541 if (load_string(self) < 0)
3542 break;
3543 continue;
3544
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003545 case UNICODE:
3546 if (load_unicode(self) < 0)
3547 break;
3548 continue;
3549
3550 case BINUNICODE:
3551 if (load_binunicode(self) < 0)
3552 break;
3553 continue;
3554
Guido van Rossum60456fd1997-04-09 17:36:32 +00003555 case EMPTY_TUPLE:
3556 if (load_empty_tuple(self) < 0)
3557 break;
3558 continue;
3559
3560 case TUPLE:
3561 if (load_tuple(self) < 0)
3562 break;
3563 continue;
3564
3565 case EMPTY_LIST:
3566 if (load_empty_list(self) < 0)
3567 break;
3568 continue;
3569
3570 case LIST:
3571 if (load_list(self) < 0)
3572 break;
3573 continue;
3574
3575 case EMPTY_DICT:
3576 if (load_empty_dict(self) < 0)
3577 break;
3578 continue;
3579
3580 case DICT:
3581 if (load_dict(self) < 0)
3582 break;
3583 continue;
3584
3585 case OBJ:
3586 if (load_obj(self) < 0)
3587 break;
3588 continue;
3589
3590 case INST:
3591 if (load_inst(self) < 0)
3592 break;
3593 continue;
3594
3595 case GLOBAL:
3596 if (load_global(self) < 0)
3597 break;
3598 continue;
3599
3600 case APPEND:
3601 if (load_append(self) < 0)
3602 break;
3603 continue;
3604
3605 case APPENDS:
3606 if (load_appends(self) < 0)
3607 break;
3608 continue;
3609
3610 case BUILD:
3611 if (load_build(self) < 0)
3612 break;
3613 continue;
3614
3615 case DUP:
3616 if (load_dup(self) < 0)
3617 break;
3618 continue;
3619
3620 case BINGET:
3621 if (load_binget(self) < 0)
3622 break;
3623 continue;
3624
3625 case LONG_BINGET:
3626 if (load_long_binget(self) < 0)
3627 break;
3628 continue;
3629
3630 case GET:
3631 if (load_get(self) < 0)
3632 break;
3633 continue;
3634
3635 case MARK:
3636 if (load_mark(self) < 0)
3637 break;
3638 continue;
3639
3640 case BINPUT:
3641 if (load_binput(self) < 0)
3642 break;
3643 continue;
3644
3645 case LONG_BINPUT:
3646 if (load_long_binput(self) < 0)
3647 break;
3648 continue;
3649
3650 case PUT:
3651 if (load_put(self) < 0)
3652 break;
3653 continue;
3654
3655 case POP:
3656 if (load_pop(self) < 0)
3657 break;
3658 continue;
3659
3660 case POP_MARK:
3661 if (load_pop_mark(self) < 0)
3662 break;
3663 continue;
3664
3665 case SETITEM:
3666 if (load_setitem(self) < 0)
3667 break;
3668 continue;
3669
3670 case SETITEMS:
3671 if (load_setitems(self) < 0)
3672 break;
3673 continue;
3674
3675 case STOP:
3676 break;
3677
3678 case PERSID:
3679 if (load_persid(self) < 0)
3680 break;
3681 continue;
3682
3683 case BINPERSID:
3684 if (load_binpersid(self) < 0)
3685 break;
3686 continue;
3687
3688 case REDUCE:
3689 if (load_reduce(self) < 0)
3690 break;
3691 continue;
3692
3693 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003694 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003695 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003696 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003697 }
3698
3699 break;
3700 }
3701
Guido van Rossum053b8df1998-11-25 16:18:00 +00003702 if ((err = PyErr_Occurred())) {
3703 if (err == PyExc_EOFError) {
3704 PyErr_SetNone(PyExc_EOFError);
3705 }
3706 return NULL;
3707 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003708
Guido van Rossum053b8df1998-11-25 16:18:00 +00003709 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003710 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003711}
3712
3713
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003714/* No-load functions to support noload, which is used to
3715 find persistent references. */
3716
3717static int
3718noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003719 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003720
3721 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003722 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003723}
3724
3725
3726static int
3727noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003728 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003729 char *s;
3730
3731 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003732 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003733 if ((*self->readline_func)(self, &s) < 0) return -1;
3734 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003735 PDATA_APPEND(self->stack, Py_None,-1);
3736 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003737}
3738
3739static int
3740noload_global(Unpicklerobject *self) {
3741 char *s;
3742
3743 if ((*self->readline_func)(self, &s) < 0) return -1;
3744 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003745 PDATA_APPEND(self->stack, Py_None,-1);
3746 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003747}
3748
3749static int
3750noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003751
Guido van Rossum053b8df1998-11-25 16:18:00 +00003752 if (self->stack->length < 2) return stackUnderflow();
3753 Pdata_clear(self->stack, self->stack->length-2);
3754 PDATA_APPEND(self->stack, Py_None,-1);
3755 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003756}
3757
3758static int
3759noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003760
Guido van Rossum053b8df1998-11-25 16:18:00 +00003761 if (self->stack->length < 1) return stackUnderflow();
3762 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003763 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003764}
3765
3766
3767static PyObject *
3768noload(Unpicklerobject *self) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00003769 PyObject *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003770 char *s;
3771
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003772 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003773 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003774
3775 while (1) {
3776 if ((*self->read_func)(self, &s, 1) < 0)
3777 break;
3778
3779 switch (s[0]) {
3780 case NONE:
3781 if (load_none(self) < 0)
3782 break;
3783 continue;
3784
3785 case BININT:
3786 if (load_binint(self) < 0)
3787 break;
3788 continue;
3789
3790 case BININT1:
3791 if (load_binint1(self) < 0)
3792 break;
3793 continue;
3794
3795 case BININT2:
3796 if (load_binint2(self) < 0)
3797 break;
3798 continue;
3799
3800 case INT:
3801 if (load_int(self) < 0)
3802 break;
3803 continue;
3804
3805 case LONG:
3806 if (load_long(self) < 0)
3807 break;
3808 continue;
3809
3810 case FLOAT:
3811 if (load_float(self) < 0)
3812 break;
3813 continue;
3814
3815 case BINFLOAT:
3816 if (load_binfloat(self) < 0)
3817 break;
3818 continue;
3819
3820 case BINSTRING:
3821 if (load_binstring(self) < 0)
3822 break;
3823 continue;
3824
3825 case SHORT_BINSTRING:
3826 if (load_short_binstring(self) < 0)
3827 break;
3828 continue;
3829
3830 case STRING:
3831 if (load_string(self) < 0)
3832 break;
3833 continue;
3834
Guido van Rossum5fccb7c2000-03-10 23:11:40 +00003835 case UNICODE:
3836 if (load_unicode(self) < 0)
3837 break;
3838 continue;
3839
3840 case BINUNICODE:
3841 if (load_binunicode(self) < 0)
3842 break;
3843 continue;
3844
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003845 case EMPTY_TUPLE:
3846 if (load_empty_tuple(self) < 0)
3847 break;
3848 continue;
3849
3850 case TUPLE:
3851 if (load_tuple(self) < 0)
3852 break;
3853 continue;
3854
3855 case EMPTY_LIST:
3856 if (load_empty_list(self) < 0)
3857 break;
3858 continue;
3859
3860 case LIST:
3861 if (load_list(self) < 0)
3862 break;
3863 continue;
3864
3865 case EMPTY_DICT:
3866 if (load_empty_dict(self) < 0)
3867 break;
3868 continue;
3869
3870 case DICT:
3871 if (load_dict(self) < 0)
3872 break;
3873 continue;
3874
3875 case OBJ:
3876 if (noload_obj(self) < 0)
3877 break;
3878 continue;
3879
3880 case INST:
3881 if (noload_inst(self) < 0)
3882 break;
3883 continue;
3884
3885 case GLOBAL:
3886 if (noload_global(self) < 0)
3887 break;
3888 continue;
3889
3890 case APPEND:
3891 if (load_append(self) < 0)
3892 break;
3893 continue;
3894
3895 case APPENDS:
3896 if (load_appends(self) < 0)
3897 break;
3898 continue;
3899
3900 case BUILD:
3901 if (noload_build(self) < 0)
3902 break;
3903 continue;
3904
3905 case DUP:
3906 if (load_dup(self) < 0)
3907 break;
3908 continue;
3909
3910 case BINGET:
3911 if (load_binget(self) < 0)
3912 break;
3913 continue;
3914
3915 case LONG_BINGET:
3916 if (load_long_binget(self) < 0)
3917 break;
3918 continue;
3919
3920 case GET:
3921 if (load_get(self) < 0)
3922 break;
3923 continue;
3924
3925 case MARK:
3926 if (load_mark(self) < 0)
3927 break;
3928 continue;
3929
3930 case BINPUT:
3931 if (load_binput(self) < 0)
3932 break;
3933 continue;
3934
3935 case LONG_BINPUT:
3936 if (load_long_binput(self) < 0)
3937 break;
3938 continue;
3939
3940 case PUT:
3941 if (load_put(self) < 0)
3942 break;
3943 continue;
3944
3945 case POP:
3946 if (load_pop(self) < 0)
3947 break;
3948 continue;
3949
3950 case POP_MARK:
3951 if (load_pop_mark(self) < 0)
3952 break;
3953 continue;
3954
3955 case SETITEM:
3956 if (load_setitem(self) < 0)
3957 break;
3958 continue;
3959
3960 case SETITEMS:
3961 if (load_setitems(self) < 0)
3962 break;
3963 continue;
3964
3965 case STOP:
3966 break;
3967
3968 case PERSID:
3969 if (load_persid(self) < 0)
3970 break;
3971 continue;
3972
3973 case BINPERSID:
3974 if (load_binpersid(self) < 0)
3975 break;
3976 continue;
3977
3978 case REDUCE:
3979 if (noload_reduce(self) < 0)
3980 break;
3981 continue;
3982
3983 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003984 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003985 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003986 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003987 }
3988
3989 break;
3990 }
3991
Guido van Rossum053b8df1998-11-25 16:18:00 +00003992 if ((err = PyErr_Occurred())) {
3993 if (err == PyExc_EOFError) {
3994 PyErr_SetNone(PyExc_EOFError);
3995 }
3996 return NULL;
3997 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003998
Guido van Rossum053b8df1998-11-25 16:18:00 +00003999 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004000 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004001}
4002
4003
Guido van Rossum60456fd1997-04-09 17:36:32 +00004004static PyObject *
4005Unpickler_load(Unpicklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00004006 UNLESS (PyArg_ParseTuple(args, ":load"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004007 return NULL;
4008
4009 return load(self);
4010}
4011
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004012static PyObject *
4013Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Guido van Rossum43713e52000-02-29 13:59:29 +00004014 UNLESS (PyArg_ParseTuple(args, ":noload"))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004015 return NULL;
4016
4017 return noload(self);
4018}
4019
Guido van Rossum60456fd1997-04-09 17:36:32 +00004020
4021static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004022 {"load", (PyCFunction)Unpickler_load, 1,
4023 "load() -- Load a pickle"
4024 },
4025 {"noload", (PyCFunction)Unpickler_noload, 1,
4026 "noload() -- not load a pickle, but go through most of the motions\n"
4027 "\n"
4028 "This function can be used to read past a pickle without instantiating\n"
4029 "any objects or importing any modules. It can also be used to find all\n"
4030 "persistent references without instantiating any objects or importing\n"
4031 "any modules.\n"
4032 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00004033 {NULL, NULL} /* sentinel */
4034};
4035
4036
4037static Unpicklerobject *
4038newUnpicklerobject(PyObject *f) {
4039 Unpicklerobject *self;
4040
Guido van Rossumb18618d2000-05-03 23:44:39 +00004041 UNLESS (self = PyObject_New(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004042 return NULL;
4043
4044 self->file = NULL;
4045 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004046 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00004047 self->pers_func = NULL;
4048 self->last_string = NULL;
4049 self->marks = NULL;
4050 self->num_marks = 0;
4051 self->marks_size = 0;
4052 self->buf_size = 0;
4053 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00004054 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00004055 self->safe_constructors = NULL;
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004056 self->find_class = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004057
Guido van Rossum83addc72000-04-21 20:49:36 +00004058 UNLESS (self->memo = PyDict_New())
4059 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004060
4061 Py_INCREF(f);
4062 self->file = f;
4063
4064 /* Set read, readline based on type of f */
4065 if (PyFile_Check(f)) {
4066 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004067 if (self->fp == NULL) {
Guido van Rossum83addc72000-04-21 20:49:36 +00004068 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
4069 goto err;
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00004070 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00004071 self->read_func = read_file;
4072 self->readline_func = readline_file;
4073 }
4074 else if (PycStringIO_InputCheck(f)) {
4075 self->fp = NULL;
4076 self->read_func = read_cStringIO;
4077 self->readline_func = readline_cStringIO;
4078 }
4079 else {
4080
4081 self->fp = NULL;
4082 self->read_func = read_other;
4083 self->readline_func = readline_other;
4084
Guido van Rossum053b8df1998-11-25 16:18:00 +00004085 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004086 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00004087 PyErr_Clear();
4088 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
4089 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00004090 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004091 }
4092 }
4093
Guido van Rossum053b8df1998-11-25 16:18:00 +00004094 if (PyEval_GetRestricted()) {
4095 /* Restricted execution, get private tables */
4096 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004097
Guido van Rossum053b8df1998-11-25 16:18:00 +00004098 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
4099 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
4100 Py_DECREF(m);
4101 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004102 }
4103 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004104 self->safe_constructors=safe_constructors;
4105 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004106 }
4107
Guido van Rossum60456fd1997-04-09 17:36:32 +00004108 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004109
4110err:
4111 Py_DECREF((PyObject *)self);
4112 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004113}
4114
4115
4116static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004117get_Unpickler(PyObject *self, PyObject *args) {
4118 PyObject *file;
4119
Guido van Rossum43713e52000-02-29 13:59:29 +00004120 UNLESS (PyArg_ParseTuple(args, "O:Unpickler", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004121 return NULL;
4122 return (PyObject *)newUnpicklerobject(file);
4123}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004124
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004125
Guido van Rossum60456fd1997-04-09 17:36:32 +00004126static void
4127Unpickler_dealloc(Unpicklerobject *self) {
4128 Py_XDECREF(self->readline);
4129 Py_XDECREF(self->read);
4130 Py_XDECREF(self->file);
4131 Py_XDECREF(self->memo);
4132 Py_XDECREF(self->stack);
4133 Py_XDECREF(self->pers_func);
4134 Py_XDECREF(self->arg);
4135 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004136 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004137
Guido van Rossum60456fd1997-04-09 17:36:32 +00004138 if (self->marks) {
4139 free(self->marks);
4140 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004141
Guido van Rossum60456fd1997-04-09 17:36:32 +00004142 if (self->buf_size) {
4143 free(self->buf);
4144 }
4145
Guido van Rossumb18618d2000-05-03 23:44:39 +00004146 PyObject_Del(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004147}
4148
4149
4150static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004151Unpickler_getattr(Unpicklerobject *self, char *name) {
4152 if (!strcmp(name, "persistent_load")) {
4153 if (!self->pers_func) {
4154 PyErr_SetString(PyExc_AttributeError, name);
4155 return NULL;
4156 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004157
Guido van Rossum60456fd1997-04-09 17:36:32 +00004158 Py_INCREF(self->pers_func);
4159 return self->pers_func;
4160 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004161
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004162 if (!strcmp(name, "find_global")) {
4163 if (!self->find_class) {
4164 PyErr_SetString(PyExc_AttributeError, name);
4165 return NULL;
4166 }
4167
4168 Py_INCREF(self->find_class);
4169 return self->find_class;
4170 }
4171
Guido van Rossum60456fd1997-04-09 17:36:32 +00004172 if (!strcmp(name, "memo")) {
4173 if (!self->memo) {
4174 PyErr_SetString(PyExc_AttributeError, name);
4175 return NULL;
4176 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004177
Guido van Rossum60456fd1997-04-09 17:36:32 +00004178 Py_INCREF(self->memo);
4179 return self->memo;
4180 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004181
Guido van Rossum60456fd1997-04-09 17:36:32 +00004182 if (!strcmp(name, "UnpicklingError")) {
4183 Py_INCREF(UnpicklingError);
4184 return UnpicklingError;
4185 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004186
Guido van Rossum60456fd1997-04-09 17:36:32 +00004187 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4188}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004189
Guido van Rossum60456fd1997-04-09 17:36:32 +00004190
4191static int
4192Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004193
Guido van Rossum1b9e0aa1999-04-19 17:58:18 +00004194 if (!strcmp(name, "persistent_load")) {
4195 Py_XDECREF(self->pers_func);
4196 self->pers_func = value;
4197 Py_XINCREF(value);
4198 return 0;
4199 }
4200
4201 if (!strcmp(name, "find_global")) {
4202 Py_XDECREF(self->find_class);
4203 self->find_class = value;
4204 Py_XINCREF(value);
4205 return 0;
4206 }
4207
Guido van Rossum053b8df1998-11-25 16:18:00 +00004208 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004209 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004210 "attribute deletion is not supported");
4211 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004212 }
4213
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004214 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004215 if (! PyDict_Check(value)) {
4216 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4217 return -1;
4218 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004219 Py_XDECREF(self->memo);
4220 self->memo = value;
4221 Py_INCREF(value);
4222 return 0;
4223 }
4224
Guido van Rossum60456fd1997-04-09 17:36:32 +00004225 PyErr_SetString(PyExc_AttributeError, name);
4226 return -1;
4227}
4228
4229
4230static PyObject *
4231cpm_dump(PyObject *self, PyObject *args) {
4232 PyObject *ob, *file, *res = NULL;
4233 Picklerobject *pickler = 0;
4234 int bin = 0;
4235
Guido van Rossum053b8df1998-11-25 16:18:00 +00004236 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004237 goto finally;
4238
Guido van Rossum053b8df1998-11-25 16:18:00 +00004239 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004240 goto finally;
4241
4242 if (dump(pickler, ob) < 0)
4243 goto finally;
4244
4245 Py_INCREF(Py_None);
4246 res = Py_None;
4247
4248finally:
4249 Py_XDECREF(pickler);
4250
4251 return res;
4252}
4253
4254
4255static PyObject *
4256cpm_dumps(PyObject *self, PyObject *args) {
4257 PyObject *ob, *file = 0, *res = NULL;
4258 Picklerobject *pickler = 0;
4259 int bin = 0;
4260
Guido van Rossum43713e52000-02-29 13:59:29 +00004261 UNLESS (PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004262 goto finally;
4263
Guido van Rossum053b8df1998-11-25 16:18:00 +00004264 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004265 goto finally;
4266
Guido van Rossum053b8df1998-11-25 16:18:00 +00004267 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004268 goto finally;
4269
4270 if (dump(pickler, ob) < 0)
4271 goto finally;
4272
4273 res = PycStringIO->cgetvalue(file);
4274
4275finally:
4276 Py_XDECREF(pickler);
4277 Py_XDECREF(file);
4278
4279 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004280}
4281
4282
4283static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004284cpm_load(PyObject *self, PyObject *args) {
4285 Unpicklerobject *unpickler = 0;
4286 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004287
Guido van Rossum43713e52000-02-29 13:59:29 +00004288 UNLESS (PyArg_ParseTuple(args, "O:load", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004289 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004290
Guido van Rossum053b8df1998-11-25 16:18:00 +00004291 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004292 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004293
Guido van Rossum60456fd1997-04-09 17:36:32 +00004294 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004295
Guido van Rossum60456fd1997-04-09 17:36:32 +00004296finally:
4297 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004298
Guido van Rossum60456fd1997-04-09 17:36:32 +00004299 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004300}
4301
4302
4303static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004304cpm_loads(PyObject *self, PyObject *args) {
4305 PyObject *ob, *file = 0, *res = NULL;
4306 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004307
Guido van Rossum43713e52000-02-29 13:59:29 +00004308 UNLESS (PyArg_ParseTuple(args, "S:loads", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004309 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004310
Guido van Rossum053b8df1998-11-25 16:18:00 +00004311 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004312 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004313
Guido van Rossum053b8df1998-11-25 16:18:00 +00004314 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004315 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004316
Guido van Rossum60456fd1997-04-09 17:36:32 +00004317 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004318
Guido van Rossum60456fd1997-04-09 17:36:32 +00004319finally:
4320 Py_XDECREF(file);
4321 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004322
Guido van Rossum60456fd1997-04-09 17:36:32 +00004323 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004324}
4325
4326
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004327static char Unpicklertype__doc__[] =
4328"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004329
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004330static PyTypeObject Unpicklertype = {
4331 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004332 0, /*ob_size*/
4333 "Unpickler", /*tp_name*/
4334 sizeof(Unpicklerobject), /*tp_basicsize*/
4335 0, /*tp_itemsize*/
4336 /* methods */
4337 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4338 (printfunc)0, /*tp_print*/
4339 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4340 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4341 (cmpfunc)0, /*tp_compare*/
4342 (reprfunc)0, /*tp_repr*/
4343 0, /*tp_as_number*/
4344 0, /*tp_as_sequence*/
4345 0, /*tp_as_mapping*/
4346 (hashfunc)0, /*tp_hash*/
4347 (ternaryfunc)0, /*tp_call*/
4348 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004349
Guido van Rossum60456fd1997-04-09 17:36:32 +00004350 /* Space for future expansion */
4351 0L,0L,0L,0L,
4352 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004353};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004354
Guido van Rossum60456fd1997-04-09 17:36:32 +00004355static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004356 {"dump", (PyCFunction)cpm_dump, 1,
4357 "dump(object, file, [binary]) --"
4358 "Write an object in pickle format to the given file\n"
4359 "\n"
4360 "If the optional argument, binary, is provided and is true, then the\n"
4361 "pickle will be written in binary format, which is more space and\n"
4362 "computationally efficient. \n"
4363 },
4364 {"dumps", (PyCFunction)cpm_dumps, 1,
4365 "dumps(object, [binary]) --"
4366 "Return a string containing an object in pickle format\n"
4367 "\n"
4368 "If the optional argument, binary, is provided and is true, then the\n"
4369 "pickle will be written in binary format, which is more space and\n"
4370 "computationally efficient. \n"
4371 },
4372 {"load", (PyCFunction)cpm_load, 1,
4373 "load(file) -- Load a pickle from the given file"},
4374 {"loads", (PyCFunction)cpm_loads, 1,
4375 "loads(string) -- Load a pickle from the given string"},
4376 {"Pickler", (PyCFunction)get_Pickler, 1,
4377 "Pickler(file, [binary]) -- Create a pickler\n"
4378 "\n"
4379 "If the optional argument, binary, is provided and is true, then\n"
4380 "pickles will be written in binary format, which is more space and\n"
4381 "computationally efficient. \n"
4382 },
4383 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4384 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004385 { NULL, NULL }
4386};
4387
Guido van Rossum60456fd1997-04-09 17:36:32 +00004388static int
4389init_stuff(PyObject *module, PyObject *module_dict) {
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004390 PyObject *string, *copy_reg, *t, *r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004391
4392#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4393
4394 INIT_STR(__class__);
4395 INIT_STR(__getinitargs__);
4396 INIT_STR(__dict__);
4397 INIT_STR(__getstate__);
4398 INIT_STR(__setstate__);
4399 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004400 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004401 INIT_STR(__reduce__);
4402 INIT_STR(write);
4403 INIT_STR(__safe_for_unpickling__);
4404 INIT_STR(append);
4405 INIT_STR(read);
4406 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004407 INIT_STR(copy_reg);
4408 INIT_STR(dispatch_table);
4409 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004410 INIT_STR(__basicnew__);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004411 UNLESS (empty_str=PyString_FromString("")) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004412
Guido van Rossum053b8df1998-11-25 16:18:00 +00004413 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004414 return -1;
4415
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004416 /* These next few are special because we want to use different
4417 ones in restricted mode. */
4418
Guido van Rossum053b8df1998-11-25 16:18:00 +00004419 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004420 return -1;
4421
Guido van Rossum053b8df1998-11-25 16:18:00 +00004422 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4423 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004424 return -1;
4425
4426 Py_DECREF(copy_reg);
4427
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004428 /* Down to here ********************************** */
4429
Guido van Rossum053b8df1998-11-25 16:18:00 +00004430 UNLESS (string = PyImport_ImportModule("string"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004431 return -1;
4432
Guido van Rossum053b8df1998-11-25 16:18:00 +00004433 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004434 return -1;
4435
4436 Py_DECREF(string);
4437
Guido van Rossum053b8df1998-11-25 16:18:00 +00004438 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004439 return -1;
4440
Guido van Rossumc03158b1999-06-09 15:23:31 +00004441 /* Ugh */
4442 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4443 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4444 return -1;
4445
4446 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004447 UNLESS (r=PyRun_String(
4448 "def __init__(self, *args): self.args=args\n\n"
4449 "def __str__(self):\n"
4450 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4451 Py_file_input,
4452 module_dict, t) ) return -1;
4453 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004454
4455 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4456 return -1;
4457
4458 Py_DECREF(t);
4459
4460
4461 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
4462 PickleError, NULL))
4463 return -1;
4464
4465 UNLESS (t=PyDict_New()) return -1;
Guido van Rossumc3be1a31999-06-15 14:36:59 +00004466 UNLESS (r=PyRun_String(
4467 "def __init__(self, *args): self.args=args\n\n"
4468 "def __str__(self):\n"
4469 " a=self.args\n"
4470 " a=a and type(a[0]) or '(what)'\n"
4471 " return 'Cannot pickle %s objects' % a\n"
4472 , Py_file_input,
4473 module_dict, t) ) return -1;
4474 Py_DECREF(r);
Guido van Rossumc03158b1999-06-09 15:23:31 +00004475
4476 UNLESS (UnpickleableError = PyErr_NewException(
4477 "cPickle.UnpickleableError", PicklingError, t))
4478 return -1;
4479
4480 Py_DECREF(t);
4481
4482 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
4483 PickleError, NULL))
4484 return -1;
4485
4486 if (PyDict_SetItemString(module_dict, "PickleError",
4487 PickleError) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004488 return -1;
4489
4490 if (PyDict_SetItemString(module_dict, "PicklingError",
4491 PicklingError) < 0)
4492 return -1;
4493
Guido van Rossum60456fd1997-04-09 17:36:32 +00004494 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4495 UnpicklingError) < 0)
4496 return -1;
4497
Guido van Rossumc03158b1999-06-09 15:23:31 +00004498 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4499 UnpickleableError) < 0)
4500 return -1;
4501
Guido van Rossum053b8df1998-11-25 16:18:00 +00004502 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4503 return -1;
4504
4505 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4506 BadPickleGet) < 0)
4507 return -1;
4508
Guido van Rossum60456fd1997-04-09 17:36:32 +00004509 PycString_IMPORT;
4510
4511 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004512}
4513
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004514#ifndef DL_EXPORT /* declarations for DLL import/export */
4515#define DL_EXPORT(RTYPE) RTYPE
4516#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004517DL_EXPORT(void)
Thomas Wouters58d05102000-07-24 14:43:35 +00004518initcPickle(void) {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004519 PyObject *m, *d, *v;
Guido van Rossum2f80d961999-07-13 15:18:58 +00004520 char *rev="1.71";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004521 PyObject *format_version;
4522 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004523
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004524 Picklertype.ob_type = &PyType_Type;
4525 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004526 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004527
Guido van Rossum60456fd1997-04-09 17:36:32 +00004528 /* Create the module and add the functions */
4529 m = Py_InitModule4("cPickle", cPickle_methods,
4530 cPickle_module_documentation,
4531 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004532
Guido van Rossum60456fd1997-04-09 17:36:32 +00004533 /* Add some symbolic constants to the module */
4534 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004535 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004536 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004537
Guido van Rossum60456fd1997-04-09 17:36:32 +00004538 format_version = PyString_FromString("1.3");
4539 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004540
Guido van Rossum60456fd1997-04-09 17:36:32 +00004541 PyDict_SetItemString(d, "format_version", format_version);
4542 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004543 Py_XDECREF(format_version);
4544 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004545
Guido van Rossum60456fd1997-04-09 17:36:32 +00004546 init_stuff(m, d);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004547}