2 * cPickle.c,v 1.71 1999/07/11 13:30:34 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.71 1999/07/11 13:30:34 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
*PickleError
;
119 static PyObject
*PicklingError
;
120 static PyObject
*UnpickleableError
;
121 static PyObject
*UnpicklingError
;
122 static PyObject
*BadPickleGet
;
125 static PyObject
*dispatch_table
;
126 static PyObject
*safe_constructors
;
127 static PyObject
*empty_tuple
;
129 static PyObject
*__class___str
, *__getinitargs___str
, *__dict___str
,
130 *__getstate___str
, *__setstate___str
, *__name___str
, *__reduce___str
,
131 *write_str
, *__safe_for_unpickling___str
, *append_str
,
132 *read_str
, *readline_str
, *__main___str
, *__basicnew___str
,
133 *copy_reg_str
, *dispatch_table_str
, *safe_constructors_str
, *empty_str
;
138 #ifndef PyList_SET_ITEM
139 #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
141 #ifndef PyList_GET_SIZE
142 #define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
144 #ifndef PyTuple_SET_ITEM
145 #define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
147 #ifndef PyTuple_GET_SIZE
148 #define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
150 #ifndef PyString_GET_SIZE
151 #define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
154 /*************************************************************************
155 Internal Data type for pickle data. */
164 Pdata_dealloc(Pdata
*self
) {
168 for (i
=self
->length
, p
=self
->data
; --i
>= 0; p
++) Py_DECREF(*p
);
170 if (self
->data
) free(self
->data
);
175 static PyTypeObject PdataType
= {
176 PyObject_HEAD_INIT(NULL
) 0, "Pdata", sizeof(Pdata
), 0,
177 (destructor
)Pdata_dealloc
,
178 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
181 #define Pdata_Check(O) ((O)->ob_type == &PdataType)
187 UNLESS (self
= PyObject_NEW(Pdata
, &PdataType
)) return NULL
;
190 self
->data
=malloc(self
->size
* sizeof(PyObject
*));
191 if (self
->data
) return (PyObject
*)self
;
193 return PyErr_NoMemory();
198 PyErr_SetString(UnpicklingError
, "unpickling stack underflow");
203 Pdata_clear(Pdata
*self
, int clearto
) {
207 if (clearto
< 0) return stackUnderflow();
208 if (clearto
>= self
->length
) return 0;
210 for (i
=self
->length
, p
=self
->data
+clearto
; --i
>= clearto
; p
++)
212 self
->length
=clearto
;
219 Pdata_grow(Pdata
*self
) {
225 self
->data
= realloc(self
->data
, self
->size
*sizeof(PyObject
*));
234 #define PDATA_POP(D,V) { \
235 if ((D)->length) V=D->data[--((D)->length)]; \
237 PyErr_SetString(UnpicklingError, "bad pickle data"); \
244 Pdata_popTuple(Pdata
*self
, int start
) {
248 l
=self
->length
-start
;
249 UNLESS (r
=PyTuple_New(l
)) return NULL
;
250 for (i
=start
, j
=0 ; j
< l
; )
251 PyTuple_SET_ITEM(r
,j
++,self
->data
[i
++]);
258 Pdata_popList(Pdata
*self
, int start
) {
262 l
=self
->length
-start
;
263 UNLESS (r
=PyList_New(l
)) return NULL
;
264 for (i
=start
, j
=0 ; j
< l
; )
265 PyList_SET_ITEM(r
,j
++,self
->data
[i
++]);
271 #define PDATA_APPEND_(D,O,ER) { \
272 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
275 #define PDATA_APPEND(D,O,ER) { \
276 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
277 Pdata_grow((Pdata*)(D)) < 0) \
280 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
283 #define PDATA_PUSH(D,O,ER) { \
284 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
285 Pdata_grow((Pdata*)(D)) < 0) { \
289 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
292 /*************************************************************************/
294 #define ARG_TUP(self, o) { \
295 if (self->arg || (self->arg=PyTuple_New(1))) { \
296 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
297 PyTuple_SET_ITEM(self->arg,0,o); \
304 #define FREE_ARG_TUP(self) { \
305 if (self->arg->ob_refcnt > 1) { \
306 Py_DECREF(self->arg); \
319 PyObject
*inst_pers_func
;
321 int fast
; /* Fast mode doesn't save in memo, don't use if circ ref */
325 PyObject
*dispatch_table
;
328 staticforward PyTypeObject Picklertype
;
341 PyObject
*last_string
;
346 int (*readline_func
)();
349 PyObject
*safe_constructors
;
350 PyObject
*find_class
;
353 staticforward PyTypeObject Unpicklertype
;
356 cPickle_PyMapping_HasKey(PyObject
*o
, PyObject
*key
) {
359 if ((v
= PyObject_GetItem(o
,key
))) {
370 #ifdef HAVE_STDARG_PROTOTYPES
372 cPickle_ErrFormat(PyObject
*ErrType
, char *stringformat
, char *format
, ...) {
375 cPickle_ErrFormat(va_alist
) va_dcl
{
378 PyObject
*args
=0, *retval
=0;
379 #ifdef HAVE_STDARG_PROTOTYPES
380 va_start(va
, format
);
383 char *stringformat
, *format
;
385 ErrType
= va_arg(va
, PyObject
*);
386 stringformat
= va_arg(va
, char *);
387 format
= va_arg(va
, char *);
390 if (format
) args
= Py_VaBuildValue(format
, va
);
392 if (format
&& ! args
) return NULL
;
393 if (stringformat
&& !(retval
=PyString_FromString(stringformat
))) return NULL
;
398 v
=PyString_Format(retval
, args
);
401 if (! v
) return NULL
;
406 if (args
) retval
=args
;
408 PyErr_SetObject(ErrType
,Py_None
);
411 PyErr_SetObject(ErrType
,retval
);
417 write_file(Picklerobject
*self
, char *s
, int n
) {
422 if ((int)fwrite(s
, sizeof(char), n
, self
->fp
) != n
) {
423 PyErr_SetFromErrno(PyExc_IOError
);
431 write_cStringIO(Picklerobject
*self
, char *s
, int n
) {
436 if (PycStringIO
->cwrite((PyObject
*)self
->file
, s
, n
) != n
) {
444 write_none(Picklerobject
*self
, char *s
, int n
) {
445 if (s
== NULL
) return 0;
450 write_other(Picklerobject
*self
, char *s
, int n
) {
451 PyObject
*py_str
= 0, *junk
= 0;
454 UNLESS (self
->buf_size
) return 0;
456 PyString_FromStringAndSize(self
->write_buf
, self
->buf_size
))
460 if (self
->buf_size
&& (n
+ self
->buf_size
) > WRITE_BUF_SIZE
) {
461 if (write_other(self
, NULL
, 0) < 0)
465 if (n
> WRITE_BUF_SIZE
) {
467 PyString_FromStringAndSize(s
, n
))
471 memcpy(self
->write_buf
+ self
->buf_size
, s
, n
);
478 /* object with write method */
479 ARG_TUP(self
, py_str
);
481 junk
= PyObject_CallObject(self
->write
, self
->arg
);
484 if (junk
) Py_DECREF(junk
);
488 PDATA_PUSH(self
->file
, py_str
, -1);
496 read_file(Unpicklerobject
*self
, char **s
, int n
) {
498 if (self
->buf_size
== 0) {
501 size
= ((n
< 32) ? 32 : n
);
502 UNLESS (self
->buf
= (char *)malloc(size
* sizeof(char))) {
507 self
->buf_size
= size
;
509 else if (n
> self
->buf_size
) {
510 UNLESS (self
->buf
= (char *)realloc(self
->buf
, n
* sizeof(char))) {
518 if ((int)fread(self
->buf
, sizeof(char), n
, self
->fp
) != n
) {
519 if (feof(self
->fp
)) {
520 PyErr_SetNone(PyExc_EOFError
);
524 PyErr_SetFromErrno(PyExc_IOError
);
535 readline_file(Unpicklerobject
*self
, char **s
) {
538 if (self
->buf_size
== 0) {
539 UNLESS (self
->buf
= (char *)malloc(40 * sizeof(char))) {
549 for (; i
< (self
->buf_size
- 1); i
++) {
550 if (feof(self
->fp
) || (self
->buf
[i
] = getc(self
->fp
)) == '\n') {
551 self
->buf
[i
+ 1] = '\0';
557 UNLESS (self
->buf
= (char *)realloc(self
->buf
,
558 (self
->buf_size
* 2) * sizeof(char))) {
570 read_cStringIO(Unpicklerobject
*self
, char **s
, int n
) {
573 if (PycStringIO
->cread((PyObject
*)self
->file
, &ptr
, n
) != n
) {
574 PyErr_SetNone(PyExc_EOFError
);
585 readline_cStringIO(Unpicklerobject
*self
, char **s
) {
589 if ((n
= PycStringIO
->creadline((PyObject
*)self
->file
, &ptr
)) < 0) {
600 read_other(Unpicklerobject
*self
, char **s
, int n
) {
601 PyObject
*bytes
, *str
=0;
603 UNLESS (bytes
= PyInt_FromLong(n
)) return -1;
605 ARG_TUP(self
, bytes
);
607 str
= PyObject_CallObject(self
->read
, self
->arg
);
610 if (! str
) return -1;
612 Py_XDECREF(self
->last_string
);
613 self
->last_string
= str
;
615 if (! (*s
= PyString_AsString(str
))) return -1;
621 readline_other(Unpicklerobject
*self
, char **s
) {
625 UNLESS (str
= PyObject_CallObject(self
->readline
, empty_tuple
)) {
629 if ((str_size
= PyString_Size(str
)) < 0)
632 Py_XDECREF(self
->last_string
);
633 self
->last_string
= str
;
635 if (! (*s
= PyString_AsString(str
)))
643 pystrndup(char *s
, int l
) {
645 UNLESS (r
=malloc((l
+1)*sizeof(char))) return (char*)PyErr_NoMemory();
653 get(Picklerobject
*self
, PyObject
*id
) {
654 PyObject
*value
, *mv
;
659 UNLESS (mv
= PyDict_GetItem(self
->memo
, id
)) {
660 PyErr_SetObject(PyExc_KeyError
, id
);
664 UNLESS (value
= PyTuple_GetItem(mv
, 0))
667 UNLESS (PyInt_Check(value
)) {
668 PyErr_SetString(PicklingError
, "no int where int expected in memo");
671 c_value
= PyInt_AS_LONG((PyIntObject
*)value
);
675 sprintf(s
+ 1, "%ld\n", c_value
);
678 else if (Pdata_Check(self
->file
)) {
679 if (write_other(self
, NULL
, 0) < 0) return -1;
680 PDATA_APPEND(self
->file
, mv
, -1);
686 s
[1] = (int)(c_value
& 0xff);
691 s
[1] = (int)(c_value
& 0xff);
692 s
[2] = (int)((c_value
>> 8) & 0xff);
693 s
[3] = (int)((c_value
>> 16) & 0xff);
694 s
[4] = (int)((c_value
>> 24) & 0xff);
699 if ((*self
->write_func
)(self
, s
, len
) < 0)
707 put(Picklerobject
*self
, PyObject
*ob
) {
708 if (ob
->ob_refcnt
< 2 || self
->fast
)
711 return put2(self
, ob
);
716 put2(Picklerobject
*self
, PyObject
*ob
) {
718 int p
, len
, res
= -1;
719 PyObject
*py_ob_id
= 0, *memo_len
= 0, *t
= 0;
721 if (self
->fast
) return 0;
723 if ((p
= PyDict_Size(self
->memo
)) < 0)
726 p
++; /* Make sure memo keys are positive! */
728 UNLESS (py_ob_id
= PyInt_FromLong((long)ob
))
731 UNLESS (memo_len
= PyInt_FromLong(p
))
734 UNLESS (t
= PyTuple_New(2))
737 PyTuple_SET_ITEM(t
, 0, memo_len
);
739 PyTuple_SET_ITEM(t
, 1, ob
);
742 if (PyDict_SetItem(self
->memo
, py_ob_id
, t
) < 0)
747 sprintf(c_str
+ 1, "%d\n", p
);
750 else if (Pdata_Check(self
->file
)) {
751 if (write_other(self
, NULL
, 0) < 0) return -1;
752 PDATA_APPEND(self
->file
, memo_len
, -1);
753 res
=0; /* Job well done ;) */
758 c_str
[0] = LONG_BINPUT
;
759 c_str
[1] = (int)(p
& 0xff);
760 c_str
[2] = (int)((p
>> 8) & 0xff);
761 c_str
[3] = (int)((p
>> 16) & 0xff);
762 c_str
[4] = (int)((p
>> 24) & 0xff);
772 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
778 Py_XDECREF(py_ob_id
);
779 Py_XDECREF(memo_len
);
785 #define PyImport_Import cPickle_Import
788 PyImport_Import(PyObject
*module_name
) {
789 static PyObject
*silly_list
=0, *__builtins___str
=0, *__import___str
;
790 static PyObject
*standard_builtins
=0;
791 PyObject
*globals
=0, *__import__
=0, *__builtins__
=0, *r
=0;
793 UNLESS (silly_list
) {
794 UNLESS (__import___str
=PyString_FromString("__import__"))
796 UNLESS (__builtins___str
=PyString_FromString("__builtins__"))
798 UNLESS (silly_list
=Py_BuildValue("[s]","__doc__"))
802 if ((globals
=PyEval_GetGlobals())) {
804 UNLESS (__builtins__
=PyObject_GetItem(globals
,__builtins___str
))
810 UNLESS (standard_builtins
||
811 (standard_builtins
=PyImport_ImportModule("__builtin__")))
814 __builtins__
=standard_builtins
;
815 Py_INCREF(__builtins__
);
816 UNLESS (globals
= Py_BuildValue("{sO}", "__builtins__", __builtins__
))
820 if (PyDict_Check(__builtins__
)) {
821 UNLESS (__import__
=PyObject_GetItem(__builtins__
,__import___str
)) goto err
;
824 UNLESS (__import__
=PyObject_GetAttr(__builtins__
,__import___str
)) goto err
;
827 UNLESS (r
=PyObject_CallFunction(__import__
,"OOOO",
828 module_name
, globals
, globals
, silly_list
))
832 Py_DECREF(__builtins__
);
833 Py_DECREF(__import__
);
838 Py_XDECREF(__builtins__
);
839 Py_XDECREF(__import__
);
844 whichmodule(PyObject
*global
, PyObject
*global_name
) {
846 PyObject
*module
= 0, *modules_dict
= 0,
847 *global_name_attr
= 0, *name
= 0;
849 module
= PyObject_GetAttrString(global
, "__module__");
850 if (module
) return module
;
853 UNLESS (modules_dict
= PySys_GetObject("modules"))
857 while ((j
= PyDict_Next(modules_dict
, &i
, &name
, &module
))) {
859 if (PyObject_Compare(name
, __main___str
)==0) continue;
861 UNLESS (global_name_attr
= PyObject_GetAttr(module
, global_name
)) {
866 if (global_name_attr
!= global
) {
867 Py_DECREF(global_name_attr
);
871 Py_DECREF(global_name_attr
);
876 /* The following implements the rule in pickle.py added in 1.5
877 that used __main__ if no module is found. I don't actually
891 save_none(Picklerobject
*self
, PyObject
*args
) {
892 static char none
= NONE
;
893 if ((*self
->write_func
)(self
, &none
, 1) < 0)
901 save_int(Picklerobject
*self
, PyObject
*args
) {
903 long l
= PyInt_AS_LONG((PyIntObject
*)args
);
911 /* Save extra-long ints in non-binary mode, so that
912 we can use python long parsing code to restore,
915 sprintf(c_str
+ 1, "%ld\n", l
);
916 if ((*self
->write_func
)(self
, c_str
, strlen(c_str
)) < 0)
920 c_str
[1] = (int)( l
& 0xff);
921 c_str
[2] = (int)((l
>> 8) & 0xff);
922 c_str
[3] = (int)((l
>> 16) & 0xff);
923 c_str
[4] = (int)((l
>> 24) & 0xff);
925 if ((c_str
[4] == 0) && (c_str
[3] == 0)) {
940 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
949 save_long(Picklerobject
*self
, PyObject
*args
) {
953 static char l
= LONG
;
955 UNLESS (repr
= PyObject_Repr(args
))
958 if ((size
= PyString_Size(repr
)) < 0)
961 if ((*self
->write_func
)(self
, &l
, 1) < 0)
964 if ((*self
->write_func
)(self
,
965 PyString_AS_STRING((PyStringObject
*)repr
), size
) < 0)
968 if ((*self
->write_func
)(self
, "\n", 1) < 0)
981 save_float(Picklerobject
*self
, PyObject
*args
) {
982 double x
= PyFloat_AS_DOUBLE((PyFloatObject
*)args
);
988 char str
[9], *p
= str
;
1002 /* Normalize f to be in the range [1.0, 2.0) */
1003 if (0.5 <= f
&& f
< 1.0) {
1007 else if (f
== 0.0) {
1011 PyErr_SetString(PyExc_SystemError
,
1012 "frexp() result out of range");
1017 /* XXX 1024 itself is reserved for Inf/NaN */
1018 PyErr_SetString(PyExc_OverflowError
,
1019 "float too large to pack with d format");
1022 else if (e
< -1022) {
1023 /* Gradual underflow */
1024 f
= ldexp(f
, 1022 + e
);
1027 else if (!(e
== 0 && f
== 0.0)) {
1029 f
-= 1.0; /* Get rid of leading 1 */
1032 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1033 f
*= 268435456.0; /* 2**28 */
1034 fhi
= (long) floor(f
); /* Truncate */
1036 f
*= 16777216.0; /* 2**24 */
1037 flo
= (long) floor(f
+ 0.5); /* Round */
1040 *p
= (s
<<7) | (e
>>4);
1044 *p
= (char) (((e
&0xF)<<4) | (fhi
>>24));
1048 *p
= (fhi
>>16) & 0xFF;
1052 *p
= (fhi
>>8) & 0xFF;
1060 *p
= (flo
>>16) & 0xFF;
1064 *p
= (flo
>>8) & 0xFF;
1070 if ((*self
->write_func
)(self
, str
, 9) < 0)
1076 sprintf(c_str
+ 1, "%.17g\n", x
);
1078 if ((*self
->write_func
)(self
, c_str
, strlen(c_str
)) < 0)
1087 save_string(Picklerobject
*self
, PyObject
*args
, int doput
) {
1091 if ((size
= PyString_Size(args
)) < 0)
1097 static char string
= STRING
;
1099 UNLESS (repr
= PyObject_Repr(args
))
1102 if ((len
= PyString_Size(repr
)) < 0)
1104 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1106 if ((*self
->write_func
)(self
, &string
, 1) < 0)
1109 if ((*self
->write_func
)(self
, repr_str
, len
) < 0)
1112 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1121 if ((size
= PyString_Size(args
)) < 0)
1125 c_str
[0] = SHORT_BINSTRING
;
1130 c_str
[0] = BINSTRING
;
1131 for (i
= 1; i
< 5; i
++)
1132 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1136 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
1139 if (size
> 128 && Pdata_Check(self
->file
)) {
1140 if (write_other(self
, NULL
, 0) < 0) return -1;
1141 PDATA_APPEND(self
->file
, args
, -1);
1144 if ((*self
->write_func
)(self
,
1145 PyString_AS_STRING((PyStringObject
*)args
), size
) < 0)
1151 if (put(self
, args
) < 0)
1163 save_tuple(Picklerobject
*self
, PyObject
*args
) {
1164 PyObject
*element
= 0, *py_tuple_id
= 0;
1165 int len
, i
, has_key
, res
= -1;
1167 static char tuple
= TUPLE
;
1169 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1172 if ((len
= PyTuple_Size(args
)) < 0)
1175 for (i
= 0; i
< len
; i
++) {
1176 UNLESS (element
= PyTuple_GET_ITEM((PyTupleObject
*)args
, i
))
1179 if (save(self
, element
, 0) < 0)
1183 UNLESS (py_tuple_id
= PyInt_FromLong((long)args
))
1187 if ((has_key
= cPickle_PyMapping_HasKey(self
->memo
, py_tuple_id
)) < 0)
1192 static char pop_mark
= POP_MARK
;
1194 if ((*self
->write_func
)(self
, &pop_mark
, 1) < 0)
1198 static char pop
= POP
;
1200 for (i
= 0; i
<= len
; i
++) {
1201 if ((*self
->write_func
)(self
, &pop
, 1) < 0)
1206 if (get(self
, py_tuple_id
) < 0)
1214 if ((*self
->write_func
)(self
, &tuple
, 1) < 0) {
1218 if (put(self
, args
) < 0)
1224 Py_XDECREF(py_tuple_id
);
1230 save_empty_tuple(Picklerobject
*self
, PyObject
*args
) {
1231 static char tuple
= EMPTY_TUPLE
;
1233 return (*self
->write_func
)(self
, &tuple
, 1);
1238 save_list(Picklerobject
*self
, PyObject
*args
) {
1239 PyObject
*element
= 0;
1240 int s_len
, len
, i
, using_appends
, res
= -1;
1243 static char append
= APPEND
, appends
= APPENDS
;
1255 if ((len
= PyList_Size(args
)) < 0)
1258 if ((*self
->write_func
)(self
, s
, s_len
) < 0)
1262 if (put(self
, args
) < 0)
1266 if (put2(self
, args
) < 0)
1270 if ((using_appends
= (self
->bin
&& (len
> 1))))
1271 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1274 for (i
= 0; i
< len
; i
++) {
1275 UNLESS (element
= PyList_GET_ITEM((PyListObject
*)args
, i
))
1278 if (save(self
, element
, 0) < 0)
1281 if (!using_appends
) {
1282 if ((*self
->write_func
)(self
, &append
, 1) < 0)
1287 if (using_appends
) {
1288 if ((*self
->write_func
)(self
, &appends
, 1) < 0)
1301 save_dict(Picklerobject
*self
, PyObject
*args
) {
1302 PyObject
*key
= 0, *value
= 0;
1303 int i
, len
, res
= -1, using_setitems
;
1306 static char setitem
= SETITEM
, setitems
= SETITEMS
;
1318 if ((*self
->write_func
)(self
, s
, len
) < 0)
1321 if ((len
= PyDict_Size(args
)) < 0)
1325 if (put(self
, args
) < 0)
1329 if (put2(self
, args
) < 0)
1333 if ((using_setitems
= (self
->bin
&& (PyDict_Size(args
) > 1))))
1334 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1338 while (PyDict_Next(args
, &i
, &key
, &value
)) {
1339 if (save(self
, key
, 0) < 0)
1342 if (save(self
, value
, 0) < 0)
1345 if (!using_setitems
) {
1346 if ((*self
->write_func
)(self
, &setitem
, 1) < 0)
1351 if (using_setitems
) {
1352 if ((*self
->write_func
)(self
, &setitems
, 1) < 0)
1365 save_inst(Picklerobject
*self
, PyObject
*args
) {
1366 PyObject
*class = 0, *module
= 0, *name
= 0, *state
= 0,
1367 *getinitargs_func
= 0, *getstate_func
= 0, *class_args
= 0;
1368 char *module_str
, *name_str
;
1369 int module_size
, name_size
, res
= -1;
1371 static char inst
= INST
, obj
= OBJ
, build
= BUILD
;
1373 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1376 UNLESS (class = PyObject_GetAttr(args
, __class___str
))
1380 if (save(self
, class, 0) < 0)
1384 if ((getinitargs_func
= PyObject_GetAttr(args
, __getinitargs___str
))) {
1385 PyObject
*element
= 0;
1388 UNLESS (class_args
=
1389 PyObject_CallObject(getinitargs_func
, empty_tuple
))
1392 if ((len
= PyObject_Length(class_args
)) < 0)
1395 for (i
= 0; i
< len
; i
++) {
1396 UNLESS (element
= PySequence_GetItem(class_args
, i
))
1399 if (save(self
, element
, 0) < 0) {
1412 UNLESS (name
= ((PyClassObject
*)class)->cl_name
) {
1413 PyErr_SetString(PicklingError
, "class has no name");
1417 UNLESS (module
= whichmodule(class, name
))
1421 if ((module_size
= PyString_Size(module
)) < 0 ||
1422 (name_size
= PyString_Size(name
)) < 0)
1425 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
1426 name_str
= PyString_AS_STRING((PyStringObject
*)name
);
1428 if ((*self
->write_func
)(self
, &inst
, 1) < 0)
1431 if ((*self
->write_func
)(self
, module_str
, module_size
) < 0)
1434 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1437 if ((*self
->write_func
)(self
, name_str
, name_size
) < 0)
1440 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1443 else if ((*self
->write_func
)(self
, &obj
, 1) < 0) {
1447 if ((getstate_func
= PyObject_GetAttr(args
, __getstate___str
))) {
1448 UNLESS (state
= PyObject_CallObject(getstate_func
, empty_tuple
))
1454 UNLESS (state
= PyObject_GetAttr(args
, __dict___str
)) {
1461 if (!PyDict_Check(state
)) {
1462 if (put2(self
, args
) < 0)
1466 if (put(self
, args
) < 0)
1470 if (save(self
, state
, 0) < 0)
1473 if ((*self
->write_func
)(self
, &build
, 1) < 0)
1482 Py_XDECREF(getinitargs_func
);
1483 Py_XDECREF(getstate_func
);
1484 Py_XDECREF(class_args
);
1491 save_global(Picklerobject
*self
, PyObject
*args
, PyObject
*name
) {
1492 PyObject
*global_name
= 0, *module
= 0;
1493 char *name_str
, *module_str
;
1494 int module_size
, name_size
, res
= -1;
1496 static char global
= GLOBAL
;
1500 Py_INCREF(global_name
);
1503 UNLESS (global_name
= PyObject_GetAttr(args
, __name___str
))
1507 UNLESS (module
= whichmodule(args
, global_name
))
1510 if ((module_size
= PyString_Size(module
)) < 0 ||
1511 (name_size
= PyString_Size(global_name
)) < 0)
1514 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
1515 name_str
= PyString_AS_STRING((PyStringObject
*)global_name
);
1517 if ((*self
->write_func
)(self
, &global
, 1) < 0)
1520 if ((*self
->write_func
)(self
, module_str
, module_size
) < 0)
1523 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1526 if ((*self
->write_func
)(self
, name_str
, name_size
) < 0)
1529 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1532 if (put(self
, args
) < 0)
1539 Py_XDECREF(global_name
);
1545 save_pers(Picklerobject
*self
, PyObject
*args
, PyObject
*f
) {
1549 static char persid
= PERSID
, binpersid
= BINPERSID
;
1552 ARG_TUP(self
, args
);
1554 pid
= PyObject_CallObject(f
, self
->arg
);
1557 if (! pid
) return -1;
1559 if (pid
!= Py_None
) {
1561 if (!PyString_Check(pid
)) {
1562 PyErr_SetString(PicklingError
,
1563 "persistent id must be string");
1567 if ((*self
->write_func
)(self
, &persid
, 1) < 0)
1570 if ((size
= PyString_Size(pid
)) < 0)
1573 if ((*self
->write_func
)(self
,
1574 PyString_AS_STRING((PyStringObject
*)pid
), size
) < 0)
1577 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1583 else if (save(self
, pid
, 1) >= 0) {
1584 if ((*self
->write_func
)(self
, &binpersid
, 1) < 0)
1603 save_reduce(Picklerobject
*self
, PyObject
*callable
,
1604 PyObject
*tup
, PyObject
*state
, PyObject
*ob
) {
1605 static char reduce
= REDUCE
, build
= BUILD
;
1607 if (save(self
, callable
, 0) < 0)
1610 if (save(self
, tup
, 0) < 0)
1613 if ((*self
->write_func
)(self
, &reduce
, 1) < 0)
1617 if (state
&& !PyDict_Check(state
)) {
1618 if (put2(self
, ob
) < 0)
1622 if (put(self
, ob
) < 0)
1628 if (save(self
, state
, 0) < 0)
1631 if ((*self
->write_func
)(self
, &build
, 1) < 0)
1639 save(Picklerobject
*self
, PyObject
*args
, int pers_save
) {
1641 PyObject
*py_ob_id
= 0, *__reduce__
= 0, *t
= 0, *arg_tup
= 0,
1642 *callable
= 0, *state
= 0;
1643 int res
= -1, tmp
, size
;
1645 if (!pers_save
&& self
->pers_func
) {
1646 if ((tmp
= save_pers(self
, args
, self
->pers_func
)) != 0) {
1652 if (args
== Py_None
) {
1653 res
= save_none(self
, args
);
1657 type
= args
->ob_type
;
1659 switch (type
->tp_name
[0]) {
1661 if (type
== &PyInt_Type
) {
1662 res
= save_int(self
, args
);
1668 if (type
== &PyLong_Type
) {
1669 res
= save_long(self
, args
);
1675 if (type
== &PyFloat_Type
) {
1676 res
= save_float(self
, args
);
1682 if (type
== &PyTuple_Type
&& PyTuple_Size(args
)==0) {
1683 if (self
->bin
) res
= save_empty_tuple(self
, args
);
1684 else res
= save_tuple(self
, args
);
1690 if ((type
== &PyString_Type
) && (PyString_GET_SIZE(args
) < 2)) {
1691 res
= save_string(self
, args
, 0);
1696 if (args
->ob_refcnt
> 1) {
1702 UNLESS (py_ob_id
= PyInt_FromLong(ob_id
))
1705 if ((has_key
= cPickle_PyMapping_HasKey(self
->memo
, py_ob_id
)) < 0)
1709 if (get(self
, py_ob_id
) < 0)
1717 switch (type
->tp_name
[0]) {
1719 if (type
== &PyString_Type
) {
1720 res
= save_string(self
, args
, 1);
1726 if (type
== &PyTuple_Type
) {
1727 res
= save_tuple(self
, args
);
1733 if (type
== &PyList_Type
) {
1734 res
= save_list(self
, args
);
1740 if (type
== &PyDict_Type
) {
1741 res
= save_dict(self
, args
);
1747 if (type
== &PyInstance_Type
) {
1748 res
= save_inst(self
, args
);
1754 if (type
== &PyClass_Type
) {
1755 res
= save_global(self
, args
, NULL
);
1761 if (type
== &PyFunction_Type
) {
1762 res
= save_global(self
, args
, NULL
);
1768 if (type
== &PyCFunction_Type
) {
1769 res
= save_global(self
, args
, NULL
);
1774 if (!pers_save
&& self
->inst_pers_func
) {
1775 if ((tmp
= save_pers(self
, args
, self
->inst_pers_func
)) != 0) {
1781 if ((__reduce__
= PyDict_GetItem(dispatch_table
, (PyObject
*)type
))) {
1782 Py_INCREF(__reduce__
);
1785 ARG_TUP(self
, args
);
1787 t
= PyObject_CallObject(__reduce__
, self
->arg
);
1790 if (! t
) goto finally
;
1795 if ((__reduce__
= PyObject_GetAttr(args
, __reduce___str
))) {
1796 UNLESS (t
= PyObject_CallObject(__reduce__
, empty_tuple
))
1805 if (PyString_Check(t
)) {
1806 res
= save_global(self
, args
, t
);
1810 if (!PyTuple_Check(t
)) {
1811 cPickle_ErrFormat(PicklingError
, "Value returned by %s must "
1812 "be a tuple", "O", __reduce__
);
1816 size
= PyTuple_Size(t
);
1818 if ((size
!= 3) && (size
!= 2)) {
1819 cPickle_ErrFormat(PicklingError
, "tuple returned by %s must "
1820 "contain only two or three elements", "O", __reduce__
);
1824 callable
= PyTuple_GET_ITEM(t
, 0);
1826 arg_tup
= PyTuple_GET_ITEM(t
, 1);
1829 state
= PyTuple_GET_ITEM(t
, 2);
1832 UNLESS (PyTuple_Check(arg_tup
) || arg_tup
==Py_None
) {
1833 cPickle_ErrFormat(PicklingError
, "Second element of tuple "
1834 "returned by %s must be a tuple", "O", __reduce__
);
1838 res
= save_reduce(self
, callable
, arg_tup
, state
, args
);
1842 PyErr_SetObject(UnpickleableError
, args
);
1845 Py_XDECREF(py_ob_id
);
1846 Py_XDECREF(__reduce__
);
1854 dump(Picklerobject
*self
, PyObject
*args
) {
1855 static char stop
= STOP
;
1857 if (save(self
, args
, 0) < 0)
1860 if ((*self
->write_func
)(self
, &stop
, 1) < 0)
1863 if ((*self
->write_func
)(self
, NULL
, 0) < 0)
1870 Pickle_clear_memo(Picklerobject
*self
, PyObject
*args
) {
1871 if (args
&& ! PyArg_ParseTuple(args
,"")) return NULL
;
1872 if (self
->memo
) PyDict_Clear(self
->memo
);
1878 Pickle_getvalue(Picklerobject
*self
, PyObject
*args
) {
1879 int l
, i
, rsize
, ssize
, clear
=1, lm
;
1882 char *s
, *p
, *have_get
;
1885 if (args
&& ! PyArg_ParseTuple(args
,"|i",&clear
)) return NULL
;
1887 /* Check to make sure we are based on a list */
1888 if (! Pdata_Check(self
->file
)) {
1889 PyErr_SetString(PicklingError
,
1890 "Attempt to getvalue a non-list-based pickler");
1894 /* flush write buffer */
1895 if (write_other(self
, NULL
, 0) < 0) return NULL
;
1897 data
=(Pdata
*)self
->file
;
1900 /* set up an array to hold get/put status */
1901 if ((lm
=PyDict_Size(self
->memo
)) < 0) return NULL
;
1903 if (! (have_get
=malloc((lm
)*sizeof(char)))) return PyErr_NoMemory();
1904 memset(have_get
,0,lm
);
1906 /* Scan for gets. */
1907 for (rsize
=0, i
=l
; --i
>= 0; ) {
1910 if (PyString_Check(k
)) {
1911 rsize
+= PyString_GET_SIZE(k
);
1914 else if (PyInt_Check(k
)) { /* put */
1915 ik
=PyInt_AS_LONG((PyIntObject
*)k
);
1916 if (ik
>= lm
|| ik
==0) {
1917 PyErr_SetString(PicklingError
,
1918 "Invalid get data");
1921 if (have_get
[ik
]) { /* with matching get */
1922 if (ik
< 256) rsize
+= 2;
1927 else if (! (PyTuple_Check(k
) &&
1928 PyTuple_GET_SIZE(k
) == 2 &&
1929 PyInt_Check((k
=PyTuple_GET_ITEM(k
,0))))
1931 PyErr_SetString(PicklingError
,
1932 "Unexpected data in internal list");
1937 ik
=PyInt_AS_LONG((PyIntObject
*)k
);
1938 if (ik
>= lm
|| ik
==0) {
1939 PyErr_SetString(PicklingError
,
1940 "Invalid get data");
1944 if (ik
< 256) rsize
+= 2;
1950 /* Now generate the result */
1951 UNLESS (r
=PyString_FromStringAndSize(NULL
,rsize
)) goto err
;
1952 s
=PyString_AS_STRING((PyStringObject
*)r
);
1954 for (i
=0; i
<l
; i
++) {
1957 if (PyString_Check(k
)) {
1958 ssize
=PyString_GET_SIZE(k
);
1960 p
=PyString_AS_STRING((PyStringObject
*)k
);
1961 while (--ssize
>= 0) *s
++=*p
++;
1965 else if (PyTuple_Check(k
)) { /* get */
1966 ik
=PyInt_AS_LONG((PyIntObject
*)PyTuple_GET_ITEM(k
,0));
1969 *s
++ = (int)(ik
& 0xff);
1973 *s
++ = (int)(ik
& 0xff);
1974 *s
++ = (int)((ik
>> 8) & 0xff);
1975 *s
++ = (int)((ik
>> 16) & 0xff);
1976 *s
++ = (int)((ik
>> 24) & 0xff);
1981 ik
=PyInt_AS_LONG((PyIntObject
*)k
);
1983 if (have_get
[ik
]) { /* with matching get */
1986 *s
++ = (int)(ik
& 0xff);
1990 *s
++ = (int)(ik
& 0xff);
1991 *s
++ = (int)((ik
>> 8) & 0xff);
1992 *s
++ = (int)((ik
>> 16) & 0xff);
1993 *s
++ = (int)((ik
>> 24) & 0xff);
2001 PyDict_Clear(self
->memo
);
2002 Pdata_clear(data
,0);
2013 Pickler_dump(Picklerobject
*self
, PyObject
*args
) {
2017 UNLESS (PyArg_ParseTuple(args
, "O|i", &ob
, &get
))
2020 if (dump(self
, ob
) < 0)
2023 if (get
) return Pickle_getvalue(self
, NULL
);
2026 return (PyObject
*)self
;
2030 static struct PyMethodDef Pickler_methods
[] = {
2031 {"dump", (PyCFunction
)Pickler_dump
, 1,
2033 "Write an object in pickle format to the object's pickle stream\n"
2035 {"clear_memo", (PyCFunction
)Pickle_clear_memo
, 1,
2036 "clear_memo() -- Clear the picklers memo"},
2037 {"getvalue", (PyCFunction
)Pickle_getvalue
, 1,
2038 "getvalue() -- Finish picking a list-based pickle"},
2039 {NULL
, NULL
} /* sentinel */
2043 static Picklerobject
*
2044 newPicklerobject(PyObject
*file
, int bin
) {
2045 Picklerobject
*self
;
2047 UNLESS (self
= PyObject_NEW(Picklerobject
, &Picklertype
))
2054 self
->pers_func
= NULL
;
2055 self
->inst_pers_func
= NULL
;
2056 self
->write_buf
= NULL
;
2060 self
->dispatch_table
= NULL
;
2069 UNLESS (self
->memo
= PyDict_New()) {
2070 Py_XDECREF((PyObject
*)self
);
2074 if (PyFile_Check(file
)) {
2075 self
->fp
= PyFile_AsFile(file
);
2076 if (self
->fp
== NULL
) {
2077 PyErr_SetString(PyExc_IOError
, "output file closed");
2080 self
->write_func
= write_file
;
2082 else if (PycStringIO_OutputCheck(file
)) {
2083 self
->write_func
= write_cStringIO
;
2085 else if (file
== Py_None
) {
2086 self
->write_func
= write_none
;
2089 self
->write_func
= write_other
;
2091 if (! Pdata_Check(file
)) {
2092 UNLESS (self
->write
= PyObject_GetAttr(file
, write_str
)) {
2094 PyErr_SetString(PyExc_TypeError
, "argument must have 'write' "
2100 UNLESS (self
->write_buf
=
2101 (char *)malloc(WRITE_BUF_SIZE
* sizeof(char))) {
2107 if (PyEval_GetRestricted()) {
2108 /* Restricted execution, get private tables */
2111 UNLESS (m
=PyImport_Import(copy_reg_str
)) goto err
;
2112 self
->dispatch_table
=PyObject_GetAttr(m
, dispatch_table_str
);
2114 UNLESS (self
->dispatch_table
) goto err
;
2117 self
->dispatch_table
=dispatch_table
;
2118 Py_INCREF(dispatch_table
);
2124 Py_DECREF((PyObject
*)self
);
2130 get_Pickler(PyObject
*self
, PyObject
*args
) {
2131 PyObject
*file
=NULL
;
2135 if (! PyArg_ParseTuple(args
, "|i", &bin
)) {
2138 if (! PyArg_ParseTuple(args
, "O|i", &file
, &bin
))
2141 return (PyObject
*)newPicklerobject(file
, bin
);
2146 Pickler_dealloc(Picklerobject
*self
) {
2147 Py_XDECREF(self
->write
);
2148 Py_XDECREF(self
->memo
);
2149 Py_XDECREF(self
->arg
);
2150 Py_XDECREF(self
->file
);
2151 Py_XDECREF(self
->pers_func
);
2152 Py_XDECREF(self
->inst_pers_func
);
2153 Py_XDECREF(self
->dispatch_table
);
2155 if (self
->write_buf
) {
2156 free(self
->write_buf
);
2164 Pickler_getattr(Picklerobject
*self
, char *name
) {
2168 if (strcmp(name
, "persistent_id") == 0) {
2169 if (!self
->pers_func
) {
2170 PyErr_SetString(PyExc_AttributeError
, name
);
2174 Py_INCREF(self
->pers_func
);
2175 return self
->pers_func
;
2179 if (strcmp(name
, "memo") == 0) {
2181 PyErr_SetString(PyExc_AttributeError
, name
);
2185 Py_INCREF(self
->memo
);
2190 if (strcmp(name
, "PicklingError") == 0) {
2191 Py_INCREF(PicklingError
);
2192 return PicklingError
;
2196 if (strcmp(name
, "binary")==0)
2197 return PyInt_FromLong(self
->bin
);
2200 if (strcmp(name
, "fast")==0)
2201 return PyInt_FromLong(self
->fast
);
2204 if (strcmp(name
, "getvalue")==0 && ! Pdata_Check(self
->file
)) {
2205 PyErr_SetString(PyExc_AttributeError
, name
);
2210 return Py_FindMethod(Pickler_methods
, (PyObject
*)self
, name
);
2215 Pickler_setattr(Picklerobject
*self
, char *name
, PyObject
*value
) {
2218 PyErr_SetString(PyExc_TypeError
,
2219 "attribute deletion is not supported");
2223 if (strcmp(name
, "persistent_id") == 0) {
2224 Py_XDECREF(self
->pers_func
);
2225 self
->pers_func
= value
;
2230 if (strcmp(name
, "inst_persistent_id") == 0) {
2231 Py_XDECREF(self
->inst_pers_func
);
2232 self
->inst_pers_func
= value
;
2237 if (strcmp(name
, "memo") == 0) {
2238 if (! PyDict_Check(value
)) {
2239 PyErr_SetString(PyExc_TypeError
, "memo must be a dictionary");
2242 Py_XDECREF(self
->memo
);
2248 if (strcmp(name
, "binary")==0) {
2249 self
->bin
=PyObject_IsTrue(value
);
2253 if (strcmp(name
, "fast")==0) {
2254 self
->fast
=PyObject_IsTrue(value
);
2258 PyErr_SetString(PyExc_AttributeError
, name
);
2263 static char Picklertype__doc__
[] =
2264 "Objects that know how to pickle objects\n"
2267 static PyTypeObject Picklertype
= {
2268 PyObject_HEAD_INIT(NULL
)
2270 "Pickler", /*tp_name*/
2271 sizeof(Picklerobject
), /*tp_basicsize*/
2274 (destructor
)Pickler_dealloc
, /*tp_dealloc*/
2275 (printfunc
)0, /*tp_print*/
2276 (getattrfunc
)Pickler_getattr
, /*tp_getattr*/
2277 (setattrfunc
)Pickler_setattr
, /*tp_setattr*/
2278 (cmpfunc
)0, /*tp_compare*/
2279 (reprfunc
)0, /*tp_repr*/
2281 0, /*tp_as_sequence*/
2282 0, /*tp_as_mapping*/
2283 (hashfunc
)0, /*tp_hash*/
2284 (ternaryfunc
)0, /*tp_call*/
2285 (reprfunc
)0, /*tp_str*/
2287 /* Space for future expansion */
2289 Picklertype__doc__
/* Documentation string */
2293 find_class(PyObject
*py_module_name
, PyObject
*py_global_name
, PyObject
*fc
) {
2294 PyObject
*global
= 0, *module
;
2298 PyErr_SetString(UnpicklingError
,
2299 "Global and instance pickles are not supported.");
2302 return PyObject_CallFunction(fc
, "OO", py_module_name
, py_global_name
);
2305 module
= PySys_GetObject("modules");
2309 module
= PyDict_GetItem(module
, py_module_name
);
2310 if (module
== NULL
) {
2311 module
= PyImport_Import(py_module_name
);
2314 global
= PyObject_GetAttr(module
, py_global_name
);
2318 global
= PyObject_GetAttr(module
, py_global_name
);
2319 if (global
== NULL
) {
2321 sprintf(buf
, "Failed to import class %.128s from module %.128s",
2322 PyString_AS_STRING((PyStringObject
*)py_global_name
),
2323 PyString_AS_STRING((PyStringObject
*)py_module_name
));
2324 PyErr_SetString(PyExc_SystemError
, buf
);
2331 marker(Unpicklerobject
*self
) {
2332 if (self
->num_marks
< 1) {
2333 PyErr_SetString(UnpicklingError
, "could not find MARK");
2337 return self
->marks
[--self
->num_marks
];
2342 load_none(Unpicklerobject
*self
) {
2343 PDATA_APPEND(self
->stack
, Py_None
, -1);
2349 PyErr_SetString(UnpicklingError
, "pickle data was truncated");
2354 load_int(Unpicklerobject
*self
) {
2355 PyObject
*py_int
= 0;
2360 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2361 if (len
< 2) return bad_readline();
2362 UNLESS (s
=pystrndup(s
,len
)) return -1;
2365 l
= strtol(s
, &endptr
, 0);
2367 if (errno
|| (*endptr
!= '\n') || (endptr
[1] != '\0')) {
2368 /* Hm, maybe we've got something long. Let's try reading
2369 it as a Python long object. */
2371 UNLESS (py_int
=PyLong_FromString(s
,&endptr
,0)) goto finally
;
2373 if ((*endptr
!= '\n') || (endptr
[1] != '\0')) {
2374 PyErr_SetString(PyExc_ValueError
,
2375 "could not convert string to int");
2380 UNLESS (py_int
= PyInt_FromLong(l
)) goto finally
;
2384 PDATA_PUSH(self
->stack
, py_int
, -1);
2395 calc_binint(char *s
, int x
) {
2400 for (i
= 0, l
= 0L; i
< x
; i
++) {
2401 c
= (unsigned char)s
[i
];
2402 l
|= (long)c
<< (i
* 8);
2410 load_binintx(Unpicklerobject
*self
, char *s
, int x
) {
2411 PyObject
*py_int
= 0;
2414 l
= calc_binint(s
, x
);
2416 UNLESS (py_int
= PyInt_FromLong(l
))
2419 PDATA_PUSH(self
->stack
, py_int
, -1);
2425 load_binint(Unpicklerobject
*self
) {
2428 if ((*self
->read_func
)(self
, &s
, 4) < 0)
2431 return load_binintx(self
, s
, 4);
2436 load_binint1(Unpicklerobject
*self
) {
2439 if ((*self
->read_func
)(self
, &s
, 1) < 0)
2442 return load_binintx(self
, s
, 1);
2447 load_binint2(Unpicklerobject
*self
) {
2450 if ((*self
->read_func
)(self
, &s
, 2) < 0)
2453 return load_binintx(self
, s
, 2);
2457 load_long(Unpicklerobject
*self
) {
2462 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2463 if (len
< 2) return bad_readline();
2464 UNLESS (s
=pystrndup(s
,len
)) return -1;
2466 UNLESS (l
= PyLong_FromString(s
, &end
, 0))
2470 PDATA_PUSH(self
->stack
, l
, -1);
2481 load_float(Unpicklerobject
*self
) {
2482 PyObject
*py_float
= 0;
2487 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2488 if (len
< 2) return bad_readline();
2489 UNLESS (s
=pystrndup(s
,len
)) return -1;
2492 d
= strtod(s
, &endptr
);
2494 if (errno
|| (endptr
[0] != '\n') || (endptr
[1] != '\0')) {
2495 PyErr_SetString(PyExc_ValueError
,
2496 "could not convert string to float");
2500 UNLESS (py_float
= PyFloat_FromDouble(d
))
2504 PDATA_PUSH(self
->stack
, py_float
, -1);
2514 load_binfloat(Unpicklerobject
*self
) {
2515 PyObject
*py_float
= 0;
2521 if ((*self
->read_func
)(self
, &p
, 8) < 0)
2526 e
= (*p
& 0x7F) << 4;
2531 fhi
= (*p
& 0xF) << 24;
2535 fhi
|= (*p
& 0xFF) << 16;
2539 fhi
|= (*p
& 0xFF) << 8;
2547 flo
= (*p
& 0xFF) << 16;
2551 flo
|= (*p
& 0xFF) << 8;
2557 x
= (double)fhi
+ (double)flo
/ 16777216.0; /* 2**24 */
2558 x
/= 268435456.0; /* 2**28 */
2560 /* XXX This sadly ignores Inf/NaN */
2572 UNLESS (py_float
= PyFloat_FromDouble(x
)) return -1;
2574 PDATA_PUSH(self
->stack
, py_float
, -1);
2579 load_string(Unpicklerobject
*self
) {
2581 int len
, res
= -1, nslash
;
2584 static PyObject
*eval_dict
= 0;
2586 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2587 if (len
< 2) return bad_readline();
2588 UNLESS (s
=pystrndup(s
,len
)) return -1;
2590 /* Check for unquoted quotes (evil strings) */
2592 if (q
!= '"' && q
!= '\'') goto insecure
;
2593 for (p
=s
+1, nslash
=0; *p
; p
++) {
2594 if (*p
==q
&& nslash
%2==0) break;
2595 if (*p
=='\\') nslash
++;
2600 for (p
++; *p
; p
++) if (*p
> ' ') goto insecure
;
2603 /********************************************/
2606 UNLESS (eval_dict
= Py_BuildValue("{s{}}", "__builtins__"))
2609 UNLESS (str
= PyRun_String(s
, Py_eval_input
, eval_dict
, eval_dict
))
2613 PDATA_PUSH(self
->stack
, str
, -1);
2623 PyErr_SetString(PyExc_ValueError
,"insecure string pickle");
2629 load_binstring(Unpicklerobject
*self
) {
2630 PyObject
*py_string
= 0;
2634 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
2636 l
= calc_binint(s
, 4);
2638 if ((*self
->read_func
)(self
, &s
, l
) < 0)
2641 UNLESS (py_string
= PyString_FromStringAndSize(s
, l
))
2644 PDATA_PUSH(self
->stack
, py_string
, -1);
2650 load_short_binstring(Unpicklerobject
*self
) {
2651 PyObject
*py_string
= 0;
2655 if ((*self
->read_func
)(self
, &s
, 1) < 0)
2658 l
= (unsigned char)s
[0];
2660 if ((*self
->read_func
)(self
, &s
, l
) < 0) return -1;
2662 UNLESS (py_string
= PyString_FromStringAndSize(s
, l
)) return -1;
2664 PDATA_PUSH(self
->stack
, py_string
, -1);
2670 load_tuple(Unpicklerobject
*self
) {
2674 if ((i
= marker(self
)) < 0) return -1;
2675 UNLESS (tup
=Pdata_popTuple(self
->stack
, i
)) return -1;
2676 PDATA_PUSH(self
->stack
, tup
, -1);
2681 load_empty_tuple(Unpicklerobject
*self
) {
2684 UNLESS (tup
=PyTuple_New(0)) return -1;
2685 PDATA_PUSH(self
->stack
, tup
, -1);
2690 load_empty_list(Unpicklerobject
*self
) {
2693 UNLESS (list
=PyList_New(0)) return -1;
2694 PDATA_PUSH(self
->stack
, list
, -1);
2699 load_empty_dict(Unpicklerobject
*self
) {
2702 UNLESS (dict
=PyDict_New()) return -1;
2703 PDATA_PUSH(self
->stack
, dict
, -1);
2709 load_list(Unpicklerobject
*self
) {
2713 if ((i
= marker(self
)) < 0) return -1;
2714 UNLESS (list
=Pdata_popList(self
->stack
, i
)) return -1;
2715 PDATA_PUSH(self
->stack
, list
, -1);
2720 load_dict(Unpicklerobject
*self
) {
2721 PyObject
*dict
, *key
, *value
;
2724 if ((i
= marker(self
)) < 0) return -1;
2725 j
=self
->stack
->length
;
2727 UNLESS (dict
= PyDict_New()) return -1;
2729 for (k
= i
+1; k
< j
; k
+= 2) {
2730 key
=self
->stack
->data
[k
-1];
2731 value
=self
->stack
->data
[k
];
2732 if (PyDict_SetItem(dict
, key
, value
) < 0) {
2737 Pdata_clear(self
->stack
, i
);
2738 PDATA_PUSH(self
->stack
, dict
, -1);
2743 Instance_New(PyObject
*cls
, PyObject
*args
) {
2745 PyObject
*safe
=0, *r
=0;
2747 if (PyClass_Check(cls
)) {
2750 if ((l
=PyObject_Length(args
)) < 0) goto err
;
2752 PyObject
*__getinitargs__
;
2754 UNLESS (__getinitargs__
=PyObject_GetAttr(cls
, __getinitargs___str
)) {
2755 /* We have a class with no __getinitargs__, so bypass usual
2757 PyInstanceObject
*inst
;
2760 UNLESS (inst
=PyObject_NEW(PyInstanceObject
, &PyInstance_Type
))
2762 inst
->in_class
=(PyClassObject
*)cls
;
2764 UNLESS (inst
->in_dict
=PyDict_New()) {
2769 return (PyObject
*)inst
;
2771 Py_DECREF(__getinitargs__
);
2774 if ((r
=PyInstance_New(cls
, args
, NULL
))) return r
;
2779 if ((has_key
= cPickle_PyMapping_HasKey(safe_constructors
, cls
)) < 0)
2783 if (!(safe
= PyObject_GetAttr(cls
, __safe_for_unpickling___str
)) ||
2784 !PyObject_IsTrue(safe
)) {
2785 cPickle_ErrFormat(UnpicklingError
,
2786 "%s is not safe for unpickling", "O", cls
);
2791 if (args
==Py_None
) {
2792 /* Special case, call cls.__basicnew__() */
2795 UNLESS (basicnew
=PyObject_GetAttr(cls
, __basicnew___str
)) return NULL
;
2796 r
=PyObject_CallObject(basicnew
, NULL
);
2797 Py_DECREF(basicnew
);
2801 if ((r
=PyObject_CallObject(cls
, args
))) return r
;
2805 PyObject
*tp
, *v
, *tb
;
2807 PyErr_Fetch(&tp
, &v
, &tb
);
2808 if ((r
=Py_BuildValue("OOO",v
,cls
,args
))) {
2812 PyErr_Restore(tp
,v
,tb
);
2819 load_obj(Unpicklerobject
*self
) {
2820 PyObject
*class, *tup
, *obj
=0;
2823 if ((i
= marker(self
)) < 0) return -1;
2824 UNLESS (tup
=Pdata_popTuple(self
->stack
, i
+1)) return -1;
2825 PDATA_POP(self
->stack
, class);
2827 obj
= Instance_New(class, tup
);
2832 if (! obj
) return -1;
2833 PDATA_PUSH(self
->stack
, obj
, -1);
2839 load_inst(Unpicklerobject
*self
) {
2840 PyObject
*tup
, *class=0, *obj
=0, *module_name
, *class_name
;
2844 if ((i
= marker(self
)) < 0) return -1;
2846 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2847 if (len
< 2) return bad_readline();
2848 UNLESS (module_name
= PyString_FromStringAndSize(s
, len
- 1)) return -1;
2850 if ((len
= (*self
->readline_func
)(self
, &s
)) >= 0) {
2851 if (len
< 2) return bad_readline();
2852 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
2853 class = find_class(module_name
, class_name
, self
->find_class
);
2854 Py_DECREF(class_name
);
2857 Py_DECREF(module_name
);
2859 if (! class) return -1;
2861 if ((tup
=Pdata_popTuple(self
->stack
, i
))) {
2862 obj
= Instance_New(class, tup
);
2867 if (! obj
) return -1;
2869 PDATA_PUSH(self
->stack
, obj
, -1);
2875 load_global(Unpicklerobject
*self
) {
2876 PyObject
*class = 0, *module_name
= 0, *class_name
= 0;
2880 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2881 if (len
< 2) return bad_readline();
2882 UNLESS (module_name
= PyString_FromStringAndSize(s
, len
- 1)) return -1;
2884 if ((len
= (*self
->readline_func
)(self
, &s
)) >= 0) {
2885 if (len
< 2) return bad_readline();
2886 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
2887 class = find_class(module_name
, class_name
, self
->find_class
);
2888 Py_DECREF(class_name
);
2891 Py_DECREF(module_name
);
2893 if (! class) return -1;
2894 PDATA_PUSH(self
->stack
, class, -1);
2900 load_persid(Unpicklerobject
*self
) {
2905 if (self
->pers_func
) {
2906 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2907 if (len
< 2) return bad_readline();
2909 UNLESS (pid
= PyString_FromStringAndSize(s
, len
- 1)) return -1;
2911 if (PyList_Check(self
->pers_func
)) {
2912 if (PyList_Append(self
->pers_func
, pid
) < 0) {
2920 pid
= PyObject_CallObject(self
->pers_func
, self
->arg
);
2925 if (! pid
) return -1;
2927 PDATA_PUSH(self
->stack
, pid
, -1);
2931 PyErr_SetString(UnpicklingError
,
2932 "A load persistent id instruction was encountered,\n"
2933 "but no persistent_load function was specified.");
2939 load_binpersid(Unpicklerobject
*self
) {
2942 if (self
->pers_func
) {
2943 PDATA_POP(self
->stack
, pid
);
2944 if (! pid
) return -1;
2946 if (PyList_Check(self
->pers_func
)) {
2947 if (PyList_Append(self
->pers_func
, pid
) < 0) {
2955 pid
= PyObject_CallObject(self
->pers_func
, self
->arg
);
2958 if (! pid
) return -1;
2961 PDATA_PUSH(self
->stack
, pid
, -1);
2965 PyErr_SetString(UnpicklingError
,
2966 "A load persistent id instruction was encountered,\n"
2967 "but no persistent_load function was specified.");
2974 load_pop(Unpicklerobject
*self
) {
2977 UNLESS ((len
=self
->stack
->length
) > 0) return stackUnderflow();
2979 if ((self
->num_marks
> 0) &&
2980 (self
->marks
[self
->num_marks
- 1] == len
))
2983 Py_DECREF(self
->stack
->data
[--(self
->stack
->length
)]);
2990 load_pop_mark(Unpicklerobject
*self
) {
2993 if ((i
= marker(self
)) < 0)
2996 Pdata_clear(self
->stack
, i
);
3003 load_dup(Unpicklerobject
*self
) {
3007 if ((len
= self
->stack
->length
) <= 0) return stackUnderflow();
3008 last
=self
->stack
->data
[len
-1];
3010 PDATA_PUSH(self
->stack
, last
, -1);
3016 load_get(Unpicklerobject
*self
) {
3017 PyObject
*py_str
= 0, *value
= 0;
3022 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3023 if (len
< 2) return bad_readline();
3025 UNLESS (py_str
= PyString_FromStringAndSize(s
, len
- 1)) return -1;
3027 value
= PyDict_GetItem(self
->memo
, py_str
);
3029 PyErr_SetObject(BadPickleGet
, py_str
);
3032 PDATA_APPEND(self
->stack
, value
, -1);
3042 load_binget(Unpicklerobject
*self
) {
3043 PyObject
*py_key
= 0, *value
= 0;
3048 if ((*self
->read_func
)(self
, &s
, 1) < 0) return -1;
3050 key
= (unsigned char)s
[0];
3051 UNLESS (py_key
= PyInt_FromLong((long)key
)) return -1;
3053 value
= PyDict_GetItem(self
->memo
, py_key
);
3055 PyErr_SetObject(BadPickleGet
, py_key
);
3058 PDATA_APPEND(self
->stack
, value
, -1);
3068 load_long_binget(Unpicklerobject
*self
) {
3069 PyObject
*py_key
= 0, *value
= 0;
3070 unsigned char c
, *s
;
3074 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
3076 c
= (unsigned char)s
[0];
3078 c
= (unsigned char)s
[1];
3079 key
|= (long)c
<< 8;
3080 c
= (unsigned char)s
[2];
3081 key
|= (long)c
<< 16;
3082 c
= (unsigned char)s
[3];
3083 key
|= (long)c
<< 24;
3085 UNLESS (py_key
= PyInt_FromLong((long)key
)) return -1;
3087 value
= PyDict_GetItem(self
->memo
, py_key
);
3089 PyErr_SetObject(BadPickleGet
, py_key
);
3092 PDATA_APPEND(self
->stack
, value
, -1);
3102 load_put(Unpicklerobject
*self
) {
3103 PyObject
*py_str
= 0, *value
= 0;
3107 if ((l
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3108 if (l
< 2) return bad_readline();
3109 UNLESS (len
=self
->stack
->length
) return stackUnderflow();
3110 UNLESS (py_str
= PyString_FromStringAndSize(s
, l
- 1)) return -1;
3111 value
=self
->stack
->data
[len
-1];
3112 l
=PyDict_SetItem(self
->memo
, py_str
, value
);
3119 load_binput(Unpicklerobject
*self
) {
3120 PyObject
*py_key
= 0, *value
= 0;
3121 unsigned char key
, *s
;
3124 if ((*self
->read_func
)(self
, &s
, 1) < 0) return -1;
3125 UNLESS ((len
=self
->stack
->length
) > 0) return stackUnderflow();
3127 key
= (unsigned char)s
[0];
3129 UNLESS (py_key
= PyInt_FromLong((long)key
)) return -1;
3130 value
=self
->stack
->data
[len
-1];
3131 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
3138 load_long_binput(Unpicklerobject
*self
) {
3139 PyObject
*py_key
= 0, *value
= 0;
3141 unsigned char c
, *s
;
3144 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
3145 UNLESS (len
=self
->stack
->length
) return stackUnderflow();
3147 c
= (unsigned char)s
[0];
3149 c
= (unsigned char)s
[1];
3150 key
|= (long)c
<< 8;
3151 c
= (unsigned char)s
[2];
3152 key
|= (long)c
<< 16;
3153 c
= (unsigned char)s
[3];
3154 key
|= (long)c
<< 24;
3156 UNLESS (py_key
= PyInt_FromLong(key
)) return -1;
3157 value
=self
->stack
->data
[len
-1];
3158 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
3165 do_append(Unpicklerobject
*self
, int x
) {
3166 PyObject
*value
= 0, *list
= 0, *append_method
= 0;
3169 UNLESS ((len
=self
->stack
->length
) >= x
&& x
> 0) return stackUnderflow();
3170 if (len
==x
) return 0; /* nothing to do */
3172 list
=self
->stack
->data
[x
-1];
3174 if (PyList_Check(list
)) {
3178 slice
=Pdata_popList(self
->stack
, x
);
3179 list_len
= PyList_GET_SIZE(list
);
3180 i
=PyList_SetSlice(list
, list_len
, list_len
, slice
);
3186 UNLESS (append_method
= PyObject_GetAttr(list
, append_str
))
3189 for (i
= x
; i
< len
; i
++) {
3192 value
=self
->stack
->data
[i
];
3194 ARG_TUP(self
, value
);
3196 junk
= PyObject_CallObject(append_method
, self
->arg
);
3200 Pdata_clear(self
->stack
, i
+1);
3201 self
->stack
->length
=x
;
3202 Py_DECREF(append_method
);
3207 self
->stack
->length
=x
;
3208 Py_DECREF(append_method
);
3216 load_append(Unpicklerobject
*self
) {
3217 return do_append(self
, self
->stack
->length
- 1);
3222 load_appends(Unpicklerobject
*self
) {
3223 return do_append(self
, marker(self
));
3228 do_setitems(Unpicklerobject
*self
, int x
) {
3229 PyObject
*value
= 0, *key
= 0, *dict
= 0;
3232 UNLESS ((len
=self
->stack
->length
) >= x
3233 && x
> 0) return stackUnderflow();
3235 dict
=self
->stack
->data
[x
-1];
3237 for (i
= x
+1; i
< len
; i
+= 2) {
3238 key
=self
->stack
->data
[i
-1];
3239 value
=self
->stack
->data
[i
];
3240 if (PyObject_SetItem(dict
, key
, value
) < 0) {
3246 Pdata_clear(self
->stack
, x
);
3253 load_setitem(Unpicklerobject
*self
) {
3254 return do_setitems(self
, self
->stack
->length
- 2);
3258 load_setitems(Unpicklerobject
*self
) {
3259 return do_setitems(self
, marker(self
));
3264 load_build(Unpicklerobject
*self
) {
3265 PyObject
*value
= 0, *inst
= 0, *instdict
= 0, *d_key
= 0, *d_value
= 0,
3266 *junk
= 0, *__setstate__
= 0;
3269 if (self
->stack
->length
< 2) return stackUnderflow();
3270 PDATA_POP(self
->stack
, value
);
3271 if (! value
) return -1;
3272 inst
=self
->stack
->data
[self
->stack
->length
-1];
3274 if ((__setstate__
= PyObject_GetAttr(inst
, __setstate___str
))) {
3275 ARG_TUP(self
, value
);
3277 junk
= PyObject_CallObject(__setstate__
, self
->arg
);
3280 Py_DECREF(__setstate__
);
3281 if (! junk
) return -1;
3287 if ((instdict
= PyObject_GetAttr(inst
, __dict___str
))) {
3289 while (PyDict_Next(value
, &i
, &d_key
, &d_value
)) {
3290 if (PyObject_SetItem(instdict
, d_key
, d_value
) < 0) {
3295 Py_DECREF(instdict
);
3306 load_mark(Unpicklerobject
*self
) {
3309 if ((self
->num_marks
+ 1) >= self
->marks_size
) {
3310 s
=self
->marks_size
+20;
3311 if (s
<= self
->num_marks
) s
=self
->num_marks
+ 1;
3312 if (self
->marks
== NULL
)
3313 self
->marks
=(int *)malloc(s
* sizeof(int));
3315 self
->marks
=(int *)realloc(self
->marks
, s
* sizeof(int));
3316 if (! self
->marks
) {
3320 self
->marks_size
= s
;
3323 self
->marks
[self
->num_marks
++] = self
->stack
->length
;
3329 load_reduce(Unpicklerobject
*self
) {
3330 PyObject
*callable
= 0, *arg_tup
= 0, *ob
= 0;
3332 PDATA_POP(self
->stack
, arg_tup
);
3333 if (! arg_tup
) return -1;
3334 PDATA_POP(self
->stack
, callable
);
3336 ob
= Instance_New(callable
, arg_tup
);
3337 Py_DECREF(callable
);
3341 if (! ob
) return -1;
3343 PDATA_PUSH(self
->stack
, ob
, -1);
3348 load(Unpicklerobject
*self
) {
3349 PyObject
*err
= 0, *val
= 0;
3352 self
->num_marks
= 0;
3353 if (self
->stack
->length
) Pdata_clear(self
->stack
, 0);
3356 if ((*self
->read_func
)(self
, &s
, 1) < 0)
3361 if (load_none(self
) < 0)
3366 if (load_binint(self
) < 0)
3371 if (load_binint1(self
) < 0)
3376 if (load_binint2(self
) < 0)
3381 if (load_int(self
) < 0)
3386 if (load_long(self
) < 0)
3391 if (load_float(self
) < 0)
3396 if (load_binfloat(self
) < 0)
3401 if (load_binstring(self
) < 0)
3405 case SHORT_BINSTRING
:
3406 if (load_short_binstring(self
) < 0)
3411 if (load_string(self
) < 0)
3416 if (load_empty_tuple(self
) < 0)
3421 if (load_tuple(self
) < 0)
3426 if (load_empty_list(self
) < 0)
3431 if (load_list(self
) < 0)
3436 if (load_empty_dict(self
) < 0)
3441 if (load_dict(self
) < 0)
3446 if (load_obj(self
) < 0)
3451 if (load_inst(self
) < 0)
3456 if (load_global(self
) < 0)
3461 if (load_append(self
) < 0)
3466 if (load_appends(self
) < 0)
3471 if (load_build(self
) < 0)
3476 if (load_dup(self
) < 0)
3481 if (load_binget(self
) < 0)
3486 if (load_long_binget(self
) < 0)
3491 if (load_get(self
) < 0)
3496 if (load_mark(self
) < 0)
3501 if (load_binput(self
) < 0)
3506 if (load_long_binput(self
) < 0)
3511 if (load_put(self
) < 0)
3516 if (load_pop(self
) < 0)
3521 if (load_pop_mark(self
) < 0)
3526 if (load_setitem(self
) < 0)
3531 if (load_setitems(self
) < 0)
3539 if (load_persid(self
) < 0)
3544 if (load_binpersid(self
) < 0)
3549 if (load_reduce(self
) < 0)
3554 cPickle_ErrFormat(UnpicklingError
, "invalid load key, '%s'.",
3562 if ((err
= PyErr_Occurred())) {
3563 if (err
== PyExc_EOFError
) {
3564 PyErr_SetNone(PyExc_EOFError
);
3569 PDATA_POP(self
->stack
, val
);
3574 /* No-load functions to support noload, which is used to
3575 find persistent references. */
3578 noload_obj(Unpicklerobject
*self
) {
3581 if ((i
= marker(self
)) < 0) return -1;
3582 return Pdata_clear(self
->stack
, i
+1);
3587 noload_inst(Unpicklerobject
*self
) {
3591 if ((i
= marker(self
)) < 0) return -1;
3592 Pdata_clear(self
->stack
, i
);
3593 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3594 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3595 PDATA_APPEND(self
->stack
, Py_None
,-1);
3600 noload_global(Unpicklerobject
*self
) {
3603 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3604 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3605 PDATA_APPEND(self
->stack
, Py_None
,-1);
3610 noload_reduce(Unpicklerobject
*self
) {
3612 if (self
->stack
->length
< 2) return stackUnderflow();
3613 Pdata_clear(self
->stack
, self
->stack
->length
-2);
3614 PDATA_APPEND(self
->stack
, Py_None
,-1);
3619 noload_build(Unpicklerobject
*self
) {
3621 if (self
->stack
->length
< 1) return stackUnderflow();
3622 Pdata_clear(self
->stack
, self
->stack
->length
-1);
3628 noload(Unpicklerobject
*self
) {
3629 PyObject
*err
= 0, *val
= 0;
3632 self
->num_marks
= 0;
3633 Pdata_clear(self
->stack
, 0);
3636 if ((*self
->read_func
)(self
, &s
, 1) < 0)
3641 if (load_none(self
) < 0)
3646 if (load_binint(self
) < 0)
3651 if (load_binint1(self
) < 0)
3656 if (load_binint2(self
) < 0)
3661 if (load_int(self
) < 0)
3666 if (load_long(self
) < 0)
3671 if (load_float(self
) < 0)
3676 if (load_binfloat(self
) < 0)
3681 if (load_binstring(self
) < 0)
3685 case SHORT_BINSTRING
:
3686 if (load_short_binstring(self
) < 0)
3691 if (load_string(self
) < 0)
3696 if (load_empty_tuple(self
) < 0)
3701 if (load_tuple(self
) < 0)
3706 if (load_empty_list(self
) < 0)
3711 if (load_list(self
) < 0)
3716 if (load_empty_dict(self
) < 0)
3721 if (load_dict(self
) < 0)
3726 if (noload_obj(self
) < 0)
3731 if (noload_inst(self
) < 0)
3736 if (noload_global(self
) < 0)
3741 if (load_append(self
) < 0)
3746 if (load_appends(self
) < 0)
3751 if (noload_build(self
) < 0)
3756 if (load_dup(self
) < 0)
3761 if (load_binget(self
) < 0)
3766 if (load_long_binget(self
) < 0)
3771 if (load_get(self
) < 0)
3776 if (load_mark(self
) < 0)
3781 if (load_binput(self
) < 0)
3786 if (load_long_binput(self
) < 0)
3791 if (load_put(self
) < 0)
3796 if (load_pop(self
) < 0)
3801 if (load_pop_mark(self
) < 0)
3806 if (load_setitem(self
) < 0)
3811 if (load_setitems(self
) < 0)
3819 if (load_persid(self
) < 0)
3824 if (load_binpersid(self
) < 0)
3829 if (noload_reduce(self
) < 0)
3834 cPickle_ErrFormat(UnpicklingError
, "invalid load key, '%s'.",
3842 if ((err
= PyErr_Occurred())) {
3843 if (err
== PyExc_EOFError
) {
3844 PyErr_SetNone(PyExc_EOFError
);
3849 PDATA_POP(self
->stack
, val
);
3855 Unpickler_load(Unpicklerobject
*self
, PyObject
*args
) {
3856 UNLESS (PyArg_ParseTuple(args
, ""))
3863 Unpickler_noload(Unpicklerobject
*self
, PyObject
*args
) {
3864 UNLESS (PyArg_ParseTuple(args
, ""))
3867 return noload(self
);
3871 static struct PyMethodDef Unpickler_methods
[] = {
3872 {"load", (PyCFunction
)Unpickler_load
, 1,
3873 "load() -- Load a pickle"
3875 {"noload", (PyCFunction
)Unpickler_noload
, 1,
3876 "noload() -- not load a pickle, but go through most of the motions\n"
3878 "This function can be used to read past a pickle without instantiating\n"
3879 "any objects or importing any modules. It can also be used to find all\n"
3880 "persistent references without instantiating any objects or importing\n"
3883 {NULL
, NULL
} /* sentinel */
3887 static Unpicklerobject
*
3888 newUnpicklerobject(PyObject
*f
) {
3889 Unpicklerobject
*self
;
3891 UNLESS (self
= PyObject_NEW(Unpicklerobject
, &Unpicklertype
))
3896 self
->stack
= (Pdata
*)Pdata_New();
3897 self
->pers_func
= NULL
;
3898 self
->last_string
= NULL
;
3900 self
->num_marks
= 0;
3901 self
->marks_size
= 0;
3904 self
->readline
= NULL
;
3905 self
->safe_constructors
= NULL
;
3906 self
->find_class
= NULL
;
3908 UNLESS (self
->memo
= PyDict_New()) {
3909 Py_XDECREF((PyObject
*)self
);
3916 /* Set read, readline based on type of f */
3917 if (PyFile_Check(f
)) {
3918 self
->fp
= PyFile_AsFile(f
);
3919 if (self
->fp
== NULL
) {
3920 PyErr_SetString(PyExc_IOError
, "input file closed");
3923 self
->read_func
= read_file
;
3924 self
->readline_func
= readline_file
;
3926 else if (PycStringIO_InputCheck(f
)) {
3928 self
->read_func
= read_cStringIO
;
3929 self
->readline_func
= readline_cStringIO
;
3934 self
->read_func
= read_other
;
3935 self
->readline_func
= readline_other
;
3937 UNLESS ((self
->readline
= PyObject_GetAttr(f
, readline_str
)) &&
3938 (self
->read
= PyObject_GetAttr(f
, read_str
))) {
3940 PyErr_SetString( PyExc_TypeError
, "argument must have 'read' and "
3941 "'readline' attributes" );
3946 if (PyEval_GetRestricted()) {
3947 /* Restricted execution, get private tables */
3950 UNLESS (m
=PyImport_Import(copy_reg_str
)) goto err
;
3951 self
->safe_constructors
=PyObject_GetAttr(m
, safe_constructors_str
);
3953 UNLESS (self
->safe_constructors
) goto err
;
3956 self
->safe_constructors
=safe_constructors
;
3957 Py_INCREF(safe_constructors
);
3963 Py_DECREF((PyObject
*)self
);
3969 get_Unpickler(PyObject
*self
, PyObject
*args
) {
3972 UNLESS (PyArg_ParseTuple(args
, "O", &file
))
3974 return (PyObject
*)newUnpicklerobject(file
);
3979 Unpickler_dealloc(Unpicklerobject
*self
) {
3980 Py_XDECREF(self
->readline
);
3981 Py_XDECREF(self
->read
);
3982 Py_XDECREF(self
->file
);
3983 Py_XDECREF(self
->memo
);
3984 Py_XDECREF(self
->stack
);
3985 Py_XDECREF(self
->pers_func
);
3986 Py_XDECREF(self
->arg
);
3987 Py_XDECREF(self
->last_string
);
3988 Py_XDECREF(self
->safe_constructors
);
3994 if (self
->buf_size
) {
4003 Unpickler_getattr(Unpicklerobject
*self
, char *name
) {
4004 if (!strcmp(name
, "persistent_load")) {
4005 if (!self
->pers_func
) {
4006 PyErr_SetString(PyExc_AttributeError
, name
);
4010 Py_INCREF(self
->pers_func
);
4011 return self
->pers_func
;
4014 if (!strcmp(name
, "find_global")) {
4015 if (!self
->find_class
) {
4016 PyErr_SetString(PyExc_AttributeError
, name
);
4020 Py_INCREF(self
->find_class
);
4021 return self
->find_class
;
4024 if (!strcmp(name
, "memo")) {
4026 PyErr_SetString(PyExc_AttributeError
, name
);
4030 Py_INCREF(self
->memo
);
4034 if (!strcmp(name
, "UnpicklingError")) {
4035 Py_INCREF(UnpicklingError
);
4036 return UnpicklingError
;
4039 return Py_FindMethod(Unpickler_methods
, (PyObject
*)self
, name
);
4044 Unpickler_setattr(Unpicklerobject
*self
, char *name
, PyObject
*value
) {
4046 if (!strcmp(name
, "persistent_load")) {
4047 Py_XDECREF(self
->pers_func
);
4048 self
->pers_func
= value
;
4053 if (!strcmp(name
, "find_global")) {
4054 Py_XDECREF(self
->find_class
);
4055 self
->find_class
= value
;
4061 PyErr_SetString(PyExc_TypeError
,
4062 "attribute deletion is not supported");
4066 if (strcmp(name
, "memo") == 0) {
4067 if (! PyDict_Check(value
)) {
4068 PyErr_SetString(PyExc_TypeError
, "memo must be a dictionary");
4071 Py_XDECREF(self
->memo
);
4077 PyErr_SetString(PyExc_AttributeError
, name
);
4083 cpm_dump(PyObject
*self
, PyObject
*args
) {
4084 PyObject
*ob
, *file
, *res
= NULL
;
4085 Picklerobject
*pickler
= 0;
4088 UNLESS (PyArg_ParseTuple(args
, "OO|i", &ob
, &file
, &bin
))
4091 UNLESS (pickler
= newPicklerobject(file
, bin
))
4094 if (dump(pickler
, ob
) < 0)
4101 Py_XDECREF(pickler
);
4108 cpm_dumps(PyObject
*self
, PyObject
*args
) {
4109 PyObject
*ob
, *file
= 0, *res
= NULL
;
4110 Picklerobject
*pickler
= 0;
4113 UNLESS (PyArg_ParseTuple(args
, "O|i", &ob
, &bin
))
4116 UNLESS (file
= PycStringIO
->NewOutput(128))
4119 UNLESS (pickler
= newPicklerobject(file
, bin
))
4122 if (dump(pickler
, ob
) < 0)
4125 res
= PycStringIO
->cgetvalue(file
);
4128 Py_XDECREF(pickler
);
4136 cpm_load(PyObject
*self
, PyObject
*args
) {
4137 Unpicklerobject
*unpickler
= 0;
4138 PyObject
*ob
, *res
= NULL
;
4140 UNLESS (PyArg_ParseTuple(args
, "O", &ob
))
4143 UNLESS (unpickler
= newUnpicklerobject(ob
))
4146 res
= load(unpickler
);
4149 Py_XDECREF(unpickler
);
4156 cpm_loads(PyObject
*self
, PyObject
*args
) {
4157 PyObject
*ob
, *file
= 0, *res
= NULL
;
4158 Unpicklerobject
*unpickler
= 0;
4160 UNLESS (PyArg_ParseTuple(args
, "S", &ob
))
4163 UNLESS (file
= PycStringIO
->NewInput(ob
))
4166 UNLESS (unpickler
= newUnpicklerobject(file
))
4169 res
= load(unpickler
);
4173 Py_XDECREF(unpickler
);
4179 static char Unpicklertype__doc__
[] =
4180 "Objects that know how to unpickle";
4182 static PyTypeObject Unpicklertype
= {
4183 PyObject_HEAD_INIT(NULL
)
4185 "Unpickler", /*tp_name*/
4186 sizeof(Unpicklerobject
), /*tp_basicsize*/
4189 (destructor
)Unpickler_dealloc
, /*tp_dealloc*/
4190 (printfunc
)0, /*tp_print*/
4191 (getattrfunc
)Unpickler_getattr
, /*tp_getattr*/
4192 (setattrfunc
)Unpickler_setattr
, /*tp_setattr*/
4193 (cmpfunc
)0, /*tp_compare*/
4194 (reprfunc
)0, /*tp_repr*/
4196 0, /*tp_as_sequence*/
4197 0, /*tp_as_mapping*/
4198 (hashfunc
)0, /*tp_hash*/
4199 (ternaryfunc
)0, /*tp_call*/
4200 (reprfunc
)0, /*tp_str*/
4202 /* Space for future expansion */
4204 Unpicklertype__doc__
/* Documentation string */
4207 static struct PyMethodDef cPickle_methods
[] = {
4208 {"dump", (PyCFunction
)cpm_dump
, 1,
4209 "dump(object, file, [binary]) --"
4210 "Write an object in pickle format to the given file\n"
4212 "If the optional argument, binary, is provided and is true, then the\n"
4213 "pickle will be written in binary format, which is more space and\n"
4214 "computationally efficient. \n"
4216 {"dumps", (PyCFunction
)cpm_dumps
, 1,
4217 "dumps(object, [binary]) --"
4218 "Return a string containing an object in pickle format\n"
4220 "If the optional argument, binary, is provided and is true, then the\n"
4221 "pickle will be written in binary format, which is more space and\n"
4222 "computationally efficient. \n"
4224 {"load", (PyCFunction
)cpm_load
, 1,
4225 "load(file) -- Load a pickle from the given file"},
4226 {"loads", (PyCFunction
)cpm_loads
, 1,
4227 "loads(string) -- Load a pickle from the given string"},
4228 {"Pickler", (PyCFunction
)get_Pickler
, 1,
4229 "Pickler(file, [binary]) -- Create a pickler\n"
4231 "If the optional argument, binary, is provided and is true, then\n"
4232 "pickles will be written in binary format, which is more space and\n"
4233 "computationally efficient. \n"
4235 {"Unpickler", (PyCFunction
)get_Unpickler
, 1,
4236 "Unpickler(file) -- Create an unpickler"},
4241 init_stuff(PyObject
*module
, PyObject
*module_dict
) {
4242 PyObject
*string
, *copy_reg
, *t
, *r
;
4244 #define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4246 INIT_STR(__class__
);
4247 INIT_STR(__getinitargs__
);
4249 INIT_STR(__getstate__
);
4250 INIT_STR(__setstate__
);
4253 INIT_STR(__reduce__
);
4255 INIT_STR(__safe_for_unpickling__
);
4260 INIT_STR(dispatch_table
);
4261 INIT_STR(safe_constructors
);
4262 INIT_STR(__basicnew__
);
4263 UNLESS (empty_str
=PyString_FromString("")) return -1;
4265 UNLESS (copy_reg
= PyImport_ImportModule("copy_reg"))
4268 /* These next few are special because we want to use different
4269 ones in restricted mode. */
4271 UNLESS (dispatch_table
= PyObject_GetAttr(copy_reg
, dispatch_table_str
))
4274 UNLESS (safe_constructors
= PyObject_GetAttr(copy_reg
,
4275 safe_constructors_str
))
4278 Py_DECREF(copy_reg
);
4280 /* Down to here ********************************** */
4282 UNLESS (string
= PyImport_ImportModule("string"))
4285 UNLESS (atol_func
= PyObject_GetAttrString(string
, "atol"))
4290 UNLESS (empty_tuple
= PyTuple_New(0))
4294 UNLESS (t
=PyImport_ImportModule("__builtin__")) return -1;
4295 if (PyDict_SetItemString(module_dict
, "__builtins__", t
) < 0)
4298 UNLESS (t
=PyDict_New()) return -1;
4299 UNLESS (r
=PyRun_String(
4300 "def __init__(self, *args): self.args=args\n\n"
4301 "def __str__(self):\n"
4302 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4304 module_dict
, t
) ) return -1;
4307 UNLESS (PickleError
= PyErr_NewException("cPickle.PickleError", NULL
, t
))
4313 UNLESS (PicklingError
= PyErr_NewException("cPickle.PicklingError",
4317 UNLESS (t
=PyDict_New()) return -1;
4318 UNLESS (r
=PyRun_String(
4319 "def __init__(self, *args): self.args=args\n\n"
4320 "def __str__(self):\n"
4322 " a=a and type(a[0]) or '(what)'\n"
4323 " return 'Cannot pickle %s objects' % a\n"
4325 module_dict
, t
) ) return -1;
4328 UNLESS (UnpickleableError
= PyErr_NewException(
4329 "cPickle.UnpickleableError", PicklingError
, t
))
4334 UNLESS (UnpicklingError
= PyErr_NewException("cPickle.UnpicklingError",
4338 if (PyDict_SetItemString(module_dict
, "PickleError",
4342 if (PyDict_SetItemString(module_dict
, "PicklingError",
4346 if (PyDict_SetItemString(module_dict
, "UnpicklingError",
4347 UnpicklingError
) < 0)
4350 if (PyDict_SetItemString(module_dict
, "UnpickleableError",
4351 UnpickleableError
) < 0)
4354 UNLESS (BadPickleGet
= PyString_FromString("cPickle.BadPickleGet"))
4357 if (PyDict_SetItemString(module_dict
, "BadPickleGet",
4366 #ifndef DL_EXPORT /* declarations for DLL import/export */
4367 #define DL_EXPORT(RTYPE) RTYPE
4371 PyObject
*m
, *d
, *v
;
4373 PyObject
*format_version
;
4374 PyObject
*compatible_formats
;
4376 Picklertype
.ob_type
= &PyType_Type
;
4377 Unpicklertype
.ob_type
= &PyType_Type
;
4378 PdataType
.ob_type
= &PyType_Type
;
4380 /* Create the module and add the functions */
4381 m
= Py_InitModule4("cPickle", cPickle_methods
,
4382 cPickle_module_documentation
,
4383 (PyObject
*)NULL
,PYTHON_API_VERSION
);
4385 /* Add some symbolic constants to the module */
4386 d
= PyModule_GetDict(m
);
4387 PyDict_SetItemString(d
,"__version__", v
= PyString_FromString(rev
));
4390 format_version
= PyString_FromString("1.3");
4391 compatible_formats
= Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
4393 PyDict_SetItemString(d
, "format_version", format_version
);
4394 PyDict_SetItemString(d
, "compatible_formats", compatible_formats
);
4395 Py_XDECREF(format_version
);
4396 Py_XDECREF(compatible_formats
);