2 * cPickle.c,v 1.63 1999/02/05 01:40:06 jim Exp
4 * Copyright (c) 1996-1998, Digital Creations, Fredericksburg, VA, USA.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * o Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the disclaimer that follows.
14 * o Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions, and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * o Neither the name of Digital Creations nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
25 * IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
28 * CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
31 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38 # If you have questions regarding this software, contact:
40 # Digital Creations, L.C.
41 # 910 Princess Ann Street
42 # Fredericksburge, Virginia 22401
49 static char cPickle_module_documentation
[] =
50 "C implementation and optimization of the Python pickle module\n"
52 "cPickle.c,v 1.63 1999/02/05 01:40:06 jim Exp\n"
56 #include "cStringIO.h"
61 #define Py_eval_input eval_input
62 #endif /* Py_eval_input */
66 #define UNLESS(E) if (!(E))
68 #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
70 #define WRITE_BUF_SIZE 256
91 #define SHORT_BINSTRING 'U'
96 #define EMPTY_DICT '}'
101 #define LONG_BINGET 'j'
103 #define EMPTY_LIST ']'
107 #define LONG_BINPUT 'r'
110 #define EMPTY_TUPLE ')'
113 static char MARKv
= MARK
;
115 /* atol function from string module */
116 static PyObject
*atol_func
;
118 static PyObject
*PicklingError
;
119 static PyObject
*UnpicklingError
;
120 static PyObject
*BadPickleGet
;
123 static PyObject
*dispatch_table
;
124 static PyObject
*safe_constructors
;
125 static PyObject
*empty_tuple
;
127 static PyObject
*__class___str
, *__getinitargs___str
, *__dict___str
,
128 *__getstate___str
, *__setstate___str
, *__name___str
, *__reduce___str
,
129 *write_str
, *__safe_for_unpickling___str
, *append_str
,
130 *read_str
, *readline_str
, *__main___str
, *__basicnew___str
,
131 *copy_reg_str
, *dispatch_table_str
, *safe_constructors_str
, *empty_str
;
136 #ifndef PyList_SET_ITEM
137 #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
139 #ifndef PyList_GET_SIZE
140 #define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
142 #ifndef PyTuple_SET_ITEM
143 #define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
145 #ifndef PyTuple_GET_SIZE
146 #define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
148 #ifndef PyString_GET_SIZE
149 #define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
152 /*************************************************************************
153 Internal Data type for pickle data. */
162 Pdata_dealloc(Pdata
*self
) {
166 for (i
=self
->length
, p
=self
->data
; --i
>= 0; p
++) Py_DECREF(*p
);
168 if (self
->data
) free(self
->data
);
173 static PyTypeObject PdataType
= {
174 PyObject_HEAD_INIT(NULL
) 0, "Pdata", sizeof(Pdata
), 0,
175 (destructor
)Pdata_dealloc
,
176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
179 #define Pdata_Check(O) ((O)->ob_type == &PdataType)
185 UNLESS (self
= PyObject_NEW(Pdata
, &PdataType
)) return NULL
;
188 self
->data
=malloc(self
->size
* sizeof(PyObject
*));
189 if (self
->data
) return (PyObject
*)self
;
191 return PyErr_NoMemory();
196 PyErr_SetString(UnpicklingError
, "unpickling stack underflow");
201 Pdata_clear(Pdata
*self
, int clearto
) {
205 if (clearto
< 0) return stackUnderflow();
206 if (clearto
>= self
->length
) return 0;
208 for (i
=self
->length
, p
=self
->data
+clearto
; --i
>= clearto
; p
++)
210 self
->length
=clearto
;
217 Pdata_grow(Pdata
*self
) {
223 self
->data
= realloc(self
->data
, self
->size
*sizeof(PyObject
*));
232 #define PDATA_POP(D,V) { \
233 if ((D)->length) V=D->data[--((D)->length)]; \
235 PyErr_SetString(UnpicklingError, "bad pickle data"); \
242 Pdata_popTuple(Pdata
*self
, int start
) {
246 l
=self
->length
-start
;
247 UNLESS (r
=PyTuple_New(l
)) return NULL
;
248 for (i
=start
, j
=0 ; j
< l
; )
249 PyTuple_SET_ITEM(r
,j
++,self
->data
[i
++]);
256 Pdata_popList(Pdata
*self
, int start
) {
260 l
=self
->length
-start
;
261 UNLESS (r
=PyList_New(l
)) return NULL
;
262 for (i
=start
, j
=0 ; j
< l
; )
263 PyList_SET_ITEM(r
,j
++,self
->data
[i
++]);
269 #define PDATA_APPEND_(D,O,ER) { \
270 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
273 #define PDATA_APPEND(D,O,ER) { \
274 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
275 Pdata_grow((Pdata*)(D)) < 0) \
278 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
281 #define PDATA_PUSH(D,O,ER) { \
282 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
283 Pdata_grow((Pdata*)(D)) < 0) { \
287 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
290 /*************************************************************************/
292 #define ARG_TUP(self, o) { \
293 if (self->arg || (self->arg=PyTuple_New(1))) { \
294 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
295 PyTuple_SET_ITEM(self->arg,0,o); \
302 #define FREE_ARG_TUP(self) { \
303 if (self->arg->ob_refcnt > 1) { \
304 Py_DECREF(self->arg); \
317 PyObject
*inst_pers_func
;
319 int fast
; /* Fast mode doesn't save in memo, don't use if circ ref */
323 PyObject
*dispatch_table
;
326 staticforward PyTypeObject Picklertype
;
339 PyObject
*last_string
;
344 int (*readline_func
)();
347 PyObject
*safe_constructors
;
350 staticforward PyTypeObject Unpicklertype
;
353 cPickle_PyMapping_HasKey(PyObject
*o
, PyObject
*key
) {
356 if ((v
= PyObject_GetItem(o
,key
))) {
367 #ifdef HAVE_STDARG_PROTOTYPES
369 cPickle_ErrFormat(PyObject
*ErrType
, char *stringformat
, char *format
, ...) {
372 cPickle_ErrFormat(va_alist
) va_dcl
{
375 PyObject
*args
=0, *retval
=0;
376 #ifdef HAVE_STDARG_PROTOTYPES
377 va_start(va
, format
);
380 char *stringformat
, *format
;
382 ErrType
= va_arg(va
, PyObject
*);
383 stringformat
= va_arg(va
, char *);
384 format
= va_arg(va
, char *);
387 if (format
) args
= Py_VaBuildValue(format
, va
);
389 if (format
&& ! args
) return NULL
;
390 if (stringformat
&& !(retval
=PyString_FromString(stringformat
))) return NULL
;
395 v
=PyString_Format(retval
, args
);
398 if (! v
) return NULL
;
403 if (args
) retval
=args
;
405 PyErr_SetObject(ErrType
,Py_None
);
408 PyErr_SetObject(ErrType
,retval
);
414 write_file(Picklerobject
*self
, char *s
, int n
) {
419 if ((int)fwrite(s
, sizeof(char), n
, self
->fp
) != n
) {
420 PyErr_SetFromErrno(PyExc_IOError
);
428 write_cStringIO(Picklerobject
*self
, char *s
, int n
) {
433 if (PycStringIO
->cwrite((PyObject
*)self
->file
, s
, n
) != n
) {
441 write_none(Picklerobject
*self
, char *s
, int n
) {
442 if (s
== NULL
) return 0;
447 write_other(Picklerobject
*self
, char *s
, int n
) {
448 PyObject
*py_str
= 0, *junk
= 0;
451 UNLESS (self
->buf_size
) return 0;
453 PyString_FromStringAndSize(self
->write_buf
, self
->buf_size
))
457 if (self
->buf_size
&& (n
+ self
->buf_size
) > WRITE_BUF_SIZE
) {
458 if (write_other(self
, NULL
, 0) < 0)
462 if (n
> WRITE_BUF_SIZE
) {
464 PyString_FromStringAndSize(s
, n
))
468 memcpy(self
->write_buf
+ self
->buf_size
, s
, n
);
475 /* object with write method */
476 ARG_TUP(self
, py_str
);
478 junk
= PyObject_CallObject(self
->write
, self
->arg
);
481 if (junk
) Py_DECREF(junk
);
485 PDATA_PUSH(self
->file
, py_str
, -1);
493 read_file(Unpicklerobject
*self
, char **s
, int n
) {
495 if (self
->buf_size
== 0) {
498 size
= ((n
< 32) ? 32 : n
);
499 UNLESS (self
->buf
= (char *)malloc(size
* sizeof(char))) {
504 self
->buf_size
= size
;
506 else if (n
> self
->buf_size
) {
507 UNLESS (self
->buf
= (char *)realloc(self
->buf
, n
* sizeof(char))) {
515 if ((int)fread(self
->buf
, sizeof(char), n
, self
->fp
) != n
) {
516 if (feof(self
->fp
)) {
517 PyErr_SetNone(PyExc_EOFError
);
521 PyErr_SetFromErrno(PyExc_IOError
);
532 readline_file(Unpicklerobject
*self
, char **s
) {
535 if (self
->buf_size
== 0) {
536 UNLESS (self
->buf
= (char *)malloc(40 * sizeof(char))) {
546 for (; i
< (self
->buf_size
- 1); i
++) {
547 if (feof(self
->fp
) || (self
->buf
[i
] = getc(self
->fp
)) == '\n') {
548 self
->buf
[i
+ 1] = '\0';
554 UNLESS (self
->buf
= (char *)realloc(self
->buf
,
555 (self
->buf_size
* 2) * sizeof(char))) {
567 read_cStringIO(Unpicklerobject
*self
, char **s
, int n
) {
570 if (PycStringIO
->cread((PyObject
*)self
->file
, &ptr
, n
) != n
) {
571 PyErr_SetNone(PyExc_EOFError
);
582 readline_cStringIO(Unpicklerobject
*self
, char **s
) {
586 if ((n
= PycStringIO
->creadline((PyObject
*)self
->file
, &ptr
)) < 0) {
597 read_other(Unpicklerobject
*self
, char **s
, int n
) {
598 PyObject
*bytes
, *str
=0;
601 UNLESS (bytes
= PyInt_FromLong(n
)) return -1;
603 ARG_TUP(self
, bytes
);
605 str
= PyObject_CallObject(self
->read
, self
->arg
);
608 if (! str
) return -1;
610 Py_XDECREF(self
->last_string
);
611 self
->last_string
= str
;
613 if (! (*s
= PyString_AsString(str
))) return -1;
619 readline_other(Unpicklerobject
*self
, char **s
) {
623 UNLESS (str
= PyObject_CallObject(self
->readline
, empty_tuple
)) {
627 if ((str_size
= PyString_Size(str
)) < 0)
630 Py_XDECREF(self
->last_string
);
631 self
->last_string
= str
;
633 if (! (*s
= PyString_AsString(str
)))
641 pystrndup(char *s
, int l
) {
643 UNLESS (r
=malloc((l
+1)*sizeof(char))) return (char*)PyErr_NoMemory();
651 get(Picklerobject
*self
, PyObject
*id
) {
652 PyObject
*value
, *mv
;
657 UNLESS (mv
= PyDict_GetItem(self
->memo
, id
)) {
658 PyErr_SetObject(PyExc_KeyError
, id
);
662 UNLESS (value
= PyTuple_GetItem(mv
, 0))
665 UNLESS (PyInt_Check(value
)) {
666 PyErr_SetString(PicklingError
, "no int where int expected in memo");
669 c_value
= PyInt_AS_LONG((PyIntObject
*)value
);
673 sprintf(s
+ 1, "%ld\n", c_value
);
676 else if (Pdata_Check(self
->file
)) {
677 if (write_other(self
, NULL
, 0) < 0) return -1;
678 PDATA_APPEND(self
->file
, mv
, -1);
684 s
[1] = (int)(c_value
& 0xff);
689 s
[1] = (int)(c_value
& 0xff);
690 s
[2] = (int)((c_value
>> 8) & 0xff);
691 s
[3] = (int)((c_value
>> 16) & 0xff);
692 s
[4] = (int)((c_value
>> 24) & 0xff);
697 if ((*self
->write_func
)(self
, s
, len
) < 0)
705 put(Picklerobject
*self
, PyObject
*ob
) {
706 if (ob
->ob_refcnt
< 2 || self
->fast
)
709 return put2(self
, ob
);
714 put2(Picklerobject
*self
, PyObject
*ob
) {
716 int p
, len
, res
= -1;
717 PyObject
*py_ob_id
= 0, *memo_len
= 0, *t
= 0;
719 if (self
->fast
) return 0;
721 if ((p
= PyDict_Size(self
->memo
)) < 0)
724 p
++; /* Make sure memo keys are positive! */
726 UNLESS (py_ob_id
= PyInt_FromLong((long)ob
))
729 UNLESS (memo_len
= PyInt_FromLong(p
))
732 UNLESS (t
= PyTuple_New(2))
735 PyTuple_SET_ITEM(t
, 0, memo_len
);
737 PyTuple_SET_ITEM(t
, 1, ob
);
740 if (PyDict_SetItem(self
->memo
, py_ob_id
, t
) < 0)
745 sprintf(c_str
+ 1, "%d\n", p
);
748 else if (Pdata_Check(self
->file
)) {
749 if (write_other(self
, NULL
, 0) < 0) return -1;
750 PDATA_APPEND(self
->file
, memo_len
, -1);
751 res
=0; /* Job well done ;) */
756 c_str
[0] = LONG_BINPUT
;
757 c_str
[1] = (int)(p
& 0xff);
758 c_str
[2] = (int)((p
>> 8) & 0xff);
759 c_str
[3] = (int)((p
>> 16) & 0xff);
760 c_str
[4] = (int)((p
>> 24) & 0xff);
770 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
776 Py_XDECREF(py_ob_id
);
777 Py_XDECREF(memo_len
);
783 #define PyImport_Import cPickle_Import
786 PyImport_Import(PyObject
*module_name
) {
787 static PyObject
*silly_list
=0, *__builtins___str
=0, *__import___str
;
788 static PyObject
*standard_builtins
=0;
789 PyObject
*globals
=0, *__import__
=0, *__builtins__
=0, *r
=0;
791 UNLESS (silly_list
) {
792 UNLESS (__import___str
=PyString_FromString("__import__"))
794 UNLESS (__builtins___str
=PyString_FromString("__builtins__"))
796 UNLESS (silly_list
=Py_BuildValue("[s]","__doc__"))
800 if ((globals
=PyEval_GetGlobals())) {
802 UNLESS (__builtins__
=PyObject_GetItem(globals
,__builtins___str
))
808 UNLESS (standard_builtins
||
809 (standard_builtins
=PyImport_ImportModule("__builtin__")))
812 __builtins__
=standard_builtins
;
813 Py_INCREF(__builtins__
);
814 UNLESS (globals
= Py_BuildValue("{sO}", "__builtins__", __builtins__
))
818 if (PyDict_Check(__builtins__
)) {
819 UNLESS (__import__
=PyObject_GetItem(__builtins__
,__import___str
)) goto err
;
822 UNLESS (__import__
=PyObject_GetAttr(__builtins__
,__import___str
)) goto err
;
825 UNLESS (r
=PyObject_CallFunction(__import__
,"OOOO",
826 module_name
, globals
, globals
, silly_list
))
830 Py_DECREF(__builtins__
);
831 Py_DECREF(__import__
);
836 Py_XDECREF(__builtins__
);
837 Py_XDECREF(__import__
);
842 whichmodule(PyObject
*global
, PyObject
*global_name
) {
844 PyObject
*module
= 0, *modules_dict
= 0,
845 *global_name_attr
= 0, *name
= 0;
847 module
= PyObject_GetAttrString(global
, "__module__");
848 if (module
) return module
;
851 UNLESS (modules_dict
= PySys_GetObject("modules"))
855 while ((j
= PyDict_Next(modules_dict
, &i
, &name
, &module
))) {
857 if (PyObject_Compare(name
, __main___str
)==0) continue;
859 UNLESS (global_name_attr
= PyObject_GetAttr(module
, global_name
)) {
864 if (global_name_attr
!= global
) {
865 Py_DECREF(global_name_attr
);
869 Py_DECREF(global_name_attr
);
874 /* The following implements the rule in pickle.py added in 1.5
875 that used __main__ if no module is found. I don't actually
889 save_none(Picklerobject
*self
, PyObject
*args
) {
890 static char none
= NONE
;
891 if ((*self
->write_func
)(self
, &none
, 1) < 0)
899 save_int(Picklerobject
*self
, PyObject
*args
) {
901 long l
= PyInt_AS_LONG((PyIntObject
*)args
);
909 /* Save extra-long ints in non-binary mode, so that
910 we can use python long parsing code to restore,
913 sprintf(c_str
+ 1, "%ld\n", l
);
914 if ((*self
->write_func
)(self
, c_str
, strlen(c_str
)) < 0)
918 c_str
[1] = (int)( l
& 0xff);
919 c_str
[2] = (int)((l
>> 8) & 0xff);
920 c_str
[3] = (int)((l
>> 16) & 0xff);
921 c_str
[4] = (int)((l
>> 24) & 0xff);
923 if ((c_str
[4] == 0) && (c_str
[3] == 0)) {
938 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
947 save_long(Picklerobject
*self
, PyObject
*args
) {
951 static char l
= LONG
;
953 UNLESS (repr
= PyObject_Repr(args
))
956 if ((size
= PyString_Size(repr
)) < 0)
959 if ((*self
->write_func
)(self
, &l
, 1) < 0)
962 if ((*self
->write_func
)(self
,
963 PyString_AS_STRING((PyStringObject
*)repr
), size
) < 0)
966 if ((*self
->write_func
)(self
, "\n", 1) < 0)
979 save_float(Picklerobject
*self
, PyObject
*args
) {
980 double x
= PyFloat_AS_DOUBLE((PyFloatObject
*)args
);
986 char str
[9], *p
= str
;
1000 /* Normalize f to be in the range [1.0, 2.0) */
1001 if (0.5 <= f
&& f
< 1.0) {
1005 else if (f
== 0.0) {
1009 PyErr_SetString(PyExc_SystemError
,
1010 "frexp() result out of range");
1015 /* XXX 1024 itself is reserved for Inf/NaN */
1016 PyErr_SetString(PyExc_OverflowError
,
1017 "float too large to pack with d format");
1020 else if (e
< -1022) {
1021 /* Gradual underflow */
1022 f
= ldexp(f
, 1022 + e
);
1025 else if (!(e
== 0 && f
== 0.0)) {
1027 f
-= 1.0; /* Get rid of leading 1 */
1030 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1031 f
*= 268435456.0; /* 2**28 */
1032 fhi
= (long) floor(f
); /* Truncate */
1034 f
*= 16777216.0; /* 2**24 */
1035 flo
= (long) floor(f
+ 0.5); /* Round */
1038 *p
= (s
<<7) | (e
>>4);
1042 *p
= (char) (((e
&0xF)<<4) | (fhi
>>24));
1046 *p
= (fhi
>>16) & 0xFF;
1050 *p
= (fhi
>>8) & 0xFF;
1058 *p
= (flo
>>16) & 0xFF;
1062 *p
= (flo
>>8) & 0xFF;
1068 if ((*self
->write_func
)(self
, str
, 9) < 0)
1074 sprintf(c_str
+ 1, "%.17g\n", x
);
1076 if ((*self
->write_func
)(self
, c_str
, strlen(c_str
)) < 0)
1085 save_string(Picklerobject
*self
, PyObject
*args
, int doput
) {
1089 if ((size
= PyString_Size(args
)) < 0)
1095 static char string
= STRING
;
1097 UNLESS (repr
= PyObject_Repr(args
))
1100 if ((len
= PyString_Size(repr
)) < 0)
1102 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1104 if ((*self
->write_func
)(self
, &string
, 1) < 0)
1107 if ((*self
->write_func
)(self
, repr_str
, len
) < 0)
1110 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1119 if ((size
= PyString_Size(args
)) < 0)
1123 c_str
[0] = SHORT_BINSTRING
;
1128 c_str
[0] = BINSTRING
;
1129 for (i
= 1; i
< 5; i
++)
1130 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1134 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
1137 if (size
> 128 && Pdata_Check(self
->file
)) {
1138 if (write_other(self
, NULL
, 0) < 0) return -1;
1139 PDATA_APPEND(self
->file
, args
, -1);
1142 if ((*self
->write_func
)(self
,
1143 PyString_AS_STRING((PyStringObject
*)args
), size
) < 0)
1149 if (put(self
, args
) < 0)
1161 save_tuple(Picklerobject
*self
, PyObject
*args
) {
1162 PyObject
*element
= 0, *py_tuple_id
= 0;
1163 int len
, i
, has_key
, res
= -1;
1165 static char tuple
= TUPLE
;
1167 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1170 if ((len
= PyTuple_Size(args
)) < 0)
1173 for (i
= 0; i
< len
; i
++) {
1174 UNLESS (element
= PyTuple_GET_ITEM((PyTupleObject
*)args
, i
))
1177 if (save(self
, element
, 0) < 0)
1181 UNLESS (py_tuple_id
= PyInt_FromLong((long)args
))
1185 if ((has_key
= cPickle_PyMapping_HasKey(self
->memo
, py_tuple_id
)) < 0)
1190 static char pop_mark
= POP_MARK
;
1192 if ((*self
->write_func
)(self
, &pop_mark
, 1) < 0)
1196 static char pop
= POP
;
1198 for (i
= 0; i
<= len
; i
++) {
1199 if ((*self
->write_func
)(self
, &pop
, 1) < 0)
1204 if (get(self
, py_tuple_id
) < 0)
1212 if ((*self
->write_func
)(self
, &tuple
, 1) < 0) {
1216 if (put(self
, args
) < 0)
1222 Py_XDECREF(py_tuple_id
);
1228 save_empty_tuple(Picklerobject
*self
, PyObject
*args
) {
1229 static char tuple
= EMPTY_TUPLE
;
1231 return (*self
->write_func
)(self
, &tuple
, 1);
1236 save_list(Picklerobject
*self
, PyObject
*args
) {
1237 PyObject
*element
= 0;
1238 int s_len
, len
, i
, using_appends
, res
= -1;
1241 static char append
= APPEND
, appends
= APPENDS
;
1253 if ((len
= PyList_Size(args
)) < 0)
1256 if ((*self
->write_func
)(self
, s
, s_len
) < 0)
1260 if (put(self
, args
) < 0)
1264 if (put2(self
, args
) < 0)
1268 if ((using_appends
= (self
->bin
&& (len
> 1))))
1269 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1272 for (i
= 0; i
< len
; i
++) {
1273 UNLESS (element
= PyList_GET_ITEM((PyListObject
*)args
, i
))
1276 if (save(self
, element
, 0) < 0)
1279 if (!using_appends
) {
1280 if ((*self
->write_func
)(self
, &append
, 1) < 0)
1285 if (using_appends
) {
1286 if ((*self
->write_func
)(self
, &appends
, 1) < 0)
1299 save_dict(Picklerobject
*self
, PyObject
*args
) {
1300 PyObject
*key
= 0, *value
= 0;
1301 int i
, len
, res
= -1, using_setitems
;
1304 static char setitem
= SETITEM
, setitems
= SETITEMS
;
1316 if ((*self
->write_func
)(self
, s
, len
) < 0)
1319 if ((len
= PyDict_Size(args
)) < 0)
1323 if (put(self
, args
) < 0)
1327 if (put2(self
, args
) < 0)
1331 if ((using_setitems
= (self
->bin
&& (PyDict_Size(args
) > 1))))
1332 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1336 while (PyDict_Next(args
, &i
, &key
, &value
)) {
1337 if (save(self
, key
, 0) < 0)
1340 if (save(self
, value
, 0) < 0)
1343 if (!using_setitems
) {
1344 if ((*self
->write_func
)(self
, &setitem
, 1) < 0)
1349 if (using_setitems
) {
1350 if ((*self
->write_func
)(self
, &setitems
, 1) < 0)
1363 save_inst(Picklerobject
*self
, PyObject
*args
) {
1364 PyObject
*class = 0, *module
= 0, *name
= 0, *state
= 0,
1365 *getinitargs_func
= 0, *getstate_func
= 0, *class_args
= 0;
1366 char *module_str
, *name_str
;
1367 int module_size
, name_size
, res
= -1;
1369 static char inst
= INST
, obj
= OBJ
, build
= BUILD
;
1371 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1374 UNLESS (class = PyObject_GetAttr(args
, __class___str
))
1378 if (save(self
, class, 0) < 0)
1382 if ((getinitargs_func
= PyObject_GetAttr(args
, __getinitargs___str
))) {
1383 PyObject
*element
= 0;
1386 UNLESS (class_args
=
1387 PyObject_CallObject(getinitargs_func
, empty_tuple
))
1390 if ((len
= PyObject_Length(class_args
)) < 0)
1393 for (i
= 0; i
< len
; i
++) {
1394 UNLESS (element
= PySequence_GetItem(class_args
, i
))
1397 if (save(self
, element
, 0) < 0) {
1410 UNLESS (name
= ((PyClassObject
*)class)->cl_name
) {
1411 PyErr_SetString(PicklingError
, "class has no name");
1415 UNLESS (module
= whichmodule(class, name
))
1419 if ((module_size
= PyString_Size(module
)) < 0 ||
1420 (name_size
= PyString_Size(name
)) < 0)
1423 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
1424 name_str
= PyString_AS_STRING((PyStringObject
*)name
);
1426 if ((*self
->write_func
)(self
, &inst
, 1) < 0)
1429 if ((*self
->write_func
)(self
, module_str
, module_size
) < 0)
1432 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1435 if ((*self
->write_func
)(self
, name_str
, name_size
) < 0)
1438 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1441 else if ((*self
->write_func
)(self
, &obj
, 1) < 0) {
1445 if ((getstate_func
= PyObject_GetAttr(args
, __getstate___str
))) {
1446 UNLESS (state
= PyObject_CallObject(getstate_func
, empty_tuple
))
1452 UNLESS (state
= PyObject_GetAttr(args
, __dict___str
)) {
1459 if (!PyDict_Check(state
)) {
1460 if (put2(self
, args
) < 0)
1464 if (put(self
, args
) < 0)
1468 if (save(self
, state
, 0) < 0)
1471 if ((*self
->write_func
)(self
, &build
, 1) < 0)
1480 Py_XDECREF(getinitargs_func
);
1481 Py_XDECREF(getstate_func
);
1482 Py_XDECREF(class_args
);
1489 save_global(Picklerobject
*self
, PyObject
*args
, PyObject
*name
) {
1490 PyObject
*global_name
= 0, *module
= 0;
1491 char *name_str
, *module_str
;
1492 int module_size
, name_size
, res
= -1;
1494 static char global
= GLOBAL
;
1498 Py_INCREF(global_name
);
1501 UNLESS (global_name
= PyObject_GetAttr(args
, __name___str
))
1505 UNLESS (module
= whichmodule(args
, global_name
))
1508 if ((module_size
= PyString_Size(module
)) < 0 ||
1509 (name_size
= PyString_Size(global_name
)) < 0)
1512 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
1513 name_str
= PyString_AS_STRING((PyStringObject
*)global_name
);
1515 if ((*self
->write_func
)(self
, &global
, 1) < 0)
1518 if ((*self
->write_func
)(self
, module_str
, module_size
) < 0)
1521 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1524 if ((*self
->write_func
)(self
, name_str
, name_size
) < 0)
1527 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1530 if (put(self
, args
) < 0)
1537 Py_XDECREF(global_name
);
1543 save_pers(Picklerobject
*self
, PyObject
*args
, PyObject
*f
) {
1547 static char persid
= PERSID
, binpersid
= BINPERSID
;
1550 ARG_TUP(self
, args
);
1552 pid
= PyObject_CallObject(f
, self
->arg
);
1555 if (! pid
) return -1;
1557 if (pid
!= Py_None
) {
1559 if (!PyString_Check(pid
)) {
1560 PyErr_SetString(PicklingError
,
1561 "persistent id must be string");
1565 if ((*self
->write_func
)(self
, &persid
, 1) < 0)
1568 if ((size
= PyString_Size(pid
)) < 0)
1571 if ((*self
->write_func
)(self
,
1572 PyString_AS_STRING((PyStringObject
*)pid
), size
) < 0)
1575 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1581 else if (save(self
, pid
, 1) >= 0) {
1582 if ((*self
->write_func
)(self
, &binpersid
, 1) < 0)
1601 save_reduce(Picklerobject
*self
, PyObject
*callable
,
1602 PyObject
*tup
, PyObject
*state
, PyObject
*ob
) {
1603 static char reduce
= REDUCE
, build
= BUILD
;
1605 if (save(self
, callable
, 0) < 0)
1608 if (save(self
, tup
, 0) < 0)
1611 if ((*self
->write_func
)(self
, &reduce
, 1) < 0)
1615 if (state
&& !PyDict_Check(state
)) {
1616 if (put2(self
, ob
) < 0)
1620 if (put(self
, ob
) < 0)
1626 if (save(self
, state
, 0) < 0)
1629 if ((*self
->write_func
)(self
, &build
, 1) < 0)
1637 save(Picklerobject
*self
, PyObject
*args
, int pers_save
) {
1639 PyObject
*py_ob_id
= 0, *__reduce__
= 0, *t
= 0, *arg_tup
= 0,
1640 *callable
= 0, *state
= 0;
1641 int res
= -1, tmp
, size
;
1643 if (!pers_save
&& self
->pers_func
) {
1644 if ((tmp
= save_pers(self
, args
, self
->pers_func
)) != 0) {
1650 if (args
== Py_None
) {
1651 res
= save_none(self
, args
);
1655 type
= args
->ob_type
;
1657 switch (type
->tp_name
[0]) {
1659 if (type
== &PyInt_Type
) {
1660 res
= save_int(self
, args
);
1666 if (type
== &PyLong_Type
) {
1667 res
= save_long(self
, args
);
1673 if (type
== &PyFloat_Type
) {
1674 res
= save_float(self
, args
);
1680 if (type
== &PyTuple_Type
&& PyTuple_Size(args
)==0) {
1681 if (self
->bin
) res
= save_empty_tuple(self
, args
);
1682 else res
= save_tuple(self
, args
);
1688 if ((type
== &PyString_Type
) && (PyString_GET_SIZE(args
) < 2)) {
1689 res
= save_string(self
, args
, 0);
1694 if (args
->ob_refcnt
> 1) {
1700 UNLESS (py_ob_id
= PyInt_FromLong(ob_id
))
1703 if ((has_key
= cPickle_PyMapping_HasKey(self
->memo
, py_ob_id
)) < 0)
1707 if (get(self
, py_ob_id
) < 0)
1715 switch (type
->tp_name
[0]) {
1717 if (type
== &PyString_Type
) {
1718 res
= save_string(self
, args
, 1);
1724 if (type
== &PyTuple_Type
) {
1725 res
= save_tuple(self
, args
);
1731 if (type
== &PyList_Type
) {
1732 res
= save_list(self
, args
);
1738 if (type
== &PyDict_Type
) {
1739 res
= save_dict(self
, args
);
1745 if (type
== &PyInstance_Type
) {
1746 res
= save_inst(self
, args
);
1752 if (type
== &PyClass_Type
) {
1753 res
= save_global(self
, args
, NULL
);
1759 if (type
== &PyFunction_Type
) {
1760 res
= save_global(self
, args
, NULL
);
1766 if (type
== &PyCFunction_Type
) {
1767 res
= save_global(self
, args
, NULL
);
1772 if (!pers_save
&& self
->inst_pers_func
) {
1773 if ((tmp
= save_pers(self
, args
, self
->inst_pers_func
)) != 0) {
1779 if ((__reduce__
= PyDict_GetItem(dispatch_table
, (PyObject
*)type
))) {
1780 Py_INCREF(__reduce__
);
1783 ARG_TUP(self
, args
);
1785 t
= PyObject_CallObject(__reduce__
, self
->arg
);
1788 if (! t
) goto finally
;
1793 if ((__reduce__
= PyObject_GetAttr(args
, __reduce___str
))) {
1794 UNLESS (t
= PyObject_CallObject(__reduce__
, empty_tuple
))
1803 if (PyString_Check(t
)) {
1804 res
= save_global(self
, args
, t
);
1808 if (!PyTuple_Check(t
)) {
1809 cPickle_ErrFormat(PicklingError
, "Value returned by %s must "
1810 "be a tuple", "O", __reduce__
);
1814 size
= PyTuple_Size(t
);
1816 if ((size
!= 3) && (size
!= 2)) {
1817 cPickle_ErrFormat(PicklingError
, "tuple returned by %s must "
1818 "contain only two or three elements", "O", __reduce__
);
1822 callable
= PyTuple_GET_ITEM(t
, 0);
1824 arg_tup
= PyTuple_GET_ITEM(t
, 1);
1827 state
= PyTuple_GET_ITEM(t
, 2);
1830 UNLESS (PyTuple_Check(arg_tup
) || arg_tup
==Py_None
) {
1831 cPickle_ErrFormat(PicklingError
, "Second element of tuple "
1832 "returned by %s must be a tuple", "O", __reduce__
);
1836 res
= save_reduce(self
, callable
, arg_tup
, state
, args
);
1840 cPickle_ErrFormat(PicklingError
, "Cannot pickle %s objects.",
1841 "O", (PyObject
*)type
);
1844 Py_XDECREF(py_ob_id
);
1845 Py_XDECREF(__reduce__
);
1853 dump(Picklerobject
*self
, PyObject
*args
) {
1854 static char stop
= STOP
;
1856 if (save(self
, args
, 0) < 0)
1859 if ((*self
->write_func
)(self
, &stop
, 1) < 0)
1862 if ((*self
->write_func
)(self
, NULL
, 0) < 0)
1869 Pickle_clear_memo(Picklerobject
*self
, PyObject
*args
) {
1870 if (args
&& ! PyArg_ParseTuple(args
,"")) return NULL
;
1871 if (self
->memo
) PyDict_Clear(self
->memo
);
1877 Pickle_getvalue(Picklerobject
*self
, PyObject
*args
) {
1878 int l
, i
, rsize
, ssize
, clear
=1, lm
;
1881 char *s
, *p
, *have_get
;
1884 if (args
&& ! PyArg_ParseTuple(args
,"|i",&clear
)) return NULL
;
1886 /* Check to make sure we are based on a list */
1887 if (! Pdata_Check(self
->file
)) {
1888 PyErr_SetString(PicklingError
,
1889 "Attempt to getvalue a non-list-based pickler");
1893 /* flush write buffer */
1894 if (write_other(self
, NULL
, 0) < 0) return NULL
;
1896 data
=(Pdata
*)self
->file
;
1899 /* set up an array to hold get/put status */
1900 if ((lm
=PyDict_Size(self
->memo
)) < 0) return NULL
;
1902 if (! (have_get
=malloc((lm
)*sizeof(char)))) return PyErr_NoMemory();
1903 memset(have_get
,0,lm
);
1905 /* Scan for gets. */
1906 for (rsize
=0, i
=l
; --i
>= 0; ) {
1909 if (PyString_Check(k
)) {
1910 rsize
+= PyString_GET_SIZE(k
);
1913 else if (PyInt_Check(k
)) { /* put */
1914 ik
=PyInt_AS_LONG((PyIntObject
*)k
);
1915 if (ik
>= lm
|| ik
==0) {
1916 PyErr_SetString(PicklingError
,
1917 "Invalid get data");
1920 if (have_get
[ik
]) { /* with matching get */
1921 if (ik
< 256) rsize
+= 2;
1926 else if (! (PyTuple_Check(k
) &&
1927 PyTuple_GET_SIZE(k
) == 2 &&
1928 PyInt_Check((k
=PyTuple_GET_ITEM(k
,0))))
1930 PyErr_SetString(PicklingError
,
1931 "Unexpected data in internal list");
1936 ik
=PyInt_AS_LONG((PyIntObject
*)k
);
1937 if (ik
>= lm
|| ik
==0) {
1938 PyErr_SetString(PicklingError
,
1939 "Invalid get data");
1943 if (ik
< 256) rsize
+= 2;
1949 /* Now generate the result */
1950 UNLESS (r
=PyString_FromStringAndSize(NULL
,rsize
)) goto err
;
1951 s
=PyString_AS_STRING((PyStringObject
*)r
);
1953 for (i
=0; i
<l
; i
++) {
1956 if (PyString_Check(k
)) {
1957 ssize
=PyString_GET_SIZE(k
);
1959 p
=PyString_AS_STRING((PyStringObject
*)k
);
1960 while (--ssize
>= 0) *s
++=*p
++;
1964 else if (PyTuple_Check(k
)) { /* get */
1965 ik
=PyInt_AS_LONG((PyIntObject
*)PyTuple_GET_ITEM(k
,0));
1968 *s
++ = (int)(ik
& 0xff);
1972 *s
++ = (int)(ik
& 0xff);
1973 *s
++ = (int)((ik
>> 8) & 0xff);
1974 *s
++ = (int)((ik
>> 16) & 0xff);
1975 *s
++ = (int)((ik
>> 24) & 0xff);
1980 ik
=PyInt_AS_LONG((PyIntObject
*)k
);
1982 if (have_get
[ik
]) { /* with matching get */
1985 *s
++ = (int)(ik
& 0xff);
1989 *s
++ = (int)(ik
& 0xff);
1990 *s
++ = (int)((ik
>> 8) & 0xff);
1991 *s
++ = (int)((ik
>> 16) & 0xff);
1992 *s
++ = (int)((ik
>> 24) & 0xff);
2000 PyDict_Clear(self
->memo
);
2001 Pdata_clear(data
,0);
2012 Pickler_dump(Picklerobject
*self
, PyObject
*args
) {
2016 UNLESS (PyArg_ParseTuple(args
, "O|i", &ob
, &get
))
2019 if (dump(self
, ob
) < 0)
2022 if (get
) return Pickle_getvalue(self
, NULL
);
2025 return (PyObject
*)self
;
2029 static struct PyMethodDef Pickler_methods
[] = {
2030 {"dump", (PyCFunction
)Pickler_dump
, 1,
2032 "Write an object in pickle format to the object's pickle stream\n"
2034 {"clear_memo", (PyCFunction
)Pickle_clear_memo
, 1,
2035 "clear_memo() -- Clear the picklers memo"},
2036 {"getvalue", (PyCFunction
)Pickle_getvalue
, 1,
2037 "getvalue() -- Finish picking a list-based pickle"},
2038 {NULL
, NULL
} /* sentinel */
2042 static Picklerobject
*
2043 newPicklerobject(PyObject
*file
, int bin
) {
2044 Picklerobject
*self
;
2046 UNLESS (self
= PyObject_NEW(Picklerobject
, &Picklertype
))
2053 self
->pers_func
= NULL
;
2054 self
->inst_pers_func
= NULL
;
2055 self
->write_buf
= NULL
;
2059 self
->dispatch_table
= NULL
;
2068 UNLESS (self
->memo
= PyDict_New()) {
2069 Py_XDECREF((PyObject
*)self
);
2073 if (PyFile_Check(file
)) {
2074 self
->fp
= PyFile_AsFile(file
);
2075 self
->write_func
= write_file
;
2077 else if (PycStringIO_OutputCheck(file
)) {
2078 self
->write_func
= write_cStringIO
;
2080 else if (file
== Py_None
) {
2081 self
->write_func
= write_none
;
2084 self
->write_func
= write_other
;
2086 if (! Pdata_Check(file
)) {
2087 UNLESS (self
->write
= PyObject_GetAttr(file
, write_str
)) {
2089 PyErr_SetString(PyExc_TypeError
, "argument must have 'write' "
2095 UNLESS (self
->write_buf
=
2096 (char *)malloc(WRITE_BUF_SIZE
* sizeof(char))) {
2102 if (PyEval_GetRestricted()) {
2103 /* Restricted execution, get private tables */
2106 UNLESS (m
=PyImport_Import(copy_reg_str
)) goto err
;
2107 self
->dispatch_table
=PyObject_GetAttr(m
, dispatch_table_str
);
2109 UNLESS (self
->dispatch_table
) goto err
;
2112 self
->dispatch_table
=dispatch_table
;
2113 Py_INCREF(dispatch_table
);
2119 Py_DECREF((PyObject
*)self
);
2125 get_Pickler(PyObject
*self
, PyObject
*args
) {
2126 PyObject
*file
=NULL
;
2130 if (! PyArg_ParseTuple(args
, "|i", &bin
)) {
2133 if (! PyArg_ParseTuple(args
, "O|i", &file
, &bin
))
2136 return (PyObject
*)newPicklerobject(file
, bin
);
2141 Pickler_dealloc(Picklerobject
*self
) {
2142 Py_XDECREF(self
->write
);
2143 Py_XDECREF(self
->memo
);
2144 Py_XDECREF(self
->arg
);
2145 Py_XDECREF(self
->file
);
2146 Py_XDECREF(self
->pers_func
);
2147 Py_XDECREF(self
->inst_pers_func
);
2148 Py_XDECREF(self
->dispatch_table
);
2150 if (self
->write_buf
) {
2151 free(self
->write_buf
);
2159 Pickler_getattr(Picklerobject
*self
, char *name
) {
2163 if (strcmp(name
, "persistent_id") == 0) {
2164 if (!self
->pers_func
) {
2165 PyErr_SetString(PyExc_AttributeError
, name
);
2169 Py_INCREF(self
->pers_func
);
2170 return self
->pers_func
;
2174 if (strcmp(name
, "memo") == 0) {
2176 PyErr_SetString(PyExc_AttributeError
, name
);
2180 Py_INCREF(self
->memo
);
2185 if (strcmp(name
, "PicklingError") == 0) {
2186 Py_INCREF(PicklingError
);
2187 return PicklingError
;
2191 if (strcmp(name
, "binary")==0)
2192 return PyInt_FromLong(self
->bin
);
2195 if (strcmp(name
, "fast")==0)
2196 return PyInt_FromLong(self
->fast
);
2199 if (strcmp(name
, "getvalue")==0 && ! Pdata_Check(self
->file
)) {
2200 PyErr_SetString(PyExc_AttributeError
, name
);
2205 return Py_FindMethod(Pickler_methods
, (PyObject
*)self
, name
);
2210 Pickler_setattr(Picklerobject
*self
, char *name
, PyObject
*value
) {
2213 PyErr_SetString(PyExc_TypeError
,
2214 "attribute deletion is not supported");
2218 if (strcmp(name
, "persistent_id") == 0) {
2219 Py_XDECREF(self
->pers_func
);
2220 self
->pers_func
= value
;
2225 if (strcmp(name
, "inst_persistent_id") == 0) {
2226 Py_XDECREF(self
->inst_pers_func
);
2227 self
->inst_pers_func
= value
;
2232 if (strcmp(name
, "memo") == 0) {
2233 if (! PyDict_Check(value
)) {
2234 PyErr_SetString(PyExc_TypeError
, "memo must be a dictionary");
2237 Py_XDECREF(self
->memo
);
2243 if (strcmp(name
, "binary")==0) {
2244 self
->bin
=PyObject_IsTrue(value
);
2248 if (strcmp(name
, "fast")==0) {
2249 self
->fast
=PyObject_IsTrue(value
);
2253 PyErr_SetString(PyExc_AttributeError
, name
);
2258 static char Picklertype__doc__
[] =
2259 "Objects that know how to pickle objects\n"
2262 static PyTypeObject Picklertype
= {
2263 PyObject_HEAD_INIT(NULL
)
2265 "Pickler", /*tp_name*/
2266 sizeof(Picklerobject
), /*tp_basicsize*/
2269 (destructor
)Pickler_dealloc
, /*tp_dealloc*/
2270 (printfunc
)0, /*tp_print*/
2271 (getattrfunc
)Pickler_getattr
, /*tp_getattr*/
2272 (setattrfunc
)Pickler_setattr
, /*tp_setattr*/
2273 (cmpfunc
)0, /*tp_compare*/
2274 (reprfunc
)0, /*tp_repr*/
2276 0, /*tp_as_sequence*/
2277 0, /*tp_as_mapping*/
2278 (hashfunc
)0, /*tp_hash*/
2279 (ternaryfunc
)0, /*tp_call*/
2280 (reprfunc
)0, /*tp_str*/
2282 /* Space for future expansion */
2284 Picklertype__doc__
/* Documentation string */
2288 find_class(PyObject
*py_module_name
, PyObject
*py_global_name
) {
2289 PyObject
*global
= 0, *module
;
2291 module
= PySys_GetObject("modules");
2295 module
= PyDict_GetItem(module
, py_module_name
);
2296 if (module
== NULL
) {
2297 module
= PyImport_Import(py_module_name
);
2300 global
= PyObject_GetAttr(module
, py_global_name
);
2304 global
= PyObject_GetAttr(module
, py_global_name
);
2305 if (global
== NULL
) {
2307 sprintf(buf
, "Failed to import class %.128s from module %.128s",
2308 PyString_AS_STRING((PyStringObject
*)py_global_name
),
2309 PyString_AS_STRING((PyStringObject
*)py_module_name
));
2310 PyErr_SetString(PyExc_SystemError
, buf
);
2317 marker(Unpicklerobject
*self
) {
2318 if (self
->num_marks
< 1) {
2319 PyErr_SetString(UnpicklingError
, "could not find MARK");
2323 return self
->marks
[--self
->num_marks
];
2328 load_none(Unpicklerobject
*self
) {
2329 PDATA_APPEND(self
->stack
, Py_None
, -1);
2335 PyErr_SetString(UnpicklingError
, "pickle data was truncated");
2340 load_int(Unpicklerobject
*self
) {
2341 PyObject
*py_int
= 0;
2346 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2347 if (len
< 2) return bad_readline();
2348 UNLESS (s
=pystrndup(s
,len
)) return -1;
2351 l
= strtol(s
, &endptr
, 0);
2353 if (errno
|| (*endptr
!= '\n') || (endptr
[1] != '\0')) {
2354 /* Hm, maybe we've got something long. Let's try reading
2355 it as a Python long object. */
2357 UNLESS (py_int
=PyLong_FromString(s
,&endptr
,0)) goto finally
;
2359 if ((*endptr
!= '\n') || (endptr
[1] != '\0')) {
2360 PyErr_SetString(PyExc_ValueError
,
2361 "could not convert string to int");
2366 UNLESS (py_int
= PyInt_FromLong(l
)) goto finally
;
2370 PDATA_PUSH(self
->stack
, py_int
, -1);
2381 calc_binint(char *s
, int x
) {
2386 for (i
= 0, l
= 0L; i
< x
; i
++) {
2387 c
= (unsigned char)s
[i
];
2388 l
|= (long)c
<< (i
* 8);
2396 load_binintx(Unpicklerobject
*self
, char *s
, int x
) {
2397 PyObject
*py_int
= 0;
2400 l
= calc_binint(s
, x
);
2402 UNLESS (py_int
= PyInt_FromLong(l
))
2405 PDATA_PUSH(self
->stack
, py_int
, -1);
2411 load_binint(Unpicklerobject
*self
) {
2414 if ((*self
->read_func
)(self
, &s
, 4) < 0)
2417 return load_binintx(self
, s
, 4);
2422 load_binint1(Unpicklerobject
*self
) {
2425 if ((*self
->read_func
)(self
, &s
, 1) < 0)
2428 return load_binintx(self
, s
, 1);
2433 load_binint2(Unpicklerobject
*self
) {
2436 if ((*self
->read_func
)(self
, &s
, 2) < 0)
2439 return load_binintx(self
, s
, 2);
2443 load_long(Unpicklerobject
*self
) {
2448 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2449 if (len
< 2) return bad_readline();
2450 UNLESS (s
=pystrndup(s
,len
)) return -1;
2452 UNLESS (l
= PyLong_FromString(s
, &end
, 0))
2456 PDATA_PUSH(self
->stack
, l
, -1);
2467 load_float(Unpicklerobject
*self
) {
2468 PyObject
*py_float
= 0;
2473 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2474 if (len
< 2) return bad_readline();
2475 UNLESS (s
=pystrndup(s
,len
)) return -1;
2478 d
= strtod(s
, &endptr
);
2480 if (errno
|| (endptr
[0] != '\n') || (endptr
[1] != '\0')) {
2481 PyErr_SetString(PyExc_ValueError
,
2482 "could not convert string to float");
2486 UNLESS (py_float
= PyFloat_FromDouble(d
))
2490 PDATA_PUSH(self
->stack
, py_float
, -1);
2500 load_binfloat(Unpicklerobject
*self
) {
2501 PyObject
*py_float
= 0;
2507 if ((*self
->read_func
)(self
, &p
, 8) < 0)
2512 e
= (*p
& 0x7F) << 4;
2517 fhi
= (*p
& 0xF) << 24;
2521 fhi
|= (*p
& 0xFF) << 16;
2525 fhi
|= (*p
& 0xFF) << 8;
2533 flo
= (*p
& 0xFF) << 16;
2537 flo
|= (*p
& 0xFF) << 8;
2543 x
= (double)fhi
+ (double)flo
/ 16777216.0; /* 2**24 */
2544 x
/= 268435456.0; /* 2**28 */
2546 /* XXX This sadly ignores Inf/NaN */
2558 UNLESS (py_float
= PyFloat_FromDouble(x
)) return -1;
2560 PDATA_PUSH(self
->stack
, py_float
, -1);
2565 load_string(Unpicklerobject
*self
) {
2567 int len
, res
= -1, nslash
;
2570 static PyObject
*eval_dict
= 0;
2572 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2573 if (len
< 2) return bad_readline();
2574 UNLESS (s
=pystrndup(s
,len
)) return -1;
2576 /* Check for unquoted quotes (evil strings) */
2578 if (q
!= '"' && q
!= '\'') goto insecure
;
2579 for (p
=s
+1, nslash
=0; *p
; p
++) {
2580 if (*p
==q
&& nslash
%2==0) break;
2581 if (*p
=='\\') nslash
++;
2586 for (p
++; *p
; p
++) if (*p
> ' ') goto insecure
;
2589 /********************************************/
2592 UNLESS (eval_dict
= Py_BuildValue("{s{}}", "__builtins__"))
2595 UNLESS (str
= PyRun_String(s
, Py_eval_input
, eval_dict
, eval_dict
))
2599 PDATA_PUSH(self
->stack
, str
, -1);
2609 PyErr_SetString(PyExc_ValueError
,"insecure string pickle");
2615 load_binstring(Unpicklerobject
*self
) {
2616 PyObject
*py_string
= 0;
2621 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
2623 l
= calc_binint(s
, 4);
2625 if ((*self
->read_func
)(self
, &s
, l
) < 0)
2628 UNLESS (py_string
= PyString_FromStringAndSize(s
, l
))
2631 PDATA_PUSH(self
->stack
, py_string
, -1);
2637 load_short_binstring(Unpicklerobject
*self
) {
2638 PyObject
*py_string
= 0;
2643 if ((*self
->read_func
)(self
, &s
, 1) < 0)
2646 l
= (unsigned char)s
[0];
2648 if ((*self
->read_func
)(self
, &s
, l
) < 0) return -1;
2650 UNLESS (py_string
= PyString_FromStringAndSize(s
, l
)) return -1;
2652 PDATA_PUSH(self
->stack
, py_string
, -1);
2658 load_tuple(Unpicklerobject
*self
) {
2662 if ((i
= marker(self
)) < 0) return -1;
2663 UNLESS (tup
=Pdata_popTuple(self
->stack
, i
)) return -1;
2664 PDATA_PUSH(self
->stack
, tup
, -1);
2669 load_empty_tuple(Unpicklerobject
*self
) {
2672 UNLESS (tup
=PyTuple_New(0)) return -1;
2673 PDATA_PUSH(self
->stack
, tup
, -1);
2678 load_empty_list(Unpicklerobject
*self
) {
2681 UNLESS (list
=PyList_New(0)) return -1;
2682 PDATA_PUSH(self
->stack
, list
, -1);
2687 load_empty_dict(Unpicklerobject
*self
) {
2690 UNLESS (dict
=PyDict_New()) return -1;
2691 PDATA_PUSH(self
->stack
, dict
, -1);
2697 load_list(Unpicklerobject
*self
) {
2701 if ((i
= marker(self
)) < 0) return -1;
2702 UNLESS (list
=Pdata_popList(self
->stack
, i
)) return -1;
2703 PDATA_PUSH(self
->stack
, list
, -1);
2708 load_dict(Unpicklerobject
*self
) {
2709 PyObject
*dict
, *key
, *value
;
2712 if ((i
= marker(self
)) < 0) return -1;
2713 j
=self
->stack
->length
;
2715 UNLESS (dict
= PyDict_New()) return -1;
2717 for (k
= i
+1; k
< j
; k
+= 2) {
2718 key
=self
->stack
->data
[k
-1];
2719 value
=self
->stack
->data
[k
];
2720 if (PyDict_SetItem(dict
, key
, value
) < 0) {
2725 Pdata_clear(self
->stack
, i
);
2726 PDATA_PUSH(self
->stack
, dict
, -1);
2731 Instance_New(PyObject
*cls
, PyObject
*args
) {
2733 PyObject
*safe
=0, *r
=0;
2735 if (PyClass_Check(cls
)) {
2738 if ((l
=PyObject_Length(args
)) < 0) goto err
;
2740 PyObject
*__getinitargs__
;
2742 UNLESS (__getinitargs__
=PyObject_GetAttr(cls
, __getinitargs___str
)) {
2743 /* We have a class with no __getinitargs__, so bypass usual
2745 PyInstanceObject
*inst
;
2748 UNLESS (inst
=PyObject_NEW(PyInstanceObject
, &PyInstance_Type
))
2750 inst
->in_class
=(PyClassObject
*)cls
;
2752 UNLESS (inst
->in_dict
=PyDict_New()) {
2757 return (PyObject
*)inst
;
2759 Py_DECREF(__getinitargs__
);
2762 if ((r
=PyInstance_New(cls
, args
, NULL
))) return r
;
2767 if ((has_key
= cPickle_PyMapping_HasKey(safe_constructors
, cls
)) < 0)
2771 if (!(safe
= PyObject_GetAttr(cls
, __safe_for_unpickling___str
)) ||
2772 !PyObject_IsTrue(safe
)) {
2773 cPickle_ErrFormat(UnpicklingError
,
2774 "%s is not safe for unpickling", "O", cls
);
2779 if (args
==Py_None
) {
2780 /* Special case, call cls.__basicnew__() */
2783 UNLESS (basicnew
=PyObject_GetAttr(cls
, __basicnew___str
)) return NULL
;
2784 r
=PyObject_CallObject(basicnew
, NULL
);
2785 Py_DECREF(basicnew
);
2789 if ((r
=PyObject_CallObject(cls
, args
))) return r
;
2793 PyObject
*tp
, *v
, *tb
;
2795 PyErr_Fetch(&tp
, &v
, &tb
);
2796 if ((r
=Py_BuildValue("OOO",v
,cls
,args
))) {
2800 PyErr_Restore(tp
,v
,tb
);
2807 load_obj(Unpicklerobject
*self
) {
2808 PyObject
*class, *tup
, *obj
=0;
2811 if ((i
= marker(self
)) < 0) return -1;
2812 UNLESS (tup
=Pdata_popTuple(self
->stack
, i
+1)) return -1;
2813 PDATA_POP(self
->stack
, class);
2815 obj
= Instance_New(class, tup
);
2820 if (! obj
) return -1;
2821 PDATA_PUSH(self
->stack
, obj
, -1);
2827 load_inst(Unpicklerobject
*self
) {
2828 PyObject
*tup
, *class, *obj
, *module_name
, *class_name
;
2832 if ((i
= marker(self
)) < 0) return -1;
2834 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2835 if (len
< 2) return bad_readline();
2836 UNLESS (module_name
= PyString_FromStringAndSize(s
, len
- 1)) return -1;
2838 if ((len
= (*self
->readline_func
)(self
, &s
)) >= 0) {
2839 if (len
< 2) return bad_readline();
2840 if (class_name
= PyString_FromStringAndSize(s
, len
- 1)) {
2841 class = find_class(module_name
, class_name
);
2842 Py_DECREF(class_name
);
2845 Py_DECREF(module_name
);
2847 if (! class) return -1;
2849 if (tup
=Pdata_popTuple(self
->stack
, i
)) {
2850 obj
= Instance_New(class, tup
);
2855 if (! obj
) return -1;
2857 PDATA_PUSH(self
->stack
, obj
, -1);
2863 load_global(Unpicklerobject
*self
) {
2864 PyObject
*class = 0, *module_name
= 0, *class_name
= 0;
2868 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2869 if (len
< 2) return bad_readline();
2870 UNLESS (module_name
= PyString_FromStringAndSize(s
, len
- 1)) return -1;
2872 if ((len
= (*self
->readline_func
)(self
, &s
)) >= 0) {
2873 if (len
< 2) return bad_readline();
2874 if (class_name
= PyString_FromStringAndSize(s
, len
- 1)) {
2875 class = find_class(module_name
, class_name
);
2876 Py_DECREF(class_name
);
2879 Py_DECREF(module_name
);
2881 if (! class) return -1;
2882 PDATA_PUSH(self
->stack
, class, -1);
2888 load_persid(Unpicklerobject
*self
) {
2893 if (self
->pers_func
) {
2894 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2895 if (len
< 2) return bad_readline();
2897 UNLESS (pid
= PyString_FromStringAndSize(s
, len
- 1)) return -1;
2899 if (PyList_Check(self
->pers_func
)) {
2900 if (PyList_Append(self
->pers_func
, pid
) < 0) {
2908 pid
= PyObject_CallObject(self
->pers_func
, self
->arg
);
2913 if (! pid
) return -1;
2915 PDATA_PUSH(self
->stack
, pid
, -1);
2919 PyErr_SetString(UnpicklingError
,
2920 "A load persistent id instruction was encountered,\n"
2921 "but no persistent_load function was specified.");
2927 load_binpersid(Unpicklerobject
*self
) {
2931 if (self
->pers_func
) {
2932 PDATA_POP(self
->stack
, pid
);
2933 if (! pid
) return -1;
2935 if (PyList_Check(self
->pers_func
)) {
2936 if (PyList_Append(self
->pers_func
, pid
) < 0) {
2944 pid
= PyObject_CallObject(self
->pers_func
, self
->arg
);
2947 if (! pid
) return -1;
2950 PDATA_PUSH(self
->stack
, pid
, -1);
2954 PyErr_SetString(UnpicklingError
,
2955 "A load persistent id instruction was encountered,\n"
2956 "but no persistent_load function was specified.");
2963 load_pop(Unpicklerobject
*self
) {
2966 UNLESS ((len
=self
->stack
->length
) > 0) return stackUnderflow();
2968 if ((self
->num_marks
> 0) &&
2969 (self
->marks
[self
->num_marks
- 1] == len
))
2972 Py_DECREF(self
->stack
->data
[--(self
->stack
->length
)]);
2979 load_pop_mark(Unpicklerobject
*self
) {
2982 if ((i
= marker(self
)) < 0)
2985 Pdata_clear(self
->stack
, i
);
2992 load_dup(Unpicklerobject
*self
) {
2996 if ((len
= self
->stack
->length
) <= 0) return stackUnderflow();
2997 last
=self
->stack
->data
[len
-1];
2999 PDATA_PUSH(self
->stack
, last
, -1);
3005 load_get(Unpicklerobject
*self
) {
3006 PyObject
*py_str
= 0, *value
= 0;
3010 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3011 if (len
< 2) return bad_readline();
3013 UNLESS (py_str
= PyString_FromStringAndSize(s
, len
- 1)) return -1;
3015 value
= PyDict_GetItem(self
->memo
, py_str
);
3018 PyErr_SetObject(BadPickleGet
, py_str
);
3022 PDATA_APPEND(self
->stack
, value
, -1);
3028 load_binget(Unpicklerobject
*self
) {
3029 PyObject
*py_key
= 0, *value
= 0;
3034 if ((*self
->read_func
)(self
, &s
, 1) < 0) return -1;
3036 key
= (unsigned char)s
[0];
3037 UNLESS (py_key
= PyInt_FromLong((long)key
)) return -1;
3039 value
= PyDict_GetItem(self
->memo
, py_key
);
3042 PyErr_SetObject(BadPickleGet
, py_key
);
3046 PDATA_APPEND(self
->stack
, value
, -1);
3052 load_long_binget(Unpicklerobject
*self
) {
3053 PyObject
*py_key
= 0, *value
= 0;
3054 unsigned char c
, *s
;
3058 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
3060 c
= (unsigned char)s
[0];
3062 c
= (unsigned char)s
[1];
3063 key
|= (long)c
<< 8;
3064 c
= (unsigned char)s
[2];
3065 key
|= (long)c
<< 16;
3066 c
= (unsigned char)s
[3];
3067 key
|= (long)c
<< 24;
3069 UNLESS (py_key
= PyInt_FromLong((long)key
)) return -1;
3071 value
= PyDict_GetItem(self
->memo
, py_key
);
3074 PyErr_SetObject(BadPickleGet
, py_key
);
3078 PDATA_APPEND(self
->stack
, value
, -1);
3084 load_put(Unpicklerobject
*self
) {
3085 PyObject
*py_str
= 0, *value
= 0;
3089 if ((l
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3090 if (l
< 2) return bad_readline();
3091 UNLESS (len
=self
->stack
->length
) return stackUnderflow();
3092 UNLESS (py_str
= PyString_FromStringAndSize(s
, l
- 1)) return -1;
3093 value
=self
->stack
->data
[len
-1];
3094 l
=PyDict_SetItem(self
->memo
, py_str
, value
);
3101 load_binput(Unpicklerobject
*self
) {
3102 PyObject
*py_key
= 0, *value
= 0;
3103 unsigned char key
, *s
;
3106 if ((*self
->read_func
)(self
, &s
, 1) < 0) return -1;
3107 UNLESS ((len
=self
->stack
->length
) > 0) return stackUnderflow();
3109 key
= (unsigned char)s
[0];
3111 UNLESS (py_key
= PyInt_FromLong((long)key
)) return -1;
3112 value
=self
->stack
->data
[len
-1];
3113 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
3120 load_long_binput(Unpicklerobject
*self
) {
3121 PyObject
*py_key
= 0, *value
= 0;
3123 unsigned char c
, *s
;
3126 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
3127 UNLESS (len
=self
->stack
->length
) return stackUnderflow();
3129 c
= (unsigned char)s
[0];
3131 c
= (unsigned char)s
[1];
3132 key
|= (long)c
<< 8;
3133 c
= (unsigned char)s
[2];
3134 key
|= (long)c
<< 16;
3135 c
= (unsigned char)s
[3];
3136 key
|= (long)c
<< 24;
3138 UNLESS (py_key
= PyInt_FromLong(key
)) return -1;
3139 value
=self
->stack
->data
[len
-1];
3140 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
3147 do_append(Unpicklerobject
*self
, int x
) {
3148 PyObject
*value
= 0, *list
= 0, *append_method
= 0;
3151 UNLESS ((len
=self
->stack
->length
) >= x
&& x
> 0) return stackUnderflow();
3152 if (len
==x
) return 0; /* nothing to do */
3154 list
=self
->stack
->data
[x
-1];
3156 if (PyList_Check(list
)) {
3160 slice
=Pdata_popList(self
->stack
, x
);
3161 list_len
= PyList_GET_SIZE(list
);
3162 i
=PyList_SetSlice(list
, list_len
, list_len
, slice
);
3168 UNLESS (append_method
= PyObject_GetAttr(list
, append_str
))
3171 for (i
= x
; i
< len
; i
++) {
3174 value
=self
->stack
->data
[i
];
3176 ARG_TUP(self
, value
);
3178 junk
= PyObject_CallObject(append_method
, self
->arg
);
3182 Pdata_clear(self
->stack
, i
+1);
3183 self
->stack
->length
=x
;
3184 Py_DECREF(append_method
);
3189 self
->stack
->length
=x
;
3190 Py_DECREF(append_method
);
3198 load_append(Unpicklerobject
*self
) {
3199 return do_append(self
, self
->stack
->length
- 1);
3204 load_appends(Unpicklerobject
*self
) {
3205 return do_append(self
, marker(self
));
3210 do_setitems(Unpicklerobject
*self
, int x
) {
3211 PyObject
*value
= 0, *key
= 0, *dict
= 0;
3214 UNLESS ((len
=self
->stack
->length
) >= x
3215 && x
> 0) return stackUnderflow();
3217 dict
=self
->stack
->data
[x
-1];
3219 for (i
= x
+1; i
< len
; i
+= 2) {
3220 key
=self
->stack
->data
[i
-1];
3221 value
=self
->stack
->data
[i
];
3222 if (PyObject_SetItem(dict
, key
, value
) < 0) {
3228 Pdata_clear(self
->stack
, x
);
3235 load_setitem(Unpicklerobject
*self
) {
3236 return do_setitems(self
, self
->stack
->length
- 2);
3240 load_setitems(Unpicklerobject
*self
) {
3241 return do_setitems(self
, marker(self
));
3246 load_build(Unpicklerobject
*self
) {
3247 PyObject
*value
= 0, *inst
= 0, *instdict
= 0, *d_key
= 0, *d_value
= 0,
3248 *junk
= 0, *__setstate__
= 0;
3251 if (self
->stack
->length
< 2) return stackUnderflow();
3252 PDATA_POP(self
->stack
, value
);
3253 if (! value
) return -1;
3254 inst
=self
->stack
->data
[self
->stack
->length
-1];
3256 if ((__setstate__
= PyObject_GetAttr(inst
, __setstate___str
))) {
3257 ARG_TUP(self
, value
);
3259 junk
= PyObject_CallObject(__setstate__
, self
->arg
);
3262 Py_DECREF(__setstate__
);
3263 if (! junk
) return -1;
3269 if ((instdict
= PyObject_GetAttr(inst
, __dict___str
))) {
3271 while (PyDict_Next(value
, &i
, &d_key
, &d_value
)) {
3272 if (PyObject_SetItem(instdict
, d_key
, d_value
) < 0) {
3277 Py_DECREF(instdict
);
3288 load_mark(Unpicklerobject
*self
) {
3291 if ((self
->num_marks
+ 1) >= self
->marks_size
) {
3292 s
=self
->marks_size
+20;
3293 if (s
<= self
->num_marks
) s
=self
->num_marks
+ 1;
3295 self
->marks
=(int *)malloc(s
* sizeof(int));
3297 self
->marks
=(int *)realloc(self
->marks
, s
* sizeof(int));
3298 if (! self
->marks
) {
3302 self
->marks_size
= s
;
3305 self
->marks
[self
->num_marks
++] = self
->stack
->length
;
3311 load_reduce(Unpicklerobject
*self
) {
3312 PyObject
*callable
= 0, *arg_tup
= 0, *ob
= 0;
3314 PDATA_POP(self
->stack
, arg_tup
);
3315 if (! arg_tup
) return -1;
3316 PDATA_POP(self
->stack
, callable
);
3318 ob
= Instance_New(callable
, arg_tup
);
3319 Py_DECREF(callable
);
3323 if (! ob
) return -1;
3325 PDATA_PUSH(self
->stack
, ob
, -1);
3330 load(Unpicklerobject
*self
) {
3331 PyObject
*stack
= 0, *err
= 0, *val
= 0;
3334 self
->num_marks
= 0;
3335 if (self
->stack
->length
) Pdata_clear(self
->stack
, 0);
3338 if ((*self
->read_func
)(self
, &s
, 1) < 0)
3343 if (load_none(self
) < 0)
3348 if (load_binint(self
) < 0)
3353 if (load_binint1(self
) < 0)
3358 if (load_binint2(self
) < 0)
3363 if (load_int(self
) < 0)
3368 if (load_long(self
) < 0)
3373 if (load_float(self
) < 0)
3378 if (load_binfloat(self
) < 0)
3383 if (load_binstring(self
) < 0)
3387 case SHORT_BINSTRING
:
3388 if (load_short_binstring(self
) < 0)
3393 if (load_string(self
) < 0)
3398 if (load_empty_tuple(self
) < 0)
3403 if (load_tuple(self
) < 0)
3408 if (load_empty_list(self
) < 0)
3413 if (load_list(self
) < 0)
3418 if (load_empty_dict(self
) < 0)
3423 if (load_dict(self
) < 0)
3428 if (load_obj(self
) < 0)
3433 if (load_inst(self
) < 0)
3438 if (load_global(self
) < 0)
3443 if (load_append(self
) < 0)
3448 if (load_appends(self
) < 0)
3453 if (load_build(self
) < 0)
3458 if (load_dup(self
) < 0)
3463 if (load_binget(self
) < 0)
3468 if (load_long_binget(self
) < 0)
3473 if (load_get(self
) < 0)
3478 if (load_mark(self
) < 0)
3483 if (load_binput(self
) < 0)
3488 if (load_long_binput(self
) < 0)
3493 if (load_put(self
) < 0)
3498 if (load_pop(self
) < 0)
3503 if (load_pop_mark(self
) < 0)
3508 if (load_setitem(self
) < 0)
3513 if (load_setitems(self
) < 0)
3521 if (load_persid(self
) < 0)
3526 if (load_binpersid(self
) < 0)
3531 if (load_reduce(self
) < 0)
3536 cPickle_ErrFormat(UnpicklingError
, "invalid load key, '%s'.",
3544 if ((err
= PyErr_Occurred())) {
3545 if (err
== PyExc_EOFError
) {
3546 PyErr_SetNone(PyExc_EOFError
);
3551 PDATA_POP(self
->stack
, val
);
3556 /* No-load functions to support noload, which is used to
3557 find persistent references. */
3560 noload_obj(Unpicklerobject
*self
) {
3563 if ((i
= marker(self
)) < 0) return -1;
3564 return Pdata_clear(self
->stack
, i
+1);
3569 noload_inst(Unpicklerobject
*self
) {
3573 if ((i
= marker(self
)) < 0) return -1;
3574 Pdata_clear(self
->stack
, i
);
3575 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3576 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3577 PDATA_APPEND(self
->stack
, Py_None
,-1);
3582 noload_global(Unpicklerobject
*self
) {
3585 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3586 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3587 PDATA_APPEND(self
->stack
, Py_None
,-1);
3592 noload_reduce(Unpicklerobject
*self
) {
3594 if (self
->stack
->length
< 2) return stackUnderflow();
3595 Pdata_clear(self
->stack
, self
->stack
->length
-2);
3596 PDATA_APPEND(self
->stack
, Py_None
,-1);
3601 noload_build(Unpicklerobject
*self
) {
3603 if (self
->stack
->length
< 1) return stackUnderflow();
3604 Pdata_clear(self
->stack
, self
->stack
->length
-1);
3610 noload(Unpicklerobject
*self
) {
3611 PyObject
*stack
= 0, *err
= 0, *val
= 0;
3614 self
->num_marks
= 0;
3615 Pdata_clear(self
->stack
, 0);
3618 if ((*self
->read_func
)(self
, &s
, 1) < 0)
3623 if (load_none(self
) < 0)
3628 if (load_binint(self
) < 0)
3633 if (load_binint1(self
) < 0)
3638 if (load_binint2(self
) < 0)
3643 if (load_int(self
) < 0)
3648 if (load_long(self
) < 0)
3653 if (load_float(self
) < 0)
3658 if (load_binfloat(self
) < 0)
3663 if (load_binstring(self
) < 0)
3667 case SHORT_BINSTRING
:
3668 if (load_short_binstring(self
) < 0)
3673 if (load_string(self
) < 0)
3678 if (load_empty_tuple(self
) < 0)
3683 if (load_tuple(self
) < 0)
3688 if (load_empty_list(self
) < 0)
3693 if (load_list(self
) < 0)
3698 if (load_empty_dict(self
) < 0)
3703 if (load_dict(self
) < 0)
3708 if (noload_obj(self
) < 0)
3713 if (noload_inst(self
) < 0)
3718 if (noload_global(self
) < 0)
3723 if (load_append(self
) < 0)
3728 if (load_appends(self
) < 0)
3733 if (noload_build(self
) < 0)
3738 if (load_dup(self
) < 0)
3743 if (load_binget(self
) < 0)
3748 if (load_long_binget(self
) < 0)
3753 if (load_get(self
) < 0)
3758 if (load_mark(self
) < 0)
3763 if (load_binput(self
) < 0)
3768 if (load_long_binput(self
) < 0)
3773 if (load_put(self
) < 0)
3778 if (load_pop(self
) < 0)
3783 if (load_pop_mark(self
) < 0)
3788 if (load_setitem(self
) < 0)
3793 if (load_setitems(self
) < 0)
3801 if (load_persid(self
) < 0)
3806 if (load_binpersid(self
) < 0)
3811 if (noload_reduce(self
) < 0)
3816 cPickle_ErrFormat(UnpicklingError
, "invalid load key, '%s'.",
3824 if ((err
= PyErr_Occurred())) {
3825 if (err
== PyExc_EOFError
) {
3826 PyErr_SetNone(PyExc_EOFError
);
3831 PDATA_POP(self
->stack
, val
);
3837 Unpickler_load(Unpicklerobject
*self
, PyObject
*args
) {
3838 UNLESS (PyArg_ParseTuple(args
, ""))
3845 Unpickler_noload(Unpicklerobject
*self
, PyObject
*args
) {
3846 UNLESS (PyArg_ParseTuple(args
, ""))
3849 return noload(self
);
3853 static struct PyMethodDef Unpickler_methods
[] = {
3854 {"load", (PyCFunction
)Unpickler_load
, 1,
3855 "load() -- Load a pickle"
3857 {"noload", (PyCFunction
)Unpickler_noload
, 1,
3858 "noload() -- not load a pickle, but go through most of the motions\n"
3860 "This function can be used to read past a pickle without instantiating\n"
3861 "any objects or importing any modules. It can also be used to find all\n"
3862 "persistent references without instantiating any objects or importing\n"
3865 {NULL
, NULL
} /* sentinel */
3869 static Unpicklerobject
*
3870 newUnpicklerobject(PyObject
*f
) {
3871 Unpicklerobject
*self
;
3873 UNLESS (self
= PyObject_NEW(Unpicklerobject
, &Unpicklertype
))
3878 self
->stack
= (Pdata
*)Pdata_New();
3879 self
->pers_func
= NULL
;
3880 self
->last_string
= NULL
;
3882 self
->num_marks
= 0;
3883 self
->marks_size
= 0;
3886 self
->readline
= NULL
;
3887 self
->safe_constructors
= NULL
;
3889 UNLESS (self
->memo
= PyDict_New()) {
3890 Py_XDECREF((PyObject
*)self
);
3897 /* Set read, readline based on type of f */
3898 if (PyFile_Check(f
)) {
3899 self
->fp
= PyFile_AsFile(f
);
3900 self
->read_func
= read_file
;
3901 self
->readline_func
= readline_file
;
3903 else if (PycStringIO_InputCheck(f
)) {
3905 self
->read_func
= read_cStringIO
;
3906 self
->readline_func
= readline_cStringIO
;
3911 self
->read_func
= read_other
;
3912 self
->readline_func
= readline_other
;
3914 UNLESS ((self
->readline
= PyObject_GetAttr(f
, readline_str
)) &&
3915 (self
->read
= PyObject_GetAttr(f
, read_str
))) {
3917 PyErr_SetString( PyExc_TypeError
, "argument must have 'read' and "
3918 "'readline' attributes" );
3923 if (PyEval_GetRestricted()) {
3924 /* Restricted execution, get private tables */
3927 UNLESS (m
=PyImport_Import(copy_reg_str
)) goto err
;
3928 self
->safe_constructors
=PyObject_GetAttr(m
, safe_constructors_str
);
3930 UNLESS (self
->safe_constructors
) goto err
;
3933 self
->safe_constructors
=safe_constructors
;
3934 Py_INCREF(safe_constructors
);
3940 Py_DECREF((PyObject
*)self
);
3946 get_Unpickler(PyObject
*self
, PyObject
*args
) {
3949 UNLESS (PyArg_ParseTuple(args
, "O", &file
))
3951 return (PyObject
*)newUnpicklerobject(file
);
3956 Unpickler_dealloc(Unpicklerobject
*self
) {
3957 Py_XDECREF(self
->readline
);
3958 Py_XDECREF(self
->read
);
3959 Py_XDECREF(self
->file
);
3960 Py_XDECREF(self
->memo
);
3961 Py_XDECREF(self
->stack
);
3962 Py_XDECREF(self
->pers_func
);
3963 Py_XDECREF(self
->arg
);
3964 Py_XDECREF(self
->last_string
);
3965 Py_XDECREF(self
->safe_constructors
);
3971 if (self
->buf_size
) {
3980 Unpickler_getattr(Unpicklerobject
*self
, char *name
) {
3981 if (!strcmp(name
, "persistent_load")) {
3982 if (!self
->pers_func
) {
3983 PyErr_SetString(PyExc_AttributeError
, name
);
3987 Py_INCREF(self
->pers_func
);
3988 return self
->pers_func
;
3991 if (!strcmp(name
, "memo")) {
3993 PyErr_SetString(PyExc_AttributeError
, name
);
3997 Py_INCREF(self
->memo
);
4001 if (!strcmp(name
, "UnpicklingError")) {
4002 Py_INCREF(UnpicklingError
);
4003 return UnpicklingError
;
4006 return Py_FindMethod(Unpickler_methods
, (PyObject
*)self
, name
);
4011 Unpickler_setattr(Unpicklerobject
*self
, char *name
, PyObject
*value
) {
4014 PyErr_SetString(PyExc_TypeError
,
4015 "attribute deletion is not supported");
4019 if (!strcmp(name
, "persistent_load")) {
4020 Py_XDECREF(self
->pers_func
);
4021 self
->pers_func
= value
;
4026 if (strcmp(name
, "memo") == 0) {
4027 if (! PyDict_Check(value
)) {
4028 PyErr_SetString(PyExc_TypeError
, "memo must be a dictionary");
4031 Py_XDECREF(self
->memo
);
4037 PyErr_SetString(PyExc_AttributeError
, name
);
4043 cpm_dump(PyObject
*self
, PyObject
*args
) {
4044 PyObject
*ob
, *file
, *res
= NULL
;
4045 Picklerobject
*pickler
= 0;
4048 UNLESS (PyArg_ParseTuple(args
, "OO|i", &ob
, &file
, &bin
))
4051 UNLESS (pickler
= newPicklerobject(file
, bin
))
4054 if (dump(pickler
, ob
) < 0)
4061 Py_XDECREF(pickler
);
4068 cpm_dumps(PyObject
*self
, PyObject
*args
) {
4069 PyObject
*ob
, *file
= 0, *res
= NULL
;
4070 Picklerobject
*pickler
= 0;
4073 UNLESS (PyArg_ParseTuple(args
, "O|i", &ob
, &bin
))
4076 UNLESS (file
= PycStringIO
->NewOutput(128))
4079 UNLESS (pickler
= newPicklerobject(file
, bin
))
4082 if (dump(pickler
, ob
) < 0)
4085 res
= PycStringIO
->cgetvalue(file
);
4088 Py_XDECREF(pickler
);
4096 cpm_load(PyObject
*self
, PyObject
*args
) {
4097 Unpicklerobject
*unpickler
= 0;
4098 PyObject
*ob
, *res
= NULL
;
4100 UNLESS (PyArg_ParseTuple(args
, "O", &ob
))
4103 UNLESS (unpickler
= newUnpicklerobject(ob
))
4106 res
= load(unpickler
);
4109 Py_XDECREF(unpickler
);
4116 cpm_loads(PyObject
*self
, PyObject
*args
) {
4117 PyObject
*ob
, *file
= 0, *res
= NULL
;
4118 Unpicklerobject
*unpickler
= 0;
4120 UNLESS (PyArg_ParseTuple(args
, "S", &ob
))
4123 UNLESS (file
= PycStringIO
->NewInput(ob
))
4126 UNLESS (unpickler
= newUnpicklerobject(file
))
4129 res
= load(unpickler
);
4133 Py_XDECREF(unpickler
);
4139 static char Unpicklertype__doc__
[] =
4140 "Objects that know how to unpickle";
4142 static PyTypeObject Unpicklertype
= {
4143 PyObject_HEAD_INIT(NULL
)
4145 "Unpickler", /*tp_name*/
4146 sizeof(Unpicklerobject
), /*tp_basicsize*/
4149 (destructor
)Unpickler_dealloc
, /*tp_dealloc*/
4150 (printfunc
)0, /*tp_print*/
4151 (getattrfunc
)Unpickler_getattr
, /*tp_getattr*/
4152 (setattrfunc
)Unpickler_setattr
, /*tp_setattr*/
4153 (cmpfunc
)0, /*tp_compare*/
4154 (reprfunc
)0, /*tp_repr*/
4156 0, /*tp_as_sequence*/
4157 0, /*tp_as_mapping*/
4158 (hashfunc
)0, /*tp_hash*/
4159 (ternaryfunc
)0, /*tp_call*/
4160 (reprfunc
)0, /*tp_str*/
4162 /* Space for future expansion */
4164 Unpicklertype__doc__
/* Documentation string */
4167 static struct PyMethodDef cPickle_methods
[] = {
4168 {"dump", (PyCFunction
)cpm_dump
, 1,
4169 "dump(object, file, [binary]) --"
4170 "Write an object in pickle format to the given file\n"
4172 "If the optional argument, binary, is provided and is true, then the\n"
4173 "pickle will be written in binary format, which is more space and\n"
4174 "computationally efficient. \n"
4176 {"dumps", (PyCFunction
)cpm_dumps
, 1,
4177 "dumps(object, [binary]) --"
4178 "Return a string containing an object in pickle format\n"
4180 "If the optional argument, binary, is provided and is true, then the\n"
4181 "pickle will be written in binary format, which is more space and\n"
4182 "computationally efficient. \n"
4184 {"load", (PyCFunction
)cpm_load
, 1,
4185 "load(file) -- Load a pickle from the given file"},
4186 {"loads", (PyCFunction
)cpm_loads
, 1,
4187 "loads(string) -- Load a pickle from the given string"},
4188 {"Pickler", (PyCFunction
)get_Pickler
, 1,
4189 "Pickler(file, [binary]) -- Create a pickler\n"
4191 "If the optional argument, binary, is provided and is true, then\n"
4192 "pickles will be written in binary format, which is more space and\n"
4193 "computationally efficient. \n"
4195 {"Unpickler", (PyCFunction
)get_Unpickler
, 1,
4196 "Unpickler(file) -- Create an unpickler"},
4201 #define CHECK_FOR_ERRORS(MESS) \
4202 if (PyErr_Occurred()) { \
4203 PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \
4204 PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \
4205 fprintf(stderr, # MESS ":\n\t"); \
4206 PyObject_Print(__sys_exc_type, stderr,0); \
4207 fprintf(stderr,", "); \
4208 PyObject_Print(__sys_exc_value, stderr,0); \
4209 fprintf(stderr,"\n"); \
4211 Py_FatalError(# MESS); \
4216 init_stuff(PyObject
*module
, PyObject
*module_dict
) {
4217 PyObject
*string
, *copy_reg
;
4219 #define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4221 INIT_STR(__class__
);
4222 INIT_STR(__getinitargs__
);
4224 INIT_STR(__getstate__
);
4225 INIT_STR(__setstate__
);
4228 INIT_STR(__reduce__
);
4230 INIT_STR(__safe_for_unpickling__
);
4235 INIT_STR(dispatch_table
);
4236 INIT_STR(safe_constructors
);
4237 INIT_STR(__basicnew__
);
4238 UNLESS (empty_str
=PyString_FromString("")) return -1;
4240 UNLESS (copy_reg
= PyImport_ImportModule("copy_reg"))
4243 /* These next few are special because we want to use different
4244 ones in restricted mode. */
4246 UNLESS (dispatch_table
= PyObject_GetAttr(copy_reg
, dispatch_table_str
))
4249 UNLESS (safe_constructors
= PyObject_GetAttr(copy_reg
,
4250 safe_constructors_str
))
4253 Py_DECREF(copy_reg
);
4255 /* Down to here ********************************** */
4257 UNLESS (string
= PyImport_ImportModule("string"))
4260 UNLESS (atol_func
= PyObject_GetAttrString(string
, "atol"))
4265 UNLESS (empty_tuple
= PyTuple_New(0))
4268 UNLESS (PicklingError
= PyString_FromString("cPickle.PicklingError"))
4271 if (PyDict_SetItemString(module_dict
, "PicklingError",
4275 UNLESS (UnpicklingError
= PyString_FromString("cPickle.UnpicklingError"))
4278 if (PyDict_SetItemString(module_dict
, "UnpicklingError",
4279 UnpicklingError
) < 0)
4282 UNLESS (BadPickleGet
= PyString_FromString("cPickle.BadPickleGet"))
4285 if (PyDict_SetItemString(module_dict
, "BadPickleGet",
4294 #ifndef DL_EXPORT /* declarations for DLL import/export */
4295 #define DL_EXPORT(RTYPE) RTYPE
4299 PyObject
*m
, *d
, *v
;
4301 PyObject
*format_version
;
4302 PyObject
*compatible_formats
;
4304 Picklertype
.ob_type
= &PyType_Type
;
4305 Unpicklertype
.ob_type
= &PyType_Type
;
4306 PdataType
.ob_type
= &PyType_Type
;
4308 /* Create the module and add the functions */
4309 m
= Py_InitModule4("cPickle", cPickle_methods
,
4310 cPickle_module_documentation
,
4311 (PyObject
*)NULL
,PYTHON_API_VERSION
);
4313 /* Add some symbolic constants to the module */
4314 d
= PyModule_GetDict(m
);
4315 PyDict_SetItemString(d
,"__version__", v
= PyString_FromString(rev
));
4318 format_version
= PyString_FromString("1.3");
4319 compatible_formats
= Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
4321 PyDict_SetItemString(d
, "format_version", format_version
);
4322 PyDict_SetItemString(d
, "compatible_formats", compatible_formats
);
4323 Py_XDECREF(format_version
);
4324 Py_XDECREF(compatible_formats
);
4327 CHECK_FOR_ERRORS("can't initialize module cPickle");