blob: a661c660b048c02d9b07e2184487f1adcf86f4a7 [file] [log] [blame]
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001/*
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002 * cPickle.c,v 1.63 1999/02/05 01:40:06 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 Rossumf9ffb031999-02-04 14:54:04 +000052"cPickle.c,v 1.63 1999/02/05 01:40:06 jim Exp\n"
Guido van Rossum2f4caa41997-01-06 22:59:08 +000053;
54
55#include "Python.h"
56#include "cStringIO.h"
Guido van Rossum60456fd1997-04-09 17:36:32 +000057#include "mymath.h"
Guido van Rossum2f4caa41997-01-06 22:59:08 +000058
Guido van Rossum142eeb81997-08-13 03:14:41 +000059#ifndef Py_eval_input
60#include <graminit.h>
61#define Py_eval_input eval_input
Guido van Rossumc6ef2041997-08-21 02:30:45 +000062#endif /* Py_eval_input */
Guido van Rossum142eeb81997-08-13 03:14:41 +000063
Guido van Rossum2f4caa41997-01-06 22:59:08 +000064#include <errno.h>
65
Guido van Rossum2f4caa41997-01-06 22:59:08 +000066#define UNLESS(E) if (!(E))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000067
Guido van Rossum60456fd1997-04-09 17:36:32 +000068#define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
Guido van Rossum2f4caa41997-01-06 22:59:08 +000069
Guido van Rossum60456fd1997-04-09 17:36:32 +000070#define WRITE_BUF_SIZE 256
71
72
73#define MARK '('
74#define STOP '.'
75#define POP '0'
76#define POP_MARK '1'
77#define DUP '2'
78#define FLOAT 'F'
Guido van Rossum60456fd1997-04-09 17:36:32 +000079#define BINFLOAT 'G'
Guido van Rossum60456fd1997-04-09 17:36:32 +000080#define INT 'I'
81#define BININT 'J'
82#define BININT1 'K'
83#define LONG 'L'
84#define BININT2 'M'
85#define NONE 'N'
86#define PERSID 'P'
87#define BINPERSID 'Q'
88#define REDUCE 'R'
89#define STRING 'S'
90#define BINSTRING 'T'
Guido van Rossum2f4caa41997-01-06 22:59:08 +000091#define SHORT_BINSTRING 'U'
Guido van Rossum60456fd1997-04-09 17:36:32 +000092#define APPEND 'a'
93#define BUILD 'b'
94#define GLOBAL 'c'
95#define DICT 'd'
96#define EMPTY_DICT '}'
97#define APPENDS 'e'
98#define GET 'g'
99#define BINGET 'h'
100#define INST 'i'
101#define LONG_BINGET 'j'
102#define LIST 'l'
103#define EMPTY_LIST ']'
104#define OBJ 'o'
105#define PUT 'p'
106#define BINPUT 'q'
107#define LONG_BINPUT 'r'
108#define SETITEM 's'
109#define TUPLE 't'
110#define EMPTY_TUPLE ')'
111#define SETITEMS 'u'
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000112
Guido van Rossum60456fd1997-04-09 17:36:32 +0000113static char MARKv = MARK;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000114
115/* atol function from string module */
116static PyObject *atol_func;
117
118static PyObject *PicklingError;
119static PyObject *UnpicklingError;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000120static PyObject *BadPickleGet;
121
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000122
Guido van Rossum60456fd1997-04-09 17:36:32 +0000123static PyObject *dispatch_table;
124static PyObject *safe_constructors;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000125static PyObject *empty_tuple;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000126
Guido van Rossum60456fd1997-04-09 17:36:32 +0000127static PyObject *__class___str, *__getinitargs___str, *__dict___str,
128 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
129 *write_str, *__safe_for_unpickling___str, *append_str,
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000130 *read_str, *readline_str, *__main___str, *__basicnew___str,
Guido van Rossum053b8df1998-11-25 16:18:00 +0000131 *copy_reg_str, *dispatch_table_str, *safe_constructors_str, *empty_str;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000132
Guido van Rossum60456fd1997-04-09 17:36:32 +0000133static int save();
134static int put2();
135
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
170 PyMem_DEL(self);
171}
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 *
182Pdata_New() {
183 Pdata *self;
184
185 UNLESS (self = PyObject_NEW(Pdata, &PdataType)) return NULL;
186 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
195stackUnderflow() {
196 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;
248 for (i=start, j=0 ; j < l; )
249 PyTuple_SET_ITEM(r,j++,self->data[i++]);
250
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;
262 for (i=start, j=0 ; j < l; )
263 PyList_SET_ITEM(r,j++,self->data[i++]);
264
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
Guido van Rossum60456fd1997-04-09 17:36:32 +0000309typedef struct {
310 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 */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000320 int (*write_func)();
321 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
Guido van Rossum60456fd1997-04-09 17:36:32 +0000328typedef struct {
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;
343 int (*read_func)();
344 int (*readline_func)();
345 int buf_size;
346 char *buf;
347 PyObject *safe_constructors;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000348} Unpicklerobject;
349
Guido van Rossum9716aaa1997-12-08 15:15:16 +0000350staticforward PyTypeObject Unpicklertype;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000351
Guido van Rossum60456fd1997-04-09 17:36:32 +0000352int
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000353cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000354 PyObject *v;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000355
Guido van Rossum053b8df1998-11-25 16:18:00 +0000356 if ((v = PyObject_GetItem(o,key))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000357 Py_DECREF(v);
358 return 1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000359 }
360
Guido van Rossum60456fd1997-04-09 17:36:32 +0000361 PyErr_Clear();
362 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000363}
364
Guido van Rossumd385d591997-04-09 17:47:47 +0000365static
Guido van Rossum60456fd1997-04-09 17:36:32 +0000366PyObject *
367#ifdef HAVE_STDARG_PROTOTYPES
368/* VARARGS 2 */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000369cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000370#else
371/* VARARGS */
Guido van Rossum57d9f2e1998-01-19 23:18:18 +0000372cPickle_ErrFormat(va_alist) va_dcl {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000373#endif
Guido van Rossum60456fd1997-04-09 17:36:32 +0000374 va_list va;
375 PyObject *args=0, *retval=0;
376#ifdef HAVE_STDARG_PROTOTYPES
377 va_start(va, format);
378#else
379 PyObject *ErrType;
380 char *stringformat, *format;
381 va_start(va);
382 ErrType = va_arg(va, PyObject *);
383 stringformat = va_arg(va, char *);
384 format = va_arg(va, char *);
385#endif
386
Guido van Rossum053b8df1998-11-25 16:18:00 +0000387 if (format) args = Py_VaBuildValue(format, va);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000388 va_end(va);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000389 if (format && ! args) return NULL;
390 if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000391
Guido van Rossum053b8df1998-11-25 16:18:00 +0000392 if (retval) {
393 if (args) {
394 PyObject *v;
395 v=PyString_Format(retval, args);
396 Py_DECREF(retval);
397 Py_DECREF(args);
398 if (! v) return NULL;
399 retval=v;
400 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000401 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000402 else
Guido van Rossum053b8df1998-11-25 16:18:00 +0000403 if (args) retval=args;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000404 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000405 PyErr_SetObject(ErrType,Py_None);
406 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000407 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000408 PyErr_SetObject(ErrType,retval);
409 Py_DECREF(retval);
410 return NULL;
411}
412
413static int
414write_file(Picklerobject *self, char *s, int n) {
415 if (s == NULL) {
416 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000417 }
418
Guido van Rossum60456fd1997-04-09 17:36:32 +0000419 if ((int)fwrite(s, sizeof(char), n, self->fp) != n) {
420 PyErr_SetFromErrno(PyExc_IOError);
421 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000422 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000423
424 return n;
425}
426
Guido van Rossum60456fd1997-04-09 17:36:32 +0000427static int
428write_cStringIO(Picklerobject *self, char *s, int n) {
429 if (s == NULL) {
430 return 0;
431 }
432
433 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
434 return -1;
435 }
436
437 return n;
438}
439
Guido van Rossum60456fd1997-04-09 17:36:32 +0000440static int
Guido van Rossum142eeb81997-08-13 03:14:41 +0000441write_none(Picklerobject *self, char *s, int n) {
442 if (s == NULL) return 0;
443 return n;
444}
445
Guido van Rossum142eeb81997-08-13 03:14:41 +0000446static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000447write_other(Picklerobject *self, char *s, int n) {
448 PyObject *py_str = 0, *junk = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000449
450 if (s == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000451 UNLESS (self->buf_size) return 0;
452 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000453 PyString_FromStringAndSize(self->write_buf, self->buf_size))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000454 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000455 }
456 else {
457 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
458 if (write_other(self, NULL, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +0000459 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000460 }
461
462 if (n > WRITE_BUF_SIZE) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000463 UNLESS (py_str =
Guido van Rossum60456fd1997-04-09 17:36:32 +0000464 PyString_FromStringAndSize(s, n))
Guido van Rossum053b8df1998-11-25 16:18:00 +0000465 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000466 }
467 else {
468 memcpy(self->write_buf + self->buf_size, s, n);
469 self->buf_size += n;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000470 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000471 }
472 }
473
Guido van Rossum053b8df1998-11-25 16:18:00 +0000474 if (self->write) {
475 /* object with write method */
476 ARG_TUP(self, py_str);
477 if (self->arg) {
478 junk = PyObject_CallObject(self->write, self->arg);
479 FREE_ARG_TUP(self);
480 }
481 if (junk) Py_DECREF(junk);
482 else return -1;
483 }
484 else
485 PDATA_PUSH(self->file, py_str, -1);
486
487 self->buf_size = 0;
488 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000489}
490
491
492static int
493read_file(Unpicklerobject *self, char **s, int n) {
494
495 if (self->buf_size == 0) {
496 int size;
497
498 size = ((n < 32) ? 32 : n);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000499 UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000500 PyErr_NoMemory();
501 return -1;
502 }
503
504 self->buf_size = size;
505 }
506 else if (n > self->buf_size) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000507 UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000508 PyErr_NoMemory();
509 return -1;
510 }
511
512 self->buf_size = n;
513 }
514
515 if ((int)fread(self->buf, sizeof(char), n, self->fp) != n) {
516 if (feof(self->fp)) {
517 PyErr_SetNone(PyExc_EOFError);
518 return -1;
519 }
520
521 PyErr_SetFromErrno(PyExc_IOError);
522 return -1;
523 }
524
525 *s = self->buf;
526
527 return n;
528}
529
530
531static int
532readline_file(Unpicklerobject *self, char **s) {
533 int i;
534
535 if (self->buf_size == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000536 UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000537 PyErr_NoMemory();
538 return -1;
539 }
540
541 self->buf_size = 40;
542 }
543
544 i = 0;
545 while (1) {
546 for (; i < (self->buf_size - 1); i++) {
547 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
548 self->buf[i + 1] = '\0';
549 *s = self->buf;
550 return i + 1;
551 }
552 }
553
Guido van Rossum053b8df1998-11-25 16:18:00 +0000554 UNLESS (self->buf = (char *)realloc(self->buf,
Guido van Rossum60456fd1997-04-09 17:36:32 +0000555 (self->buf_size * 2) * sizeof(char))) {
556 PyErr_NoMemory();
557 return -1;
558 }
559
560 self->buf_size *= 2;
561 }
562
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000563}
564
565
566static int
Guido van Rossum60456fd1997-04-09 17:36:32 +0000567read_cStringIO(Unpicklerobject *self, char **s, int n) {
568 char *ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000569
Guido van Rossum60456fd1997-04-09 17:36:32 +0000570 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
571 PyErr_SetNone(PyExc_EOFError);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000572 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000573 }
574
Guido van Rossum60456fd1997-04-09 17:36:32 +0000575 *s = ptr;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000576
Guido van Rossum60456fd1997-04-09 17:36:32 +0000577 return n;
578}
579
580
581static int
582readline_cStringIO(Unpicklerobject *self, char **s) {
583 int n;
584 char *ptr;
585
586 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
587 return -1;
588 }
589
590 *s = ptr;
591
592 return n;
593}
594
595
596static int
597read_other(Unpicklerobject *self, char **s, int n) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000598 PyObject *bytes, *str=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000599 int res = -1;
600
Guido van Rossum053b8df1998-11-25 16:18:00 +0000601 UNLESS (bytes = PyInt_FromLong(n)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000602
Guido van Rossum053b8df1998-11-25 16:18:00 +0000603 ARG_TUP(self, bytes);
604 if (self->arg) {
605 str = PyObject_CallObject(self->read, self->arg);
606 FREE_ARG_TUP(self);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000607 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000608 if (! str) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000609
610 Py_XDECREF(self->last_string);
611 self->last_string = str;
612
Guido van Rossum053b8df1998-11-25 16:18:00 +0000613 if (! (*s = PyString_AsString(str))) return -1;
614 return n;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000615}
616
617
618static int
619readline_other(Unpicklerobject *self, char **s) {
620 PyObject *str;
621 int str_size;
622
Guido van Rossum053b8df1998-11-25 16:18:00 +0000623 UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000624 return -1;
625 }
626
Guido van Rossum053b8df1998-11-25 16:18:00 +0000627 if ((str_size = PyString_Size(str)) < 0)
628 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000629
630 Py_XDECREF(self->last_string);
631 self->last_string = str;
632
Guido van Rossum053b8df1998-11-25 16:18:00 +0000633 if (! (*s = PyString_AsString(str)))
634 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000635
636 return str_size;
637}
638
639
640static char *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000641pystrndup(char *s, int l) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000642 char *r;
Guido van Rossum053b8df1998-11-25 16:18:00 +0000643 UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
Guido van Rossum60456fd1997-04-09 17:36:32 +0000644 memcpy(r,s,l);
645 r[l]=0;
646 return r;
647}
648
649
650static int
651get(Picklerobject *self, PyObject *id) {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000652 PyObject *value, *mv;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000653 long c_value;
654 char s[30];
655 int len;
656
Guido van Rossum053b8df1998-11-25 16:18:00 +0000657 UNLESS (mv = PyDict_GetItem(self->memo, id)) {
658 PyErr_SetObject(PyExc_KeyError, id);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000659 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000660 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000661
Guido van Rossum053b8df1998-11-25 16:18:00 +0000662 UNLESS (value = PyTuple_GetItem(mv, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000663 return -1;
664
Guido van Rossum053b8df1998-11-25 16:18:00 +0000665 UNLESS (PyInt_Check(value)) {
666 PyErr_SetString(PicklingError, "no int where int expected in memo");
667 return -1;
668 }
669 c_value = PyInt_AS_LONG((PyIntObject*)value);
Guido van Rossum60456fd1997-04-09 17:36:32 +0000670
671 if (!self->bin) {
672 s[0] = GET;
673 sprintf(s + 1, "%ld\n", c_value);
674 len = strlen(s);
675 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000676 else if (Pdata_Check(self->file)) {
677 if (write_other(self, NULL, 0) < 0) return -1;
678 PDATA_APPEND(self->file, mv, -1);
679 return 0;
680 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000681 else {
682 if (c_value < 256) {
683 s[0] = BINGET;
684 s[1] = (int)(c_value & 0xff);
685 len = 2;
686 }
687 else {
688 s[0] = LONG_BINGET;
689 s[1] = (int)(c_value & 0xff);
690 s[2] = (int)((c_value >> 8) & 0xff);
691 s[3] = (int)((c_value >> 16) & 0xff);
692 s[4] = (int)((c_value >> 24) & 0xff);
693 len = 5;
694 }
695 }
696
697 if ((*self->write_func)(self, s, len) < 0)
698 return -1;
699
700 return 0;
701}
702
703
704static int
705put(Picklerobject *self, PyObject *ob) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000706 if (ob->ob_refcnt < 2 || self->fast)
Guido van Rossum60456fd1997-04-09 17:36:32 +0000707 return 0;
708
709 return put2(self, ob);
710}
711
712
713static int
714put2(Picklerobject *self, PyObject *ob) {
715 char c_str[30];
716 int p, len, res = -1;
717 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +0000718
719 if (self->fast) return 0;
720
Guido van Rossum60456fd1997-04-09 17:36:32 +0000721 if ((p = PyDict_Size(self->memo)) < 0)
722 goto finally;
723
Guido van Rossum053b8df1998-11-25 16:18:00 +0000724 p++; /* Make sure memo keys are positive! */
725
726 UNLESS (py_ob_id = PyInt_FromLong((long)ob))
727 goto finally;
728
729 UNLESS (memo_len = PyInt_FromLong(p))
730 goto finally;
731
732 UNLESS (t = PyTuple_New(2))
733 goto finally;
734
735 PyTuple_SET_ITEM(t, 0, memo_len);
736 Py_INCREF(memo_len);
737 PyTuple_SET_ITEM(t, 1, ob);
738 Py_INCREF(ob);
739
740 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
741 goto finally;
742
Guido van Rossum60456fd1997-04-09 17:36:32 +0000743 if (!self->bin) {
744 c_str[0] = PUT;
745 sprintf(c_str + 1, "%d\n", p);
746 len = strlen(c_str);
747 }
Guido van Rossum053b8df1998-11-25 16:18:00 +0000748 else if (Pdata_Check(self->file)) {
749 if (write_other(self, NULL, 0) < 0) return -1;
750 PDATA_APPEND(self->file, memo_len, -1);
751 res=0; /* Job well done ;) */
752 goto finally;
753 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000754 else {
755 if (p >= 256) {
756 c_str[0] = LONG_BINPUT;
757 c_str[1] = (int)(p & 0xff);
758 c_str[2] = (int)((p >> 8) & 0xff);
759 c_str[3] = (int)((p >> 16) & 0xff);
760 c_str[4] = (int)((p >> 24) & 0xff);
761 len = 5;
762 }
763 else {
764 c_str[0] = BINPUT;
765 c_str[1] = p;
766 len = 2;
767 }
768 }
769
770 if ((*self->write_func)(self, c_str, len) < 0)
771 goto finally;
772
Guido van Rossum60456fd1997-04-09 17:36:32 +0000773 res = 0;
774
775finally:
776 Py_XDECREF(py_ob_id);
777 Py_XDECREF(memo_len);
778 Py_XDECREF(t);
779
780 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000781}
782
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000783#define PyImport_Import cPickle_Import
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000784
785static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000786PyImport_Import(PyObject *module_name) {
787 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
788 static PyObject *standard_builtins=0;
789 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
790
Guido van Rossum053b8df1998-11-25 16:18:00 +0000791 UNLESS (silly_list) {
792 UNLESS (__import___str=PyString_FromString("__import__"))
793 return NULL;
794 UNLESS (__builtins___str=PyString_FromString("__builtins__"))
795 return NULL;
796 UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
797 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000798 }
799
Guido van Rossum053b8df1998-11-25 16:18:00 +0000800 if ((globals=PyEval_GetGlobals())) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000801 Py_INCREF(globals);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000802 UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
803 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000804 }
805 else {
806 PyErr_Clear();
807
Guido van Rossum053b8df1998-11-25 16:18:00 +0000808 UNLESS (standard_builtins ||
809 (standard_builtins=PyImport_ImportModule("__builtin__")))
810 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000811
812 __builtins__=standard_builtins;
813 Py_INCREF(__builtins__);
Guido van Rossum053b8df1998-11-25 16:18:00 +0000814 UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
815 goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000816 }
817
Guido van Rossum053b8df1998-11-25 16:18:00 +0000818 if (PyDict_Check(__builtins__)) {
819 UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000820 }
821 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +0000822 UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000823 }
824
Guido van Rossum053b8df1998-11-25 16:18:00 +0000825 UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
826 module_name, globals, globals, silly_list))
Guido van Rossumfdde96c1997-12-04 01:13:01 +0000827 goto err;
828
829 Py_DECREF(globals);
830 Py_DECREF(__builtins__);
831 Py_DECREF(__import__);
832
833 return r;
834err:
835 Py_XDECREF(globals);
836 Py_XDECREF(__builtins__);
837 Py_XDECREF(__import__);
838 return NULL;
839}
840
841static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +0000842whichmodule(PyObject *global, PyObject *global_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000843 int i, j;
844 PyObject *module = 0, *modules_dict = 0,
845 *global_name_attr = 0, *name = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000846
Guido van Rossum45188231997-09-28 05:38:51 +0000847 module = PyObject_GetAttrString(global, "__module__");
848 if (module) return module;
849 PyErr_Clear();
850
Guido van Rossum053b8df1998-11-25 16:18:00 +0000851 UNLESS (modules_dict = PySys_GetObject("modules"))
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000852 return NULL;
853
Guido van Rossum60456fd1997-04-09 17:36:32 +0000854 i = 0;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000855 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
856
Guido van Rossum053b8df1998-11-25 16:18:00 +0000857 if (PyObject_Compare(name, __main___str)==0) continue;
Guido van Rossum142eeb81997-08-13 03:14:41 +0000858
Guido van Rossum053b8df1998-11-25 16:18:00 +0000859 UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +0000860 PyErr_Clear();
861 continue;
862 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000863
Guido van Rossum60456fd1997-04-09 17:36:32 +0000864 if (global_name_attr != global) {
865 Py_DECREF(global_name_attr);
866 continue;
867 }
868
869 Py_DECREF(global_name_attr);
870
871 break;
872 }
Guido van Rossum142eeb81997-08-13 03:14:41 +0000873
874 /* The following implements the rule in pickle.py added in 1.5
875 that used __main__ if no module is found. I don't actually
876 like this rule. jlf
877 */
Guido van Rossum053b8df1998-11-25 16:18:00 +0000878 if (!j) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000879 j=1;
880 name=__main___str;
881 }
Guido van Rossum60456fd1997-04-09 17:36:32 +0000882
883 Py_INCREF(name);
884 return name;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000885}
886
887
Guido van Rossum60456fd1997-04-09 17:36:32 +0000888static int
889save_none(Picklerobject *self, PyObject *args) {
890 static char none = NONE;
891 if ((*self->write_func)(self, &none, 1) < 0)
892 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000893
Guido van Rossum60456fd1997-04-09 17:36:32 +0000894 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000895}
896
897
Guido van Rossum60456fd1997-04-09 17:36:32 +0000898static int
899save_int(Picklerobject *self, PyObject *args) {
900 char c_str[32];
901 long l = PyInt_AS_LONG((PyIntObject *)args);
902 int len = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000903
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000904 if (!self->bin
905#if SIZEOF_LONG > 4
Guido van Rossum053b8df1998-11-25 16:18:00 +0000906 || (l >> 32)
Guido van Rossumde8d6d71997-05-13 18:00:44 +0000907#endif
Guido van Rossum053b8df1998-11-25 16:18:00 +0000908 ) {
909 /* Save extra-long ints in non-binary mode, so that
910 we can use python long parsing code to restore,
911 if necessary. */
Guido van Rossum60456fd1997-04-09 17:36:32 +0000912 c_str[0] = INT;
913 sprintf(c_str + 1, "%ld\n", l);
914 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
915 return -1;
916 }
917 else {
918 c_str[1] = (int)( l & 0xff);
919 c_str[2] = (int)((l >> 8) & 0xff);
920 c_str[3] = (int)((l >> 16) & 0xff);
921 c_str[4] = (int)((l >> 24) & 0xff);
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000922
Guido van Rossum60456fd1997-04-09 17:36:32 +0000923 if ((c_str[4] == 0) && (c_str[3] == 0)) {
924 if (c_str[2] == 0) {
925 c_str[0] = BININT1;
926 len = 2;
927 }
928 else {
929 c_str[0] = BININT2;
930 len = 3;
931 }
932 }
933 else {
934 c_str[0] = BININT;
935 len = 5;
936 }
937
938 if ((*self->write_func)(self, c_str, len) < 0)
939 return -1;
940 }
941
942 return 0;
943}
944
945
946static int
947save_long(Picklerobject *self, PyObject *args) {
948 int size, res = -1;
949 PyObject *repr = 0;
950
951 static char l = LONG;
952
Guido van Rossum053b8df1998-11-25 16:18:00 +0000953 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +0000954 goto finally;
955
956 if ((size = PyString_Size(repr)) < 0)
957 goto finally;
958
959 if ((*self->write_func)(self, &l, 1) < 0)
960 goto finally;
961
962 if ((*self->write_func)(self,
963 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
964 goto finally;
965
966 if ((*self->write_func)(self, "\n", 1) < 0)
967 goto finally;
968
969 res = 0;
970
971finally:
972 Py_XDECREF(repr);
973
974 return res;
975}
976
977
978static int
979save_float(Picklerobject *self, PyObject *args) {
980 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
981
Guido van Rossum60456fd1997-04-09 17:36:32 +0000982 if (self->bin) {
Guido van Rossum142eeb81997-08-13 03:14:41 +0000983 int s, e;
Guido van Rossum60456fd1997-04-09 17:36:32 +0000984 double f;
985 long fhi, flo;
986 char str[9], *p = str;
987
988 *p = BINFLOAT;
989 p++;
990
991 if (x < 0) {
992 s = 1;
993 x = -x;
Guido van Rossum2f4caa41997-01-06 22:59:08 +0000994 }
995 else
Guido van Rossum60456fd1997-04-09 17:36:32 +0000996 s = 0;
997
998 f = frexp(x, &e);
999
1000 /* Normalize f to be in the range [1.0, 2.0) */
1001 if (0.5 <= f && f < 1.0) {
1002 f *= 2.0;
1003 e--;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001004 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001005 else if (f == 0.0) {
1006 e = 0;
1007 }
1008 else {
1009 PyErr_SetString(PyExc_SystemError,
1010 "frexp() result out of range");
1011 return -1;
1012 }
1013
1014 if (e >= 1024) {
1015 /* XXX 1024 itself is reserved for Inf/NaN */
1016 PyErr_SetString(PyExc_OverflowError,
1017 "float too large to pack with d format");
1018 return -1;
1019 }
1020 else if (e < -1022) {
1021 /* Gradual underflow */
1022 f = ldexp(f, 1022 + e);
1023 e = 0;
1024 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001025 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001026 e += 1023;
1027 f -= 1.0; /* Get rid of leading 1 */
1028 }
1029
1030 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1031 f *= 268435456.0; /* 2**28 */
1032 fhi = (long) floor(f); /* Truncate */
1033 f -= (double)fhi;
1034 f *= 16777216.0; /* 2**24 */
1035 flo = (long) floor(f + 0.5); /* Round */
1036
1037 /* First byte */
1038 *p = (s<<7) | (e>>4);
1039 p++;
1040
1041 /* Second byte */
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001042 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum60456fd1997-04-09 17:36:32 +00001043 p++;
1044
1045 /* Third byte */
1046 *p = (fhi>>16) & 0xFF;
1047 p++;
1048
1049 /* Fourth byte */
1050 *p = (fhi>>8) & 0xFF;
1051 p++;
1052
1053 /* Fifth byte */
1054 *p = fhi & 0xFF;
1055 p++;
1056
1057 /* Sixth byte */
1058 *p = (flo>>16) & 0xFF;
1059 p++;
1060
1061 /* Seventh byte */
1062 *p = (flo>>8) & 0xFF;
1063 p++;
1064
1065 /* Eighth byte */
1066 *p = flo & 0xFF;
1067
1068 if ((*self->write_func)(self, str, 9) < 0)
1069 return -1;
1070 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001071 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001072 char c_str[250];
1073 c_str[0] = FLOAT;
Guido van Rossum104be4a1998-04-03 21:13:02 +00001074 sprintf(c_str + 1, "%.17g\n", x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001075
1076 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
1077 return -1;
1078 }
1079
1080 return 0;
1081}
1082
1083
1084static int
Guido van Rossum142eeb81997-08-13 03:14:41 +00001085save_string(Picklerobject *self, PyObject *args, int doput) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001086 int size, len;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001087 PyObject *repr=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001088
Guido van Rossum053b8df1998-11-25 16:18:00 +00001089 if ((size = PyString_Size(args)) < 0)
1090 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001091
1092 if (!self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001093 char *repr_str;
1094
1095 static char string = STRING;
1096
Guido van Rossum053b8df1998-11-25 16:18:00 +00001097 UNLESS (repr = PyObject_Repr(args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001098 return -1;
1099
Guido van Rossum053b8df1998-11-25 16:18:00 +00001100 if ((len = PyString_Size(repr)) < 0)
1101 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001102 repr_str = PyString_AS_STRING((PyStringObject *)repr);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001103
1104 if ((*self->write_func)(self, &string, 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001105 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001106
1107 if ((*self->write_func)(self, repr_str, len) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001108 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001109
1110 if ((*self->write_func)(self, "\n", 1) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001111 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001112
1113 Py_XDECREF(repr);
1114 }
1115 else {
1116 int i;
1117 char c_str[5];
1118
Guido van Rossum053b8df1998-11-25 16:18:00 +00001119 if ((size = PyString_Size(args)) < 0)
1120 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001121
1122 if (size < 256) {
1123 c_str[0] = SHORT_BINSTRING;
1124 c_str[1] = size;
1125 len = 2;
1126 }
1127 else {
1128 c_str[0] = BINSTRING;
1129 for (i = 1; i < 5; i++)
1130 c_str[i] = (int)(size >> ((i - 1) * 8));
1131 len = 5;
1132 }
1133
1134 if ((*self->write_func)(self, c_str, len) < 0)
1135 return -1;
1136
Guido van Rossum053b8df1998-11-25 16:18:00 +00001137 if (size > 128 && Pdata_Check(self->file)) {
1138 if (write_other(self, NULL, 0) < 0) return -1;
1139 PDATA_APPEND(self->file, args, -1);
1140 }
1141 else {
1142 if ((*self->write_func)(self,
1143 PyString_AS_STRING((PyStringObject *)args), size) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001144 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001145 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001146 }
1147
Guido van Rossum142eeb81997-08-13 03:14:41 +00001148 if (doput)
1149 if (put(self, args) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001150 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001151
1152 return 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001153
1154err:
1155 Py_XDECREF(repr);
1156 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001157}
1158
1159
1160static int
1161save_tuple(Picklerobject *self, PyObject *args) {
1162 PyObject *element = 0, *py_tuple_id = 0;
1163 int len, i, has_key, res = -1;
1164
1165 static char tuple = TUPLE;
1166
1167 if ((*self->write_func)(self, &MARKv, 1) < 0)
1168 goto finally;
1169
1170 if ((len = PyTuple_Size(args)) < 0)
1171 goto finally;
1172
1173 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001174 UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001175 goto finally;
1176
1177 if (save(self, element, 0) < 0)
1178 goto finally;
1179 }
1180
Guido van Rossum053b8df1998-11-25 16:18:00 +00001181 UNLESS (py_tuple_id = PyInt_FromLong((long)args))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001182 goto finally;
1183
1184 if (len) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001185 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
Guido van Rossum60456fd1997-04-09 17:36:32 +00001186 goto finally;
1187
1188 if (has_key) {
1189 if (self->bin) {
1190 static char pop_mark = POP_MARK;
1191
1192 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1193 goto finally;
1194 }
1195 else {
1196 static char pop = POP;
1197
1198 for (i = 0; i <= len; i++) {
1199 if ((*self->write_func)(self, &pop, 1) < 0)
1200 goto finally;
1201 }
1202 }
1203
1204 if (get(self, py_tuple_id) < 0)
1205 goto finally;
1206
1207 res = 0;
1208 goto finally;
1209 }
1210 }
1211
1212 if ((*self->write_func)(self, &tuple, 1) < 0) {
1213 goto finally;
1214 }
1215
1216 if (put(self, args) < 0)
1217 goto finally;
1218
1219 res = 0;
1220
1221finally:
1222 Py_XDECREF(py_tuple_id);
1223
1224 return res;
1225}
1226
1227static int
1228save_empty_tuple(Picklerobject *self, PyObject *args) {
1229 static char tuple = EMPTY_TUPLE;
1230
1231 return (*self->write_func)(self, &tuple, 1);
1232}
1233
1234
1235static int
1236save_list(Picklerobject *self, PyObject *args) {
1237 PyObject *element = 0;
1238 int s_len, len, i, using_appends, res = -1;
1239 char s[3];
1240
1241 static char append = APPEND, appends = APPENDS;
1242
Guido van Rossum053b8df1998-11-25 16:18:00 +00001243 if (self->bin) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001244 s[0] = EMPTY_LIST;
1245 s_len = 1;
1246 }
1247 else {
1248 s[0] = MARK;
1249 s[1] = LIST;
1250 s_len = 2;
1251 }
1252
1253 if ((len = PyList_Size(args)) < 0)
1254 goto finally;
1255
1256 if ((*self->write_func)(self, s, s_len) < 0)
1257 goto finally;
1258
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001259 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001260 if (put(self, args) < 0)
1261 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001262 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001263 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001264 if (put2(self, args) < 0)
1265 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001266 }
1267
Guido van Rossum142eeb81997-08-13 03:14:41 +00001268 if ((using_appends = (self->bin && (len > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001269 if ((*self->write_func)(self, &MARKv, 1) < 0)
1270 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001271
Guido van Rossum60456fd1997-04-09 17:36:32 +00001272 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001273 UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001274 goto finally;
1275
1276 if (save(self, element, 0) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00001277 goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001278
1279 if (!using_appends) {
1280 if ((*self->write_func)(self, &append, 1) < 0)
1281 goto finally;
1282 }
1283 }
1284
1285 if (using_appends) {
1286 if ((*self->write_func)(self, &appends, 1) < 0)
1287 goto finally;
1288 }
1289
1290 res = 0;
1291
1292finally:
1293
1294 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001295}
1296
1297
Guido van Rossum60456fd1997-04-09 17:36:32 +00001298static int
1299save_dict(Picklerobject *self, PyObject *args) {
1300 PyObject *key = 0, *value = 0;
1301 int i, len, res = -1, using_setitems;
1302 char s[3];
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001303
Guido van Rossum60456fd1997-04-09 17:36:32 +00001304 static char setitem = SETITEM, setitems = SETITEMS;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001305
Guido van Rossum60456fd1997-04-09 17:36:32 +00001306 if (self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001307 s[0] = EMPTY_DICT;
1308 len = 1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001309 }
1310 else {
1311 s[0] = MARK;
1312 s[1] = DICT;
1313 len = 2;
1314 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001315
Guido van Rossum60456fd1997-04-09 17:36:32 +00001316 if ((*self->write_func)(self, s, len) < 0)
1317 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001318
Guido van Rossum60456fd1997-04-09 17:36:32 +00001319 if ((len = PyDict_Size(args)) < 0)
1320 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001321
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001322 if (len == 0) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001323 if (put(self, args) < 0)
1324 goto finally;
1325 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001326 else {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001327 if (put2(self, args) < 0)
1328 goto finally;
1329 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001330
Guido van Rossum142eeb81997-08-13 03:14:41 +00001331 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001332 if ((*self->write_func)(self, &MARKv, 1) < 0)
1333 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001334
Guido van Rossum60456fd1997-04-09 17:36:32 +00001335 i = 0;
1336 while (PyDict_Next(args, &i, &key, &value)) {
1337 if (save(self, key, 0) < 0)
1338 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001339
Guido van Rossum60456fd1997-04-09 17:36:32 +00001340 if (save(self, value, 0) < 0)
1341 goto finally;
1342
1343 if (!using_setitems) {
1344 if ((*self->write_func)(self, &setitem, 1) < 0)
1345 goto finally;
1346 }
1347 }
1348
1349 if (using_setitems) {
1350 if ((*self->write_func)(self, &setitems, 1) < 0)
1351 goto finally;
1352 }
1353
1354 res = 0;
1355
1356finally:
1357
1358 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001359}
1360
1361
Guido van Rossum60456fd1997-04-09 17:36:32 +00001362static int
1363save_inst(Picklerobject *self, PyObject *args) {
1364 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1365 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1366 char *module_str, *name_str;
1367 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001368
Guido van Rossum60456fd1997-04-09 17:36:32 +00001369 static char inst = INST, obj = OBJ, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001370
Guido van Rossum60456fd1997-04-09 17:36:32 +00001371 if ((*self->write_func)(self, &MARKv, 1) < 0)
1372 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001373
Guido van Rossum053b8df1998-11-25 16:18:00 +00001374 UNLESS (class = PyObject_GetAttr(args, __class___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001375 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001376
Guido van Rossum60456fd1997-04-09 17:36:32 +00001377 if (self->bin) {
1378 if (save(self, class, 0) < 0)
1379 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001380 }
1381
Guido van Rossum142eeb81997-08-13 03:14:41 +00001382 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001383 PyObject *element = 0;
1384 int i, len;
1385
Guido van Rossum053b8df1998-11-25 16:18:00 +00001386 UNLESS (class_args =
Guido van Rossum60456fd1997-04-09 17:36:32 +00001387 PyObject_CallObject(getinitargs_func, empty_tuple))
1388 goto finally;
1389
1390 if ((len = PyObject_Length(class_args)) < 0)
1391 goto finally;
1392
1393 for (i = 0; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001394 UNLESS (element = PySequence_GetItem(class_args, i))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001395 goto finally;
1396
1397 if (save(self, element, 0) < 0) {
1398 Py_DECREF(element);
1399 goto finally;
1400 }
1401
1402 Py_DECREF(element);
1403 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001404 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001405 else {
1406 PyErr_Clear();
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001407 }
1408
Guido van Rossum60456fd1997-04-09 17:36:32 +00001409 if (!self->bin) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001410 UNLESS (name = ((PyClassObject *)class)->cl_name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001411 PyErr_SetString(PicklingError, "class has no name");
1412 goto finally;
1413 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001414
Guido van Rossum053b8df1998-11-25 16:18:00 +00001415 UNLESS (module = whichmodule(class, name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001416 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001417
1418
1419 if ((module_size = PyString_Size(module)) < 0 ||
1420 (name_size = PyString_Size(name)) < 0)
1421 goto finally;
1422
Guido van Rossum60456fd1997-04-09 17:36:32 +00001423 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001424 name_str = PyString_AS_STRING((PyStringObject *)name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001425
Guido van Rossum60456fd1997-04-09 17:36:32 +00001426 if ((*self->write_func)(self, &inst, 1) < 0)
1427 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001428
Guido van Rossum60456fd1997-04-09 17:36:32 +00001429 if ((*self->write_func)(self, module_str, module_size) < 0)
1430 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001431
Guido van Rossum60456fd1997-04-09 17:36:32 +00001432 if ((*self->write_func)(self, "\n", 1) < 0)
1433 goto finally;
1434
1435 if ((*self->write_func)(self, name_str, name_size) < 0)
1436 goto finally;
1437
1438 if ((*self->write_func)(self, "\n", 1) < 0)
1439 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001440 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00001441 else if ((*self->write_func)(self, &obj, 1) < 0) {
1442 goto finally;
1443 }
1444
Guido van Rossum142eeb81997-08-13 03:14:41 +00001445 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001446 UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001447 goto finally;
1448 }
1449 else {
1450 PyErr_Clear();
1451
Guido van Rossum053b8df1998-11-25 16:18:00 +00001452 UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001453 PyErr_Clear();
1454 res = 0;
1455 goto finally;
1456 }
1457 }
1458
1459 if (!PyDict_Check(state)) {
1460 if (put2(self, args) < 0)
1461 goto finally;
1462 }
1463 else {
1464 if (put(self, args) < 0)
1465 goto finally;
1466 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001467
Guido van Rossum60456fd1997-04-09 17:36:32 +00001468 if (save(self, state, 0) < 0)
1469 goto finally;
1470
1471 if ((*self->write_func)(self, &build, 1) < 0)
1472 goto finally;
1473
1474 res = 0;
1475
1476finally:
1477 Py_XDECREF(module);
1478 Py_XDECREF(class);
1479 Py_XDECREF(state);
1480 Py_XDECREF(getinitargs_func);
1481 Py_XDECREF(getstate_func);
1482 Py_XDECREF(class_args);
1483
1484 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001485}
1486
1487
Guido van Rossum60456fd1997-04-09 17:36:32 +00001488static int
1489save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1490 PyObject *global_name = 0, *module = 0;
1491 char *name_str, *module_str;
1492 int module_size, name_size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001493
Guido van Rossum60456fd1997-04-09 17:36:32 +00001494 static char global = GLOBAL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001495
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001496 if (name) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001497 global_name = name;
1498 Py_INCREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001499 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001500 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001501 UNLESS (global_name = PyObject_GetAttr(args, __name___str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001502 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001503 }
1504
Guido van Rossum053b8df1998-11-25 16:18:00 +00001505 UNLESS (module = whichmodule(args, global_name))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001506 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001507
Guido van Rossum053b8df1998-11-25 16:18:00 +00001508 if ((module_size = PyString_Size(module)) < 0 ||
1509 (name_size = PyString_Size(global_name)) < 0)
1510 goto finally;
1511
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001512 module_str = PyString_AS_STRING((PyStringObject *)module);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001513 name_str = PyString_AS_STRING((PyStringObject *)global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001514
Guido van Rossum60456fd1997-04-09 17:36:32 +00001515 if ((*self->write_func)(self, &global, 1) < 0)
1516 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001517
Guido van Rossum60456fd1997-04-09 17:36:32 +00001518 if ((*self->write_func)(self, module_str, module_size) < 0)
1519 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001520
Guido van Rossum60456fd1997-04-09 17:36:32 +00001521 if ((*self->write_func)(self, "\n", 1) < 0)
1522 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001523
Guido van Rossum60456fd1997-04-09 17:36:32 +00001524 if ((*self->write_func)(self, name_str, name_size) < 0)
1525 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001526
Guido van Rossum60456fd1997-04-09 17:36:32 +00001527 if ((*self->write_func)(self, "\n", 1) < 0)
1528 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001529
Guido van Rossum60456fd1997-04-09 17:36:32 +00001530 if (put(self, args) < 0)
1531 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001532
Guido van Rossum60456fd1997-04-09 17:36:32 +00001533 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001534
Guido van Rossum60456fd1997-04-09 17:36:32 +00001535finally:
1536 Py_XDECREF(module);
1537 Py_XDECREF(global_name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001538
Guido van Rossum60456fd1997-04-09 17:36:32 +00001539 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001540}
1541
Guido van Rossum60456fd1997-04-09 17:36:32 +00001542static int
1543save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1544 PyObject *pid = 0;
1545 int size, res = -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001546
Guido van Rossum60456fd1997-04-09 17:36:32 +00001547 static char persid = PERSID, binpersid = BINPERSID;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001548
Guido van Rossum053b8df1998-11-25 16:18:00 +00001549 Py_INCREF(args);
1550 ARG_TUP(self, args);
1551 if (self->arg) {
1552 pid = PyObject_CallObject(f, self->arg);
1553 FREE_ARG_TUP(self);
1554 }
1555 if (! pid) return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001556
Guido van Rossum60456fd1997-04-09 17:36:32 +00001557 if (pid != Py_None) {
1558 if (!self->bin) {
1559 if (!PyString_Check(pid)) {
1560 PyErr_SetString(PicklingError,
1561 "persistent id must be string");
1562 goto finally;
1563 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001564
Guido van Rossum60456fd1997-04-09 17:36:32 +00001565 if ((*self->write_func)(self, &persid, 1) < 0)
1566 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001567
Guido van Rossum60456fd1997-04-09 17:36:32 +00001568 if ((size = PyString_Size(pid)) < 0)
1569 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001570
Guido van Rossum60456fd1997-04-09 17:36:32 +00001571 if ((*self->write_func)(self,
1572 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1573 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001574
Guido van Rossum60456fd1997-04-09 17:36:32 +00001575 if ((*self->write_func)(self, "\n", 1) < 0)
1576 goto finally;
1577
1578 res = 1;
1579 goto finally;
1580 }
1581 else if (save(self, pid, 1) >= 0) {
1582 if ((*self->write_func)(self, &binpersid, 1) < 0)
1583 res = -1;
1584 else
1585 res = 1;
1586 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001587
Guido van Rossum60456fd1997-04-09 17:36:32 +00001588 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001589 }
1590
Guido van Rossum60456fd1997-04-09 17:36:32 +00001591 res = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001592
Guido van Rossum60456fd1997-04-09 17:36:32 +00001593finally:
1594 Py_XDECREF(pid);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001595
Guido van Rossum60456fd1997-04-09 17:36:32 +00001596 return res;
1597}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001598
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001599
Guido van Rossum60456fd1997-04-09 17:36:32 +00001600static int
1601save_reduce(Picklerobject *self, PyObject *callable,
1602 PyObject *tup, PyObject *state, PyObject *ob) {
1603 static char reduce = REDUCE, build = BUILD;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001604
Guido van Rossum60456fd1997-04-09 17:36:32 +00001605 if (save(self, callable, 0) < 0)
1606 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001607
Guido van Rossum60456fd1997-04-09 17:36:32 +00001608 if (save(self, tup, 0) < 0)
1609 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001610
Guido van Rossum60456fd1997-04-09 17:36:32 +00001611 if ((*self->write_func)(self, &reduce, 1) < 0)
1612 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001613
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001614 if (ob != NULL) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001615 if (state && !PyDict_Check(state)) {
1616 if (put2(self, ob) < 0)
1617 return -1;
1618 }
1619 else {
1620 if (put(self, ob) < 0)
1621 return -1;
1622 }
1623 }
1624
Guido van Rossumfdde96c1997-12-04 01:13:01 +00001625 if (state) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001626 if (save(self, state, 0) < 0)
1627 return -1;
1628
1629 if ((*self->write_func)(self, &build, 1) < 0)
1630 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001631 }
1632
Guido van Rossum60456fd1997-04-09 17:36:32 +00001633 return 0;
1634}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001635
Guido van Rossum60456fd1997-04-09 17:36:32 +00001636static int
1637save(Picklerobject *self, PyObject *args, int pers_save) {
1638 PyTypeObject *type;
1639 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
Guido van Rossum142eeb81997-08-13 03:14:41 +00001640 *callable = 0, *state = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001641 int res = -1, tmp, size;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001642
Guido van Rossum60456fd1997-04-09 17:36:32 +00001643 if (!pers_save && self->pers_func) {
1644 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1645 res = tmp;
1646 goto finally;
1647 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001648 }
1649
Guido van Rossum60456fd1997-04-09 17:36:32 +00001650 if (args == Py_None) {
1651 res = save_none(self, args);
1652 goto finally;
1653 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001654
Guido van Rossum60456fd1997-04-09 17:36:32 +00001655 type = args->ob_type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001656
Guido van Rossum60456fd1997-04-09 17:36:32 +00001657 switch (type->tp_name[0]) {
1658 case 'i':
1659 if (type == &PyInt_Type) {
1660 res = save_int(self, args);
1661 goto finally;
1662 }
1663 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001664
Guido van Rossum60456fd1997-04-09 17:36:32 +00001665 case 'l':
1666 if (type == &PyLong_Type) {
1667 res = save_long(self, args);
1668 goto finally;
1669 }
1670 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001671
Guido van Rossum60456fd1997-04-09 17:36:32 +00001672 case 'f':
1673 if (type == &PyFloat_Type) {
1674 res = save_float(self, args);
1675 goto finally;
1676 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001677 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001678
Guido van Rossum60456fd1997-04-09 17:36:32 +00001679 case 't':
1680 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001681 if (self->bin) res = save_empty_tuple(self, args);
1682 else res = save_tuple(self, args);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001683 goto finally;
1684 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001685 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001686
Guido van Rossum60456fd1997-04-09 17:36:32 +00001687 case 's':
Guido van Rossum053b8df1998-11-25 16:18:00 +00001688 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001689 res = save_string(self, args, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001690 goto finally;
1691 }
1692 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001693
Guido van Rossum60456fd1997-04-09 17:36:32 +00001694 if (args->ob_refcnt > 1) {
1695 long ob_id;
1696 int has_key;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001697
Guido van Rossum60456fd1997-04-09 17:36:32 +00001698 ob_id = (long)args;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001699
Guido van Rossum053b8df1998-11-25 16:18:00 +00001700 UNLESS (py_ob_id = PyInt_FromLong(ob_id))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001701 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001702
Guido van Rossum60456fd1997-04-09 17:36:32 +00001703 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1704 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001705
Guido van Rossum60456fd1997-04-09 17:36:32 +00001706 if (has_key) {
1707 if (get(self, py_ob_id) < 0)
1708 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001709
Guido van Rossum60456fd1997-04-09 17:36:32 +00001710 res = 0;
1711 goto finally;
1712 }
1713 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001714
Guido van Rossum60456fd1997-04-09 17:36:32 +00001715 switch (type->tp_name[0]) {
1716 case 's':
1717 if (type == &PyString_Type) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00001718 res = save_string(self, args, 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00001719 goto finally;
1720 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001721 break;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00001722
Guido van Rossum60456fd1997-04-09 17:36:32 +00001723 case 't':
1724 if (type == &PyTuple_Type) {
1725 res = save_tuple(self, args);
1726 goto finally;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001727 }
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 == &PyList_Type) {
1732 res = save_list(self, args);
1733 goto finally;
1734 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001735 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001736
1737 case 'd':
1738 if (type == &PyDict_Type) {
1739 res = save_dict(self, args);
1740 goto finally;
1741 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001742 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001743
1744 case 'i':
1745 if (type == &PyInstance_Type) {
1746 res = save_inst(self, args);
1747 goto finally;
1748 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001749 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001750
1751 case 'c':
1752 if (type == &PyClass_Type) {
1753 res = save_global(self, args, NULL);
1754 goto finally;
1755 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001756 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001757
1758 case 'f':
1759 if (type == &PyFunction_Type) {
1760 res = save_global(self, args, NULL);
1761 goto finally;
1762 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00001763 break;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001764
1765 case 'b':
1766 if (type == &PyCFunction_Type) {
1767 res = save_global(self, args, NULL);
1768 goto finally;
1769 }
1770 }
1771
1772 if (!pers_save && self->inst_pers_func) {
1773 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1774 res = tmp;
1775 goto finally;
1776 }
1777 }
1778
Guido van Rossum142eeb81997-08-13 03:14:41 +00001779 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00001780 Py_INCREF(__reduce__);
1781
Guido van Rossum60456fd1997-04-09 17:36:32 +00001782 Py_INCREF(args);
Guido van Rossum053b8df1998-11-25 16:18:00 +00001783 ARG_TUP(self, args);
1784 if (self->arg) {
1785 t = PyObject_CallObject(__reduce__, self->arg);
1786 FREE_ARG_TUP(self);
1787 }
1788 if (! t) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00001789 }
1790 else {
1791 PyErr_Clear();
1792
Guido van Rossum142eeb81997-08-13 03:14:41 +00001793 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00001794 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
Guido van Rossum60456fd1997-04-09 17:36:32 +00001795 goto finally;
1796 }
1797 else {
1798 PyErr_Clear();
1799 }
1800 }
1801
1802 if (t) {
1803 if (PyString_Check(t)) {
1804 res = save_global(self, args, t);
1805 goto finally;
1806 }
1807
1808 if (!PyTuple_Check(t)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001809 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001810 "be a tuple", "O", __reduce__);
1811 goto finally;
1812 }
1813
1814 size = PyTuple_Size(t);
1815
1816 if ((size != 3) && (size != 2)) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001817 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001818 "contain only two or three elements", "O", __reduce__);
1819 goto finally;
1820 }
1821
1822 callable = PyTuple_GET_ITEM(t, 0);
Guido van Rossum142eeb81997-08-13 03:14:41 +00001823
Guido van Rossum60456fd1997-04-09 17:36:32 +00001824 arg_tup = PyTuple_GET_ITEM(t, 1);
1825
1826 if (size > 2) {
1827 state = PyTuple_GET_ITEM(t, 2);
1828 }
1829
Guido van Rossum053b8df1998-11-25 16:18:00 +00001830 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001831 cPickle_ErrFormat(PicklingError, "Second element of tuple "
Guido van Rossum60456fd1997-04-09 17:36:32 +00001832 "returned by %s must be a tuple", "O", __reduce__);
1833 goto finally;
1834 }
1835
1836 res = save_reduce(self, callable, arg_tup, state, args);
1837 goto finally;
1838 }
1839
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00001840 cPickle_ErrFormat(PicklingError, "Cannot pickle %s objects.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00001841 "O", (PyObject *)type);
1842
1843finally:
1844 Py_XDECREF(py_ob_id);
1845 Py_XDECREF(__reduce__);
1846 Py_XDECREF(t);
1847
1848 return res;
1849}
1850
1851
1852static int
1853dump(Picklerobject *self, PyObject *args) {
1854 static char stop = STOP;
1855
1856 if (save(self, args, 0) < 0)
1857 return -1;
1858
1859 if ((*self->write_func)(self, &stop, 1) < 0)
1860 return -1;
1861
1862 if ((*self->write_func)(self, NULL, 0) < 0)
1863 return -1;
1864
1865 return 0;
1866}
1867
1868static PyObject *
Guido van Rossum053b8df1998-11-25 16:18:00 +00001869Pickle_clear_memo(Picklerobject *self, PyObject *args) {
1870 if (args && ! PyArg_ParseTuple(args,"")) return NULL;
1871 if (self->memo) PyDict_Clear(self->memo);
1872 Py_INCREF(Py_None);
1873 return Py_None;
1874}
1875
1876static PyObject *
1877Pickle_getvalue(Picklerobject *self, PyObject *args) {
1878 int l, i, rsize, ssize, clear=1, lm;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00001879 long ik;
Guido van Rossum053b8df1998-11-25 16:18:00 +00001880 PyObject *k, *r;
1881 char *s, *p, *have_get;
1882 Pdata *data;
1883
1884 if (args && ! PyArg_ParseTuple(args,"|i",&clear)) return NULL;
1885
1886 /* Check to make sure we are based on a list */
1887 if (! Pdata_Check(self->file)) {
1888 PyErr_SetString(PicklingError,
1889 "Attempt to getvalue a non-list-based pickler");
1890 return NULL;
1891 }
1892
1893 /* flush write buffer */
1894 if (write_other(self, NULL, 0) < 0) return NULL;
1895
1896 data=(Pdata*)self->file;
1897 l=data->length;
1898
1899 /* set up an array to hold get/put status */
1900 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
1901 lm++;
1902 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
1903 memset(have_get,0,lm);
1904
1905 /* Scan for gets. */
1906 for (rsize=0, i=l; --i >= 0; ) {
1907 k=data->data[i];
1908
1909 if (PyString_Check(k)) {
1910 rsize += PyString_GET_SIZE(k);
1911 }
1912
1913 else if (PyInt_Check(k)) { /* put */
1914 ik=PyInt_AS_LONG((PyIntObject*)k);
1915 if (ik >= lm || ik==0) {
1916 PyErr_SetString(PicklingError,
1917 "Invalid get data");
1918 return NULL;
1919 }
1920 if (have_get[ik]) { /* with matching get */
1921 if (ik < 256) rsize += 2;
1922 else rsize+=5;
1923 }
1924 }
1925
1926 else if (! (PyTuple_Check(k) &&
1927 PyTuple_GET_SIZE(k) == 2 &&
1928 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
1929 ) {
1930 PyErr_SetString(PicklingError,
1931 "Unexpected data in internal list");
1932 return NULL;
1933 }
1934
1935 else { /* put */
1936 ik=PyInt_AS_LONG((PyIntObject*)k);
1937 if (ik >= lm || ik==0) {
1938 PyErr_SetString(PicklingError,
1939 "Invalid get data");
1940 return NULL;
1941 }
1942 have_get[ik]=1;
1943 if (ik < 256) rsize += 2;
1944 else rsize+=5;
1945 }
1946
1947 }
1948
1949 /* Now generate the result */
1950 UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
1951 s=PyString_AS_STRING((PyStringObject*)r);
1952
1953 for (i=0; i<l; i++) {
1954 k=data->data[i];
1955
1956 if (PyString_Check(k)) {
1957 ssize=PyString_GET_SIZE(k);
1958 if (ssize) {
1959 p=PyString_AS_STRING((PyStringObject*)k);
1960 while (--ssize >= 0) *s++=*p++;
1961 }
1962 }
1963
1964 else if (PyTuple_Check(k)) { /* get */
1965 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
1966 if (ik < 256) {
1967 *s++ = BINGET;
1968 *s++ = (int)(ik & 0xff);
1969 }
1970 else {
1971 *s++ = LONG_BINGET;
1972 *s++ = (int)(ik & 0xff);
1973 *s++ = (int)((ik >> 8) & 0xff);
1974 *s++ = (int)((ik >> 16) & 0xff);
1975 *s++ = (int)((ik >> 24) & 0xff);
1976 }
1977 }
1978
1979 else { /* put */
1980 ik=PyInt_AS_LONG((PyIntObject*)k);
1981
1982 if (have_get[ik]) { /* with matching get */
1983 if (ik < 256) {
1984 *s++ = BINPUT;
1985 *s++ = (int)(ik & 0xff);
1986 }
1987 else {
1988 *s++ = LONG_BINPUT;
1989 *s++ = (int)(ik & 0xff);
1990 *s++ = (int)((ik >> 8) & 0xff);
1991 *s++ = (int)((ik >> 16) & 0xff);
1992 *s++ = (int)((ik >> 24) & 0xff);
1993 }
1994 }
1995 }
1996
1997 }
1998
1999 if (clear) {
2000 PyDict_Clear(self->memo);
2001 Pdata_clear(data,0);
2002 }
2003
2004 free(have_get);
2005 return r;
2006err:
2007 free(have_get);
2008 return NULL;
2009}
2010
2011static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002012Pickler_dump(Picklerobject *self, PyObject *args) {
2013 PyObject *ob;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002014 int get=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002015
Guido van Rossum053b8df1998-11-25 16:18:00 +00002016 UNLESS (PyArg_ParseTuple(args, "O|i", &ob, &get))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002017 return NULL;
2018
2019 if (dump(self, ob) < 0)
2020 return NULL;
2021
Guido van Rossum053b8df1998-11-25 16:18:00 +00002022 if (get) return Pickle_getvalue(self, NULL);
2023
2024 Py_INCREF(self);
2025 return (PyObject*)self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002026}
2027
2028
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002029static struct PyMethodDef Pickler_methods[] = {
Guido van Rossum142eeb81997-08-13 03:14:41 +00002030 {"dump", (PyCFunction)Pickler_dump, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002031 "dump(object) --"
2032 "Write an object in pickle format to the object's pickle stream\n"
2033 },
Guido van Rossum142eeb81997-08-13 03:14:41 +00002034 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002035 "clear_memo() -- Clear the picklers memo"},
Guido van Rossum053b8df1998-11-25 16:18:00 +00002036 {"getvalue", (PyCFunction)Pickle_getvalue, 1,
2037 "getvalue() -- Finish picking a list-based pickle"},
Guido van Rossum60456fd1997-04-09 17:36:32 +00002038 {NULL, NULL} /* sentinel */
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002039};
2040
2041
2042static Picklerobject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002043newPicklerobject(PyObject *file, int bin) {
2044 Picklerobject *self;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002045
Guido van Rossum053b8df1998-11-25 16:18:00 +00002046 UNLESS (self = PyObject_NEW(Picklerobject, &Picklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002047 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002048
2049 self->fp = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002050 self->write = NULL;
2051 self->memo = NULL;
2052 self->arg = NULL;
2053 self->pers_func = NULL;
2054 self->inst_pers_func = NULL;
2055 self->write_buf = NULL;
2056 self->bin = bin;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002057 self->fast = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002058 self->buf_size = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002059 self->dispatch_table = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002060
Guido van Rossum053b8df1998-11-25 16:18:00 +00002061 if (file)
2062 Py_INCREF(file);
2063 else
Guido van Rossum50f385c1998-12-04 18:48:44 +00002064 file=Pdata_New();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002065
Guido van Rossum60456fd1997-04-09 17:36:32 +00002066 self->file = file;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002067
Guido van Rossum053b8df1998-11-25 16:18:00 +00002068 UNLESS (self->memo = PyDict_New()) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002069 Py_XDECREF((PyObject *)self);
2070 return NULL;
2071 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002072
Guido van Rossum60456fd1997-04-09 17:36:32 +00002073 if (PyFile_Check(file)) {
2074 self->fp = PyFile_AsFile(file);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00002075 if (self->fp == NULL) {
2076 PyErr_SetString(PyExc_IOError, "output file closed");
2077 return NULL;
2078 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002079 self->write_func = write_file;
2080 }
2081 else if (PycStringIO_OutputCheck(file)) {
2082 self->write_func = write_cStringIO;
2083 }
Guido van Rossum142eeb81997-08-13 03:14:41 +00002084 else if (file == Py_None) {
2085 self->write_func = write_none;
2086 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002087 else {
2088 self->write_func = write_other;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002089
Guido van Rossum053b8df1998-11-25 16:18:00 +00002090 if (! Pdata_Check(file)) {
2091 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002092 PyErr_Clear();
2093 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
Guido van Rossum053b8df1998-11-25 16:18:00 +00002094 "attribute");
2095 goto err;
2096 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002097 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002098
Guido van Rossum053b8df1998-11-25 16:18:00 +00002099 UNLESS (self->write_buf =
Guido van Rossum60456fd1997-04-09 17:36:32 +00002100 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
2101 PyErr_NoMemory();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002102 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002103 }
2104 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002105
Guido van Rossum053b8df1998-11-25 16:18:00 +00002106 if (PyEval_GetRestricted()) {
2107 /* Restricted execution, get private tables */
2108 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002109
Guido van Rossum053b8df1998-11-25 16:18:00 +00002110 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2111 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2112 Py_DECREF(m);
2113 UNLESS (self->dispatch_table) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002114 }
2115 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002116 self->dispatch_table=dispatch_table;
2117 Py_INCREF(dispatch_table);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002118 }
2119
Guido van Rossum60456fd1997-04-09 17:36:32 +00002120 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002121
2122err:
2123 Py_DECREF((PyObject *)self);
2124 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002125}
2126
2127
2128static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002129get_Pickler(PyObject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002130 PyObject *file=NULL;
2131 int bin;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002132
Guido van Rossum053b8df1998-11-25 16:18:00 +00002133 bin=1;
2134 if (! PyArg_ParseTuple(args, "|i", &bin)) {
2135 PyErr_Clear();
2136 bin=0;
2137 if (! PyArg_ParseTuple(args, "O|i", &file, &bin))
2138 return NULL;
2139 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002140 return (PyObject *)newPicklerobject(file, bin);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002141}
2142
2143
2144static void
Guido van Rossum60456fd1997-04-09 17:36:32 +00002145Pickler_dealloc(Picklerobject *self) {
2146 Py_XDECREF(self->write);
2147 Py_XDECREF(self->memo);
2148 Py_XDECREF(self->arg);
2149 Py_XDECREF(self->file);
2150 Py_XDECREF(self->pers_func);
2151 Py_XDECREF(self->inst_pers_func);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002152 Py_XDECREF(self->dispatch_table);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002153
2154 if (self->write_buf) {
2155 free(self->write_buf);
2156 }
2157
2158 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002159}
2160
2161
2162static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00002163Pickler_getattr(Picklerobject *self, char *name) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002164
2165 switch (*name) {
2166 case 'p':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002167 if (strcmp(name, "persistent_id") == 0) {
2168 if (!self->pers_func) {
2169 PyErr_SetString(PyExc_AttributeError, name);
2170 return NULL;
2171 }
2172
2173 Py_INCREF(self->pers_func);
2174 return self->pers_func;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002175 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002176 break;
2177 case 'm':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002178 if (strcmp(name, "memo") == 0) {
2179 if (!self->memo) {
2180 PyErr_SetString(PyExc_AttributeError, name);
2181 return NULL;
2182 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002183
Guido van Rossum60456fd1997-04-09 17:36:32 +00002184 Py_INCREF(self->memo);
2185 return self->memo;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002186 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002187 break;
2188 case 'P':
Guido van Rossum60456fd1997-04-09 17:36:32 +00002189 if (strcmp(name, "PicklingError") == 0) {
2190 Py_INCREF(PicklingError);
2191 return PicklingError;
2192 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002193 break;
2194 case 'b':
2195 if (strcmp(name, "binary")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002196 return PyInt_FromLong(self->bin);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002197 break;
2198 case 'f':
2199 if (strcmp(name, "fast")==0)
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002200 return PyInt_FromLong(self->fast);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002201 break;
2202 case 'g':
2203 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2204 PyErr_SetString(PyExc_AttributeError, name);
2205 return NULL;
2206 }
2207 break;
2208 }
2209 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002210}
2211
2212
2213int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002214Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002215
Guido van Rossum053b8df1998-11-25 16:18:00 +00002216 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002217 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002218 "attribute deletion is not supported");
2219 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002220 }
2221
Guido van Rossum60456fd1997-04-09 17:36:32 +00002222 if (strcmp(name, "persistent_id") == 0) {
2223 Py_XDECREF(self->pers_func);
2224 self->pers_func = value;
2225 Py_INCREF(value);
2226 return 0;
2227 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002228
Guido van Rossum60456fd1997-04-09 17:36:32 +00002229 if (strcmp(name, "inst_persistent_id") == 0) {
2230 Py_XDECREF(self->inst_pers_func);
2231 self->inst_pers_func = value;
2232 Py_INCREF(value);
2233 return 0;
2234 }
2235
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002236 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002237 if (! PyDict_Check(value)) {
2238 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2239 return -1;
2240 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002241 Py_XDECREF(self->memo);
2242 self->memo = value;
2243 Py_INCREF(value);
2244 return 0;
2245 }
2246
Guido van Rossum053b8df1998-11-25 16:18:00 +00002247 if (strcmp(name, "binary")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002248 self->bin=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002249 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002250 }
2251
Guido van Rossum053b8df1998-11-25 16:18:00 +00002252 if (strcmp(name, "fast")==0) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002253 self->fast=PyObject_IsTrue(value);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002254 return 0;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00002255 }
2256
Guido van Rossum60456fd1997-04-09 17:36:32 +00002257 PyErr_SetString(PyExc_AttributeError, name);
2258 return -1;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002259}
2260
2261
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002262static char Picklertype__doc__[] =
2263"Objects that know how to pickle objects\n"
2264;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002265
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002266static PyTypeObject Picklertype = {
2267 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00002268 0, /*ob_size*/
2269 "Pickler", /*tp_name*/
2270 sizeof(Picklerobject), /*tp_basicsize*/
2271 0, /*tp_itemsize*/
2272 /* methods */
2273 (destructor)Pickler_dealloc, /*tp_dealloc*/
2274 (printfunc)0, /*tp_print*/
2275 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2276 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2277 (cmpfunc)0, /*tp_compare*/
2278 (reprfunc)0, /*tp_repr*/
2279 0, /*tp_as_number*/
2280 0, /*tp_as_sequence*/
2281 0, /*tp_as_mapping*/
2282 (hashfunc)0, /*tp_hash*/
2283 (ternaryfunc)0, /*tp_call*/
2284 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002285
Guido van Rossum60456fd1997-04-09 17:36:32 +00002286 /* Space for future expansion */
2287 0L,0L,0L,0L,
2288 Picklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002289};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002290
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002291static PyObject *
Guido van Rossume2d81cd1998-08-08 19:40:10 +00002292find_class(PyObject *py_module_name, PyObject *py_global_name) {
2293 PyObject *global = 0, *module;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002294
Jeremy Hyltond1055231998-08-11 19:52:51 +00002295 module = PySys_GetObject("modules");
2296 if (module == NULL)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002297 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002298
2299 module = PyDict_GetItem(module, py_module_name);
2300 if (module == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002301 module = PyImport_Import(py_module_name);
2302 if (!module)
2303 return NULL;
2304 global = PyObject_GetAttr(module, py_global_name);
2305 Py_DECREF(module);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002306 }
Jeremy Hyltond1055231998-08-11 19:52:51 +00002307 else
Guido van Rossum053b8df1998-11-25 16:18:00 +00002308 global = PyObject_GetAttr(module, py_global_name);
Jeremy Hyltond1055231998-08-11 19:52:51 +00002309 if (global == NULL) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002310 char buf[256 + 37];
2311 sprintf(buf, "Failed to import class %.128s from module %.128s",
2312 PyString_AS_STRING((PyStringObject*)py_global_name),
2313 PyString_AS_STRING((PyStringObject*)py_module_name));
2314 PyErr_SetString(PyExc_SystemError, buf);
2315 return NULL;
Jeremy Hyltond1055231998-08-11 19:52:51 +00002316 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002317 return global;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002318}
2319
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002320static int
Guido van Rossum60456fd1997-04-09 17:36:32 +00002321marker(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002322 if (self->num_marks < 1) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002323 PyErr_SetString(UnpicklingError, "could not find MARK");
2324 return -1;
2325 }
2326
2327 return self->marks[--self->num_marks];
2328}
2329
2330
2331static int
2332load_none(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002333 PDATA_APPEND(self->stack, Py_None, -1);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002334 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002335}
2336
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002337static int
2338bad_readline() {
2339 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2340 return -1;
2341}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002342
2343static int
2344load_int(Unpicklerobject *self) {
2345 PyObject *py_int = 0;
2346 char *endptr, *s;
2347 int len, res = -1;
2348 long l;
2349
2350 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002351 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002352 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002353
2354 errno = 0;
2355 l = strtol(s, &endptr, 0);
2356
2357 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2358 /* Hm, maybe we've got something long. Let's try reading
Guido van Rossum053b8df1998-11-25 16:18:00 +00002359 it as a Python long object. */
Guido van Rossum60456fd1997-04-09 17:36:32 +00002360 errno=0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002361 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002362
Guido van Rossum053b8df1998-11-25 16:18:00 +00002363 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2364 PyErr_SetString(PyExc_ValueError,
2365 "could not convert string to int");
2366 goto finally;
2367 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002368 }
2369 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002370 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002371 }
2372
Guido van Rossum053b8df1998-11-25 16:18:00 +00002373 free(s);
2374 PDATA_PUSH(self->stack, py_int, -1);
2375 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002376
2377finally:
2378 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002379
2380 return res;
2381}
2382
2383
2384static long
2385calc_binint(char *s, int x) {
2386 unsigned char c;
2387 int i;
2388 long l;
2389
2390 for (i = 0, l = 0L; i < x; i++) {
2391 c = (unsigned char)s[i];
2392 l |= (long)c << (i * 8);
2393 }
2394
2395 return l;
2396}
2397
2398
2399static int
2400load_binintx(Unpicklerobject *self, char *s, int x) {
2401 PyObject *py_int = 0;
2402 long l;
2403
2404 l = calc_binint(s, x);
2405
Guido van Rossum053b8df1998-11-25 16:18:00 +00002406 UNLESS (py_int = PyInt_FromLong(l))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002407 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002408
Guido van Rossum053b8df1998-11-25 16:18:00 +00002409 PDATA_PUSH(self->stack, py_int, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002410 return 0;
2411}
2412
2413
2414static int
2415load_binint(Unpicklerobject *self) {
2416 char *s;
2417
2418 if ((*self->read_func)(self, &s, 4) < 0)
2419 return -1;
2420
2421 return load_binintx(self, s, 4);
2422}
2423
2424
2425static int
2426load_binint1(Unpicklerobject *self) {
2427 char *s;
2428
2429 if ((*self->read_func)(self, &s, 1) < 0)
2430 return -1;
2431
2432 return load_binintx(self, s, 1);
2433}
2434
2435
2436static int
2437load_binint2(Unpicklerobject *self) {
2438 char *s;
2439
2440 if ((*self->read_func)(self, &s, 2) < 0)
2441 return -1;
2442
2443 return load_binintx(self, s, 2);
2444}
2445
2446static int
2447load_long(Unpicklerobject *self) {
2448 PyObject *l = 0;
2449 char *end, *s;
2450 int len, res = -1;
2451
Guido van Rossum60456fd1997-04-09 17:36:32 +00002452 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002453 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002454 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002455
Guido van Rossum053b8df1998-11-25 16:18:00 +00002456 UNLESS (l = PyLong_FromString(s, &end, 0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002457 goto finally;
2458
Guido van Rossum053b8df1998-11-25 16:18:00 +00002459 free(s);
2460 PDATA_PUSH(self->stack, l, -1);
2461 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002462
2463finally:
2464 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002465
2466 return res;
2467}
2468
2469
2470static int
2471load_float(Unpicklerobject *self) {
2472 PyObject *py_float = 0;
2473 char *endptr, *s;
2474 int len, res = -1;
2475 double d;
2476
2477 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002478 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002479 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002480
2481 errno = 0;
2482 d = strtod(s, &endptr);
2483
2484 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2485 PyErr_SetString(PyExc_ValueError,
2486 "could not convert string to float");
2487 goto finally;
2488 }
2489
Guido van Rossum053b8df1998-11-25 16:18:00 +00002490 UNLESS (py_float = PyFloat_FromDouble(d))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002491 goto finally;
2492
Guido van Rossum053b8df1998-11-25 16:18:00 +00002493 free(s);
2494 PDATA_PUSH(self->stack, py_float, -1);
2495 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002496
2497finally:
2498 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002499
2500 return res;
2501}
2502
Guido van Rossum60456fd1997-04-09 17:36:32 +00002503static int
2504load_binfloat(Unpicklerobject *self) {
2505 PyObject *py_float = 0;
2506 int s, e, res = -1;
2507 long fhi, flo;
2508 double x;
2509 char *p;
2510
2511 if ((*self->read_func)(self, &p, 8) < 0)
2512 return -1;
2513
2514 /* First byte */
2515 s = (*p>>7) & 1;
2516 e = (*p & 0x7F) << 4;
2517 p++;
2518
2519 /* Second byte */
2520 e |= (*p>>4) & 0xF;
2521 fhi = (*p & 0xF) << 24;
2522 p++;
2523
2524 /* Third byte */
2525 fhi |= (*p & 0xFF) << 16;
2526 p++;
2527
2528 /* Fourth byte */
2529 fhi |= (*p & 0xFF) << 8;
2530 p++;
2531
2532 /* Fifth byte */
2533 fhi |= *p & 0xFF;
2534 p++;
2535
2536 /* Sixth byte */
2537 flo = (*p & 0xFF) << 16;
2538 p++;
2539
2540 /* Seventh byte */
2541 flo |= (*p & 0xFF) << 8;
2542 p++;
2543
2544 /* Eighth byte */
2545 flo |= *p & 0xFF;
2546
2547 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2548 x /= 268435456.0; /* 2**28 */
2549
2550 /* XXX This sadly ignores Inf/NaN */
2551 if (e == 0)
2552 e = -1022;
2553 else {
2554 x += 1.0;
2555 e -= 1023;
2556 }
2557 x = ldexp(x, e);
2558
2559 if (s)
2560 x = -x;
2561
Guido van Rossum053b8df1998-11-25 16:18:00 +00002562 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002563
Guido van Rossum053b8df1998-11-25 16:18:00 +00002564 PDATA_PUSH(self->stack, py_float, -1);
2565 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002566}
Guido van Rossum60456fd1997-04-09 17:36:32 +00002567
2568static int
2569load_string(Unpicklerobject *self) {
2570 PyObject *str = 0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002571 int len, res = -1, nslash;
2572 char *s, q, *p;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002573
2574 static PyObject *eval_dict = 0;
2575
2576 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002577 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002578 UNLESS (s=pystrndup(s,len)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002579
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002580 /* Check for unquoted quotes (evil strings) */
2581 q=*s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002582 if (q != '"' && q != '\'') goto insecure;
2583 for (p=s+1, nslash=0; *p; p++) {
2584 if (*p==q && nslash%2==0) break;
2585 if (*p=='\\') nslash++;
2586 else nslash=0;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002587 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002588 if (*p==q)
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002589 {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002590 for (p++; *p; p++) if (*p > ' ') goto insecure;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002591 }
2592 else goto insecure;
2593 /********************************************/
2594
Guido van Rossum053b8df1998-11-25 16:18:00 +00002595 UNLESS (eval_dict)
2596 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002597 goto finally;
2598
Guido van Rossum053b8df1998-11-25 16:18:00 +00002599 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
Guido van Rossum60456fd1997-04-09 17:36:32 +00002600 goto finally;
2601
Guido van Rossum053b8df1998-11-25 16:18:00 +00002602 free(s);
2603 PDATA_PUSH(self->stack, str, -1);
2604 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002605
2606finally:
2607 free(s);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002608
2609 return res;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002610
2611insecure:
2612 free(s);
2613 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2614 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002615}
2616
2617
2618static int
2619load_binstring(Unpicklerobject *self) {
2620 PyObject *py_string = 0;
2621 long l;
2622 int res = -1;
2623 char *s;
2624
Guido van Rossum053b8df1998-11-25 16:18:00 +00002625 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002626
2627 l = calc_binint(s, 4);
2628
2629 if ((*self->read_func)(self, &s, l) < 0)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002630 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002631
Guido van Rossum053b8df1998-11-25 16:18:00 +00002632 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2633 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002634
Guido van Rossum053b8df1998-11-25 16:18:00 +00002635 PDATA_PUSH(self->stack, py_string, -1);
2636 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002637}
2638
2639
2640static int
2641load_short_binstring(Unpicklerobject *self) {
2642 PyObject *py_string = 0;
2643 unsigned char l;
2644 int res = -1;
2645 char *s;
2646
2647 if ((*self->read_func)(self, &s, 1) < 0)
2648 return -1;
2649
2650 l = (unsigned char)s[0];
2651
Guido van Rossum053b8df1998-11-25 16:18:00 +00002652 if ((*self->read_func)(self, &s, l) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002653
Guido van Rossum053b8df1998-11-25 16:18:00 +00002654 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002655
Guido van Rossum053b8df1998-11-25 16:18:00 +00002656 PDATA_PUSH(self->stack, py_string, -1);
2657 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002658}
2659
2660
2661static int
2662load_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002663 PyObject *tup;
2664 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002665
Guido van Rossum053b8df1998-11-25 16:18:00 +00002666 if ((i = marker(self)) < 0) return -1;
2667 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2668 PDATA_PUSH(self->stack, tup, -1);
2669 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002670}
2671
2672static int
2673load_empty_tuple(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002674 PyObject *tup;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002675
Guido van Rossum053b8df1998-11-25 16:18:00 +00002676 UNLESS (tup=PyTuple_New(0)) return -1;
2677 PDATA_PUSH(self->stack, tup, -1);
2678 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002679}
2680
2681static int
2682load_empty_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002683 PyObject *list;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002684
Guido van Rossum053b8df1998-11-25 16:18:00 +00002685 UNLESS (list=PyList_New(0)) return -1;
2686 PDATA_PUSH(self->stack, list, -1);
2687 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002688}
2689
2690static int
2691load_empty_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002692 PyObject *dict;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002693
Guido van Rossum053b8df1998-11-25 16:18:00 +00002694 UNLESS (dict=PyDict_New()) return -1;
2695 PDATA_PUSH(self->stack, dict, -1);
2696 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002697}
2698
2699
2700static int
2701load_list(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002702 PyObject *list = 0;
2703 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002704
Guido van Rossum053b8df1998-11-25 16:18:00 +00002705 if ((i = marker(self)) < 0) return -1;
2706 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2707 PDATA_PUSH(self->stack, list, -1);
2708 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002709}
2710
2711static int
2712load_dict(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002713 PyObject *dict, *key, *value;
2714 int i, j, k;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002715
Guido van Rossum053b8df1998-11-25 16:18:00 +00002716 if ((i = marker(self)) < 0) return -1;
2717 j=self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002718
Guido van Rossum053b8df1998-11-25 16:18:00 +00002719 UNLESS (dict = PyDict_New()) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002720
Guido van Rossum053b8df1998-11-25 16:18:00 +00002721 for (k = i+1; k < j; k += 2) {
2722 key =self->stack->data[k-1];
2723 value=self->stack->data[k ];
2724 if (PyDict_SetItem(dict, key, value) < 0) {
2725 Py_DECREF(dict);
2726 return -1;
2727 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002728 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00002729 Pdata_clear(self->stack, i);
2730 PDATA_PUSH(self->stack, dict, -1);
2731 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002732}
2733
2734static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002735Instance_New(PyObject *cls, PyObject *args) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00002736 int has_key;
2737 PyObject *safe=0, *r=0;
2738
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002739 if (PyClass_Check(cls)) {
2740 int l;
2741
Guido van Rossum053b8df1998-11-25 16:18:00 +00002742 if ((l=PyObject_Length(args)) < 0) goto err;
2743 UNLESS (l) {
2744 PyObject *__getinitargs__;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002745
Guido van Rossum053b8df1998-11-25 16:18:00 +00002746 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2747 /* We have a class with no __getinitargs__, so bypass usual
2748 construction */
2749 PyInstanceObject *inst;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002750
Guido van Rossum053b8df1998-11-25 16:18:00 +00002751 PyErr_Clear();
2752 UNLESS (inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
2753 goto err;
2754 inst->in_class=(PyClassObject*)cls;
2755 Py_INCREF(cls);
2756 UNLESS (inst->in_dict=PyDict_New()) {
2757 Py_DECREF(inst);
2758 goto err;
2759 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002760
Guido van Rossum053b8df1998-11-25 16:18:00 +00002761 return (PyObject *)inst;
2762 }
2763 Py_DECREF(__getinitargs__);
2764 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002765
Guido van Rossum053b8df1998-11-25 16:18:00 +00002766 if ((r=PyInstance_New(cls, args, NULL))) return r;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002767 else goto err;
2768 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002769
2770
2771 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2772 goto err;
2773
2774 if (!has_key)
Guido van Rossum053b8df1998-11-25 16:18:00 +00002775 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
Guido van Rossum60456fd1997-04-09 17:36:32 +00002776 !PyObject_IsTrue(safe)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002777 cPickle_ErrFormat(UnpicklingError,
2778 "%s is not safe for unpickling", "O", cls);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002779 Py_XDECREF(safe);
2780 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00002781 }
2782
Guido van Rossum053b8df1998-11-25 16:18:00 +00002783 if (args==Py_None) {
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002784 /* Special case, call cls.__basicnew__() */
2785 PyObject *basicnew;
2786
Guido van Rossum053b8df1998-11-25 16:18:00 +00002787 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002788 r=PyObject_CallObject(basicnew, NULL);
2789 Py_DECREF(basicnew);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002790 if (r) return r;
Guido van Rossum9716aaa1997-12-08 15:15:16 +00002791 }
2792
Guido van Rossum053b8df1998-11-25 16:18:00 +00002793 if ((r=PyObject_CallObject(cls, args))) return r;
Guido van Rossum142eeb81997-08-13 03:14:41 +00002794
Guido van Rossum60456fd1997-04-09 17:36:32 +00002795err:
2796 {
2797 PyObject *tp, *v, *tb;
2798
2799 PyErr_Fetch(&tp, &v, &tb);
Guido van Rossum053b8df1998-11-25 16:18:00 +00002800 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2801 Py_XDECREF(v);
2802 v=r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002803 }
2804 PyErr_Restore(tp,v,tb);
2805 }
2806 return NULL;
2807}
2808
2809
2810static int
2811load_obj(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002812 PyObject *class, *tup, *obj=0;
2813 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002814
Guido van Rossum053b8df1998-11-25 16:18:00 +00002815 if ((i = marker(self)) < 0) return -1;
2816 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
2817 PDATA_POP(self->stack, class);
2818 if (class) {
2819 obj = Instance_New(class, tup);
2820 Py_DECREF(class);
2821 }
2822 Py_DECREF(tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002823
Guido van Rossum053b8df1998-11-25 16:18:00 +00002824 if (! obj) return -1;
2825 PDATA_PUSH(self->stack, obj, -1);
2826 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002827}
2828
2829
2830static int
2831load_inst(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002832 PyObject *tup, *class, *obj, *module_name, *class_name;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002833 int i, len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002834 char *s;
2835
Guido van Rossum053b8df1998-11-25 16:18:00 +00002836 if ((i = marker(self)) < 0) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002837
Guido van Rossum053b8df1998-11-25 16:18:00 +00002838 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002839 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002840 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002841
Guido van Rossum053b8df1998-11-25 16:18:00 +00002842 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002843 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002844 if (class_name = PyString_FromStringAndSize(s, len - 1)) {
2845 class = find_class(module_name, class_name);
2846 Py_DECREF(class_name);
2847 }
2848 }
2849 Py_DECREF(module_name);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002850
Guido van Rossum053b8df1998-11-25 16:18:00 +00002851 if (! class) return -1;
2852
2853 if (tup=Pdata_popTuple(self->stack, i)) {
2854 obj = Instance_New(class, tup);
2855 Py_DECREF(tup);
2856 }
2857 Py_DECREF(class);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002858
Guido van Rossum053b8df1998-11-25 16:18:00 +00002859 if (! obj) return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002860
Guido van Rossum053b8df1998-11-25 16:18:00 +00002861 PDATA_PUSH(self->stack, obj, -1);
2862 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002863}
2864
2865
2866static int
2867load_global(Unpicklerobject *self) {
2868 PyObject *class = 0, *module_name = 0, *class_name = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002869 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002870 char *s;
2871
Guido van Rossum053b8df1998-11-25 16:18:00 +00002872 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002873 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002874 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002875
Guido van Rossum053b8df1998-11-25 16:18:00 +00002876 if ((len = (*self->readline_func)(self, &s)) >= 0) {
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002877 if (len < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00002878 if (class_name = PyString_FromStringAndSize(s, len - 1)) {
2879 class = find_class(module_name, class_name);
2880 Py_DECREF(class_name);
2881 }
2882 }
2883 Py_DECREF(module_name);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002884
Guido van Rossum053b8df1998-11-25 16:18:00 +00002885 if (! class) return -1;
2886 PDATA_PUSH(self->stack, class, -1);
2887 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002888}
2889
2890
2891static int
2892load_persid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002893 PyObject *pid = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002894 int len, res = -1;
2895 char *s;
2896
2897 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002898 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00002899 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00002900
Guido van Rossum053b8df1998-11-25 16:18:00 +00002901 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002902
Guido van Rossum053b8df1998-11-25 16:18:00 +00002903 if (PyList_Check(self->pers_func)) {
2904 if (PyList_Append(self->pers_func, pid) < 0) {
2905 Py_DECREF(pid);
2906 return -1;
2907 }
2908 }
2909 else {
2910 ARG_TUP(self, pid);
2911 if (self->arg) {
2912 pid = PyObject_CallObject(self->pers_func, self->arg);
2913 FREE_ARG_TUP(self);
2914 }
2915 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002916
Guido van Rossum053b8df1998-11-25 16:18:00 +00002917 if (! pid) return -1;
2918
2919 PDATA_PUSH(self->stack, pid, -1);
2920 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002921 }
2922 else {
2923 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002924 "A load persistent id instruction was encountered,\n"
2925 "but no persistent_load function was specified.");
2926 return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002927 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002928}
2929
Guido van Rossum60456fd1997-04-09 17:36:32 +00002930static int
2931load_binpersid(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002932 PyObject *pid = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002933 int res = -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002934
2935 if (self->pers_func) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00002936 PDATA_POP(self->stack, pid);
2937 if (! pid) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002938
Guido van Rossum053b8df1998-11-25 16:18:00 +00002939 if (PyList_Check(self->pers_func)) {
2940 if (PyList_Append(self->pers_func, pid) < 0) {
2941 Py_DECREF(pid);
2942 return -1;
2943 }
2944 }
2945 else {
2946 ARG_TUP(self, pid);
2947 if (self->arg) {
2948 pid = PyObject_CallObject(self->pers_func, self->arg);
2949 FREE_ARG_TUP(self);
2950 }
2951 if (! pid) return -1;
2952 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002953
Guido van Rossum053b8df1998-11-25 16:18:00 +00002954 PDATA_PUSH(self->stack, pid, -1);
2955 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002956 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002957 else {
2958 PyErr_SetString(UnpicklingError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00002959 "A load persistent id instruction was encountered,\n"
2960 "but no persistent_load function was specified.");
2961 return -1;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00002962 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00002963}
2964
2965
2966static int
2967load_pop(Unpicklerobject *self) {
2968 int len;
2969
Guido van Rossum053b8df1998-11-25 16:18:00 +00002970 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00002971
2972 if ((self->num_marks > 0) &&
2973 (self->marks[self->num_marks - 1] == len))
2974 self->num_marks--;
Guido van Rossum053b8df1998-11-25 16:18:00 +00002975 else
2976 Py_DECREF(self->stack->data[--(self->stack->length)]);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002977
2978 return 0;
2979}
2980
2981
2982static int
2983load_pop_mark(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00002984 int i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00002985
2986 if ((i = marker(self)) < 0)
2987 return -1;
2988
Guido van Rossum053b8df1998-11-25 16:18:00 +00002989 Pdata_clear(self->stack, i);
Guido van Rossum60456fd1997-04-09 17:36:32 +00002990
2991 return 0;
2992}
2993
2994
2995static int
2996load_dup(Unpicklerobject *self) {
2997 PyObject *last;
2998 int len;
2999
Guido van Rossum053b8df1998-11-25 16:18:00 +00003000 if ((len = self->stack->length) <= 0) return stackUnderflow();
3001 last=self->stack->data[len-1];
3002 Py_INCREF(last);
3003 PDATA_PUSH(self->stack, last, -1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003004 return 0;
3005}
3006
3007
3008static int
3009load_get(Unpicklerobject *self) {
3010 PyObject *py_str = 0, *value = 0;
3011 int len, res = -1;
3012 char *s;
3013
Guido van Rossum053b8df1998-11-25 16:18:00 +00003014 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00003015 if (len < 2) return bad_readline();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003016
Guido van Rossum053b8df1998-11-25 16:18:00 +00003017 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3018
3019 value = PyDict_GetItem(self->memo, py_str);
3020 Py_DECREF(py_str);
3021 if (! value) {
3022 PyErr_SetObject(BadPickleGet, py_str);
3023 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00003024 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003025
Guido van Rossum053b8df1998-11-25 16:18:00 +00003026 PDATA_APPEND(self->stack, value, -1);
3027 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003028}
3029
3030
3031static int
3032load_binget(Unpicklerobject *self) {
3033 PyObject *py_key = 0, *value = 0;
3034 unsigned char key;
3035 int res = -1;
3036 char *s;
3037
Guido van Rossum053b8df1998-11-25 16:18:00 +00003038 if ((*self->read_func)(self, &s, 1) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003039
3040 key = (unsigned char)s[0];
Guido van Rossum053b8df1998-11-25 16:18:00 +00003041 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3042
3043 value = PyDict_GetItem(self->memo, py_key);
3044 Py_DECREF(py_key);
3045 if (! value) {
3046 PyErr_SetObject(BadPickleGet, py_key);
3047 return -1;
3048 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003049
Guido van Rossum053b8df1998-11-25 16:18:00 +00003050 PDATA_APPEND(self->stack, value, -1);
3051 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003052}
3053
3054
3055static int
3056load_long_binget(Unpicklerobject *self) {
3057 PyObject *py_key = 0, *value = 0;
3058 unsigned char c, *s;
3059 long key;
3060 int res = -1;
3061
Guido van Rossum053b8df1998-11-25 16:18:00 +00003062 if ((*self->read_func)(self, &s, 4) < 0) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003063
3064 c = (unsigned char)s[0];
3065 key = (long)c;
3066 c = (unsigned char)s[1];
3067 key |= (long)c << 8;
3068 c = (unsigned char)s[2];
3069 key |= (long)c << 16;
3070 c = (unsigned char)s[3];
3071 key |= (long)c << 24;
3072
Guido van Rossum053b8df1998-11-25 16:18:00 +00003073 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3074
3075 value = PyDict_GetItem(self->memo, py_key);
3076 Py_DECREF(py_key);
3077 if (! value) {
3078 PyErr_SetObject(BadPickleGet, py_key);
3079 return -1;
3080 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003081
Guido van Rossum053b8df1998-11-25 16:18:00 +00003082 PDATA_APPEND(self->stack, value, -1);
3083 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003084}
3085
3086
3087static int
3088load_put(Unpicklerobject *self) {
3089 PyObject *py_str = 0, *value = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003090 int len, l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003091 char *s;
3092
Guido van Rossum053b8df1998-11-25 16:18:00 +00003093 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
Guido van Rossumd1f66dc1999-02-08 22:38:25 +00003094 if (l < 2) return bad_readline();
Guido van Rossum053b8df1998-11-25 16:18:00 +00003095 UNLESS (len=self->stack->length) return stackUnderflow();
3096 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3097 value=self->stack->data[len-1];
3098 l=PyDict_SetItem(self->memo, py_str, value);
3099 Py_DECREF(py_str);
3100 return l;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003101}
3102
3103
3104static int
3105load_binput(Unpicklerobject *self) {
3106 PyObject *py_key = 0, *value = 0;
3107 unsigned char key, *s;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003108 int len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003109
Guido van Rossum053b8df1998-11-25 16:18:00 +00003110 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3111 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003112
3113 key = (unsigned char)s[0];
3114
Guido van Rossum053b8df1998-11-25 16:18:00 +00003115 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3116 value=self->stack->data[len-1];
3117 len=PyDict_SetItem(self->memo, py_key, value);
3118 Py_DECREF(py_key);
3119 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003120}
3121
3122
3123static int
3124load_long_binput(Unpicklerobject *self) {
3125 PyObject *py_key = 0, *value = 0;
3126 long key;
3127 unsigned char c, *s;
3128 int len, res = -1;
3129
Guido van Rossum053b8df1998-11-25 16:18:00 +00003130 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3131 UNLESS (len=self->stack->length) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003132
3133 c = (unsigned char)s[0];
3134 key = (long)c;
3135 c = (unsigned char)s[1];
3136 key |= (long)c << 8;
3137 c = (unsigned char)s[2];
3138 key |= (long)c << 16;
3139 c = (unsigned char)s[3];
3140 key |= (long)c << 24;
3141
Guido van Rossum053b8df1998-11-25 16:18:00 +00003142 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3143 value=self->stack->data[len-1];
3144 len=PyDict_SetItem(self->memo, py_key, value);
3145 Py_DECREF(py_key);
3146 return len;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003147}
3148
3149
3150static int
3151do_append(Unpicklerobject *self, int x) {
3152 PyObject *value = 0, *list = 0, *append_method = 0;
3153 int len, i;
3154
Guido van Rossum053b8df1998-11-25 16:18:00 +00003155 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3156 if (len==x) return 0; /* nothing to do */
Guido van Rossum60456fd1997-04-09 17:36:32 +00003157
Guido van Rossum053b8df1998-11-25 16:18:00 +00003158 list=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003159
3160 if (PyList_Check(list)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003161 PyObject *slice;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003162 int list_len;
3163
Guido van Rossum053b8df1998-11-25 16:18:00 +00003164 slice=Pdata_popList(self->stack, x);
3165 list_len = PyList_GET_SIZE(list);
3166 i=PyList_SetSlice(list, list_len, list_len, slice);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003167 Py_DECREF(slice);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003168 return i;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003169 }
3170 else {
3171
Guido van Rossum053b8df1998-11-25 16:18:00 +00003172 UNLESS (append_method = PyObject_GetAttr(list, append_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003173 return -1;
3174
3175 for (i = x; i < len; i++) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003176 PyObject *junk;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003177
Guido van Rossum053b8df1998-11-25 16:18:00 +00003178 value=self->stack->data[i];
3179 junk=0;
3180 ARG_TUP(self, value);
3181 if (self->arg) {
3182 junk = PyObject_CallObject(append_method, self->arg);
3183 FREE_ARG_TUP(self);
3184 }
3185 if (! junk) {
3186 Pdata_clear(self->stack, i+1);
3187 self->stack->length=x;
3188 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003189 return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003190 }
3191 Py_DECREF(junk);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003192 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003193 self->stack->length=x;
3194 Py_DECREF(append_method);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003195 }
3196
Guido van Rossum60456fd1997-04-09 17:36:32 +00003197 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003198}
3199
3200
3201static int
3202load_append(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003203 return do_append(self, self->stack->length - 1);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003204}
3205
3206
3207static int
3208load_appends(Unpicklerobject *self) {
3209 return do_append(self, marker(self));
3210}
3211
3212
3213static int
3214do_setitems(Unpicklerobject *self, int x) {
3215 PyObject *value = 0, *key = 0, *dict = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003216 int len, i, r=0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003217
Guido van Rossum053b8df1998-11-25 16:18:00 +00003218 UNLESS ((len=self->stack->length) >= x
3219 && x > 0) return stackUnderflow();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003220
Guido van Rossum053b8df1998-11-25 16:18:00 +00003221 dict=self->stack->data[x-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003222
Guido van Rossum053b8df1998-11-25 16:18:00 +00003223 for (i = x+1; i < len; i += 2) {
3224 key =self->stack->data[i-1];
3225 value=self->stack->data[i ];
3226 if (PyObject_SetItem(dict, key, value) < 0) {
3227 r=-1;
3228 break;
3229 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003230 }
3231
Guido van Rossum053b8df1998-11-25 16:18:00 +00003232 Pdata_clear(self->stack, x);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003233
Guido van Rossum053b8df1998-11-25 16:18:00 +00003234 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003235}
3236
3237
3238static int
3239load_setitem(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003240 return do_setitems(self, self->stack->length - 2);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003241}
3242
Guido van Rossum60456fd1997-04-09 17:36:32 +00003243static int
3244load_setitems(Unpicklerobject *self) {
3245 return do_setitems(self, marker(self));
3246}
3247
3248
3249static int
3250load_build(Unpicklerobject *self) {
3251 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3252 *junk = 0, *__setstate__ = 0;
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003253 int i, r = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003254
Guido van Rossum053b8df1998-11-25 16:18:00 +00003255 if (self->stack->length < 2) return stackUnderflow();
3256 PDATA_POP(self->stack, value);
3257 if (! value) return -1;
3258 inst=self->stack->data[self->stack->length-1];
Guido van Rossum60456fd1997-04-09 17:36:32 +00003259
Guido van Rossum053b8df1998-11-25 16:18:00 +00003260 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3261 ARG_TUP(self, value);
3262 if (self->arg) {
3263 junk = PyObject_CallObject(__setstate__, self->arg);
3264 FREE_ARG_TUP(self);
3265 }
3266 Py_DECREF(__setstate__);
3267 if (! junk) return -1;
3268 Py_DECREF(junk);
3269 return 0;
3270 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003271
Guido van Rossum053b8df1998-11-25 16:18:00 +00003272 PyErr_Clear();
3273 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003274 i = 0;
3275 while (PyDict_Next(value, &i, &d_key, &d_value)) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003276 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3277 r=-1;
3278 break;
3279 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003280 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003281 Py_DECREF(instdict);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003282 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003283 else r=-1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003284
Guido van Rossum053b8df1998-11-25 16:18:00 +00003285 Py_XDECREF(value);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003286
Guido van Rossum053b8df1998-11-25 16:18:00 +00003287 return r;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003288}
3289
3290
3291static int
3292load_mark(Unpicklerobject *self) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003293 int s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003294
Guido van Rossum053b8df1998-11-25 16:18:00 +00003295 if ((self->num_marks + 1) >= self->marks_size) {
3296 s=self->marks_size+20;
3297 if (s <= self->num_marks) s=self->num_marks + 1;
Guido van Rossum761fcd01999-04-12 22:51:20 +00003298 if (self->marks == NULL)
Guido van Rossumaa8d1671999-01-25 21:43:51 +00003299 self->marks=(int *)malloc(s * sizeof(int));
3300 else
3301 self->marks=(int *)realloc(self->marks, s * sizeof(int));
Guido van Rossum053b8df1998-11-25 16:18:00 +00003302 if (! self->marks) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003303 PyErr_NoMemory();
3304 return -1;
3305 }
Guido van Rossum053b8df1998-11-25 16:18:00 +00003306 self->marks_size = s;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003307 }
3308
Guido van Rossum053b8df1998-11-25 16:18:00 +00003309 self->marks[self->num_marks++] = self->stack->length;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003310
3311 return 0;
3312}
3313
3314static int
3315load_reduce(Unpicklerobject *self) {
3316 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003317
Guido van Rossum053b8df1998-11-25 16:18:00 +00003318 PDATA_POP(self->stack, arg_tup);
3319 if (! arg_tup) return -1;
3320 PDATA_POP(self->stack, callable);
3321 if (callable) {
3322 ob = Instance_New(callable, arg_tup);
3323 Py_DECREF(callable);
3324 }
3325 Py_DECREF(arg_tup);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003326
Guido van Rossum053b8df1998-11-25 16:18:00 +00003327 if (! ob) return -1;
3328
3329 PDATA_PUSH(self->stack, ob, -1);
3330 return 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003331}
3332
3333static PyObject *
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003334load(Unpicklerobject *self) {
Guido van Rossum142eeb81997-08-13 03:14:41 +00003335 PyObject *stack = 0, *err = 0, *val = 0;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003336 char *s;
3337
Guido van Rossum60456fd1997-04-09 17:36:32 +00003338 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003339 if (self->stack->length) Pdata_clear(self->stack, 0);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003340
3341 while (1) {
3342 if ((*self->read_func)(self, &s, 1) < 0)
3343 break;
3344
3345 switch (s[0]) {
3346 case NONE:
3347 if (load_none(self) < 0)
3348 break;
3349 continue;
3350
3351 case BININT:
3352 if (load_binint(self) < 0)
3353 break;
3354 continue;
3355
3356 case BININT1:
3357 if (load_binint1(self) < 0)
3358 break;
3359 continue;
3360
3361 case BININT2:
3362 if (load_binint2(self) < 0)
3363 break;
3364 continue;
3365
3366 case INT:
3367 if (load_int(self) < 0)
3368 break;
3369 continue;
3370
3371 case LONG:
3372 if (load_long(self) < 0)
3373 break;
3374 continue;
3375
3376 case FLOAT:
3377 if (load_float(self) < 0)
3378 break;
3379 continue;
3380
Guido van Rossum60456fd1997-04-09 17:36:32 +00003381 case BINFLOAT:
3382 if (load_binfloat(self) < 0)
3383 break;
3384 continue;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003385
3386 case BINSTRING:
3387 if (load_binstring(self) < 0)
3388 break;
3389 continue;
3390
3391 case SHORT_BINSTRING:
3392 if (load_short_binstring(self) < 0)
3393 break;
3394 continue;
3395
3396 case STRING:
3397 if (load_string(self) < 0)
3398 break;
3399 continue;
3400
3401 case EMPTY_TUPLE:
3402 if (load_empty_tuple(self) < 0)
3403 break;
3404 continue;
3405
3406 case TUPLE:
3407 if (load_tuple(self) < 0)
3408 break;
3409 continue;
3410
3411 case EMPTY_LIST:
3412 if (load_empty_list(self) < 0)
3413 break;
3414 continue;
3415
3416 case LIST:
3417 if (load_list(self) < 0)
3418 break;
3419 continue;
3420
3421 case EMPTY_DICT:
3422 if (load_empty_dict(self) < 0)
3423 break;
3424 continue;
3425
3426 case DICT:
3427 if (load_dict(self) < 0)
3428 break;
3429 continue;
3430
3431 case OBJ:
3432 if (load_obj(self) < 0)
3433 break;
3434 continue;
3435
3436 case INST:
3437 if (load_inst(self) < 0)
3438 break;
3439 continue;
3440
3441 case GLOBAL:
3442 if (load_global(self) < 0)
3443 break;
3444 continue;
3445
3446 case APPEND:
3447 if (load_append(self) < 0)
3448 break;
3449 continue;
3450
3451 case APPENDS:
3452 if (load_appends(self) < 0)
3453 break;
3454 continue;
3455
3456 case BUILD:
3457 if (load_build(self) < 0)
3458 break;
3459 continue;
3460
3461 case DUP:
3462 if (load_dup(self) < 0)
3463 break;
3464 continue;
3465
3466 case BINGET:
3467 if (load_binget(self) < 0)
3468 break;
3469 continue;
3470
3471 case LONG_BINGET:
3472 if (load_long_binget(self) < 0)
3473 break;
3474 continue;
3475
3476 case GET:
3477 if (load_get(self) < 0)
3478 break;
3479 continue;
3480
3481 case MARK:
3482 if (load_mark(self) < 0)
3483 break;
3484 continue;
3485
3486 case BINPUT:
3487 if (load_binput(self) < 0)
3488 break;
3489 continue;
3490
3491 case LONG_BINPUT:
3492 if (load_long_binput(self) < 0)
3493 break;
3494 continue;
3495
3496 case PUT:
3497 if (load_put(self) < 0)
3498 break;
3499 continue;
3500
3501 case POP:
3502 if (load_pop(self) < 0)
3503 break;
3504 continue;
3505
3506 case POP_MARK:
3507 if (load_pop_mark(self) < 0)
3508 break;
3509 continue;
3510
3511 case SETITEM:
3512 if (load_setitem(self) < 0)
3513 break;
3514 continue;
3515
3516 case SETITEMS:
3517 if (load_setitems(self) < 0)
3518 break;
3519 continue;
3520
3521 case STOP:
3522 break;
3523
3524 case PERSID:
3525 if (load_persid(self) < 0)
3526 break;
3527 continue;
3528
3529 case BINPERSID:
3530 if (load_binpersid(self) < 0)
3531 break;
3532 continue;
3533
3534 case REDUCE:
3535 if (load_reduce(self) < 0)
3536 break;
3537 continue;
3538
3539 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003540 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossum60456fd1997-04-09 17:36:32 +00003541 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003542 return NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003543 }
3544
3545 break;
3546 }
3547
Guido van Rossum053b8df1998-11-25 16:18:00 +00003548 if ((err = PyErr_Occurred())) {
3549 if (err == PyExc_EOFError) {
3550 PyErr_SetNone(PyExc_EOFError);
3551 }
3552 return NULL;
3553 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003554
Guido van Rossum053b8df1998-11-25 16:18:00 +00003555 PDATA_POP(self->stack, val);
Guido van Rossum60456fd1997-04-09 17:36:32 +00003556 return val;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003557}
3558
3559
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003560/* No-load functions to support noload, which is used to
3561 find persistent references. */
3562
3563static int
3564noload_obj(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003565 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003566
3567 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003568 return Pdata_clear(self->stack, i+1);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003569}
3570
3571
3572static int
3573noload_inst(Unpicklerobject *self) {
Guido van Rossume94e3fb1998-12-08 17:37:19 +00003574 int i;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003575 char *s;
3576
3577 if ((i = marker(self)) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003578 Pdata_clear(self->stack, i);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003579 if ((*self->readline_func)(self, &s) < 0) return -1;
3580 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003581 PDATA_APPEND(self->stack, Py_None,-1);
3582 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003583}
3584
3585static int
3586noload_global(Unpicklerobject *self) {
3587 char *s;
3588
3589 if ((*self->readline_func)(self, &s) < 0) return -1;
3590 if ((*self->readline_func)(self, &s) < 0) return -1;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003591 PDATA_APPEND(self->stack, Py_None,-1);
3592 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003593}
3594
3595static int
3596noload_reduce(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003597
Guido van Rossum053b8df1998-11-25 16:18:00 +00003598 if (self->stack->length < 2) return stackUnderflow();
3599 Pdata_clear(self->stack, self->stack->length-2);
3600 PDATA_APPEND(self->stack, Py_None,-1);
3601 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003602}
3603
3604static int
3605noload_build(Unpicklerobject *self) {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003606
Guido van Rossum053b8df1998-11-25 16:18:00 +00003607 if (self->stack->length < 1) return stackUnderflow();
3608 Pdata_clear(self->stack, self->stack->length-1);
Guido van Rossum50f385c1998-12-04 18:48:44 +00003609 return 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003610}
3611
3612
3613static PyObject *
3614noload(Unpicklerobject *self) {
3615 PyObject *stack = 0, *err = 0, *val = 0;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003616 char *s;
3617
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003618 self->num_marks = 0;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003619 Pdata_clear(self->stack, 0);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003620
3621 while (1) {
3622 if ((*self->read_func)(self, &s, 1) < 0)
3623 break;
3624
3625 switch (s[0]) {
3626 case NONE:
3627 if (load_none(self) < 0)
3628 break;
3629 continue;
3630
3631 case BININT:
3632 if (load_binint(self) < 0)
3633 break;
3634 continue;
3635
3636 case BININT1:
3637 if (load_binint1(self) < 0)
3638 break;
3639 continue;
3640
3641 case BININT2:
3642 if (load_binint2(self) < 0)
3643 break;
3644 continue;
3645
3646 case INT:
3647 if (load_int(self) < 0)
3648 break;
3649 continue;
3650
3651 case LONG:
3652 if (load_long(self) < 0)
3653 break;
3654 continue;
3655
3656 case FLOAT:
3657 if (load_float(self) < 0)
3658 break;
3659 continue;
3660
3661 case BINFLOAT:
3662 if (load_binfloat(self) < 0)
3663 break;
3664 continue;
3665
3666 case BINSTRING:
3667 if (load_binstring(self) < 0)
3668 break;
3669 continue;
3670
3671 case SHORT_BINSTRING:
3672 if (load_short_binstring(self) < 0)
3673 break;
3674 continue;
3675
3676 case STRING:
3677 if (load_string(self) < 0)
3678 break;
3679 continue;
3680
3681 case EMPTY_TUPLE:
3682 if (load_empty_tuple(self) < 0)
3683 break;
3684 continue;
3685
3686 case TUPLE:
3687 if (load_tuple(self) < 0)
3688 break;
3689 continue;
3690
3691 case EMPTY_LIST:
3692 if (load_empty_list(self) < 0)
3693 break;
3694 continue;
3695
3696 case LIST:
3697 if (load_list(self) < 0)
3698 break;
3699 continue;
3700
3701 case EMPTY_DICT:
3702 if (load_empty_dict(self) < 0)
3703 break;
3704 continue;
3705
3706 case DICT:
3707 if (load_dict(self) < 0)
3708 break;
3709 continue;
3710
3711 case OBJ:
3712 if (noload_obj(self) < 0)
3713 break;
3714 continue;
3715
3716 case INST:
3717 if (noload_inst(self) < 0)
3718 break;
3719 continue;
3720
3721 case GLOBAL:
3722 if (noload_global(self) < 0)
3723 break;
3724 continue;
3725
3726 case APPEND:
3727 if (load_append(self) < 0)
3728 break;
3729 continue;
3730
3731 case APPENDS:
3732 if (load_appends(self) < 0)
3733 break;
3734 continue;
3735
3736 case BUILD:
3737 if (noload_build(self) < 0)
3738 break;
3739 continue;
3740
3741 case DUP:
3742 if (load_dup(self) < 0)
3743 break;
3744 continue;
3745
3746 case BINGET:
3747 if (load_binget(self) < 0)
3748 break;
3749 continue;
3750
3751 case LONG_BINGET:
3752 if (load_long_binget(self) < 0)
3753 break;
3754 continue;
3755
3756 case GET:
3757 if (load_get(self) < 0)
3758 break;
3759 continue;
3760
3761 case MARK:
3762 if (load_mark(self) < 0)
3763 break;
3764 continue;
3765
3766 case BINPUT:
3767 if (load_binput(self) < 0)
3768 break;
3769 continue;
3770
3771 case LONG_BINPUT:
3772 if (load_long_binput(self) < 0)
3773 break;
3774 continue;
3775
3776 case PUT:
3777 if (load_put(self) < 0)
3778 break;
3779 continue;
3780
3781 case POP:
3782 if (load_pop(self) < 0)
3783 break;
3784 continue;
3785
3786 case POP_MARK:
3787 if (load_pop_mark(self) < 0)
3788 break;
3789 continue;
3790
3791 case SETITEM:
3792 if (load_setitem(self) < 0)
3793 break;
3794 continue;
3795
3796 case SETITEMS:
3797 if (load_setitems(self) < 0)
3798 break;
3799 continue;
3800
3801 case STOP:
3802 break;
3803
3804 case PERSID:
3805 if (load_persid(self) < 0)
3806 break;
3807 continue;
3808
3809 case BINPERSID:
3810 if (load_binpersid(self) < 0)
3811 break;
3812 continue;
3813
3814 case REDUCE:
3815 if (noload_reduce(self) < 0)
3816 break;
3817 continue;
3818
3819 default:
Guido van Rossum57d9f2e1998-01-19 23:18:18 +00003820 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003821 "c", s[0]);
Guido van Rossum053b8df1998-11-25 16:18:00 +00003822 return NULL;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003823 }
3824
3825 break;
3826 }
3827
Guido van Rossum053b8df1998-11-25 16:18:00 +00003828 if ((err = PyErr_Occurred())) {
3829 if (err == PyExc_EOFError) {
3830 PyErr_SetNone(PyExc_EOFError);
3831 }
3832 return NULL;
3833 }
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003834
Guido van Rossum053b8df1998-11-25 16:18:00 +00003835 PDATA_POP(self->stack, val);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003836 return val;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003837}
3838
3839
Guido van Rossum60456fd1997-04-09 17:36:32 +00003840static PyObject *
3841Unpickler_load(Unpicklerobject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003842 UNLESS (PyArg_ParseTuple(args, ""))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003843 return NULL;
3844
3845 return load(self);
3846}
3847
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003848static PyObject *
3849Unpickler_noload(Unpicklerobject *self, PyObject *args) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003850 UNLESS (PyArg_ParseTuple(args, ""))
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003851 return NULL;
3852
3853 return noload(self);
3854}
3855
Guido van Rossum60456fd1997-04-09 17:36:32 +00003856
3857static struct PyMethodDef Unpickler_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003858 {"load", (PyCFunction)Unpickler_load, 1,
3859 "load() -- Load a pickle"
3860 },
3861 {"noload", (PyCFunction)Unpickler_noload, 1,
3862 "noload() -- not load a pickle, but go through most of the motions\n"
3863 "\n"
3864 "This function can be used to read past a pickle without instantiating\n"
3865 "any objects or importing any modules. It can also be used to find all\n"
3866 "persistent references without instantiating any objects or importing\n"
3867 "any modules.\n"
3868 },
Guido van Rossum60456fd1997-04-09 17:36:32 +00003869 {NULL, NULL} /* sentinel */
3870};
3871
3872
3873static Unpicklerobject *
3874newUnpicklerobject(PyObject *f) {
3875 Unpicklerobject *self;
3876
Guido van Rossum053b8df1998-11-25 16:18:00 +00003877 UNLESS (self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003878 return NULL;
3879
3880 self->file = NULL;
3881 self->arg = NULL;
Guido van Rossum053b8df1998-11-25 16:18:00 +00003882 self->stack = (Pdata*)Pdata_New();
Guido van Rossum60456fd1997-04-09 17:36:32 +00003883 self->pers_func = NULL;
3884 self->last_string = NULL;
3885 self->marks = NULL;
3886 self->num_marks = 0;
3887 self->marks_size = 0;
3888 self->buf_size = 0;
3889 self->read = NULL;
Guido van Rossum8a6dba31998-03-06 01:39:39 +00003890 self->readline = NULL;
Guido van Rossum21ef0881998-12-11 03:20:00 +00003891 self->safe_constructors = NULL;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003892
Guido van Rossum053b8df1998-11-25 16:18:00 +00003893 UNLESS (self->memo = PyDict_New()) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003894 Py_XDECREF((PyObject *)self);
3895 return NULL;
3896 }
3897
3898 Py_INCREF(f);
3899 self->file = f;
3900
3901 /* Set read, readline based on type of f */
3902 if (PyFile_Check(f)) {
3903 self->fp = PyFile_AsFile(f);
Guido van Rossumc91fcaa1999-03-29 20:00:14 +00003904 if (self->fp == NULL) {
3905 PyErr_SetString(PyExc_IOError, "input file closed");
3906 return NULL;
3907 }
Guido van Rossum60456fd1997-04-09 17:36:32 +00003908 self->read_func = read_file;
3909 self->readline_func = readline_file;
3910 }
3911 else if (PycStringIO_InputCheck(f)) {
3912 self->fp = NULL;
3913 self->read_func = read_cStringIO;
3914 self->readline_func = readline_cStringIO;
3915 }
3916 else {
3917
3918 self->fp = NULL;
3919 self->read_func = read_other;
3920 self->readline_func = readline_other;
3921
Guido van Rossum053b8df1998-11-25 16:18:00 +00003922 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003923 (self->read = PyObject_GetAttr(f, read_str))) {
Guido van Rossum60456fd1997-04-09 17:36:32 +00003924 PyErr_Clear();
3925 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
3926 "'readline' attributes" );
Guido van Rossum053b8df1998-11-25 16:18:00 +00003927 goto err;
Guido van Rossum60456fd1997-04-09 17:36:32 +00003928 }
3929 }
3930
Guido van Rossum053b8df1998-11-25 16:18:00 +00003931 if (PyEval_GetRestricted()) {
3932 /* Restricted execution, get private tables */
3933 PyObject *m;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003934
Guido van Rossum053b8df1998-11-25 16:18:00 +00003935 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
3936 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
3937 Py_DECREF(m);
3938 UNLESS (self->safe_constructors) goto err;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003939 }
3940 else {
Guido van Rossum053b8df1998-11-25 16:18:00 +00003941 self->safe_constructors=safe_constructors;
3942 Py_INCREF(safe_constructors);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003943 }
3944
Guido van Rossum60456fd1997-04-09 17:36:32 +00003945 return self;
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003946
3947err:
3948 Py_DECREF((PyObject *)self);
3949 return NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003950}
3951
3952
3953static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003954get_Unpickler(PyObject *self, PyObject *args) {
3955 PyObject *file;
3956
Guido van Rossum053b8df1998-11-25 16:18:00 +00003957 UNLESS (PyArg_ParseTuple(args, "O", &file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00003958 return NULL;
3959 return (PyObject *)newUnpicklerobject(file);
3960}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003961
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003962
Guido van Rossum60456fd1997-04-09 17:36:32 +00003963static void
3964Unpickler_dealloc(Unpicklerobject *self) {
3965 Py_XDECREF(self->readline);
3966 Py_XDECREF(self->read);
3967 Py_XDECREF(self->file);
3968 Py_XDECREF(self->memo);
3969 Py_XDECREF(self->stack);
3970 Py_XDECREF(self->pers_func);
3971 Py_XDECREF(self->arg);
3972 Py_XDECREF(self->last_string);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00003973 Py_XDECREF(self->safe_constructors);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003974
Guido van Rossum60456fd1997-04-09 17:36:32 +00003975 if (self->marks) {
3976 free(self->marks);
3977 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003978
Guido van Rossum60456fd1997-04-09 17:36:32 +00003979 if (self->buf_size) {
3980 free(self->buf);
3981 }
3982
3983 PyMem_DEL(self);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003984}
3985
3986
3987static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00003988Unpickler_getattr(Unpicklerobject *self, char *name) {
3989 if (!strcmp(name, "persistent_load")) {
3990 if (!self->pers_func) {
3991 PyErr_SetString(PyExc_AttributeError, name);
3992 return NULL;
3993 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003994
Guido van Rossum60456fd1997-04-09 17:36:32 +00003995 Py_INCREF(self->pers_func);
3996 return self->pers_func;
3997 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00003998
Guido van Rossum60456fd1997-04-09 17:36:32 +00003999 if (!strcmp(name, "memo")) {
4000 if (!self->memo) {
4001 PyErr_SetString(PyExc_AttributeError, name);
4002 return NULL;
4003 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004004
Guido van Rossum60456fd1997-04-09 17:36:32 +00004005 Py_INCREF(self->memo);
4006 return self->memo;
4007 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004008
Guido van Rossum60456fd1997-04-09 17:36:32 +00004009 if (!strcmp(name, "UnpicklingError")) {
4010 Py_INCREF(UnpicklingError);
4011 return UnpicklingError;
4012 }
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004013
Guido van Rossum60456fd1997-04-09 17:36:32 +00004014 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4015}
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004016
Guido van Rossum60456fd1997-04-09 17:36:32 +00004017
4018static int
4019Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004020
Guido van Rossum053b8df1998-11-25 16:18:00 +00004021 if (! value) {
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004022 PyErr_SetString(PyExc_TypeError,
Guido van Rossum053b8df1998-11-25 16:18:00 +00004023 "attribute deletion is not supported");
4024 return -1;
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004025 }
4026
Guido van Rossum60456fd1997-04-09 17:36:32 +00004027 if (!strcmp(name, "persistent_load")) {
4028 Py_XDECREF(self->pers_func);
4029 self->pers_func = value;
4030 Py_INCREF(value);
4031 return 0;
4032 }
4033
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004034 if (strcmp(name, "memo") == 0) {
Guido van Rossum053b8df1998-11-25 16:18:00 +00004035 if (! PyDict_Check(value)) {
4036 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4037 return -1;
4038 }
Jeremy Hyltonce616e41998-08-13 23:13:52 +00004039 Py_XDECREF(self->memo);
4040 self->memo = value;
4041 Py_INCREF(value);
4042 return 0;
4043 }
4044
Guido van Rossum60456fd1997-04-09 17:36:32 +00004045 PyErr_SetString(PyExc_AttributeError, name);
4046 return -1;
4047}
4048
4049
4050static PyObject *
4051cpm_dump(PyObject *self, PyObject *args) {
4052 PyObject *ob, *file, *res = NULL;
4053 Picklerobject *pickler = 0;
4054 int bin = 0;
4055
Guido van Rossum053b8df1998-11-25 16:18:00 +00004056 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004057 goto finally;
4058
Guido van Rossum053b8df1998-11-25 16:18:00 +00004059 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004060 goto finally;
4061
4062 if (dump(pickler, ob) < 0)
4063 goto finally;
4064
4065 Py_INCREF(Py_None);
4066 res = Py_None;
4067
4068finally:
4069 Py_XDECREF(pickler);
4070
4071 return res;
4072}
4073
4074
4075static PyObject *
4076cpm_dumps(PyObject *self, PyObject *args) {
4077 PyObject *ob, *file = 0, *res = NULL;
4078 Picklerobject *pickler = 0;
4079 int bin = 0;
4080
Guido van Rossum053b8df1998-11-25 16:18:00 +00004081 UNLESS (PyArg_ParseTuple(args, "O|i", &ob, &bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004082 goto finally;
4083
Guido van Rossum053b8df1998-11-25 16:18:00 +00004084 UNLESS (file = PycStringIO->NewOutput(128))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004085 goto finally;
4086
Guido van Rossum053b8df1998-11-25 16:18:00 +00004087 UNLESS (pickler = newPicklerobject(file, bin))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004088 goto finally;
4089
4090 if (dump(pickler, ob) < 0)
4091 goto finally;
4092
4093 res = PycStringIO->cgetvalue(file);
4094
4095finally:
4096 Py_XDECREF(pickler);
4097 Py_XDECREF(file);
4098
4099 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004100}
4101
4102
4103static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004104cpm_load(PyObject *self, PyObject *args) {
4105 Unpicklerobject *unpickler = 0;
4106 PyObject *ob, *res = NULL;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004107
Guido van Rossum053b8df1998-11-25 16:18:00 +00004108 UNLESS (PyArg_ParseTuple(args, "O", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004109 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004110
Guido van Rossum053b8df1998-11-25 16:18:00 +00004111 UNLESS (unpickler = newUnpicklerobject(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004112 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004113
Guido van Rossum60456fd1997-04-09 17:36:32 +00004114 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004115
Guido van Rossum60456fd1997-04-09 17:36:32 +00004116finally:
4117 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004118
Guido van Rossum60456fd1997-04-09 17:36:32 +00004119 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004120}
4121
4122
4123static PyObject *
Guido van Rossum60456fd1997-04-09 17:36:32 +00004124cpm_loads(PyObject *self, PyObject *args) {
4125 PyObject *ob, *file = 0, *res = NULL;
4126 Unpicklerobject *unpickler = 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004127
Guido van Rossum053b8df1998-11-25 16:18:00 +00004128 UNLESS (PyArg_ParseTuple(args, "S", &ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004129 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004130
Guido van Rossum053b8df1998-11-25 16:18:00 +00004131 UNLESS (file = PycStringIO->NewInput(ob))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004132 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004133
Guido van Rossum053b8df1998-11-25 16:18:00 +00004134 UNLESS (unpickler = newUnpicklerobject(file))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004135 goto finally;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004136
Guido van Rossum60456fd1997-04-09 17:36:32 +00004137 res = load(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004138
Guido van Rossum60456fd1997-04-09 17:36:32 +00004139finally:
4140 Py_XDECREF(file);
4141 Py_XDECREF(unpickler);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004142
Guido van Rossum60456fd1997-04-09 17:36:32 +00004143 return res;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004144}
4145
4146
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004147static char Unpicklertype__doc__[] =
4148"Objects that know how to unpickle";
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004149
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004150static PyTypeObject Unpicklertype = {
4151 PyObject_HEAD_INIT(NULL)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004152 0, /*ob_size*/
4153 "Unpickler", /*tp_name*/
4154 sizeof(Unpicklerobject), /*tp_basicsize*/
4155 0, /*tp_itemsize*/
4156 /* methods */
4157 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4158 (printfunc)0, /*tp_print*/
4159 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4160 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4161 (cmpfunc)0, /*tp_compare*/
4162 (reprfunc)0, /*tp_repr*/
4163 0, /*tp_as_number*/
4164 0, /*tp_as_sequence*/
4165 0, /*tp_as_mapping*/
4166 (hashfunc)0, /*tp_hash*/
4167 (ternaryfunc)0, /*tp_call*/
4168 (reprfunc)0, /*tp_str*/
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004169
Guido van Rossum60456fd1997-04-09 17:36:32 +00004170 /* Space for future expansion */
4171 0L,0L,0L,0L,
4172 Unpicklertype__doc__ /* Documentation string */
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004173};
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004174
Guido van Rossum60456fd1997-04-09 17:36:32 +00004175static struct PyMethodDef cPickle_methods[] = {
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004176 {"dump", (PyCFunction)cpm_dump, 1,
4177 "dump(object, file, [binary]) --"
4178 "Write an object in pickle format to the given file\n"
4179 "\n"
4180 "If the optional argument, binary, is provided and is true, then the\n"
4181 "pickle will be written in binary format, which is more space and\n"
4182 "computationally efficient. \n"
4183 },
4184 {"dumps", (PyCFunction)cpm_dumps, 1,
4185 "dumps(object, [binary]) --"
4186 "Return a string containing an object in pickle format\n"
4187 "\n"
4188 "If the optional argument, binary, is provided and is true, then the\n"
4189 "pickle will be written in binary format, which is more space and\n"
4190 "computationally efficient. \n"
4191 },
4192 {"load", (PyCFunction)cpm_load, 1,
4193 "load(file) -- Load a pickle from the given file"},
4194 {"loads", (PyCFunction)cpm_loads, 1,
4195 "loads(string) -- Load a pickle from the given string"},
4196 {"Pickler", (PyCFunction)get_Pickler, 1,
4197 "Pickler(file, [binary]) -- Create a pickler\n"
4198 "\n"
4199 "If the optional argument, binary, is provided and is true, then\n"
4200 "pickles will be written in binary format, which is more space and\n"
4201 "computationally efficient. \n"
4202 },
4203 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4204 "Unpickler(file) -- Create an unpickler"},
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004205 { NULL, NULL }
4206};
4207
4208
Guido van Rossum60456fd1997-04-09 17:36:32 +00004209#define CHECK_FOR_ERRORS(MESS) \
Guido van Rossum053b8df1998-11-25 16:18:00 +00004210if (PyErr_Occurred()) { \
Guido van Rossum60456fd1997-04-09 17:36:32 +00004211 PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \
4212 PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \
4213 fprintf(stderr, # MESS ":\n\t"); \
4214 PyObject_Print(__sys_exc_type, stderr,0); \
4215 fprintf(stderr,", "); \
4216 PyObject_Print(__sys_exc_value, stderr,0); \
4217 fprintf(stderr,"\n"); \
4218 fflush(stderr); \
4219 Py_FatalError(# MESS); \
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004220}
4221
4222
Guido van Rossum60456fd1997-04-09 17:36:32 +00004223static int
4224init_stuff(PyObject *module, PyObject *module_dict) {
4225 PyObject *string, *copy_reg;
4226
4227#define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4228
4229 INIT_STR(__class__);
4230 INIT_STR(__getinitargs__);
4231 INIT_STR(__dict__);
4232 INIT_STR(__getstate__);
4233 INIT_STR(__setstate__);
4234 INIT_STR(__name__);
Guido van Rossum142eeb81997-08-13 03:14:41 +00004235 INIT_STR(__main__);
Guido van Rossum60456fd1997-04-09 17:36:32 +00004236 INIT_STR(__reduce__);
4237 INIT_STR(write);
4238 INIT_STR(__safe_for_unpickling__);
4239 INIT_STR(append);
4240 INIT_STR(read);
4241 INIT_STR(readline);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004242 INIT_STR(copy_reg);
4243 INIT_STR(dispatch_table);
4244 INIT_STR(safe_constructors);
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004245 INIT_STR(__basicnew__);
Guido van Rossum053b8df1998-11-25 16:18:00 +00004246 UNLESS (empty_str=PyString_FromString("")) return -1;
Guido van Rossum60456fd1997-04-09 17:36:32 +00004247
Guido van Rossum053b8df1998-11-25 16:18:00 +00004248 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004249 return -1;
4250
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004251 /* These next few are special because we want to use different
4252 ones in restricted mode. */
4253
Guido van Rossum053b8df1998-11-25 16:18:00 +00004254 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004255 return -1;
4256
Guido van Rossum053b8df1998-11-25 16:18:00 +00004257 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4258 safe_constructors_str))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004259 return -1;
4260
4261 Py_DECREF(copy_reg);
4262
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004263 /* Down to here ********************************** */
4264
Guido van Rossum053b8df1998-11-25 16:18:00 +00004265 UNLESS (string = PyImport_ImportModule("string"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004266 return -1;
4267
Guido van Rossum053b8df1998-11-25 16:18:00 +00004268 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004269 return -1;
4270
4271 Py_DECREF(string);
4272
Guido van Rossum053b8df1998-11-25 16:18:00 +00004273 UNLESS (empty_tuple = PyTuple_New(0))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004274 return -1;
4275
Guido van Rossum053b8df1998-11-25 16:18:00 +00004276 UNLESS (PicklingError = PyString_FromString("cPickle.PicklingError"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004277 return -1;
4278
4279 if (PyDict_SetItemString(module_dict, "PicklingError",
4280 PicklingError) < 0)
4281 return -1;
4282
Guido van Rossum053b8df1998-11-25 16:18:00 +00004283 UNLESS (UnpicklingError = PyString_FromString("cPickle.UnpicklingError"))
Guido van Rossum60456fd1997-04-09 17:36:32 +00004284 return -1;
4285
4286 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4287 UnpicklingError) < 0)
4288 return -1;
4289
Guido van Rossum053b8df1998-11-25 16:18:00 +00004290 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4291 return -1;
4292
4293 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4294 BadPickleGet) < 0)
4295 return -1;
4296
Guido van Rossum60456fd1997-04-09 17:36:32 +00004297 PycString_IMPORT;
4298
4299 return 0;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004300}
4301
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004302#ifndef DL_EXPORT /* declarations for DLL import/export */
4303#define DL_EXPORT(RTYPE) RTYPE
4304#endif
Guido van Rossum50f385c1998-12-04 18:48:44 +00004305DL_EXPORT(void)
Guido van Rossum60456fd1997-04-09 17:36:32 +00004306initcPickle() {
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004307 PyObject *m, *d, *v;
Guido van Rossumf9ffb031999-02-04 14:54:04 +00004308 char *rev="1.63";
Guido van Rossum60456fd1997-04-09 17:36:32 +00004309 PyObject *format_version;
4310 PyObject *compatible_formats;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004311
Guido van Rossum9716aaa1997-12-08 15:15:16 +00004312 Picklertype.ob_type = &PyType_Type;
4313 Unpicklertype.ob_type = &PyType_Type;
Guido van Rossum053b8df1998-11-25 16:18:00 +00004314 PdataType.ob_type = &PyType_Type;
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004315
Guido van Rossum60456fd1997-04-09 17:36:32 +00004316 /* Create the module and add the functions */
4317 m = Py_InitModule4("cPickle", cPickle_methods,
4318 cPickle_module_documentation,
4319 (PyObject*)NULL,PYTHON_API_VERSION);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004320
Guido van Rossum60456fd1997-04-09 17:36:32 +00004321 /* Add some symbolic constants to the module */
4322 d = PyModule_GetDict(m);
Guido van Rossumfdde96c1997-12-04 01:13:01 +00004323 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004324 Py_XDECREF(v);
Barry Warsaw93d29b61997-01-14 17:45:08 +00004325
Guido van Rossum60456fd1997-04-09 17:36:32 +00004326 format_version = PyString_FromString("1.3");
4327 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004328
Guido van Rossum60456fd1997-04-09 17:36:32 +00004329 PyDict_SetItemString(d, "format_version", format_version);
4330 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
Guido van Rossum9efe8ef1997-09-03 18:19:40 +00004331 Py_XDECREF(format_version);
4332 Py_XDECREF(compatible_formats);
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004333
Guido van Rossum60456fd1997-04-09 17:36:32 +00004334 init_stuff(m, d);
4335 CHECK_FOR_ERRORS("can't initialize module cPickle");
Guido van Rossum2f4caa41997-01-06 22:59:08 +00004336}