The 0.5 release happened on 2/15, not on 2/14. :-)
[python/dscho.git] / Modules / cPickle.c
blob0737d947d0609a68719e119d5edf76e1aa07271e
1 /*
2 * cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp
3 *
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:
11 * o Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the disclaimer that follows.
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.
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.
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.
38 # If you have questions regarding this software, contact:
40 # Digital Creations, L.C.
41 # 910 Princess Ann Street
42 # Fredericksburge, Virginia 22401
44 # info@digicool.com
46 # (540) 371-6909
49 static char cPickle_module_documentation[] =
50 "C implementation and optimization of the Python pickle module\n"
51 "\n"
52 "cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp\n"
55 #include "Python.h"
56 #include "cStringIO.h"
57 #include "mymath.h"
59 #ifndef Py_eval_input
60 #include <graminit.h>
61 #define Py_eval_input eval_input
62 #endif /* Py_eval_input */
64 #include <errno.h>
66 #define UNLESS(E) if (!(E))
68 #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
70 #define WRITE_BUF_SIZE 256
73 #define MARK '('
74 #define STOP '.'
75 #define POP '0'
76 #define POP_MARK '1'
77 #define DUP '2'
78 #define FLOAT 'F'
79 #define BINFLOAT 'G'
80 #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'
91 #define SHORT_BINSTRING 'U'
92 #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'
113 static char MARKv = MARK;
115 /* atol function from string module */
116 static PyObject *atol_func;
118 static PyObject *PickleError;
119 static PyObject *PicklingError;
120 static PyObject *UnpickleableError;
121 static PyObject *UnpicklingError;
122 static PyObject *BadPickleGet;
125 static PyObject *dispatch_table;
126 static PyObject *safe_constructors;
127 static PyObject *empty_tuple;
129 static PyObject *__class___str, *__getinitargs___str, *__dict___str,
130 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
131 *write_str, *__safe_for_unpickling___str, *append_str,
132 *read_str, *readline_str, *__main___str, *__basicnew___str,
133 *copy_reg_str, *dispatch_table_str, *safe_constructors_str, *empty_str;
135 static int save();
136 static int put2();
138 #ifndef PyList_SET_ITEM
139 #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
140 #endif
141 #ifndef PyList_GET_SIZE
142 #define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
143 #endif
144 #ifndef PyTuple_SET_ITEM
145 #define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
146 #endif
147 #ifndef PyTuple_GET_SIZE
148 #define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
149 #endif
150 #ifndef PyString_GET_SIZE
151 #define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
152 #endif
154 /*************************************************************************
155 Internal Data type for pickle data. */
157 typedef struct {
158 PyObject_HEAD
159 int length, size;
160 PyObject **data;
161 } Pdata;
163 static void
164 Pdata_dealloc(Pdata *self) {
165 int i;
166 PyObject **p;
168 for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
170 if (self->data) free(self->data);
172 PyMem_DEL(self);
175 static PyTypeObject PdataType = {
176 PyObject_HEAD_INIT(NULL) 0, "Pdata", sizeof(Pdata), 0,
177 (destructor)Pdata_dealloc,
178 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
181 #define Pdata_Check(O) ((O)->ob_type == &PdataType)
183 static PyObject *
184 Pdata_New() {
185 Pdata *self;
187 UNLESS (self = PyObject_NEW(Pdata, &PdataType)) return NULL;
188 self->size=8;
189 self->length=0;
190 self->data=malloc(self->size * sizeof(PyObject*));
191 if (self->data) return (PyObject*)self;
192 Py_DECREF(self);
193 return PyErr_NoMemory();
196 static int
197 stackUnderflow() {
198 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
199 return -1;
202 static int
203 Pdata_clear(Pdata *self, int clearto) {
204 int i;
205 PyObject **p;
207 if (clearto < 0) return stackUnderflow();
208 if (clearto >= self->length) return 0;
210 for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
211 Py_DECREF(*p);
212 self->length=clearto;
214 return 0;
218 static int
219 Pdata_grow(Pdata *self) {
220 if (! self->size) {
221 PyErr_NoMemory();
222 return -1;
224 self->size *= 2;
225 self->data = realloc(self->data, self->size*sizeof(PyObject*));
226 if (! self->data) {
227 self->size = 0;
228 PyErr_NoMemory();
229 return -1;
231 return 0;
234 #define PDATA_POP(D,V) { \
235 if ((D)->length) V=D->data[--((D)->length)]; \
236 else { \
237 PyErr_SetString(UnpicklingError, "bad pickle data"); \
238 V=NULL; \
243 static PyObject *
244 Pdata_popTuple(Pdata *self, int start) {
245 PyObject *r;
246 int i, j, l;
248 l=self->length-start;
249 UNLESS (r=PyTuple_New(l)) return NULL;
250 for (i=start, j=0 ; j < l; )
251 PyTuple_SET_ITEM(r,j++,self->data[i++]);
253 self->length=start;
254 return r;
257 static PyObject *
258 Pdata_popList(Pdata *self, int start) {
259 PyObject *r;
260 int i, j, l;
262 l=self->length-start;
263 UNLESS (r=PyList_New(l)) return NULL;
264 for (i=start, j=0 ; j < l; )
265 PyList_SET_ITEM(r,j++,self->data[i++]);
267 self->length=start;
268 return r;
271 #define PDATA_APPEND_(D,O,ER) { \
272 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
275 #define PDATA_APPEND(D,O,ER) { \
276 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
277 Pdata_grow((Pdata*)(D)) < 0) \
278 return ER; \
279 Py_INCREF(O); \
280 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
283 #define PDATA_PUSH(D,O,ER) { \
284 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
285 Pdata_grow((Pdata*)(D)) < 0) { \
286 Py_DECREF(O); \
287 return ER; \
289 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
292 /*************************************************************************/
294 #define ARG_TUP(self, o) { \
295 if (self->arg || (self->arg=PyTuple_New(1))) { \
296 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
297 PyTuple_SET_ITEM(self->arg,0,o); \
299 else { \
300 Py_DECREF(o); \
304 #define FREE_ARG_TUP(self) { \
305 if (self->arg->ob_refcnt > 1) { \
306 Py_DECREF(self->arg); \
307 self->arg=NULL; \
311 typedef struct {
312 PyObject_HEAD
313 FILE *fp;
314 PyObject *write;
315 PyObject *file;
316 PyObject *memo;
317 PyObject *arg;
318 PyObject *pers_func;
319 PyObject *inst_pers_func;
320 int bin;
321 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
322 int (*write_func)();
323 char *write_buf;
324 int buf_size;
325 PyObject *dispatch_table;
326 } Picklerobject;
328 staticforward PyTypeObject Picklertype;
330 typedef struct {
331 PyObject_HEAD
332 FILE *fp;
333 PyObject *file;
334 PyObject *readline;
335 PyObject *read;
336 PyObject *memo;
337 PyObject *arg;
338 Pdata *stack;
339 PyObject *mark;
340 PyObject *pers_func;
341 PyObject *last_string;
342 int *marks;
343 int num_marks;
344 int marks_size;
345 int (*read_func)();
346 int (*readline_func)();
347 int buf_size;
348 char *buf;
349 PyObject *safe_constructors;
350 PyObject *find_class;
351 } Unpicklerobject;
353 staticforward PyTypeObject Unpicklertype;
355 int
356 cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
357 PyObject *v;
359 if ((v = PyObject_GetItem(o,key))) {
360 Py_DECREF(v);
361 return 1;
364 PyErr_Clear();
365 return 0;
368 static
369 PyObject *
370 #ifdef HAVE_STDARG_PROTOTYPES
371 /* VARARGS 2 */
372 cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) {
373 #else
374 /* VARARGS */
375 cPickle_ErrFormat(va_alist) va_dcl {
376 #endif
377 va_list va;
378 PyObject *args=0, *retval=0;
379 #ifdef HAVE_STDARG_PROTOTYPES
380 va_start(va, format);
381 #else
382 PyObject *ErrType;
383 char *stringformat, *format;
384 va_start(va);
385 ErrType = va_arg(va, PyObject *);
386 stringformat = va_arg(va, char *);
387 format = va_arg(va, char *);
388 #endif
390 if (format) args = Py_VaBuildValue(format, va);
391 va_end(va);
392 if (format && ! args) return NULL;
393 if (stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
395 if (retval) {
396 if (args) {
397 PyObject *v;
398 v=PyString_Format(retval, args);
399 Py_DECREF(retval);
400 Py_DECREF(args);
401 if (! v) return NULL;
402 retval=v;
405 else
406 if (args) retval=args;
407 else {
408 PyErr_SetObject(ErrType,Py_None);
409 return NULL;
411 PyErr_SetObject(ErrType,retval);
412 Py_DECREF(retval);
413 return NULL;
416 static int
417 write_file(Picklerobject *self, char *s, int n) {
418 if (s == NULL) {
419 return 0;
422 if ((int)fwrite(s, sizeof(char), n, self->fp) != n) {
423 PyErr_SetFromErrno(PyExc_IOError);
424 return -1;
427 return n;
430 static int
431 write_cStringIO(Picklerobject *self, char *s, int n) {
432 if (s == NULL) {
433 return 0;
436 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
437 return -1;
440 return n;
443 static int
444 write_none(Picklerobject *self, char *s, int n) {
445 if (s == NULL) return 0;
446 return n;
449 static int
450 write_other(Picklerobject *self, char *s, int n) {
451 PyObject *py_str = 0, *junk = 0;
453 if (s == NULL) {
454 UNLESS (self->buf_size) return 0;
455 UNLESS (py_str =
456 PyString_FromStringAndSize(self->write_buf, self->buf_size))
457 return -1;
459 else {
460 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
461 if (write_other(self, NULL, 0) < 0)
462 return -1;
465 if (n > WRITE_BUF_SIZE) {
466 UNLESS (py_str =
467 PyString_FromStringAndSize(s, n))
468 return -1;
470 else {
471 memcpy(self->write_buf + self->buf_size, s, n);
472 self->buf_size += n;
473 return n;
477 if (self->write) {
478 /* object with write method */
479 ARG_TUP(self, py_str);
480 if (self->arg) {
481 junk = PyObject_CallObject(self->write, self->arg);
482 FREE_ARG_TUP(self);
484 if (junk) Py_DECREF(junk);
485 else return -1;
487 else
488 PDATA_PUSH(self->file, py_str, -1);
490 self->buf_size = 0;
491 return n;
495 static int
496 read_file(Unpicklerobject *self, char **s, int n) {
498 if (self->buf_size == 0) {
499 int size;
501 size = ((n < 32) ? 32 : n);
502 UNLESS (self->buf = (char *)malloc(size * sizeof(char))) {
503 PyErr_NoMemory();
504 return -1;
507 self->buf_size = size;
509 else if (n > self->buf_size) {
510 UNLESS (self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
511 PyErr_NoMemory();
512 return -1;
515 self->buf_size = n;
518 if ((int)fread(self->buf, sizeof(char), n, self->fp) != n) {
519 if (feof(self->fp)) {
520 PyErr_SetNone(PyExc_EOFError);
521 return -1;
524 PyErr_SetFromErrno(PyExc_IOError);
525 return -1;
528 *s = self->buf;
530 return n;
534 static int
535 readline_file(Unpicklerobject *self, char **s) {
536 int i;
538 if (self->buf_size == 0) {
539 UNLESS (self->buf = (char *)malloc(40 * sizeof(char))) {
540 PyErr_NoMemory();
541 return -1;
544 self->buf_size = 40;
547 i = 0;
548 while (1) {
549 for (; i < (self->buf_size - 1); i++) {
550 if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
551 self->buf[i + 1] = '\0';
552 *s = self->buf;
553 return i + 1;
557 UNLESS (self->buf = (char *)realloc(self->buf,
558 (self->buf_size * 2) * sizeof(char))) {
559 PyErr_NoMemory();
560 return -1;
563 self->buf_size *= 2;
569 static int
570 read_cStringIO(Unpicklerobject *self, char **s, int n) {
571 char *ptr;
573 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
574 PyErr_SetNone(PyExc_EOFError);
575 return -1;
578 *s = ptr;
580 return n;
584 static int
585 readline_cStringIO(Unpicklerobject *self, char **s) {
586 int n;
587 char *ptr;
589 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
590 return -1;
593 *s = ptr;
595 return n;
599 static int
600 read_other(Unpicklerobject *self, char **s, int n) {
601 PyObject *bytes, *str=0;
603 UNLESS (bytes = PyInt_FromLong(n)) return -1;
605 ARG_TUP(self, bytes);
606 if (self->arg) {
607 str = PyObject_CallObject(self->read, self->arg);
608 FREE_ARG_TUP(self);
610 if (! str) return -1;
612 Py_XDECREF(self->last_string);
613 self->last_string = str;
615 if (! (*s = PyString_AsString(str))) return -1;
616 return n;
620 static int
621 readline_other(Unpicklerobject *self, char **s) {
622 PyObject *str;
623 int str_size;
625 UNLESS (str = PyObject_CallObject(self->readline, empty_tuple)) {
626 return -1;
629 if ((str_size = PyString_Size(str)) < 0)
630 return -1;
632 Py_XDECREF(self->last_string);
633 self->last_string = str;
635 if (! (*s = PyString_AsString(str)))
636 return -1;
638 return str_size;
642 static char *
643 pystrndup(char *s, int l) {
644 char *r;
645 UNLESS (r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
646 memcpy(r,s,l);
647 r[l]=0;
648 return r;
652 static int
653 get(Picklerobject *self, PyObject *id) {
654 PyObject *value, *mv;
655 long c_value;
656 char s[30];
657 int len;
659 UNLESS (mv = PyDict_GetItem(self->memo, id)) {
660 PyErr_SetObject(PyExc_KeyError, id);
661 return -1;
664 UNLESS (value = PyTuple_GetItem(mv, 0))
665 return -1;
667 UNLESS (PyInt_Check(value)) {
668 PyErr_SetString(PicklingError, "no int where int expected in memo");
669 return -1;
671 c_value = PyInt_AS_LONG((PyIntObject*)value);
673 if (!self->bin) {
674 s[0] = GET;
675 sprintf(s + 1, "%ld\n", c_value);
676 len = strlen(s);
678 else if (Pdata_Check(self->file)) {
679 if (write_other(self, NULL, 0) < 0) return -1;
680 PDATA_APPEND(self->file, mv, -1);
681 return 0;
683 else {
684 if (c_value < 256) {
685 s[0] = BINGET;
686 s[1] = (int)(c_value & 0xff);
687 len = 2;
689 else {
690 s[0] = LONG_BINGET;
691 s[1] = (int)(c_value & 0xff);
692 s[2] = (int)((c_value >> 8) & 0xff);
693 s[3] = (int)((c_value >> 16) & 0xff);
694 s[4] = (int)((c_value >> 24) & 0xff);
695 len = 5;
699 if ((*self->write_func)(self, s, len) < 0)
700 return -1;
702 return 0;
706 static int
707 put(Picklerobject *self, PyObject *ob) {
708 if (ob->ob_refcnt < 2 || self->fast)
709 return 0;
711 return put2(self, ob);
715 static int
716 put2(Picklerobject *self, PyObject *ob) {
717 char c_str[30];
718 int p, len, res = -1;
719 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
721 if (self->fast) return 0;
723 if ((p = PyDict_Size(self->memo)) < 0)
724 goto finally;
726 p++; /* Make sure memo keys are positive! */
728 UNLESS (py_ob_id = PyInt_FromLong((long)ob))
729 goto finally;
731 UNLESS (memo_len = PyInt_FromLong(p))
732 goto finally;
734 UNLESS (t = PyTuple_New(2))
735 goto finally;
737 PyTuple_SET_ITEM(t, 0, memo_len);
738 Py_INCREF(memo_len);
739 PyTuple_SET_ITEM(t, 1, ob);
740 Py_INCREF(ob);
742 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
743 goto finally;
745 if (!self->bin) {
746 c_str[0] = PUT;
747 sprintf(c_str + 1, "%d\n", p);
748 len = strlen(c_str);
750 else if (Pdata_Check(self->file)) {
751 if (write_other(self, NULL, 0) < 0) return -1;
752 PDATA_APPEND(self->file, memo_len, -1);
753 res=0; /* Job well done ;) */
754 goto finally;
756 else {
757 if (p >= 256) {
758 c_str[0] = LONG_BINPUT;
759 c_str[1] = (int)(p & 0xff);
760 c_str[2] = (int)((p >> 8) & 0xff);
761 c_str[3] = (int)((p >> 16) & 0xff);
762 c_str[4] = (int)((p >> 24) & 0xff);
763 len = 5;
765 else {
766 c_str[0] = BINPUT;
767 c_str[1] = p;
768 len = 2;
772 if ((*self->write_func)(self, c_str, len) < 0)
773 goto finally;
775 res = 0;
777 finally:
778 Py_XDECREF(py_ob_id);
779 Py_XDECREF(memo_len);
780 Py_XDECREF(t);
782 return res;
785 #define PyImport_Import cPickle_Import
787 static PyObject *
788 PyImport_Import(PyObject *module_name) {
789 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
790 static PyObject *standard_builtins=0;
791 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
793 UNLESS (silly_list) {
794 UNLESS (__import___str=PyString_FromString("__import__"))
795 return NULL;
796 UNLESS (__builtins___str=PyString_FromString("__builtins__"))
797 return NULL;
798 UNLESS (silly_list=Py_BuildValue("[s]","__doc__"))
799 return NULL;
802 if ((globals=PyEval_GetGlobals())) {
803 Py_INCREF(globals);
804 UNLESS (__builtins__=PyObject_GetItem(globals,__builtins___str))
805 goto err;
807 else {
808 PyErr_Clear();
810 UNLESS (standard_builtins ||
811 (standard_builtins=PyImport_ImportModule("__builtin__")))
812 return NULL;
814 __builtins__=standard_builtins;
815 Py_INCREF(__builtins__);
816 UNLESS (globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
817 goto err;
820 if (PyDict_Check(__builtins__)) {
821 UNLESS (__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
823 else {
824 UNLESS (__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
827 UNLESS (r=PyObject_CallFunction(__import__,"OOOO",
828 module_name, globals, globals, silly_list))
829 goto err;
831 Py_DECREF(globals);
832 Py_DECREF(__builtins__);
833 Py_DECREF(__import__);
835 return r;
836 err:
837 Py_XDECREF(globals);
838 Py_XDECREF(__builtins__);
839 Py_XDECREF(__import__);
840 return NULL;
843 static PyObject *
844 whichmodule(PyObject *global, PyObject *global_name) {
845 int i, j;
846 PyObject *module = 0, *modules_dict = 0,
847 *global_name_attr = 0, *name = 0;
849 module = PyObject_GetAttrString(global, "__module__");
850 if (module) return module;
851 PyErr_Clear();
853 UNLESS (modules_dict = PySys_GetObject("modules"))
854 return NULL;
856 i = 0;
857 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
859 if (PyObject_Compare(name, __main___str)==0) continue;
861 UNLESS (global_name_attr = PyObject_GetAttr(module, global_name)) {
862 PyErr_Clear();
863 continue;
866 if (global_name_attr != global) {
867 Py_DECREF(global_name_attr);
868 continue;
871 Py_DECREF(global_name_attr);
873 break;
876 /* The following implements the rule in pickle.py added in 1.5
877 that used __main__ if no module is found. I don't actually
878 like this rule. jlf
880 if (!j) {
881 j=1;
882 name=__main___str;
885 Py_INCREF(name);
886 return name;
890 static int
891 save_none(Picklerobject *self, PyObject *args) {
892 static char none = NONE;
893 if ((*self->write_func)(self, &none, 1) < 0)
894 return -1;
896 return 0;
900 static int
901 save_int(Picklerobject *self, PyObject *args) {
902 char c_str[32];
903 long l = PyInt_AS_LONG((PyIntObject *)args);
904 int len = 0;
906 if (!self->bin
907 #if SIZEOF_LONG > 4
908 || (l >> 32)
909 #endif
911 /* Save extra-long ints in non-binary mode, so that
912 we can use python long parsing code to restore,
913 if necessary. */
914 c_str[0] = INT;
915 sprintf(c_str + 1, "%ld\n", l);
916 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
917 return -1;
919 else {
920 c_str[1] = (int)( l & 0xff);
921 c_str[2] = (int)((l >> 8) & 0xff);
922 c_str[3] = (int)((l >> 16) & 0xff);
923 c_str[4] = (int)((l >> 24) & 0xff);
925 if ((c_str[4] == 0) && (c_str[3] == 0)) {
926 if (c_str[2] == 0) {
927 c_str[0] = BININT1;
928 len = 2;
930 else {
931 c_str[0] = BININT2;
932 len = 3;
935 else {
936 c_str[0] = BININT;
937 len = 5;
940 if ((*self->write_func)(self, c_str, len) < 0)
941 return -1;
944 return 0;
948 static int
949 save_long(Picklerobject *self, PyObject *args) {
950 int size, res = -1;
951 PyObject *repr = 0;
953 static char l = LONG;
955 UNLESS (repr = PyObject_Repr(args))
956 goto finally;
958 if ((size = PyString_Size(repr)) < 0)
959 goto finally;
961 if ((*self->write_func)(self, &l, 1) < 0)
962 goto finally;
964 if ((*self->write_func)(self,
965 PyString_AS_STRING((PyStringObject *)repr), size) < 0)
966 goto finally;
968 if ((*self->write_func)(self, "\n", 1) < 0)
969 goto finally;
971 res = 0;
973 finally:
974 Py_XDECREF(repr);
976 return res;
980 static int
981 save_float(Picklerobject *self, PyObject *args) {
982 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
984 if (self->bin) {
985 int s, e;
986 double f;
987 long fhi, flo;
988 char str[9], *p = str;
990 *p = BINFLOAT;
991 p++;
993 if (x < 0) {
994 s = 1;
995 x = -x;
997 else
998 s = 0;
1000 f = frexp(x, &e);
1002 /* Normalize f to be in the range [1.0, 2.0) */
1003 if (0.5 <= f && f < 1.0) {
1004 f *= 2.0;
1005 e--;
1007 else if (f == 0.0) {
1008 e = 0;
1010 else {
1011 PyErr_SetString(PyExc_SystemError,
1012 "frexp() result out of range");
1013 return -1;
1016 if (e >= 1024) {
1017 /* XXX 1024 itself is reserved for Inf/NaN */
1018 PyErr_SetString(PyExc_OverflowError,
1019 "float too large to pack with d format");
1020 return -1;
1022 else if (e < -1022) {
1023 /* Gradual underflow */
1024 f = ldexp(f, 1022 + e);
1025 e = 0;
1027 else if (!(e == 0 && f == 0.0)) {
1028 e += 1023;
1029 f -= 1.0; /* Get rid of leading 1 */
1032 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1033 f *= 268435456.0; /* 2**28 */
1034 fhi = (long) floor(f); /* Truncate */
1035 f -= (double)fhi;
1036 f *= 16777216.0; /* 2**24 */
1037 flo = (long) floor(f + 0.5); /* Round */
1039 /* First byte */
1040 *p = (s<<7) | (e>>4);
1041 p++;
1043 /* Second byte */
1044 *p = (char) (((e&0xF)<<4) | (fhi>>24));
1045 p++;
1047 /* Third byte */
1048 *p = (fhi>>16) & 0xFF;
1049 p++;
1051 /* Fourth byte */
1052 *p = (fhi>>8) & 0xFF;
1053 p++;
1055 /* Fifth byte */
1056 *p = fhi & 0xFF;
1057 p++;
1059 /* Sixth byte */
1060 *p = (flo>>16) & 0xFF;
1061 p++;
1063 /* Seventh byte */
1064 *p = (flo>>8) & 0xFF;
1065 p++;
1067 /* Eighth byte */
1068 *p = flo & 0xFF;
1070 if ((*self->write_func)(self, str, 9) < 0)
1071 return -1;
1073 else {
1074 char c_str[250];
1075 c_str[0] = FLOAT;
1076 sprintf(c_str + 1, "%.17g\n", x);
1078 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
1079 return -1;
1082 return 0;
1086 static int
1087 save_string(Picklerobject *self, PyObject *args, int doput) {
1088 int size, len;
1089 PyObject *repr=0;
1091 if ((size = PyString_Size(args)) < 0)
1092 return -1;
1094 if (!self->bin) {
1095 char *repr_str;
1097 static char string = STRING;
1099 UNLESS (repr = PyObject_Repr(args))
1100 return -1;
1102 if ((len = PyString_Size(repr)) < 0)
1103 goto err;
1104 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1106 if ((*self->write_func)(self, &string, 1) < 0)
1107 goto err;
1109 if ((*self->write_func)(self, repr_str, len) < 0)
1110 goto err;
1112 if ((*self->write_func)(self, "\n", 1) < 0)
1113 goto err;
1115 Py_XDECREF(repr);
1117 else {
1118 int i;
1119 char c_str[5];
1121 if ((size = PyString_Size(args)) < 0)
1122 return -1;
1124 if (size < 256) {
1125 c_str[0] = SHORT_BINSTRING;
1126 c_str[1] = size;
1127 len = 2;
1129 else {
1130 c_str[0] = BINSTRING;
1131 for (i = 1; i < 5; i++)
1132 c_str[i] = (int)(size >> ((i - 1) * 8));
1133 len = 5;
1136 if ((*self->write_func)(self, c_str, len) < 0)
1137 return -1;
1139 if (size > 128 && Pdata_Check(self->file)) {
1140 if (write_other(self, NULL, 0) < 0) return -1;
1141 PDATA_APPEND(self->file, args, -1);
1143 else {
1144 if ((*self->write_func)(self,
1145 PyString_AS_STRING((PyStringObject *)args), size) < 0)
1146 return -1;
1150 if (doput)
1151 if (put(self, args) < 0)
1152 return -1;
1154 return 0;
1156 err:
1157 Py_XDECREF(repr);
1158 return -1;
1162 static int
1163 save_tuple(Picklerobject *self, PyObject *args) {
1164 PyObject *element = 0, *py_tuple_id = 0;
1165 int len, i, has_key, res = -1;
1167 static char tuple = TUPLE;
1169 if ((*self->write_func)(self, &MARKv, 1) < 0)
1170 goto finally;
1172 if ((len = PyTuple_Size(args)) < 0)
1173 goto finally;
1175 for (i = 0; i < len; i++) {
1176 UNLESS (element = PyTuple_GET_ITEM((PyTupleObject *)args, i))
1177 goto finally;
1179 if (save(self, element, 0) < 0)
1180 goto finally;
1183 UNLESS (py_tuple_id = PyInt_FromLong((long)args))
1184 goto finally;
1186 if (len) {
1187 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
1188 goto finally;
1190 if (has_key) {
1191 if (self->bin) {
1192 static char pop_mark = POP_MARK;
1194 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1195 goto finally;
1197 else {
1198 static char pop = POP;
1200 for (i = 0; i <= len; i++) {
1201 if ((*self->write_func)(self, &pop, 1) < 0)
1202 goto finally;
1206 if (get(self, py_tuple_id) < 0)
1207 goto finally;
1209 res = 0;
1210 goto finally;
1214 if ((*self->write_func)(self, &tuple, 1) < 0) {
1215 goto finally;
1218 if (put(self, args) < 0)
1219 goto finally;
1221 res = 0;
1223 finally:
1224 Py_XDECREF(py_tuple_id);
1226 return res;
1229 static int
1230 save_empty_tuple(Picklerobject *self, PyObject *args) {
1231 static char tuple = EMPTY_TUPLE;
1233 return (*self->write_func)(self, &tuple, 1);
1237 static int
1238 save_list(Picklerobject *self, PyObject *args) {
1239 PyObject *element = 0;
1240 int s_len, len, i, using_appends, res = -1;
1241 char s[3];
1243 static char append = APPEND, appends = APPENDS;
1245 if (self->bin) {
1246 s[0] = EMPTY_LIST;
1247 s_len = 1;
1249 else {
1250 s[0] = MARK;
1251 s[1] = LIST;
1252 s_len = 2;
1255 if ((len = PyList_Size(args)) < 0)
1256 goto finally;
1258 if ((*self->write_func)(self, s, s_len) < 0)
1259 goto finally;
1261 if (len == 0) {
1262 if (put(self, args) < 0)
1263 goto finally;
1265 else {
1266 if (put2(self, args) < 0)
1267 goto finally;
1270 if ((using_appends = (self->bin && (len > 1))))
1271 if ((*self->write_func)(self, &MARKv, 1) < 0)
1272 goto finally;
1274 for (i = 0; i < len; i++) {
1275 UNLESS (element = PyList_GET_ITEM((PyListObject *)args, i))
1276 goto finally;
1278 if (save(self, element, 0) < 0)
1279 goto finally;
1281 if (!using_appends) {
1282 if ((*self->write_func)(self, &append, 1) < 0)
1283 goto finally;
1287 if (using_appends) {
1288 if ((*self->write_func)(self, &appends, 1) < 0)
1289 goto finally;
1292 res = 0;
1294 finally:
1296 return res;
1300 static int
1301 save_dict(Picklerobject *self, PyObject *args) {
1302 PyObject *key = 0, *value = 0;
1303 int i, len, res = -1, using_setitems;
1304 char s[3];
1306 static char setitem = SETITEM, setitems = SETITEMS;
1308 if (self->bin) {
1309 s[0] = EMPTY_DICT;
1310 len = 1;
1312 else {
1313 s[0] = MARK;
1314 s[1] = DICT;
1315 len = 2;
1318 if ((*self->write_func)(self, s, len) < 0)
1319 goto finally;
1321 if ((len = PyDict_Size(args)) < 0)
1322 goto finally;
1324 if (len == 0) {
1325 if (put(self, args) < 0)
1326 goto finally;
1328 else {
1329 if (put2(self, args) < 0)
1330 goto finally;
1333 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
1334 if ((*self->write_func)(self, &MARKv, 1) < 0)
1335 goto finally;
1337 i = 0;
1338 while (PyDict_Next(args, &i, &key, &value)) {
1339 if (save(self, key, 0) < 0)
1340 goto finally;
1342 if (save(self, value, 0) < 0)
1343 goto finally;
1345 if (!using_setitems) {
1346 if ((*self->write_func)(self, &setitem, 1) < 0)
1347 goto finally;
1351 if (using_setitems) {
1352 if ((*self->write_func)(self, &setitems, 1) < 0)
1353 goto finally;
1356 res = 0;
1358 finally:
1360 return res;
1364 static int
1365 save_inst(Picklerobject *self, PyObject *args) {
1366 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1367 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1368 char *module_str, *name_str;
1369 int module_size, name_size, res = -1;
1371 static char inst = INST, obj = OBJ, build = BUILD;
1373 if ((*self->write_func)(self, &MARKv, 1) < 0)
1374 goto finally;
1376 UNLESS (class = PyObject_GetAttr(args, __class___str))
1377 goto finally;
1379 if (self->bin) {
1380 if (save(self, class, 0) < 0)
1381 goto finally;
1384 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1385 PyObject *element = 0;
1386 int i, len;
1388 UNLESS (class_args =
1389 PyObject_CallObject(getinitargs_func, empty_tuple))
1390 goto finally;
1392 if ((len = PyObject_Length(class_args)) < 0)
1393 goto finally;
1395 for (i = 0; i < len; i++) {
1396 UNLESS (element = PySequence_GetItem(class_args, i))
1397 goto finally;
1399 if (save(self, element, 0) < 0) {
1400 Py_DECREF(element);
1401 goto finally;
1404 Py_DECREF(element);
1407 else {
1408 PyErr_Clear();
1411 if (!self->bin) {
1412 UNLESS (name = ((PyClassObject *)class)->cl_name) {
1413 PyErr_SetString(PicklingError, "class has no name");
1414 goto finally;
1417 UNLESS (module = whichmodule(class, name))
1418 goto finally;
1421 if ((module_size = PyString_Size(module)) < 0 ||
1422 (name_size = PyString_Size(name)) < 0)
1423 goto finally;
1425 module_str = PyString_AS_STRING((PyStringObject *)module);
1426 name_str = PyString_AS_STRING((PyStringObject *)name);
1428 if ((*self->write_func)(self, &inst, 1) < 0)
1429 goto finally;
1431 if ((*self->write_func)(self, module_str, module_size) < 0)
1432 goto finally;
1434 if ((*self->write_func)(self, "\n", 1) < 0)
1435 goto finally;
1437 if ((*self->write_func)(self, name_str, name_size) < 0)
1438 goto finally;
1440 if ((*self->write_func)(self, "\n", 1) < 0)
1441 goto finally;
1443 else if ((*self->write_func)(self, &obj, 1) < 0) {
1444 goto finally;
1447 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1448 UNLESS (state = PyObject_CallObject(getstate_func, empty_tuple))
1449 goto finally;
1451 else {
1452 PyErr_Clear();
1454 UNLESS (state = PyObject_GetAttr(args, __dict___str)) {
1455 PyErr_Clear();
1456 res = 0;
1457 goto finally;
1461 if (!PyDict_Check(state)) {
1462 if (put2(self, args) < 0)
1463 goto finally;
1465 else {
1466 if (put(self, args) < 0)
1467 goto finally;
1470 if (save(self, state, 0) < 0)
1471 goto finally;
1473 if ((*self->write_func)(self, &build, 1) < 0)
1474 goto finally;
1476 res = 0;
1478 finally:
1479 Py_XDECREF(module);
1480 Py_XDECREF(class);
1481 Py_XDECREF(state);
1482 Py_XDECREF(getinitargs_func);
1483 Py_XDECREF(getstate_func);
1484 Py_XDECREF(class_args);
1486 return res;
1490 static int
1491 save_global(Picklerobject *self, PyObject *args, PyObject *name) {
1492 PyObject *global_name = 0, *module = 0;
1493 char *name_str, *module_str;
1494 int module_size, name_size, res = -1;
1496 static char global = GLOBAL;
1498 if (name) {
1499 global_name = name;
1500 Py_INCREF(global_name);
1502 else {
1503 UNLESS (global_name = PyObject_GetAttr(args, __name___str))
1504 goto finally;
1507 UNLESS (module = whichmodule(args, global_name))
1508 goto finally;
1510 if ((module_size = PyString_Size(module)) < 0 ||
1511 (name_size = PyString_Size(global_name)) < 0)
1512 goto finally;
1514 module_str = PyString_AS_STRING((PyStringObject *)module);
1515 name_str = PyString_AS_STRING((PyStringObject *)global_name);
1517 if ((*self->write_func)(self, &global, 1) < 0)
1518 goto finally;
1520 if ((*self->write_func)(self, module_str, module_size) < 0)
1521 goto finally;
1523 if ((*self->write_func)(self, "\n", 1) < 0)
1524 goto finally;
1526 if ((*self->write_func)(self, name_str, name_size) < 0)
1527 goto finally;
1529 if ((*self->write_func)(self, "\n", 1) < 0)
1530 goto finally;
1532 if (put(self, args) < 0)
1533 goto finally;
1535 res = 0;
1537 finally:
1538 Py_XDECREF(module);
1539 Py_XDECREF(global_name);
1541 return res;
1544 static int
1545 save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
1546 PyObject *pid = 0;
1547 int size, res = -1;
1549 static char persid = PERSID, binpersid = BINPERSID;
1551 Py_INCREF(args);
1552 ARG_TUP(self, args);
1553 if (self->arg) {
1554 pid = PyObject_CallObject(f, self->arg);
1555 FREE_ARG_TUP(self);
1557 if (! pid) return -1;
1559 if (pid != Py_None) {
1560 if (!self->bin) {
1561 if (!PyString_Check(pid)) {
1562 PyErr_SetString(PicklingError,
1563 "persistent id must be string");
1564 goto finally;
1567 if ((*self->write_func)(self, &persid, 1) < 0)
1568 goto finally;
1570 if ((size = PyString_Size(pid)) < 0)
1571 goto finally;
1573 if ((*self->write_func)(self,
1574 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1575 goto finally;
1577 if ((*self->write_func)(self, "\n", 1) < 0)
1578 goto finally;
1580 res = 1;
1581 goto finally;
1583 else if (save(self, pid, 1) >= 0) {
1584 if ((*self->write_func)(self, &binpersid, 1) < 0)
1585 res = -1;
1586 else
1587 res = 1;
1590 goto finally;
1593 res = 0;
1595 finally:
1596 Py_XDECREF(pid);
1598 return res;
1602 static int
1603 save_reduce(Picklerobject *self, PyObject *callable,
1604 PyObject *tup, PyObject *state, PyObject *ob) {
1605 static char reduce = REDUCE, build = BUILD;
1607 if (save(self, callable, 0) < 0)
1608 return -1;
1610 if (save(self, tup, 0) < 0)
1611 return -1;
1613 if ((*self->write_func)(self, &reduce, 1) < 0)
1614 return -1;
1616 if (ob != NULL) {
1617 if (state && !PyDict_Check(state)) {
1618 if (put2(self, ob) < 0)
1619 return -1;
1621 else {
1622 if (put(self, ob) < 0)
1623 return -1;
1627 if (state) {
1628 if (save(self, state, 0) < 0)
1629 return -1;
1631 if ((*self->write_func)(self, &build, 1) < 0)
1632 return -1;
1635 return 0;
1638 static int
1639 save(Picklerobject *self, PyObject *args, int pers_save) {
1640 PyTypeObject *type;
1641 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
1642 *callable = 0, *state = 0;
1643 int res = -1, tmp, size;
1645 if (!pers_save && self->pers_func) {
1646 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1647 res = tmp;
1648 goto finally;
1652 if (args == Py_None) {
1653 res = save_none(self, args);
1654 goto finally;
1657 type = args->ob_type;
1659 switch (type->tp_name[0]) {
1660 case 'i':
1661 if (type == &PyInt_Type) {
1662 res = save_int(self, args);
1663 goto finally;
1665 break;
1667 case 'l':
1668 if (type == &PyLong_Type) {
1669 res = save_long(self, args);
1670 goto finally;
1672 break;
1674 case 'f':
1675 if (type == &PyFloat_Type) {
1676 res = save_float(self, args);
1677 goto finally;
1679 break;
1681 case 't':
1682 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
1683 if (self->bin) res = save_empty_tuple(self, args);
1684 else res = save_tuple(self, args);
1685 goto finally;
1687 break;
1689 case 's':
1690 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
1691 res = save_string(self, args, 0);
1692 goto finally;
1696 if (args->ob_refcnt > 1) {
1697 long ob_id;
1698 int has_key;
1700 ob_id = (long)args;
1702 UNLESS (py_ob_id = PyInt_FromLong(ob_id))
1703 goto finally;
1705 if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
1706 goto finally;
1708 if (has_key) {
1709 if (get(self, py_ob_id) < 0)
1710 goto finally;
1712 res = 0;
1713 goto finally;
1717 switch (type->tp_name[0]) {
1718 case 's':
1719 if (type == &PyString_Type) {
1720 res = save_string(self, args, 1);
1721 goto finally;
1723 break;
1725 case 't':
1726 if (type == &PyTuple_Type) {
1727 res = save_tuple(self, args);
1728 goto finally;
1730 break;
1732 case 'l':
1733 if (type == &PyList_Type) {
1734 res = save_list(self, args);
1735 goto finally;
1737 break;
1739 case 'd':
1740 if (type == &PyDict_Type) {
1741 res = save_dict(self, args);
1742 goto finally;
1744 break;
1746 case 'i':
1747 if (type == &PyInstance_Type) {
1748 res = save_inst(self, args);
1749 goto finally;
1751 break;
1753 case 'c':
1754 if (type == &PyClass_Type) {
1755 res = save_global(self, args, NULL);
1756 goto finally;
1758 break;
1760 case 'f':
1761 if (type == &PyFunction_Type) {
1762 res = save_global(self, args, NULL);
1763 goto finally;
1765 break;
1767 case 'b':
1768 if (type == &PyCFunction_Type) {
1769 res = save_global(self, args, NULL);
1770 goto finally;
1774 if (!pers_save && self->inst_pers_func) {
1775 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
1776 res = tmp;
1777 goto finally;
1781 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
1782 Py_INCREF(__reduce__);
1784 Py_INCREF(args);
1785 ARG_TUP(self, args);
1786 if (self->arg) {
1787 t = PyObject_CallObject(__reduce__, self->arg);
1788 FREE_ARG_TUP(self);
1790 if (! t) goto finally;
1792 else {
1793 PyErr_Clear();
1795 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
1796 UNLESS (t = PyObject_CallObject(__reduce__, empty_tuple))
1797 goto finally;
1799 else {
1800 PyErr_Clear();
1804 if (t) {
1805 if (PyString_Check(t)) {
1806 res = save_global(self, args, t);
1807 goto finally;
1810 if (!PyTuple_Check(t)) {
1811 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
1812 "be a tuple", "O", __reduce__);
1813 goto finally;
1816 size = PyTuple_Size(t);
1818 if ((size != 3) && (size != 2)) {
1819 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
1820 "contain only two or three elements", "O", __reduce__);
1821 goto finally;
1824 callable = PyTuple_GET_ITEM(t, 0);
1826 arg_tup = PyTuple_GET_ITEM(t, 1);
1828 if (size > 2) {
1829 state = PyTuple_GET_ITEM(t, 2);
1832 UNLESS (PyTuple_Check(arg_tup) || arg_tup==Py_None) {
1833 cPickle_ErrFormat(PicklingError, "Second element of tuple "
1834 "returned by %s must be a tuple", "O", __reduce__);
1835 goto finally;
1838 res = save_reduce(self, callable, arg_tup, state, args);
1839 goto finally;
1842 PyErr_SetObject(UnpickleableError, args);
1844 finally:
1845 Py_XDECREF(py_ob_id);
1846 Py_XDECREF(__reduce__);
1847 Py_XDECREF(t);
1849 return res;
1853 static int
1854 dump(Picklerobject *self, PyObject *args) {
1855 static char stop = STOP;
1857 if (save(self, args, 0) < 0)
1858 return -1;
1860 if ((*self->write_func)(self, &stop, 1) < 0)
1861 return -1;
1863 if ((*self->write_func)(self, NULL, 0) < 0)
1864 return -1;
1866 return 0;
1869 static PyObject *
1870 Pickle_clear_memo(Picklerobject *self, PyObject *args) {
1871 if (args && ! PyArg_ParseTuple(args,"")) return NULL;
1872 if (self->memo) PyDict_Clear(self->memo);
1873 Py_INCREF(Py_None);
1874 return Py_None;
1877 static PyObject *
1878 Pickle_getvalue(Picklerobject *self, PyObject *args) {
1879 int l, i, rsize, ssize, clear=1, lm;
1880 long ik;
1881 PyObject *k, *r;
1882 char *s, *p, *have_get;
1883 Pdata *data;
1885 if (args && ! PyArg_ParseTuple(args,"|i",&clear)) return NULL;
1887 /* Check to make sure we are based on a list */
1888 if (! Pdata_Check(self->file)) {
1889 PyErr_SetString(PicklingError,
1890 "Attempt to getvalue a non-list-based pickler");
1891 return NULL;
1894 /* flush write buffer */
1895 if (write_other(self, NULL, 0) < 0) return NULL;
1897 data=(Pdata*)self->file;
1898 l=data->length;
1900 /* set up an array to hold get/put status */
1901 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
1902 lm++;
1903 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
1904 memset(have_get,0,lm);
1906 /* Scan for gets. */
1907 for (rsize=0, i=l; --i >= 0; ) {
1908 k=data->data[i];
1910 if (PyString_Check(k)) {
1911 rsize += PyString_GET_SIZE(k);
1914 else if (PyInt_Check(k)) { /* put */
1915 ik=PyInt_AS_LONG((PyIntObject*)k);
1916 if (ik >= lm || ik==0) {
1917 PyErr_SetString(PicklingError,
1918 "Invalid get data");
1919 return NULL;
1921 if (have_get[ik]) { /* with matching get */
1922 if (ik < 256) rsize += 2;
1923 else rsize+=5;
1927 else if (! (PyTuple_Check(k) &&
1928 PyTuple_GET_SIZE(k) == 2 &&
1929 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
1931 PyErr_SetString(PicklingError,
1932 "Unexpected data in internal list");
1933 return NULL;
1936 else { /* put */
1937 ik=PyInt_AS_LONG((PyIntObject*)k);
1938 if (ik >= lm || ik==0) {
1939 PyErr_SetString(PicklingError,
1940 "Invalid get data");
1941 return NULL;
1943 have_get[ik]=1;
1944 if (ik < 256) rsize += 2;
1945 else rsize+=5;
1950 /* Now generate the result */
1951 UNLESS (r=PyString_FromStringAndSize(NULL,rsize)) goto err;
1952 s=PyString_AS_STRING((PyStringObject*)r);
1954 for (i=0; i<l; i++) {
1955 k=data->data[i];
1957 if (PyString_Check(k)) {
1958 ssize=PyString_GET_SIZE(k);
1959 if (ssize) {
1960 p=PyString_AS_STRING((PyStringObject*)k);
1961 while (--ssize >= 0) *s++=*p++;
1965 else if (PyTuple_Check(k)) { /* get */
1966 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
1967 if (ik < 256) {
1968 *s++ = BINGET;
1969 *s++ = (int)(ik & 0xff);
1971 else {
1972 *s++ = LONG_BINGET;
1973 *s++ = (int)(ik & 0xff);
1974 *s++ = (int)((ik >> 8) & 0xff);
1975 *s++ = (int)((ik >> 16) & 0xff);
1976 *s++ = (int)((ik >> 24) & 0xff);
1980 else { /* put */
1981 ik=PyInt_AS_LONG((PyIntObject*)k);
1983 if (have_get[ik]) { /* with matching get */
1984 if (ik < 256) {
1985 *s++ = BINPUT;
1986 *s++ = (int)(ik & 0xff);
1988 else {
1989 *s++ = LONG_BINPUT;
1990 *s++ = (int)(ik & 0xff);
1991 *s++ = (int)((ik >> 8) & 0xff);
1992 *s++ = (int)((ik >> 16) & 0xff);
1993 *s++ = (int)((ik >> 24) & 0xff);
2000 if (clear) {
2001 PyDict_Clear(self->memo);
2002 Pdata_clear(data,0);
2005 free(have_get);
2006 return r;
2007 err:
2008 free(have_get);
2009 return NULL;
2012 static PyObject *
2013 Pickler_dump(Picklerobject *self, PyObject *args) {
2014 PyObject *ob;
2015 int get=0;
2017 UNLESS (PyArg_ParseTuple(args, "O|i", &ob, &get))
2018 return NULL;
2020 if (dump(self, ob) < 0)
2021 return NULL;
2023 if (get) return Pickle_getvalue(self, NULL);
2025 Py_INCREF(self);
2026 return (PyObject*)self;
2030 static struct PyMethodDef Pickler_methods[] = {
2031 {"dump", (PyCFunction)Pickler_dump, 1,
2032 "dump(object) --"
2033 "Write an object in pickle format to the object's pickle stream\n"
2035 {"clear_memo", (PyCFunction)Pickle_clear_memo, 1,
2036 "clear_memo() -- Clear the picklers memo"},
2037 {"getvalue", (PyCFunction)Pickle_getvalue, 1,
2038 "getvalue() -- Finish picking a list-based pickle"},
2039 {NULL, NULL} /* sentinel */
2043 static Picklerobject *
2044 newPicklerobject(PyObject *file, int bin) {
2045 Picklerobject *self;
2047 UNLESS (self = PyObject_NEW(Picklerobject, &Picklertype))
2048 return NULL;
2050 self->fp = NULL;
2051 self->write = NULL;
2052 self->memo = NULL;
2053 self->arg = NULL;
2054 self->pers_func = NULL;
2055 self->inst_pers_func = NULL;
2056 self->write_buf = NULL;
2057 self->bin = bin;
2058 self->fast = 0;
2059 self->buf_size = 0;
2060 self->dispatch_table = NULL;
2062 if (file)
2063 Py_INCREF(file);
2064 else
2065 file=Pdata_New();
2067 self->file = file;
2069 UNLESS (self->memo = PyDict_New()) {
2070 Py_XDECREF((PyObject *)self);
2071 return NULL;
2074 if (PyFile_Check(file)) {
2075 self->fp = PyFile_AsFile(file);
2076 if (self->fp == NULL) {
2077 PyErr_SetString(PyExc_IOError, "output file closed");
2078 return NULL;
2080 self->write_func = write_file;
2082 else if (PycStringIO_OutputCheck(file)) {
2083 self->write_func = write_cStringIO;
2085 else if (file == Py_None) {
2086 self->write_func = write_none;
2088 else {
2089 self->write_func = write_other;
2091 if (! Pdata_Check(file)) {
2092 UNLESS (self->write = PyObject_GetAttr(file, write_str)) {
2093 PyErr_Clear();
2094 PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
2095 "attribute");
2096 goto err;
2100 UNLESS (self->write_buf =
2101 (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) {
2102 PyErr_NoMemory();
2103 goto err;
2107 if (PyEval_GetRestricted()) {
2108 /* Restricted execution, get private tables */
2109 PyObject *m;
2111 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
2112 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2113 Py_DECREF(m);
2114 UNLESS (self->dispatch_table) goto err;
2116 else {
2117 self->dispatch_table=dispatch_table;
2118 Py_INCREF(dispatch_table);
2121 return self;
2123 err:
2124 Py_DECREF((PyObject *)self);
2125 return NULL;
2129 static PyObject *
2130 get_Pickler(PyObject *self, PyObject *args) {
2131 PyObject *file=NULL;
2132 int bin;
2134 bin=1;
2135 if (! PyArg_ParseTuple(args, "|i", &bin)) {
2136 PyErr_Clear();
2137 bin=0;
2138 if (! PyArg_ParseTuple(args, "O|i", &file, &bin))
2139 return NULL;
2141 return (PyObject *)newPicklerobject(file, bin);
2145 static void
2146 Pickler_dealloc(Picklerobject *self) {
2147 Py_XDECREF(self->write);
2148 Py_XDECREF(self->memo);
2149 Py_XDECREF(self->arg);
2150 Py_XDECREF(self->file);
2151 Py_XDECREF(self->pers_func);
2152 Py_XDECREF(self->inst_pers_func);
2153 Py_XDECREF(self->dispatch_table);
2155 if (self->write_buf) {
2156 free(self->write_buf);
2159 PyMem_DEL(self);
2163 static PyObject *
2164 Pickler_getattr(Picklerobject *self, char *name) {
2166 switch (*name) {
2167 case 'p':
2168 if (strcmp(name, "persistent_id") == 0) {
2169 if (!self->pers_func) {
2170 PyErr_SetString(PyExc_AttributeError, name);
2171 return NULL;
2174 Py_INCREF(self->pers_func);
2175 return self->pers_func;
2177 break;
2178 case 'm':
2179 if (strcmp(name, "memo") == 0) {
2180 if (!self->memo) {
2181 PyErr_SetString(PyExc_AttributeError, name);
2182 return NULL;
2185 Py_INCREF(self->memo);
2186 return self->memo;
2188 break;
2189 case 'P':
2190 if (strcmp(name, "PicklingError") == 0) {
2191 Py_INCREF(PicklingError);
2192 return PicklingError;
2194 break;
2195 case 'b':
2196 if (strcmp(name, "binary")==0)
2197 return PyInt_FromLong(self->bin);
2198 break;
2199 case 'f':
2200 if (strcmp(name, "fast")==0)
2201 return PyInt_FromLong(self->fast);
2202 break;
2203 case 'g':
2204 if (strcmp(name, "getvalue")==0 && ! Pdata_Check(self->file)) {
2205 PyErr_SetString(PyExc_AttributeError, name);
2206 return NULL;
2208 break;
2210 return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
2214 int
2215 Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
2217 if (! value) {
2218 PyErr_SetString(PyExc_TypeError,
2219 "attribute deletion is not supported");
2220 return -1;
2223 if (strcmp(name, "persistent_id") == 0) {
2224 Py_XDECREF(self->pers_func);
2225 self->pers_func = value;
2226 Py_INCREF(value);
2227 return 0;
2230 if (strcmp(name, "inst_persistent_id") == 0) {
2231 Py_XDECREF(self->inst_pers_func);
2232 self->inst_pers_func = value;
2233 Py_INCREF(value);
2234 return 0;
2237 if (strcmp(name, "memo") == 0) {
2238 if (! PyDict_Check(value)) {
2239 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2240 return -1;
2242 Py_XDECREF(self->memo);
2243 self->memo = value;
2244 Py_INCREF(value);
2245 return 0;
2248 if (strcmp(name, "binary")==0) {
2249 self->bin=PyObject_IsTrue(value);
2250 return 0;
2253 if (strcmp(name, "fast")==0) {
2254 self->fast=PyObject_IsTrue(value);
2255 return 0;
2258 PyErr_SetString(PyExc_AttributeError, name);
2259 return -1;
2263 static char Picklertype__doc__[] =
2264 "Objects that know how to pickle objects\n"
2267 static PyTypeObject Picklertype = {
2268 PyObject_HEAD_INIT(NULL)
2269 0, /*ob_size*/
2270 "Pickler", /*tp_name*/
2271 sizeof(Picklerobject), /*tp_basicsize*/
2272 0, /*tp_itemsize*/
2273 /* methods */
2274 (destructor)Pickler_dealloc, /*tp_dealloc*/
2275 (printfunc)0, /*tp_print*/
2276 (getattrfunc)Pickler_getattr, /*tp_getattr*/
2277 (setattrfunc)Pickler_setattr, /*tp_setattr*/
2278 (cmpfunc)0, /*tp_compare*/
2279 (reprfunc)0, /*tp_repr*/
2280 0, /*tp_as_number*/
2281 0, /*tp_as_sequence*/
2282 0, /*tp_as_mapping*/
2283 (hashfunc)0, /*tp_hash*/
2284 (ternaryfunc)0, /*tp_call*/
2285 (reprfunc)0, /*tp_str*/
2287 /* Space for future expansion */
2288 0L,0L,0L,0L,
2289 Picklertype__doc__ /* Documentation string */
2292 static PyObject *
2293 find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc) {
2294 PyObject *global = 0, *module;
2296 if (fc) {
2297 if (fc==Py_None) {
2298 PyErr_SetString(UnpicklingError,
2299 "Global and instance pickles are not supported.");
2300 return NULL;
2302 return PyObject_CallFunction(fc, "OO", py_module_name, py_global_name);
2305 module = PySys_GetObject("modules");
2306 if (module == NULL)
2307 return NULL;
2309 module = PyDict_GetItem(module, py_module_name);
2310 if (module == NULL) {
2311 module = PyImport_Import(py_module_name);
2312 if (!module)
2313 return NULL;
2314 global = PyObject_GetAttr(module, py_global_name);
2315 Py_DECREF(module);
2317 else
2318 global = PyObject_GetAttr(module, py_global_name);
2319 if (global == NULL) {
2320 char buf[256 + 37];
2321 sprintf(buf, "Failed to import class %.128s from module %.128s",
2322 PyString_AS_STRING((PyStringObject*)py_global_name),
2323 PyString_AS_STRING((PyStringObject*)py_module_name));
2324 PyErr_SetString(PyExc_SystemError, buf);
2325 return NULL;
2327 return global;
2330 static int
2331 marker(Unpicklerobject *self) {
2332 if (self->num_marks < 1) {
2333 PyErr_SetString(UnpicklingError, "could not find MARK");
2334 return -1;
2337 return self->marks[--self->num_marks];
2341 static int
2342 load_none(Unpicklerobject *self) {
2343 PDATA_APPEND(self->stack, Py_None, -1);
2344 return 0;
2347 static int
2348 bad_readline() {
2349 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2350 return -1;
2353 static int
2354 load_int(Unpicklerobject *self) {
2355 PyObject *py_int = 0;
2356 char *endptr, *s;
2357 int len, res = -1;
2358 long l;
2360 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2361 if (len < 2) return bad_readline();
2362 UNLESS (s=pystrndup(s,len)) return -1;
2364 errno = 0;
2365 l = strtol(s, &endptr, 0);
2367 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2368 /* Hm, maybe we've got something long. Let's try reading
2369 it as a Python long object. */
2370 errno=0;
2371 UNLESS (py_int=PyLong_FromString(s,&endptr,0)) goto finally;
2373 if ((*endptr != '\n') || (endptr[1] != '\0')) {
2374 PyErr_SetString(PyExc_ValueError,
2375 "could not convert string to int");
2376 goto finally;
2379 else {
2380 UNLESS (py_int = PyInt_FromLong(l)) goto finally;
2383 free(s);
2384 PDATA_PUSH(self->stack, py_int, -1);
2385 return 0;
2387 finally:
2388 free(s);
2390 return res;
2394 static long
2395 calc_binint(char *s, int x) {
2396 unsigned char c;
2397 int i;
2398 long l;
2400 for (i = 0, l = 0L; i < x; i++) {
2401 c = (unsigned char)s[i];
2402 l |= (long)c << (i * 8);
2405 return l;
2409 static int
2410 load_binintx(Unpicklerobject *self, char *s, int x) {
2411 PyObject *py_int = 0;
2412 long l;
2414 l = calc_binint(s, x);
2416 UNLESS (py_int = PyInt_FromLong(l))
2417 return -1;
2419 PDATA_PUSH(self->stack, py_int, -1);
2420 return 0;
2424 static int
2425 load_binint(Unpicklerobject *self) {
2426 char *s;
2428 if ((*self->read_func)(self, &s, 4) < 0)
2429 return -1;
2431 return load_binintx(self, s, 4);
2435 static int
2436 load_binint1(Unpicklerobject *self) {
2437 char *s;
2439 if ((*self->read_func)(self, &s, 1) < 0)
2440 return -1;
2442 return load_binintx(self, s, 1);
2446 static int
2447 load_binint2(Unpicklerobject *self) {
2448 char *s;
2450 if ((*self->read_func)(self, &s, 2) < 0)
2451 return -1;
2453 return load_binintx(self, s, 2);
2456 static int
2457 load_long(Unpicklerobject *self) {
2458 PyObject *l = 0;
2459 char *end, *s;
2460 int len, res = -1;
2462 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2463 if (len < 2) return bad_readline();
2464 UNLESS (s=pystrndup(s,len)) return -1;
2466 UNLESS (l = PyLong_FromString(s, &end, 0))
2467 goto finally;
2469 free(s);
2470 PDATA_PUSH(self->stack, l, -1);
2471 return 0;
2473 finally:
2474 free(s);
2476 return res;
2480 static int
2481 load_float(Unpicklerobject *self) {
2482 PyObject *py_float = 0;
2483 char *endptr, *s;
2484 int len, res = -1;
2485 double d;
2487 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2488 if (len < 2) return bad_readline();
2489 UNLESS (s=pystrndup(s,len)) return -1;
2491 errno = 0;
2492 d = strtod(s, &endptr);
2494 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2495 PyErr_SetString(PyExc_ValueError,
2496 "could not convert string to float");
2497 goto finally;
2500 UNLESS (py_float = PyFloat_FromDouble(d))
2501 goto finally;
2503 free(s);
2504 PDATA_PUSH(self->stack, py_float, -1);
2505 return 0;
2507 finally:
2508 free(s);
2510 return res;
2513 static int
2514 load_binfloat(Unpicklerobject *self) {
2515 PyObject *py_float = 0;
2516 int s, e;
2517 long fhi, flo;
2518 double x;
2519 char *p;
2521 if ((*self->read_func)(self, &p, 8) < 0)
2522 return -1;
2524 /* First byte */
2525 s = (*p>>7) & 1;
2526 e = (*p & 0x7F) << 4;
2527 p++;
2529 /* Second byte */
2530 e |= (*p>>4) & 0xF;
2531 fhi = (*p & 0xF) << 24;
2532 p++;
2534 /* Third byte */
2535 fhi |= (*p & 0xFF) << 16;
2536 p++;
2538 /* Fourth byte */
2539 fhi |= (*p & 0xFF) << 8;
2540 p++;
2542 /* Fifth byte */
2543 fhi |= *p & 0xFF;
2544 p++;
2546 /* Sixth byte */
2547 flo = (*p & 0xFF) << 16;
2548 p++;
2550 /* Seventh byte */
2551 flo |= (*p & 0xFF) << 8;
2552 p++;
2554 /* Eighth byte */
2555 flo |= *p & 0xFF;
2557 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2558 x /= 268435456.0; /* 2**28 */
2560 /* XXX This sadly ignores Inf/NaN */
2561 if (e == 0)
2562 e = -1022;
2563 else {
2564 x += 1.0;
2565 e -= 1023;
2567 x = ldexp(x, e);
2569 if (s)
2570 x = -x;
2572 UNLESS (py_float = PyFloat_FromDouble(x)) return -1;
2574 PDATA_PUSH(self->stack, py_float, -1);
2575 return 0;
2578 static int
2579 load_string(Unpicklerobject *self) {
2580 PyObject *str = 0;
2581 int len, res = -1, nslash;
2582 char *s, q, *p;
2584 static PyObject *eval_dict = 0;
2586 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2587 if (len < 2) return bad_readline();
2588 UNLESS (s=pystrndup(s,len)) return -1;
2590 /* Check for unquoted quotes (evil strings) */
2591 q=*s;
2592 if (q != '"' && q != '\'') goto insecure;
2593 for (p=s+1, nslash=0; *p; p++) {
2594 if (*p==q && nslash%2==0) break;
2595 if (*p=='\\') nslash++;
2596 else nslash=0;
2598 if (*p==q)
2600 for (p++; *p; p++) if (*p > ' ') goto insecure;
2602 else goto insecure;
2603 /********************************************/
2605 UNLESS (eval_dict)
2606 UNLESS (eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
2607 goto finally;
2609 UNLESS (str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
2610 goto finally;
2612 free(s);
2613 PDATA_PUSH(self->stack, str, -1);
2614 return 0;
2616 finally:
2617 free(s);
2619 return res;
2621 insecure:
2622 free(s);
2623 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2624 return -1;
2628 static int
2629 load_binstring(Unpicklerobject *self) {
2630 PyObject *py_string = 0;
2631 long l;
2632 char *s;
2634 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2636 l = calc_binint(s, 4);
2638 if ((*self->read_func)(self, &s, l) < 0)
2639 return -1;
2641 UNLESS (py_string = PyString_FromStringAndSize(s, l))
2642 return -1;
2644 PDATA_PUSH(self->stack, py_string, -1);
2645 return 0;
2649 static int
2650 load_short_binstring(Unpicklerobject *self) {
2651 PyObject *py_string = 0;
2652 unsigned char l;
2653 char *s;
2655 if ((*self->read_func)(self, &s, 1) < 0)
2656 return -1;
2658 l = (unsigned char)s[0];
2660 if ((*self->read_func)(self, &s, l) < 0) return -1;
2662 UNLESS (py_string = PyString_FromStringAndSize(s, l)) return -1;
2664 PDATA_PUSH(self->stack, py_string, -1);
2665 return 0;
2669 static int
2670 load_tuple(Unpicklerobject *self) {
2671 PyObject *tup;
2672 int i;
2674 if ((i = marker(self)) < 0) return -1;
2675 UNLESS (tup=Pdata_popTuple(self->stack, i)) return -1;
2676 PDATA_PUSH(self->stack, tup, -1);
2677 return 0;
2680 static int
2681 load_empty_tuple(Unpicklerobject *self) {
2682 PyObject *tup;
2684 UNLESS (tup=PyTuple_New(0)) return -1;
2685 PDATA_PUSH(self->stack, tup, -1);
2686 return 0;
2689 static int
2690 load_empty_list(Unpicklerobject *self) {
2691 PyObject *list;
2693 UNLESS (list=PyList_New(0)) return -1;
2694 PDATA_PUSH(self->stack, list, -1);
2695 return 0;
2698 static int
2699 load_empty_dict(Unpicklerobject *self) {
2700 PyObject *dict;
2702 UNLESS (dict=PyDict_New()) return -1;
2703 PDATA_PUSH(self->stack, dict, -1);
2704 return 0;
2708 static int
2709 load_list(Unpicklerobject *self) {
2710 PyObject *list = 0;
2711 int i;
2713 if ((i = marker(self)) < 0) return -1;
2714 UNLESS (list=Pdata_popList(self->stack, i)) return -1;
2715 PDATA_PUSH(self->stack, list, -1);
2716 return 0;
2719 static int
2720 load_dict(Unpicklerobject *self) {
2721 PyObject *dict, *key, *value;
2722 int i, j, k;
2724 if ((i = marker(self)) < 0) return -1;
2725 j=self->stack->length;
2727 UNLESS (dict = PyDict_New()) return -1;
2729 for (k = i+1; k < j; k += 2) {
2730 key =self->stack->data[k-1];
2731 value=self->stack->data[k ];
2732 if (PyDict_SetItem(dict, key, value) < 0) {
2733 Py_DECREF(dict);
2734 return -1;
2737 Pdata_clear(self->stack, i);
2738 PDATA_PUSH(self->stack, dict, -1);
2739 return 0;
2742 static PyObject *
2743 Instance_New(PyObject *cls, PyObject *args) {
2744 int has_key;
2745 PyObject *safe=0, *r=0;
2747 if (PyClass_Check(cls)) {
2748 int l;
2750 if ((l=PyObject_Length(args)) < 0) goto err;
2751 UNLESS (l) {
2752 PyObject *__getinitargs__;
2754 UNLESS (__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
2755 /* We have a class with no __getinitargs__, so bypass usual
2756 construction */
2757 PyInstanceObject *inst;
2759 PyErr_Clear();
2760 UNLESS (inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
2761 goto err;
2762 inst->in_class=(PyClassObject*)cls;
2763 Py_INCREF(cls);
2764 UNLESS (inst->in_dict=PyDict_New()) {
2765 Py_DECREF(inst);
2766 goto err;
2769 return (PyObject *)inst;
2771 Py_DECREF(__getinitargs__);
2774 if ((r=PyInstance_New(cls, args, NULL))) return r;
2775 else goto err;
2779 if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
2780 goto err;
2782 if (!has_key)
2783 if (!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
2784 !PyObject_IsTrue(safe)) {
2785 cPickle_ErrFormat(UnpicklingError,
2786 "%s is not safe for unpickling", "O", cls);
2787 Py_XDECREF(safe);
2788 return NULL;
2791 if (args==Py_None) {
2792 /* Special case, call cls.__basicnew__() */
2793 PyObject *basicnew;
2795 UNLESS (basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
2796 r=PyObject_CallObject(basicnew, NULL);
2797 Py_DECREF(basicnew);
2798 if (r) return r;
2801 if ((r=PyObject_CallObject(cls, args))) return r;
2803 err:
2805 PyObject *tp, *v, *tb;
2807 PyErr_Fetch(&tp, &v, &tb);
2808 if ((r=Py_BuildValue("OOO",v,cls,args))) {
2809 Py_XDECREF(v);
2810 v=r;
2812 PyErr_Restore(tp,v,tb);
2814 return NULL;
2818 static int
2819 load_obj(Unpicklerobject *self) {
2820 PyObject *class, *tup, *obj=0;
2821 int i;
2823 if ((i = marker(self)) < 0) return -1;
2824 UNLESS (tup=Pdata_popTuple(self->stack, i+1)) return -1;
2825 PDATA_POP(self->stack, class);
2826 if (class) {
2827 obj = Instance_New(class, tup);
2828 Py_DECREF(class);
2830 Py_DECREF(tup);
2832 if (! obj) return -1;
2833 PDATA_PUSH(self->stack, obj, -1);
2834 return 0;
2838 static int
2839 load_inst(Unpicklerobject *self) {
2840 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
2841 int i, len;
2842 char *s;
2844 if ((i = marker(self)) < 0) return -1;
2846 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2847 if (len < 2) return bad_readline();
2848 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
2850 if ((len = (*self->readline_func)(self, &s)) >= 0) {
2851 if (len < 2) return bad_readline();
2852 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
2853 class = find_class(module_name, class_name, self->find_class);
2854 Py_DECREF(class_name);
2857 Py_DECREF(module_name);
2859 if (! class) return -1;
2861 if ((tup=Pdata_popTuple(self->stack, i))) {
2862 obj = Instance_New(class, tup);
2863 Py_DECREF(tup);
2865 Py_DECREF(class);
2867 if (! obj) return -1;
2869 PDATA_PUSH(self->stack, obj, -1);
2870 return 0;
2874 static int
2875 load_global(Unpicklerobject *self) {
2876 PyObject *class = 0, *module_name = 0, *class_name = 0;
2877 int len;
2878 char *s;
2880 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2881 if (len < 2) return bad_readline();
2882 UNLESS (module_name = PyString_FromStringAndSize(s, len - 1)) return -1;
2884 if ((len = (*self->readline_func)(self, &s)) >= 0) {
2885 if (len < 2) return bad_readline();
2886 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
2887 class = find_class(module_name, class_name, self->find_class);
2888 Py_DECREF(class_name);
2891 Py_DECREF(module_name);
2893 if (! class) return -1;
2894 PDATA_PUSH(self->stack, class, -1);
2895 return 0;
2899 static int
2900 load_persid(Unpicklerobject *self) {
2901 PyObject *pid = 0;
2902 int len;
2903 char *s;
2905 if (self->pers_func) {
2906 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2907 if (len < 2) return bad_readline();
2909 UNLESS (pid = PyString_FromStringAndSize(s, len - 1)) return -1;
2911 if (PyList_Check(self->pers_func)) {
2912 if (PyList_Append(self->pers_func, pid) < 0) {
2913 Py_DECREF(pid);
2914 return -1;
2917 else {
2918 ARG_TUP(self, pid);
2919 if (self->arg) {
2920 pid = PyObject_CallObject(self->pers_func, self->arg);
2921 FREE_ARG_TUP(self);
2925 if (! pid) return -1;
2927 PDATA_PUSH(self->stack, pid, -1);
2928 return 0;
2930 else {
2931 PyErr_SetString(UnpicklingError,
2932 "A load persistent id instruction was encountered,\n"
2933 "but no persistent_load function was specified.");
2934 return -1;
2938 static int
2939 load_binpersid(Unpicklerobject *self) {
2940 PyObject *pid = 0;
2942 if (self->pers_func) {
2943 PDATA_POP(self->stack, pid);
2944 if (! pid) return -1;
2946 if (PyList_Check(self->pers_func)) {
2947 if (PyList_Append(self->pers_func, pid) < 0) {
2948 Py_DECREF(pid);
2949 return -1;
2952 else {
2953 ARG_TUP(self, pid);
2954 if (self->arg) {
2955 pid = PyObject_CallObject(self->pers_func, self->arg);
2956 FREE_ARG_TUP(self);
2958 if (! pid) return -1;
2961 PDATA_PUSH(self->stack, pid, -1);
2962 return 0;
2964 else {
2965 PyErr_SetString(UnpicklingError,
2966 "A load persistent id instruction was encountered,\n"
2967 "but no persistent_load function was specified.");
2968 return -1;
2973 static int
2974 load_pop(Unpicklerobject *self) {
2975 int len;
2977 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
2979 if ((self->num_marks > 0) &&
2980 (self->marks[self->num_marks - 1] == len))
2981 self->num_marks--;
2982 else
2983 Py_DECREF(self->stack->data[--(self->stack->length)]);
2985 return 0;
2989 static int
2990 load_pop_mark(Unpicklerobject *self) {
2991 int i;
2993 if ((i = marker(self)) < 0)
2994 return -1;
2996 Pdata_clear(self->stack, i);
2998 return 0;
3002 static int
3003 load_dup(Unpicklerobject *self) {
3004 PyObject *last;
3005 int len;
3007 if ((len = self->stack->length) <= 0) return stackUnderflow();
3008 last=self->stack->data[len-1];
3009 Py_INCREF(last);
3010 PDATA_PUSH(self->stack, last, -1);
3011 return 0;
3015 static int
3016 load_get(Unpicklerobject *self) {
3017 PyObject *py_str = 0, *value = 0;
3018 int len;
3019 char *s;
3020 int rc;
3022 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
3023 if (len < 2) return bad_readline();
3025 UNLESS (py_str = PyString_FromStringAndSize(s, len - 1)) return -1;
3027 value = PyDict_GetItem(self->memo, py_str);
3028 if (! value) {
3029 PyErr_SetObject(BadPickleGet, py_str);
3030 rc = -1;
3031 } else {
3032 PDATA_APPEND(self->stack, value, -1);
3033 rc = 0;
3036 Py_DECREF(py_str);
3037 return rc;
3041 static int
3042 load_binget(Unpicklerobject *self) {
3043 PyObject *py_key = 0, *value = 0;
3044 unsigned char key;
3045 char *s;
3046 int rc;
3048 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3050 key = (unsigned char)s[0];
3051 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3053 value = PyDict_GetItem(self->memo, py_key);
3054 if (! value) {
3055 PyErr_SetObject(BadPickleGet, py_key);
3056 rc = -1;
3057 } else {
3058 PDATA_APPEND(self->stack, value, -1);
3059 rc = 0;
3062 Py_DECREF(py_key);
3063 return rc;
3067 static int
3068 load_long_binget(Unpicklerobject *self) {
3069 PyObject *py_key = 0, *value = 0;
3070 unsigned char c, *s;
3071 long key;
3072 int rc;
3074 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3076 c = (unsigned char)s[0];
3077 key = (long)c;
3078 c = (unsigned char)s[1];
3079 key |= (long)c << 8;
3080 c = (unsigned char)s[2];
3081 key |= (long)c << 16;
3082 c = (unsigned char)s[3];
3083 key |= (long)c << 24;
3085 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3087 value = PyDict_GetItem(self->memo, py_key);
3088 if (! value) {
3089 PyErr_SetObject(BadPickleGet, py_key);
3090 rc = -1;
3091 } else {
3092 PDATA_APPEND(self->stack, value, -1);
3093 rc = 0;
3096 Py_DECREF(py_key);
3097 return rc;
3101 static int
3102 load_put(Unpicklerobject *self) {
3103 PyObject *py_str = 0, *value = 0;
3104 int len, l;
3105 char *s;
3107 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
3108 if (l < 2) return bad_readline();
3109 UNLESS (len=self->stack->length) return stackUnderflow();
3110 UNLESS (py_str = PyString_FromStringAndSize(s, l - 1)) return -1;
3111 value=self->stack->data[len-1];
3112 l=PyDict_SetItem(self->memo, py_str, value);
3113 Py_DECREF(py_str);
3114 return l;
3118 static int
3119 load_binput(Unpicklerobject *self) {
3120 PyObject *py_key = 0, *value = 0;
3121 unsigned char key, *s;
3122 int len;
3124 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3125 UNLESS ((len=self->stack->length) > 0) return stackUnderflow();
3127 key = (unsigned char)s[0];
3129 UNLESS (py_key = PyInt_FromLong((long)key)) return -1;
3130 value=self->stack->data[len-1];
3131 len=PyDict_SetItem(self->memo, py_key, value);
3132 Py_DECREF(py_key);
3133 return len;
3137 static int
3138 load_long_binput(Unpicklerobject *self) {
3139 PyObject *py_key = 0, *value = 0;
3140 long key;
3141 unsigned char c, *s;
3142 int len;
3144 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3145 UNLESS (len=self->stack->length) return stackUnderflow();
3147 c = (unsigned char)s[0];
3148 key = (long)c;
3149 c = (unsigned char)s[1];
3150 key |= (long)c << 8;
3151 c = (unsigned char)s[2];
3152 key |= (long)c << 16;
3153 c = (unsigned char)s[3];
3154 key |= (long)c << 24;
3156 UNLESS (py_key = PyInt_FromLong(key)) return -1;
3157 value=self->stack->data[len-1];
3158 len=PyDict_SetItem(self->memo, py_key, value);
3159 Py_DECREF(py_key);
3160 return len;
3164 static int
3165 do_append(Unpicklerobject *self, int x) {
3166 PyObject *value = 0, *list = 0, *append_method = 0;
3167 int len, i;
3169 UNLESS ((len=self->stack->length) >= x && x > 0) return stackUnderflow();
3170 if (len==x) return 0; /* nothing to do */
3172 list=self->stack->data[x-1];
3174 if (PyList_Check(list)) {
3175 PyObject *slice;
3176 int list_len;
3178 slice=Pdata_popList(self->stack, x);
3179 list_len = PyList_GET_SIZE(list);
3180 i=PyList_SetSlice(list, list_len, list_len, slice);
3181 Py_DECREF(slice);
3182 return i;
3184 else {
3186 UNLESS (append_method = PyObject_GetAttr(list, append_str))
3187 return -1;
3189 for (i = x; i < len; i++) {
3190 PyObject *junk;
3192 value=self->stack->data[i];
3193 junk=0;
3194 ARG_TUP(self, value);
3195 if (self->arg) {
3196 junk = PyObject_CallObject(append_method, self->arg);
3197 FREE_ARG_TUP(self);
3199 if (! junk) {
3200 Pdata_clear(self->stack, i+1);
3201 self->stack->length=x;
3202 Py_DECREF(append_method);
3203 return -1;
3205 Py_DECREF(junk);
3207 self->stack->length=x;
3208 Py_DECREF(append_method);
3211 return 0;
3215 static int
3216 load_append(Unpicklerobject *self) {
3217 return do_append(self, self->stack->length - 1);
3221 static int
3222 load_appends(Unpicklerobject *self) {
3223 return do_append(self, marker(self));
3227 static int
3228 do_setitems(Unpicklerobject *self, int x) {
3229 PyObject *value = 0, *key = 0, *dict = 0;
3230 int len, i, r=0;
3232 UNLESS ((len=self->stack->length) >= x
3233 && x > 0) return stackUnderflow();
3235 dict=self->stack->data[x-1];
3237 for (i = x+1; i < len; i += 2) {
3238 key =self->stack->data[i-1];
3239 value=self->stack->data[i ];
3240 if (PyObject_SetItem(dict, key, value) < 0) {
3241 r=-1;
3242 break;
3246 Pdata_clear(self->stack, x);
3248 return r;
3252 static int
3253 load_setitem(Unpicklerobject *self) {
3254 return do_setitems(self, self->stack->length - 2);
3257 static int
3258 load_setitems(Unpicklerobject *self) {
3259 return do_setitems(self, marker(self));
3263 static int
3264 load_build(Unpicklerobject *self) {
3265 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3266 *junk = 0, *__setstate__ = 0;
3267 int i, r = 0;
3269 if (self->stack->length < 2) return stackUnderflow();
3270 PDATA_POP(self->stack, value);
3271 if (! value) return -1;
3272 inst=self->stack->data[self->stack->length-1];
3274 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3275 ARG_TUP(self, value);
3276 if (self->arg) {
3277 junk = PyObject_CallObject(__setstate__, self->arg);
3278 FREE_ARG_TUP(self);
3280 Py_DECREF(__setstate__);
3281 if (! junk) return -1;
3282 Py_DECREF(junk);
3283 return 0;
3286 PyErr_Clear();
3287 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
3288 i = 0;
3289 while (PyDict_Next(value, &i, &d_key, &d_value)) {
3290 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3291 r=-1;
3292 break;
3295 Py_DECREF(instdict);
3297 else r=-1;
3299 Py_XDECREF(value);
3301 return r;
3305 static int
3306 load_mark(Unpicklerobject *self) {
3307 int s;
3309 if ((self->num_marks + 1) >= self->marks_size) {
3310 s=self->marks_size+20;
3311 if (s <= self->num_marks) s=self->num_marks + 1;
3312 if (self->marks == NULL)
3313 self->marks=(int *)malloc(s * sizeof(int));
3314 else
3315 self->marks=(int *)realloc(self->marks, s * sizeof(int));
3316 if (! self->marks) {
3317 PyErr_NoMemory();
3318 return -1;
3320 self->marks_size = s;
3323 self->marks[self->num_marks++] = self->stack->length;
3325 return 0;
3328 static int
3329 load_reduce(Unpicklerobject *self) {
3330 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
3332 PDATA_POP(self->stack, arg_tup);
3333 if (! arg_tup) return -1;
3334 PDATA_POP(self->stack, callable);
3335 if (callable) {
3336 ob = Instance_New(callable, arg_tup);
3337 Py_DECREF(callable);
3339 Py_DECREF(arg_tup);
3341 if (! ob) return -1;
3343 PDATA_PUSH(self->stack, ob, -1);
3344 return 0;
3347 static PyObject *
3348 load(Unpicklerobject *self) {
3349 PyObject *err = 0, *val = 0;
3350 char *s;
3352 self->num_marks = 0;
3353 if (self->stack->length) Pdata_clear(self->stack, 0);
3355 while (1) {
3356 if ((*self->read_func)(self, &s, 1) < 0)
3357 break;
3359 switch (s[0]) {
3360 case NONE:
3361 if (load_none(self) < 0)
3362 break;
3363 continue;
3365 case BININT:
3366 if (load_binint(self) < 0)
3367 break;
3368 continue;
3370 case BININT1:
3371 if (load_binint1(self) < 0)
3372 break;
3373 continue;
3375 case BININT2:
3376 if (load_binint2(self) < 0)
3377 break;
3378 continue;
3380 case INT:
3381 if (load_int(self) < 0)
3382 break;
3383 continue;
3385 case LONG:
3386 if (load_long(self) < 0)
3387 break;
3388 continue;
3390 case FLOAT:
3391 if (load_float(self) < 0)
3392 break;
3393 continue;
3395 case BINFLOAT:
3396 if (load_binfloat(self) < 0)
3397 break;
3398 continue;
3400 case BINSTRING:
3401 if (load_binstring(self) < 0)
3402 break;
3403 continue;
3405 case SHORT_BINSTRING:
3406 if (load_short_binstring(self) < 0)
3407 break;
3408 continue;
3410 case STRING:
3411 if (load_string(self) < 0)
3412 break;
3413 continue;
3415 case EMPTY_TUPLE:
3416 if (load_empty_tuple(self) < 0)
3417 break;
3418 continue;
3420 case TUPLE:
3421 if (load_tuple(self) < 0)
3422 break;
3423 continue;
3425 case EMPTY_LIST:
3426 if (load_empty_list(self) < 0)
3427 break;
3428 continue;
3430 case LIST:
3431 if (load_list(self) < 0)
3432 break;
3433 continue;
3435 case EMPTY_DICT:
3436 if (load_empty_dict(self) < 0)
3437 break;
3438 continue;
3440 case DICT:
3441 if (load_dict(self) < 0)
3442 break;
3443 continue;
3445 case OBJ:
3446 if (load_obj(self) < 0)
3447 break;
3448 continue;
3450 case INST:
3451 if (load_inst(self) < 0)
3452 break;
3453 continue;
3455 case GLOBAL:
3456 if (load_global(self) < 0)
3457 break;
3458 continue;
3460 case APPEND:
3461 if (load_append(self) < 0)
3462 break;
3463 continue;
3465 case APPENDS:
3466 if (load_appends(self) < 0)
3467 break;
3468 continue;
3470 case BUILD:
3471 if (load_build(self) < 0)
3472 break;
3473 continue;
3475 case DUP:
3476 if (load_dup(self) < 0)
3477 break;
3478 continue;
3480 case BINGET:
3481 if (load_binget(self) < 0)
3482 break;
3483 continue;
3485 case LONG_BINGET:
3486 if (load_long_binget(self) < 0)
3487 break;
3488 continue;
3490 case GET:
3491 if (load_get(self) < 0)
3492 break;
3493 continue;
3495 case MARK:
3496 if (load_mark(self) < 0)
3497 break;
3498 continue;
3500 case BINPUT:
3501 if (load_binput(self) < 0)
3502 break;
3503 continue;
3505 case LONG_BINPUT:
3506 if (load_long_binput(self) < 0)
3507 break;
3508 continue;
3510 case PUT:
3511 if (load_put(self) < 0)
3512 break;
3513 continue;
3515 case POP:
3516 if (load_pop(self) < 0)
3517 break;
3518 continue;
3520 case POP_MARK:
3521 if (load_pop_mark(self) < 0)
3522 break;
3523 continue;
3525 case SETITEM:
3526 if (load_setitem(self) < 0)
3527 break;
3528 continue;
3530 case SETITEMS:
3531 if (load_setitems(self) < 0)
3532 break;
3533 continue;
3535 case STOP:
3536 break;
3538 case PERSID:
3539 if (load_persid(self) < 0)
3540 break;
3541 continue;
3543 case BINPERSID:
3544 if (load_binpersid(self) < 0)
3545 break;
3546 continue;
3548 case REDUCE:
3549 if (load_reduce(self) < 0)
3550 break;
3551 continue;
3553 default:
3554 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
3555 "c", s[0]);
3556 return NULL;
3559 break;
3562 if ((err = PyErr_Occurred())) {
3563 if (err == PyExc_EOFError) {
3564 PyErr_SetNone(PyExc_EOFError);
3566 return NULL;
3569 PDATA_POP(self->stack, val);
3570 return val;
3574 /* No-load functions to support noload, which is used to
3575 find persistent references. */
3577 static int
3578 noload_obj(Unpicklerobject *self) {
3579 int i;
3581 if ((i = marker(self)) < 0) return -1;
3582 return Pdata_clear(self->stack, i+1);
3586 static int
3587 noload_inst(Unpicklerobject *self) {
3588 int i;
3589 char *s;
3591 if ((i = marker(self)) < 0) return -1;
3592 Pdata_clear(self->stack, i);
3593 if ((*self->readline_func)(self, &s) < 0) return -1;
3594 if ((*self->readline_func)(self, &s) < 0) return -1;
3595 PDATA_APPEND(self->stack, Py_None,-1);
3596 return 0;
3599 static int
3600 noload_global(Unpicklerobject *self) {
3601 char *s;
3603 if ((*self->readline_func)(self, &s) < 0) return -1;
3604 if ((*self->readline_func)(self, &s) < 0) return -1;
3605 PDATA_APPEND(self->stack, Py_None,-1);
3606 return 0;
3609 static int
3610 noload_reduce(Unpicklerobject *self) {
3612 if (self->stack->length < 2) return stackUnderflow();
3613 Pdata_clear(self->stack, self->stack->length-2);
3614 PDATA_APPEND(self->stack, Py_None,-1);
3615 return 0;
3618 static int
3619 noload_build(Unpicklerobject *self) {
3621 if (self->stack->length < 1) return stackUnderflow();
3622 Pdata_clear(self->stack, self->stack->length-1);
3623 return 0;
3627 static PyObject *
3628 noload(Unpicklerobject *self) {
3629 PyObject *err = 0, *val = 0;
3630 char *s;
3632 self->num_marks = 0;
3633 Pdata_clear(self->stack, 0);
3635 while (1) {
3636 if ((*self->read_func)(self, &s, 1) < 0)
3637 break;
3639 switch (s[0]) {
3640 case NONE:
3641 if (load_none(self) < 0)
3642 break;
3643 continue;
3645 case BININT:
3646 if (load_binint(self) < 0)
3647 break;
3648 continue;
3650 case BININT1:
3651 if (load_binint1(self) < 0)
3652 break;
3653 continue;
3655 case BININT2:
3656 if (load_binint2(self) < 0)
3657 break;
3658 continue;
3660 case INT:
3661 if (load_int(self) < 0)
3662 break;
3663 continue;
3665 case LONG:
3666 if (load_long(self) < 0)
3667 break;
3668 continue;
3670 case FLOAT:
3671 if (load_float(self) < 0)
3672 break;
3673 continue;
3675 case BINFLOAT:
3676 if (load_binfloat(self) < 0)
3677 break;
3678 continue;
3680 case BINSTRING:
3681 if (load_binstring(self) < 0)
3682 break;
3683 continue;
3685 case SHORT_BINSTRING:
3686 if (load_short_binstring(self) < 0)
3687 break;
3688 continue;
3690 case STRING:
3691 if (load_string(self) < 0)
3692 break;
3693 continue;
3695 case EMPTY_TUPLE:
3696 if (load_empty_tuple(self) < 0)
3697 break;
3698 continue;
3700 case TUPLE:
3701 if (load_tuple(self) < 0)
3702 break;
3703 continue;
3705 case EMPTY_LIST:
3706 if (load_empty_list(self) < 0)
3707 break;
3708 continue;
3710 case LIST:
3711 if (load_list(self) < 0)
3712 break;
3713 continue;
3715 case EMPTY_DICT:
3716 if (load_empty_dict(self) < 0)
3717 break;
3718 continue;
3720 case DICT:
3721 if (load_dict(self) < 0)
3722 break;
3723 continue;
3725 case OBJ:
3726 if (noload_obj(self) < 0)
3727 break;
3728 continue;
3730 case INST:
3731 if (noload_inst(self) < 0)
3732 break;
3733 continue;
3735 case GLOBAL:
3736 if (noload_global(self) < 0)
3737 break;
3738 continue;
3740 case APPEND:
3741 if (load_append(self) < 0)
3742 break;
3743 continue;
3745 case APPENDS:
3746 if (load_appends(self) < 0)
3747 break;
3748 continue;
3750 case BUILD:
3751 if (noload_build(self) < 0)
3752 break;
3753 continue;
3755 case DUP:
3756 if (load_dup(self) < 0)
3757 break;
3758 continue;
3760 case BINGET:
3761 if (load_binget(self) < 0)
3762 break;
3763 continue;
3765 case LONG_BINGET:
3766 if (load_long_binget(self) < 0)
3767 break;
3768 continue;
3770 case GET:
3771 if (load_get(self) < 0)
3772 break;
3773 continue;
3775 case MARK:
3776 if (load_mark(self) < 0)
3777 break;
3778 continue;
3780 case BINPUT:
3781 if (load_binput(self) < 0)
3782 break;
3783 continue;
3785 case LONG_BINPUT:
3786 if (load_long_binput(self) < 0)
3787 break;
3788 continue;
3790 case PUT:
3791 if (load_put(self) < 0)
3792 break;
3793 continue;
3795 case POP:
3796 if (load_pop(self) < 0)
3797 break;
3798 continue;
3800 case POP_MARK:
3801 if (load_pop_mark(self) < 0)
3802 break;
3803 continue;
3805 case SETITEM:
3806 if (load_setitem(self) < 0)
3807 break;
3808 continue;
3810 case SETITEMS:
3811 if (load_setitems(self) < 0)
3812 break;
3813 continue;
3815 case STOP:
3816 break;
3818 case PERSID:
3819 if (load_persid(self) < 0)
3820 break;
3821 continue;
3823 case BINPERSID:
3824 if (load_binpersid(self) < 0)
3825 break;
3826 continue;
3828 case REDUCE:
3829 if (noload_reduce(self) < 0)
3830 break;
3831 continue;
3833 default:
3834 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.",
3835 "c", s[0]);
3836 return NULL;
3839 break;
3842 if ((err = PyErr_Occurred())) {
3843 if (err == PyExc_EOFError) {
3844 PyErr_SetNone(PyExc_EOFError);
3846 return NULL;
3849 PDATA_POP(self->stack, val);
3850 return val;
3854 static PyObject *
3855 Unpickler_load(Unpicklerobject *self, PyObject *args) {
3856 UNLESS (PyArg_ParseTuple(args, ""))
3857 return NULL;
3859 return load(self);
3862 static PyObject *
3863 Unpickler_noload(Unpicklerobject *self, PyObject *args) {
3864 UNLESS (PyArg_ParseTuple(args, ""))
3865 return NULL;
3867 return noload(self);
3871 static struct PyMethodDef Unpickler_methods[] = {
3872 {"load", (PyCFunction)Unpickler_load, 1,
3873 "load() -- Load a pickle"
3875 {"noload", (PyCFunction)Unpickler_noload, 1,
3876 "noload() -- not load a pickle, but go through most of the motions\n"
3877 "\n"
3878 "This function can be used to read past a pickle without instantiating\n"
3879 "any objects or importing any modules. It can also be used to find all\n"
3880 "persistent references without instantiating any objects or importing\n"
3881 "any modules.\n"
3883 {NULL, NULL} /* sentinel */
3887 static Unpicklerobject *
3888 newUnpicklerobject(PyObject *f) {
3889 Unpicklerobject *self;
3891 UNLESS (self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
3892 return NULL;
3894 self->file = NULL;
3895 self->arg = NULL;
3896 self->stack = (Pdata*)Pdata_New();
3897 self->pers_func = NULL;
3898 self->last_string = NULL;
3899 self->marks = NULL;
3900 self->num_marks = 0;
3901 self->marks_size = 0;
3902 self->buf_size = 0;
3903 self->read = NULL;
3904 self->readline = NULL;
3905 self->safe_constructors = NULL;
3906 self->find_class = NULL;
3908 UNLESS (self->memo = PyDict_New()) {
3909 Py_XDECREF((PyObject *)self);
3910 return NULL;
3913 Py_INCREF(f);
3914 self->file = f;
3916 /* Set read, readline based on type of f */
3917 if (PyFile_Check(f)) {
3918 self->fp = PyFile_AsFile(f);
3919 if (self->fp == NULL) {
3920 PyErr_SetString(PyExc_IOError, "input file closed");
3921 return NULL;
3923 self->read_func = read_file;
3924 self->readline_func = readline_file;
3926 else if (PycStringIO_InputCheck(f)) {
3927 self->fp = NULL;
3928 self->read_func = read_cStringIO;
3929 self->readline_func = readline_cStringIO;
3931 else {
3933 self->fp = NULL;
3934 self->read_func = read_other;
3935 self->readline_func = readline_other;
3937 UNLESS ((self->readline = PyObject_GetAttr(f, readline_str)) &&
3938 (self->read = PyObject_GetAttr(f, read_str))) {
3939 PyErr_Clear();
3940 PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
3941 "'readline' attributes" );
3942 goto err;
3946 if (PyEval_GetRestricted()) {
3947 /* Restricted execution, get private tables */
3948 PyObject *m;
3950 UNLESS (m=PyImport_Import(copy_reg_str)) goto err;
3951 self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
3952 Py_DECREF(m);
3953 UNLESS (self->safe_constructors) goto err;
3955 else {
3956 self->safe_constructors=safe_constructors;
3957 Py_INCREF(safe_constructors);
3960 return self;
3962 err:
3963 Py_DECREF((PyObject *)self);
3964 return NULL;
3968 static PyObject *
3969 get_Unpickler(PyObject *self, PyObject *args) {
3970 PyObject *file;
3972 UNLESS (PyArg_ParseTuple(args, "O", &file))
3973 return NULL;
3974 return (PyObject *)newUnpicklerobject(file);
3978 static void
3979 Unpickler_dealloc(Unpicklerobject *self) {
3980 Py_XDECREF(self->readline);
3981 Py_XDECREF(self->read);
3982 Py_XDECREF(self->file);
3983 Py_XDECREF(self->memo);
3984 Py_XDECREF(self->stack);
3985 Py_XDECREF(self->pers_func);
3986 Py_XDECREF(self->arg);
3987 Py_XDECREF(self->last_string);
3988 Py_XDECREF(self->safe_constructors);
3990 if (self->marks) {
3991 free(self->marks);
3994 if (self->buf_size) {
3995 free(self->buf);
3998 PyMem_DEL(self);
4002 static PyObject *
4003 Unpickler_getattr(Unpicklerobject *self, char *name) {
4004 if (!strcmp(name, "persistent_load")) {
4005 if (!self->pers_func) {
4006 PyErr_SetString(PyExc_AttributeError, name);
4007 return NULL;
4010 Py_INCREF(self->pers_func);
4011 return self->pers_func;
4014 if (!strcmp(name, "find_global")) {
4015 if (!self->find_class) {
4016 PyErr_SetString(PyExc_AttributeError, name);
4017 return NULL;
4020 Py_INCREF(self->find_class);
4021 return self->find_class;
4024 if (!strcmp(name, "memo")) {
4025 if (!self->memo) {
4026 PyErr_SetString(PyExc_AttributeError, name);
4027 return NULL;
4030 Py_INCREF(self->memo);
4031 return self->memo;
4034 if (!strcmp(name, "UnpicklingError")) {
4035 Py_INCREF(UnpicklingError);
4036 return UnpicklingError;
4039 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4043 static int
4044 Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
4046 if (!strcmp(name, "persistent_load")) {
4047 Py_XDECREF(self->pers_func);
4048 self->pers_func = value;
4049 Py_XINCREF(value);
4050 return 0;
4053 if (!strcmp(name, "find_global")) {
4054 Py_XDECREF(self->find_class);
4055 self->find_class = value;
4056 Py_XINCREF(value);
4057 return 0;
4060 if (! value) {
4061 PyErr_SetString(PyExc_TypeError,
4062 "attribute deletion is not supported");
4063 return -1;
4066 if (strcmp(name, "memo") == 0) {
4067 if (! PyDict_Check(value)) {
4068 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
4069 return -1;
4071 Py_XDECREF(self->memo);
4072 self->memo = value;
4073 Py_INCREF(value);
4074 return 0;
4077 PyErr_SetString(PyExc_AttributeError, name);
4078 return -1;
4082 static PyObject *
4083 cpm_dump(PyObject *self, PyObject *args) {
4084 PyObject *ob, *file, *res = NULL;
4085 Picklerobject *pickler = 0;
4086 int bin = 0;
4088 UNLESS (PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
4089 goto finally;
4091 UNLESS (pickler = newPicklerobject(file, bin))
4092 goto finally;
4094 if (dump(pickler, ob) < 0)
4095 goto finally;
4097 Py_INCREF(Py_None);
4098 res = Py_None;
4100 finally:
4101 Py_XDECREF(pickler);
4103 return res;
4107 static PyObject *
4108 cpm_dumps(PyObject *self, PyObject *args) {
4109 PyObject *ob, *file = 0, *res = NULL;
4110 Picklerobject *pickler = 0;
4111 int bin = 0;
4113 UNLESS (PyArg_ParseTuple(args, "O|i", &ob, &bin))
4114 goto finally;
4116 UNLESS (file = PycStringIO->NewOutput(128))
4117 goto finally;
4119 UNLESS (pickler = newPicklerobject(file, bin))
4120 goto finally;
4122 if (dump(pickler, ob) < 0)
4123 goto finally;
4125 res = PycStringIO->cgetvalue(file);
4127 finally:
4128 Py_XDECREF(pickler);
4129 Py_XDECREF(file);
4131 return res;
4135 static PyObject *
4136 cpm_load(PyObject *self, PyObject *args) {
4137 Unpicklerobject *unpickler = 0;
4138 PyObject *ob, *res = NULL;
4140 UNLESS (PyArg_ParseTuple(args, "O", &ob))
4141 goto finally;
4143 UNLESS (unpickler = newUnpicklerobject(ob))
4144 goto finally;
4146 res = load(unpickler);
4148 finally:
4149 Py_XDECREF(unpickler);
4151 return res;
4155 static PyObject *
4156 cpm_loads(PyObject *self, PyObject *args) {
4157 PyObject *ob, *file = 0, *res = NULL;
4158 Unpicklerobject *unpickler = 0;
4160 UNLESS (PyArg_ParseTuple(args, "S", &ob))
4161 goto finally;
4163 UNLESS (file = PycStringIO->NewInput(ob))
4164 goto finally;
4166 UNLESS (unpickler = newUnpicklerobject(file))
4167 goto finally;
4169 res = load(unpickler);
4171 finally:
4172 Py_XDECREF(file);
4173 Py_XDECREF(unpickler);
4175 return res;
4179 static char Unpicklertype__doc__[] =
4180 "Objects that know how to unpickle";
4182 static PyTypeObject Unpicklertype = {
4183 PyObject_HEAD_INIT(NULL)
4184 0, /*ob_size*/
4185 "Unpickler", /*tp_name*/
4186 sizeof(Unpicklerobject), /*tp_basicsize*/
4187 0, /*tp_itemsize*/
4188 /* methods */
4189 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4190 (printfunc)0, /*tp_print*/
4191 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4192 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4193 (cmpfunc)0, /*tp_compare*/
4194 (reprfunc)0, /*tp_repr*/
4195 0, /*tp_as_number*/
4196 0, /*tp_as_sequence*/
4197 0, /*tp_as_mapping*/
4198 (hashfunc)0, /*tp_hash*/
4199 (ternaryfunc)0, /*tp_call*/
4200 (reprfunc)0, /*tp_str*/
4202 /* Space for future expansion */
4203 0L,0L,0L,0L,
4204 Unpicklertype__doc__ /* Documentation string */
4207 static struct PyMethodDef cPickle_methods[] = {
4208 {"dump", (PyCFunction)cpm_dump, 1,
4209 "dump(object, file, [binary]) --"
4210 "Write an object in pickle format to the given file\n"
4211 "\n"
4212 "If the optional argument, binary, is provided and is true, then the\n"
4213 "pickle will be written in binary format, which is more space and\n"
4214 "computationally efficient. \n"
4216 {"dumps", (PyCFunction)cpm_dumps, 1,
4217 "dumps(object, [binary]) --"
4218 "Return a string containing an object in pickle format\n"
4219 "\n"
4220 "If the optional argument, binary, is provided and is true, then the\n"
4221 "pickle will be written in binary format, which is more space and\n"
4222 "computationally efficient. \n"
4224 {"load", (PyCFunction)cpm_load, 1,
4225 "load(file) -- Load a pickle from the given file"},
4226 {"loads", (PyCFunction)cpm_loads, 1,
4227 "loads(string) -- Load a pickle from the given string"},
4228 {"Pickler", (PyCFunction)get_Pickler, 1,
4229 "Pickler(file, [binary]) -- Create a pickler\n"
4230 "\n"
4231 "If the optional argument, binary, is provided and is true, then\n"
4232 "pickles will be written in binary format, which is more space and\n"
4233 "computationally efficient. \n"
4235 {"Unpickler", (PyCFunction)get_Unpickler, 1,
4236 "Unpickler(file) -- Create an unpickler"},
4237 { NULL, NULL }
4240 static int
4241 init_stuff(PyObject *module, PyObject *module_dict) {
4242 PyObject *string, *copy_reg, *t, *r;
4244 #define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4246 INIT_STR(__class__);
4247 INIT_STR(__getinitargs__);
4248 INIT_STR(__dict__);
4249 INIT_STR(__getstate__);
4250 INIT_STR(__setstate__);
4251 INIT_STR(__name__);
4252 INIT_STR(__main__);
4253 INIT_STR(__reduce__);
4254 INIT_STR(write);
4255 INIT_STR(__safe_for_unpickling__);
4256 INIT_STR(append);
4257 INIT_STR(read);
4258 INIT_STR(readline);
4259 INIT_STR(copy_reg);
4260 INIT_STR(dispatch_table);
4261 INIT_STR(safe_constructors);
4262 INIT_STR(__basicnew__);
4263 UNLESS (empty_str=PyString_FromString("")) return -1;
4265 UNLESS (copy_reg = PyImport_ImportModule("copy_reg"))
4266 return -1;
4268 /* These next few are special because we want to use different
4269 ones in restricted mode. */
4271 UNLESS (dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
4272 return -1;
4274 UNLESS (safe_constructors = PyObject_GetAttr(copy_reg,
4275 safe_constructors_str))
4276 return -1;
4278 Py_DECREF(copy_reg);
4280 /* Down to here ********************************** */
4282 UNLESS (string = PyImport_ImportModule("string"))
4283 return -1;
4285 UNLESS (atol_func = PyObject_GetAttrString(string, "atol"))
4286 return -1;
4288 Py_DECREF(string);
4290 UNLESS (empty_tuple = PyTuple_New(0))
4291 return -1;
4293 /* Ugh */
4294 UNLESS (t=PyImport_ImportModule("__builtin__")) return -1;
4295 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4296 return -1;
4298 UNLESS (t=PyDict_New()) return -1;
4299 UNLESS (r=PyRun_String(
4300 "def __init__(self, *args): self.args=args\n\n"
4301 "def __str__(self):\n"
4302 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4303 Py_file_input,
4304 module_dict, t) ) return -1;
4305 Py_DECREF(r);
4307 UNLESS (PickleError = PyErr_NewException("cPickle.PickleError", NULL, t))
4308 return -1;
4310 Py_DECREF(t);
4313 UNLESS (PicklingError = PyErr_NewException("cPickle.PicklingError",
4314 PickleError, NULL))
4315 return -1;
4317 UNLESS (t=PyDict_New()) return -1;
4318 UNLESS (r=PyRun_String(
4319 "def __init__(self, *args): self.args=args\n\n"
4320 "def __str__(self):\n"
4321 " a=self.args\n"
4322 " a=a and type(a[0]) or '(what)'\n"
4323 " return 'Cannot pickle %s objects' % a\n"
4324 , Py_file_input,
4325 module_dict, t) ) return -1;
4326 Py_DECREF(r);
4328 UNLESS (UnpickleableError = PyErr_NewException(
4329 "cPickle.UnpickleableError", PicklingError, t))
4330 return -1;
4332 Py_DECREF(t);
4334 UNLESS (UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
4335 PickleError, NULL))
4336 return -1;
4338 if (PyDict_SetItemString(module_dict, "PickleError",
4339 PickleError) < 0)
4340 return -1;
4342 if (PyDict_SetItemString(module_dict, "PicklingError",
4343 PicklingError) < 0)
4344 return -1;
4346 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4347 UnpicklingError) < 0)
4348 return -1;
4350 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4351 UnpickleableError) < 0)
4352 return -1;
4354 UNLESS (BadPickleGet = PyString_FromString("cPickle.BadPickleGet"))
4355 return -1;
4357 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4358 BadPickleGet) < 0)
4359 return -1;
4361 PycString_IMPORT;
4363 return 0;
4366 #ifndef DL_EXPORT /* declarations for DLL import/export */
4367 #define DL_EXPORT(RTYPE) RTYPE
4368 #endif
4369 DL_EXPORT(void)
4370 initcPickle() {
4371 PyObject *m, *d, *v;
4372 char *rev="1.71";
4373 PyObject *format_version;
4374 PyObject *compatible_formats;
4376 Picklertype.ob_type = &PyType_Type;
4377 Unpicklertype.ob_type = &PyType_Type;
4378 PdataType.ob_type = &PyType_Type;
4380 /* Create the module and add the functions */
4381 m = Py_InitModule4("cPickle", cPickle_methods,
4382 cPickle_module_documentation,
4383 (PyObject*)NULL,PYTHON_API_VERSION);
4385 /* Add some symbolic constants to the module */
4386 d = PyModule_GetDict(m);
4387 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
4388 Py_XDECREF(v);
4390 format_version = PyString_FromString("1.3");
4391 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
4393 PyDict_SetItemString(d, "format_version", format_version);
4394 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
4395 Py_XDECREF(format_version);
4396 Py_XDECREF(compatible_formats);
4398 init_stuff(m, d);