append(): Fixing the test for convertability after consultation with
[python/dscho.git] / Modules / cPickle.c
blob32fdd6ed76860abce93ae9cdc9aae9fb2869d254
1 #include "Python.h"
2 #include "cStringIO.h"
3 #include "structmember.h"
5 PyDoc_STRVAR(cPickle_module_documentation,
6 "C implementation and optimization of the Python pickle module\n"
7 "\n"
8 "cPickle.c,v 1.71 1999/07/11 13:30:34 jim Exp\n");
10 #ifndef Py_eval_input
11 #include <graminit.h>
12 #define Py_eval_input eval_input
13 #endif /* Py_eval_input */
15 #include <errno.h>
19 #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
21 #define WRITE_BUF_SIZE 256
23 /* --------------------------------------------------------------------------
24 NOTES on format codes.
25 XXX much more is needed here
27 Integer types
28 BININT1 8-bit unsigned integer; followed by 1 byte.
29 BININT2 16-bit unsigned integer; followed by 2 bytes, little-endian.
30 BININT 32-bit signed integer; followed by 4 bytes, little-endian.
31 INT Integer; natural decimal string conversion, then newline.
32 CAUTION: INT-reading code can't assume that what follows
33 fits in a Python int, because the size of Python ints varies
34 across platforms.
35 LONG Long (unbounded) integer; repr(i), then newline.
36 -------------------------------------------------------------------------- */
38 #define MARK '('
39 #define STOP '.'
40 #define POP '0'
41 #define POP_MARK '1'
42 #define DUP '2'
43 #define FLOAT 'F'
44 #define BINFLOAT 'G'
45 #define INT 'I'
46 #define BININT 'J'
47 #define BININT1 'K'
48 #define LONG 'L'
49 #define BININT2 'M'
50 #define NONE 'N'
51 #define PERSID 'P'
52 #define BINPERSID 'Q'
53 #define REDUCE 'R'
54 #define STRING 'S'
55 #define BINSTRING 'T'
56 #define SHORT_BINSTRING 'U'
57 #define UNICODE 'V'
58 #define BINUNICODE 'X'
59 #define APPEND 'a'
60 #define BUILD 'b'
61 #define GLOBAL 'c'
62 #define DICT 'd'
63 #define EMPTY_DICT '}'
64 #define APPENDS 'e'
65 #define GET 'g'
66 #define BINGET 'h'
67 #define INST 'i'
68 #define LONG_BINGET 'j'
69 #define LIST 'l'
70 #define EMPTY_LIST ']'
71 #define OBJ 'o'
72 #define PUT 'p'
73 #define BINPUT 'q'
74 #define LONG_BINPUT 'r'
75 #define SETITEM 's'
76 #define TUPLE 't'
77 #define EMPTY_TUPLE ')'
78 #define SETITEMS 'u'
79 #undef TRUE
80 #define TRUE "I01\n"
81 #undef FALSE
82 #define FALSE "I00\n"
85 static char MARKv = MARK;
87 static PyObject *PickleError;
88 static PyObject *PicklingError;
89 static PyObject *UnpickleableError;
90 static PyObject *UnpicklingError;
91 static PyObject *BadPickleGet;
94 static PyObject *dispatch_table;
95 static PyObject *safe_constructors;
96 static PyObject *empty_tuple;
98 static PyObject *__class___str, *__getinitargs___str, *__dict___str,
99 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
100 *write_str, *__safe_for_unpickling___str, *append_str,
101 *read_str, *readline_str, *__main___str, *__basicnew___str,
102 *copy_reg_str, *dispatch_table_str, *safe_constructors_str;
104 /*************************************************************************
105 Internal Data type for pickle data. */
107 typedef struct {
108 PyObject_HEAD
109 int length, size;
110 PyObject **data;
111 } Pdata;
113 static void
114 Pdata_dealloc(Pdata *self)
116 int i;
117 PyObject **p;
119 for (i=self->length, p=self->data; --i >= 0; p++) Py_DECREF(*p);
121 if (self->data) free(self->data);
123 PyObject_Del(self);
126 static PyTypeObject PdataType = {
127 PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0,
128 (destructor)Pdata_dealloc,
129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
132 #define Pdata_Check(O) ((O)->ob_type == &PdataType)
134 static PyObject *
135 Pdata_New(void)
137 Pdata *self;
139 if (!( self = PyObject_New(Pdata, &PdataType))) return NULL;
140 self->size=8;
141 self->length=0;
142 self->data=malloc(self->size * sizeof(PyObject*));
143 if (self->data) return (PyObject*)self;
144 Py_DECREF(self);
145 return PyErr_NoMemory();
148 static int
149 stackUnderflow(void)
151 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
152 return -1;
155 static int
156 Pdata_clear(Pdata *self, int clearto)
158 int i;
159 PyObject **p;
161 if (clearto < 0) return stackUnderflow();
162 if (clearto >= self->length) return 0;
164 for (i=self->length, p=self->data+clearto; --i >= clearto; p++)
165 Py_DECREF(*p);
166 self->length=clearto;
168 return 0;
172 static int
173 Pdata_grow(Pdata *self)
175 if (! self->size) {
176 PyErr_NoMemory();
177 return -1;
179 self->size *= 2;
180 self->data = realloc(self->data, self->size*sizeof(PyObject*));
181 if (! self->data) {
182 self->size = 0;
183 PyErr_NoMemory();
184 return -1;
186 return 0;
189 #define PDATA_POP(D,V) { \
190 if ((D)->length) V=D->data[--((D)->length)]; \
191 else { \
192 PyErr_SetString(UnpicklingError, "bad pickle data"); \
193 V=NULL; \
198 static PyObject *
199 Pdata_popTuple(Pdata *self, int start)
201 PyObject *r;
202 int i, j, l;
204 l=self->length-start;
205 if (!( r=PyTuple_New(l))) return NULL;
206 for (i=start, j=0 ; j < l; i++, j++)
207 PyTuple_SET_ITEM(r, j, self->data[i]);
209 self->length=start;
210 return r;
213 static PyObject *
214 Pdata_popList(Pdata *self, int start)
216 PyObject *r;
217 int i, j, l;
219 l=self->length-start;
220 if (!( r=PyList_New(l))) return NULL;
221 for (i=start, j=0 ; j < l; i++, j++)
222 PyList_SET_ITEM(r, j, self->data[i]);
224 self->length=start;
225 return r;
228 #define PDATA_APPEND_(D,O,ER) { \
229 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
232 #define PDATA_APPEND(D,O,ER) { \
233 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
234 Pdata_grow((Pdata*)(D)) < 0) \
235 return ER; \
236 Py_INCREF(O); \
237 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
240 #define PDATA_PUSH(D,O,ER) { \
241 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
242 Pdata_grow((Pdata*)(D)) < 0) { \
243 Py_DECREF(O); \
244 return ER; \
246 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
249 /*************************************************************************/
251 #define ARG_TUP(self, o) { \
252 if (self->arg || (self->arg=PyTuple_New(1))) { \
253 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
254 PyTuple_SET_ITEM(self->arg,0,o); \
256 else { \
257 Py_DECREF(o); \
261 #define FREE_ARG_TUP(self) { \
262 if (self->arg->ob_refcnt > 1) { \
263 Py_DECREF(self->arg); \
264 self->arg=NULL; \
268 typedef struct Picklerobject {
269 PyObject_HEAD
270 FILE *fp;
271 PyObject *write;
272 PyObject *file;
273 PyObject *memo;
274 PyObject *arg;
275 PyObject *pers_func;
276 PyObject *inst_pers_func;
277 int bin;
278 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
279 int nesting;
280 int (*write_func)(struct Picklerobject *, char *, int);
281 char *write_buf;
282 int buf_size;
283 PyObject *dispatch_table;
284 int fast_container; /* count nested container dumps */
285 PyObject *fast_memo;
286 } Picklerobject;
288 #ifndef PY_CPICKLE_FAST_LIMIT
289 #define PY_CPICKLE_FAST_LIMIT 50
290 #endif
292 static PyTypeObject Picklertype;
294 typedef struct Unpicklerobject {
295 PyObject_HEAD
296 FILE *fp;
297 PyObject *file;
298 PyObject *readline;
299 PyObject *read;
300 PyObject *memo;
301 PyObject *arg;
302 Pdata *stack;
303 PyObject *mark;
304 PyObject *pers_func;
305 PyObject *last_string;
306 int *marks;
307 int num_marks;
308 int marks_size;
309 int (*read_func)(struct Unpicklerobject *, char **, int);
310 int (*readline_func)(struct Unpicklerobject *, char **);
311 int buf_size;
312 char *buf;
313 PyObject *safe_constructors;
314 PyObject *find_class;
315 } Unpicklerobject;
317 static PyTypeObject Unpicklertype;
319 /* Forward decls that need the above structs */
320 static int save(Picklerobject *, PyObject *, int);
321 static int put2(Picklerobject *, PyObject *);
324 cPickle_PyMapping_HasKey(PyObject *o, PyObject *key)
326 PyObject *v;
328 if ((v = PyObject_GetItem(o,key))) {
329 Py_DECREF(v);
330 return 1;
333 PyErr_Clear();
334 return 0;
337 static
338 PyObject *
339 cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
341 va_list va;
342 PyObject *args=0, *retval=0;
343 va_start(va, format);
345 if (format) args = Py_VaBuildValue(format, va);
346 va_end(va);
347 if (format && ! args) return NULL;
348 if (stringformat && !(retval=PyString_FromString(stringformat)))
349 return NULL;
351 if (retval) {
352 if (args) {
353 PyObject *v;
354 v=PyString_Format(retval, args);
355 Py_DECREF(retval);
356 Py_DECREF(args);
357 if (! v) return NULL;
358 retval=v;
361 else
362 if (args) retval=args;
363 else {
364 PyErr_SetObject(ErrType,Py_None);
365 return NULL;
367 PyErr_SetObject(ErrType,retval);
368 Py_DECREF(retval);
369 return NULL;
372 static int
373 write_file(Picklerobject *self, char *s, int n)
375 size_t nbyteswritten;
377 if (s == NULL) {
378 return 0;
381 Py_BEGIN_ALLOW_THREADS
382 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
383 Py_END_ALLOW_THREADS
384 if (nbyteswritten != (size_t)n) {
385 PyErr_SetFromErrno(PyExc_IOError);
386 return -1;
389 return n;
392 static int
393 write_cStringIO(Picklerobject *self, char *s, int n)
395 if (s == NULL) {
396 return 0;
399 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
400 return -1;
403 return n;
406 static int
407 write_none(Picklerobject *self, char *s, int n)
409 if (s == NULL) return 0;
410 return n;
413 static int
414 write_other(Picklerobject *self, char *s, int n)
416 PyObject *py_str = 0, *junk = 0;
418 if (s == NULL) {
419 if (!( self->buf_size )) return 0;
420 py_str = PyString_FromStringAndSize(self->write_buf,
421 self->buf_size);
422 if (!py_str)
423 return -1;
425 else {
426 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
427 if (write_other(self, NULL, 0) < 0)
428 return -1;
431 if (n > WRITE_BUF_SIZE) {
432 if (!( py_str =
433 PyString_FromStringAndSize(s, n)))
434 return -1;
436 else {
437 memcpy(self->write_buf + self->buf_size, s, n);
438 self->buf_size += n;
439 return n;
443 if (self->write) {
444 /* object with write method */
445 ARG_TUP(self, py_str);
446 if (self->arg) {
447 junk = PyObject_Call(self->write, self->arg, NULL);
448 FREE_ARG_TUP(self);
450 if (junk) Py_DECREF(junk);
451 else return -1;
453 else
454 PDATA_PUSH(self->file, py_str, -1);
456 self->buf_size = 0;
457 return n;
461 static int
462 read_file(Unpicklerobject *self, char **s, int n)
464 size_t nbytesread;
466 if (self->buf_size == 0) {
467 int size;
469 size = ((n < 32) ? 32 : n);
470 if (!( self->buf = (char *)malloc(size * sizeof(char)))) {
471 PyErr_NoMemory();
472 return -1;
475 self->buf_size = size;
477 else if (n > self->buf_size) {
478 self->buf = (char *)realloc(self->buf, n * sizeof(char));
479 if (!self->buf) {
480 PyErr_NoMemory();
481 return -1;
484 self->buf_size = n;
487 Py_BEGIN_ALLOW_THREADS
488 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
489 Py_END_ALLOW_THREADS
490 if (nbytesread != (size_t)n) {
491 if (feof(self->fp)) {
492 PyErr_SetNone(PyExc_EOFError);
493 return -1;
496 PyErr_SetFromErrno(PyExc_IOError);
497 return -1;
500 *s = self->buf;
502 return n;
506 static int
507 readline_file(Unpicklerobject *self, char **s)
509 int i;
511 if (self->buf_size == 0) {
512 if (!( self->buf = (char *)malloc(40 * sizeof(char)))) {
513 PyErr_NoMemory();
514 return -1;
517 self->buf_size = 40;
520 i = 0;
521 while (1) {
522 for (; i < (self->buf_size - 1); i++) {
523 if (feof(self->fp) ||
524 (self->buf[i] = getc(self->fp)) == '\n') {
525 self->buf[i + 1] = '\0';
526 *s = self->buf;
527 return i + 1;
530 self->buf = (char *)realloc(self->buf,
531 (self->buf_size * 2) * sizeof(char));
532 if (!self->buf) {
533 PyErr_NoMemory();
534 return -1;
537 self->buf_size *= 2;
542 static int
543 read_cStringIO(Unpicklerobject *self, char **s, int n)
545 char *ptr;
547 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
548 PyErr_SetNone(PyExc_EOFError);
549 return -1;
552 *s = ptr;
554 return n;
558 static int
559 readline_cStringIO(Unpicklerobject *self, char **s)
561 int n;
562 char *ptr;
564 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
565 return -1;
568 *s = ptr;
570 return n;
574 static int
575 read_other(Unpicklerobject *self, char **s, int n)
577 PyObject *bytes, *str=0;
579 if (!( bytes = PyInt_FromLong(n))) return -1;
581 ARG_TUP(self, bytes);
582 if (self->arg) {
583 str = PyObject_Call(self->read, self->arg, NULL);
584 FREE_ARG_TUP(self);
586 if (! str) return -1;
588 Py_XDECREF(self->last_string);
589 self->last_string = str;
591 if (! (*s = PyString_AsString(str))) return -1;
592 return n;
596 static int
597 readline_other(Unpicklerobject *self, char **s)
599 PyObject *str;
600 int str_size;
602 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
603 return -1;
606 if ((str_size = PyString_Size(str)) < 0)
607 return -1;
609 Py_XDECREF(self->last_string);
610 self->last_string = str;
612 if (! (*s = PyString_AsString(str)))
613 return -1;
615 return str_size;
619 static char *
620 pystrndup(char *s, int l)
622 char *r;
623 if (!( r=malloc((l+1)*sizeof(char)))) return (char*)PyErr_NoMemory();
624 memcpy(r,s,l);
625 r[l]=0;
626 return r;
630 static int
631 get(Picklerobject *self, PyObject *id)
633 PyObject *value, *mv;
634 long c_value;
635 char s[30];
636 size_t len;
638 if (!( mv = PyDict_GetItem(self->memo, id))) {
639 PyErr_SetObject(PyExc_KeyError, id);
640 return -1;
643 if (!( value = PyTuple_GetItem(mv, 0)))
644 return -1;
646 if (!( PyInt_Check(value))) {
647 PyErr_SetString(PicklingError, "no int where int expected in memo");
648 return -1;
650 c_value = PyInt_AS_LONG((PyIntObject*)value);
652 if (!self->bin) {
653 s[0] = GET;
654 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
655 len = strlen(s);
657 else if (Pdata_Check(self->file)) {
658 if (write_other(self, NULL, 0) < 0) return -1;
659 PDATA_APPEND(self->file, mv, -1);
660 return 0;
662 else {
663 if (c_value < 256) {
664 s[0] = BINGET;
665 s[1] = (int)(c_value & 0xff);
666 len = 2;
668 else {
669 s[0] = LONG_BINGET;
670 s[1] = (int)(c_value & 0xff);
671 s[2] = (int)((c_value >> 8) & 0xff);
672 s[3] = (int)((c_value >> 16) & 0xff);
673 s[4] = (int)((c_value >> 24) & 0xff);
674 len = 5;
678 if ((*self->write_func)(self, s, len) < 0)
679 return -1;
681 return 0;
685 static int
686 put(Picklerobject *self, PyObject *ob)
688 if (ob->ob_refcnt < 2 || self->fast)
689 return 0;
691 return put2(self, ob);
695 static int
696 put2(Picklerobject *self, PyObject *ob)
698 char c_str[30];
699 int p;
700 size_t len;
701 int res = -1;
702 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
704 if (self->fast)
705 return 0;
707 if ((p = PyDict_Size(self->memo)) < 0)
708 goto finally;
710 /* Make sure memo keys are positive! */
711 p++;
713 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
714 goto finally;
716 if (!( memo_len = PyInt_FromLong(p)))
717 goto finally;
719 if (!( t = PyTuple_New(2)))
720 goto finally;
722 PyTuple_SET_ITEM(t, 0, memo_len);
723 Py_INCREF(memo_len);
724 PyTuple_SET_ITEM(t, 1, ob);
725 Py_INCREF(ob);
727 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
728 goto finally;
730 if (!self->bin) {
731 c_str[0] = PUT;
732 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
733 len = strlen(c_str);
735 else if (Pdata_Check(self->file)) {
736 if (write_other(self, NULL, 0) < 0) return -1;
737 PDATA_APPEND(self->file, memo_len, -1);
738 res=0; /* Job well done ;) */
739 goto finally;
741 else {
742 if (p >= 256) {
743 c_str[0] = LONG_BINPUT;
744 c_str[1] = (int)(p & 0xff);
745 c_str[2] = (int)((p >> 8) & 0xff);
746 c_str[3] = (int)((p >> 16) & 0xff);
747 c_str[4] = (int)((p >> 24) & 0xff);
748 len = 5;
750 else {
751 c_str[0] = BINPUT;
752 c_str[1] = p;
753 len = 2;
757 if ((*self->write_func)(self, c_str, len) < 0)
758 goto finally;
760 res = 0;
762 finally:
763 Py_XDECREF(py_ob_id);
764 Py_XDECREF(memo_len);
765 Py_XDECREF(t);
767 return res;
770 #define PyImport_Import cPickle_Import
772 static PyObject *
773 PyImport_Import(PyObject *module_name)
775 static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
776 static PyObject *standard_builtins=0;
777 PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
779 if (!( silly_list )) {
780 if (!( __import___str=PyString_FromString("__import__")))
781 return NULL;
782 if (!( __builtins___str=PyString_FromString("__builtins__")))
783 return NULL;
784 if (!( silly_list=Py_BuildValue("[s]","__doc__")))
785 return NULL;
788 if ((globals=PyEval_GetGlobals())) {
789 Py_INCREF(globals);
790 __builtins__=PyObject_GetItem(globals,__builtins___str);
791 if (!__builtins__)
792 goto err;
794 else {
795 PyErr_Clear();
797 if (!(standard_builtins ||
798 (standard_builtins=PyImport_ImportModule("__builtin__"))))
799 return NULL;
801 __builtins__=standard_builtins;
802 Py_INCREF(__builtins__);
803 globals = Py_BuildValue("{sO}", "__builtins__", __builtins__);
804 if (!globals)
805 goto err;
808 if (PyDict_Check(__builtins__)) {
809 __import__=PyObject_GetItem(__builtins__,__import___str);
810 if (!__import__) goto err;
812 else {
813 __import__=PyObject_GetAttr(__builtins__,__import___str);
814 if (!__import__) goto err;
817 r=PyObject_CallFunction(__import__,"OOOO",
818 module_name, globals, globals, silly_list);
819 if (!r)
820 goto err;
822 Py_DECREF(globals);
823 Py_DECREF(__builtins__);
824 Py_DECREF(__import__);
826 return r;
827 err:
828 Py_XDECREF(globals);
829 Py_XDECREF(__builtins__);
830 Py_XDECREF(__import__);
831 return NULL;
834 static PyObject *
835 whichmodule(PyObject *global, PyObject *global_name)
837 int i, j;
838 PyObject *module = 0, *modules_dict = 0,
839 *global_name_attr = 0, *name = 0;
841 module = PyObject_GetAttrString(global, "__module__");
842 if (module) return module;
843 PyErr_Clear();
845 if (!( modules_dict = PySys_GetObject("modules")))
846 return NULL;
848 i = 0;
849 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
851 if (PyObject_Compare(name, __main___str)==0) continue;
853 global_name_attr = PyObject_GetAttr(module, global_name);
854 if (!global_name_attr) {
855 PyErr_Clear();
856 continue;
859 if (global_name_attr != global) {
860 Py_DECREF(global_name_attr);
861 continue;
864 Py_DECREF(global_name_attr);
866 break;
869 /* The following implements the rule in pickle.py added in 1.5
870 that used __main__ if no module is found. I don't actually
871 like this rule. jlf
873 if (!j) {
874 j=1;
875 name=__main___str;
878 Py_INCREF(name);
879 return name;
883 static int
884 fast_save_enter(Picklerobject *self, PyObject *obj)
886 /* if fast_container < 0, we're doing an error exit. */
887 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
888 PyObject *key = NULL;
889 if (self->fast_memo == NULL) {
890 self->fast_memo = PyDict_New();
891 if (self->fast_memo == NULL) {
892 self->fast_container = -1;
893 return 0;
896 key = PyLong_FromVoidPtr(obj);
897 if (key == NULL)
898 return 0;
899 if (PyDict_GetItem(self->fast_memo, key)) {
900 PyErr_Format(PyExc_ValueError,
901 "fast mode: can't pickle cyclic objects including object type %s at %p",
902 obj->ob_type->tp_name, obj);
903 self->fast_container = -1;
904 return 0;
906 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
907 self->fast_container = -1;
908 return 0;
911 return 1;
914 int
915 fast_save_leave(Picklerobject *self, PyObject *obj)
917 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
918 PyObject *key = PyLong_FromVoidPtr(obj);
919 if (key == NULL)
920 return 0;
921 if (PyDict_DelItem(self->fast_memo, key) < 0) {
922 return 0;
925 return 1;
928 static int
929 save_none(Picklerobject *self, PyObject *args)
931 static char none = NONE;
932 if ((*self->write_func)(self, &none, 1) < 0)
933 return -1;
935 return 0;
938 static int
939 save_bool(Picklerobject *self, PyObject *args)
941 static char *buf[2] = {FALSE, TRUE};
942 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
943 long l = PyInt_AS_LONG((PyIntObject *)args);
945 if ((*self->write_func)(self, buf[l], len[l]) < 0)
946 return -1;
948 return 0;
951 static int
952 save_int(Picklerobject *self, PyObject *args)
954 char c_str[32];
955 long l = PyInt_AS_LONG((PyIntObject *)args);
956 int len = 0;
958 if (!self->bin
959 #if SIZEOF_LONG > 4
960 || l > 0x7fffffffL
961 || l < -0x80000000L
962 #endif
964 /* Text-mode pickle, or long too big to fit in the 4-byte
965 * signed BININT format: store as a string.
967 c_str[0] = INT;
968 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
969 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
970 return -1;
972 else {
973 /* Binary pickle and l fits in a signed 4-byte int. */
974 c_str[1] = (int)( l & 0xff);
975 c_str[2] = (int)((l >> 8) & 0xff);
976 c_str[3] = (int)((l >> 16) & 0xff);
977 c_str[4] = (int)((l >> 24) & 0xff);
979 if ((c_str[4] == 0) && (c_str[3] == 0)) {
980 if (c_str[2] == 0) {
981 c_str[0] = BININT1;
982 len = 2;
984 else {
985 c_str[0] = BININT2;
986 len = 3;
989 else {
990 c_str[0] = BININT;
991 len = 5;
994 if ((*self->write_func)(self, c_str, len) < 0)
995 return -1;
998 return 0;
1002 static int
1003 save_long(Picklerobject *self, PyObject *args)
1005 int size, res = -1;
1006 PyObject *repr = 0;
1008 static char l = LONG;
1010 if (!( repr = PyObject_Repr(args)))
1011 goto finally;
1013 if ((size = PyString_Size(repr)) < 0)
1014 goto finally;
1016 if ((*self->write_func)(self, &l, 1) < 0)
1017 goto finally;
1019 if ((*self->write_func)(self,
1020 PyString_AS_STRING((PyStringObject *)repr),
1021 size) < 0)
1022 goto finally;
1024 if ((*self->write_func)(self, "\n", 1) < 0)
1025 goto finally;
1027 res = 0;
1029 finally:
1030 Py_XDECREF(repr);
1032 return res;
1036 static int
1037 save_float(Picklerobject *self, PyObject *args)
1039 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
1041 if (self->bin) {
1042 int s, e;
1043 double f;
1044 long fhi, flo;
1045 char str[9];
1046 unsigned char *p = (unsigned char *)str;
1048 *p = BINFLOAT;
1049 p++;
1051 if (x < 0) {
1052 s = 1;
1053 x = -x;
1055 else
1056 s = 0;
1058 f = frexp(x, &e);
1060 /* Normalize f to be in the range [1.0, 2.0) */
1061 if (0.5 <= f && f < 1.0) {
1062 f *= 2.0;
1063 e--;
1065 else if (f == 0.0) {
1066 e = 0;
1068 else {
1069 PyErr_SetString(PyExc_SystemError,
1070 "frexp() result out of range");
1071 return -1;
1074 if (e >= 1024) {
1075 /* XXX 1024 itself is reserved for Inf/NaN */
1076 PyErr_SetString(PyExc_OverflowError,
1077 "float too large to pack with d format");
1078 return -1;
1080 else if (e < -1022) {
1081 /* Gradual underflow */
1082 f = ldexp(f, 1022 + e);
1083 e = 0;
1085 else if (!(e == 0 && f == 0.0)) {
1086 e += 1023;
1087 f -= 1.0; /* Get rid of leading 1 */
1090 /* fhi receives the high 28 bits;
1091 flo the low 24 bits (== 52 bits) */
1092 f *= 268435456.0; /* 2**28 */
1093 fhi = (long) floor(f); /* Truncate */
1094 f -= (double)fhi;
1095 f *= 16777216.0; /* 2**24 */
1096 flo = (long) floor(f + 0.5); /* Round */
1098 /* First byte */
1099 *p = (s<<7) | (e>>4);
1100 p++;
1102 /* Second byte */
1103 *p = (unsigned char) (((e&0xF)<<4) | (fhi>>24));
1104 p++;
1106 /* Third byte */
1107 *p = (unsigned char) ((fhi>>16) & 0xFF);
1108 p++;
1110 /* Fourth byte */
1111 *p = (unsigned char) ((fhi>>8) & 0xFF);
1112 p++;
1114 /* Fifth byte */
1115 *p = (unsigned char) (fhi & 0xFF);
1116 p++;
1118 /* Sixth byte */
1119 *p = (unsigned char) ((flo>>16) & 0xFF);
1120 p++;
1122 /* Seventh byte */
1123 *p = (unsigned char) ((flo>>8) & 0xFF);
1124 p++;
1126 /* Eighth byte */
1127 *p = (unsigned char) (flo & 0xFF);
1129 if ((*self->write_func)(self, str, 9) < 0)
1130 return -1;
1132 else {
1133 char c_str[250];
1134 c_str[0] = FLOAT;
1135 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%.17g\n", x);
1137 if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
1138 return -1;
1141 return 0;
1145 static int
1146 save_string(Picklerobject *self, PyObject *args, int doput)
1148 int size, len;
1149 PyObject *repr=0;
1151 if ((size = PyString_Size(args)) < 0)
1152 return -1;
1154 if (!self->bin) {
1155 char *repr_str;
1157 static char string = STRING;
1159 if (!( repr = PyObject_Repr(args)))
1160 return -1;
1162 if ((len = PyString_Size(repr)) < 0)
1163 goto err;
1164 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1166 if ((*self->write_func)(self, &string, 1) < 0)
1167 goto err;
1169 if ((*self->write_func)(self, repr_str, len) < 0)
1170 goto err;
1172 if ((*self->write_func)(self, "\n", 1) < 0)
1173 goto err;
1175 Py_XDECREF(repr);
1177 else {
1178 int i;
1179 char c_str[5];
1181 if ((size = PyString_Size(args)) < 0)
1182 return -1;
1184 if (size < 256) {
1185 c_str[0] = SHORT_BINSTRING;
1186 c_str[1] = size;
1187 len = 2;
1189 else {
1190 c_str[0] = BINSTRING;
1191 for (i = 1; i < 5; i++)
1192 c_str[i] = (int)(size >> ((i - 1) * 8));
1193 len = 5;
1196 if ((*self->write_func)(self, c_str, len) < 0)
1197 return -1;
1199 if (size > 128 && Pdata_Check(self->file)) {
1200 if (write_other(self, NULL, 0) < 0) return -1;
1201 PDATA_APPEND(self->file, args, -1);
1203 else {
1204 if ((*self->write_func)(self,
1205 PyString_AS_STRING((PyStringObject *)args), size) < 0)
1206 return -1;
1210 if (doput)
1211 if (put(self, args) < 0)
1212 return -1;
1214 return 0;
1216 err:
1217 Py_XDECREF(repr);
1218 return -1;
1222 #ifdef Py_USING_UNICODE
1223 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1224 backslash and newline characters to \uXXXX escapes. */
1225 static PyObject *
1226 modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1228 PyObject *repr;
1229 char *p;
1230 char *q;
1232 static const char *hexdigit = "0123456789ABCDEF";
1234 repr = PyString_FromStringAndSize(NULL, 6 * size);
1235 if (repr == NULL)
1236 return NULL;
1237 if (size == 0)
1238 return repr;
1240 p = q = PyString_AS_STRING(repr);
1241 while (size-- > 0) {
1242 Py_UNICODE ch = *s++;
1243 /* Map 16-bit characters to '\uxxxx' */
1244 if (ch >= 256 || ch == '\\' || ch == '\n') {
1245 *p++ = '\\';
1246 *p++ = 'u';
1247 *p++ = hexdigit[(ch >> 12) & 0xf];
1248 *p++ = hexdigit[(ch >> 8) & 0xf];
1249 *p++ = hexdigit[(ch >> 4) & 0xf];
1250 *p++ = hexdigit[ch & 15];
1252 /* Copy everything else as-is */
1253 else
1254 *p++ = (char) ch;
1256 *p = '\0';
1257 _PyString_Resize(&repr, p - q);
1258 return repr;
1262 static int
1263 save_unicode(Picklerobject *self, PyObject *args, int doput)
1265 int size, len;
1266 PyObject *repr=0;
1268 if (!PyUnicode_Check(args))
1269 return -1;
1271 if (!self->bin) {
1272 char *repr_str;
1273 static char string = UNICODE;
1275 repr = modified_EncodeRawUnicodeEscape(
1276 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1277 if (!repr)
1278 return -1;
1280 if ((len = PyString_Size(repr)) < 0)
1281 goto err;
1282 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1284 if ((*self->write_func)(self, &string, 1) < 0)
1285 goto err;
1287 if ((*self->write_func)(self, repr_str, len) < 0)
1288 goto err;
1290 if ((*self->write_func)(self, "\n", 1) < 0)
1291 goto err;
1293 Py_XDECREF(repr);
1295 else {
1296 int i;
1297 char c_str[5];
1299 if (!( repr = PyUnicode_AsUTF8String(args)))
1300 return -1;
1302 if ((size = PyString_Size(repr)) < 0)
1303 goto err;
1305 c_str[0] = BINUNICODE;
1306 for (i = 1; i < 5; i++)
1307 c_str[i] = (int)(size >> ((i - 1) * 8));
1308 len = 5;
1310 if ((*self->write_func)(self, c_str, len) < 0)
1311 goto err;
1313 if (size > 128 && Pdata_Check(self->file)) {
1314 if (write_other(self, NULL, 0) < 0)
1315 goto err;
1316 PDATA_APPEND(self->file, repr, -1);
1318 else {
1319 if ((*self->write_func)(self, PyString_AS_STRING(repr),
1320 size) < 0)
1321 goto err;
1324 Py_DECREF(repr);
1327 if (doput)
1328 if (put(self, args) < 0)
1329 return -1;
1331 return 0;
1333 err:
1334 Py_XDECREF(repr);
1335 return -1;
1337 #endif
1340 static int
1341 save_tuple(Picklerobject *self, PyObject *args)
1343 PyObject *element = 0, *py_tuple_id = 0;
1344 int len, i, res = -1;
1346 static char tuple = TUPLE;
1348 if ((*self->write_func)(self, &MARKv, 1) < 0)
1349 goto finally;
1351 if ((len = PyTuple_Size(args)) < 0)
1352 goto finally;
1354 for (i = 0; i < len; i++) {
1355 if (!( element = PyTuple_GET_ITEM((PyTupleObject *)args, i)))
1356 goto finally;
1358 if (save(self, element, 0) < 0)
1359 goto finally;
1362 if (!( py_tuple_id = PyLong_FromVoidPtr(args)))
1363 goto finally;
1365 if (len) {
1366 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1367 if (self->bin) {
1368 static char pop_mark = POP_MARK;
1370 if ((*self->write_func)(self, &pop_mark, 1) < 0)
1371 goto finally;
1373 else {
1374 static char pop = POP;
1376 for (i = 0; i <= len; i++) {
1377 if ((*self->write_func)(self, &pop, 1) < 0)
1378 goto finally;
1382 if (get(self, py_tuple_id) < 0)
1383 goto finally;
1385 res = 0;
1386 goto finally;
1390 if ((*self->write_func)(self, &tuple, 1) < 0) {
1391 goto finally;
1394 if (put(self, args) < 0)
1395 goto finally;
1397 res = 0;
1399 finally:
1400 Py_XDECREF(py_tuple_id);
1402 return res;
1405 static int
1406 save_empty_tuple(Picklerobject *self, PyObject *args)
1408 static char tuple = EMPTY_TUPLE;
1410 return (*self->write_func)(self, &tuple, 1);
1414 static int
1415 save_list(Picklerobject *self, PyObject *args)
1417 PyObject *element = 0;
1418 int s_len, len, i, using_appends, res = -1;
1419 char s[3];
1421 static char append = APPEND, appends = APPENDS;
1423 if (self->fast && !fast_save_enter(self, args))
1424 goto finally;
1426 if (self->bin) {
1427 s[0] = EMPTY_LIST;
1428 s_len = 1;
1430 else {
1431 s[0] = MARK;
1432 s[1] = LIST;
1433 s_len = 2;
1436 if ((len = PyList_Size(args)) < 0)
1437 goto finally;
1439 if ((*self->write_func)(self, s, s_len) < 0)
1440 goto finally;
1442 if (len == 0) {
1443 if (put(self, args) < 0)
1444 goto finally;
1446 else {
1447 if (put2(self, args) < 0)
1448 goto finally;
1451 if ((using_appends = (self->bin && (len > 1))))
1452 if ((*self->write_func)(self, &MARKv, 1) < 0)
1453 goto finally;
1455 for (i = 0; i < len; i++) {
1456 if (!( element = PyList_GET_ITEM((PyListObject *)args, i)))
1457 goto finally;
1459 if (save(self, element, 0) < 0)
1460 goto finally;
1462 if (!using_appends) {
1463 if ((*self->write_func)(self, &append, 1) < 0)
1464 goto finally;
1468 if (using_appends) {
1469 if ((*self->write_func)(self, &appends, 1) < 0)
1470 goto finally;
1473 res = 0;
1475 finally:
1476 if (self->fast && !fast_save_leave(self, args))
1477 res = -1;
1479 return res;
1483 static int
1484 save_dict(Picklerobject *self, PyObject *args)
1486 PyObject *key = 0, *value = 0;
1487 int i, len, res = -1, using_setitems;
1488 char s[3];
1490 static char setitem = SETITEM, setitems = SETITEMS;
1492 if (self->fast && !fast_save_enter(self, args))
1493 goto finally;
1495 if (self->bin) {
1496 s[0] = EMPTY_DICT;
1497 len = 1;
1499 else {
1500 s[0] = MARK;
1501 s[1] = DICT;
1502 len = 2;
1505 if ((*self->write_func)(self, s, len) < 0)
1506 goto finally;
1508 if ((len = PyDict_Size(args)) < 0)
1509 goto finally;
1511 if (len == 0) {
1512 if (put(self, args) < 0)
1513 goto finally;
1515 else {
1516 if (put2(self, args) < 0)
1517 goto finally;
1520 if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
1521 if ((*self->write_func)(self, &MARKv, 1) < 0)
1522 goto finally;
1524 i = 0;
1525 while (PyDict_Next(args, &i, &key, &value)) {
1526 if (save(self, key, 0) < 0)
1527 goto finally;
1529 if (save(self, value, 0) < 0)
1530 goto finally;
1532 if (!using_setitems) {
1533 if ((*self->write_func)(self, &setitem, 1) < 0)
1534 goto finally;
1538 if (using_setitems) {
1539 if ((*self->write_func)(self, &setitems, 1) < 0)
1540 goto finally;
1543 res = 0;
1545 finally:
1546 if (self->fast && !fast_save_leave(self, args))
1547 res = -1;
1549 return res;
1553 static int
1554 save_inst(Picklerobject *self, PyObject *args)
1556 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1557 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1558 char *module_str, *name_str;
1559 int module_size, name_size, res = -1;
1561 static char inst = INST, obj = OBJ, build = BUILD;
1563 if (self->fast && !fast_save_enter(self, args))
1564 goto finally;
1566 if ((*self->write_func)(self, &MARKv, 1) < 0)
1567 goto finally;
1569 if (!( class = PyObject_GetAttr(args, __class___str)))
1570 goto finally;
1572 if (self->bin) {
1573 if (save(self, class, 0) < 0)
1574 goto finally;
1577 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1578 PyObject *element = 0;
1579 int i, len;
1581 if (!( class_args =
1582 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
1583 goto finally;
1585 if ((len = PyObject_Size(class_args)) < 0)
1586 goto finally;
1588 for (i = 0; i < len; i++) {
1589 if (!( element = PySequence_GetItem(class_args, i)))
1590 goto finally;
1592 if (save(self, element, 0) < 0) {
1593 Py_DECREF(element);
1594 goto finally;
1597 Py_DECREF(element);
1600 else {
1601 PyErr_Clear();
1604 if (!self->bin) {
1605 if (!( name = ((PyClassObject *)class)->cl_name )) {
1606 PyErr_SetString(PicklingError, "class has no name");
1607 goto finally;
1610 if (!( module = whichmodule(class, name)))
1611 goto finally;
1614 if ((module_size = PyString_Size(module)) < 0 ||
1615 (name_size = PyString_Size(name)) < 0)
1616 goto finally;
1618 module_str = PyString_AS_STRING((PyStringObject *)module);
1619 name_str = PyString_AS_STRING((PyStringObject *)name);
1621 if ((*self->write_func)(self, &inst, 1) < 0)
1622 goto finally;
1624 if ((*self->write_func)(self, module_str, module_size) < 0)
1625 goto finally;
1627 if ((*self->write_func)(self, "\n", 1) < 0)
1628 goto finally;
1630 if ((*self->write_func)(self, name_str, name_size) < 0)
1631 goto finally;
1633 if ((*self->write_func)(self, "\n", 1) < 0)
1634 goto finally;
1636 else if ((*self->write_func)(self, &obj, 1) < 0) {
1637 goto finally;
1640 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1641 state = PyObject_Call(getstate_func, empty_tuple, NULL);
1642 if (!state)
1643 goto finally;
1645 else {
1646 PyErr_Clear();
1648 if (!( state = PyObject_GetAttr(args, __dict___str))) {
1649 PyErr_Clear();
1650 res = 0;
1651 goto finally;
1655 if (!PyDict_Check(state)) {
1656 if (put2(self, args) < 0)
1657 goto finally;
1659 else {
1660 if (put(self, args) < 0)
1661 goto finally;
1664 if (save(self, state, 0) < 0)
1665 goto finally;
1667 if ((*self->write_func)(self, &build, 1) < 0)
1668 goto finally;
1670 res = 0;
1672 finally:
1673 if (self->fast && !fast_save_leave(self, args))
1674 res = -1;
1676 Py_XDECREF(module);
1677 Py_XDECREF(class);
1678 Py_XDECREF(state);
1679 Py_XDECREF(getinitargs_func);
1680 Py_XDECREF(getstate_func);
1681 Py_XDECREF(class_args);
1683 return res;
1687 static int
1688 save_global(Picklerobject *self, PyObject *args, PyObject *name)
1690 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
1691 char *name_str, *module_str;
1692 int module_size, name_size, res = -1;
1694 static char global = GLOBAL;
1696 if (name) {
1697 global_name = name;
1698 Py_INCREF(global_name);
1700 else {
1701 if (!( global_name = PyObject_GetAttr(args, __name___str)))
1702 goto finally;
1705 if (!( module = whichmodule(args, global_name)))
1706 goto finally;
1708 if ((module_size = PyString_Size(module)) < 0 ||
1709 (name_size = PyString_Size(global_name)) < 0)
1710 goto finally;
1712 module_str = PyString_AS_STRING((PyStringObject *)module);
1713 name_str = PyString_AS_STRING((PyStringObject *)global_name);
1715 mod = PyImport_ImportModule(module_str);
1716 if (mod == NULL) {
1717 /* Py_ErrClear(); ?? */
1718 cPickle_ErrFormat(PicklingError,
1719 "Can't pickle %s: it's not found as %s.%s",
1720 "OSS", args, module, global_name);
1721 goto finally;
1723 klass = PyObject_GetAttrString(mod, name_str);
1724 if (klass == NULL) {
1725 cPickle_ErrFormat(PicklingError,
1726 "Can't pickle %s: it's not found as %s.%s",
1727 "OSS", args, module, global_name);
1728 goto finally;
1730 if (klass != args) {
1731 Py_DECREF(klass);
1732 cPickle_ErrFormat(PicklingError,
1733 "Can't pickle %s: it's not the same object as %s.%s",
1734 "OSS", args, module, global_name);
1735 goto finally;
1737 Py_DECREF(klass);
1739 if ((*self->write_func)(self, &global, 1) < 0)
1740 goto finally;
1742 if ((*self->write_func)(self, module_str, module_size) < 0)
1743 goto finally;
1745 if ((*self->write_func)(self, "\n", 1) < 0)
1746 goto finally;
1748 if ((*self->write_func)(self, name_str, name_size) < 0)
1749 goto finally;
1751 if ((*self->write_func)(self, "\n", 1) < 0)
1752 goto finally;
1754 if (put(self, args) < 0)
1755 goto finally;
1757 res = 0;
1759 finally:
1760 Py_XDECREF(module);
1761 Py_XDECREF(global_name);
1762 Py_XDECREF(mod);
1764 return res;
1767 static int
1768 save_pers(Picklerobject *self, PyObject *args, PyObject *f)
1770 PyObject *pid = 0;
1771 int size, res = -1;
1773 static char persid = PERSID, binpersid = BINPERSID;
1775 Py_INCREF(args);
1776 ARG_TUP(self, args);
1777 if (self->arg) {
1778 pid = PyObject_Call(f, self->arg, NULL);
1779 FREE_ARG_TUP(self);
1781 if (! pid) return -1;
1783 if (pid != Py_None) {
1784 if (!self->bin) {
1785 if (!PyString_Check(pid)) {
1786 PyErr_SetString(PicklingError,
1787 "persistent id must be string");
1788 goto finally;
1791 if ((*self->write_func)(self, &persid, 1) < 0)
1792 goto finally;
1794 if ((size = PyString_Size(pid)) < 0)
1795 goto finally;
1797 if ((*self->write_func)(self,
1798 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
1799 goto finally;
1801 if ((*self->write_func)(self, "\n", 1) < 0)
1802 goto finally;
1804 res = 1;
1805 goto finally;
1807 else if (save(self, pid, 1) >= 0) {
1808 if ((*self->write_func)(self, &binpersid, 1) < 0)
1809 res = -1;
1810 else
1811 res = 1;
1814 goto finally;
1817 res = 0;
1819 finally:
1820 Py_XDECREF(pid);
1822 return res;
1826 static int
1827 save_reduce(Picklerobject *self, PyObject *callable,
1828 PyObject *tup, PyObject *state, PyObject *ob)
1830 static char reduce = REDUCE, build = BUILD;
1832 if (save(self, callable, 0) < 0)
1833 return -1;
1835 if (save(self, tup, 0) < 0)
1836 return -1;
1838 if ((*self->write_func)(self, &reduce, 1) < 0)
1839 return -1;
1841 if (ob != NULL) {
1842 if (state && !PyDict_Check(state)) {
1843 if (put2(self, ob) < 0)
1844 return -1;
1846 else {
1847 if (put(self, ob) < 0)
1848 return -1;
1852 if (state) {
1853 if (save(self, state, 0) < 0)
1854 return -1;
1856 if ((*self->write_func)(self, &build, 1) < 0)
1857 return -1;
1860 return 0;
1863 static int
1864 save(Picklerobject *self, PyObject *args, int pers_save)
1866 PyTypeObject *type;
1867 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
1868 *callable = 0, *state = 0;
1869 int res = -1, tmp, size;
1871 if (self->nesting++ > Py_GetRecursionLimit()){
1872 PyErr_SetString(PyExc_RuntimeError,
1873 "maximum recursion depth exceeded");
1874 goto finally;
1877 if (!pers_save && self->pers_func) {
1878 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
1879 res = tmp;
1880 goto finally;
1884 if (args == Py_None) {
1885 res = save_none(self, args);
1886 goto finally;
1889 type = args->ob_type;
1891 switch (type->tp_name[0]) {
1892 case 'b':
1893 if (args == Py_False || args == Py_True) {
1894 res = save_bool(self, args);
1895 goto finally;
1897 break;
1898 case 'i':
1899 if (type == &PyInt_Type) {
1900 res = save_int(self, args);
1901 goto finally;
1903 break;
1905 case 'l':
1906 if (type == &PyLong_Type) {
1907 res = save_long(self, args);
1908 goto finally;
1910 break;
1912 case 'f':
1913 if (type == &PyFloat_Type) {
1914 res = save_float(self, args);
1915 goto finally;
1917 break;
1919 case 't':
1920 if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
1921 if (self->bin) res = save_empty_tuple(self, args);
1922 else res = save_tuple(self, args);
1923 goto finally;
1925 break;
1927 case 's':
1928 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
1929 res = save_string(self, args, 0);
1930 goto finally;
1933 #ifdef Py_USING_UNICODE
1934 case 'u':
1935 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
1936 res = save_unicode(self, args, 0);
1937 goto finally;
1939 #endif
1942 if (args->ob_refcnt > 1) {
1943 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
1944 goto finally;
1946 if (PyDict_GetItem(self->memo, py_ob_id)) {
1947 if (get(self, py_ob_id) < 0)
1948 goto finally;
1950 res = 0;
1951 goto finally;
1955 switch (type->tp_name[0]) {
1956 case 's':
1957 if (type == &PyString_Type) {
1958 res = save_string(self, args, 1);
1959 goto finally;
1961 break;
1963 #ifdef Py_USING_UNICODE
1964 case 'u':
1965 if (type == &PyUnicode_Type) {
1966 res = save_unicode(self, args, 1);
1967 goto finally;
1969 break;
1970 #endif
1972 case 't':
1973 if (type == &PyTuple_Type) {
1974 res = save_tuple(self, args);
1975 goto finally;
1977 if (type == &PyType_Type) {
1978 res = save_global(self, args, NULL);
1979 goto finally;
1981 break;
1983 case 'l':
1984 if (type == &PyList_Type) {
1985 res = save_list(self, args);
1986 goto finally;
1988 break;
1990 case 'd':
1991 if (type == &PyDict_Type) {
1992 res = save_dict(self, args);
1993 goto finally;
1995 break;
1997 case 'i':
1998 if (type == &PyInstance_Type) {
1999 res = save_inst(self, args);
2000 goto finally;
2002 break;
2004 case 'c':
2005 if (type == &PyClass_Type) {
2006 res = save_global(self, args, NULL);
2007 goto finally;
2009 break;
2011 case 'f':
2012 if (type == &PyFunction_Type) {
2013 res = save_global(self, args, NULL);
2014 goto finally;
2016 break;
2018 case 'b':
2019 if (type == &PyCFunction_Type) {
2020 res = save_global(self, args, NULL);
2021 goto finally;
2025 if (!pers_save && self->inst_pers_func) {
2026 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2027 res = tmp;
2028 goto finally;
2032 if (PyType_IsSubtype(type, &PyType_Type)) {
2033 res = save_global(self, args, NULL);
2034 goto finally;
2037 if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
2038 Py_INCREF(__reduce__);
2040 Py_INCREF(args);
2041 ARG_TUP(self, args);
2042 if (self->arg) {
2043 t = PyObject_Call(__reduce__, self->arg, NULL);
2044 FREE_ARG_TUP(self);
2046 if (! t) goto finally;
2048 else {
2049 PyErr_Clear();
2051 if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
2052 t = PyObject_Call(__reduce__, empty_tuple, NULL);
2053 if (!t)
2054 goto finally;
2056 else {
2057 PyErr_Clear();
2061 if (t) {
2062 if (PyString_Check(t)) {
2063 res = save_global(self, args, t);
2064 goto finally;
2067 if (!PyTuple_Check(t)) {
2068 cPickle_ErrFormat(PicklingError, "Value returned by %s must "
2069 "be a tuple", "O", __reduce__);
2070 goto finally;
2073 size = PyTuple_Size(t);
2075 if ((size != 3) && (size != 2)) {
2076 cPickle_ErrFormat(PicklingError, "tuple returned by %s must "
2077 "contain only two or three elements", "O", __reduce__);
2078 goto finally;
2081 callable = PyTuple_GET_ITEM(t, 0);
2083 arg_tup = PyTuple_GET_ITEM(t, 1);
2085 if (size > 2) {
2086 state = PyTuple_GET_ITEM(t, 2);
2089 if (!( PyTuple_Check(arg_tup) || arg_tup==Py_None )) {
2090 cPickle_ErrFormat(PicklingError, "Second element of tuple "
2091 "returned by %s must be a tuple", "O", __reduce__);
2092 goto finally;
2095 res = save_reduce(self, callable, arg_tup, state, args);
2096 goto finally;
2099 PyErr_SetObject(UnpickleableError, args);
2101 finally:
2102 self->nesting--;
2103 Py_XDECREF(py_ob_id);
2104 Py_XDECREF(__reduce__);
2105 Py_XDECREF(t);
2107 return res;
2111 static int
2112 dump(Picklerobject *self, PyObject *args)
2114 static char stop = STOP;
2116 if (save(self, args, 0) < 0)
2117 return -1;
2119 if ((*self->write_func)(self, &stop, 1) < 0)
2120 return -1;
2122 if ((*self->write_func)(self, NULL, 0) < 0)
2123 return -1;
2125 return 0;
2128 static PyObject *
2129 Pickle_clear_memo(Picklerobject *self, PyObject *args)
2131 if (self->memo)
2132 PyDict_Clear(self->memo);
2133 Py_INCREF(Py_None);
2134 return Py_None;
2137 static PyObject *
2138 Pickle_getvalue(Picklerobject *self, PyObject *args)
2140 int l, i, rsize, ssize, clear=1, lm;
2141 long ik;
2142 PyObject *k, *r;
2143 char *s, *p, *have_get;
2144 Pdata *data;
2146 /* Can be called by Python code or C code */
2147 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2148 return NULL;
2150 /* Check to make sure we are based on a list */
2151 if (! Pdata_Check(self->file)) {
2152 PyErr_SetString(PicklingError,
2153 "Attempt to getvalue() a non-list-based pickler");
2154 return NULL;
2157 /* flush write buffer */
2158 if (write_other(self, NULL, 0) < 0) return NULL;
2160 data=(Pdata*)self->file;
2161 l=data->length;
2163 /* set up an array to hold get/put status */
2164 if ((lm=PyDict_Size(self->memo)) < 0) return NULL;
2165 lm++;
2166 if (! (have_get=malloc((lm)*sizeof(char)))) return PyErr_NoMemory();
2167 memset(have_get,0,lm);
2169 /* Scan for gets. */
2170 for (rsize=0, i=l; --i >= 0; ) {
2171 k=data->data[i];
2173 if (PyString_Check(k)) {
2174 rsize += PyString_GET_SIZE(k);
2177 else if (PyInt_Check(k)) { /* put */
2178 ik=PyInt_AS_LONG((PyIntObject*)k);
2179 if (ik >= lm || ik==0) {
2180 PyErr_SetString(PicklingError,
2181 "Invalid get data");
2182 return NULL;
2184 if (have_get[ik]) { /* with matching get */
2185 if (ik < 256) rsize += 2;
2186 else rsize+=5;
2190 else if (! (PyTuple_Check(k) &&
2191 PyTuple_GET_SIZE(k) == 2 &&
2192 PyInt_Check((k=PyTuple_GET_ITEM(k,0))))
2194 PyErr_SetString(PicklingError,
2195 "Unexpected data in internal list");
2196 return NULL;
2199 else { /* put */
2200 ik=PyInt_AS_LONG((PyIntObject*)k);
2201 if (ik >= lm || ik==0) {
2202 PyErr_SetString(PicklingError,
2203 "Invalid get data");
2204 return NULL;
2206 have_get[ik]=1;
2207 if (ik < 256) rsize += 2;
2208 else rsize+=5;
2213 /* Now generate the result */
2214 if (!( r=PyString_FromStringAndSize(NULL,rsize))) goto err;
2215 s=PyString_AS_STRING((PyStringObject*)r);
2217 for (i=0; i<l; i++) {
2218 k=data->data[i];
2220 if (PyString_Check(k)) {
2221 ssize=PyString_GET_SIZE(k);
2222 if (ssize) {
2223 p=PyString_AS_STRING((PyStringObject*)k);
2224 while (--ssize >= 0) *s++=*p++;
2228 else if (PyTuple_Check(k)) { /* get */
2229 ik=PyInt_AS_LONG((PyIntObject*)PyTuple_GET_ITEM(k,0));
2230 if (ik < 256) {
2231 *s++ = BINGET;
2232 *s++ = (int)(ik & 0xff);
2234 else {
2235 *s++ = LONG_BINGET;
2236 *s++ = (int)(ik & 0xff);
2237 *s++ = (int)((ik >> 8) & 0xff);
2238 *s++ = (int)((ik >> 16) & 0xff);
2239 *s++ = (int)((ik >> 24) & 0xff);
2243 else { /* put */
2244 ik=PyInt_AS_LONG((PyIntObject*)k);
2246 if (have_get[ik]) { /* with matching get */
2247 if (ik < 256) {
2248 *s++ = BINPUT;
2249 *s++ = (int)(ik & 0xff);
2251 else {
2252 *s++ = LONG_BINPUT;
2253 *s++ = (int)(ik & 0xff);
2254 *s++ = (int)((ik >> 8) & 0xff);
2255 *s++ = (int)((ik >> 16) & 0xff);
2256 *s++ = (int)((ik >> 24) & 0xff);
2263 if (clear) {
2264 PyDict_Clear(self->memo);
2265 Pdata_clear(data,0);
2268 free(have_get);
2269 return r;
2270 err:
2271 free(have_get);
2272 return NULL;
2275 static PyObject *
2276 Pickler_dump(Picklerobject *self, PyObject *args)
2278 PyObject *ob;
2279 int get=0;
2281 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
2282 return NULL;
2284 if (dump(self, ob) < 0)
2285 return NULL;
2287 if (get) return Pickle_getvalue(self, NULL);
2289 /* XXX Why does dump() return self? */
2290 Py_INCREF(self);
2291 return (PyObject*)self;
2295 static struct PyMethodDef Pickler_methods[] =
2297 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
2298 PyDoc_STR("dump(object) -- "
2299 "Write an object in pickle format to the object's pickle stream")},
2300 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
2301 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2302 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
2303 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2304 {NULL, NULL} /* sentinel */
2308 static Picklerobject *
2309 newPicklerobject(PyObject *file, int bin)
2311 Picklerobject *self;
2313 if (!( self = PyObject_New(Picklerobject, &Picklertype)))
2314 return NULL;
2316 self->fp = NULL;
2317 self->write = NULL;
2318 self->memo = NULL;
2319 self->arg = NULL;
2320 self->pers_func = NULL;
2321 self->inst_pers_func = NULL;
2322 self->write_buf = NULL;
2323 self->bin = bin;
2324 self->fast = 0;
2325 self->nesting = 0;
2326 self->fast_container = 0;
2327 self->fast_memo = NULL;
2328 self->buf_size = 0;
2329 self->dispatch_table = NULL;
2331 if (file)
2332 Py_INCREF(file);
2333 else
2334 file=Pdata_New();
2336 if (!( self->file = file ))
2337 goto err;
2339 if (!( self->memo = PyDict_New()))
2340 goto err;
2342 if (PyFile_Check(file)) {
2343 self->fp = PyFile_AsFile(file);
2344 if (self->fp == NULL) {
2345 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
2346 goto err;
2348 self->write_func = write_file;
2350 else if (PycStringIO_OutputCheck(file)) {
2351 self->write_func = write_cStringIO;
2353 else if (file == Py_None) {
2354 self->write_func = write_none;
2356 else {
2357 self->write_func = write_other;
2359 if (! Pdata_Check(file)) {
2360 self->write = PyObject_GetAttr(file, write_str);
2361 if (!self->write) {
2362 PyErr_Clear();
2363 PyErr_SetString(PyExc_TypeError,
2364 "argument must have 'write' "
2365 "attribute");
2366 goto err;
2370 if (!( self->write_buf =
2371 (char *)malloc(WRITE_BUF_SIZE * sizeof(char)))) {
2372 PyErr_NoMemory();
2373 goto err;
2377 if (PyEval_GetRestricted()) {
2378 /* Restricted execution, get private tables */
2379 PyObject *m;
2381 if (!( m=PyImport_Import(copy_reg_str))) goto err;
2382 self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
2383 Py_DECREF(m);
2384 if (!( self->dispatch_table )) goto err;
2386 else {
2387 self->dispatch_table=dispatch_table;
2388 Py_INCREF(dispatch_table);
2391 return self;
2393 err:
2394 Py_DECREF((PyObject *)self);
2395 return NULL;
2399 static PyObject *
2400 get_Pickler(PyObject *self, PyObject *args)
2402 PyObject *file = NULL;
2403 int bin = 1;
2405 if (!PyArg_ParseTuple(args, "|i:Pickler", &bin)) {
2406 PyErr_Clear();
2407 bin = 0;
2408 if (!PyArg_ParseTuple(args, "O|i:Pickler", &file, &bin))
2409 return NULL;
2411 return (PyObject *)newPicklerobject(file, bin);
2415 static void
2416 Pickler_dealloc(Picklerobject *self)
2418 Py_XDECREF(self->write);
2419 Py_XDECREF(self->memo);
2420 Py_XDECREF(self->fast_memo);
2421 Py_XDECREF(self->arg);
2422 Py_XDECREF(self->file);
2423 Py_XDECREF(self->pers_func);
2424 Py_XDECREF(self->inst_pers_func);
2425 Py_XDECREF(self->dispatch_table);
2427 if (self->write_buf) {
2428 free(self->write_buf);
2431 PyObject_Del(self);
2434 static PyObject *
2435 Pickler_get_pers_func(Picklerobject *p)
2437 if (p->pers_func == NULL)
2438 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2439 else
2440 Py_INCREF(p->pers_func);
2441 return p->pers_func;
2444 static int
2445 Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2447 if (v == NULL) {
2448 PyErr_SetString(PyExc_TypeError,
2449 "attribute deletion is not supported");
2450 return -1;
2452 Py_XDECREF(p->pers_func);
2453 Py_INCREF(v);
2454 p->pers_func = v;
2455 return 0;
2458 static int
2459 Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2461 if (v == NULL) {
2462 PyErr_SetString(PyExc_TypeError,
2463 "attribute deletion is not supported");
2464 return -1;
2466 Py_XDECREF(p->inst_pers_func);
2467 Py_INCREF(v);
2468 p->inst_pers_func = v;
2469 return 0;
2472 static PyObject *
2473 Pickler_get_memo(Picklerobject *p)
2475 if (p->memo == NULL)
2476 PyErr_SetString(PyExc_AttributeError, "memo");
2477 else
2478 Py_INCREF(p->memo);
2479 return p->memo;
2482 static int
2483 Pickler_set_memo(Picklerobject *p, PyObject *v)
2485 if (v == NULL) {
2486 PyErr_SetString(PyExc_TypeError,
2487 "attribute deletion is not supported");
2488 return -1;
2490 if (!PyDict_Check(v)) {
2491 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2492 return -1;
2494 Py_XDECREF(p->memo);
2495 Py_INCREF(v);
2496 p->memo = v;
2497 return 0;
2500 static PyObject *
2501 Pickler_get_error(Picklerobject *p)
2503 /* why is this an attribute on the Pickler? */
2504 Py_INCREF(PicklingError);
2505 return PicklingError;
2508 static PyMemberDef Pickler_members[] = {
2509 {"binary", T_INT, offsetof(Picklerobject, bin)},
2510 {"fast", T_INT, offsetof(Picklerobject, fast)},
2511 {NULL}
2514 static PyGetSetDef Pickler_getsets[] = {
2515 {"persistent_id", (getter)Pickler_get_pers_func,
2516 (setter)Pickler_set_pers_func},
2517 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
2518 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
2519 {"PicklingError", (getter)Pickler_get_error, NULL},
2520 {NULL}
2523 PyDoc_STRVAR(Picklertype__doc__,
2524 "Objects that know how to pickle objects\n");
2526 static PyTypeObject Picklertype = {
2527 PyObject_HEAD_INIT(NULL)
2528 0, /*ob_size*/
2529 "cPickle.Pickler", /*tp_name*/
2530 sizeof(Picklerobject), /*tp_basicsize*/
2532 (destructor)Pickler_dealloc, /* tp_dealloc */
2533 0, /* tp_print */
2534 0, /* tp_getattr */
2535 0, /* tp_setattr */
2536 0, /* tp_compare */
2537 0, /* tp_repr */
2538 0, /* tp_as_number */
2539 0, /* tp_as_sequence */
2540 0, /* tp_as_mapping */
2541 0, /* tp_hash */
2542 0, /* tp_call */
2543 0, /* tp_str */
2544 0, /* set below */ /* tp_getattro */
2545 0, /* set below */ /* tp_setattro */
2546 0, /* tp_as_buffer */
2547 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2548 Picklertype__doc__, /* tp_doc */
2549 0, /* tp_traverse */
2550 0, /* tp_clear */
2551 0, /* tp_richcompare */
2552 0, /* tp_weaklistoffset */
2553 0, /* tp_iter */
2554 0, /* tp_iternext */
2555 Pickler_methods, /* tp_methods */
2556 Pickler_members, /* tp_members */
2557 Pickler_getsets, /* tp_getset */
2560 static PyObject *
2561 find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
2563 PyObject *global = 0, *module;
2565 if (fc) {
2566 if (fc==Py_None) {
2567 PyErr_SetString(UnpicklingError,
2568 "Global and instance pickles are not supported.");
2569 return NULL;
2571 return PyObject_CallFunction(fc, "OO", py_module_name,
2572 py_global_name);
2575 module = PySys_GetObject("modules");
2576 if (module == NULL)
2577 return NULL;
2579 module = PyDict_GetItem(module, py_module_name);
2580 if (module == NULL) {
2581 module = PyImport_Import(py_module_name);
2582 if (!module)
2583 return NULL;
2584 global = PyObject_GetAttr(module, py_global_name);
2585 Py_DECREF(module);
2587 else
2588 global = PyObject_GetAttr(module, py_global_name);
2589 return global;
2592 static int
2593 marker(Unpicklerobject *self)
2595 if (self->num_marks < 1) {
2596 PyErr_SetString(UnpicklingError, "could not find MARK");
2597 return -1;
2600 return self->marks[--self->num_marks];
2604 static int
2605 load_none(Unpicklerobject *self)
2607 PDATA_APPEND(self->stack, Py_None, -1);
2608 return 0;
2611 static int
2612 bad_readline(void)
2614 PyErr_SetString(UnpicklingError, "pickle data was truncated");
2615 return -1;
2618 static int
2619 load_int(Unpicklerobject *self)
2621 PyObject *py_int = 0;
2622 char *endptr, *s;
2623 int len, res = -1;
2624 long l;
2626 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2627 if (len < 2) return bad_readline();
2628 if (!( s=pystrndup(s,len))) return -1;
2630 errno = 0;
2631 l = strtol(s, &endptr, 0);
2633 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
2634 /* Hm, maybe we've got something long. Let's try reading
2635 it as a Python long object. */
2636 errno = 0;
2637 py_int = PyLong_FromString(s, NULL, 0);
2638 if (py_int == NULL) {
2639 PyErr_SetString(PyExc_ValueError,
2640 "could not convert string to int");
2641 goto finally;
2644 else {
2645 if (len == 3 && (l == 0 || l == 1)) {
2646 if (!( py_int = PyBool_FromLong(l))) goto finally;
2648 else {
2649 if (!( py_int = PyInt_FromLong(l))) goto finally;
2653 free(s);
2654 PDATA_PUSH(self->stack, py_int, -1);
2655 return 0;
2657 finally:
2658 free(s);
2660 return res;
2664 static long
2665 calc_binint(char *s, int x)
2667 unsigned char c;
2668 int i;
2669 long l;
2671 for (i = 0, l = 0L; i < x; i++) {
2672 c = (unsigned char)s[i];
2673 l |= (long)c << (i * 8);
2675 #if SIZEOF_LONG > 4
2676 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
2677 * is signed, so on a box with longs bigger than 4 bytes we need
2678 * to extend a BININT's sign bit to the full width.
2680 if (x == 4 && l & (1L << 31))
2681 l |= (~0L) << 32;
2682 #endif
2683 return l;
2687 static int
2688 load_binintx(Unpicklerobject *self, char *s, int x)
2690 PyObject *py_int = 0;
2691 long l;
2693 l = calc_binint(s, x);
2695 if (!( py_int = PyInt_FromLong(l)))
2696 return -1;
2698 PDATA_PUSH(self->stack, py_int, -1);
2699 return 0;
2703 static int
2704 load_binint(Unpicklerobject *self)
2706 char *s;
2708 if ((*self->read_func)(self, &s, 4) < 0)
2709 return -1;
2711 return load_binintx(self, s, 4);
2715 static int
2716 load_binint1(Unpicklerobject *self)
2718 char *s;
2720 if ((*self->read_func)(self, &s, 1) < 0)
2721 return -1;
2723 return load_binintx(self, s, 1);
2727 static int
2728 load_binint2(Unpicklerobject *self)
2730 char *s;
2732 if ((*self->read_func)(self, &s, 2) < 0)
2733 return -1;
2735 return load_binintx(self, s, 2);
2738 static int
2739 load_long(Unpicklerobject *self)
2741 PyObject *l = 0;
2742 char *end, *s;
2743 int len, res = -1;
2745 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2746 if (len < 2) return bad_readline();
2747 if (!( s=pystrndup(s,len))) return -1;
2749 if (!( l = PyLong_FromString(s, &end, 0)))
2750 goto finally;
2752 free(s);
2753 PDATA_PUSH(self->stack, l, -1);
2754 return 0;
2756 finally:
2757 free(s);
2759 return res;
2763 static int
2764 load_float(Unpicklerobject *self)
2766 PyObject *py_float = 0;
2767 char *endptr, *s;
2768 int len, res = -1;
2769 double d;
2771 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2772 if (len < 2) return bad_readline();
2773 if (!( s=pystrndup(s,len))) return -1;
2775 errno = 0;
2776 d = strtod(s, &endptr);
2778 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
2779 PyErr_SetString(PyExc_ValueError,
2780 "could not convert string to float");
2781 goto finally;
2784 if (!( py_float = PyFloat_FromDouble(d)))
2785 goto finally;
2787 free(s);
2788 PDATA_PUSH(self->stack, py_float, -1);
2789 return 0;
2791 finally:
2792 free(s);
2794 return res;
2797 static int
2798 load_binfloat(Unpicklerobject *self)
2800 PyObject *py_float = 0;
2801 int s, e;
2802 long fhi, flo;
2803 double x;
2804 char *p;
2806 if ((*self->read_func)(self, &p, 8) < 0)
2807 return -1;
2809 /* First byte */
2810 s = (*p>>7) & 1;
2811 e = (*p & 0x7F) << 4;
2812 p++;
2814 /* Second byte */
2815 e |= (*p>>4) & 0xF;
2816 fhi = (*p & 0xF) << 24;
2817 p++;
2819 /* Third byte */
2820 fhi |= (*p & 0xFF) << 16;
2821 p++;
2823 /* Fourth byte */
2824 fhi |= (*p & 0xFF) << 8;
2825 p++;
2827 /* Fifth byte */
2828 fhi |= *p & 0xFF;
2829 p++;
2831 /* Sixth byte */
2832 flo = (*p & 0xFF) << 16;
2833 p++;
2835 /* Seventh byte */
2836 flo |= (*p & 0xFF) << 8;
2837 p++;
2839 /* Eighth byte */
2840 flo |= *p & 0xFF;
2842 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
2843 x /= 268435456.0; /* 2**28 */
2845 /* XXX This sadly ignores Inf/NaN */
2846 if (e == 0)
2847 e = -1022;
2848 else {
2849 x += 1.0;
2850 e -= 1023;
2852 x = ldexp(x, e);
2854 if (s)
2855 x = -x;
2857 if (!( py_float = PyFloat_FromDouble(x))) return -1;
2859 PDATA_PUSH(self->stack, py_float, -1);
2860 return 0;
2863 static int
2864 load_string(Unpicklerobject *self)
2866 PyObject *str = 0;
2867 int len, res = -1;
2868 char *s, *p;
2870 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2871 if (len < 2) return bad_readline();
2872 if (!( s=pystrndup(s,len))) return -1;
2875 /* Strip outermost quotes */
2876 while (s[len-1] <= ' ')
2877 len--;
2878 if(s[0]=='"' && s[len-1]=='"'){
2879 s[len-1] = '\0';
2880 p = s + 1 ;
2881 len -= 2;
2882 } else if(s[0]=='\'' && s[len-1]=='\''){
2883 s[len-1] = '\0';
2884 p = s + 1 ;
2885 len -= 2;
2886 } else
2887 goto insecure;
2888 /********************************************/
2890 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
2891 if (str) {
2892 PDATA_PUSH(self->stack, str, -1);
2893 res = 0;
2895 free(s);
2896 return res;
2898 insecure:
2899 free(s);
2900 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
2901 return -1;
2905 static int
2906 load_binstring(Unpicklerobject *self)
2908 PyObject *py_string = 0;
2909 long l;
2910 char *s;
2912 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2914 l = calc_binint(s, 4);
2916 if ((*self->read_func)(self, &s, l) < 0)
2917 return -1;
2919 if (!( py_string = PyString_FromStringAndSize(s, l)))
2920 return -1;
2922 PDATA_PUSH(self->stack, py_string, -1);
2923 return 0;
2927 static int
2928 load_short_binstring(Unpicklerobject *self)
2930 PyObject *py_string = 0;
2931 unsigned char l;
2932 char *s;
2934 if ((*self->read_func)(self, &s, 1) < 0)
2935 return -1;
2937 l = (unsigned char)s[0];
2939 if ((*self->read_func)(self, &s, l) < 0) return -1;
2941 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
2943 PDATA_PUSH(self->stack, py_string, -1);
2944 return 0;
2948 #ifdef Py_USING_UNICODE
2949 static int
2950 load_unicode(Unpicklerobject *self)
2952 PyObject *str = 0;
2953 int len, res = -1;
2954 char *s;
2956 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
2957 if (len < 1) return bad_readline();
2959 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
2960 goto finally;
2962 PDATA_PUSH(self->stack, str, -1);
2963 return 0;
2965 finally:
2966 return res;
2968 #endif
2971 #ifdef Py_USING_UNICODE
2972 static int
2973 load_binunicode(Unpicklerobject *self)
2975 PyObject *unicode;
2976 long l;
2977 char *s;
2979 if ((*self->read_func)(self, &s, 4) < 0) return -1;
2981 l = calc_binint(s, 4);
2983 if ((*self->read_func)(self, &s, l) < 0)
2984 return -1;
2986 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
2987 return -1;
2989 PDATA_PUSH(self->stack, unicode, -1);
2990 return 0;
2992 #endif
2995 static int
2996 load_tuple(Unpicklerobject *self)
2998 PyObject *tup;
2999 int i;
3001 if ((i = marker(self)) < 0) return -1;
3002 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3003 PDATA_PUSH(self->stack, tup, -1);
3004 return 0;
3007 static int
3008 load_empty_tuple(Unpicklerobject *self)
3010 PyObject *tup;
3012 if (!( tup=PyTuple_New(0))) return -1;
3013 PDATA_PUSH(self->stack, tup, -1);
3014 return 0;
3017 static int
3018 load_empty_list(Unpicklerobject *self)
3020 PyObject *list;
3022 if (!( list=PyList_New(0))) return -1;
3023 PDATA_PUSH(self->stack, list, -1);
3024 return 0;
3027 static int
3028 load_empty_dict(Unpicklerobject *self)
3030 PyObject *dict;
3032 if (!( dict=PyDict_New())) return -1;
3033 PDATA_PUSH(self->stack, dict, -1);
3034 return 0;
3038 static int
3039 load_list(Unpicklerobject *self)
3041 PyObject *list = 0;
3042 int i;
3044 if ((i = marker(self)) < 0) return -1;
3045 if (!( list=Pdata_popList(self->stack, i))) return -1;
3046 PDATA_PUSH(self->stack, list, -1);
3047 return 0;
3050 static int
3051 load_dict(Unpicklerobject *self)
3053 PyObject *dict, *key, *value;
3054 int i, j, k;
3056 if ((i = marker(self)) < 0) return -1;
3057 j=self->stack->length;
3059 if (!( dict = PyDict_New())) return -1;
3061 for (k = i+1; k < j; k += 2) {
3062 key =self->stack->data[k-1];
3063 value=self->stack->data[k ];
3064 if (PyDict_SetItem(dict, key, value) < 0) {
3065 Py_DECREF(dict);
3066 return -1;
3069 Pdata_clear(self->stack, i);
3070 PDATA_PUSH(self->stack, dict, -1);
3071 return 0;
3074 static PyObject *
3075 Instance_New(PyObject *cls, PyObject *args)
3077 int has_key;
3078 PyObject *safe=0, *r=0;
3080 if (PyClass_Check(cls)) {
3081 int l;
3083 if ((l=PyObject_Size(args)) < 0) goto err;
3084 if (!( l )) {
3085 PyObject *__getinitargs__;
3087 __getinitargs__ = PyObject_GetAttr(cls,
3088 __getinitargs___str);
3089 if (!__getinitargs__) {
3090 /* We have a class with no __getinitargs__,
3091 so bypass usual construction */
3092 PyObject *inst;
3094 PyErr_Clear();
3095 if (!( inst=PyInstance_NewRaw(cls, NULL)))
3096 goto err;
3097 return inst;
3099 Py_DECREF(__getinitargs__);
3102 if ((r=PyInstance_New(cls, args, NULL))) return r;
3103 else goto err;
3106 /* Is safe_constructors always a dict? */
3107 has_key = cPickle_PyMapping_HasKey(safe_constructors, cls);
3108 if (!has_key) {
3109 safe = PyObject_GetAttr(cls, __safe_for_unpickling___str);
3110 if (!safe ||
3111 !PyObject_IsTrue(safe)) {
3112 cPickle_ErrFormat(UnpicklingError,
3113 "%s is not safe for unpickling",
3114 "O", cls);
3115 Py_XDECREF(safe);
3116 return NULL;
3120 if (args==Py_None) {
3121 /* Special case, call cls.__basicnew__() */
3122 PyObject *basicnew;
3124 basicnew = PyObject_GetAttr(cls, __basicnew___str);
3125 if (!basicnew) return NULL;
3126 r=PyObject_CallObject(basicnew, NULL);
3127 Py_DECREF(basicnew);
3128 if (r) return r;
3131 if ((r=PyObject_CallObject(cls, args))) return r;
3133 err:
3135 PyObject *tp, *v, *tb;
3137 PyErr_Fetch(&tp, &v, &tb);
3138 if ((r=Py_BuildValue("OOO",v,cls,args))) {
3139 Py_XDECREF(v);
3140 v=r;
3142 PyErr_Restore(tp,v,tb);
3144 return NULL;
3148 static int
3149 load_obj(Unpicklerobject *self)
3151 PyObject *class, *tup, *obj=0;
3152 int i;
3154 if ((i = marker(self)) < 0) return -1;
3155 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3156 PDATA_POP(self->stack, class);
3157 if (class) {
3158 obj = Instance_New(class, tup);
3159 Py_DECREF(class);
3161 Py_DECREF(tup);
3163 if (! obj) return -1;
3164 PDATA_PUSH(self->stack, obj, -1);
3165 return 0;
3169 static int
3170 load_inst(Unpicklerobject *self)
3172 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3173 int i, len;
3174 char *s;
3176 if ((i = marker(self)) < 0) return -1;
3178 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
3179 if (len < 2) return bad_readline();
3180 module_name = PyString_FromStringAndSize(s, len - 1);
3181 if (!module_name) return -1;
3183 if ((len = (*self->readline_func)(self, &s)) >= 0) {
3184 if (len < 2) return bad_readline();
3185 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3186 class = find_class(module_name, class_name,
3187 self->find_class);
3188 Py_DECREF(class_name);
3191 Py_DECREF(module_name);
3193 if (! class) return -1;
3195 if ((tup=Pdata_popTuple(self->stack, i))) {
3196 obj = Instance_New(class, tup);
3197 Py_DECREF(tup);
3199 Py_DECREF(class);
3201 if (! obj) return -1;
3203 PDATA_PUSH(self->stack, obj, -1);
3204 return 0;
3208 static int
3209 load_global(Unpicklerobject *self)
3211 PyObject *class = 0, *module_name = 0, *class_name = 0;
3212 int len;
3213 char *s;
3215 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
3216 if (len < 2) return bad_readline();
3217 module_name = PyString_FromStringAndSize(s, len - 1);
3218 if (!module_name) return -1;
3220 if ((len = (*self->readline_func)(self, &s)) >= 0) {
3221 if (len < 2) return bad_readline();
3222 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3223 class = find_class(module_name, class_name,
3224 self->find_class);
3225 Py_DECREF(class_name);
3228 Py_DECREF(module_name);
3230 if (! class) return -1;
3231 PDATA_PUSH(self->stack, class, -1);
3232 return 0;
3236 static int
3237 load_persid(Unpicklerobject *self)
3239 PyObject *pid = 0;
3240 int len;
3241 char *s;
3243 if (self->pers_func) {
3244 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
3245 if (len < 2) return bad_readline();
3247 pid = PyString_FromStringAndSize(s, len - 1);
3248 if (!pid) return -1;
3250 if (PyList_Check(self->pers_func)) {
3251 if (PyList_Append(self->pers_func, pid) < 0) {
3252 Py_DECREF(pid);
3253 return -1;
3256 else {
3257 ARG_TUP(self, pid);
3258 if (self->arg) {
3259 pid = PyObject_Call(self->pers_func, self->arg,
3260 NULL);
3261 FREE_ARG_TUP(self);
3265 if (! pid) return -1;
3267 PDATA_PUSH(self->stack, pid, -1);
3268 return 0;
3270 else {
3271 PyErr_SetString(UnpicklingError,
3272 "A load persistent id instruction was encountered,\n"
3273 "but no persistent_load function was specified.");
3274 return -1;
3278 static int
3279 load_binpersid(Unpicklerobject *self)
3281 PyObject *pid = 0;
3283 if (self->pers_func) {
3284 PDATA_POP(self->stack, pid);
3285 if (! pid) return -1;
3287 if (PyList_Check(self->pers_func)) {
3288 if (PyList_Append(self->pers_func, pid) < 0) {
3289 Py_DECREF(pid);
3290 return -1;
3293 else {
3294 ARG_TUP(self, pid);
3295 if (self->arg) {
3296 pid = PyObject_Call(self->pers_func, self->arg,
3297 NULL);
3298 FREE_ARG_TUP(self);
3300 if (! pid) return -1;
3303 PDATA_PUSH(self->stack, pid, -1);
3304 return 0;
3306 else {
3307 PyErr_SetString(UnpicklingError,
3308 "A load persistent id instruction was encountered,\n"
3309 "but no persistent_load function was specified.");
3310 return -1;
3315 static int
3316 load_pop(Unpicklerobject *self)
3318 int len;
3320 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3322 /* Note that we split the (pickle.py) stack into two stacks,
3323 an object stack and a mark stack. We have to be clever and
3324 pop the right one. We do this by looking at the top of the
3325 mark stack.
3328 if ((self->num_marks > 0) &&
3329 (self->marks[self->num_marks - 1] == len))
3330 self->num_marks--;
3331 else {
3332 len--;
3333 Py_DECREF(self->stack->data[len]);
3334 self->stack->length=len;
3337 return 0;
3341 static int
3342 load_pop_mark(Unpicklerobject *self)
3344 int i;
3346 if ((i = marker(self)) < 0)
3347 return -1;
3349 Pdata_clear(self->stack, i);
3351 return 0;
3355 static int
3356 load_dup(Unpicklerobject *self)
3358 PyObject *last;
3359 int len;
3361 if ((len = self->stack->length) <= 0) return stackUnderflow();
3362 last=self->stack->data[len-1];
3363 Py_INCREF(last);
3364 PDATA_PUSH(self->stack, last, -1);
3365 return 0;
3369 static int
3370 load_get(Unpicklerobject *self)
3372 PyObject *py_str = 0, *value = 0;
3373 int len;
3374 char *s;
3375 int rc;
3377 if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
3378 if (len < 2) return bad_readline();
3380 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
3382 value = PyDict_GetItem(self->memo, py_str);
3383 if (! value) {
3384 PyErr_SetObject(BadPickleGet, py_str);
3385 rc = -1;
3386 } else {
3387 PDATA_APPEND(self->stack, value, -1);
3388 rc = 0;
3391 Py_DECREF(py_str);
3392 return rc;
3396 static int
3397 load_binget(Unpicklerobject *self)
3399 PyObject *py_key = 0, *value = 0;
3400 unsigned char key;
3401 char *s;
3402 int rc;
3404 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3406 key = (unsigned char)s[0];
3407 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3409 value = PyDict_GetItem(self->memo, py_key);
3410 if (! value) {
3411 PyErr_SetObject(BadPickleGet, py_key);
3412 rc = -1;
3413 } else {
3414 PDATA_APPEND(self->stack, value, -1);
3415 rc = 0;
3418 Py_DECREF(py_key);
3419 return rc;
3423 static int
3424 load_long_binget(Unpicklerobject *self)
3426 PyObject *py_key = 0, *value = 0;
3427 unsigned char c;
3428 char *s;
3429 long key;
3430 int rc;
3432 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3434 c = (unsigned char)s[0];
3435 key = (long)c;
3436 c = (unsigned char)s[1];
3437 key |= (long)c << 8;
3438 c = (unsigned char)s[2];
3439 key |= (long)c << 16;
3440 c = (unsigned char)s[3];
3441 key |= (long)c << 24;
3443 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3445 value = PyDict_GetItem(self->memo, py_key);
3446 if (! value) {
3447 PyErr_SetObject(BadPickleGet, py_key);
3448 rc = -1;
3449 } else {
3450 PDATA_APPEND(self->stack, value, -1);
3451 rc = 0;
3454 Py_DECREF(py_key);
3455 return rc;
3459 static int
3460 load_put(Unpicklerobject *self)
3462 PyObject *py_str = 0, *value = 0;
3463 int len, l;
3464 char *s;
3466 if ((l = (*self->readline_func)(self, &s)) < 0) return -1;
3467 if (l < 2) return bad_readline();
3468 if (!( len=self->stack->length )) return stackUnderflow();
3469 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
3470 value=self->stack->data[len-1];
3471 l=PyDict_SetItem(self->memo, py_str, value);
3472 Py_DECREF(py_str);
3473 return l;
3477 static int
3478 load_binput(Unpicklerobject *self)
3480 PyObject *py_key = 0, *value = 0;
3481 unsigned char key;
3482 char *s;
3483 int len;
3485 if ((*self->read_func)(self, &s, 1) < 0) return -1;
3486 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3488 key = (unsigned char)s[0];
3490 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3491 value=self->stack->data[len-1];
3492 len=PyDict_SetItem(self->memo, py_key, value);
3493 Py_DECREF(py_key);
3494 return len;
3498 static int
3499 load_long_binput(Unpicklerobject *self)
3501 PyObject *py_key = 0, *value = 0;
3502 long key;
3503 unsigned char c;
3504 char *s;
3505 int len;
3507 if ((*self->read_func)(self, &s, 4) < 0) return -1;
3508 if (!( len=self->stack->length )) return stackUnderflow();
3510 c = (unsigned char)s[0];
3511 key = (long)c;
3512 c = (unsigned char)s[1];
3513 key |= (long)c << 8;
3514 c = (unsigned char)s[2];
3515 key |= (long)c << 16;
3516 c = (unsigned char)s[3];
3517 key |= (long)c << 24;
3519 if (!( py_key = PyInt_FromLong(key))) return -1;
3520 value=self->stack->data[len-1];
3521 len=PyDict_SetItem(self->memo, py_key, value);
3522 Py_DECREF(py_key);
3523 return len;
3527 static int
3528 do_append(Unpicklerobject *self, int x)
3530 PyObject *value = 0, *list = 0, *append_method = 0;
3531 int len, i;
3533 len=self->stack->length;
3534 if (!( len >= x && x > 0 )) return stackUnderflow();
3535 /* nothing to do */
3536 if (len==x) return 0;
3538 list=self->stack->data[x-1];
3540 if (PyList_Check(list)) {
3541 PyObject *slice;
3542 int list_len;
3544 slice=Pdata_popList(self->stack, x);
3545 list_len = PyList_GET_SIZE(list);
3546 i=PyList_SetSlice(list, list_len, list_len, slice);
3547 Py_DECREF(slice);
3548 return i;
3550 else {
3552 if (!( append_method = PyObject_GetAttr(list, append_str)))
3553 return -1;
3555 for (i = x; i < len; i++) {
3556 PyObject *junk;
3558 value=self->stack->data[i];
3559 junk=0;
3560 ARG_TUP(self, value);
3561 if (self->arg) {
3562 junk = PyObject_Call(append_method, self->arg,
3563 NULL);
3564 FREE_ARG_TUP(self);
3566 if (! junk) {
3567 Pdata_clear(self->stack, i+1);
3568 self->stack->length=x;
3569 Py_DECREF(append_method);
3570 return -1;
3572 Py_DECREF(junk);
3574 self->stack->length=x;
3575 Py_DECREF(append_method);
3578 return 0;
3582 static int
3583 load_append(Unpicklerobject *self)
3585 return do_append(self, self->stack->length - 1);
3589 static int
3590 load_appends(Unpicklerobject *self)
3592 return do_append(self, marker(self));
3596 static int
3597 do_setitems(Unpicklerobject *self, int x)
3599 PyObject *value = 0, *key = 0, *dict = 0;
3600 int len, i, r=0;
3602 if (!( (len=self->stack->length) >= x
3603 && x > 0 )) return stackUnderflow();
3605 dict=self->stack->data[x-1];
3607 for (i = x+1; i < len; i += 2) {
3608 key =self->stack->data[i-1];
3609 value=self->stack->data[i ];
3610 if (PyObject_SetItem(dict, key, value) < 0) {
3611 r=-1;
3612 break;
3616 Pdata_clear(self->stack, x);
3618 return r;
3622 static int
3623 load_setitem(Unpicklerobject *self)
3625 return do_setitems(self, self->stack->length - 2);
3628 static int
3629 load_setitems(Unpicklerobject *self)
3631 return do_setitems(self, marker(self));
3635 static int
3636 load_build(Unpicklerobject *self)
3638 PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0,
3639 *junk = 0, *__setstate__ = 0;
3640 int i, r = 0;
3642 if (self->stack->length < 2) return stackUnderflow();
3643 PDATA_POP(self->stack, value);
3644 if (! value) return -1;
3645 inst=self->stack->data[self->stack->length-1];
3647 if ((__setstate__ = PyObject_GetAttr(inst, __setstate___str))) {
3648 ARG_TUP(self, value);
3649 if (self->arg) {
3650 junk = PyObject_Call(__setstate__, self->arg, NULL);
3651 FREE_ARG_TUP(self);
3653 Py_DECREF(__setstate__);
3654 if (! junk) return -1;
3655 Py_DECREF(junk);
3656 return 0;
3659 PyErr_Clear();
3660 if ((instdict = PyObject_GetAttr(inst, __dict___str))) {
3661 i = 0;
3662 while (PyDict_Next(value, &i, &d_key, &d_value)) {
3663 if (PyObject_SetItem(instdict, d_key, d_value) < 0) {
3664 r=-1;
3665 break;
3668 Py_DECREF(instdict);
3670 else r=-1;
3672 Py_XDECREF(value);
3674 return r;
3678 static int
3679 load_mark(Unpicklerobject *self)
3681 int s;
3683 /* Note that we split the (pickle.py) stack into two stacks, an
3684 object stack and a mark stack. Here we push a mark onto the
3685 mark stack.
3688 if ((self->num_marks + 1) >= self->marks_size) {
3689 s=self->marks_size+20;
3690 if (s <= self->num_marks) s=self->num_marks + 1;
3691 if (self->marks == NULL)
3692 self->marks=(int *)malloc(s * sizeof(int));
3693 else
3694 self->marks=(int *)realloc(self->marks,
3695 s * sizeof(int));
3696 if (! self->marks) {
3697 PyErr_NoMemory();
3698 return -1;
3700 self->marks_size = s;
3703 self->marks[self->num_marks++] = self->stack->length;
3705 return 0;
3708 static int
3709 load_reduce(Unpicklerobject *self)
3711 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
3713 PDATA_POP(self->stack, arg_tup);
3714 if (! arg_tup) return -1;
3715 PDATA_POP(self->stack, callable);
3716 if (callable) {
3717 ob = Instance_New(callable, arg_tup);
3718 Py_DECREF(callable);
3720 Py_DECREF(arg_tup);
3722 if (! ob) return -1;
3724 PDATA_PUSH(self->stack, ob, -1);
3725 return 0;
3728 static PyObject *
3729 load(Unpicklerobject *self)
3731 PyObject *err = 0, *val = 0;
3732 char *s;
3734 self->num_marks = 0;
3735 if (self->stack->length) Pdata_clear(self->stack, 0);
3737 while (1) {
3738 if ((*self->read_func)(self, &s, 1) < 0)
3739 break;
3741 switch (s[0]) {
3742 case NONE:
3743 if (load_none(self) < 0)
3744 break;
3745 continue;
3747 case BININT:
3748 if (load_binint(self) < 0)
3749 break;
3750 continue;
3752 case BININT1:
3753 if (load_binint1(self) < 0)
3754 break;
3755 continue;
3757 case BININT2:
3758 if (load_binint2(self) < 0)
3759 break;
3760 continue;
3762 case INT:
3763 if (load_int(self) < 0)
3764 break;
3765 continue;
3767 case LONG:
3768 if (load_long(self) < 0)
3769 break;
3770 continue;
3772 case FLOAT:
3773 if (load_float(self) < 0)
3774 break;
3775 continue;
3777 case BINFLOAT:
3778 if (load_binfloat(self) < 0)
3779 break;
3780 continue;
3782 case BINSTRING:
3783 if (load_binstring(self) < 0)
3784 break;
3785 continue;
3787 case SHORT_BINSTRING:
3788 if (load_short_binstring(self) < 0)
3789 break;
3790 continue;
3792 case STRING:
3793 if (load_string(self) < 0)
3794 break;
3795 continue;
3797 #ifdef Py_USING_UNICODE
3798 case UNICODE:
3799 if (load_unicode(self) < 0)
3800 break;
3801 continue;
3803 case BINUNICODE:
3804 if (load_binunicode(self) < 0)
3805 break;
3806 continue;
3807 #endif
3809 case EMPTY_TUPLE:
3810 if (load_empty_tuple(self) < 0)
3811 break;
3812 continue;
3814 case TUPLE:
3815 if (load_tuple(self) < 0)
3816 break;
3817 continue;
3819 case EMPTY_LIST:
3820 if (load_empty_list(self) < 0)
3821 break;
3822 continue;
3824 case LIST:
3825 if (load_list(self) < 0)
3826 break;
3827 continue;
3829 case EMPTY_DICT:
3830 if (load_empty_dict(self) < 0)
3831 break;
3832 continue;
3834 case DICT:
3835 if (load_dict(self) < 0)
3836 break;
3837 continue;
3839 case OBJ:
3840 if (load_obj(self) < 0)
3841 break;
3842 continue;
3844 case INST:
3845 if (load_inst(self) < 0)
3846 break;
3847 continue;
3849 case GLOBAL:
3850 if (load_global(self) < 0)
3851 break;
3852 continue;
3854 case APPEND:
3855 if (load_append(self) < 0)
3856 break;
3857 continue;
3859 case APPENDS:
3860 if (load_appends(self) < 0)
3861 break;
3862 continue;
3864 case BUILD:
3865 if (load_build(self) < 0)
3866 break;
3867 continue;
3869 case DUP:
3870 if (load_dup(self) < 0)
3871 break;
3872 continue;
3874 case BINGET:
3875 if (load_binget(self) < 0)
3876 break;
3877 continue;
3879 case LONG_BINGET:
3880 if (load_long_binget(self) < 0)
3881 break;
3882 continue;
3884 case GET:
3885 if (load_get(self) < 0)
3886 break;
3887 continue;
3889 case MARK:
3890 if (load_mark(self) < 0)
3891 break;
3892 continue;
3894 case BINPUT:
3895 if (load_binput(self) < 0)
3896 break;
3897 continue;
3899 case LONG_BINPUT:
3900 if (load_long_binput(self) < 0)
3901 break;
3902 continue;
3904 case PUT:
3905 if (load_put(self) < 0)
3906 break;
3907 continue;
3909 case POP:
3910 if (load_pop(self) < 0)
3911 break;
3912 continue;
3914 case POP_MARK:
3915 if (load_pop_mark(self) < 0)
3916 break;
3917 continue;
3919 case SETITEM:
3920 if (load_setitem(self) < 0)
3921 break;
3922 continue;
3924 case SETITEMS:
3925 if (load_setitems(self) < 0)
3926 break;
3927 continue;
3929 case STOP:
3930 break;
3932 case PERSID:
3933 if (load_persid(self) < 0)
3934 break;
3935 continue;
3937 case BINPERSID:
3938 if (load_binpersid(self) < 0)
3939 break;
3940 continue;
3942 case REDUCE:
3943 if (load_reduce(self) < 0)
3944 break;
3945 continue;
3947 case '\0':
3948 /* end of file */
3949 PyErr_SetNone(PyExc_EOFError);
3950 break;
3952 default:
3953 cPickle_ErrFormat(UnpicklingError,
3954 "invalid load key, '%s'.",
3955 "c", s[0]);
3956 return NULL;
3959 break;
3962 if ((err = PyErr_Occurred())) {
3963 if (err == PyExc_EOFError) {
3964 PyErr_SetNone(PyExc_EOFError);
3966 return NULL;
3969 PDATA_POP(self->stack, val);
3970 return val;
3974 /* No-load functions to support noload, which is used to
3975 find persistent references. */
3977 static int
3978 noload_obj(Unpicklerobject *self)
3980 int i;
3982 if ((i = marker(self)) < 0) return -1;
3983 return Pdata_clear(self->stack, i+1);
3987 static int
3988 noload_inst(Unpicklerobject *self)
3990 int i;
3991 char *s;
3993 if ((i = marker(self)) < 0) return -1;
3994 Pdata_clear(self->stack, i);
3995 if ((*self->readline_func)(self, &s) < 0) return -1;
3996 if ((*self->readline_func)(self, &s) < 0) return -1;
3997 PDATA_APPEND(self->stack, Py_None,-1);
3998 return 0;
4001 static int
4002 noload_global(Unpicklerobject *self)
4004 char *s;
4006 if ((*self->readline_func)(self, &s) < 0) return -1;
4007 if ((*self->readline_func)(self, &s) < 0) return -1;
4008 PDATA_APPEND(self->stack, Py_None,-1);
4009 return 0;
4012 static int
4013 noload_reduce(Unpicklerobject *self)
4016 if (self->stack->length < 2) return stackUnderflow();
4017 Pdata_clear(self->stack, self->stack->length-2);
4018 PDATA_APPEND(self->stack, Py_None,-1);
4019 return 0;
4022 static int
4023 noload_build(Unpicklerobject *self) {
4025 if (self->stack->length < 1) return stackUnderflow();
4026 Pdata_clear(self->stack, self->stack->length-1);
4027 return 0;
4031 static PyObject *
4032 noload(Unpicklerobject *self)
4034 PyObject *err = 0, *val = 0;
4035 char *s;
4037 self->num_marks = 0;
4038 Pdata_clear(self->stack, 0);
4040 while (1) {
4041 if ((*self->read_func)(self, &s, 1) < 0)
4042 break;
4044 switch (s[0]) {
4045 case NONE:
4046 if (load_none(self) < 0)
4047 break;
4048 continue;
4050 case BININT:
4051 if (load_binint(self) < 0)
4052 break;
4053 continue;
4055 case BININT1:
4056 if (load_binint1(self) < 0)
4057 break;
4058 continue;
4060 case BININT2:
4061 if (load_binint2(self) < 0)
4062 break;
4063 continue;
4065 case INT:
4066 if (load_int(self) < 0)
4067 break;
4068 continue;
4070 case LONG:
4071 if (load_long(self) < 0)
4072 break;
4073 continue;
4075 case FLOAT:
4076 if (load_float(self) < 0)
4077 break;
4078 continue;
4080 case BINFLOAT:
4081 if (load_binfloat(self) < 0)
4082 break;
4083 continue;
4085 case BINSTRING:
4086 if (load_binstring(self) < 0)
4087 break;
4088 continue;
4090 case SHORT_BINSTRING:
4091 if (load_short_binstring(self) < 0)
4092 break;
4093 continue;
4095 case STRING:
4096 if (load_string(self) < 0)
4097 break;
4098 continue;
4100 #ifdef Py_USING_UNICODE
4101 case UNICODE:
4102 if (load_unicode(self) < 0)
4103 break;
4104 continue;
4106 case BINUNICODE:
4107 if (load_binunicode(self) < 0)
4108 break;
4109 continue;
4110 #endif
4112 case EMPTY_TUPLE:
4113 if (load_empty_tuple(self) < 0)
4114 break;
4115 continue;
4117 case TUPLE:
4118 if (load_tuple(self) < 0)
4119 break;
4120 continue;
4122 case EMPTY_LIST:
4123 if (load_empty_list(self) < 0)
4124 break;
4125 continue;
4127 case LIST:
4128 if (load_list(self) < 0)
4129 break;
4130 continue;
4132 case EMPTY_DICT:
4133 if (load_empty_dict(self) < 0)
4134 break;
4135 continue;
4137 case DICT:
4138 if (load_dict(self) < 0)
4139 break;
4140 continue;
4142 case OBJ:
4143 if (noload_obj(self) < 0)
4144 break;
4145 continue;
4147 case INST:
4148 if (noload_inst(self) < 0)
4149 break;
4150 continue;
4152 case GLOBAL:
4153 if (noload_global(self) < 0)
4154 break;
4155 continue;
4157 case APPEND:
4158 if (load_append(self) < 0)
4159 break;
4160 continue;
4162 case APPENDS:
4163 if (load_appends(self) < 0)
4164 break;
4165 continue;
4167 case BUILD:
4168 if (noload_build(self) < 0)
4169 break;
4170 continue;
4172 case DUP:
4173 if (load_dup(self) < 0)
4174 break;
4175 continue;
4177 case BINGET:
4178 if (load_binget(self) < 0)
4179 break;
4180 continue;
4182 case LONG_BINGET:
4183 if (load_long_binget(self) < 0)
4184 break;
4185 continue;
4187 case GET:
4188 if (load_get(self) < 0)
4189 break;
4190 continue;
4192 case MARK:
4193 if (load_mark(self) < 0)
4194 break;
4195 continue;
4197 case BINPUT:
4198 if (load_binput(self) < 0)
4199 break;
4200 continue;
4202 case LONG_BINPUT:
4203 if (load_long_binput(self) < 0)
4204 break;
4205 continue;
4207 case PUT:
4208 if (load_put(self) < 0)
4209 break;
4210 continue;
4212 case POP:
4213 if (load_pop(self) < 0)
4214 break;
4215 continue;
4217 case POP_MARK:
4218 if (load_pop_mark(self) < 0)
4219 break;
4220 continue;
4222 case SETITEM:
4223 if (load_setitem(self) < 0)
4224 break;
4225 continue;
4227 case SETITEMS:
4228 if (load_setitems(self) < 0)
4229 break;
4230 continue;
4232 case STOP:
4233 break;
4235 case PERSID:
4236 if (load_persid(self) < 0)
4237 break;
4238 continue;
4240 case BINPERSID:
4241 if (load_binpersid(self) < 0)
4242 break;
4243 continue;
4245 case REDUCE:
4246 if (noload_reduce(self) < 0)
4247 break;
4248 continue;
4250 default:
4251 cPickle_ErrFormat(UnpicklingError,
4252 "invalid load key, '%s'.",
4253 "c", s[0]);
4254 return NULL;
4257 break;
4260 if ((err = PyErr_Occurred())) {
4261 if (err == PyExc_EOFError) {
4262 PyErr_SetNone(PyExc_EOFError);
4264 return NULL;
4267 PDATA_POP(self->stack, val);
4268 return val;
4272 static PyObject *
4273 Unpickler_load(Unpicklerobject *self, PyObject *args)
4275 if (!( PyArg_ParseTuple(args, ":load")))
4276 return NULL;
4278 return load(self);
4281 static PyObject *
4282 Unpickler_noload(Unpicklerobject *self, PyObject *args)
4284 if (!( PyArg_ParseTuple(args, ":noload")))
4285 return NULL;
4287 return noload(self);
4291 static struct PyMethodDef Unpickler_methods[] = {
4292 {"load", (PyCFunction)Unpickler_load, METH_VARARGS,
4293 PyDoc_STR("load() -- Load a pickle")
4295 {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS,
4296 PyDoc_STR(
4297 "noload() -- not load a pickle, but go through most of the motions\n"
4298 "\n"
4299 "This function can be used to read past a pickle without instantiating\n"
4300 "any objects or importing any modules. It can also be used to find all\n"
4301 "persistent references without instantiating any objects or importing\n"
4302 "any modules.\n")
4304 {NULL, NULL} /* sentinel */
4308 static Unpicklerobject *
4309 newUnpicklerobject(PyObject *f)
4311 Unpicklerobject *self;
4313 if (!( self = PyObject_New(Unpicklerobject, &Unpicklertype)))
4314 return NULL;
4316 self->file = NULL;
4317 self->arg = NULL;
4318 self->stack = (Pdata*)Pdata_New();
4319 self->pers_func = NULL;
4320 self->last_string = NULL;
4321 self->marks = NULL;
4322 self->num_marks = 0;
4323 self->marks_size = 0;
4324 self->buf_size = 0;
4325 self->read = NULL;
4326 self->readline = NULL;
4327 self->safe_constructors = NULL;
4328 self->find_class = NULL;
4330 if (!( self->memo = PyDict_New()))
4331 goto err;
4333 Py_INCREF(f);
4334 self->file = f;
4336 /* Set read, readline based on type of f */
4337 if (PyFile_Check(f)) {
4338 self->fp = PyFile_AsFile(f);
4339 if (self->fp == NULL) {
4340 PyErr_SetString(PyExc_ValueError,
4341 "I/O operation on closed file");
4342 goto err;
4344 self->read_func = read_file;
4345 self->readline_func = readline_file;
4347 else if (PycStringIO_InputCheck(f)) {
4348 self->fp = NULL;
4349 self->read_func = read_cStringIO;
4350 self->readline_func = readline_cStringIO;
4352 else {
4354 self->fp = NULL;
4355 self->read_func = read_other;
4356 self->readline_func = readline_other;
4358 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
4359 (self->read = PyObject_GetAttr(f, read_str)))) {
4360 PyErr_Clear();
4361 PyErr_SetString( PyExc_TypeError,
4362 "argument must have 'read' and "
4363 "'readline' attributes" );
4364 goto err;
4368 if (PyEval_GetRestricted()) {
4369 /* Restricted execution, get private tables */
4370 PyObject *m;
4372 if (!( m=PyImport_Import(copy_reg_str))) goto err;
4373 self->safe_constructors=PyObject_GetAttr(m,
4374 safe_constructors_str);
4375 Py_DECREF(m);
4376 if (!( self->safe_constructors )) goto err;
4378 else {
4379 self->safe_constructors=safe_constructors;
4380 Py_INCREF(safe_constructors);
4383 return self;
4385 err:
4386 Py_DECREF((PyObject *)self);
4387 return NULL;
4391 static PyObject *
4392 get_Unpickler(PyObject *self, PyObject *args)
4394 PyObject *file;
4396 if (!( PyArg_ParseTuple(args, "O:Unpickler", &file)))
4397 return NULL;
4398 return (PyObject *)newUnpicklerobject(file);
4402 static void
4403 Unpickler_dealloc(Unpicklerobject *self)
4405 Py_XDECREF(self->readline);
4406 Py_XDECREF(self->read);
4407 Py_XDECREF(self->file);
4408 Py_XDECREF(self->memo);
4409 Py_XDECREF(self->stack);
4410 Py_XDECREF(self->pers_func);
4411 Py_XDECREF(self->arg);
4412 Py_XDECREF(self->last_string);
4413 Py_XDECREF(self->safe_constructors);
4415 if (self->marks) {
4416 free(self->marks);
4419 if (self->buf_size) {
4420 free(self->buf);
4423 PyObject_Del(self);
4427 static PyObject *
4428 Unpickler_getattr(Unpicklerobject *self, char *name)
4430 if (!strcmp(name, "persistent_load")) {
4431 if (!self->pers_func) {
4432 PyErr_SetString(PyExc_AttributeError, name);
4433 return NULL;
4436 Py_INCREF(self->pers_func);
4437 return self->pers_func;
4440 if (!strcmp(name, "find_global")) {
4441 if (!self->find_class) {
4442 PyErr_SetString(PyExc_AttributeError, name);
4443 return NULL;
4446 Py_INCREF(self->find_class);
4447 return self->find_class;
4450 if (!strcmp(name, "memo")) {
4451 if (!self->memo) {
4452 PyErr_SetString(PyExc_AttributeError, name);
4453 return NULL;
4456 Py_INCREF(self->memo);
4457 return self->memo;
4460 if (!strcmp(name, "UnpicklingError")) {
4461 Py_INCREF(UnpicklingError);
4462 return UnpicklingError;
4465 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
4469 static int
4470 Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
4473 if (!strcmp(name, "persistent_load")) {
4474 Py_XDECREF(self->pers_func);
4475 self->pers_func = value;
4476 Py_XINCREF(value);
4477 return 0;
4480 if (!strcmp(name, "find_global")) {
4481 Py_XDECREF(self->find_class);
4482 self->find_class = value;
4483 Py_XINCREF(value);
4484 return 0;
4487 if (! value) {
4488 PyErr_SetString(PyExc_TypeError,
4489 "attribute deletion is not supported");
4490 return -1;
4493 if (strcmp(name, "memo") == 0) {
4494 if (!PyDict_Check(value)) {
4495 PyErr_SetString(PyExc_TypeError,
4496 "memo must be a dictionary");
4497 return -1;
4499 Py_XDECREF(self->memo);
4500 self->memo = value;
4501 Py_INCREF(value);
4502 return 0;
4505 PyErr_SetString(PyExc_AttributeError, name);
4506 return -1;
4510 static PyObject *
4511 cpm_dump(PyObject *self, PyObject *args)
4513 PyObject *ob, *file, *res = NULL;
4514 Picklerobject *pickler = 0;
4515 int bin = 0;
4517 if (!( PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin)))
4518 goto finally;
4520 if (!( pickler = newPicklerobject(file, bin)))
4521 goto finally;
4523 if (dump(pickler, ob) < 0)
4524 goto finally;
4526 Py_INCREF(Py_None);
4527 res = Py_None;
4529 finally:
4530 Py_XDECREF(pickler);
4532 return res;
4536 static PyObject *
4537 cpm_dumps(PyObject *self, PyObject *args)
4539 PyObject *ob, *file = 0, *res = NULL;
4540 Picklerobject *pickler = 0;
4541 int bin = 0;
4543 if (!( PyArg_ParseTuple(args, "O|i:dumps", &ob, &bin)))
4544 goto finally;
4546 if (!( file = PycStringIO->NewOutput(128)))
4547 goto finally;
4549 if (!( pickler = newPicklerobject(file, bin)))
4550 goto finally;
4552 if (dump(pickler, ob) < 0)
4553 goto finally;
4555 res = PycStringIO->cgetvalue(file);
4557 finally:
4558 Py_XDECREF(pickler);
4559 Py_XDECREF(file);
4561 return res;
4565 static PyObject *
4566 cpm_load(PyObject *self, PyObject *args)
4568 Unpicklerobject *unpickler = 0;
4569 PyObject *ob, *res = NULL;
4571 if (!( PyArg_ParseTuple(args, "O:load", &ob)))
4572 goto finally;
4574 if (!( unpickler = newUnpicklerobject(ob)))
4575 goto finally;
4577 res = load(unpickler);
4579 finally:
4580 Py_XDECREF(unpickler);
4582 return res;
4586 static PyObject *
4587 cpm_loads(PyObject *self, PyObject *args)
4589 PyObject *ob, *file = 0, *res = NULL;
4590 Unpicklerobject *unpickler = 0;
4592 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
4593 goto finally;
4595 if (!( file = PycStringIO->NewInput(ob)))
4596 goto finally;
4598 if (!( unpickler = newUnpicklerobject(file)))
4599 goto finally;
4601 res = load(unpickler);
4603 finally:
4604 Py_XDECREF(file);
4605 Py_XDECREF(unpickler);
4607 return res;
4611 PyDoc_STRVAR(Unpicklertype__doc__,
4612 "Objects that know how to unpickle");
4614 static PyTypeObject Unpicklertype = {
4615 PyObject_HEAD_INIT(NULL)
4616 0, /*ob_size*/
4617 "cPickle.Unpickler", /*tp_name*/
4618 sizeof(Unpicklerobject), /*tp_basicsize*/
4619 0, /*tp_itemsize*/
4620 /* methods */
4621 (destructor)Unpickler_dealloc, /*tp_dealloc*/
4622 (printfunc)0, /*tp_print*/
4623 (getattrfunc)Unpickler_getattr, /*tp_getattr*/
4624 (setattrfunc)Unpickler_setattr, /*tp_setattr*/
4625 (cmpfunc)0, /*tp_compare*/
4626 (reprfunc)0, /*tp_repr*/
4627 0, /*tp_as_number*/
4628 0, /*tp_as_sequence*/
4629 0, /*tp_as_mapping*/
4630 (hashfunc)0, /*tp_hash*/
4631 (ternaryfunc)0, /*tp_call*/
4632 (reprfunc)0, /*tp_str*/
4634 /* Space for future expansion */
4635 0L,0L,0L,0L,
4636 Unpicklertype__doc__ /* Documentation string */
4639 static struct PyMethodDef cPickle_methods[] = {
4640 {"dump", (PyCFunction)cpm_dump, METH_VARARGS,
4641 PyDoc_STR("dump(object, file, [binary]) --"
4642 "Write an object in pickle format to the given file\n"
4643 "\n"
4644 "If the optional argument, binary, is provided and is true, then the\n"
4645 "pickle will be written in binary format, which is more space and\n"
4646 "computationally efficient. \n")
4648 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS,
4649 PyDoc_STR("dumps(object, [binary]) --"
4650 "Return a string containing an object in pickle format\n"
4651 "\n"
4652 "If the optional argument, binary, is provided and is true, then the\n"
4653 "pickle will be written in binary format, which is more space and\n"
4654 "computationally efficient. \n")
4656 {"load", (PyCFunction)cpm_load, METH_VARARGS,
4657 PyDoc_STR("load(file) -- Load a pickle from the given file")},
4658 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
4659 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
4660 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS,
4661 PyDoc_STR("Pickler(file, [binary]) -- Create a pickler\n"
4662 "\n"
4663 "If the optional argument, binary, is provided and is true, then\n"
4664 "pickles will be written in binary format, which is more space and\n"
4665 "computationally efficient. \n")
4667 {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
4668 PyDoc_STR("Unpickler(file) -- Create an unpickler")},
4669 { NULL, NULL }
4672 static int
4673 init_stuff(PyObject *module_dict)
4675 PyObject *copy_reg, *t, *r;
4677 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
4679 INIT_STR(__class__);
4680 INIT_STR(__getinitargs__);
4681 INIT_STR(__dict__);
4682 INIT_STR(__getstate__);
4683 INIT_STR(__setstate__);
4684 INIT_STR(__name__);
4685 INIT_STR(__main__);
4686 INIT_STR(__reduce__);
4687 INIT_STR(write);
4688 INIT_STR(__safe_for_unpickling__);
4689 INIT_STR(append);
4690 INIT_STR(read);
4691 INIT_STR(readline);
4692 INIT_STR(copy_reg);
4693 INIT_STR(dispatch_table);
4694 INIT_STR(safe_constructors);
4695 INIT_STR(__basicnew__);
4697 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
4698 return -1;
4700 /* These next few are special because we want to use different
4701 ones in restricted mode. */
4702 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
4703 if (!dispatch_table)
4704 return -1;
4706 if (!( safe_constructors = PyObject_GetAttr(copy_reg,
4707 safe_constructors_str)))
4708 return -1;
4710 Py_DECREF(copy_reg);
4712 /* Down to here ********************************** */
4714 if (!( empty_tuple = PyTuple_New(0)))
4715 return -1;
4717 /* Ugh */
4718 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
4719 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
4720 return -1;
4722 if (!( t=PyDict_New())) return -1;
4723 if (!( r=PyRun_String(
4724 "def __init__(self, *args): self.args=args\n\n"
4725 "def __str__(self):\n"
4726 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4727 Py_file_input,
4728 module_dict, t) )) return -1;
4729 Py_DECREF(r);
4731 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
4732 if (!PickleError)
4733 return -1;
4735 Py_DECREF(t);
4737 PicklingError = PyErr_NewException("cPickle.PicklingError",
4738 PickleError, NULL);
4739 if (!PicklingError)
4740 return -1;
4742 if (!( t=PyDict_New())) return -1;
4743 if (!( r=PyRun_String(
4744 "def __init__(self, *args): self.args=args\n\n"
4745 "def __str__(self):\n"
4746 " a=self.args\n"
4747 " a=a and type(a[0]) or '(what)'\n"
4748 " return 'Cannot pickle %s objects' % a\n"
4749 , Py_file_input,
4750 module_dict, t) )) return -1;
4751 Py_DECREF(r);
4753 if (!( UnpickleableError = PyErr_NewException(
4754 "cPickle.UnpickleableError", PicklingError, t)))
4755 return -1;
4757 Py_DECREF(t);
4759 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
4760 PickleError, NULL)))
4761 return -1;
4763 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
4764 UnpicklingError, NULL)))
4765 return -1;
4767 if (PyDict_SetItemString(module_dict, "PickleError",
4768 PickleError) < 0)
4769 return -1;
4771 if (PyDict_SetItemString(module_dict, "PicklingError",
4772 PicklingError) < 0)
4773 return -1;
4775 if (PyDict_SetItemString(module_dict, "UnpicklingError",
4776 UnpicklingError) < 0)
4777 return -1;
4779 if (PyDict_SetItemString(module_dict, "UnpickleableError",
4780 UnpickleableError) < 0)
4781 return -1;
4783 if (PyDict_SetItemString(module_dict, "BadPickleGet",
4784 BadPickleGet) < 0)
4785 return -1;
4787 PycString_IMPORT;
4789 return 0;
4792 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
4793 #define PyMODINIT_FUNC void
4794 #endif
4795 PyMODINIT_FUNC
4796 initcPickle(void)
4798 PyObject *m, *d, *di, *v, *k;
4799 int i;
4800 char *rev="1.71";
4801 PyObject *format_version;
4802 PyObject *compatible_formats;
4804 Picklertype.ob_type = &PyType_Type;
4805 Picklertype.tp_getattro = PyObject_GenericGetAttr;
4806 Picklertype.tp_setattro = PyObject_GenericSetAttr;
4807 Unpicklertype.ob_type = &PyType_Type;
4808 PdataType.ob_type = &PyType_Type;
4810 /* Initialize some pieces. We need to do this before module creation,
4811 so we're forced to use a temporary dictionary. :(
4813 di=PyDict_New();
4814 if (!di) return;
4815 if (init_stuff(di) < 0) return;
4817 /* Create the module and add the functions */
4818 m = Py_InitModule4("cPickle", cPickle_methods,
4819 cPickle_module_documentation,
4820 (PyObject*)NULL,PYTHON_API_VERSION);
4822 /* Add some symbolic constants to the module */
4823 d = PyModule_GetDict(m);
4824 PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
4825 Py_XDECREF(v);
4827 /* Copy data from di. Waaa. */
4828 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
4829 if (PyObject_SetItem(d, k, v) < 0) {
4830 Py_DECREF(di);
4831 return;
4834 Py_DECREF(di);
4836 format_version = PyString_FromString("1.3");
4837 compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
4839 PyDict_SetItemString(d, "format_version", format_version);
4840 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
4841 Py_XDECREF(format_version);
4842 Py_XDECREF(compatible_formats);