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"
60 #define Py_eval_input eval_input
61 #endif /* Py_eval_input */
65 #define UNLESS(E) if (!(E))
67 #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
69 #define WRITE_BUF_SIZE 256
71 /* --------------------------------------------------------------------------
72 NOTES on format codes.
73 XXX much more is needed here
76 BININT1 8-bit unsigned integer; followed by 1 byte.
77 BININT2 16-bit unsigned integer; followed by 2 bytes, little-endian.
78 BININT 32-bit signed integer; followed by 4 bytes, little-endian.
79 INT Integer; natural decimal string conversion, then newline.
80 CAUTION: INT-reading code can't assume that what follows
81 fits in a Python int, because the size of Python ints varies
83 LONG Long (unbounded) integer; repr(i), then newline.
84 -------------------------------------------------------------------------- */
100 #define BINPERSID 'Q'
103 #define BINSTRING 'T'
104 #define SHORT_BINSTRING 'U'
106 #define BINUNICODE 'X'
111 #define EMPTY_DICT '}'
116 #define LONG_BINGET 'j'
118 #define EMPTY_LIST ']'
122 #define LONG_BINPUT 'r'
125 #define EMPTY_TUPLE ')'
128 static char MARKv
= MARK
;
130 static PyObject
*PickleError
;
131 static PyObject
*PicklingError
;
132 static PyObject
*UnpickleableError
;
133 static PyObject
*UnpicklingError
;
134 static PyObject
*BadPickleGet
;
137 static PyObject
*dispatch_table
;
138 static PyObject
*safe_constructors
;
139 static PyObject
*empty_tuple
;
141 static PyObject
*__class___str
, *__getinitargs___str
, *__dict___str
,
142 *__getstate___str
, *__setstate___str
, *__name___str
, *__reduce___str
,
143 *write_str
, *__safe_for_unpickling___str
, *append_str
,
144 *read_str
, *readline_str
, *__main___str
, *__basicnew___str
,
145 *copy_reg_str
, *dispatch_table_str
, *safe_constructors_str
;
147 #ifndef PyList_SET_ITEM
148 #define PyList_SET_ITEM(op, i, v) (((PyListObject *)(op))->ob_item[i] = (v))
150 #ifndef PyList_GET_SIZE
151 #define PyList_GET_SIZE(op) (((PyListObject *)(op))->ob_size)
153 #ifndef PyTuple_SET_ITEM
154 #define PyTuple_SET_ITEM(op, i, v) (((PyTupleObject *)(op))->ob_item[i] = (v))
156 #ifndef PyTuple_GET_SIZE
157 #define PyTuple_GET_SIZE(op) (((PyTupleObject *)(op))->ob_size)
159 #ifndef PyString_GET_SIZE
160 #define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
163 /*************************************************************************
164 Internal Data type for pickle data. */
173 Pdata_dealloc(Pdata
*self
) {
177 for (i
=self
->length
, p
=self
->data
; --i
>= 0; p
++) Py_DECREF(*p
);
179 if (self
->data
) free(self
->data
);
184 static PyTypeObject PdataType
= {
185 PyObject_HEAD_INIT(NULL
) 0, "Pdata", sizeof(Pdata
), 0,
186 (destructor
)Pdata_dealloc
,
187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
190 #define Pdata_Check(O) ((O)->ob_type == &PdataType)
196 UNLESS (self
= PyObject_New(Pdata
, &PdataType
)) return NULL
;
199 self
->data
=malloc(self
->size
* sizeof(PyObject
*));
200 if (self
->data
) return (PyObject
*)self
;
202 return PyErr_NoMemory();
206 stackUnderflow(void) {
207 PyErr_SetString(UnpicklingError
, "unpickling stack underflow");
212 Pdata_clear(Pdata
*self
, int clearto
) {
216 if (clearto
< 0) return stackUnderflow();
217 if (clearto
>= self
->length
) return 0;
219 for (i
=self
->length
, p
=self
->data
+clearto
; --i
>= clearto
; p
++)
221 self
->length
=clearto
;
228 Pdata_grow(Pdata
*self
) {
234 self
->data
= realloc(self
->data
, self
->size
*sizeof(PyObject
*));
243 #define PDATA_POP(D,V) { \
244 if ((D)->length) V=D->data[--((D)->length)]; \
246 PyErr_SetString(UnpicklingError, "bad pickle data"); \
253 Pdata_popTuple(Pdata
*self
, int start
) {
257 l
=self
->length
-start
;
258 UNLESS (r
=PyTuple_New(l
)) return NULL
;
259 for (i
=start
, j
=0 ; j
< l
; i
++, j
++)
260 PyTuple_SET_ITEM(r
, j
, self
->data
[i
]);
267 Pdata_popList(Pdata
*self
, int start
) {
271 l
=self
->length
-start
;
272 UNLESS (r
=PyList_New(l
)) return NULL
;
273 for (i
=start
, j
=0 ; j
< l
; i
++, j
++)
274 PyList_SET_ITEM(r
, j
, self
->data
[i
]);
280 #define PDATA_APPEND_(D,O,ER) { \
281 if (Pdata_Append(((Pdata*)(D)), O) < 0) return ER; \
284 #define PDATA_APPEND(D,O,ER) { \
285 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
286 Pdata_grow((Pdata*)(D)) < 0) \
289 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
292 #define PDATA_PUSH(D,O,ER) { \
293 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
294 Pdata_grow((Pdata*)(D)) < 0) { \
298 ((Pdata*)(D))->data[((Pdata*)(D))->length++]=O; \
301 /*************************************************************************/
303 #define ARG_TUP(self, o) { \
304 if (self->arg || (self->arg=PyTuple_New(1))) { \
305 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
306 PyTuple_SET_ITEM(self->arg,0,o); \
313 #define FREE_ARG_TUP(self) { \
314 if (self->arg->ob_refcnt > 1) { \
315 Py_DECREF(self->arg); \
320 typedef struct Picklerobject
{
328 PyObject
*inst_pers_func
;
330 int fast
; /* Fast mode doesn't save in memo, don't use if circ ref */
331 int (*write_func
)(struct Picklerobject
*, char *, int);
334 PyObject
*dispatch_table
;
337 staticforward PyTypeObject Picklertype
;
339 typedef struct Unpicklerobject
{
350 PyObject
*last_string
;
354 int (*read_func
)(struct Unpicklerobject
*, char **, int);
355 int (*readline_func
)(struct Unpicklerobject
*, char **);
358 PyObject
*safe_constructors
;
359 PyObject
*find_class
;
362 staticforward PyTypeObject Unpicklertype
;
364 /* Forward decls that need the above structs */
365 static int save(Picklerobject
*, PyObject
*, int);
366 static int put2(Picklerobject
*, PyObject
*);
369 cPickle_PyMapping_HasKey(PyObject
*o
, PyObject
*key
) {
372 if ((v
= PyObject_GetItem(o
,key
))) {
383 cPickle_ErrFormat(PyObject
*ErrType
, char *stringformat
, char *format
, ...)
386 PyObject
*args
=0, *retval
=0;
387 va_start(va
, format
);
389 if (format
) args
= Py_VaBuildValue(format
, va
);
391 if (format
&& ! args
) return NULL
;
392 if (stringformat
&& !(retval
=PyString_FromString(stringformat
))) return NULL
;
397 v
=PyString_Format(retval
, args
);
400 if (! v
) return NULL
;
405 if (args
) retval
=args
;
407 PyErr_SetObject(ErrType
,Py_None
);
410 PyErr_SetObject(ErrType
,retval
);
416 write_file(Picklerobject
*self
, char *s
, int n
) {
417 size_t nbyteswritten
;
423 Py_BEGIN_ALLOW_THREADS
424 nbyteswritten
= fwrite(s
, sizeof(char), n
, self
->fp
);
426 if (nbyteswritten
!= (size_t)n
) {
427 PyErr_SetFromErrno(PyExc_IOError
);
435 write_cStringIO(Picklerobject
*self
, char *s
, int n
) {
440 if (PycStringIO
->cwrite((PyObject
*)self
->file
, s
, n
) != n
) {
448 write_none(Picklerobject
*self
, char *s
, int n
) {
449 if (s
== NULL
) return 0;
454 write_other(Picklerobject
*self
, char *s
, int n
) {
455 PyObject
*py_str
= 0, *junk
= 0;
458 UNLESS (self
->buf_size
) return 0;
460 PyString_FromStringAndSize(self
->write_buf
, self
->buf_size
))
464 if (self
->buf_size
&& (n
+ self
->buf_size
) > WRITE_BUF_SIZE
) {
465 if (write_other(self
, NULL
, 0) < 0)
469 if (n
> WRITE_BUF_SIZE
) {
471 PyString_FromStringAndSize(s
, n
))
475 memcpy(self
->write_buf
+ self
->buf_size
, s
, n
);
482 /* object with write method */
483 ARG_TUP(self
, py_str
);
485 junk
= PyObject_CallObject(self
->write
, self
->arg
);
488 if (junk
) Py_DECREF(junk
);
492 PDATA_PUSH(self
->file
, py_str
, -1);
500 read_file(Unpicklerobject
*self
, char **s
, int n
) {
503 if (self
->buf_size
== 0) {
506 size
= ((n
< 32) ? 32 : n
);
507 UNLESS (self
->buf
= (char *)malloc(size
* sizeof(char))) {
512 self
->buf_size
= size
;
514 else if (n
> self
->buf_size
) {
515 UNLESS (self
->buf
= (char *)realloc(self
->buf
, n
* sizeof(char))) {
523 Py_BEGIN_ALLOW_THREADS
524 nbytesread
= fread(self
->buf
, sizeof(char), n
, self
->fp
);
526 if (nbytesread
!= (size_t)n
) {
527 if (feof(self
->fp
)) {
528 PyErr_SetNone(PyExc_EOFError
);
532 PyErr_SetFromErrno(PyExc_IOError
);
543 readline_file(Unpicklerobject
*self
, char **s
) {
546 if (self
->buf_size
== 0) {
547 UNLESS (self
->buf
= (char *)malloc(40 * sizeof(char))) {
557 for (; i
< (self
->buf_size
- 1); i
++) {
558 if (feof(self
->fp
) || (self
->buf
[i
] = getc(self
->fp
)) == '\n') {
559 self
->buf
[i
+ 1] = '\0';
565 UNLESS (self
->buf
= (char *)realloc(self
->buf
,
566 (self
->buf_size
* 2) * sizeof(char))) {
578 read_cStringIO(Unpicklerobject
*self
, char **s
, int n
) {
581 if (PycStringIO
->cread((PyObject
*)self
->file
, &ptr
, n
) != n
) {
582 PyErr_SetNone(PyExc_EOFError
);
593 readline_cStringIO(Unpicklerobject
*self
, char **s
) {
597 if ((n
= PycStringIO
->creadline((PyObject
*)self
->file
, &ptr
)) < 0) {
608 read_other(Unpicklerobject
*self
, char **s
, int n
) {
609 PyObject
*bytes
, *str
=0;
611 UNLESS (bytes
= PyInt_FromLong(n
)) return -1;
613 ARG_TUP(self
, bytes
);
615 str
= PyObject_CallObject(self
->read
, self
->arg
);
618 if (! str
) return -1;
620 Py_XDECREF(self
->last_string
);
621 self
->last_string
= str
;
623 if (! (*s
= PyString_AsString(str
))) return -1;
629 readline_other(Unpicklerobject
*self
, char **s
) {
633 UNLESS (str
= PyObject_CallObject(self
->readline
, empty_tuple
)) {
637 if ((str_size
= PyString_Size(str
)) < 0)
640 Py_XDECREF(self
->last_string
);
641 self
->last_string
= str
;
643 if (! (*s
= PyString_AsString(str
)))
651 pystrndup(char *s
, int l
) {
653 UNLESS (r
=malloc((l
+1)*sizeof(char))) return (char*)PyErr_NoMemory();
661 get(Picklerobject
*self
, PyObject
*id
) {
662 PyObject
*value
, *mv
;
667 UNLESS (mv
= PyDict_GetItem(self
->memo
, id
)) {
668 PyErr_SetObject(PyExc_KeyError
, id
);
672 UNLESS (value
= PyTuple_GetItem(mv
, 0))
675 UNLESS (PyInt_Check(value
)) {
676 PyErr_SetString(PicklingError
, "no int where int expected in memo");
679 c_value
= PyInt_AS_LONG((PyIntObject
*)value
);
683 sprintf(s
+ 1, "%ld\n", c_value
);
686 else if (Pdata_Check(self
->file
)) {
687 if (write_other(self
, NULL
, 0) < 0) return -1;
688 PDATA_APPEND(self
->file
, mv
, -1);
694 s
[1] = (int)(c_value
& 0xff);
699 s
[1] = (int)(c_value
& 0xff);
700 s
[2] = (int)((c_value
>> 8) & 0xff);
701 s
[3] = (int)((c_value
>> 16) & 0xff);
702 s
[4] = (int)((c_value
>> 24) & 0xff);
707 if ((*self
->write_func
)(self
, s
, len
) < 0)
715 put(Picklerobject
*self
, PyObject
*ob
) {
716 if (ob
->ob_refcnt
< 2 || self
->fast
)
719 return put2(self
, ob
);
724 put2(Picklerobject
*self
, PyObject
*ob
) {
729 PyObject
*py_ob_id
= 0, *memo_len
= 0, *t
= 0;
731 if (self
->fast
) return 0;
733 if ((p
= PyDict_Size(self
->memo
)) < 0)
736 p
++; /* Make sure memo keys are positive! */
738 UNLESS (py_ob_id
= PyLong_FromVoidPtr(ob
))
741 UNLESS (memo_len
= PyInt_FromLong(p
))
744 UNLESS (t
= PyTuple_New(2))
747 PyTuple_SET_ITEM(t
, 0, memo_len
);
749 PyTuple_SET_ITEM(t
, 1, ob
);
752 if (PyDict_SetItem(self
->memo
, py_ob_id
, t
) < 0)
757 sprintf(c_str
+ 1, "%d\n", p
);
760 else if (Pdata_Check(self
->file
)) {
761 if (write_other(self
, NULL
, 0) < 0) return -1;
762 PDATA_APPEND(self
->file
, memo_len
, -1);
763 res
=0; /* Job well done ;) */
768 c_str
[0] = LONG_BINPUT
;
769 c_str
[1] = (int)(p
& 0xff);
770 c_str
[2] = (int)((p
>> 8) & 0xff);
771 c_str
[3] = (int)((p
>> 16) & 0xff);
772 c_str
[4] = (int)((p
>> 24) & 0xff);
782 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
788 Py_XDECREF(py_ob_id
);
789 Py_XDECREF(memo_len
);
795 #define PyImport_Import cPickle_Import
798 PyImport_Import(PyObject
*module_name
) {
799 static PyObject
*silly_list
=0, *__builtins___str
=0, *__import___str
;
800 static PyObject
*standard_builtins
=0;
801 PyObject
*globals
=0, *__import__
=0, *__builtins__
=0, *r
=0;
803 UNLESS (silly_list
) {
804 UNLESS (__import___str
=PyString_FromString("__import__"))
806 UNLESS (__builtins___str
=PyString_FromString("__builtins__"))
808 UNLESS (silly_list
=Py_BuildValue("[s]","__doc__"))
812 if ((globals
=PyEval_GetGlobals())) {
814 UNLESS (__builtins__
=PyObject_GetItem(globals
,__builtins___str
))
820 UNLESS (standard_builtins
||
821 (standard_builtins
=PyImport_ImportModule("__builtin__")))
824 __builtins__
=standard_builtins
;
825 Py_INCREF(__builtins__
);
826 UNLESS (globals
= Py_BuildValue("{sO}", "__builtins__", __builtins__
))
830 if (PyDict_Check(__builtins__
)) {
831 UNLESS (__import__
=PyObject_GetItem(__builtins__
,__import___str
)) goto err
;
834 UNLESS (__import__
=PyObject_GetAttr(__builtins__
,__import___str
)) goto err
;
837 UNLESS (r
=PyObject_CallFunction(__import__
,"OOOO",
838 module_name
, globals
, globals
, silly_list
))
842 Py_DECREF(__builtins__
);
843 Py_DECREF(__import__
);
848 Py_XDECREF(__builtins__
);
849 Py_XDECREF(__import__
);
854 whichmodule(PyObject
*global
, PyObject
*global_name
) {
856 PyObject
*module
= 0, *modules_dict
= 0,
857 *global_name_attr
= 0, *name
= 0;
859 module
= PyObject_GetAttrString(global
, "__module__");
860 if (module
) return module
;
863 UNLESS (modules_dict
= PySys_GetObject("modules"))
867 while ((j
= PyDict_Next(modules_dict
, &i
, &name
, &module
))) {
869 if (PyObject_Compare(name
, __main___str
)==0) continue;
871 UNLESS (global_name_attr
= PyObject_GetAttr(module
, global_name
)) {
876 if (global_name_attr
!= global
) {
877 Py_DECREF(global_name_attr
);
881 Py_DECREF(global_name_attr
);
886 /* The following implements the rule in pickle.py added in 1.5
887 that used __main__ if no module is found. I don't actually
901 save_none(Picklerobject
*self
, PyObject
*args
) {
902 static char none
= NONE
;
903 if ((*self
->write_func
)(self
, &none
, 1) < 0)
911 save_int(Picklerobject
*self
, PyObject
*args
) {
913 long l
= PyInt_AS_LONG((PyIntObject
*)args
);
922 /* Text-mode pickle, or long too big to fit in the 4-byte
923 * signed BININT format: store as a string.
926 sprintf(c_str
+ 1, "%ld\n", l
);
927 if ((*self
->write_func
)(self
, c_str
, strlen(c_str
)) < 0)
931 /* Binary pickle and l fits in a signed 4-byte int. */
932 c_str
[1] = (int)( l
& 0xff);
933 c_str
[2] = (int)((l
>> 8) & 0xff);
934 c_str
[3] = (int)((l
>> 16) & 0xff);
935 c_str
[4] = (int)((l
>> 24) & 0xff);
937 if ((c_str
[4] == 0) && (c_str
[3] == 0)) {
952 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
961 save_long(Picklerobject
*self
, PyObject
*args
) {
965 static char l
= LONG
;
967 UNLESS (repr
= PyObject_Repr(args
))
970 if ((size
= PyString_Size(repr
)) < 0)
973 if ((*self
->write_func
)(self
, &l
, 1) < 0)
976 if ((*self
->write_func
)(self
,
977 PyString_AS_STRING((PyStringObject
*)repr
), size
) < 0)
980 if ((*self
->write_func
)(self
, "\n", 1) < 0)
993 save_float(Picklerobject
*self
, PyObject
*args
) {
994 double x
= PyFloat_AS_DOUBLE((PyFloatObject
*)args
);
1001 unsigned char *p
= (unsigned char *)str
;
1015 /* Normalize f to be in the range [1.0, 2.0) */
1016 if (0.5 <= f
&& f
< 1.0) {
1020 else if (f
== 0.0) {
1024 PyErr_SetString(PyExc_SystemError
,
1025 "frexp() result out of range");
1030 /* XXX 1024 itself is reserved for Inf/NaN */
1031 PyErr_SetString(PyExc_OverflowError
,
1032 "float too large to pack with d format");
1035 else if (e
< -1022) {
1036 /* Gradual underflow */
1037 f
= ldexp(f
, 1022 + e
);
1040 else if (!(e
== 0 && f
== 0.0)) {
1042 f
-= 1.0; /* Get rid of leading 1 */
1045 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
1046 f
*= 268435456.0; /* 2**28 */
1047 fhi
= (long) floor(f
); /* Truncate */
1049 f
*= 16777216.0; /* 2**24 */
1050 flo
= (long) floor(f
+ 0.5); /* Round */
1053 *p
= (s
<<7) | (e
>>4);
1057 *p
= (unsigned char) (((e
&0xF)<<4) | (fhi
>>24));
1061 *p
= (unsigned char) ((fhi
>>16) & 0xFF);
1065 *p
= (unsigned char) ((fhi
>>8) & 0xFF);
1069 *p
= (unsigned char) (fhi
& 0xFF);
1073 *p
= (unsigned char) ((flo
>>16) & 0xFF);
1077 *p
= (unsigned char) ((flo
>>8) & 0xFF);
1081 *p
= (unsigned char) (flo
& 0xFF);
1083 if ((*self
->write_func
)(self
, str
, 9) < 0)
1089 sprintf(c_str
+ 1, "%.17g\n", x
);
1091 if ((*self
->write_func
)(self
, c_str
, strlen(c_str
)) < 0)
1100 save_string(Picklerobject
*self
, PyObject
*args
, int doput
) {
1104 if ((size
= PyString_Size(args
)) < 0)
1110 static char string
= STRING
;
1112 UNLESS (repr
= PyObject_Repr(args
))
1115 if ((len
= PyString_Size(repr
)) < 0)
1117 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1119 if ((*self
->write_func
)(self
, &string
, 1) < 0)
1122 if ((*self
->write_func
)(self
, repr_str
, len
) < 0)
1125 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1134 if ((size
= PyString_Size(args
)) < 0)
1138 c_str
[0] = SHORT_BINSTRING
;
1143 c_str
[0] = BINSTRING
;
1144 for (i
= 1; i
< 5; i
++)
1145 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1149 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
1152 if (size
> 128 && Pdata_Check(self
->file
)) {
1153 if (write_other(self
, NULL
, 0) < 0) return -1;
1154 PDATA_APPEND(self
->file
, args
, -1);
1157 if ((*self
->write_func
)(self
,
1158 PyString_AS_STRING((PyStringObject
*)args
), size
) < 0)
1164 if (put(self
, args
) < 0)
1175 #ifdef Py_USING_UNICODE
1176 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1177 backslash and newline characters to \uXXXX escapes. */
1179 modified_EncodeRawUnicodeEscape(const Py_UNICODE
*s
, int size
)
1185 static const char *hexdigit
= "0123456789ABCDEF";
1187 repr
= PyString_FromStringAndSize(NULL
, 6 * size
);
1193 p
= q
= PyString_AS_STRING(repr
);
1194 while (size
-- > 0) {
1195 Py_UNICODE ch
= *s
++;
1196 /* Map 16-bit characters to '\uxxxx' */
1197 if (ch
>= 256 || ch
== '\\' || ch
== '\n') {
1200 *p
++ = hexdigit
[(ch
>> 12) & 0xf];
1201 *p
++ = hexdigit
[(ch
>> 8) & 0xf];
1202 *p
++ = hexdigit
[(ch
>> 4) & 0xf];
1203 *p
++ = hexdigit
[ch
& 15];
1205 /* Copy everything else as-is */
1210 if (_PyString_Resize(&repr
, p
- q
))
1222 save_unicode(Picklerobject
*self
, PyObject
*args
, int doput
) {
1226 if (!PyUnicode_Check(args
))
1231 static char string
= UNICODE
;
1233 UNLESS(repr
= modified_EncodeRawUnicodeEscape(
1234 PyUnicode_AS_UNICODE(args
), PyUnicode_GET_SIZE(args
)))
1237 if ((len
= PyString_Size(repr
)) < 0)
1239 repr_str
= PyString_AS_STRING((PyStringObject
*)repr
);
1241 if ((*self
->write_func
)(self
, &string
, 1) < 0)
1244 if ((*self
->write_func
)(self
, repr_str
, len
) < 0)
1247 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1256 UNLESS (repr
= PyUnicode_AsUTF8String(args
))
1259 if ((size
= PyString_Size(repr
)) < 0)
1262 c_str
[0] = BINUNICODE
;
1263 for (i
= 1; i
< 5; i
++)
1264 c_str
[i
] = (int)(size
>> ((i
- 1) * 8));
1267 if ((*self
->write_func
)(self
, c_str
, len
) < 0)
1270 if (size
> 128 && Pdata_Check(self
->file
)) {
1271 if (write_other(self
, NULL
, 0) < 0)
1273 PDATA_APPEND(self
->file
, repr
, -1);
1276 if ((*self
->write_func
)(self
, PyString_AS_STRING(repr
), size
) < 0)
1284 if (put(self
, args
) < 0)
1297 save_tuple(Picklerobject
*self
, PyObject
*args
) {
1298 PyObject
*element
= 0, *py_tuple_id
= 0;
1299 int len
, i
, has_key
, res
= -1;
1301 static char tuple
= TUPLE
;
1303 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1306 if ((len
= PyTuple_Size(args
)) < 0)
1309 for (i
= 0; i
< len
; i
++) {
1310 UNLESS (element
= PyTuple_GET_ITEM((PyTupleObject
*)args
, i
))
1313 if (save(self
, element
, 0) < 0)
1317 UNLESS (py_tuple_id
= PyLong_FromVoidPtr(args
))
1321 if ((has_key
= cPickle_PyMapping_HasKey(self
->memo
, py_tuple_id
)) < 0)
1326 static char pop_mark
= POP_MARK
;
1328 if ((*self
->write_func
)(self
, &pop_mark
, 1) < 0)
1332 static char pop
= POP
;
1334 for (i
= 0; i
<= len
; i
++) {
1335 if ((*self
->write_func
)(self
, &pop
, 1) < 0)
1340 if (get(self
, py_tuple_id
) < 0)
1348 if ((*self
->write_func
)(self
, &tuple
, 1) < 0) {
1352 if (put(self
, args
) < 0)
1358 Py_XDECREF(py_tuple_id
);
1364 save_empty_tuple(Picklerobject
*self
, PyObject
*args
) {
1365 static char tuple
= EMPTY_TUPLE
;
1367 return (*self
->write_func
)(self
, &tuple
, 1);
1372 save_list(Picklerobject
*self
, PyObject
*args
) {
1373 PyObject
*element
= 0;
1374 int s_len
, len
, i
, using_appends
, res
= -1;
1377 static char append
= APPEND
, appends
= APPENDS
;
1389 if ((len
= PyList_Size(args
)) < 0)
1392 if ((*self
->write_func
)(self
, s
, s_len
) < 0)
1396 if (put(self
, args
) < 0)
1400 if (put2(self
, args
) < 0)
1404 if ((using_appends
= (self
->bin
&& (len
> 1))))
1405 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1408 for (i
= 0; i
< len
; i
++) {
1409 UNLESS (element
= PyList_GET_ITEM((PyListObject
*)args
, i
))
1412 if (save(self
, element
, 0) < 0)
1415 if (!using_appends
) {
1416 if ((*self
->write_func
)(self
, &append
, 1) < 0)
1421 if (using_appends
) {
1422 if ((*self
->write_func
)(self
, &appends
, 1) < 0)
1435 save_dict(Picklerobject
*self
, PyObject
*args
) {
1436 PyObject
*key
= 0, *value
= 0;
1437 int i
, len
, res
= -1, using_setitems
;
1440 static char setitem
= SETITEM
, setitems
= SETITEMS
;
1452 if ((*self
->write_func
)(self
, s
, len
) < 0)
1455 if ((len
= PyDict_Size(args
)) < 0)
1459 if (put(self
, args
) < 0)
1463 if (put2(self
, args
) < 0)
1467 if ((using_setitems
= (self
->bin
&& (PyDict_Size(args
) > 1))))
1468 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1472 while (PyDict_Next(args
, &i
, &key
, &value
)) {
1473 if (save(self
, key
, 0) < 0)
1476 if (save(self
, value
, 0) < 0)
1479 if (!using_setitems
) {
1480 if ((*self
->write_func
)(self
, &setitem
, 1) < 0)
1485 if (using_setitems
) {
1486 if ((*self
->write_func
)(self
, &setitems
, 1) < 0)
1499 save_inst(Picklerobject
*self
, PyObject
*args
) {
1500 PyObject
*class = 0, *module
= 0, *name
= 0, *state
= 0,
1501 *getinitargs_func
= 0, *getstate_func
= 0, *class_args
= 0;
1502 char *module_str
, *name_str
;
1503 int module_size
, name_size
, res
= -1;
1505 static char inst
= INST
, obj
= OBJ
, build
= BUILD
;
1507 if ((*self
->write_func
)(self
, &MARKv
, 1) < 0)
1510 UNLESS (class = PyObject_GetAttr(args
, __class___str
))
1514 if (save(self
, class, 0) < 0)
1518 if ((getinitargs_func
= PyObject_GetAttr(args
, __getinitargs___str
))) {
1519 PyObject
*element
= 0;
1522 UNLESS (class_args
=
1523 PyObject_CallObject(getinitargs_func
, empty_tuple
))
1526 if ((len
= PyObject_Size(class_args
)) < 0)
1529 for (i
= 0; i
< len
; i
++) {
1530 UNLESS (element
= PySequence_GetItem(class_args
, i
))
1533 if (save(self
, element
, 0) < 0) {
1546 UNLESS (name
= ((PyClassObject
*)class)->cl_name
) {
1547 PyErr_SetString(PicklingError
, "class has no name");
1551 UNLESS (module
= whichmodule(class, name
))
1555 if ((module_size
= PyString_Size(module
)) < 0 ||
1556 (name_size
= PyString_Size(name
)) < 0)
1559 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
1560 name_str
= PyString_AS_STRING((PyStringObject
*)name
);
1562 if ((*self
->write_func
)(self
, &inst
, 1) < 0)
1565 if ((*self
->write_func
)(self
, module_str
, module_size
) < 0)
1568 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1571 if ((*self
->write_func
)(self
, name_str
, name_size
) < 0)
1574 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1577 else if ((*self
->write_func
)(self
, &obj
, 1) < 0) {
1581 if ((getstate_func
= PyObject_GetAttr(args
, __getstate___str
))) {
1582 UNLESS (state
= PyObject_CallObject(getstate_func
, empty_tuple
))
1588 UNLESS (state
= PyObject_GetAttr(args
, __dict___str
)) {
1595 if (!PyDict_Check(state
)) {
1596 if (put2(self
, args
) < 0)
1600 if (put(self
, args
) < 0)
1604 if (save(self
, state
, 0) < 0)
1607 if ((*self
->write_func
)(self
, &build
, 1) < 0)
1616 Py_XDECREF(getinitargs_func
);
1617 Py_XDECREF(getstate_func
);
1618 Py_XDECREF(class_args
);
1625 save_global(Picklerobject
*self
, PyObject
*args
, PyObject
*name
) {
1626 PyObject
*global_name
= 0, *module
= 0, *mod
= 0, *moddict
= 0, *klass
= 0;
1627 char *name_str
, *module_str
;
1628 int module_size
, name_size
, res
= -1;
1630 static char global
= GLOBAL
;
1634 Py_INCREF(global_name
);
1637 UNLESS (global_name
= PyObject_GetAttr(args
, __name___str
))
1641 UNLESS (module
= whichmodule(args
, global_name
))
1644 if ((module_size
= PyString_Size(module
)) < 0 ||
1645 (name_size
= PyString_Size(global_name
)) < 0)
1648 module_str
= PyString_AS_STRING((PyStringObject
*)module
);
1649 name_str
= PyString_AS_STRING((PyStringObject
*)global_name
);
1651 mod
= PyImport_ImportModule(module_str
);
1653 /* Py_ErrClear(); ?? */
1654 cPickle_ErrFormat(PicklingError
,
1655 "Can't pickle %s: it's not found as %s.%s",
1656 "OSS", args
, module
, global_name
);
1659 moddict
= PyModule_GetDict(mod
); /* borrowed ref */
1660 klass
= PyDict_GetItemString(moddict
, name_str
); /* borrowed ref */
1661 if (klass
== NULL
) {
1662 cPickle_ErrFormat(PicklingError
,
1663 "Can't pickle %s: it's not found as %s.%s",
1664 "OSS", args
, module
, global_name
);
1667 if (klass
!= args
) {
1668 cPickle_ErrFormat(PicklingError
,
1669 "Can't pickle %s: it's not the same object as %s.%s",
1670 "OSS", args
, module
, global_name
);
1674 if ((*self
->write_func
)(self
, &global
, 1) < 0)
1677 if ((*self
->write_func
)(self
, module_str
, module_size
) < 0)
1680 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1683 if ((*self
->write_func
)(self
, name_str
, name_size
) < 0)
1686 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1689 if (put(self
, args
) < 0)
1696 Py_XDECREF(global_name
);
1703 save_pers(Picklerobject
*self
, PyObject
*args
, PyObject
*f
) {
1707 static char persid
= PERSID
, binpersid
= BINPERSID
;
1710 ARG_TUP(self
, args
);
1712 pid
= PyObject_CallObject(f
, self
->arg
);
1715 if (! pid
) return -1;
1717 if (pid
!= Py_None
) {
1719 if (!PyString_Check(pid
)) {
1720 PyErr_SetString(PicklingError
,
1721 "persistent id must be string");
1725 if ((*self
->write_func
)(self
, &persid
, 1) < 0)
1728 if ((size
= PyString_Size(pid
)) < 0)
1731 if ((*self
->write_func
)(self
,
1732 PyString_AS_STRING((PyStringObject
*)pid
), size
) < 0)
1735 if ((*self
->write_func
)(self
, "\n", 1) < 0)
1741 else if (save(self
, pid
, 1) >= 0) {
1742 if ((*self
->write_func
)(self
, &binpersid
, 1) < 0)
1761 save_reduce(Picklerobject
*self
, PyObject
*callable
,
1762 PyObject
*tup
, PyObject
*state
, PyObject
*ob
) {
1763 static char reduce
= REDUCE
, build
= BUILD
;
1765 if (save(self
, callable
, 0) < 0)
1768 if (save(self
, tup
, 0) < 0)
1771 if ((*self
->write_func
)(self
, &reduce
, 1) < 0)
1775 if (state
&& !PyDict_Check(state
)) {
1776 if (put2(self
, ob
) < 0)
1780 if (put(self
, ob
) < 0)
1786 if (save(self
, state
, 0) < 0)
1789 if ((*self
->write_func
)(self
, &build
, 1) < 0)
1797 save(Picklerobject
*self
, PyObject
*args
, int pers_save
) {
1799 PyObject
*py_ob_id
= 0, *__reduce__
= 0, *t
= 0, *arg_tup
= 0,
1800 *callable
= 0, *state
= 0;
1801 int res
= -1, tmp
, size
;
1803 if (!pers_save
&& self
->pers_func
) {
1804 if ((tmp
= save_pers(self
, args
, self
->pers_func
)) != 0) {
1810 if (args
== Py_None
) {
1811 res
= save_none(self
, args
);
1815 type
= args
->ob_type
;
1817 switch (type
->tp_name
[0]) {
1819 if (type
== &PyInt_Type
) {
1820 res
= save_int(self
, args
);
1826 if (type
== &PyLong_Type
) {
1827 res
= save_long(self
, args
);
1833 if (type
== &PyFloat_Type
) {
1834 res
= save_float(self
, args
);
1840 if (type
== &PyTuple_Type
&& PyTuple_Size(args
)==0) {
1841 if (self
->bin
) res
= save_empty_tuple(self
, args
);
1842 else res
= save_tuple(self
, args
);
1848 if ((type
== &PyString_Type
) && (PyString_GET_SIZE(args
) < 2)) {
1849 res
= save_string(self
, args
, 0);
1853 #ifdef Py_USING_UNICODE
1855 if ((type
== &PyUnicode_Type
) && (PyString_GET_SIZE(args
) < 2)) {
1856 res
= save_unicode(self
, args
, 0);
1862 if (args
->ob_refcnt
> 1) {
1865 UNLESS (py_ob_id
= PyLong_FromVoidPtr(args
))
1868 if ((has_key
= cPickle_PyMapping_HasKey(self
->memo
, py_ob_id
)) < 0)
1872 if (get(self
, py_ob_id
) < 0)
1880 switch (type
->tp_name
[0]) {
1882 if (type
== &PyString_Type
) {
1883 res
= save_string(self
, args
, 1);
1888 #ifdef Py_USING_UNICODE
1890 if (type
== &PyUnicode_Type
) {
1891 res
= save_unicode(self
, args
, 1);
1898 if (type
== &PyTuple_Type
) {
1899 res
= save_tuple(self
, args
);
1902 if (type
== &PyType_Type
) {
1903 res
= save_global(self
, args
, NULL
);
1909 if (type
== &PyList_Type
) {
1910 res
= save_list(self
, args
);
1916 if (type
== &PyDict_Type
) {
1917 res
= save_dict(self
, args
);
1923 if (type
== &PyInstance_Type
) {
1924 res
= save_inst(self
, args
);
1930 if (type
== &PyClass_Type
) {
1931 res
= save_global(self
, args
, NULL
);
1937 if (type
== &PyFunction_Type
) {
1938 res
= save_global(self
, args
, NULL
);
1944 if (type
== &PyCFunction_Type
) {
1945 res
= save_global(self
, args
, NULL
);
1950 if (!pers_save
&& self
->inst_pers_func
) {
1951 if ((tmp
= save_pers(self
, args
, self
->inst_pers_func
)) != 0) {
1957 if ((__reduce__
= PyDict_GetItem(dispatch_table
, (PyObject
*)type
))) {
1958 Py_INCREF(__reduce__
);
1961 ARG_TUP(self
, args
);
1963 t
= PyObject_CallObject(__reduce__
, self
->arg
);
1966 if (! t
) goto finally
;
1971 if ((__reduce__
= PyObject_GetAttr(args
, __reduce___str
))) {
1972 UNLESS (t
= PyObject_CallObject(__reduce__
, empty_tuple
))
1981 if (PyString_Check(t
)) {
1982 res
= save_global(self
, args
, t
);
1986 if (!PyTuple_Check(t
)) {
1987 cPickle_ErrFormat(PicklingError
, "Value returned by %s must "
1988 "be a tuple", "O", __reduce__
);
1992 size
= PyTuple_Size(t
);
1994 if ((size
!= 3) && (size
!= 2)) {
1995 cPickle_ErrFormat(PicklingError
, "tuple returned by %s must "
1996 "contain only two or three elements", "O", __reduce__
);
2000 callable
= PyTuple_GET_ITEM(t
, 0);
2002 arg_tup
= PyTuple_GET_ITEM(t
, 1);
2005 state
= PyTuple_GET_ITEM(t
, 2);
2008 UNLESS (PyTuple_Check(arg_tup
) || arg_tup
==Py_None
) {
2009 cPickle_ErrFormat(PicklingError
, "Second element of tuple "
2010 "returned by %s must be a tuple", "O", __reduce__
);
2014 res
= save_reduce(self
, callable
, arg_tup
, state
, args
);
2018 PyErr_SetObject(UnpickleableError
, args
);
2021 Py_XDECREF(py_ob_id
);
2022 Py_XDECREF(__reduce__
);
2030 dump(Picklerobject
*self
, PyObject
*args
) {
2031 static char stop
= STOP
;
2033 if (save(self
, args
, 0) < 0)
2036 if ((*self
->write_func
)(self
, &stop
, 1) < 0)
2039 if ((*self
->write_func
)(self
, NULL
, 0) < 0)
2046 Pickle_clear_memo(Picklerobject
*self
, PyObject
*args
) {
2047 if (args
&& ! PyArg_ParseTuple(args
,":clear_memo")) return NULL
;
2048 if (self
->memo
) PyDict_Clear(self
->memo
);
2054 Pickle_getvalue(Picklerobject
*self
, PyObject
*args
) {
2055 int l
, i
, rsize
, ssize
, clear
=1, lm
;
2058 char *s
, *p
, *have_get
;
2061 if (args
&& ! PyArg_ParseTuple(args
,"|i:getvalue",&clear
)) return NULL
;
2063 /* Check to make sure we are based on a list */
2064 if (! Pdata_Check(self
->file
)) {
2065 PyErr_SetString(PicklingError
,
2066 "Attempt to getvalue a non-list-based pickler");
2070 /* flush write buffer */
2071 if (write_other(self
, NULL
, 0) < 0) return NULL
;
2073 data
=(Pdata
*)self
->file
;
2076 /* set up an array to hold get/put status */
2077 if ((lm
=PyDict_Size(self
->memo
)) < 0) return NULL
;
2079 if (! (have_get
=malloc((lm
)*sizeof(char)))) return PyErr_NoMemory();
2080 memset(have_get
,0,lm
);
2082 /* Scan for gets. */
2083 for (rsize
=0, i
=l
; --i
>= 0; ) {
2086 if (PyString_Check(k
)) {
2087 rsize
+= PyString_GET_SIZE(k
);
2090 else if (PyInt_Check(k
)) { /* put */
2091 ik
=PyInt_AS_LONG((PyIntObject
*)k
);
2092 if (ik
>= lm
|| ik
==0) {
2093 PyErr_SetString(PicklingError
,
2094 "Invalid get data");
2097 if (have_get
[ik
]) { /* with matching get */
2098 if (ik
< 256) rsize
+= 2;
2103 else if (! (PyTuple_Check(k
) &&
2104 PyTuple_GET_SIZE(k
) == 2 &&
2105 PyInt_Check((k
=PyTuple_GET_ITEM(k
,0))))
2107 PyErr_SetString(PicklingError
,
2108 "Unexpected data in internal list");
2113 ik
=PyInt_AS_LONG((PyIntObject
*)k
);
2114 if (ik
>= lm
|| ik
==0) {
2115 PyErr_SetString(PicklingError
,
2116 "Invalid get data");
2120 if (ik
< 256) rsize
+= 2;
2126 /* Now generate the result */
2127 UNLESS (r
=PyString_FromStringAndSize(NULL
,rsize
)) goto err
;
2128 s
=PyString_AS_STRING((PyStringObject
*)r
);
2130 for (i
=0; i
<l
; i
++) {
2133 if (PyString_Check(k
)) {
2134 ssize
=PyString_GET_SIZE(k
);
2136 p
=PyString_AS_STRING((PyStringObject
*)k
);
2137 while (--ssize
>= 0) *s
++=*p
++;
2141 else if (PyTuple_Check(k
)) { /* get */
2142 ik
=PyInt_AS_LONG((PyIntObject
*)PyTuple_GET_ITEM(k
,0));
2145 *s
++ = (int)(ik
& 0xff);
2149 *s
++ = (int)(ik
& 0xff);
2150 *s
++ = (int)((ik
>> 8) & 0xff);
2151 *s
++ = (int)((ik
>> 16) & 0xff);
2152 *s
++ = (int)((ik
>> 24) & 0xff);
2157 ik
=PyInt_AS_LONG((PyIntObject
*)k
);
2159 if (have_get
[ik
]) { /* with matching get */
2162 *s
++ = (int)(ik
& 0xff);
2166 *s
++ = (int)(ik
& 0xff);
2167 *s
++ = (int)((ik
>> 8) & 0xff);
2168 *s
++ = (int)((ik
>> 16) & 0xff);
2169 *s
++ = (int)((ik
>> 24) & 0xff);
2177 PyDict_Clear(self
->memo
);
2178 Pdata_clear(data
,0);
2189 Pickler_dump(Picklerobject
*self
, PyObject
*args
) {
2193 UNLESS (PyArg_ParseTuple(args
, "O|i:dump", &ob
, &get
))
2196 if (dump(self
, ob
) < 0)
2199 if (get
) return Pickle_getvalue(self
, NULL
);
2202 return (PyObject
*)self
;
2206 static struct PyMethodDef Pickler_methods
[] = {
2207 {"dump", (PyCFunction
)Pickler_dump
, 1,
2209 "Write an object in pickle format to the object's pickle stream\n"
2211 {"clear_memo", (PyCFunction
)Pickle_clear_memo
, 1,
2212 "clear_memo() -- Clear the picklers memo"},
2213 {"getvalue", (PyCFunction
)Pickle_getvalue
, 1,
2214 "getvalue() -- Finish picking a list-based pickle"},
2215 {NULL
, NULL
} /* sentinel */
2219 static Picklerobject
*
2220 newPicklerobject(PyObject
*file
, int bin
) {
2221 Picklerobject
*self
;
2223 UNLESS (self
= PyObject_New(Picklerobject
, &Picklertype
))
2230 self
->pers_func
= NULL
;
2231 self
->inst_pers_func
= NULL
;
2232 self
->write_buf
= NULL
;
2236 self
->dispatch_table
= NULL
;
2243 UNLESS (self
->file
= file
)
2246 UNLESS (self
->memo
= PyDict_New())
2249 if (PyFile_Check(file
)) {
2250 self
->fp
= PyFile_AsFile(file
);
2251 if (self
->fp
== NULL
) {
2252 PyErr_SetString(PyExc_ValueError
, "I/O operation on closed file");
2255 self
->write_func
= write_file
;
2257 else if (PycStringIO_OutputCheck(file
)) {
2258 self
->write_func
= write_cStringIO
;
2260 else if (file
== Py_None
) {
2261 self
->write_func
= write_none
;
2264 self
->write_func
= write_other
;
2266 if (! Pdata_Check(file
)) {
2267 UNLESS (self
->write
= PyObject_GetAttr(file
, write_str
)) {
2269 PyErr_SetString(PyExc_TypeError
, "argument must have 'write' "
2275 UNLESS (self
->write_buf
=
2276 (char *)malloc(WRITE_BUF_SIZE
* sizeof(char))) {
2282 if (PyEval_GetRestricted()) {
2283 /* Restricted execution, get private tables */
2286 UNLESS (m
=PyImport_Import(copy_reg_str
)) goto err
;
2287 self
->dispatch_table
=PyObject_GetAttr(m
, dispatch_table_str
);
2289 UNLESS (self
->dispatch_table
) goto err
;
2292 self
->dispatch_table
=dispatch_table
;
2293 Py_INCREF(dispatch_table
);
2299 Py_DECREF((PyObject
*)self
);
2305 get_Pickler(PyObject
*self
, PyObject
*args
) {
2306 PyObject
*file
=NULL
;
2310 if (! PyArg_ParseTuple(args
, "|i:Pickler", &bin
)) {
2313 if (! PyArg_ParseTuple(args
, "O|i:Pickler", &file
, &bin
))
2316 return (PyObject
*)newPicklerobject(file
, bin
);
2321 Pickler_dealloc(Picklerobject
*self
) {
2322 Py_XDECREF(self
->write
);
2323 Py_XDECREF(self
->memo
);
2324 Py_XDECREF(self
->arg
);
2325 Py_XDECREF(self
->file
);
2326 Py_XDECREF(self
->pers_func
);
2327 Py_XDECREF(self
->inst_pers_func
);
2328 Py_XDECREF(self
->dispatch_table
);
2330 if (self
->write_buf
) {
2331 free(self
->write_buf
);
2339 Pickler_getattr(Picklerobject
*self
, char *name
) {
2343 if (strcmp(name
, "persistent_id") == 0) {
2344 if (!self
->pers_func
) {
2345 PyErr_SetString(PyExc_AttributeError
, name
);
2349 Py_INCREF(self
->pers_func
);
2350 return self
->pers_func
;
2354 if (strcmp(name
, "memo") == 0) {
2356 PyErr_SetString(PyExc_AttributeError
, name
);
2360 Py_INCREF(self
->memo
);
2365 if (strcmp(name
, "PicklingError") == 0) {
2366 Py_INCREF(PicklingError
);
2367 return PicklingError
;
2371 if (strcmp(name
, "binary")==0)
2372 return PyInt_FromLong(self
->bin
);
2375 if (strcmp(name
, "fast")==0)
2376 return PyInt_FromLong(self
->fast
);
2379 if (strcmp(name
, "getvalue")==0 && ! Pdata_Check(self
->file
)) {
2380 PyErr_SetString(PyExc_AttributeError
, name
);
2385 return Py_FindMethod(Pickler_methods
, (PyObject
*)self
, name
);
2390 Pickler_setattr(Picklerobject
*self
, char *name
, PyObject
*value
) {
2393 PyErr_SetString(PyExc_TypeError
,
2394 "attribute deletion is not supported");
2398 if (strcmp(name
, "persistent_id") == 0) {
2399 Py_XDECREF(self
->pers_func
);
2400 self
->pers_func
= value
;
2405 if (strcmp(name
, "inst_persistent_id") == 0) {
2406 Py_XDECREF(self
->inst_pers_func
);
2407 self
->inst_pers_func
= value
;
2412 if (strcmp(name
, "memo") == 0) {
2413 if (! PyDict_Check(value
)) {
2414 PyErr_SetString(PyExc_TypeError
, "memo must be a dictionary");
2417 Py_XDECREF(self
->memo
);
2423 if (strcmp(name
, "binary")==0) {
2424 self
->bin
=PyObject_IsTrue(value
);
2428 if (strcmp(name
, "fast")==0) {
2429 self
->fast
=PyObject_IsTrue(value
);
2433 PyErr_SetString(PyExc_AttributeError
, name
);
2438 static char Picklertype__doc__
[] =
2439 "Objects that know how to pickle objects\n"
2442 static PyTypeObject Picklertype
= {
2443 PyObject_HEAD_INIT(NULL
)
2445 "Pickler", /*tp_name*/
2446 sizeof(Picklerobject
), /*tp_basicsize*/
2449 (destructor
)Pickler_dealloc
, /*tp_dealloc*/
2450 (printfunc
)0, /*tp_print*/
2451 (getattrfunc
)Pickler_getattr
, /*tp_getattr*/
2452 (setattrfunc
)Pickler_setattr
, /*tp_setattr*/
2453 (cmpfunc
)0, /*tp_compare*/
2454 (reprfunc
)0, /*tp_repr*/
2456 0, /*tp_as_sequence*/
2457 0, /*tp_as_mapping*/
2458 (hashfunc
)0, /*tp_hash*/
2459 (ternaryfunc
)0, /*tp_call*/
2460 (reprfunc
)0, /*tp_str*/
2462 /* Space for future expansion */
2464 Picklertype__doc__
/* Documentation string */
2468 find_class(PyObject
*py_module_name
, PyObject
*py_global_name
, PyObject
*fc
) {
2469 PyObject
*global
= 0, *module
;
2473 PyErr_SetString(UnpicklingError
,
2474 "Global and instance pickles are not supported.");
2477 return PyObject_CallFunction(fc
, "OO", py_module_name
, py_global_name
);
2480 module
= PySys_GetObject("modules");
2484 module
= PyDict_GetItem(module
, py_module_name
);
2485 if (module
== NULL
) {
2486 module
= PyImport_Import(py_module_name
);
2489 global
= PyObject_GetAttr(module
, py_global_name
);
2493 global
= PyObject_GetAttr(module
, py_global_name
);
2494 if (global
== NULL
) {
2496 sprintf(buf
, "Failed to import class %.128s from module %.128s",
2497 PyString_AS_STRING((PyStringObject
*)py_global_name
),
2498 PyString_AS_STRING((PyStringObject
*)py_module_name
));
2499 PyErr_SetString(PyExc_SystemError
, buf
);
2506 marker(Unpicklerobject
*self
) {
2507 if (self
->num_marks
< 1) {
2508 PyErr_SetString(UnpicklingError
, "could not find MARK");
2512 return self
->marks
[--self
->num_marks
];
2517 load_none(Unpicklerobject
*self
) {
2518 PDATA_APPEND(self
->stack
, Py_None
, -1);
2523 bad_readline(void) {
2524 PyErr_SetString(UnpicklingError
, "pickle data was truncated");
2529 load_int(Unpicklerobject
*self
) {
2530 PyObject
*py_int
= 0;
2535 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2536 if (len
< 2) return bad_readline();
2537 UNLESS (s
=pystrndup(s
,len
)) return -1;
2540 l
= strtol(s
, &endptr
, 0);
2542 if (errno
|| (*endptr
!= '\n') || (endptr
[1] != '\0')) {
2543 /* Hm, maybe we've got something long. Let's try reading
2544 it as a Python long object. */
2546 py_int
= PyLong_FromString(s
, NULL
, 0);
2547 if (py_int
== NULL
) {
2548 PyErr_SetString(PyExc_ValueError
,
2549 "could not convert string to int");
2554 UNLESS (py_int
= PyInt_FromLong(l
)) goto finally
;
2558 PDATA_PUSH(self
->stack
, py_int
, -1);
2569 calc_binint(char *s
, int x
) {
2574 for (i
= 0, l
= 0L; i
< x
; i
++) {
2575 c
= (unsigned char)s
[i
];
2576 l
|= (long)c
<< (i
* 8);
2579 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
2580 * is signed, so on a box with longs bigger than 4 bytes we need
2581 * to extend a BININT's sign bit to the full width.
2583 if (x
== 4 && l
& (1L << 31))
2591 load_binintx(Unpicklerobject
*self
, char *s
, int x
) {
2592 PyObject
*py_int
= 0;
2595 l
= calc_binint(s
, x
);
2597 UNLESS (py_int
= PyInt_FromLong(l
))
2600 PDATA_PUSH(self
->stack
, py_int
, -1);
2606 load_binint(Unpicklerobject
*self
) {
2609 if ((*self
->read_func
)(self
, &s
, 4) < 0)
2612 return load_binintx(self
, s
, 4);
2617 load_binint1(Unpicklerobject
*self
) {
2620 if ((*self
->read_func
)(self
, &s
, 1) < 0)
2623 return load_binintx(self
, s
, 1);
2628 load_binint2(Unpicklerobject
*self
) {
2631 if ((*self
->read_func
)(self
, &s
, 2) < 0)
2634 return load_binintx(self
, s
, 2);
2638 load_long(Unpicklerobject
*self
) {
2643 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2644 if (len
< 2) return bad_readline();
2645 UNLESS (s
=pystrndup(s
,len
)) return -1;
2647 UNLESS (l
= PyLong_FromString(s
, &end
, 0))
2651 PDATA_PUSH(self
->stack
, l
, -1);
2662 load_float(Unpicklerobject
*self
) {
2663 PyObject
*py_float
= 0;
2668 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2669 if (len
< 2) return bad_readline();
2670 UNLESS (s
=pystrndup(s
,len
)) return -1;
2673 d
= strtod(s
, &endptr
);
2675 if (errno
|| (endptr
[0] != '\n') || (endptr
[1] != '\0')) {
2676 PyErr_SetString(PyExc_ValueError
,
2677 "could not convert string to float");
2681 UNLESS (py_float
= PyFloat_FromDouble(d
))
2685 PDATA_PUSH(self
->stack
, py_float
, -1);
2695 load_binfloat(Unpicklerobject
*self
) {
2696 PyObject
*py_float
= 0;
2702 if ((*self
->read_func
)(self
, &p
, 8) < 0)
2707 e
= (*p
& 0x7F) << 4;
2712 fhi
= (*p
& 0xF) << 24;
2716 fhi
|= (*p
& 0xFF) << 16;
2720 fhi
|= (*p
& 0xFF) << 8;
2728 flo
= (*p
& 0xFF) << 16;
2732 flo
|= (*p
& 0xFF) << 8;
2738 x
= (double)fhi
+ (double)flo
/ 16777216.0; /* 2**24 */
2739 x
/= 268435456.0; /* 2**28 */
2741 /* XXX This sadly ignores Inf/NaN */
2753 UNLESS (py_float
= PyFloat_FromDouble(x
)) return -1;
2755 PDATA_PUSH(self
->stack
, py_float
, -1);
2760 load_string(Unpicklerobject
*self
) {
2762 int len
, res
= -1, nslash
;
2765 static PyObject
*eval_dict
= 0;
2767 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2768 if (len
< 2) return bad_readline();
2769 UNLESS (s
=pystrndup(s
,len
)) return -1;
2771 /* Check for unquoted quotes (evil strings) */
2773 if (q
!= '"' && q
!= '\'') goto insecure
;
2774 for (p
=s
+1, nslash
=0; *p
; p
++) {
2775 if (*p
==q
&& nslash
%2==0) break;
2776 if (*p
=='\\') nslash
++;
2781 for (p
++; *p
; p
++) if (*p
> ' ') goto insecure
;
2784 /********************************************/
2787 UNLESS (eval_dict
= Py_BuildValue("{s{}}", "__builtins__"))
2790 UNLESS (str
= PyRun_String(s
, Py_eval_input
, eval_dict
, eval_dict
))
2794 PDATA_PUSH(self
->stack
, str
, -1);
2804 PyErr_SetString(PyExc_ValueError
,"insecure string pickle");
2810 load_binstring(Unpicklerobject
*self
) {
2811 PyObject
*py_string
= 0;
2815 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
2817 l
= calc_binint(s
, 4);
2819 if ((*self
->read_func
)(self
, &s
, l
) < 0)
2822 UNLESS (py_string
= PyString_FromStringAndSize(s
, l
))
2825 PDATA_PUSH(self
->stack
, py_string
, -1);
2831 load_short_binstring(Unpicklerobject
*self
) {
2832 PyObject
*py_string
= 0;
2836 if ((*self
->read_func
)(self
, &s
, 1) < 0)
2839 l
= (unsigned char)s
[0];
2841 if ((*self
->read_func
)(self
, &s
, l
) < 0) return -1;
2843 UNLESS (py_string
= PyString_FromStringAndSize(s
, l
)) return -1;
2845 PDATA_PUSH(self
->stack
, py_string
, -1);
2850 #ifdef Py_USING_UNICODE
2852 load_unicode(Unpicklerobject
*self
) {
2857 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
2858 if (len
< 1) return bad_readline();
2860 UNLESS (str
= PyUnicode_DecodeRawUnicodeEscape(s
, len
- 1, NULL
))
2863 PDATA_PUSH(self
->stack
, str
, -1);
2872 #ifdef Py_USING_UNICODE
2874 load_binunicode(Unpicklerobject
*self
) {
2879 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
2881 l
= calc_binint(s
, 4);
2883 if ((*self
->read_func
)(self
, &s
, l
) < 0)
2886 UNLESS (unicode
= PyUnicode_DecodeUTF8(s
, l
, NULL
))
2889 PDATA_PUSH(self
->stack
, unicode
, -1);
2896 load_tuple(Unpicklerobject
*self
) {
2900 if ((i
= marker(self
)) < 0) return -1;
2901 UNLESS (tup
=Pdata_popTuple(self
->stack
, i
)) return -1;
2902 PDATA_PUSH(self
->stack
, tup
, -1);
2907 load_empty_tuple(Unpicklerobject
*self
) {
2910 UNLESS (tup
=PyTuple_New(0)) return -1;
2911 PDATA_PUSH(self
->stack
, tup
, -1);
2916 load_empty_list(Unpicklerobject
*self
) {
2919 UNLESS (list
=PyList_New(0)) return -1;
2920 PDATA_PUSH(self
->stack
, list
, -1);
2925 load_empty_dict(Unpicklerobject
*self
) {
2928 UNLESS (dict
=PyDict_New()) return -1;
2929 PDATA_PUSH(self
->stack
, dict
, -1);
2935 load_list(Unpicklerobject
*self
) {
2939 if ((i
= marker(self
)) < 0) return -1;
2940 UNLESS (list
=Pdata_popList(self
->stack
, i
)) return -1;
2941 PDATA_PUSH(self
->stack
, list
, -1);
2946 load_dict(Unpicklerobject
*self
) {
2947 PyObject
*dict
, *key
, *value
;
2950 if ((i
= marker(self
)) < 0) return -1;
2951 j
=self
->stack
->length
;
2953 UNLESS (dict
= PyDict_New()) return -1;
2955 for (k
= i
+1; k
< j
; k
+= 2) {
2956 key
=self
->stack
->data
[k
-1];
2957 value
=self
->stack
->data
[k
];
2958 if (PyDict_SetItem(dict
, key
, value
) < 0) {
2963 Pdata_clear(self
->stack
, i
);
2964 PDATA_PUSH(self
->stack
, dict
, -1);
2969 Instance_New(PyObject
*cls
, PyObject
*args
) {
2971 PyObject
*safe
=0, *r
=0;
2973 if (PyClass_Check(cls
)) {
2976 if ((l
=PyObject_Size(args
)) < 0) goto err
;
2978 PyObject
*__getinitargs__
;
2980 UNLESS (__getinitargs__
=PyObject_GetAttr(cls
, __getinitargs___str
)) {
2981 /* We have a class with no __getinitargs__, so bypass usual
2986 UNLESS (inst
=PyInstance_NewRaw(cls
, NULL
))
2990 Py_DECREF(__getinitargs__
);
2993 if ((r
=PyInstance_New(cls
, args
, NULL
))) return r
;
2998 if ((has_key
= cPickle_PyMapping_HasKey(safe_constructors
, cls
)) < 0)
3002 if (!(safe
= PyObject_GetAttr(cls
, __safe_for_unpickling___str
)) ||
3003 !PyObject_IsTrue(safe
)) {
3004 cPickle_ErrFormat(UnpicklingError
,
3005 "%s is not safe for unpickling", "O", cls
);
3010 if (args
==Py_None
) {
3011 /* Special case, call cls.__basicnew__() */
3014 UNLESS (basicnew
=PyObject_GetAttr(cls
, __basicnew___str
)) return NULL
;
3015 r
=PyObject_CallObject(basicnew
, NULL
);
3016 Py_DECREF(basicnew
);
3020 if ((r
=PyObject_CallObject(cls
, args
))) return r
;
3024 PyObject
*tp
, *v
, *tb
;
3026 PyErr_Fetch(&tp
, &v
, &tb
);
3027 if ((r
=Py_BuildValue("OOO",v
,cls
,args
))) {
3031 PyErr_Restore(tp
,v
,tb
);
3038 load_obj(Unpicklerobject
*self
) {
3039 PyObject
*class, *tup
, *obj
=0;
3042 if ((i
= marker(self
)) < 0) return -1;
3043 UNLESS (tup
=Pdata_popTuple(self
->stack
, i
+1)) return -1;
3044 PDATA_POP(self
->stack
, class);
3046 obj
= Instance_New(class, tup
);
3051 if (! obj
) return -1;
3052 PDATA_PUSH(self
->stack
, obj
, -1);
3058 load_inst(Unpicklerobject
*self
) {
3059 PyObject
*tup
, *class=0, *obj
=0, *module_name
, *class_name
;
3063 if ((i
= marker(self
)) < 0) return -1;
3065 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3066 if (len
< 2) return bad_readline();
3067 UNLESS (module_name
= PyString_FromStringAndSize(s
, len
- 1)) return -1;
3069 if ((len
= (*self
->readline_func
)(self
, &s
)) >= 0) {
3070 if (len
< 2) return bad_readline();
3071 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
3072 class = find_class(module_name
, class_name
, self
->find_class
);
3073 Py_DECREF(class_name
);
3076 Py_DECREF(module_name
);
3078 if (! class) return -1;
3080 if ((tup
=Pdata_popTuple(self
->stack
, i
))) {
3081 obj
= Instance_New(class, tup
);
3086 if (! obj
) return -1;
3088 PDATA_PUSH(self
->stack
, obj
, -1);
3094 load_global(Unpicklerobject
*self
) {
3095 PyObject
*class = 0, *module_name
= 0, *class_name
= 0;
3099 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3100 if (len
< 2) return bad_readline();
3101 UNLESS (module_name
= PyString_FromStringAndSize(s
, len
- 1)) return -1;
3103 if ((len
= (*self
->readline_func
)(self
, &s
)) >= 0) {
3104 if (len
< 2) return bad_readline();
3105 if ((class_name
= PyString_FromStringAndSize(s
, len
- 1))) {
3106 class = find_class(module_name
, class_name
, self
->find_class
);
3107 Py_DECREF(class_name
);
3110 Py_DECREF(module_name
);
3112 if (! class) return -1;
3113 PDATA_PUSH(self
->stack
, class, -1);
3119 load_persid(Unpicklerobject
*self
) {
3124 if (self
->pers_func
) {
3125 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3126 if (len
< 2) return bad_readline();
3128 UNLESS (pid
= PyString_FromStringAndSize(s
, len
- 1)) return -1;
3130 if (PyList_Check(self
->pers_func
)) {
3131 if (PyList_Append(self
->pers_func
, pid
) < 0) {
3139 pid
= PyObject_CallObject(self
->pers_func
, self
->arg
);
3144 if (! pid
) return -1;
3146 PDATA_PUSH(self
->stack
, pid
, -1);
3150 PyErr_SetString(UnpicklingError
,
3151 "A load persistent id instruction was encountered,\n"
3152 "but no persistent_load function was specified.");
3158 load_binpersid(Unpicklerobject
*self
) {
3161 if (self
->pers_func
) {
3162 PDATA_POP(self
->stack
, pid
);
3163 if (! pid
) return -1;
3165 if (PyList_Check(self
->pers_func
)) {
3166 if (PyList_Append(self
->pers_func
, pid
) < 0) {
3174 pid
= PyObject_CallObject(self
->pers_func
, self
->arg
);
3177 if (! pid
) return -1;
3180 PDATA_PUSH(self
->stack
, pid
, -1);
3184 PyErr_SetString(UnpicklingError
,
3185 "A load persistent id instruction was encountered,\n"
3186 "but no persistent_load function was specified.");
3193 load_pop(Unpicklerobject
*self
) {
3196 UNLESS ((len
=self
->stack
->length
) > 0) return stackUnderflow();
3198 /* Note that we split the (pickle.py) stack into two stacks,
3199 an object stack and a mark stack. We have to be clever and
3200 pop the right one. We do this by looking at the top of the
3204 if ((self
->num_marks
> 0) &&
3205 (self
->marks
[self
->num_marks
- 1] == len
))
3209 Py_DECREF(self
->stack
->data
[len
]);
3210 self
->stack
->length
=len
;
3218 load_pop_mark(Unpicklerobject
*self
) {
3221 if ((i
= marker(self
)) < 0)
3224 Pdata_clear(self
->stack
, i
);
3231 load_dup(Unpicklerobject
*self
) {
3235 if ((len
= self
->stack
->length
) <= 0) return stackUnderflow();
3236 last
=self
->stack
->data
[len
-1];
3238 PDATA_PUSH(self
->stack
, last
, -1);
3244 load_get(Unpicklerobject
*self
) {
3245 PyObject
*py_str
= 0, *value
= 0;
3250 if ((len
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3251 if (len
< 2) return bad_readline();
3253 UNLESS (py_str
= PyString_FromStringAndSize(s
, len
- 1)) return -1;
3255 value
= PyDict_GetItem(self
->memo
, py_str
);
3257 PyErr_SetObject(BadPickleGet
, py_str
);
3260 PDATA_APPEND(self
->stack
, value
, -1);
3270 load_binget(Unpicklerobject
*self
) {
3271 PyObject
*py_key
= 0, *value
= 0;
3276 if ((*self
->read_func
)(self
, &s
, 1) < 0) return -1;
3278 key
= (unsigned char)s
[0];
3279 UNLESS (py_key
= PyInt_FromLong((long)key
)) return -1;
3281 value
= PyDict_GetItem(self
->memo
, py_key
);
3283 PyErr_SetObject(BadPickleGet
, py_key
);
3286 PDATA_APPEND(self
->stack
, value
, -1);
3296 load_long_binget(Unpicklerobject
*self
) {
3297 PyObject
*py_key
= 0, *value
= 0;
3303 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
3305 c
= (unsigned char)s
[0];
3307 c
= (unsigned char)s
[1];
3308 key
|= (long)c
<< 8;
3309 c
= (unsigned char)s
[2];
3310 key
|= (long)c
<< 16;
3311 c
= (unsigned char)s
[3];
3312 key
|= (long)c
<< 24;
3314 UNLESS (py_key
= PyInt_FromLong((long)key
)) return -1;
3316 value
= PyDict_GetItem(self
->memo
, py_key
);
3318 PyErr_SetObject(BadPickleGet
, py_key
);
3321 PDATA_APPEND(self
->stack
, value
, -1);
3331 load_put(Unpicklerobject
*self
) {
3332 PyObject
*py_str
= 0, *value
= 0;
3336 if ((l
= (*self
->readline_func
)(self
, &s
)) < 0) return -1;
3337 if (l
< 2) return bad_readline();
3338 UNLESS (len
=self
->stack
->length
) return stackUnderflow();
3339 UNLESS (py_str
= PyString_FromStringAndSize(s
, l
- 1)) return -1;
3340 value
=self
->stack
->data
[len
-1];
3341 l
=PyDict_SetItem(self
->memo
, py_str
, value
);
3348 load_binput(Unpicklerobject
*self
) {
3349 PyObject
*py_key
= 0, *value
= 0;
3354 if ((*self
->read_func
)(self
, &s
, 1) < 0) return -1;
3355 UNLESS ((len
=self
->stack
->length
) > 0) return stackUnderflow();
3357 key
= (unsigned char)s
[0];
3359 UNLESS (py_key
= PyInt_FromLong((long)key
)) return -1;
3360 value
=self
->stack
->data
[len
-1];
3361 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
3368 load_long_binput(Unpicklerobject
*self
) {
3369 PyObject
*py_key
= 0, *value
= 0;
3375 if ((*self
->read_func
)(self
, &s
, 4) < 0) return -1;
3376 UNLESS (len
=self
->stack
->length
) return stackUnderflow();
3378 c
= (unsigned char)s
[0];
3380 c
= (unsigned char)s
[1];
3381 key
|= (long)c
<< 8;
3382 c
= (unsigned char)s
[2];
3383 key
|= (long)c
<< 16;
3384 c
= (unsigned char)s
[3];
3385 key
|= (long)c
<< 24;
3387 UNLESS (py_key
= PyInt_FromLong(key
)) return -1;
3388 value
=self
->stack
->data
[len
-1];
3389 len
=PyDict_SetItem(self
->memo
, py_key
, value
);
3396 do_append(Unpicklerobject
*self
, int x
) {
3397 PyObject
*value
= 0, *list
= 0, *append_method
= 0;
3400 UNLESS ((len
=self
->stack
->length
) >= x
&& x
> 0) return stackUnderflow();
3401 if (len
==x
) return 0; /* nothing to do */
3403 list
=self
->stack
->data
[x
-1];
3405 if (PyList_Check(list
)) {
3409 slice
=Pdata_popList(self
->stack
, x
);
3410 list_len
= PyList_GET_SIZE(list
);
3411 i
=PyList_SetSlice(list
, list_len
, list_len
, slice
);
3417 UNLESS (append_method
= PyObject_GetAttr(list
, append_str
))
3420 for (i
= x
; i
< len
; i
++) {
3423 value
=self
->stack
->data
[i
];
3425 ARG_TUP(self
, value
);
3427 junk
= PyObject_CallObject(append_method
, self
->arg
);
3431 Pdata_clear(self
->stack
, i
+1);
3432 self
->stack
->length
=x
;
3433 Py_DECREF(append_method
);
3438 self
->stack
->length
=x
;
3439 Py_DECREF(append_method
);
3447 load_append(Unpicklerobject
*self
) {
3448 return do_append(self
, self
->stack
->length
- 1);
3453 load_appends(Unpicklerobject
*self
) {
3454 return do_append(self
, marker(self
));
3459 do_setitems(Unpicklerobject
*self
, int x
) {
3460 PyObject
*value
= 0, *key
= 0, *dict
= 0;
3463 UNLESS ((len
=self
->stack
->length
) >= x
3464 && x
> 0) return stackUnderflow();
3466 dict
=self
->stack
->data
[x
-1];
3468 for (i
= x
+1; i
< len
; i
+= 2) {
3469 key
=self
->stack
->data
[i
-1];
3470 value
=self
->stack
->data
[i
];
3471 if (PyObject_SetItem(dict
, key
, value
) < 0) {
3477 Pdata_clear(self
->stack
, x
);
3484 load_setitem(Unpicklerobject
*self
) {
3485 return do_setitems(self
, self
->stack
->length
- 2);
3489 load_setitems(Unpicklerobject
*self
) {
3490 return do_setitems(self
, marker(self
));
3495 load_build(Unpicklerobject
*self
) {
3496 PyObject
*value
= 0, *inst
= 0, *instdict
= 0, *d_key
= 0, *d_value
= 0,
3497 *junk
= 0, *__setstate__
= 0;
3500 if (self
->stack
->length
< 2) return stackUnderflow();
3501 PDATA_POP(self
->stack
, value
);
3502 if (! value
) return -1;
3503 inst
=self
->stack
->data
[self
->stack
->length
-1];
3505 if ((__setstate__
= PyObject_GetAttr(inst
, __setstate___str
))) {
3506 ARG_TUP(self
, value
);
3508 junk
= PyObject_CallObject(__setstate__
, self
->arg
);
3511 Py_DECREF(__setstate__
);
3512 if (! junk
) return -1;
3518 if ((instdict
= PyObject_GetAttr(inst
, __dict___str
))) {
3520 while (PyDict_Next(value
, &i
, &d_key
, &d_value
)) {
3521 if (PyObject_SetItem(instdict
, d_key
, d_value
) < 0) {
3526 Py_DECREF(instdict
);
3537 load_mark(Unpicklerobject
*self
) {
3540 /* Note that we split the (pickle.py) stack into two stacks, an
3541 object stack and a mark stack. Here we push a mark onto the
3545 if ((self
->num_marks
+ 1) >= self
->marks_size
) {
3546 s
=self
->marks_size
+20;
3547 if (s
<= self
->num_marks
) s
=self
->num_marks
+ 1;
3548 if (self
->marks
== NULL
)
3549 self
->marks
=(int *)malloc(s
* sizeof(int));
3551 self
->marks
=(int *)realloc(self
->marks
, s
* sizeof(int));
3552 if (! self
->marks
) {
3556 self
->marks_size
= s
;
3559 self
->marks
[self
->num_marks
++] = self
->stack
->length
;
3565 load_reduce(Unpicklerobject
*self
) {
3566 PyObject
*callable
= 0, *arg_tup
= 0, *ob
= 0;
3568 PDATA_POP(self
->stack
, arg_tup
);
3569 if (! arg_tup
) return -1;
3570 PDATA_POP(self
->stack
, callable
);
3572 ob
= Instance_New(callable
, arg_tup
);
3573 Py_DECREF(callable
);
3577 if (! ob
) return -1;
3579 PDATA_PUSH(self
->stack
, ob
, -1);
3584 load(Unpicklerobject
*self
) {
3585 PyObject
*err
= 0, *val
= 0;
3588 self
->num_marks
= 0;
3589 if (self
->stack
->length
) Pdata_clear(self
->stack
, 0);
3592 if ((*self
->read_func
)(self
, &s
, 1) < 0)
3597 if (load_none(self
) < 0)
3602 if (load_binint(self
) < 0)
3607 if (load_binint1(self
) < 0)
3612 if (load_binint2(self
) < 0)
3617 if (load_int(self
) < 0)
3622 if (load_long(self
) < 0)
3627 if (load_float(self
) < 0)
3632 if (load_binfloat(self
) < 0)
3637 if (load_binstring(self
) < 0)
3641 case SHORT_BINSTRING
:
3642 if (load_short_binstring(self
) < 0)
3647 if (load_string(self
) < 0)
3651 #ifdef Py_USING_UNICODE
3653 if (load_unicode(self
) < 0)
3658 if (load_binunicode(self
) < 0)
3664 if (load_empty_tuple(self
) < 0)
3669 if (load_tuple(self
) < 0)
3674 if (load_empty_list(self
) < 0)
3679 if (load_list(self
) < 0)
3684 if (load_empty_dict(self
) < 0)
3689 if (load_dict(self
) < 0)
3694 if (load_obj(self
) < 0)
3699 if (load_inst(self
) < 0)
3704 if (load_global(self
) < 0)
3709 if (load_append(self
) < 0)
3714 if (load_appends(self
) < 0)
3719 if (load_build(self
) < 0)
3724 if (load_dup(self
) < 0)
3729 if (load_binget(self
) < 0)
3734 if (load_long_binget(self
) < 0)
3739 if (load_get(self
) < 0)
3744 if (load_mark(self
) < 0)
3749 if (load_binput(self
) < 0)
3754 if (load_long_binput(self
) < 0)
3759 if (load_put(self
) < 0)
3764 if (load_pop(self
) < 0)
3769 if (load_pop_mark(self
) < 0)
3774 if (load_setitem(self
) < 0)
3779 if (load_setitems(self
) < 0)
3787 if (load_persid(self
) < 0)
3792 if (load_binpersid(self
) < 0)
3797 if (load_reduce(self
) < 0)
3802 cPickle_ErrFormat(UnpicklingError
, "invalid load key, '%s'.",
3810 if ((err
= PyErr_Occurred())) {
3811 if (err
== PyExc_EOFError
) {
3812 PyErr_SetNone(PyExc_EOFError
);
3817 PDATA_POP(self
->stack
, val
);
3822 /* No-load functions to support noload, which is used to
3823 find persistent references. */
3826 noload_obj(Unpicklerobject
*self
) {
3829 if ((i
= marker(self
)) < 0) return -1;
3830 return Pdata_clear(self
->stack
, i
+1);
3835 noload_inst(Unpicklerobject
*self
) {
3839 if ((i
= marker(self
)) < 0) return -1;
3840 Pdata_clear(self
->stack
, i
);
3841 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3842 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3843 PDATA_APPEND(self
->stack
, Py_None
,-1);
3848 noload_global(Unpicklerobject
*self
) {
3851 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3852 if ((*self
->readline_func
)(self
, &s
) < 0) return -1;
3853 PDATA_APPEND(self
->stack
, Py_None
,-1);
3858 noload_reduce(Unpicklerobject
*self
) {
3860 if (self
->stack
->length
< 2) return stackUnderflow();
3861 Pdata_clear(self
->stack
, self
->stack
->length
-2);
3862 PDATA_APPEND(self
->stack
, Py_None
,-1);
3867 noload_build(Unpicklerobject
*self
) {
3869 if (self
->stack
->length
< 1) return stackUnderflow();
3870 Pdata_clear(self
->stack
, self
->stack
->length
-1);
3876 noload(Unpicklerobject
*self
) {
3877 PyObject
*err
= 0, *val
= 0;
3880 self
->num_marks
= 0;
3881 Pdata_clear(self
->stack
, 0);
3884 if ((*self
->read_func
)(self
, &s
, 1) < 0)
3889 if (load_none(self
) < 0)
3894 if (load_binint(self
) < 0)
3899 if (load_binint1(self
) < 0)
3904 if (load_binint2(self
) < 0)
3909 if (load_int(self
) < 0)
3914 if (load_long(self
) < 0)
3919 if (load_float(self
) < 0)
3924 if (load_binfloat(self
) < 0)
3929 if (load_binstring(self
) < 0)
3933 case SHORT_BINSTRING
:
3934 if (load_short_binstring(self
) < 0)
3939 if (load_string(self
) < 0)
3943 #ifdef Py_USING_UNICODE
3945 if (load_unicode(self
) < 0)
3950 if (load_binunicode(self
) < 0)
3956 if (load_empty_tuple(self
) < 0)
3961 if (load_tuple(self
) < 0)
3966 if (load_empty_list(self
) < 0)
3971 if (load_list(self
) < 0)
3976 if (load_empty_dict(self
) < 0)
3981 if (load_dict(self
) < 0)
3986 if (noload_obj(self
) < 0)
3991 if (noload_inst(self
) < 0)
3996 if (noload_global(self
) < 0)
4001 if (load_append(self
) < 0)
4006 if (load_appends(self
) < 0)
4011 if (noload_build(self
) < 0)
4016 if (load_dup(self
) < 0)
4021 if (load_binget(self
) < 0)
4026 if (load_long_binget(self
) < 0)
4031 if (load_get(self
) < 0)
4036 if (load_mark(self
) < 0)
4041 if (load_binput(self
) < 0)
4046 if (load_long_binput(self
) < 0)
4051 if (load_put(self
) < 0)
4056 if (load_pop(self
) < 0)
4061 if (load_pop_mark(self
) < 0)
4066 if (load_setitem(self
) < 0)
4071 if (load_setitems(self
) < 0)
4079 if (load_persid(self
) < 0)
4084 if (load_binpersid(self
) < 0)
4089 if (noload_reduce(self
) < 0)
4094 cPickle_ErrFormat(UnpicklingError
, "invalid load key, '%s'.",
4102 if ((err
= PyErr_Occurred())) {
4103 if (err
== PyExc_EOFError
) {
4104 PyErr_SetNone(PyExc_EOFError
);
4109 PDATA_POP(self
->stack
, val
);
4115 Unpickler_load(Unpicklerobject
*self
, PyObject
*args
) {
4116 UNLESS (PyArg_ParseTuple(args
, ":load"))
4123 Unpickler_noload(Unpicklerobject
*self
, PyObject
*args
) {
4124 UNLESS (PyArg_ParseTuple(args
, ":noload"))
4127 return noload(self
);
4131 static struct PyMethodDef Unpickler_methods
[] = {
4132 {"load", (PyCFunction
)Unpickler_load
, 1,
4133 "load() -- Load a pickle"
4135 {"noload", (PyCFunction
)Unpickler_noload
, 1,
4136 "noload() -- not load a pickle, but go through most of the motions\n"
4138 "This function can be used to read past a pickle without instantiating\n"
4139 "any objects or importing any modules. It can also be used to find all\n"
4140 "persistent references without instantiating any objects or importing\n"
4143 {NULL
, NULL
} /* sentinel */
4147 static Unpicklerobject
*
4148 newUnpicklerobject(PyObject
*f
) {
4149 Unpicklerobject
*self
;
4151 UNLESS (self
= PyObject_New(Unpicklerobject
, &Unpicklertype
))
4156 self
->stack
= (Pdata
*)Pdata_New();
4157 self
->pers_func
= NULL
;
4158 self
->last_string
= NULL
;
4160 self
->num_marks
= 0;
4161 self
->marks_size
= 0;
4164 self
->readline
= NULL
;
4165 self
->safe_constructors
= NULL
;
4166 self
->find_class
= NULL
;
4168 UNLESS (self
->memo
= PyDict_New())
4174 /* Set read, readline based on type of f */
4175 if (PyFile_Check(f
)) {
4176 self
->fp
= PyFile_AsFile(f
);
4177 if (self
->fp
== NULL
) {
4178 PyErr_SetString(PyExc_ValueError
, "I/O operation on closed file");
4181 self
->read_func
= read_file
;
4182 self
->readline_func
= readline_file
;
4184 else if (PycStringIO_InputCheck(f
)) {
4186 self
->read_func
= read_cStringIO
;
4187 self
->readline_func
= readline_cStringIO
;
4192 self
->read_func
= read_other
;
4193 self
->readline_func
= readline_other
;
4195 UNLESS ((self
->readline
= PyObject_GetAttr(f
, readline_str
)) &&
4196 (self
->read
= PyObject_GetAttr(f
, read_str
))) {
4198 PyErr_SetString( PyExc_TypeError
, "argument must have 'read' and "
4199 "'readline' attributes" );
4204 if (PyEval_GetRestricted()) {
4205 /* Restricted execution, get private tables */
4208 UNLESS (m
=PyImport_Import(copy_reg_str
)) goto err
;
4209 self
->safe_constructors
=PyObject_GetAttr(m
, safe_constructors_str
);
4211 UNLESS (self
->safe_constructors
) goto err
;
4214 self
->safe_constructors
=safe_constructors
;
4215 Py_INCREF(safe_constructors
);
4221 Py_DECREF((PyObject
*)self
);
4227 get_Unpickler(PyObject
*self
, PyObject
*args
) {
4230 UNLESS (PyArg_ParseTuple(args
, "O:Unpickler", &file
))
4232 return (PyObject
*)newUnpicklerobject(file
);
4237 Unpickler_dealloc(Unpicklerobject
*self
) {
4238 Py_XDECREF(self
->readline
);
4239 Py_XDECREF(self
->read
);
4240 Py_XDECREF(self
->file
);
4241 Py_XDECREF(self
->memo
);
4242 Py_XDECREF(self
->stack
);
4243 Py_XDECREF(self
->pers_func
);
4244 Py_XDECREF(self
->arg
);
4245 Py_XDECREF(self
->last_string
);
4246 Py_XDECREF(self
->safe_constructors
);
4252 if (self
->buf_size
) {
4261 Unpickler_getattr(Unpicklerobject
*self
, char *name
) {
4262 if (!strcmp(name
, "persistent_load")) {
4263 if (!self
->pers_func
) {
4264 PyErr_SetString(PyExc_AttributeError
, name
);
4268 Py_INCREF(self
->pers_func
);
4269 return self
->pers_func
;
4272 if (!strcmp(name
, "find_global")) {
4273 if (!self
->find_class
) {
4274 PyErr_SetString(PyExc_AttributeError
, name
);
4278 Py_INCREF(self
->find_class
);
4279 return self
->find_class
;
4282 if (!strcmp(name
, "memo")) {
4284 PyErr_SetString(PyExc_AttributeError
, name
);
4288 Py_INCREF(self
->memo
);
4292 if (!strcmp(name
, "UnpicklingError")) {
4293 Py_INCREF(UnpicklingError
);
4294 return UnpicklingError
;
4297 return Py_FindMethod(Unpickler_methods
, (PyObject
*)self
, name
);
4302 Unpickler_setattr(Unpicklerobject
*self
, char *name
, PyObject
*value
) {
4304 if (!strcmp(name
, "persistent_load")) {
4305 Py_XDECREF(self
->pers_func
);
4306 self
->pers_func
= value
;
4311 if (!strcmp(name
, "find_global")) {
4312 Py_XDECREF(self
->find_class
);
4313 self
->find_class
= value
;
4319 PyErr_SetString(PyExc_TypeError
,
4320 "attribute deletion is not supported");
4324 if (strcmp(name
, "memo") == 0) {
4325 if (! PyDict_Check(value
)) {
4326 PyErr_SetString(PyExc_TypeError
, "memo must be a dictionary");
4329 Py_XDECREF(self
->memo
);
4335 PyErr_SetString(PyExc_AttributeError
, name
);
4341 cpm_dump(PyObject
*self
, PyObject
*args
) {
4342 PyObject
*ob
, *file
, *res
= NULL
;
4343 Picklerobject
*pickler
= 0;
4346 UNLESS (PyArg_ParseTuple(args
, "OO|i", &ob
, &file
, &bin
))
4349 UNLESS (pickler
= newPicklerobject(file
, bin
))
4352 if (dump(pickler
, ob
) < 0)
4359 Py_XDECREF(pickler
);
4366 cpm_dumps(PyObject
*self
, PyObject
*args
) {
4367 PyObject
*ob
, *file
= 0, *res
= NULL
;
4368 Picklerobject
*pickler
= 0;
4371 UNLESS (PyArg_ParseTuple(args
, "O|i:dumps", &ob
, &bin
))
4374 UNLESS (file
= PycStringIO
->NewOutput(128))
4377 UNLESS (pickler
= newPicklerobject(file
, bin
))
4380 if (dump(pickler
, ob
) < 0)
4383 res
= PycStringIO
->cgetvalue(file
);
4386 Py_XDECREF(pickler
);
4394 cpm_load(PyObject
*self
, PyObject
*args
) {
4395 Unpicklerobject
*unpickler
= 0;
4396 PyObject
*ob
, *res
= NULL
;
4398 UNLESS (PyArg_ParseTuple(args
, "O:load", &ob
))
4401 UNLESS (unpickler
= newUnpicklerobject(ob
))
4404 res
= load(unpickler
);
4407 Py_XDECREF(unpickler
);
4414 cpm_loads(PyObject
*self
, PyObject
*args
) {
4415 PyObject
*ob
, *file
= 0, *res
= NULL
;
4416 Unpicklerobject
*unpickler
= 0;
4418 UNLESS (PyArg_ParseTuple(args
, "S:loads", &ob
))
4421 UNLESS (file
= PycStringIO
->NewInput(ob
))
4424 UNLESS (unpickler
= newUnpicklerobject(file
))
4427 res
= load(unpickler
);
4431 Py_XDECREF(unpickler
);
4437 static char Unpicklertype__doc__
[] =
4438 "Objects that know how to unpickle";
4440 static PyTypeObject Unpicklertype
= {
4441 PyObject_HEAD_INIT(NULL
)
4443 "Unpickler", /*tp_name*/
4444 sizeof(Unpicklerobject
), /*tp_basicsize*/
4447 (destructor
)Unpickler_dealloc
, /*tp_dealloc*/
4448 (printfunc
)0, /*tp_print*/
4449 (getattrfunc
)Unpickler_getattr
, /*tp_getattr*/
4450 (setattrfunc
)Unpickler_setattr
, /*tp_setattr*/
4451 (cmpfunc
)0, /*tp_compare*/
4452 (reprfunc
)0, /*tp_repr*/
4454 0, /*tp_as_sequence*/
4455 0, /*tp_as_mapping*/
4456 (hashfunc
)0, /*tp_hash*/
4457 (ternaryfunc
)0, /*tp_call*/
4458 (reprfunc
)0, /*tp_str*/
4460 /* Space for future expansion */
4462 Unpicklertype__doc__
/* Documentation string */
4465 static struct PyMethodDef cPickle_methods
[] = {
4466 {"dump", (PyCFunction
)cpm_dump
, 1,
4467 "dump(object, file, [binary]) --"
4468 "Write an object in pickle format to the given file\n"
4470 "If the optional argument, binary, is provided and is true, then the\n"
4471 "pickle will be written in binary format, which is more space and\n"
4472 "computationally efficient. \n"
4474 {"dumps", (PyCFunction
)cpm_dumps
, 1,
4475 "dumps(object, [binary]) --"
4476 "Return a string containing an object in pickle format\n"
4478 "If the optional argument, binary, is provided and is true, then the\n"
4479 "pickle will be written in binary format, which is more space and\n"
4480 "computationally efficient. \n"
4482 {"load", (PyCFunction
)cpm_load
, 1,
4483 "load(file) -- Load a pickle from the given file"},
4484 {"loads", (PyCFunction
)cpm_loads
, 1,
4485 "loads(string) -- Load a pickle from the given string"},
4486 {"Pickler", (PyCFunction
)get_Pickler
, 1,
4487 "Pickler(file, [binary]) -- Create a pickler\n"
4489 "If the optional argument, binary, is provided and is true, then\n"
4490 "pickles will be written in binary format, which is more space and\n"
4491 "computationally efficient. \n"
4493 {"Unpickler", (PyCFunction
)get_Unpickler
, 1,
4494 "Unpickler(file) -- Create an unpickler"},
4499 init_stuff(PyObject
*module_dict
) {
4500 PyObject
*copy_reg
, *t
, *r
;
4502 #define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
4504 INIT_STR(__class__
);
4505 INIT_STR(__getinitargs__
);
4507 INIT_STR(__getstate__
);
4508 INIT_STR(__setstate__
);
4511 INIT_STR(__reduce__
);
4513 INIT_STR(__safe_for_unpickling__
);
4518 INIT_STR(dispatch_table
);
4519 INIT_STR(safe_constructors
);
4520 INIT_STR(__basicnew__
);
4522 UNLESS (copy_reg
= PyImport_ImportModule("copy_reg"))
4525 /* These next few are special because we want to use different
4526 ones in restricted mode. */
4528 UNLESS (dispatch_table
= PyObject_GetAttr(copy_reg
, dispatch_table_str
))
4531 UNLESS (safe_constructors
= PyObject_GetAttr(copy_reg
,
4532 safe_constructors_str
))
4535 Py_DECREF(copy_reg
);
4537 /* Down to here ********************************** */
4539 UNLESS (empty_tuple
= PyTuple_New(0))
4543 UNLESS (t
=PyImport_ImportModule("__builtin__")) return -1;
4544 if (PyDict_SetItemString(module_dict
, "__builtins__", t
) < 0)
4547 UNLESS (t
=PyDict_New()) return -1;
4548 UNLESS (r
=PyRun_String(
4549 "def __init__(self, *args): self.args=args\n\n"
4550 "def __str__(self):\n"
4551 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
4553 module_dict
, t
) ) return -1;
4556 UNLESS (PickleError
= PyErr_NewException("cPickle.PickleError", NULL
, t
))
4562 UNLESS (PicklingError
= PyErr_NewException("cPickle.PicklingError",
4566 UNLESS (t
=PyDict_New()) return -1;
4567 UNLESS (r
=PyRun_String(
4568 "def __init__(self, *args): self.args=args\n\n"
4569 "def __str__(self):\n"
4571 " a=a and type(a[0]) or '(what)'\n"
4572 " return 'Cannot pickle %s objects' % a\n"
4574 module_dict
, t
) ) return -1;
4577 UNLESS (UnpickleableError
= PyErr_NewException(
4578 "cPickle.UnpickleableError", PicklingError
, t
))
4583 UNLESS (UnpicklingError
= PyErr_NewException("cPickle.UnpicklingError",
4587 if (PyDict_SetItemString(module_dict
, "PickleError",
4591 if (PyDict_SetItemString(module_dict
, "PicklingError",
4595 if (PyDict_SetItemString(module_dict
, "UnpicklingError",
4596 UnpicklingError
) < 0)
4599 if (PyDict_SetItemString(module_dict
, "UnpickleableError",
4600 UnpickleableError
) < 0)
4603 UNLESS (BadPickleGet
= PyString_FromString("cPickle.BadPickleGet"))
4606 if (PyDict_SetItemString(module_dict
, "BadPickleGet",
4615 #ifndef DL_EXPORT /* declarations for DLL import/export */
4616 #define DL_EXPORT(RTYPE) RTYPE
4620 PyObject
*m
, *d
, *di
, *v
, *k
;
4623 PyObject
*format_version
;
4624 PyObject
*compatible_formats
;
4626 Picklertype
.ob_type
= &PyType_Type
;
4627 Unpicklertype
.ob_type
= &PyType_Type
;
4628 PdataType
.ob_type
= &PyType_Type
;
4630 /* Initialize some pieces. We need to do this before module creation,
4631 so we're forced to use a temporary dictionary. :(
4635 if (init_stuff(di
) < 0) return;
4637 /* Create the module and add the functions */
4638 m
= Py_InitModule4("cPickle", cPickle_methods
,
4639 cPickle_module_documentation
,
4640 (PyObject
*)NULL
,PYTHON_API_VERSION
);
4642 /* Add some symbolic constants to the module */
4643 d
= PyModule_GetDict(m
);
4644 PyDict_SetItemString(d
,"__version__", v
= PyString_FromString(rev
));
4647 /* Copy data from di. Waaa. */
4648 for (i
=0; PyDict_Next(di
, &i
, &k
, &v
); ) {
4649 if (PyObject_SetItem(d
, k
, v
) < 0) {
4656 format_version
= PyString_FromString("1.3");
4657 compatible_formats
= Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
4659 PyDict_SetItemString(d
, "format_version", format_version
);
4660 PyDict_SetItemString(d
, "compatible_formats", compatible_formats
);
4661 Py_XDECREF(format_version
);
4662 Py_XDECREF(compatible_formats
);