Fix HAVE_DECL_ISINF/ISNAN test (again).
[python.git] / Modules / cPickle.c
blob7f836c31269b3d04ecf8a8e435db2ec070d9cffd
1 #include "Python.h"
2 #include "cStringIO.h"
3 #include "structmember.h"
5 PyDoc_STRVAR(cPickle_module_documentation,
6 "C implementation and optimization of the Python pickle module.");
8 #ifndef Py_eval_input
9 #include <graminit.h>
10 #define Py_eval_input eval_input
11 #endif /* Py_eval_input */
13 #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
15 #define WRITE_BUF_SIZE 256
17 /* Bump this when new opcodes are added to the pickle protocol. */
18 #define HIGHEST_PROTOCOL 2
21 * Note: The UNICODE macro controls the TCHAR meaning of the win32 API. Since
22 * all headers have already been included here, we can safely redefine it.
24 #ifdef UNICODE
25 # undef UNICODE
26 #endif
29 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
30 * docs are in pickletools.py.
32 #define MARK '('
33 #define STOP '.'
34 #define POP '0'
35 #define POP_MARK '1'
36 #define DUP '2'
37 #define FLOAT 'F'
38 #define BINFLOAT 'G'
39 #define INT 'I'
40 #define BININT 'J'
41 #define BININT1 'K'
42 #define LONG 'L'
43 #define BININT2 'M'
44 #define NONE 'N'
45 #define PERSID 'P'
46 #define BINPERSID 'Q'
47 #define REDUCE 'R'
48 #define STRING 'S'
49 #define BINSTRING 'T'
50 #define SHORT_BINSTRING 'U'
51 #define UNICODE 'V'
52 #define BINUNICODE 'X'
53 #define APPEND 'a'
54 #define BUILD 'b'
55 #define GLOBAL 'c'
56 #define DICT 'd'
57 #define EMPTY_DICT '}'
58 #define APPENDS 'e'
59 #define GET 'g'
60 #define BINGET 'h'
61 #define INST 'i'
62 #define LONG_BINGET 'j'
63 #define LIST 'l'
64 #define EMPTY_LIST ']'
65 #define OBJ 'o'
66 #define PUT 'p'
67 #define BINPUT 'q'
68 #define LONG_BINPUT 'r'
69 #define SETITEM 's'
70 #define TUPLE 't'
71 #define EMPTY_TUPLE ')'
72 #define SETITEMS 'u'
74 /* Protocol 2. */
75 #define PROTO '\x80' /* identify pickle protocol */
76 #define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
77 #define EXT1 '\x82' /* push object from extension registry; 1-byte index */
78 #define EXT2 '\x83' /* ditto, but 2-byte index */
79 #define EXT4 '\x84' /* ditto, but 4-byte index */
80 #define TUPLE1 '\x85' /* build 1-tuple from stack top */
81 #define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
82 #define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
83 #define NEWTRUE '\x88' /* push True */
84 #define NEWFALSE '\x89' /* push False */
85 #define LONG1 '\x8a' /* push long from < 256 bytes */
86 #define LONG4 '\x8b' /* push really big long */
88 /* There aren't opcodes -- they're ways to pickle bools before protocol 2,
89 * so that unpicklers written before bools were introduced unpickle them
90 * as ints, but unpicklers after can recognize that bools were intended.
91 * Note that protocol 2 added direct ways to pickle bools.
93 #undef TRUE
94 #define TRUE "I01\n"
95 #undef FALSE
96 #define FALSE "I00\n"
98 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
99 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
100 * break if this gets out of synch with pickle.py, but it's unclear that
101 * would help anything either.
103 #define BATCHSIZE 1000
105 static char MARKv = MARK;
107 static PyObject *PickleError;
108 static PyObject *PicklingError;
109 static PyObject *UnpickleableError;
110 static PyObject *UnpicklingError;
111 static PyObject *BadPickleGet;
113 /* As the name says, an empty tuple. */
114 static PyObject *empty_tuple;
116 /* copy_reg.dispatch_table, {type_object: pickling_function} */
117 static PyObject *dispatch_table;
119 /* For EXT[124] opcodes. */
120 /* copy_reg._extension_registry, {(module_name, function_name): code} */
121 static PyObject *extension_registry;
122 /* copy_reg._inverted_registry, {code: (module_name, function_name)} */
123 static PyObject *inverted_registry;
124 /* copy_reg._extension_cache, {code: object} */
125 static PyObject *extension_cache;
127 /* For looking up name pairs in copy_reg._extension_registry. */
128 static PyObject *two_tuple;
130 static PyObject *__class___str, *__getinitargs___str, *__dict___str,
131 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
132 *__reduce_ex___str,
133 *write_str, *append_str,
134 *read_str, *readline_str, *__main___str,
135 *copyreg_str, *dispatch_table_str;
137 /*************************************************************************
138 Internal Data type for pickle data. */
140 typedef struct {
141 PyObject_HEAD
142 int length; /* number of initial slots in data currently used */
143 int size; /* number of slots in data allocated */
144 PyObject **data;
145 } Pdata;
147 static void
148 Pdata_dealloc(Pdata *self)
150 int i;
151 PyObject **p;
153 for (i = self->length, p = self->data; --i >= 0; p++) {
154 Py_DECREF(*p);
156 if (self->data)
157 free(self->data);
158 PyObject_Del(self);
161 static PyTypeObject PdataType = {
162 PyVarObject_HEAD_INIT(NULL, 0) "cPickle.Pdata", sizeof(Pdata), 0,
163 (destructor)Pdata_dealloc,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
167 #define Pdata_Check(O) (Py_TYPE(O) == &PdataType)
169 static PyObject *
170 Pdata_New(void)
172 Pdata *self;
174 if (!(self = PyObject_New(Pdata, &PdataType)))
175 return NULL;
176 self->size = 8;
177 self->length = 0;
178 self->data = malloc(self->size * sizeof(PyObject*));
179 if (self->data)
180 return (PyObject*)self;
181 Py_DECREF(self);
182 return PyErr_NoMemory();
185 static int
186 stackUnderflow(void)
188 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
189 return -1;
192 /* Retain only the initial clearto items. If clearto >= the current
193 * number of items, this is a (non-erroneous) NOP.
195 static int
196 Pdata_clear(Pdata *self, int clearto)
198 int i;
199 PyObject **p;
201 if (clearto < 0) return stackUnderflow();
202 if (clearto >= self->length) return 0;
204 for (i = self->length, p = self->data + clearto;
205 --i >= clearto;
206 p++) {
207 Py_CLEAR(*p);
209 self->length = clearto;
211 return 0;
214 static int
215 Pdata_grow(Pdata *self)
217 int bigger;
218 size_t nbytes;
219 PyObject **tmp;
221 bigger = self->size << 1;
222 if (bigger <= 0) /* was 0, or new value overflows */
223 goto nomemory;
224 if ((int)(size_t)bigger != bigger)
225 goto nomemory;
226 nbytes = (size_t)bigger * sizeof(PyObject *);
227 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
228 goto nomemory;
229 tmp = realloc(self->data, nbytes);
230 if (tmp == NULL)
231 goto nomemory;
232 self->data = tmp;
233 self->size = bigger;
234 return 0;
236 nomemory:
237 PyErr_NoMemory();
238 return -1;
241 /* D is a Pdata*. Pop the topmost element and store it into V, which
242 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
243 * is raised and V is set to NULL. D and V may be evaluated several times.
245 #define PDATA_POP(D, V) { \
246 if ((D)->length) \
247 (V) = (D)->data[--((D)->length)]; \
248 else { \
249 PyErr_SetString(UnpicklingError, "bad pickle data"); \
250 (V) = NULL; \
254 /* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
255 * D. If the Pdata stack can't be grown to hold the new value, both
256 * raise MemoryError and execute "return ER". The difference is in ownership
257 * of O after: _PUSH transfers ownership of O from the caller to the stack
258 * (no incref of O is done, and in case of error O is decrefed), while
259 * _APPEND pushes a new reference.
262 /* Push O on stack D, giving ownership of O to the stack. */
263 #define PDATA_PUSH(D, O, ER) { \
264 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
265 Pdata_grow((Pdata*)(D)) < 0) { \
266 Py_DECREF(O); \
267 return ER; \
269 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
272 /* Push O on stack D, pushing a new reference. */
273 #define PDATA_APPEND(D, O, ER) { \
274 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
275 Pdata_grow((Pdata*)(D)) < 0) \
276 return ER; \
277 Py_INCREF(O); \
278 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
282 static PyObject *
283 Pdata_popTuple(Pdata *self, int start)
285 PyObject *r;
286 int i, j, l;
288 l = self->length-start;
289 r = PyTuple_New(l);
290 if (r == NULL)
291 return NULL;
292 for (i = start, j = 0 ; j < l; i++, j++)
293 PyTuple_SET_ITEM(r, j, self->data[i]);
295 self->length = start;
296 return r;
299 static PyObject *
300 Pdata_popList(Pdata *self, int start)
302 PyObject *r;
303 int i, j, l;
305 l=self->length-start;
306 if (!( r=PyList_New(l))) return NULL;
307 for (i=start, j=0 ; j < l; i++, j++)
308 PyList_SET_ITEM(r, j, self->data[i]);
310 self->length=start;
311 return r;
314 /*************************************************************************/
316 #define ARG_TUP(self, o) { \
317 if (self->arg || (self->arg=PyTuple_New(1))) { \
318 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
319 PyTuple_SET_ITEM(self->arg,0,o); \
321 else { \
322 Py_DECREF(o); \
326 #define FREE_ARG_TUP(self) { \
327 if (Py_REFCNT(self->arg) > 1) { \
328 Py_DECREF(self->arg); \
329 self->arg=NULL; \
333 typedef struct Picklerobject {
334 PyObject_HEAD
335 FILE *fp;
336 PyObject *write;
337 PyObject *file;
338 PyObject *memo;
339 PyObject *arg;
340 PyObject *pers_func;
341 PyObject *inst_pers_func;
343 /* pickle protocol number, >= 0 */
344 int proto;
346 /* bool, true if proto > 0 */
347 int bin;
349 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
350 int (*write_func)(struct Picklerobject *, const char *, Py_ssize_t);
351 char *write_buf;
352 int buf_size;
353 PyObject *dispatch_table;
354 int fast_container; /* count nested container dumps */
355 PyObject *fast_memo;
356 } Picklerobject;
358 #ifndef PY_CPICKLE_FAST_LIMIT
359 #define PY_CPICKLE_FAST_LIMIT 50
360 #endif
362 static PyTypeObject Picklertype;
364 typedef struct Unpicklerobject {
365 PyObject_HEAD
366 FILE *fp;
367 PyObject *file;
368 PyObject *readline;
369 PyObject *read;
370 PyObject *memo;
371 PyObject *arg;
372 Pdata *stack;
373 PyObject *mark;
374 PyObject *pers_func;
375 PyObject *last_string;
376 int *marks;
377 int num_marks;
378 int marks_size;
379 Py_ssize_t (*read_func)(struct Unpicklerobject *, char **, Py_ssize_t);
380 Py_ssize_t (*readline_func)(struct Unpicklerobject *, char **);
381 int buf_size;
382 char *buf;
383 PyObject *find_class;
384 } Unpicklerobject;
386 static PyTypeObject Unpicklertype;
388 /* Forward decls that need the above structs */
389 static int save(Picklerobject *, PyObject *, int);
390 static int put2(Picklerobject *, PyObject *);
392 static
393 PyObject *
394 cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
396 va_list va;
397 PyObject *args=0, *retval=0;
398 va_start(va, format);
400 if (format) args = Py_VaBuildValue(format, va);
401 va_end(va);
402 if (format && ! args) return NULL;
403 if (stringformat && !(retval=PyString_FromString(stringformat)))
404 return NULL;
406 if (retval) {
407 if (args) {
408 PyObject *v;
409 v=PyString_Format(retval, args);
410 Py_DECREF(retval);
411 Py_DECREF(args);
412 if (! v) return NULL;
413 retval=v;
416 else
417 if (args) retval=args;
418 else {
419 PyErr_SetObject(ErrType,Py_None);
420 return NULL;
422 PyErr_SetObject(ErrType,retval);
423 Py_DECREF(retval);
424 return NULL;
427 static int
428 write_file(Picklerobject *self, const char *s, Py_ssize_t n)
430 size_t nbyteswritten;
432 if (s == NULL) {
433 return 0;
436 if (n > INT_MAX) {
437 /* String too large */
438 return -1;
441 PyFile_IncUseCount((PyFileObject *)self->file);
442 Py_BEGIN_ALLOW_THREADS
443 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
444 Py_END_ALLOW_THREADS
445 PyFile_DecUseCount((PyFileObject *)self->file);
446 if (nbyteswritten != (size_t)n) {
447 PyErr_SetFromErrno(PyExc_IOError);
448 return -1;
451 return (int)n;
454 static int
455 write_cStringIO(Picklerobject *self, const char *s, Py_ssize_t n)
457 if (s == NULL) {
458 return 0;
461 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
462 return -1;
465 return (int)n;
468 static int
469 write_none(Picklerobject *self, const char *s, Py_ssize_t n)
471 if (s == NULL) return 0;
472 if (n > INT_MAX) return -1;
473 return (int)n;
476 static int
477 write_other(Picklerobject *self, const char *s, Py_ssize_t _n)
479 PyObject *py_str = 0, *junk = 0;
480 int n;
482 if (_n > INT_MAX)
483 return -1;
484 n = (int)_n;
485 if (s == NULL) {
486 if (!( self->buf_size )) return 0;
487 py_str = PyString_FromStringAndSize(self->write_buf,
488 self->buf_size);
489 if (!py_str)
490 return -1;
492 else {
493 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
494 if (write_other(self, NULL, 0) < 0)
495 return -1;
498 if (n > WRITE_BUF_SIZE) {
499 if (!( py_str =
500 PyString_FromStringAndSize(s, n)))
501 return -1;
503 else {
504 memcpy(self->write_buf + self->buf_size, s, n);
505 self->buf_size += n;
506 return n;
510 if (self->write) {
511 /* object with write method */
512 ARG_TUP(self, py_str);
513 if (self->arg) {
514 junk = PyObject_Call(self->write, self->arg, NULL);
515 FREE_ARG_TUP(self);
517 if (junk) Py_DECREF(junk);
518 else return -1;
520 else
521 PDATA_PUSH(self->file, py_str, -1);
523 self->buf_size = 0;
524 return n;
528 static Py_ssize_t
529 read_file(Unpicklerobject *self, char **s, Py_ssize_t n)
531 size_t nbytesread;
533 if (self->buf_size == 0) {
534 int size;
536 size = ((n < 32) ? 32 : n);
537 if (!( self->buf = (char *)malloc(size))) {
538 PyErr_NoMemory();
539 return -1;
542 self->buf_size = size;
544 else if (n > self->buf_size) {
545 char *newbuf = (char *)realloc(self->buf, n);
546 if (!newbuf) {
547 PyErr_NoMemory();
548 return -1;
550 self->buf = newbuf;
551 self->buf_size = n;
554 PyFile_IncUseCount((PyFileObject *)self->file);
555 Py_BEGIN_ALLOW_THREADS
556 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
557 Py_END_ALLOW_THREADS
558 PyFile_DecUseCount((PyFileObject *)self->file);
559 if (nbytesread != (size_t)n) {
560 if (feof(self->fp)) {
561 PyErr_SetNone(PyExc_EOFError);
562 return -1;
565 PyErr_SetFromErrno(PyExc_IOError);
566 return -1;
569 *s = self->buf;
571 return n;
575 static Py_ssize_t
576 readline_file(Unpicklerobject *self, char **s)
578 int i;
580 if (self->buf_size == 0) {
581 if (!( self->buf = (char *)malloc(40))) {
582 PyErr_NoMemory();
583 return -1;
585 self->buf_size = 40;
588 i = 0;
589 while (1) {
590 int bigger;
591 char *newbuf;
592 for (; i < (self->buf_size - 1); i++) {
593 if (feof(self->fp) ||
594 (self->buf[i] = getc(self->fp)) == '\n') {
595 self->buf[i + 1] = '\0';
596 *s = self->buf;
597 return i + 1;
600 bigger = self->buf_size << 1;
601 if (bigger <= 0) { /* overflow */
602 PyErr_NoMemory();
603 return -1;
605 newbuf = (char *)realloc(self->buf, bigger);
606 if (!newbuf) {
607 PyErr_NoMemory();
608 return -1;
610 self->buf = newbuf;
611 self->buf_size = bigger;
616 static Py_ssize_t
617 read_cStringIO(Unpicklerobject *self, char **s, Py_ssize_t n)
619 char *ptr;
621 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
622 PyErr_SetNone(PyExc_EOFError);
623 return -1;
626 *s = ptr;
628 return n;
632 static Py_ssize_t
633 readline_cStringIO(Unpicklerobject *self, char **s)
635 Py_ssize_t n;
636 char *ptr;
638 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
639 return -1;
642 *s = ptr;
644 return n;
648 static Py_ssize_t
649 read_other(Unpicklerobject *self, char **s, Py_ssize_t n)
651 PyObject *bytes, *str=0;
653 if (!( bytes = PyInt_FromSsize_t(n))) return -1;
655 ARG_TUP(self, bytes);
656 if (self->arg) {
657 str = PyObject_Call(self->read, self->arg, NULL);
658 FREE_ARG_TUP(self);
660 if (! str) return -1;
662 Py_XDECREF(self->last_string);
663 self->last_string = str;
665 if (! (*s = PyString_AsString(str))) return -1;
666 return n;
670 static Py_ssize_t
671 readline_other(Unpicklerobject *self, char **s)
673 PyObject *str;
674 Py_ssize_t str_size;
676 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
677 return -1;
680 if ((str_size = PyString_Size(str)) < 0)
681 return -1;
683 Py_XDECREF(self->last_string);
684 self->last_string = str;
686 if (! (*s = PyString_AsString(str)))
687 return -1;
689 return str_size;
692 /* Copy the first n bytes from s into newly malloc'ed memory, plus a
693 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
694 * The caller is responsible for free()'ing the return value.
696 static char *
697 pystrndup(const char *s, int n)
699 char *r = (char *)malloc(n+1);
700 if (r == NULL)
701 return (char*)PyErr_NoMemory();
702 memcpy(r, s, n);
703 r[n] = 0;
704 return r;
708 static int
709 get(Picklerobject *self, PyObject *id)
711 PyObject *value, *mv;
712 long c_value;
713 char s[30];
714 size_t len;
716 if (!( mv = PyDict_GetItem(self->memo, id))) {
717 PyErr_SetObject(PyExc_KeyError, id);
718 return -1;
721 if (!( value = PyTuple_GetItem(mv, 0)))
722 return -1;
724 if (!( PyInt_Check(value))) {
725 PyErr_SetString(PicklingError, "no int where int expected in memo");
726 return -1;
728 c_value = PyInt_AS_LONG((PyIntObject*)value);
730 if (!self->bin) {
731 s[0] = GET;
732 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
733 len = strlen(s);
735 else if (Pdata_Check(self->file)) {
736 if (write_other(self, NULL, 0) < 0) return -1;
737 PDATA_APPEND(self->file, mv, -1);
738 return 0;
740 else {
741 if (c_value < 256) {
742 s[0] = BINGET;
743 s[1] = (int)(c_value & 0xff);
744 len = 2;
746 else {
747 s[0] = LONG_BINGET;
748 s[1] = (int)(c_value & 0xff);
749 s[2] = (int)((c_value >> 8) & 0xff);
750 s[3] = (int)((c_value >> 16) & 0xff);
751 s[4] = (int)((c_value >> 24) & 0xff);
752 len = 5;
756 if (self->write_func(self, s, len) < 0)
757 return -1;
759 return 0;
763 static int
764 put(Picklerobject *self, PyObject *ob)
766 if (Py_REFCNT(ob) < 2 || self->fast)
767 return 0;
769 return put2(self, ob);
773 static int
774 put2(Picklerobject *self, PyObject *ob)
776 char c_str[30];
777 int p;
778 size_t len;
779 int res = -1;
780 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
782 if (self->fast)
783 return 0;
785 if ((p = PyDict_Size(self->memo)) < 0)
786 goto finally;
788 /* Make sure memo keys are positive! */
789 /* XXX Why?
790 * XXX And does "positive" really mean non-negative?
791 * XXX pickle.py starts with PUT index 0, not 1. This makes for
792 * XXX gratuitous differences between the pickling modules.
794 p++;
796 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
797 goto finally;
799 if (!( memo_len = PyInt_FromLong(p)))
800 goto finally;
802 if (!( t = PyTuple_New(2)))
803 goto finally;
805 PyTuple_SET_ITEM(t, 0, memo_len);
806 Py_INCREF(memo_len);
807 PyTuple_SET_ITEM(t, 1, ob);
808 Py_INCREF(ob);
810 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
811 goto finally;
813 if (!self->bin) {
814 c_str[0] = PUT;
815 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
816 len = strlen(c_str);
818 else if (Pdata_Check(self->file)) {
819 if (write_other(self, NULL, 0) < 0) return -1;
820 PDATA_APPEND(self->file, memo_len, -1);
821 res=0; /* Job well done ;) */
822 goto finally;
824 else {
825 if (p >= 256) {
826 c_str[0] = LONG_BINPUT;
827 c_str[1] = (int)(p & 0xff);
828 c_str[2] = (int)((p >> 8) & 0xff);
829 c_str[3] = (int)((p >> 16) & 0xff);
830 c_str[4] = (int)((p >> 24) & 0xff);
831 len = 5;
833 else {
834 c_str[0] = BINPUT;
835 c_str[1] = p;
836 len = 2;
840 if (self->write_func(self, c_str, len) < 0)
841 goto finally;
843 res = 0;
845 finally:
846 Py_XDECREF(py_ob_id);
847 Py_XDECREF(memo_len);
848 Py_XDECREF(t);
850 return res;
853 static PyObject *
854 whichmodule(PyObject *global, PyObject *global_name)
856 Py_ssize_t i, j;
857 PyObject *module = 0, *modules_dict = 0,
858 *global_name_attr = 0, *name = 0;
860 module = PyObject_GetAttrString(global, "__module__");
861 if (module)
862 return module;
863 if (PyErr_ExceptionMatches(PyExc_AttributeError))
864 PyErr_Clear();
865 else
866 return NULL;
868 if (!( modules_dict = PySys_GetObject("modules")))
869 return NULL;
871 i = 0;
872 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
874 if (PyObject_Compare(name, __main___str)==0) continue;
876 global_name_attr = PyObject_GetAttr(module, global_name);
877 if (!global_name_attr) {
878 if (PyErr_ExceptionMatches(PyExc_AttributeError))
879 PyErr_Clear();
880 else
881 return NULL;
882 continue;
885 if (global_name_attr != global) {
886 Py_DECREF(global_name_attr);
887 continue;
890 Py_DECREF(global_name_attr);
892 break;
895 /* The following implements the rule in pickle.py added in 1.5
896 that used __main__ if no module is found. I don't actually
897 like this rule. jlf
899 if (!j) {
900 j=1;
901 name=__main___str;
904 Py_INCREF(name);
905 return name;
909 static int
910 fast_save_enter(Picklerobject *self, PyObject *obj)
912 /* if fast_container < 0, we're doing an error exit. */
913 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
914 PyObject *key = NULL;
915 if (self->fast_memo == NULL) {
916 self->fast_memo = PyDict_New();
917 if (self->fast_memo == NULL) {
918 self->fast_container = -1;
919 return 0;
922 key = PyLong_FromVoidPtr(obj);
923 if (key == NULL)
924 return 0;
925 if (PyDict_GetItem(self->fast_memo, key)) {
926 Py_DECREF(key);
927 PyErr_Format(PyExc_ValueError,
928 "fast mode: can't pickle cyclic objects "
929 "including object type %s at %p",
930 Py_TYPE(obj)->tp_name, obj);
931 self->fast_container = -1;
932 return 0;
934 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
935 Py_DECREF(key);
936 self->fast_container = -1;
937 return 0;
939 Py_DECREF(key);
941 return 1;
945 fast_save_leave(Picklerobject *self, PyObject *obj)
947 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
948 PyObject *key = PyLong_FromVoidPtr(obj);
949 if (key == NULL)
950 return 0;
951 if (PyDict_DelItem(self->fast_memo, key) < 0) {
952 Py_DECREF(key);
953 return 0;
955 Py_DECREF(key);
957 return 1;
960 static int
961 save_none(Picklerobject *self, PyObject *args)
963 static char none = NONE;
964 if (self->write_func(self, &none, 1) < 0)
965 return -1;
967 return 0;
970 static int
971 save_bool(Picklerobject *self, PyObject *args)
973 static const char *buf[2] = {FALSE, TRUE};
974 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
975 long l = PyInt_AS_LONG((PyIntObject *)args);
977 if (self->proto >= 2) {
978 char opcode = l ? NEWTRUE : NEWFALSE;
979 if (self->write_func(self, &opcode, 1) < 0)
980 return -1;
982 else if (self->write_func(self, buf[l], len[l]) < 0)
983 return -1;
984 return 0;
987 static int
988 save_int(Picklerobject *self, PyObject *args)
990 char c_str[32];
991 long l = PyInt_AS_LONG((PyIntObject *)args);
992 int len = 0;
994 if (!self->bin
995 #if SIZEOF_LONG > 4
996 || l > 0x7fffffffL
997 || l < -0x80000000L
998 #endif
1000 /* Text-mode pickle, or long too big to fit in the 4-byte
1001 * signed BININT format: store as a string.
1003 c_str[0] = INT;
1004 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
1005 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1006 return -1;
1008 else {
1009 /* Binary pickle and l fits in a signed 4-byte int. */
1010 c_str[1] = (int)( l & 0xff);
1011 c_str[2] = (int)((l >> 8) & 0xff);
1012 c_str[3] = (int)((l >> 16) & 0xff);
1013 c_str[4] = (int)((l >> 24) & 0xff);
1015 if ((c_str[4] == 0) && (c_str[3] == 0)) {
1016 if (c_str[2] == 0) {
1017 c_str[0] = BININT1;
1018 len = 2;
1020 else {
1021 c_str[0] = BININT2;
1022 len = 3;
1025 else {
1026 c_str[0] = BININT;
1027 len = 5;
1030 if (self->write_func(self, c_str, len) < 0)
1031 return -1;
1034 return 0;
1038 static int
1039 save_long(Picklerobject *self, PyObject *args)
1041 Py_ssize_t size;
1042 int res = -1;
1043 PyObject *repr = NULL;
1045 static char l = LONG;
1047 if (self->proto >= 2) {
1048 /* Linear-time pickling. */
1049 size_t nbits;
1050 size_t nbytes;
1051 unsigned char *pdata;
1052 char c_str[5];
1053 int i;
1054 int sign = _PyLong_Sign(args);
1056 if (sign == 0) {
1057 /* It's 0 -- an empty bytestring. */
1058 c_str[0] = LONG1;
1059 c_str[1] = 0;
1060 i = self->write_func(self, c_str, 2);
1061 if (i < 0) goto finally;
1062 res = 0;
1063 goto finally;
1065 nbits = _PyLong_NumBits(args);
1066 if (nbits == (size_t)-1 && PyErr_Occurred())
1067 goto finally;
1068 /* How many bytes do we need? There are nbits >> 3 full
1069 * bytes of data, and nbits & 7 leftover bits. If there
1070 * are any leftover bits, then we clearly need another
1071 * byte. Wnat's not so obvious is that we *probably*
1072 * need another byte even if there aren't any leftovers:
1073 * the most-significant bit of the most-significant byte
1074 * acts like a sign bit, and it's usually got a sense
1075 * opposite of the one we need. The exception is longs
1076 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1077 * its own 256's-complement, so has the right sign bit
1078 * even without the extra byte. That's a pain to check
1079 * for in advance, though, so we always grab an extra
1080 * byte at the start, and cut it back later if possible.
1082 nbytes = (nbits >> 3) + 1;
1083 if (nbytes > INT_MAX) {
1084 PyErr_SetString(PyExc_OverflowError, "long too large "
1085 "to pickle");
1086 goto finally;
1088 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1089 if (repr == NULL) goto finally;
1090 pdata = (unsigned char *)PyString_AS_STRING(repr);
1091 i = _PyLong_AsByteArray((PyLongObject *)args,
1092 pdata, nbytes,
1093 1 /* little endian */, 1 /* signed */);
1094 if (i < 0) goto finally;
1095 /* If the long is negative, this may be a byte more than
1096 * needed. This is so iff the MSB is all redundant sign
1097 * bits.
1099 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1100 (pdata[nbytes - 2] & 0x80) != 0)
1101 --nbytes;
1103 if (nbytes < 256) {
1104 c_str[0] = LONG1;
1105 c_str[1] = (char)nbytes;
1106 size = 2;
1108 else {
1109 c_str[0] = LONG4;
1110 size = (int)nbytes;
1111 for (i = 1; i < 5; i++) {
1112 c_str[i] = (char)(size & 0xff);
1113 size >>= 8;
1115 size = 5;
1117 i = self->write_func(self, c_str, size);
1118 if (i < 0) goto finally;
1119 i = self->write_func(self, (char *)pdata, (int)nbytes);
1120 if (i < 0) goto finally;
1121 res = 0;
1122 goto finally;
1125 /* proto < 2: write the repr and newline. This is quadratic-time
1126 * (in the number of digits), in both directions.
1128 if (!( repr = PyObject_Repr(args)))
1129 goto finally;
1131 if ((size = PyString_Size(repr)) < 0)
1132 goto finally;
1134 if (self->write_func(self, &l, 1) < 0)
1135 goto finally;
1137 if (self->write_func(self,
1138 PyString_AS_STRING((PyStringObject *)repr),
1139 size) < 0)
1140 goto finally;
1142 if (self->write_func(self, "\n", 1) < 0)
1143 goto finally;
1145 res = 0;
1147 finally:
1148 Py_XDECREF(repr);
1149 return res;
1153 static int
1154 save_float(Picklerobject *self, PyObject *args)
1156 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
1158 if (self->bin) {
1159 char str[9];
1160 str[0] = BINFLOAT;
1161 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
1162 return -1;
1163 if (self->write_func(self, str, 9) < 0)
1164 return -1;
1166 else {
1167 char c_str[250];
1168 c_str[0] = FLOAT;
1169 PyOS_ascii_formatd(c_str + 1, sizeof(c_str) - 2, "%.17g", x);
1170 /* Extend the formatted string with a newline character */
1171 strcat(c_str, "\n");
1173 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1174 return -1;
1177 return 0;
1181 static int
1182 save_string(Picklerobject *self, PyObject *args, int doput)
1184 int size, len;
1185 PyObject *repr=0;
1187 if ((size = PyString_Size(args)) < 0)
1188 return -1;
1190 if (!self->bin) {
1191 char *repr_str;
1193 static char string = STRING;
1195 if (!( repr = PyObject_Repr(args)))
1196 return -1;
1198 if ((len = PyString_Size(repr)) < 0)
1199 goto err;
1200 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1202 if (self->write_func(self, &string, 1) < 0)
1203 goto err;
1205 if (self->write_func(self, repr_str, len) < 0)
1206 goto err;
1208 if (self->write_func(self, "\n", 1) < 0)
1209 goto err;
1211 Py_XDECREF(repr);
1213 else {
1214 int i;
1215 char c_str[5];
1217 if ((size = PyString_Size(args)) < 0)
1218 return -1;
1220 if (size < 256) {
1221 c_str[0] = SHORT_BINSTRING;
1222 c_str[1] = size;
1223 len = 2;
1225 else if (size <= INT_MAX) {
1226 c_str[0] = BINSTRING;
1227 for (i = 1; i < 5; i++)
1228 c_str[i] = (int)(size >> ((i - 1) * 8));
1229 len = 5;
1231 else
1232 return -1; /* string too large */
1234 if (self->write_func(self, c_str, len) < 0)
1235 return -1;
1237 if (size > 128 && Pdata_Check(self->file)) {
1238 if (write_other(self, NULL, 0) < 0) return -1;
1239 PDATA_APPEND(self->file, args, -1);
1241 else {
1242 if (self->write_func(self,
1243 PyString_AS_STRING(
1244 (PyStringObject *)args),
1245 size) < 0)
1246 return -1;
1250 if (doput)
1251 if (put(self, args) < 0)
1252 return -1;
1254 return 0;
1256 err:
1257 Py_XDECREF(repr);
1258 return -1;
1262 #ifdef Py_USING_UNICODE
1263 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1264 backslash and newline characters to \uXXXX escapes. */
1265 static PyObject *
1266 modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
1268 PyObject *repr;
1269 char *p;
1270 char *q;
1272 static const char *hexdigit = "0123456789abcdef";
1273 #ifdef Py_UNICODE_WIDE
1274 const Py_ssize_t expandsize = 10;
1275 #else
1276 const Py_ssize_t expandsize = 6;
1277 #endif
1279 if (size > PY_SSIZE_T_MAX / expandsize)
1280 return PyErr_NoMemory();
1282 repr = PyString_FromStringAndSize(NULL, expandsize * size);
1283 if (repr == NULL)
1284 return NULL;
1285 if (size == 0)
1286 return repr;
1288 p = q = PyString_AS_STRING(repr);
1289 while (size-- > 0) {
1290 Py_UNICODE ch = *s++;
1291 #ifdef Py_UNICODE_WIDE
1292 /* Map 32-bit characters to '\Uxxxxxxxx' */
1293 if (ch >= 0x10000) {
1294 *p++ = '\\';
1295 *p++ = 'U';
1296 *p++ = hexdigit[(ch >> 28) & 0xf];
1297 *p++ = hexdigit[(ch >> 24) & 0xf];
1298 *p++ = hexdigit[(ch >> 20) & 0xf];
1299 *p++ = hexdigit[(ch >> 16) & 0xf];
1300 *p++ = hexdigit[(ch >> 12) & 0xf];
1301 *p++ = hexdigit[(ch >> 8) & 0xf];
1302 *p++ = hexdigit[(ch >> 4) & 0xf];
1303 *p++ = hexdigit[ch & 15];
1305 else
1306 #else
1307 /* Map UTF-16 surrogate pairs to '\U00xxxxxx' */
1308 if (ch >= 0xD800 && ch < 0xDC00) {
1309 Py_UNICODE ch2;
1310 Py_UCS4 ucs;
1312 ch2 = *s++;
1313 size--;
1314 if (ch2 >= 0xDC00 && ch2 <= 0xDFFF) {
1315 ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF)) + 0x00010000;
1316 *p++ = '\\';
1317 *p++ = 'U';
1318 *p++ = hexdigit[(ucs >> 28) & 0xf];
1319 *p++ = hexdigit[(ucs >> 24) & 0xf];
1320 *p++ = hexdigit[(ucs >> 20) & 0xf];
1321 *p++ = hexdigit[(ucs >> 16) & 0xf];
1322 *p++ = hexdigit[(ucs >> 12) & 0xf];
1323 *p++ = hexdigit[(ucs >> 8) & 0xf];
1324 *p++ = hexdigit[(ucs >> 4) & 0xf];
1325 *p++ = hexdigit[ucs & 0xf];
1326 continue;
1328 /* Fall through: isolated surrogates are copied as-is */
1329 s--;
1330 size++;
1332 #endif
1333 /* Map 16-bit characters to '\uxxxx' */
1334 if (ch >= 256 || ch == '\\' || ch == '\n') {
1335 *p++ = '\\';
1336 *p++ = 'u';
1337 *p++ = hexdigit[(ch >> 12) & 0xf];
1338 *p++ = hexdigit[(ch >> 8) & 0xf];
1339 *p++ = hexdigit[(ch >> 4) & 0xf];
1340 *p++ = hexdigit[ch & 15];
1342 /* Copy everything else as-is */
1343 else
1344 *p++ = (char) ch;
1346 *p = '\0';
1347 _PyString_Resize(&repr, p - q);
1348 return repr;
1351 static int
1352 save_unicode(Picklerobject *self, PyObject *args, int doput)
1354 Py_ssize_t size, len;
1355 PyObject *repr=0;
1357 if (!PyUnicode_Check(args))
1358 return -1;
1360 if (!self->bin) {
1361 char *repr_str;
1362 static char string = UNICODE;
1364 repr = modified_EncodeRawUnicodeEscape(
1365 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1366 if (!repr)
1367 return -1;
1369 if ((len = PyString_Size(repr)) < 0)
1370 goto err;
1371 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1373 if (self->write_func(self, &string, 1) < 0)
1374 goto err;
1376 if (self->write_func(self, repr_str, len) < 0)
1377 goto err;
1379 if (self->write_func(self, "\n", 1) < 0)
1380 goto err;
1382 Py_XDECREF(repr);
1384 else {
1385 int i;
1386 char c_str[5];
1388 if (!( repr = PyUnicode_AsUTF8String(args)))
1389 return -1;
1391 if ((size = PyString_Size(repr)) < 0)
1392 goto err;
1393 if (size > INT_MAX)
1394 return -1; /* string too large */
1396 c_str[0] = BINUNICODE;
1397 for (i = 1; i < 5; i++)
1398 c_str[i] = (int)(size >> ((i - 1) * 8));
1399 len = 5;
1401 if (self->write_func(self, c_str, len) < 0)
1402 goto err;
1404 if (size > 128 && Pdata_Check(self->file)) {
1405 if (write_other(self, NULL, 0) < 0)
1406 goto err;
1407 PDATA_APPEND(self->file, repr, -1);
1409 else {
1410 if (self->write_func(self, PyString_AS_STRING(repr),
1411 size) < 0)
1412 goto err;
1415 Py_DECREF(repr);
1418 if (doput)
1419 if (put(self, args) < 0)
1420 return -1;
1422 return 0;
1424 err:
1425 Py_XDECREF(repr);
1426 return -1;
1428 #endif
1430 /* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1431 static int
1432 store_tuple_elements(Picklerobject *self, PyObject *t, int len)
1434 int i;
1435 int res = -1; /* guilty until proved innocent */
1437 assert(PyTuple_Size(t) == len);
1439 for (i = 0; i < len; i++) {
1440 PyObject *element = PyTuple_GET_ITEM(t, i);
1442 if (element == NULL)
1443 goto finally;
1444 if (save(self, element, 0) < 0)
1445 goto finally;
1447 res = 0;
1449 finally:
1450 return res;
1453 /* Tuples are ubiquitous in the pickle protocols, so many techniques are
1454 * used across protocols to minimize the space needed to pickle them.
1455 * Tuples are also the only builtin immutable type that can be recursive
1456 * (a tuple can be reached from itself), and that requires some subtle
1457 * magic so that it works in all cases. IOW, this is a long routine.
1459 static int
1460 save_tuple(Picklerobject *self, PyObject *args)
1462 PyObject *py_tuple_id = NULL;
1463 int len, i;
1464 int res = -1;
1466 static char tuple = TUPLE;
1467 static char pop = POP;
1468 static char pop_mark = POP_MARK;
1469 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
1471 if ((len = PyTuple_Size(args)) < 0)
1472 goto finally;
1474 if (len == 0) {
1475 char c_str[2];
1477 if (self->proto) {
1478 c_str[0] = EMPTY_TUPLE;
1479 len = 1;
1481 else {
1482 c_str[0] = MARK;
1483 c_str[1] = TUPLE;
1484 len = 2;
1486 if (self->write_func(self, c_str, len) >= 0)
1487 res = 0;
1488 /* Don't memoize an empty tuple. */
1489 goto finally;
1492 /* A non-empty tuple. */
1494 /* id(tuple) isn't in the memo now. If it shows up there after
1495 * saving the tuple elements, the tuple must be recursive, in
1496 * which case we'll pop everything we put on the stack, and fetch
1497 * its value from the memo.
1499 py_tuple_id = PyLong_FromVoidPtr(args);
1500 if (py_tuple_id == NULL)
1501 goto finally;
1503 if (len <= 3 && self->proto >= 2) {
1504 /* Use TUPLE{1,2,3} opcodes. */
1505 if (store_tuple_elements(self, args, len) < 0)
1506 goto finally;
1507 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1508 /* pop the len elements */
1509 for (i = 0; i < len; ++i)
1510 if (self->write_func(self, &pop, 1) < 0)
1511 goto finally;
1512 /* fetch from memo */
1513 if (get(self, py_tuple_id) < 0)
1514 goto finally;
1515 res = 0;
1516 goto finally;
1518 /* Not recursive. */
1519 if (self->write_func(self, len2opcode + len, 1) < 0)
1520 goto finally;
1521 goto memoize;
1524 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1525 * Generate MARK elt1 elt2 ... TUPLE
1527 if (self->write_func(self, &MARKv, 1) < 0)
1528 goto finally;
1530 if (store_tuple_elements(self, args, len) < 0)
1531 goto finally;
1533 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1534 /* pop the stack stuff we pushed */
1535 if (self->bin) {
1536 if (self->write_func(self, &pop_mark, 1) < 0)
1537 goto finally;
1539 else {
1540 /* Note that we pop one more than len, to remove
1541 * the MARK too.
1543 for (i = 0; i <= len; i++)
1544 if (self->write_func(self, &pop, 1) < 0)
1545 goto finally;
1547 /* fetch from memo */
1548 if (get(self, py_tuple_id) >= 0)
1549 res = 0;
1550 goto finally;
1553 /* Not recursive. */
1554 if (self->write_func(self, &tuple, 1) < 0)
1555 goto finally;
1557 memoize:
1558 if (put(self, args) >= 0)
1559 res = 0;
1561 finally:
1562 Py_XDECREF(py_tuple_id);
1563 return res;
1566 /* iter is an iterator giving items, and we batch up chunks of
1567 * MARK item item ... item APPENDS
1568 * opcode sequences. Calling code should have arranged to first create an
1569 * empty list, or list-like object, for the APPENDS to operate on.
1570 * Returns 0 on success, <0 on error.
1572 static int
1573 batch_list(Picklerobject *self, PyObject *iter)
1575 PyObject *obj = NULL;
1576 PyObject *firstitem = NULL;
1577 int i, n;
1579 static char append = APPEND;
1580 static char appends = APPENDS;
1582 assert(iter != NULL);
1584 if (self->proto == 0) {
1585 /* APPENDS isn't available; do one at a time. */
1586 for (;;) {
1587 obj = PyIter_Next(iter);
1588 if (obj == NULL) {
1589 if (PyErr_Occurred())
1590 return -1;
1591 break;
1593 i = save(self, obj, 0);
1594 Py_DECREF(obj);
1595 if (i < 0)
1596 return -1;
1597 if (self->write_func(self, &append, 1) < 0)
1598 return -1;
1600 return 0;
1603 /* proto > 0: write in batches of BATCHSIZE. */
1604 do {
1605 /* Get first item */
1606 firstitem = PyIter_Next(iter);
1607 if (firstitem == NULL) {
1608 if (PyErr_Occurred())
1609 goto BatchFailed;
1611 /* nothing more to add */
1612 break;
1615 /* Try to get a second item */
1616 obj = PyIter_Next(iter);
1617 if (obj == NULL) {
1618 if (PyErr_Occurred())
1619 goto BatchFailed;
1621 /* Only one item to write */
1622 if (save(self, firstitem, 0) < 0)
1623 goto BatchFailed;
1624 if (self->write_func(self, &append, 1) < 0)
1625 goto BatchFailed;
1626 Py_CLEAR(firstitem);
1627 break;
1630 /* More than one item to write */
1632 /* Pump out MARK, items, APPENDS. */
1633 if (self->write_func(self, &MARKv, 1) < 0)
1634 goto BatchFailed;
1636 if (save(self, firstitem, 0) < 0)
1637 goto BatchFailed;
1638 Py_CLEAR(firstitem);
1639 n = 1;
1641 /* Fetch and save up to BATCHSIZE items */
1642 while (obj) {
1643 if (save(self, obj, 0) < 0)
1644 goto BatchFailed;
1645 Py_CLEAR(obj);
1646 n += 1;
1648 if (n == BATCHSIZE)
1649 break;
1651 obj = PyIter_Next(iter);
1652 if (obj == NULL) {
1653 if (PyErr_Occurred())
1654 goto BatchFailed;
1655 break;
1659 if (self->write_func(self, &appends, 1) < 0)
1660 goto BatchFailed;
1662 } while (n == BATCHSIZE);
1663 return 0;
1665 BatchFailed:
1666 Py_XDECREF(firstitem);
1667 Py_XDECREF(obj);
1668 return -1;
1671 static int
1672 save_list(Picklerobject *self, PyObject *args)
1674 int res = -1;
1675 char s[3];
1676 int len;
1677 PyObject *iter;
1679 if (self->fast && !fast_save_enter(self, args))
1680 goto finally;
1682 /* Create an empty list. */
1683 if (self->bin) {
1684 s[0] = EMPTY_LIST;
1685 len = 1;
1687 else {
1688 s[0] = MARK;
1689 s[1] = LIST;
1690 len = 2;
1693 if (self->write_func(self, s, len) < 0)
1694 goto finally;
1696 /* Get list length, and bow out early if empty. */
1697 if ((len = PyList_Size(args)) < 0)
1698 goto finally;
1700 /* Memoize. */
1701 if (len == 0) {
1702 if (put(self, args) >= 0)
1703 res = 0;
1704 goto finally;
1706 if (put2(self, args) < 0)
1707 goto finally;
1709 /* Materialize the list elements. */
1710 iter = PyObject_GetIter(args);
1711 if (iter == NULL)
1712 goto finally;
1714 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1716 res = batch_list(self, iter);
1717 Py_LeaveRecursiveCall();
1719 Py_DECREF(iter);
1721 finally:
1722 if (self->fast && !fast_save_leave(self, args))
1723 res = -1;
1725 return res;
1729 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1730 * MARK key value ... key value SETITEMS
1731 * opcode sequences. Calling code should have arranged to first create an
1732 * empty dict, or dict-like object, for the SETITEMS to operate on.
1733 * Returns 0 on success, <0 on error.
1735 * This is very much like batch_list(). The difference between saving
1736 * elements directly, and picking apart two-tuples, is so long-winded at
1737 * the C level, though, that attempts to combine these routines were too
1738 * ugly to bear.
1740 static int
1741 batch_dict(Picklerobject *self, PyObject *iter)
1743 PyObject *p = NULL;
1744 PyObject *firstitem = NULL;
1745 int i, n;
1747 static char setitem = SETITEM;
1748 static char setitems = SETITEMS;
1750 assert(iter != NULL);
1752 if (self->proto == 0) {
1753 /* SETITEMS isn't available; do one at a time. */
1754 for (;;) {
1755 p = PyIter_Next(iter);
1756 if (p == NULL) {
1757 if (PyErr_Occurred())
1758 return -1;
1759 break;
1761 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1762 PyErr_SetString(PyExc_TypeError, "dict items "
1763 "iterator must return 2-tuples");
1764 return -1;
1766 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1767 if (i >= 0)
1768 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1769 Py_DECREF(p);
1770 if (i < 0)
1771 return -1;
1772 if (self->write_func(self, &setitem, 1) < 0)
1773 return -1;
1775 return 0;
1778 /* proto > 0: write in batches of BATCHSIZE. */
1779 do {
1780 /* Get first item */
1781 firstitem = PyIter_Next(iter);
1782 if (firstitem == NULL) {
1783 if (PyErr_Occurred())
1784 goto BatchFailed;
1786 /* nothing more to add */
1787 break;
1789 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
1790 PyErr_SetString(PyExc_TypeError, "dict items "
1791 "iterator must return 2-tuples");
1792 goto BatchFailed;
1795 /* Try to get a second item */
1796 p = PyIter_Next(iter);
1797 if (p == NULL) {
1798 if (PyErr_Occurred())
1799 goto BatchFailed;
1801 /* Only one item to write */
1802 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1803 goto BatchFailed;
1804 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1805 goto BatchFailed;
1806 if (self->write_func(self, &setitem, 1) < 0)
1807 goto BatchFailed;
1808 Py_CLEAR(firstitem);
1809 break;
1812 /* More than one item to write */
1814 /* Pump out MARK, items, SETITEMS. */
1815 if (self->write_func(self, &MARKv, 1) < 0)
1816 goto BatchFailed;
1818 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
1819 goto BatchFailed;
1820 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
1821 goto BatchFailed;
1822 Py_CLEAR(firstitem);
1823 n = 1;
1825 /* Fetch and save up to BATCHSIZE items */
1826 while (p) {
1827 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1828 PyErr_SetString(PyExc_TypeError, "dict items "
1829 "iterator must return 2-tuples");
1830 goto BatchFailed;
1832 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1833 goto BatchFailed;
1834 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1835 goto BatchFailed;
1836 Py_CLEAR(p);
1837 n += 1;
1839 if (n == BATCHSIZE)
1840 break;
1842 p = PyIter_Next(iter);
1843 if (p == NULL) {
1844 if (PyErr_Occurred())
1845 goto BatchFailed;
1846 break;
1850 if (self->write_func(self, &setitems, 1) < 0)
1851 goto BatchFailed;
1853 } while (n == BATCHSIZE);
1854 return 0;
1856 BatchFailed:
1857 Py_XDECREF(firstitem);
1858 Py_XDECREF(p);
1859 return -1;
1862 static int
1863 save_dict(Picklerobject *self, PyObject *args)
1865 int res = -1;
1866 char s[3];
1867 int len;
1868 PyObject *iter;
1870 if (self->fast && !fast_save_enter(self, args))
1871 goto finally;
1873 /* Create an empty dict. */
1874 if (self->bin) {
1875 s[0] = EMPTY_DICT;
1876 len = 1;
1878 else {
1879 s[0] = MARK;
1880 s[1] = DICT;
1881 len = 2;
1884 if (self->write_func(self, s, len) < 0)
1885 goto finally;
1887 /* Get dict size, and bow out early if empty. */
1888 if ((len = PyDict_Size(args)) < 0)
1889 goto finally;
1891 if (len == 0) {
1892 if (put(self, args) >= 0)
1893 res = 0;
1894 goto finally;
1896 if (put2(self, args) < 0)
1897 goto finally;
1899 /* Materialize the dict items. */
1900 iter = PyObject_CallMethod(args, "iteritems", "()");
1901 if (iter == NULL)
1902 goto finally;
1903 if (Py_EnterRecursiveCall(" while pickling an object") == 0)
1905 res = batch_dict(self, iter);
1906 Py_LeaveRecursiveCall();
1908 Py_DECREF(iter);
1910 finally:
1911 if (self->fast && !fast_save_leave(self, args))
1912 res = -1;
1914 return res;
1918 static int
1919 save_inst(Picklerobject *self, PyObject *args)
1921 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1922 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1923 char *module_str, *name_str;
1924 int module_size, name_size, res = -1;
1926 static char inst = INST, obj = OBJ, build = BUILD;
1928 if (self->fast && !fast_save_enter(self, args))
1929 goto finally;
1931 if (self->write_func(self, &MARKv, 1) < 0)
1932 goto finally;
1934 if (!( class = PyObject_GetAttr(args, __class___str)))
1935 goto finally;
1937 if (self->bin) {
1938 if (save(self, class, 0) < 0)
1939 goto finally;
1942 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1943 PyObject *element = 0;
1944 int i, len;
1946 if (!( class_args =
1947 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
1948 goto finally;
1950 if ((len = PyObject_Size(class_args)) < 0)
1951 goto finally;
1953 for (i = 0; i < len; i++) {
1954 if (!( element = PySequence_GetItem(class_args, i)))
1955 goto finally;
1957 if (save(self, element, 0) < 0) {
1958 Py_DECREF(element);
1959 goto finally;
1962 Py_DECREF(element);
1965 else {
1966 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1967 PyErr_Clear();
1968 else
1969 goto finally;
1972 if (!self->bin) {
1973 if (!( name = ((PyClassObject *)class)->cl_name )) {
1974 PyErr_SetString(PicklingError, "class has no name");
1975 goto finally;
1978 if (!( module = whichmodule(class, name)))
1979 goto finally;
1982 if ((module_size = PyString_Size(module)) < 0 ||
1983 (name_size = PyString_Size(name)) < 0)
1984 goto finally;
1986 module_str = PyString_AS_STRING((PyStringObject *)module);
1987 name_str = PyString_AS_STRING((PyStringObject *)name);
1989 if (self->write_func(self, &inst, 1) < 0)
1990 goto finally;
1992 if (self->write_func(self, module_str, module_size) < 0)
1993 goto finally;
1995 if (self->write_func(self, "\n", 1) < 0)
1996 goto finally;
1998 if (self->write_func(self, name_str, name_size) < 0)
1999 goto finally;
2001 if (self->write_func(self, "\n", 1) < 0)
2002 goto finally;
2004 else if (self->write_func(self, &obj, 1) < 0) {
2005 goto finally;
2008 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
2009 state = PyObject_Call(getstate_func, empty_tuple, NULL);
2010 if (!state)
2011 goto finally;
2013 else {
2014 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2015 PyErr_Clear();
2016 else
2017 goto finally;
2019 if (!( state = PyObject_GetAttr(args, __dict___str))) {
2020 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2021 PyErr_Clear();
2022 else
2023 goto finally;
2024 res = 0;
2025 goto finally;
2029 if (!PyDict_Check(state)) {
2030 if (put2(self, args) < 0)
2031 goto finally;
2033 else {
2034 if (put(self, args) < 0)
2035 goto finally;
2038 if (save(self, state, 0) < 0)
2039 goto finally;
2041 if (self->write_func(self, &build, 1) < 0)
2042 goto finally;
2044 res = 0;
2046 finally:
2047 if (self->fast && !fast_save_leave(self, args))
2048 res = -1;
2050 Py_XDECREF(module);
2051 Py_XDECREF(class);
2052 Py_XDECREF(state);
2053 Py_XDECREF(getinitargs_func);
2054 Py_XDECREF(getstate_func);
2055 Py_XDECREF(class_args);
2057 return res;
2061 static int
2062 save_global(Picklerobject *self, PyObject *args, PyObject *name)
2064 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
2065 char *name_str, *module_str;
2066 int module_size, name_size, res = -1;
2068 static char global = GLOBAL;
2070 if (name) {
2071 global_name = name;
2072 Py_INCREF(global_name);
2074 else {
2075 if (!( global_name = PyObject_GetAttr(args, __name___str)))
2076 goto finally;
2079 if (!( module = whichmodule(args, global_name)))
2080 goto finally;
2082 if ((module_size = PyString_Size(module)) < 0 ||
2083 (name_size = PyString_Size(global_name)) < 0)
2084 goto finally;
2086 module_str = PyString_AS_STRING((PyStringObject *)module);
2087 name_str = PyString_AS_STRING((PyStringObject *)global_name);
2089 /* XXX This can be doing a relative import. Clearly it shouldn't,
2090 but I don't know how to stop it. :-( */
2091 mod = PyImport_ImportModule(module_str);
2092 if (mod == NULL) {
2093 cPickle_ErrFormat(PicklingError,
2094 "Can't pickle %s: import of module %s "
2095 "failed",
2096 "OS", args, module);
2097 goto finally;
2099 klass = PyObject_GetAttrString(mod, name_str);
2100 if (klass == NULL) {
2101 cPickle_ErrFormat(PicklingError,
2102 "Can't pickle %s: attribute lookup %s.%s "
2103 "failed",
2104 "OSS", args, module, global_name);
2105 goto finally;
2107 if (klass != args) {
2108 Py_DECREF(klass);
2109 cPickle_ErrFormat(PicklingError,
2110 "Can't pickle %s: it's not the same object "
2111 "as %s.%s",
2112 "OSS", args, module, global_name);
2113 goto finally;
2115 Py_DECREF(klass);
2117 if (self->proto >= 2) {
2118 /* See whether this is in the extension registry, and if
2119 * so generate an EXT opcode.
2121 PyObject *py_code; /* extension code as Python object */
2122 long code; /* extension code as C value */
2123 char c_str[5];
2124 int n;
2126 PyTuple_SET_ITEM(two_tuple, 0, module);
2127 PyTuple_SET_ITEM(two_tuple, 1, global_name);
2128 py_code = PyDict_GetItem(extension_registry, two_tuple);
2129 if (py_code == NULL)
2130 goto gen_global; /* not registered */
2132 /* Verify py_code has the right type and value. */
2133 if (!PyInt_Check(py_code)) {
2134 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2135 "extension code %s isn't an integer",
2136 "OO", args, py_code);
2137 goto finally;
2139 code = PyInt_AS_LONG(py_code);
2140 if (code <= 0 || code > 0x7fffffffL) {
2141 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2142 "extension code %ld is out of range",
2143 "Ol", args, code);
2144 goto finally;
2147 /* Generate an EXT opcode. */
2148 if (code <= 0xff) {
2149 c_str[0] = EXT1;
2150 c_str[1] = (char)code;
2151 n = 2;
2153 else if (code <= 0xffff) {
2154 c_str[0] = EXT2;
2155 c_str[1] = (char)(code & 0xff);
2156 c_str[2] = (char)((code >> 8) & 0xff);
2157 n = 3;
2159 else {
2160 c_str[0] = EXT4;
2161 c_str[1] = (char)(code & 0xff);
2162 c_str[2] = (char)((code >> 8) & 0xff);
2163 c_str[3] = (char)((code >> 16) & 0xff);
2164 c_str[4] = (char)((code >> 24) & 0xff);
2165 n = 5;
2168 if (self->write_func(self, c_str, n) >= 0)
2169 res = 0;
2170 goto finally; /* and don't memoize */
2173 gen_global:
2174 if (self->write_func(self, &global, 1) < 0)
2175 goto finally;
2177 if (self->write_func(self, module_str, module_size) < 0)
2178 goto finally;
2180 if (self->write_func(self, "\n", 1) < 0)
2181 goto finally;
2183 if (self->write_func(self, name_str, name_size) < 0)
2184 goto finally;
2186 if (self->write_func(self, "\n", 1) < 0)
2187 goto finally;
2189 if (put(self, args) < 0)
2190 goto finally;
2192 res = 0;
2194 finally:
2195 Py_XDECREF(module);
2196 Py_XDECREF(global_name);
2197 Py_XDECREF(mod);
2199 return res;
2202 static int
2203 save_pers(Picklerobject *self, PyObject *args, PyObject *f)
2205 PyObject *pid = 0;
2206 int size, res = -1;
2208 static char persid = PERSID, binpersid = BINPERSID;
2210 Py_INCREF(args);
2211 ARG_TUP(self, args);
2212 if (self->arg) {
2213 pid = PyObject_Call(f, self->arg, NULL);
2214 FREE_ARG_TUP(self);
2216 if (! pid) return -1;
2218 if (pid != Py_None) {
2219 if (!self->bin) {
2220 if (!PyString_Check(pid)) {
2221 PyErr_SetString(PicklingError,
2222 "persistent id must be string");
2223 goto finally;
2226 if (self->write_func(self, &persid, 1) < 0)
2227 goto finally;
2229 if ((size = PyString_Size(pid)) < 0)
2230 goto finally;
2232 if (self->write_func(self,
2233 PyString_AS_STRING(
2234 (PyStringObject *)pid),
2235 size) < 0)
2236 goto finally;
2238 if (self->write_func(self, "\n", 1) < 0)
2239 goto finally;
2241 res = 1;
2242 goto finally;
2244 else if (save(self, pid, 1) >= 0) {
2245 if (self->write_func(self, &binpersid, 1) < 0)
2246 res = -1;
2247 else
2248 res = 1;
2251 goto finally;
2254 res = 0;
2256 finally:
2257 Py_XDECREF(pid);
2259 return res;
2262 /* We're saving ob, and args is the 2-thru-5 tuple returned by the
2263 * appropriate __reduce__ method for ob.
2265 static int
2266 save_reduce(Picklerobject *self, PyObject *args, PyObject *fn, PyObject *ob)
2268 PyObject *callable;
2269 PyObject *argtup;
2270 PyObject *state = NULL;
2271 PyObject *listitems = Py_None;
2272 PyObject *dictitems = Py_None;
2273 Py_ssize_t size;
2275 int use_newobj = self->proto >= 2;
2277 static char reduce = REDUCE;
2278 static char build = BUILD;
2279 static char newobj = NEWOBJ;
2281 size = PyTuple_Size(args);
2282 if (size < 2 || size > 5) {
2283 cPickle_ErrFormat(PicklingError, "tuple returned by "
2284 "%s must contain 2 through 5 elements",
2285 "O", fn);
2286 return -1;
2289 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2290 &callable,
2291 &argtup,
2292 &state,
2293 &listitems,
2294 &dictitems))
2295 return -1;
2297 if (!PyTuple_Check(argtup)) {
2298 cPickle_ErrFormat(PicklingError, "Second element of "
2299 "tuple returned by %s must be a tuple",
2300 "O", fn);
2301 return -1;
2304 if (state == Py_None)
2305 state = NULL;
2307 if (listitems == Py_None)
2308 listitems = NULL;
2309 else if (!PyIter_Check(listitems)) {
2310 cPickle_ErrFormat(PicklingError, "Fourth element of "
2311 "tuple returned by %s must be an iterator, not %s",
2312 "Os", fn, Py_TYPE(listitems)->tp_name);
2313 return -1;
2316 if (dictitems == Py_None)
2317 dictitems = NULL;
2318 else if (!PyIter_Check(dictitems)) {
2319 cPickle_ErrFormat(PicklingError, "Fifth element of "
2320 "tuple returned by %s must be an iterator, not %s",
2321 "Os", fn, Py_TYPE(dictitems)->tp_name);
2322 return -1;
2325 /* Protocol 2 special case: if callable's name is __newobj__, use
2326 * NEWOBJ. This consumes a lot of code.
2328 if (use_newobj) {
2329 PyObject *temp = PyObject_GetAttr(callable, __name___str);
2331 if (temp == NULL) {
2332 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2333 PyErr_Clear();
2334 else
2335 return -1;
2336 use_newobj = 0;
2338 else {
2339 use_newobj = PyString_Check(temp) &&
2340 strcmp(PyString_AS_STRING(temp),
2341 "__newobj__") == 0;
2342 Py_DECREF(temp);
2345 if (use_newobj) {
2346 PyObject *cls;
2347 PyObject *newargtup;
2348 int n, i;
2350 /* Sanity checks. */
2351 n = PyTuple_Size(argtup);
2352 if (n < 1) {
2353 PyErr_SetString(PicklingError, "__newobj__ arglist "
2354 "is empty");
2355 return -1;
2358 cls = PyTuple_GET_ITEM(argtup, 0);
2359 if (! PyObject_HasAttrString(cls, "__new__")) {
2360 PyErr_SetString(PicklingError, "args[0] from "
2361 "__newobj__ args has no __new__");
2362 return -1;
2365 /* XXX How could ob be NULL? */
2366 if (ob != NULL) {
2367 PyObject *ob_dot_class;
2369 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2370 if (ob_dot_class == NULL) {
2371 if (PyErr_ExceptionMatches(
2372 PyExc_AttributeError))
2373 PyErr_Clear();
2374 else
2375 return -1;
2377 i = ob_dot_class != cls; /* true iff a problem */
2378 Py_XDECREF(ob_dot_class);
2379 if (i) {
2380 PyErr_SetString(PicklingError, "args[0] from "
2381 "__newobj__ args has the wrong class");
2382 return -1;
2386 /* Save the class and its __new__ arguments. */
2387 if (save(self, cls, 0) < 0)
2388 return -1;
2390 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2391 if (newargtup == NULL)
2392 return -1;
2393 for (i = 1; i < n; ++i) {
2394 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2395 Py_INCREF(temp);
2396 PyTuple_SET_ITEM(newargtup, i-1, temp);
2398 i = save(self, newargtup, 0);
2399 Py_DECREF(newargtup);
2400 if (i < 0)
2401 return -1;
2403 /* Add NEWOBJ opcode. */
2404 if (self->write_func(self, &newobj, 1) < 0)
2405 return -1;
2407 else {
2408 /* Not using NEWOBJ. */
2409 if (save(self, callable, 0) < 0 ||
2410 save(self, argtup, 0) < 0 ||
2411 self->write_func(self, &reduce, 1) < 0)
2412 return -1;
2415 /* Memoize. */
2416 /* XXX How can ob be NULL? */
2417 if (ob != NULL) {
2418 if (state && !PyDict_Check(state)) {
2419 if (put2(self, ob) < 0)
2420 return -1;
2422 else if (put(self, ob) < 0)
2423 return -1;
2427 if (listitems && batch_list(self, listitems) < 0)
2428 return -1;
2430 if (dictitems && batch_dict(self, dictitems) < 0)
2431 return -1;
2433 if (state) {
2434 if (save(self, state, 0) < 0 ||
2435 self->write_func(self, &build, 1) < 0)
2436 return -1;
2439 return 0;
2442 static int
2443 save(Picklerobject *self, PyObject *args, int pers_save)
2445 PyTypeObject *type;
2446 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2447 int res = -1;
2448 int tmp;
2450 if (Py_EnterRecursiveCall(" while pickling an object"))
2451 return -1;
2453 if (!pers_save && self->pers_func) {
2454 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2455 res = tmp;
2456 goto finally;
2460 if (args == Py_None) {
2461 res = save_none(self, args);
2462 goto finally;
2465 type = Py_TYPE(args);
2467 switch (type->tp_name[0]) {
2468 case 'b':
2469 if (args == Py_False || args == Py_True) {
2470 res = save_bool(self, args);
2471 goto finally;
2473 break;
2474 case 'i':
2475 if (type == &PyInt_Type) {
2476 res = save_int(self, args);
2477 goto finally;
2479 break;
2481 case 'l':
2482 if (type == &PyLong_Type) {
2483 res = save_long(self, args);
2484 goto finally;
2486 break;
2488 case 'f':
2489 if (type == &PyFloat_Type) {
2490 res = save_float(self, args);
2491 goto finally;
2493 break;
2495 case 't':
2496 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2497 res = save_tuple(self, args);
2498 goto finally;
2500 break;
2502 case 's':
2503 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2504 res = save_string(self, args, 0);
2505 goto finally;
2507 break;
2509 #ifdef Py_USING_UNICODE
2510 case 'u':
2511 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2512 res = save_unicode(self, args, 0);
2513 goto finally;
2515 break;
2516 #endif
2519 if (Py_REFCNT(args) > 1) {
2520 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2521 goto finally;
2523 if (PyDict_GetItem(self->memo, py_ob_id)) {
2524 if (get(self, py_ob_id) < 0)
2525 goto finally;
2527 res = 0;
2528 goto finally;
2532 switch (type->tp_name[0]) {
2533 case 's':
2534 if (type == &PyString_Type) {
2535 res = save_string(self, args, 1);
2536 goto finally;
2538 break;
2540 #ifdef Py_USING_UNICODE
2541 case 'u':
2542 if (type == &PyUnicode_Type) {
2543 res = save_unicode(self, args, 1);
2544 goto finally;
2546 break;
2547 #endif
2549 case 't':
2550 if (type == &PyTuple_Type) {
2551 res = save_tuple(self, args);
2552 goto finally;
2554 if (type == &PyType_Type) {
2555 res = save_global(self, args, NULL);
2556 goto finally;
2558 break;
2560 case 'l':
2561 if (type == &PyList_Type) {
2562 res = save_list(self, args);
2563 goto finally;
2565 break;
2567 case 'd':
2568 if (type == &PyDict_Type) {
2569 res = save_dict(self, args);
2570 goto finally;
2572 break;
2574 case 'i':
2575 if (type == &PyInstance_Type) {
2576 res = save_inst(self, args);
2577 goto finally;
2579 break;
2581 case 'c':
2582 if (type == &PyClass_Type) {
2583 res = save_global(self, args, NULL);
2584 goto finally;
2586 break;
2588 case 'f':
2589 if (type == &PyFunction_Type) {
2590 res = save_global(self, args, NULL);
2591 if (res && PyErr_ExceptionMatches(PickleError)) {
2592 /* fall back to reduce */
2593 PyErr_Clear();
2594 break;
2596 goto finally;
2598 break;
2600 case 'b':
2601 if (type == &PyCFunction_Type) {
2602 res = save_global(self, args, NULL);
2603 goto finally;
2607 if (!pers_save && self->inst_pers_func) {
2608 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2609 res = tmp;
2610 goto finally;
2614 if (PyType_IsSubtype(type, &PyType_Type)) {
2615 res = save_global(self, args, NULL);
2616 goto finally;
2619 /* Get a reduction callable, and call it. This may come from
2620 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2621 * or the object's __reduce__ method.
2623 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2624 if (__reduce__ != NULL) {
2625 Py_INCREF(__reduce__);
2626 Py_INCREF(args);
2627 ARG_TUP(self, args);
2628 if (self->arg) {
2629 t = PyObject_Call(__reduce__, self->arg, NULL);
2630 FREE_ARG_TUP(self);
2633 else {
2634 /* Check for a __reduce_ex__ method. */
2635 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2636 if (__reduce__ != NULL) {
2637 t = PyInt_FromLong(self->proto);
2638 if (t != NULL) {
2639 ARG_TUP(self, t);
2640 t = NULL;
2641 if (self->arg) {
2642 t = PyObject_Call(__reduce__,
2643 self->arg, NULL);
2644 FREE_ARG_TUP(self);
2648 else {
2649 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2650 PyErr_Clear();
2651 else
2652 goto finally;
2653 /* Check for a __reduce__ method. */
2654 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2655 if (__reduce__ != NULL) {
2656 t = PyObject_Call(__reduce__,
2657 empty_tuple, NULL);
2659 else {
2660 PyErr_SetObject(UnpickleableError, args);
2661 goto finally;
2666 if (t == NULL)
2667 goto finally;
2669 if (PyString_Check(t)) {
2670 res = save_global(self, args, t);
2671 goto finally;
2674 if (!PyTuple_Check(t)) {
2675 cPickle_ErrFormat(PicklingError, "Value returned by "
2676 "%s must be string or tuple",
2677 "O", __reduce__);
2678 goto finally;
2681 res = save_reduce(self, t, __reduce__, args);
2683 finally:
2684 Py_LeaveRecursiveCall();
2685 Py_XDECREF(py_ob_id);
2686 Py_XDECREF(__reduce__);
2687 Py_XDECREF(t);
2689 return res;
2693 static int
2694 dump(Picklerobject *self, PyObject *args)
2696 static char stop = STOP;
2698 if (self->proto >= 2) {
2699 char bytes[2];
2701 bytes[0] = PROTO;
2702 assert(self->proto >= 0 && self->proto < 256);
2703 bytes[1] = (char)self->proto;
2704 if (self->write_func(self, bytes, 2) < 0)
2705 return -1;
2708 if (save(self, args, 0) < 0)
2709 return -1;
2711 if (self->write_func(self, &stop, 1) < 0)
2712 return -1;
2714 if (self->write_func(self, NULL, 0) < 0)
2715 return -1;
2717 return 0;
2720 static PyObject *
2721 Pickle_clear_memo(Picklerobject *self, PyObject *args)
2723 if (self->memo)
2724 PyDict_Clear(self->memo);
2725 Py_INCREF(Py_None);
2726 return Py_None;
2729 static PyObject *
2730 Pickle_getvalue(Picklerobject *self, PyObject *args)
2732 int l, i, rsize, ssize, clear=1, lm;
2733 long ik;
2734 PyObject *k, *r;
2735 char *s, *p, *have_get;
2736 Pdata *data;
2738 /* Can be called by Python code or C code */
2739 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2740 return NULL;
2742 /* Check to make sure we are based on a list */
2743 if (! Pdata_Check(self->file)) {
2744 PyErr_SetString(PicklingError,
2745 "Attempt to getvalue() a non-list-based pickler");
2746 return NULL;
2749 /* flush write buffer */
2750 if (write_other(self, NULL, 0) < 0) return NULL;
2752 data=(Pdata*)self->file;
2753 l=data->length;
2755 /* set up an array to hold get/put status */
2756 lm = PyDict_Size(self->memo);
2757 if (lm < 0) return NULL;
2758 lm++;
2759 have_get = malloc(lm);
2760 if (have_get == NULL) return PyErr_NoMemory();
2761 memset(have_get, 0, lm);
2763 /* Scan for gets. */
2764 for (rsize = 0, i = l; --i >= 0; ) {
2765 k = data->data[i];
2767 if (PyString_Check(k))
2768 rsize += PyString_GET_SIZE(k);
2770 else if (PyInt_Check(k)) { /* put */
2771 ik = PyInt_AS_LONG((PyIntObject*)k);
2772 if (ik >= lm || ik == 0) {
2773 PyErr_SetString(PicklingError,
2774 "Invalid get data");
2775 goto err;
2777 if (have_get[ik]) /* with matching get */
2778 rsize += ik < 256 ? 2 : 5;
2781 else if (! (PyTuple_Check(k) &&
2782 PyTuple_GET_SIZE(k) == 2 &&
2783 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2785 PyErr_SetString(PicklingError,
2786 "Unexpected data in internal list");
2787 goto err;
2790 else { /* put */
2791 ik = PyInt_AS_LONG((PyIntObject *)k);
2792 if (ik >= lm || ik == 0) {
2793 PyErr_SetString(PicklingError,
2794 "Invalid get data");
2795 return NULL;
2797 have_get[ik] = 1;
2798 rsize += ik < 256 ? 2 : 5;
2802 /* Now generate the result */
2803 r = PyString_FromStringAndSize(NULL, rsize);
2804 if (r == NULL) goto err;
2805 s = PyString_AS_STRING((PyStringObject *)r);
2807 for (i = 0; i < l; i++) {
2808 k = data->data[i];
2810 if (PyString_Check(k)) {
2811 ssize = PyString_GET_SIZE(k);
2812 if (ssize) {
2813 p=PyString_AS_STRING((PyStringObject *)k);
2814 while (--ssize >= 0)
2815 *s++ = *p++;
2819 else if (PyTuple_Check(k)) { /* get */
2820 ik = PyInt_AS_LONG((PyIntObject *)
2821 PyTuple_GET_ITEM(k, 0));
2822 if (ik < 256) {
2823 *s++ = BINGET;
2824 *s++ = (int)(ik & 0xff);
2826 else {
2827 *s++ = LONG_BINGET;
2828 *s++ = (int)(ik & 0xff);
2829 *s++ = (int)((ik >> 8) & 0xff);
2830 *s++ = (int)((ik >> 16) & 0xff);
2831 *s++ = (int)((ik >> 24) & 0xff);
2835 else { /* put */
2836 ik = PyInt_AS_LONG((PyIntObject*)k);
2838 if (have_get[ik]) { /* with matching get */
2839 if (ik < 256) {
2840 *s++ = BINPUT;
2841 *s++ = (int)(ik & 0xff);
2843 else {
2844 *s++ = LONG_BINPUT;
2845 *s++ = (int)(ik & 0xff);
2846 *s++ = (int)((ik >> 8) & 0xff);
2847 *s++ = (int)((ik >> 16) & 0xff);
2848 *s++ = (int)((ik >> 24) & 0xff);
2854 if (clear) {
2855 PyDict_Clear(self->memo);
2856 Pdata_clear(data, 0);
2859 free(have_get);
2860 return r;
2861 err:
2862 free(have_get);
2863 return NULL;
2866 static PyObject *
2867 Pickler_dump(Picklerobject *self, PyObject *args)
2869 PyObject *ob;
2870 int get=0;
2872 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
2873 return NULL;
2875 if (dump(self, ob) < 0)
2876 return NULL;
2878 if (get) return Pickle_getvalue(self, NULL);
2880 /* XXX Why does dump() return self? */
2881 Py_INCREF(self);
2882 return (PyObject*)self;
2886 static struct PyMethodDef Pickler_methods[] =
2888 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
2889 PyDoc_STR("dump(object) -- "
2890 "Write an object in pickle format to the object's pickle stream")},
2891 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
2892 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2893 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
2894 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2895 {NULL, NULL} /* sentinel */
2899 static Picklerobject *
2900 newPicklerobject(PyObject *file, int proto)
2902 Picklerobject *self;
2904 if (proto < 0)
2905 proto = HIGHEST_PROTOCOL;
2906 if (proto > HIGHEST_PROTOCOL) {
2907 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2908 "the highest available protocol is %d",
2909 proto, HIGHEST_PROTOCOL);
2910 return NULL;
2913 self = PyObject_GC_New(Picklerobject, &Picklertype);
2914 if (self == NULL)
2915 return NULL;
2916 self->proto = proto;
2917 self->bin = proto > 0;
2918 self->fp = NULL;
2919 self->write = NULL;
2920 self->memo = NULL;
2921 self->arg = NULL;
2922 self->pers_func = NULL;
2923 self->inst_pers_func = NULL;
2924 self->write_buf = NULL;
2925 self->fast = 0;
2926 self->fast_container = 0;
2927 self->fast_memo = NULL;
2928 self->buf_size = 0;
2929 self->dispatch_table = NULL;
2931 self->file = NULL;
2932 if (file)
2933 Py_INCREF(file);
2934 else {
2935 file = Pdata_New();
2936 if (file == NULL)
2937 goto err;
2939 self->file = file;
2941 if (!( self->memo = PyDict_New()))
2942 goto err;
2944 if (PyFile_Check(file)) {
2945 self->fp = PyFile_AsFile(file);
2946 if (self->fp == NULL) {
2947 PyErr_SetString(PyExc_ValueError,
2948 "I/O operation on closed file");
2949 goto err;
2951 self->write_func = write_file;
2953 else if (PycStringIO_OutputCheck(file)) {
2954 self->write_func = write_cStringIO;
2956 else if (file == Py_None) {
2957 self->write_func = write_none;
2959 else {
2960 self->write_func = write_other;
2962 if (! Pdata_Check(file)) {
2963 self->write = PyObject_GetAttr(file, write_str);
2964 if (!self->write) {
2965 PyErr_Clear();
2966 PyErr_SetString(PyExc_TypeError,
2967 "argument must have 'write' "
2968 "attribute");
2969 goto err;
2973 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2974 if (self->write_buf == NULL) {
2975 PyErr_NoMemory();
2976 goto err;
2980 if (PyEval_GetRestricted()) {
2981 /* Restricted execution, get private tables */
2982 PyObject *m = PyImport_Import(copyreg_str);
2984 if (m == NULL)
2985 goto err;
2986 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
2987 Py_DECREF(m);
2988 if (self->dispatch_table == NULL)
2989 goto err;
2991 else {
2992 self->dispatch_table = dispatch_table;
2993 Py_INCREF(dispatch_table);
2995 PyObject_GC_Track(self);
2997 return self;
2999 err:
3000 Py_DECREF(self);
3001 return NULL;
3005 static PyObject *
3006 get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
3008 static char *kwlist[] = {"file", "protocol", NULL};
3009 PyObject *file = NULL;
3010 int proto = 0;
3012 /* XXX
3013 * The documented signature is Pickler(file, protocol=0), but this
3014 * accepts Pickler() and Pickler(integer) too. The meaning then
3015 * is clear as mud, undocumented, and not supported by pickle.py.
3016 * I'm told Zope uses this, but I haven't traced into this code
3017 * far enough to figure out what it means.
3019 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
3020 PyErr_Clear();
3021 proto = 0;
3022 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
3023 kwlist, &file, &proto))
3024 return NULL;
3026 return (PyObject *)newPicklerobject(file, proto);
3030 static void
3031 Pickler_dealloc(Picklerobject *self)
3033 PyObject_GC_UnTrack(self);
3034 Py_XDECREF(self->write);
3035 Py_XDECREF(self->memo);
3036 Py_XDECREF(self->fast_memo);
3037 Py_XDECREF(self->arg);
3038 Py_XDECREF(self->file);
3039 Py_XDECREF(self->pers_func);
3040 Py_XDECREF(self->inst_pers_func);
3041 Py_XDECREF(self->dispatch_table);
3042 PyMem_Free(self->write_buf);
3043 Py_TYPE(self)->tp_free((PyObject *)self);
3046 static int
3047 Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
3049 Py_VISIT(self->write);
3050 Py_VISIT(self->memo);
3051 Py_VISIT(self->fast_memo);
3052 Py_VISIT(self->arg);
3053 Py_VISIT(self->file);
3054 Py_VISIT(self->pers_func);
3055 Py_VISIT(self->inst_pers_func);
3056 Py_VISIT(self->dispatch_table);
3057 return 0;
3060 static int
3061 Pickler_clear(Picklerobject *self)
3063 Py_CLEAR(self->write);
3064 Py_CLEAR(self->memo);
3065 Py_CLEAR(self->fast_memo);
3066 Py_CLEAR(self->arg);
3067 Py_CLEAR(self->file);
3068 Py_CLEAR(self->pers_func);
3069 Py_CLEAR(self->inst_pers_func);
3070 Py_CLEAR(self->dispatch_table);
3071 return 0;
3074 static PyObject *
3075 Pickler_get_pers_func(Picklerobject *p)
3077 if (p->pers_func == NULL)
3078 PyErr_SetString(PyExc_AttributeError, "persistent_id");
3079 else
3080 Py_INCREF(p->pers_func);
3081 return p->pers_func;
3084 static int
3085 Pickler_set_pers_func(Picklerobject *p, PyObject *v)
3087 if (v == NULL) {
3088 PyErr_SetString(PyExc_TypeError,
3089 "attribute deletion is not supported");
3090 return -1;
3092 Py_XDECREF(p->pers_func);
3093 Py_INCREF(v);
3094 p->pers_func = v;
3095 return 0;
3098 static int
3099 Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
3101 if (v == NULL) {
3102 PyErr_SetString(PyExc_TypeError,
3103 "attribute deletion is not supported");
3104 return -1;
3106 Py_XDECREF(p->inst_pers_func);
3107 Py_INCREF(v);
3108 p->inst_pers_func = v;
3109 return 0;
3112 static PyObject *
3113 Pickler_get_memo(Picklerobject *p)
3115 if (p->memo == NULL)
3116 PyErr_SetString(PyExc_AttributeError, "memo");
3117 else
3118 Py_INCREF(p->memo);
3119 return p->memo;
3122 static int
3123 Pickler_set_memo(Picklerobject *p, PyObject *v)
3125 if (v == NULL) {
3126 PyErr_SetString(PyExc_TypeError,
3127 "attribute deletion is not supported");
3128 return -1;
3130 if (!PyDict_Check(v)) {
3131 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
3132 return -1;
3134 Py_XDECREF(p->memo);
3135 Py_INCREF(v);
3136 p->memo = v;
3137 return 0;
3140 static PyObject *
3141 Pickler_get_error(Picklerobject *p)
3143 /* why is this an attribute on the Pickler? */
3144 Py_INCREF(PicklingError);
3145 return PicklingError;
3148 static PyMemberDef Pickler_members[] = {
3149 {"binary", T_INT, offsetof(Picklerobject, bin)},
3150 {"fast", T_INT, offsetof(Picklerobject, fast)},
3151 {NULL}
3154 static PyGetSetDef Pickler_getsets[] = {
3155 {"persistent_id", (getter)Pickler_get_pers_func,
3156 (setter)Pickler_set_pers_func},
3157 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3158 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
3159 {"PicklingError", (getter)Pickler_get_error, NULL},
3160 {NULL}
3163 PyDoc_STRVAR(Picklertype__doc__,
3164 "Objects that know how to pickle objects\n");
3166 static PyTypeObject Picklertype = {
3167 PyVarObject_HEAD_INIT(NULL, 0)
3168 "cPickle.Pickler", /*tp_name*/
3169 sizeof(Picklerobject), /*tp_basicsize*/
3171 (destructor)Pickler_dealloc, /* tp_dealloc */
3172 0, /* tp_print */
3173 0, /* tp_getattr */
3174 0, /* tp_setattr */
3175 0, /* tp_compare */
3176 0, /* tp_repr */
3177 0, /* tp_as_number */
3178 0, /* tp_as_sequence */
3179 0, /* tp_as_mapping */
3180 0, /* tp_hash */
3181 0, /* tp_call */
3182 0, /* tp_str */
3183 PyObject_GenericGetAttr, /* tp_getattro */
3184 PyObject_GenericSetAttr, /* tp_setattro */
3185 0, /* tp_as_buffer */
3186 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3187 Picklertype__doc__, /* tp_doc */
3188 (traverseproc)Pickler_traverse, /* tp_traverse */
3189 (inquiry)Pickler_clear, /* tp_clear */
3190 0, /* tp_richcompare */
3191 0, /* tp_weaklistoffset */
3192 0, /* tp_iter */
3193 0, /* tp_iternext */
3194 Pickler_methods, /* tp_methods */
3195 Pickler_members, /* tp_members */
3196 Pickler_getsets, /* tp_getset */
3199 static PyObject *
3200 find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
3202 PyObject *global = 0, *module;
3204 if (fc) {
3205 if (fc==Py_None) {
3206 PyErr_SetString(UnpicklingError, "Global and instance "
3207 "pickles are not supported.");
3208 return NULL;
3210 return PyObject_CallFunctionObjArgs(fc, py_module_name,
3211 py_global_name, NULL);
3214 module = PySys_GetObject("modules");
3215 if (module == NULL)
3216 return NULL;
3218 module = PyDict_GetItem(module, py_module_name);
3219 if (module == NULL) {
3220 module = PyImport_Import(py_module_name);
3221 if (!module)
3222 return NULL;
3223 global = PyObject_GetAttr(module, py_global_name);
3224 Py_DECREF(module);
3226 else
3227 global = PyObject_GetAttr(module, py_global_name);
3228 return global;
3231 static int
3232 marker(Unpicklerobject *self)
3234 if (self->num_marks < 1) {
3235 PyErr_SetString(UnpicklingError, "could not find MARK");
3236 return -1;
3239 return self->marks[--self->num_marks];
3243 static int
3244 load_none(Unpicklerobject *self)
3246 PDATA_APPEND(self->stack, Py_None, -1);
3247 return 0;
3250 static int
3251 bad_readline(void)
3253 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3254 return -1;
3257 static int
3258 load_int(Unpicklerobject *self)
3260 PyObject *py_int = 0;
3261 char *endptr, *s;
3262 int len, res = -1;
3263 long l;
3265 if ((len = self->readline_func(self, &s)) < 0) return -1;
3266 if (len < 2) return bad_readline();
3267 if (!( s=pystrndup(s,len))) return -1;
3269 errno = 0;
3270 l = strtol(s, &endptr, 0);
3272 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3273 /* Hm, maybe we've got something long. Let's try reading
3274 it as a Python long object. */
3275 errno = 0;
3276 py_int = PyLong_FromString(s, NULL, 0);
3277 if (py_int == NULL) {
3278 PyErr_SetString(PyExc_ValueError,
3279 "could not convert string to int");
3280 goto finally;
3283 else {
3284 if (len == 3 && (l == 0 || l == 1)) {
3285 if (!( py_int = PyBool_FromLong(l))) goto finally;
3287 else {
3288 if (!( py_int = PyInt_FromLong(l))) goto finally;
3292 free(s);
3293 PDATA_PUSH(self->stack, py_int, -1);
3294 return 0;
3296 finally:
3297 free(s);
3299 return res;
3302 static int
3303 load_bool(Unpicklerobject *self, PyObject *boolean)
3305 assert(boolean == Py_True || boolean == Py_False);
3306 PDATA_APPEND(self->stack, boolean, -1);
3307 return 0;
3310 /* s contains x bytes of a little-endian integer. Return its value as a
3311 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3312 * int, but when x is 4 it's a signed one. This is an historical source
3313 * of x-platform bugs.
3315 static long
3316 calc_binint(char *s, int x)
3318 unsigned char c;
3319 int i;
3320 long l;
3322 for (i = 0, l = 0L; i < x; i++) {
3323 c = (unsigned char)s[i];
3324 l |= (long)c << (i * 8);
3326 #if SIZEOF_LONG > 4
3327 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3328 * is signed, so on a box with longs bigger than 4 bytes we need
3329 * to extend a BININT's sign bit to the full width.
3331 if (x == 4 && l & (1L << 31))
3332 l |= (~0L) << 32;
3333 #endif
3334 return l;
3338 static int
3339 load_binintx(Unpicklerobject *self, char *s, int x)
3341 PyObject *py_int = 0;
3342 long l;
3344 l = calc_binint(s, x);
3346 if (!( py_int = PyInt_FromLong(l)))
3347 return -1;
3349 PDATA_PUSH(self->stack, py_int, -1);
3350 return 0;
3354 static int
3355 load_binint(Unpicklerobject *self)
3357 char *s;
3359 if (self->read_func(self, &s, 4) < 0)
3360 return -1;
3362 return load_binintx(self, s, 4);
3366 static int
3367 load_binint1(Unpicklerobject *self)
3369 char *s;
3371 if (self->read_func(self, &s, 1) < 0)
3372 return -1;
3374 return load_binintx(self, s, 1);
3378 static int
3379 load_binint2(Unpicklerobject *self)
3381 char *s;
3383 if (self->read_func(self, &s, 2) < 0)
3384 return -1;
3386 return load_binintx(self, s, 2);
3389 static int
3390 load_long(Unpicklerobject *self)
3392 PyObject *l = 0;
3393 char *end, *s;
3394 int len, res = -1;
3396 if ((len = self->readline_func(self, &s)) < 0) return -1;
3397 if (len < 2) return bad_readline();
3398 if (!( s=pystrndup(s,len))) return -1;
3400 if (!( l = PyLong_FromString(s, &end, 0)))
3401 goto finally;
3403 free(s);
3404 PDATA_PUSH(self->stack, l, -1);
3405 return 0;
3407 finally:
3408 free(s);
3410 return res;
3413 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
3414 * data following.
3416 static int
3417 load_counted_long(Unpicklerobject *self, int size)
3419 Py_ssize_t i;
3420 char *nbytes;
3421 unsigned char *pdata;
3422 PyObject *along;
3424 assert(size == 1 || size == 4);
3425 i = self->read_func(self, &nbytes, size);
3426 if (i < 0) return -1;
3428 size = calc_binint(nbytes, size);
3429 if (size < 0) {
3430 /* Corrupt or hostile pickle -- we never write one like
3431 * this.
3433 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3434 "byte count");
3435 return -1;
3438 if (size == 0)
3439 along = PyLong_FromLong(0L);
3440 else {
3441 /* Read the raw little-endian bytes & convert. */
3442 i = self->read_func(self, (char **)&pdata, size);
3443 if (i < 0) return -1;
3444 along = _PyLong_FromByteArray(pdata, (size_t)size,
3445 1 /* little endian */, 1 /* signed */);
3447 if (along == NULL)
3448 return -1;
3449 PDATA_PUSH(self->stack, along, -1);
3450 return 0;
3453 static int
3454 load_float(Unpicklerobject *self)
3456 PyObject *py_float = 0;
3457 char *endptr, *s;
3458 int len, res = -1;
3459 double d;
3461 if ((len = self->readline_func(self, &s)) < 0) return -1;
3462 if (len < 2) return bad_readline();
3463 if (!( s=pystrndup(s,len))) return -1;
3465 errno = 0;
3466 d = PyOS_ascii_strtod(s, &endptr);
3468 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3469 PyErr_SetString(PyExc_ValueError,
3470 "could not convert string to float");
3471 goto finally;
3474 if (!( py_float = PyFloat_FromDouble(d)))
3475 goto finally;
3477 free(s);
3478 PDATA_PUSH(self->stack, py_float, -1);
3479 return 0;
3481 finally:
3482 free(s);
3484 return res;
3487 static int
3488 load_binfloat(Unpicklerobject *self)
3490 PyObject *py_float;
3491 double x;
3492 char *p;
3494 if (self->read_func(self, &p, 8) < 0)
3495 return -1;
3497 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3498 if (x == -1.0 && PyErr_Occurred())
3499 return -1;
3501 py_float = PyFloat_FromDouble(x);
3502 if (py_float == NULL)
3503 return -1;
3505 PDATA_PUSH(self->stack, py_float, -1);
3506 return 0;
3509 static int
3510 load_string(Unpicklerobject *self)
3512 PyObject *str = 0;
3513 int len, res = -1;
3514 char *s, *p;
3516 if ((len = self->readline_func(self, &s)) < 0) return -1;
3517 if (len < 2) return bad_readline();
3518 if (!( s=pystrndup(s,len))) return -1;
3521 /* Strip outermost quotes */
3522 while (s[len-1] <= ' ')
3523 len--;
3524 if(s[0]=='"' && s[len-1]=='"'){
3525 s[len-1] = '\0';
3526 p = s + 1 ;
3527 len -= 2;
3528 } else if(s[0]=='\'' && s[len-1]=='\''){
3529 s[len-1] = '\0';
3530 p = s + 1 ;
3531 len -= 2;
3532 } else
3533 goto insecure;
3534 /********************************************/
3536 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3537 free(s);
3538 if (str) {
3539 PDATA_PUSH(self->stack, str, -1);
3540 res = 0;
3542 return res;
3544 insecure:
3545 free(s);
3546 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3547 return -1;
3551 static int
3552 load_binstring(Unpicklerobject *self)
3554 PyObject *py_string = 0;
3555 long l;
3556 char *s;
3558 if (self->read_func(self, &s, 4) < 0) return -1;
3560 l = calc_binint(s, 4);
3561 if (l < 0) {
3562 /* Corrupt or hostile pickle -- we never write one like
3563 * this.
3565 PyErr_SetString(UnpicklingError,
3566 "BINSTRING pickle has negative byte count");
3567 return -1;
3570 if (self->read_func(self, &s, l) < 0)
3571 return -1;
3573 if (!( py_string = PyString_FromStringAndSize(s, l)))
3574 return -1;
3576 PDATA_PUSH(self->stack, py_string, -1);
3577 return 0;
3581 static int
3582 load_short_binstring(Unpicklerobject *self)
3584 PyObject *py_string = 0;
3585 unsigned char l;
3586 char *s;
3588 if (self->read_func(self, &s, 1) < 0)
3589 return -1;
3591 l = (unsigned char)s[0];
3593 if (self->read_func(self, &s, l) < 0) return -1;
3595 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
3597 PDATA_PUSH(self->stack, py_string, -1);
3598 return 0;
3602 #ifdef Py_USING_UNICODE
3603 static int
3604 load_unicode(Unpicklerobject *self)
3606 PyObject *str = 0;
3607 int len, res = -1;
3608 char *s;
3610 if ((len = self->readline_func(self, &s)) < 0) return -1;
3611 if (len < 1) return bad_readline();
3613 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
3614 goto finally;
3616 PDATA_PUSH(self->stack, str, -1);
3617 return 0;
3619 finally:
3620 return res;
3622 #endif
3625 #ifdef Py_USING_UNICODE
3626 static int
3627 load_binunicode(Unpicklerobject *self)
3629 PyObject *unicode;
3630 long l;
3631 char *s;
3633 if (self->read_func(self, &s, 4) < 0) return -1;
3635 l = calc_binint(s, 4);
3636 if (l < 0) {
3637 /* Corrupt or hostile pickle -- we never write one like
3638 * this.
3640 PyErr_SetString(UnpicklingError,
3641 "BINUNICODE pickle has negative byte count");
3642 return -1;
3645 if (self->read_func(self, &s, l) < 0)
3646 return -1;
3648 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3649 return -1;
3651 PDATA_PUSH(self->stack, unicode, -1);
3652 return 0;
3654 #endif
3657 static int
3658 load_tuple(Unpicklerobject *self)
3660 PyObject *tup;
3661 int i;
3663 if ((i = marker(self)) < 0) return -1;
3664 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3665 PDATA_PUSH(self->stack, tup, -1);
3666 return 0;
3669 static int
3670 load_counted_tuple(Unpicklerobject *self, int len)
3672 PyObject *tup = PyTuple_New(len);
3674 if (tup == NULL)
3675 return -1;
3677 while (--len >= 0) {
3678 PyObject *element;
3680 PDATA_POP(self->stack, element);
3681 if (element == NULL)
3682 return -1;
3683 PyTuple_SET_ITEM(tup, len, element);
3685 PDATA_PUSH(self->stack, tup, -1);
3686 return 0;
3689 static int
3690 load_empty_list(Unpicklerobject *self)
3692 PyObject *list;
3694 if (!( list=PyList_New(0))) return -1;
3695 PDATA_PUSH(self->stack, list, -1);
3696 return 0;
3699 static int
3700 load_empty_dict(Unpicklerobject *self)
3702 PyObject *dict;
3704 if (!( dict=PyDict_New())) return -1;
3705 PDATA_PUSH(self->stack, dict, -1);
3706 return 0;
3710 static int
3711 load_list(Unpicklerobject *self)
3713 PyObject *list = 0;
3714 int i;
3716 if ((i = marker(self)) < 0) return -1;
3717 if (!( list=Pdata_popList(self->stack, i))) return -1;
3718 PDATA_PUSH(self->stack, list, -1);
3719 return 0;
3722 static int
3723 load_dict(Unpicklerobject *self)
3725 PyObject *dict, *key, *value;
3726 int i, j, k;
3728 if ((i = marker(self)) < 0) return -1;
3729 j=self->stack->length;
3731 if (!( dict = PyDict_New())) return -1;
3733 for (k = i+1; k < j; k += 2) {
3734 key =self->stack->data[k-1];
3735 value=self->stack->data[k ];
3736 if (PyDict_SetItem(dict, key, value) < 0) {
3737 Py_DECREF(dict);
3738 return -1;
3741 Pdata_clear(self->stack, i);
3742 PDATA_PUSH(self->stack, dict, -1);
3743 return 0;
3746 static PyObject *
3747 Instance_New(PyObject *cls, PyObject *args)
3749 PyObject *r = 0;
3751 if (PyClass_Check(cls)) {
3752 int l;
3754 if ((l=PyObject_Size(args)) < 0) goto err;
3755 if (!( l )) {
3756 PyObject *__getinitargs__;
3758 __getinitargs__ = PyObject_GetAttr(cls,
3759 __getinitargs___str);
3760 if (!__getinitargs__) {
3761 /* We have a class with no __getinitargs__,
3762 so bypass usual construction */
3763 PyObject *inst;
3765 PyErr_Clear();
3766 if (!( inst=PyInstance_NewRaw(cls, NULL)))
3767 goto err;
3768 return inst;
3770 Py_DECREF(__getinitargs__);
3773 if ((r=PyInstance_New(cls, args, NULL))) return r;
3774 else goto err;
3777 if ((r=PyObject_CallObject(cls, args))) return r;
3779 err:
3781 PyObject *tp, *v, *tb, *tmp_value;
3783 PyErr_Fetch(&tp, &v, &tb);
3784 tmp_value = v;
3785 /* NULL occurs when there was a KeyboardInterrupt */
3786 if (tmp_value == NULL)
3787 tmp_value = Py_None;
3788 if ((r = PyTuple_Pack(3, tmp_value, cls, args))) {
3789 Py_XDECREF(v);
3790 v=r;
3792 PyErr_Restore(tp,v,tb);
3794 return NULL;
3798 static int
3799 load_obj(Unpicklerobject *self)
3801 PyObject *class, *tup, *obj=0;
3802 int i;
3804 if ((i = marker(self)) < 0) return -1;
3805 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3806 PDATA_POP(self->stack, class);
3807 if (class) {
3808 obj = Instance_New(class, tup);
3809 Py_DECREF(class);
3811 Py_DECREF(tup);
3813 if (! obj) return -1;
3814 PDATA_PUSH(self->stack, obj, -1);
3815 return 0;
3819 static int
3820 load_inst(Unpicklerobject *self)
3822 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3823 int i, len;
3824 char *s;
3826 if ((i = marker(self)) < 0) return -1;
3828 if ((len = self->readline_func(self, &s)) < 0) return -1;
3829 if (len < 2) return bad_readline();
3830 module_name = PyString_FromStringAndSize(s, len - 1);
3831 if (!module_name) return -1;
3833 if ((len = self->readline_func(self, &s)) >= 0) {
3834 if (len < 2) return bad_readline();
3835 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3836 class = find_class(module_name, class_name,
3837 self->find_class);
3838 Py_DECREF(class_name);
3841 Py_DECREF(module_name);
3843 if (! class) return -1;
3845 if ((tup=Pdata_popTuple(self->stack, i))) {
3846 obj = Instance_New(class, tup);
3847 Py_DECREF(tup);
3849 Py_DECREF(class);
3851 if (! obj) return -1;
3853 PDATA_PUSH(self->stack, obj, -1);
3854 return 0;
3857 static int
3858 load_newobj(Unpicklerobject *self)
3860 PyObject *args = NULL;
3861 PyObject *clsraw = NULL;
3862 PyTypeObject *cls; /* clsraw cast to its true type */
3863 PyObject *obj;
3865 /* Stack is ... cls argtuple, and we want to call
3866 * cls.__new__(cls, *argtuple).
3868 PDATA_POP(self->stack, args);
3869 if (args == NULL) goto Fail;
3870 if (! PyTuple_Check(args)) {
3871 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3872 "tuple.");
3873 goto Fail;
3876 PDATA_POP(self->stack, clsraw);
3877 cls = (PyTypeObject *)clsraw;
3878 if (cls == NULL) goto Fail;
3879 if (! PyType_Check(cls)) {
3880 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3881 "isn't a type object");
3882 goto Fail;
3884 if (cls->tp_new == NULL) {
3885 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3886 "has NULL tp_new");
3887 goto Fail;
3890 /* Call __new__. */
3891 obj = cls->tp_new(cls, args, NULL);
3892 if (obj == NULL) goto Fail;
3894 Py_DECREF(args);
3895 Py_DECREF(clsraw);
3896 PDATA_PUSH(self->stack, obj, -1);
3897 return 0;
3899 Fail:
3900 Py_XDECREF(args);
3901 Py_XDECREF(clsraw);
3902 return -1;
3905 static int
3906 load_global(Unpicklerobject *self)
3908 PyObject *class = 0, *module_name = 0, *class_name = 0;
3909 int len;
3910 char *s;
3912 if ((len = self->readline_func(self, &s)) < 0) return -1;
3913 if (len < 2) return bad_readline();
3914 module_name = PyString_FromStringAndSize(s, len - 1);
3915 if (!module_name) return -1;
3917 if ((len = self->readline_func(self, &s)) >= 0) {
3918 if (len < 2) {
3919 Py_DECREF(module_name);
3920 return bad_readline();
3922 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3923 class = find_class(module_name, class_name,
3924 self->find_class);
3925 Py_DECREF(class_name);
3928 Py_DECREF(module_name);
3930 if (! class) return -1;
3931 PDATA_PUSH(self->stack, class, -1);
3932 return 0;
3936 static int
3937 load_persid(Unpicklerobject *self)
3939 PyObject *pid = 0;
3940 int len;
3941 char *s;
3943 if (self->pers_func) {
3944 if ((len = self->readline_func(self, &s)) < 0) return -1;
3945 if (len < 2) return bad_readline();
3947 pid = PyString_FromStringAndSize(s, len - 1);
3948 if (!pid) return -1;
3950 if (PyList_Check(self->pers_func)) {
3951 if (PyList_Append(self->pers_func, pid) < 0) {
3952 Py_DECREF(pid);
3953 return -1;
3956 else {
3957 ARG_TUP(self, pid);
3958 if (self->arg) {
3959 pid = PyObject_Call(self->pers_func, self->arg,
3960 NULL);
3961 FREE_ARG_TUP(self);
3965 if (! pid) return -1;
3967 PDATA_PUSH(self->stack, pid, -1);
3968 return 0;
3970 else {
3971 PyErr_SetString(UnpicklingError,
3972 "A load persistent id instruction was encountered,\n"
3973 "but no persistent_load function was specified.");
3974 return -1;
3978 static int
3979 load_binpersid(Unpicklerobject *self)
3981 PyObject *pid = 0;
3983 if (self->pers_func) {
3984 PDATA_POP(self->stack, pid);
3985 if (! pid) return -1;
3987 if (PyList_Check(self->pers_func)) {
3988 if (PyList_Append(self->pers_func, pid) < 0) {
3989 Py_DECREF(pid);
3990 return -1;
3993 else {
3994 ARG_TUP(self, pid);
3995 if (self->arg) {
3996 pid = PyObject_Call(self->pers_func, self->arg,
3997 NULL);
3998 FREE_ARG_TUP(self);
4000 if (! pid) return -1;
4003 PDATA_PUSH(self->stack, pid, -1);
4004 return 0;
4006 else {
4007 PyErr_SetString(UnpicklingError,
4008 "A load persistent id instruction was encountered,\n"
4009 "but no persistent_load function was specified.");
4010 return -1;
4015 static int
4016 load_pop(Unpicklerobject *self)
4018 int len;
4020 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
4022 /* Note that we split the (pickle.py) stack into two stacks,
4023 an object stack and a mark stack. We have to be clever and
4024 pop the right one. We do this by looking at the top of the
4025 mark stack.
4028 if ((self->num_marks > 0) &&
4029 (self->marks[self->num_marks - 1] == len))
4030 self->num_marks--;
4031 else {
4032 len--;
4033 Py_DECREF(self->stack->data[len]);
4034 self->stack->length=len;
4037 return 0;
4041 static int
4042 load_pop_mark(Unpicklerobject *self)
4044 int i;
4046 if ((i = marker(self)) < 0)
4047 return -1;
4049 Pdata_clear(self->stack, i);
4051 return 0;
4055 static int
4056 load_dup(Unpicklerobject *self)
4058 PyObject *last;
4059 int len;
4061 if ((len = self->stack->length) <= 0) return stackUnderflow();
4062 last=self->stack->data[len-1];
4063 Py_INCREF(last);
4064 PDATA_PUSH(self->stack, last, -1);
4065 return 0;
4069 static int
4070 load_get(Unpicklerobject *self)
4072 PyObject *py_str = 0, *value = 0;
4073 int len;
4074 char *s;
4075 int rc;
4077 if ((len = self->readline_func(self, &s)) < 0) return -1;
4078 if (len < 2) return bad_readline();
4080 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
4082 value = PyDict_GetItem(self->memo, py_str);
4083 if (! value) {
4084 PyErr_SetObject(BadPickleGet, py_str);
4085 rc = -1;
4087 else {
4088 PDATA_APPEND(self->stack, value, -1);
4089 rc = 0;
4092 Py_DECREF(py_str);
4093 return rc;
4097 static int
4098 load_binget(Unpicklerobject *self)
4100 PyObject *py_key = 0, *value = 0;
4101 unsigned char key;
4102 char *s;
4103 int rc;
4105 if (self->read_func(self, &s, 1) < 0) return -1;
4107 key = (unsigned char)s[0];
4108 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4110 value = PyDict_GetItem(self->memo, py_key);
4111 if (! value) {
4112 PyErr_SetObject(BadPickleGet, py_key);
4113 rc = -1;
4115 else {
4116 PDATA_APPEND(self->stack, value, -1);
4117 rc = 0;
4120 Py_DECREF(py_key);
4121 return rc;
4125 static int
4126 load_long_binget(Unpicklerobject *self)
4128 PyObject *py_key = 0, *value = 0;
4129 unsigned char c;
4130 char *s;
4131 long key;
4132 int rc;
4134 if (self->read_func(self, &s, 4) < 0) return -1;
4136 c = (unsigned char)s[0];
4137 key = (long)c;
4138 c = (unsigned char)s[1];
4139 key |= (long)c << 8;
4140 c = (unsigned char)s[2];
4141 key |= (long)c << 16;
4142 c = (unsigned char)s[3];
4143 key |= (long)c << 24;
4145 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4147 value = PyDict_GetItem(self->memo, py_key);
4148 if (! value) {
4149 PyErr_SetObject(BadPickleGet, py_key);
4150 rc = -1;
4152 else {
4153 PDATA_APPEND(self->stack, value, -1);
4154 rc = 0;
4157 Py_DECREF(py_key);
4158 return rc;
4161 /* Push an object from the extension registry (EXT[124]). nbytes is
4162 * the number of bytes following the opcode, holding the index (code) value.
4164 static int
4165 load_extension(Unpicklerobject *self, int nbytes)
4167 char *codebytes; /* the nbytes bytes after the opcode */
4168 long code; /* calc_binint returns long */
4169 PyObject *py_code; /* code as a Python int */
4170 PyObject *obj; /* the object to push */
4171 PyObject *pair; /* (module_name, class_name) */
4172 PyObject *module_name, *class_name;
4174 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4175 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4176 code = calc_binint(codebytes, nbytes);
4177 if (code <= 0) { /* note that 0 is forbidden */
4178 /* Corrupt or hostile pickle. */
4179 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4180 return -1;
4183 /* Look for the code in the cache. */
4184 py_code = PyInt_FromLong(code);
4185 if (py_code == NULL) return -1;
4186 obj = PyDict_GetItem(extension_cache, py_code);
4187 if (obj != NULL) {
4188 /* Bingo. */
4189 Py_DECREF(py_code);
4190 PDATA_APPEND(self->stack, obj, -1);
4191 return 0;
4194 /* Look up the (module_name, class_name) pair. */
4195 pair = PyDict_GetItem(inverted_registry, py_code);
4196 if (pair == NULL) {
4197 Py_DECREF(py_code);
4198 PyErr_Format(PyExc_ValueError, "unregistered extension "
4199 "code %ld", code);
4200 return -1;
4202 /* Since the extension registry is manipulable via Python code,
4203 * confirm that pair is really a 2-tuple of strings.
4205 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4206 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4207 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4208 Py_DECREF(py_code);
4209 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4210 "isn't a 2-tuple of strings", code);
4211 return -1;
4213 /* Load the object. */
4214 obj = find_class(module_name, class_name, self->find_class);
4215 if (obj == NULL) {
4216 Py_DECREF(py_code);
4217 return -1;
4219 /* Cache code -> obj. */
4220 code = PyDict_SetItem(extension_cache, py_code, obj);
4221 Py_DECREF(py_code);
4222 if (code < 0) {
4223 Py_DECREF(obj);
4224 return -1;
4226 PDATA_PUSH(self->stack, obj, -1);
4227 return 0;
4230 static int
4231 load_put(Unpicklerobject *self)
4233 PyObject *py_str = 0, *value = 0;
4234 int len, l;
4235 char *s;
4237 if ((l = self->readline_func(self, &s)) < 0) return -1;
4238 if (l < 2) return bad_readline();
4239 if (!( len=self->stack->length )) return stackUnderflow();
4240 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4241 value=self->stack->data[len-1];
4242 l=PyDict_SetItem(self->memo, py_str, value);
4243 Py_DECREF(py_str);
4244 return l;
4248 static int
4249 load_binput(Unpicklerobject *self)
4251 PyObject *py_key = 0, *value = 0;
4252 unsigned char key;
4253 char *s;
4254 int len;
4256 if (self->read_func(self, &s, 1) < 0) return -1;
4257 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
4259 key = (unsigned char)s[0];
4261 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4262 value=self->stack->data[len-1];
4263 len=PyDict_SetItem(self->memo, py_key, value);
4264 Py_DECREF(py_key);
4265 return len;
4269 static int
4270 load_long_binput(Unpicklerobject *self)
4272 PyObject *py_key = 0, *value = 0;
4273 long key;
4274 unsigned char c;
4275 char *s;
4276 int len;
4278 if (self->read_func(self, &s, 4) < 0) return -1;
4279 if (!( len=self->stack->length )) return stackUnderflow();
4281 c = (unsigned char)s[0];
4282 key = (long)c;
4283 c = (unsigned char)s[1];
4284 key |= (long)c << 8;
4285 c = (unsigned char)s[2];
4286 key |= (long)c << 16;
4287 c = (unsigned char)s[3];
4288 key |= (long)c << 24;
4290 if (!( py_key = PyInt_FromLong(key))) return -1;
4291 value=self->stack->data[len-1];
4292 len=PyDict_SetItem(self->memo, py_key, value);
4293 Py_DECREF(py_key);
4294 return len;
4298 static int
4299 do_append(Unpicklerobject *self, int x)
4301 PyObject *value = 0, *list = 0, *append_method = 0;
4302 int len, i;
4304 len=self->stack->length;
4305 if (!( len >= x && x > 0 )) return stackUnderflow();
4306 /* nothing to do */
4307 if (len==x) return 0;
4309 list=self->stack->data[x-1];
4311 if (PyList_Check(list)) {
4312 PyObject *slice;
4313 int list_len;
4315 slice=Pdata_popList(self->stack, x);
4316 if (! slice) return -1;
4317 list_len = PyList_GET_SIZE(list);
4318 i=PyList_SetSlice(list, list_len, list_len, slice);
4319 Py_DECREF(slice);
4320 return i;
4322 else {
4324 if (!( append_method = PyObject_GetAttr(list, append_str)))
4325 return -1;
4327 for (i = x; i < len; i++) {
4328 PyObject *junk;
4330 value=self->stack->data[i];
4331 junk=0;
4332 ARG_TUP(self, value);
4333 if (self->arg) {
4334 junk = PyObject_Call(append_method, self->arg,
4335 NULL);
4336 FREE_ARG_TUP(self);
4338 if (! junk) {
4339 Pdata_clear(self->stack, i+1);
4340 self->stack->length=x;
4341 Py_DECREF(append_method);
4342 return -1;
4344 Py_DECREF(junk);
4346 self->stack->length=x;
4347 Py_DECREF(append_method);
4350 return 0;
4354 static int
4355 load_append(Unpicklerobject *self)
4357 return do_append(self, self->stack->length - 1);
4361 static int
4362 load_appends(Unpicklerobject *self)
4364 return do_append(self, marker(self));
4368 static int
4369 do_setitems(Unpicklerobject *self, int x)
4371 PyObject *value = 0, *key = 0, *dict = 0;
4372 int len, i, r=0;
4374 if (!( (len=self->stack->length) >= x
4375 && x > 0 )) return stackUnderflow();
4377 dict=self->stack->data[x-1];
4379 for (i = x+1; i < len; i += 2) {
4380 key =self->stack->data[i-1];
4381 value=self->stack->data[i ];
4382 if (PyObject_SetItem(dict, key, value) < 0) {
4383 r=-1;
4384 break;
4388 Pdata_clear(self->stack, x);
4390 return r;
4394 static int
4395 load_setitem(Unpicklerobject *self)
4397 return do_setitems(self, self->stack->length - 2);
4400 static int
4401 load_setitems(Unpicklerobject *self)
4403 return do_setitems(self, marker(self));
4407 static int
4408 load_build(Unpicklerobject *self)
4410 PyObject *state, *inst, *slotstate;
4411 PyObject *__setstate__;
4412 PyObject *d_key, *d_value;
4413 Py_ssize_t i;
4414 int res = -1;
4416 /* Stack is ... instance, state. We want to leave instance at
4417 * the stack top, possibly mutated via instance.__setstate__(state).
4419 if (self->stack->length < 2)
4420 return stackUnderflow();
4421 PDATA_POP(self->stack, state);
4422 if (state == NULL)
4423 return -1;
4424 inst = self->stack->data[self->stack->length - 1];
4426 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4427 if (__setstate__ != NULL) {
4428 PyObject *junk = NULL;
4430 /* The explicit __setstate__ is responsible for everything. */
4431 ARG_TUP(self, state);
4432 if (self->arg) {
4433 junk = PyObject_Call(__setstate__, self->arg, NULL);
4434 FREE_ARG_TUP(self);
4436 Py_DECREF(__setstate__);
4437 if (junk == NULL)
4438 return -1;
4439 Py_DECREF(junk);
4440 return 0;
4442 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4443 return -1;
4444 PyErr_Clear();
4446 /* A default __setstate__. First see whether state embeds a
4447 * slot state dict too (a proto 2 addition).
4449 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4450 PyObject *temp = state;
4451 state = PyTuple_GET_ITEM(temp, 0);
4452 slotstate = PyTuple_GET_ITEM(temp, 1);
4453 Py_INCREF(state);
4454 Py_INCREF(slotstate);
4455 Py_DECREF(temp);
4457 else
4458 slotstate = NULL;
4460 /* Set inst.__dict__ from the state dict (if any). */
4461 if (state != Py_None) {
4462 PyObject *dict;
4463 if (! PyDict_Check(state)) {
4464 PyErr_SetString(UnpicklingError, "state is not a "
4465 "dictionary");
4466 goto finally;
4468 dict = PyObject_GetAttr(inst, __dict___str);
4469 if (dict == NULL)
4470 goto finally;
4472 i = 0;
4473 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4474 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4475 goto finally;
4477 Py_DECREF(dict);
4480 /* Also set instance attributes from the slotstate dict (if any). */
4481 if (slotstate != NULL) {
4482 if (! PyDict_Check(slotstate)) {
4483 PyErr_SetString(UnpicklingError, "slot state is not "
4484 "a dictionary");
4485 goto finally;
4487 i = 0;
4488 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4489 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4490 goto finally;
4493 res = 0;
4495 finally:
4496 Py_DECREF(state);
4497 Py_XDECREF(slotstate);
4498 return res;
4502 static int
4503 load_mark(Unpicklerobject *self)
4505 int s;
4507 /* Note that we split the (pickle.py) stack into two stacks, an
4508 object stack and a mark stack. Here we push a mark onto the
4509 mark stack.
4512 if ((self->num_marks + 1) >= self->marks_size) {
4513 int *marks;
4514 s=self->marks_size+20;
4515 if (s <= self->num_marks) s=self->num_marks + 1;
4516 if (self->marks == NULL)
4517 marks=(int *)malloc(s * sizeof(int));
4518 else
4519 marks=(int *)realloc(self->marks,
4520 s * sizeof(int));
4521 if (!marks) {
4522 PyErr_NoMemory();
4523 return -1;
4525 self->marks = marks;
4526 self->marks_size = s;
4529 self->marks[self->num_marks++] = self->stack->length;
4531 return 0;
4534 static int
4535 load_reduce(Unpicklerobject *self)
4537 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
4539 PDATA_POP(self->stack, arg_tup);
4540 if (! arg_tup) return -1;
4541 PDATA_POP(self->stack, callable);
4542 if (callable) {
4543 ob = Instance_New(callable, arg_tup);
4544 Py_DECREF(callable);
4546 Py_DECREF(arg_tup);
4548 if (! ob) return -1;
4550 PDATA_PUSH(self->stack, ob, -1);
4551 return 0;
4554 /* Just raises an error if we don't know the protocol specified. PROTO
4555 * is the first opcode for protocols >= 2.
4557 static int
4558 load_proto(Unpicklerobject *self)
4560 int i;
4561 char *protobyte;
4563 i = self->read_func(self, &protobyte, 1);
4564 if (i < 0)
4565 return -1;
4567 i = calc_binint(protobyte, 1);
4568 /* No point checking for < 0, since calc_binint returns an unsigned
4569 * int when chewing on 1 byte.
4571 assert(i >= 0);
4572 if (i <= HIGHEST_PROTOCOL)
4573 return 0;
4575 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4576 return -1;
4579 static PyObject *
4580 load(Unpicklerobject *self)
4582 PyObject *err = 0, *val = 0;
4583 char *s;
4585 self->num_marks = 0;
4586 if (self->stack->length) Pdata_clear(self->stack, 0);
4588 while (1) {
4589 if (self->read_func(self, &s, 1) < 0)
4590 break;
4592 switch (s[0]) {
4593 case NONE:
4594 if (load_none(self) < 0)
4595 break;
4596 continue;
4598 case BININT:
4599 if (load_binint(self) < 0)
4600 break;
4601 continue;
4603 case BININT1:
4604 if (load_binint1(self) < 0)
4605 break;
4606 continue;
4608 case BININT2:
4609 if (load_binint2(self) < 0)
4610 break;
4611 continue;
4613 case INT:
4614 if (load_int(self) < 0)
4615 break;
4616 continue;
4618 case LONG:
4619 if (load_long(self) < 0)
4620 break;
4621 continue;
4623 case LONG1:
4624 if (load_counted_long(self, 1) < 0)
4625 break;
4626 continue;
4628 case LONG4:
4629 if (load_counted_long(self, 4) < 0)
4630 break;
4631 continue;
4633 case FLOAT:
4634 if (load_float(self) < 0)
4635 break;
4636 continue;
4638 case BINFLOAT:
4639 if (load_binfloat(self) < 0)
4640 break;
4641 continue;
4643 case BINSTRING:
4644 if (load_binstring(self) < 0)
4645 break;
4646 continue;
4648 case SHORT_BINSTRING:
4649 if (load_short_binstring(self) < 0)
4650 break;
4651 continue;
4653 case STRING:
4654 if (load_string(self) < 0)
4655 break;
4656 continue;
4658 #ifdef Py_USING_UNICODE
4659 case UNICODE:
4660 if (load_unicode(self) < 0)
4661 break;
4662 continue;
4664 case BINUNICODE:
4665 if (load_binunicode(self) < 0)
4666 break;
4667 continue;
4668 #endif
4670 case EMPTY_TUPLE:
4671 if (load_counted_tuple(self, 0) < 0)
4672 break;
4673 continue;
4675 case TUPLE1:
4676 if (load_counted_tuple(self, 1) < 0)
4677 break;
4678 continue;
4680 case TUPLE2:
4681 if (load_counted_tuple(self, 2) < 0)
4682 break;
4683 continue;
4685 case TUPLE3:
4686 if (load_counted_tuple(self, 3) < 0)
4687 break;
4688 continue;
4690 case TUPLE:
4691 if (load_tuple(self) < 0)
4692 break;
4693 continue;
4695 case EMPTY_LIST:
4696 if (load_empty_list(self) < 0)
4697 break;
4698 continue;
4700 case LIST:
4701 if (load_list(self) < 0)
4702 break;
4703 continue;
4705 case EMPTY_DICT:
4706 if (load_empty_dict(self) < 0)
4707 break;
4708 continue;
4710 case DICT:
4711 if (load_dict(self) < 0)
4712 break;
4713 continue;
4715 case OBJ:
4716 if (load_obj(self) < 0)
4717 break;
4718 continue;
4720 case INST:
4721 if (load_inst(self) < 0)
4722 break;
4723 continue;
4725 case NEWOBJ:
4726 if (load_newobj(self) < 0)
4727 break;
4728 continue;
4730 case GLOBAL:
4731 if (load_global(self) < 0)
4732 break;
4733 continue;
4735 case APPEND:
4736 if (load_append(self) < 0)
4737 break;
4738 continue;
4740 case APPENDS:
4741 if (load_appends(self) < 0)
4742 break;
4743 continue;
4745 case BUILD:
4746 if (load_build(self) < 0)
4747 break;
4748 continue;
4750 case DUP:
4751 if (load_dup(self) < 0)
4752 break;
4753 continue;
4755 case BINGET:
4756 if (load_binget(self) < 0)
4757 break;
4758 continue;
4760 case LONG_BINGET:
4761 if (load_long_binget(self) < 0)
4762 break;
4763 continue;
4765 case GET:
4766 if (load_get(self) < 0)
4767 break;
4768 continue;
4770 case EXT1:
4771 if (load_extension(self, 1) < 0)
4772 break;
4773 continue;
4775 case EXT2:
4776 if (load_extension(self, 2) < 0)
4777 break;
4778 continue;
4780 case EXT4:
4781 if (load_extension(self, 4) < 0)
4782 break;
4783 continue;
4784 case MARK:
4785 if (load_mark(self) < 0)
4786 break;
4787 continue;
4789 case BINPUT:
4790 if (load_binput(self) < 0)
4791 break;
4792 continue;
4794 case LONG_BINPUT:
4795 if (load_long_binput(self) < 0)
4796 break;
4797 continue;
4799 case PUT:
4800 if (load_put(self) < 0)
4801 break;
4802 continue;
4804 case POP:
4805 if (load_pop(self) < 0)
4806 break;
4807 continue;
4809 case POP_MARK:
4810 if (load_pop_mark(self) < 0)
4811 break;
4812 continue;
4814 case SETITEM:
4815 if (load_setitem(self) < 0)
4816 break;
4817 continue;
4819 case SETITEMS:
4820 if (load_setitems(self) < 0)
4821 break;
4822 continue;
4824 case STOP:
4825 break;
4827 case PERSID:
4828 if (load_persid(self) < 0)
4829 break;
4830 continue;
4832 case BINPERSID:
4833 if (load_binpersid(self) < 0)
4834 break;
4835 continue;
4837 case REDUCE:
4838 if (load_reduce(self) < 0)
4839 break;
4840 continue;
4842 case PROTO:
4843 if (load_proto(self) < 0)
4844 break;
4845 continue;
4847 case NEWTRUE:
4848 if (load_bool(self, Py_True) < 0)
4849 break;
4850 continue;
4852 case NEWFALSE:
4853 if (load_bool(self, Py_False) < 0)
4854 break;
4855 continue;
4857 case '\0':
4858 /* end of file */
4859 PyErr_SetNone(PyExc_EOFError);
4860 break;
4862 default:
4863 cPickle_ErrFormat(UnpicklingError,
4864 "invalid load key, '%s'.",
4865 "c", s[0]);
4866 return NULL;
4869 break;
4872 if ((err = PyErr_Occurred())) {
4873 if (err == PyExc_EOFError) {
4874 PyErr_SetNone(PyExc_EOFError);
4876 return NULL;
4879 PDATA_POP(self->stack, val);
4880 return val;
4884 /* No-load functions to support noload, which is used to
4885 find persistent references. */
4887 static int
4888 noload_obj(Unpicklerobject *self)
4890 int i;
4892 if ((i = marker(self)) < 0) return -1;
4893 return Pdata_clear(self->stack, i+1);
4897 static int
4898 noload_inst(Unpicklerobject *self)
4900 int i;
4901 char *s;
4903 if ((i = marker(self)) < 0) return -1;
4904 Pdata_clear(self->stack, i);
4905 if (self->readline_func(self, &s) < 0) return -1;
4906 if (self->readline_func(self, &s) < 0) return -1;
4907 PDATA_APPEND(self->stack, Py_None, -1);
4908 return 0;
4911 static int
4912 noload_newobj(Unpicklerobject *self)
4914 PyObject *obj;
4916 PDATA_POP(self->stack, obj); /* pop argtuple */
4917 if (obj == NULL) return -1;
4918 Py_DECREF(obj);
4920 PDATA_POP(self->stack, obj); /* pop cls */
4921 if (obj == NULL) return -1;
4922 Py_DECREF(obj);
4924 PDATA_APPEND(self->stack, Py_None, -1);
4925 return 0;
4928 static int
4929 noload_global(Unpicklerobject *self)
4931 char *s;
4933 if (self->readline_func(self, &s) < 0) return -1;
4934 if (self->readline_func(self, &s) < 0) return -1;
4935 PDATA_APPEND(self->stack, Py_None,-1);
4936 return 0;
4939 static int
4940 noload_reduce(Unpicklerobject *self)
4943 if (self->stack->length < 2) return stackUnderflow();
4944 Pdata_clear(self->stack, self->stack->length-2);
4945 PDATA_APPEND(self->stack, Py_None,-1);
4946 return 0;
4949 static int
4950 noload_build(Unpicklerobject *self) {
4952 if (self->stack->length < 1) return stackUnderflow();
4953 Pdata_clear(self->stack, self->stack->length-1);
4954 return 0;
4957 static int
4958 noload_extension(Unpicklerobject *self, int nbytes)
4960 char *codebytes;
4962 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4963 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4964 PDATA_APPEND(self->stack, Py_None, -1);
4965 return 0;
4969 static PyObject *
4970 noload(Unpicklerobject *self)
4972 PyObject *err = 0, *val = 0;
4973 char *s;
4975 self->num_marks = 0;
4976 Pdata_clear(self->stack, 0);
4978 while (1) {
4979 if (self->read_func(self, &s, 1) < 0)
4980 break;
4982 switch (s[0]) {
4983 case NONE:
4984 if (load_none(self) < 0)
4985 break;
4986 continue;
4988 case BININT:
4989 if (load_binint(self) < 0)
4990 break;
4991 continue;
4993 case BININT1:
4994 if (load_binint1(self) < 0)
4995 break;
4996 continue;
4998 case BININT2:
4999 if (load_binint2(self) < 0)
5000 break;
5001 continue;
5003 case INT:
5004 if (load_int(self) < 0)
5005 break;
5006 continue;
5008 case LONG:
5009 if (load_long(self) < 0)
5010 break;
5011 continue;
5013 case LONG1:
5014 if (load_counted_long(self, 1) < 0)
5015 break;
5016 continue;
5018 case LONG4:
5019 if (load_counted_long(self, 4) < 0)
5020 break;
5021 continue;
5023 case FLOAT:
5024 if (load_float(self) < 0)
5025 break;
5026 continue;
5028 case BINFLOAT:
5029 if (load_binfloat(self) < 0)
5030 break;
5031 continue;
5033 case BINSTRING:
5034 if (load_binstring(self) < 0)
5035 break;
5036 continue;
5038 case SHORT_BINSTRING:
5039 if (load_short_binstring(self) < 0)
5040 break;
5041 continue;
5043 case STRING:
5044 if (load_string(self) < 0)
5045 break;
5046 continue;
5048 #ifdef Py_USING_UNICODE
5049 case UNICODE:
5050 if (load_unicode(self) < 0)
5051 break;
5052 continue;
5054 case BINUNICODE:
5055 if (load_binunicode(self) < 0)
5056 break;
5057 continue;
5058 #endif
5060 case EMPTY_TUPLE:
5061 if (load_counted_tuple(self, 0) < 0)
5062 break;
5063 continue;
5065 case TUPLE1:
5066 if (load_counted_tuple(self, 1) < 0)
5067 break;
5068 continue;
5070 case TUPLE2:
5071 if (load_counted_tuple(self, 2) < 0)
5072 break;
5073 continue;
5075 case TUPLE3:
5076 if (load_counted_tuple(self, 3) < 0)
5077 break;
5078 continue;
5080 case TUPLE:
5081 if (load_tuple(self) < 0)
5082 break;
5083 continue;
5085 case EMPTY_LIST:
5086 if (load_empty_list(self) < 0)
5087 break;
5088 continue;
5090 case LIST:
5091 if (load_list(self) < 0)
5092 break;
5093 continue;
5095 case EMPTY_DICT:
5096 if (load_empty_dict(self) < 0)
5097 break;
5098 continue;
5100 case DICT:
5101 if (load_dict(self) < 0)
5102 break;
5103 continue;
5105 case OBJ:
5106 if (noload_obj(self) < 0)
5107 break;
5108 continue;
5110 case INST:
5111 if (noload_inst(self) < 0)
5112 break;
5113 continue;
5115 case NEWOBJ:
5116 if (noload_newobj(self) < 0)
5117 break;
5118 continue;
5120 case GLOBAL:
5121 if (noload_global(self) < 0)
5122 break;
5123 continue;
5125 case APPEND:
5126 if (load_append(self) < 0)
5127 break;
5128 continue;
5130 case APPENDS:
5131 if (load_appends(self) < 0)
5132 break;
5133 continue;
5135 case BUILD:
5136 if (noload_build(self) < 0)
5137 break;
5138 continue;
5140 case DUP:
5141 if (load_dup(self) < 0)
5142 break;
5143 continue;
5145 case BINGET:
5146 if (load_binget(self) < 0)
5147 break;
5148 continue;
5150 case LONG_BINGET:
5151 if (load_long_binget(self) < 0)
5152 break;
5153 continue;
5155 case GET:
5156 if (load_get(self) < 0)
5157 break;
5158 continue;
5160 case EXT1:
5161 if (noload_extension(self, 1) < 0)
5162 break;
5163 continue;
5165 case EXT2:
5166 if (noload_extension(self, 2) < 0)
5167 break;
5168 continue;
5170 case EXT4:
5171 if (noload_extension(self, 4) < 0)
5172 break;
5173 continue;
5175 case MARK:
5176 if (load_mark(self) < 0)
5177 break;
5178 continue;
5180 case BINPUT:
5181 if (load_binput(self) < 0)
5182 break;
5183 continue;
5185 case LONG_BINPUT:
5186 if (load_long_binput(self) < 0)
5187 break;
5188 continue;
5190 case PUT:
5191 if (load_put(self) < 0)
5192 break;
5193 continue;
5195 case POP:
5196 if (load_pop(self) < 0)
5197 break;
5198 continue;
5200 case POP_MARK:
5201 if (load_pop_mark(self) < 0)
5202 break;
5203 continue;
5205 case SETITEM:
5206 if (load_setitem(self) < 0)
5207 break;
5208 continue;
5210 case SETITEMS:
5211 if (load_setitems(self) < 0)
5212 break;
5213 continue;
5215 case STOP:
5216 break;
5218 case PERSID:
5219 if (load_persid(self) < 0)
5220 break;
5221 continue;
5223 case BINPERSID:
5224 if (load_binpersid(self) < 0)
5225 break;
5226 continue;
5228 case REDUCE:
5229 if (noload_reduce(self) < 0)
5230 break;
5231 continue;
5233 case PROTO:
5234 if (load_proto(self) < 0)
5235 break;
5236 continue;
5238 case NEWTRUE:
5239 if (load_bool(self, Py_True) < 0)
5240 break;
5241 continue;
5243 case NEWFALSE:
5244 if (load_bool(self, Py_False) < 0)
5245 break;
5246 continue;
5247 default:
5248 cPickle_ErrFormat(UnpicklingError,
5249 "invalid load key, '%s'.",
5250 "c", s[0]);
5251 return NULL;
5254 break;
5257 if ((err = PyErr_Occurred())) {
5258 if (err == PyExc_EOFError) {
5259 PyErr_SetNone(PyExc_EOFError);
5261 return NULL;
5264 PDATA_POP(self->stack, val);
5265 return val;
5269 static PyObject *
5270 Unpickler_load(Unpicklerobject *self, PyObject *unused)
5272 return load(self);
5275 static PyObject *
5276 Unpickler_noload(Unpicklerobject *self, PyObject *unused)
5278 return noload(self);
5282 static struct PyMethodDef Unpickler_methods[] = {
5283 {"load", (PyCFunction)Unpickler_load, METH_NOARGS,
5284 PyDoc_STR("load() -- Load a pickle")
5286 {"noload", (PyCFunction)Unpickler_noload, METH_NOARGS,
5287 PyDoc_STR(
5288 "noload() -- not load a pickle, but go through most of the motions\n"
5289 "\n"
5290 "This function can be used to read past a pickle without instantiating\n"
5291 "any objects or importing any modules. It can also be used to find all\n"
5292 "persistent references without instantiating any objects or importing\n"
5293 "any modules.\n")
5295 {NULL, NULL} /* sentinel */
5299 static Unpicklerobject *
5300 newUnpicklerobject(PyObject *f)
5302 Unpicklerobject *self;
5304 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5305 return NULL;
5307 self->file = NULL;
5308 self->arg = NULL;
5309 self->stack = (Pdata*)Pdata_New();
5310 self->pers_func = NULL;
5311 self->last_string = NULL;
5312 self->marks = NULL;
5313 self->num_marks = 0;
5314 self->marks_size = 0;
5315 self->buf_size = 0;
5316 self->read = NULL;
5317 self->readline = NULL;
5318 self->find_class = NULL;
5320 if (!( self->memo = PyDict_New()))
5321 goto err;
5323 if (!self->stack)
5324 goto err;
5326 Py_INCREF(f);
5327 self->file = f;
5329 /* Set read, readline based on type of f */
5330 if (PyFile_Check(f)) {
5331 self->fp = PyFile_AsFile(f);
5332 if (self->fp == NULL) {
5333 PyErr_SetString(PyExc_ValueError,
5334 "I/O operation on closed file");
5335 goto err;
5337 self->read_func = read_file;
5338 self->readline_func = readline_file;
5340 else if (PycStringIO_InputCheck(f)) {
5341 self->fp = NULL;
5342 self->read_func = read_cStringIO;
5343 self->readline_func = readline_cStringIO;
5345 else {
5347 self->fp = NULL;
5348 self->read_func = read_other;
5349 self->readline_func = readline_other;
5351 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5352 (self->read = PyObject_GetAttr(f, read_str)))) {
5353 PyErr_Clear();
5354 PyErr_SetString( PyExc_TypeError,
5355 "argument must have 'read' and "
5356 "'readline' attributes" );
5357 goto err;
5360 PyObject_GC_Track(self);
5362 return self;
5364 err:
5365 Py_DECREF((PyObject *)self);
5366 return NULL;
5370 static PyObject *
5371 get_Unpickler(PyObject *self, PyObject *file)
5373 return (PyObject *)newUnpicklerobject(file);
5377 static void
5378 Unpickler_dealloc(Unpicklerobject *self)
5380 PyObject_GC_UnTrack((PyObject *)self);
5381 Py_XDECREF(self->readline);
5382 Py_XDECREF(self->read);
5383 Py_XDECREF(self->file);
5384 Py_XDECREF(self->memo);
5385 Py_XDECREF(self->stack);
5386 Py_XDECREF(self->pers_func);
5387 Py_XDECREF(self->arg);
5388 Py_XDECREF(self->last_string);
5389 Py_XDECREF(self->find_class);
5391 if (self->marks) {
5392 free(self->marks);
5395 if (self->buf_size) {
5396 free(self->buf);
5399 Py_TYPE(self)->tp_free((PyObject *)self);
5402 static int
5403 Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5405 Py_VISIT(self->readline);
5406 Py_VISIT(self->read);
5407 Py_VISIT(self->file);
5408 Py_VISIT(self->memo);
5409 Py_VISIT(self->stack);
5410 Py_VISIT(self->pers_func);
5411 Py_VISIT(self->arg);
5412 Py_VISIT(self->last_string);
5413 Py_VISIT(self->find_class);
5414 return 0;
5417 static int
5418 Unpickler_clear(Unpicklerobject *self)
5420 Py_CLEAR(self->readline);
5421 Py_CLEAR(self->read);
5422 Py_CLEAR(self->file);
5423 Py_CLEAR(self->memo);
5424 Py_CLEAR(self->stack);
5425 Py_CLEAR(self->pers_func);
5426 Py_CLEAR(self->arg);
5427 Py_CLEAR(self->last_string);
5428 Py_CLEAR(self->find_class);
5429 return 0;
5432 static PyObject *
5433 Unpickler_getattr(Unpicklerobject *self, char *name)
5435 if (!strcmp(name, "persistent_load")) {
5436 if (!self->pers_func) {
5437 PyErr_SetString(PyExc_AttributeError, name);
5438 return NULL;
5441 Py_INCREF(self->pers_func);
5442 return self->pers_func;
5445 if (!strcmp(name, "find_global")) {
5446 if (!self->find_class) {
5447 PyErr_SetString(PyExc_AttributeError, name);
5448 return NULL;
5451 Py_INCREF(self->find_class);
5452 return self->find_class;
5455 if (!strcmp(name, "memo")) {
5456 if (!self->memo) {
5457 PyErr_SetString(PyExc_AttributeError, name);
5458 return NULL;
5461 Py_INCREF(self->memo);
5462 return self->memo;
5465 if (!strcmp(name, "UnpicklingError")) {
5466 Py_INCREF(UnpicklingError);
5467 return UnpicklingError;
5470 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
5474 static int
5475 Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
5478 if (!strcmp(name, "persistent_load")) {
5479 Py_XDECREF(self->pers_func);
5480 self->pers_func = value;
5481 Py_XINCREF(value);
5482 return 0;
5485 if (!strcmp(name, "find_global")) {
5486 Py_XDECREF(self->find_class);
5487 self->find_class = value;
5488 Py_XINCREF(value);
5489 return 0;
5492 if (! value) {
5493 PyErr_SetString(PyExc_TypeError,
5494 "attribute deletion is not supported");
5495 return -1;
5498 if (strcmp(name, "memo") == 0) {
5499 if (!PyDict_Check(value)) {
5500 PyErr_SetString(PyExc_TypeError,
5501 "memo must be a dictionary");
5502 return -1;
5504 Py_XDECREF(self->memo);
5505 self->memo = value;
5506 Py_INCREF(value);
5507 return 0;
5510 PyErr_SetString(PyExc_AttributeError, name);
5511 return -1;
5514 /* ---------------------------------------------------------------------------
5515 * Module-level functions.
5518 /* dump(obj, file, protocol=0). */
5519 static PyObject *
5520 cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
5522 static char *kwlist[] = {"obj", "file", "protocol", NULL};
5523 PyObject *ob, *file, *res = NULL;
5524 Picklerobject *pickler = 0;
5525 int proto = 0;
5527 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5528 &ob, &file, &proto)))
5529 goto finally;
5531 if (!( pickler = newPicklerobject(file, proto)))
5532 goto finally;
5534 if (dump(pickler, ob) < 0)
5535 goto finally;
5537 Py_INCREF(Py_None);
5538 res = Py_None;
5540 finally:
5541 Py_XDECREF(pickler);
5543 return res;
5547 /* dumps(obj, protocol=0). */
5548 static PyObject *
5549 cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
5551 static char *kwlist[] = {"obj", "protocol", NULL};
5552 PyObject *ob, *file = 0, *res = NULL;
5553 Picklerobject *pickler = 0;
5554 int proto = 0;
5556 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5557 &ob, &proto)))
5558 goto finally;
5560 if (!( file = PycStringIO->NewOutput(128)))
5561 goto finally;
5563 if (!( pickler = newPicklerobject(file, proto)))
5564 goto finally;
5566 if (dump(pickler, ob) < 0)
5567 goto finally;
5569 res = PycStringIO->cgetvalue(file);
5571 finally:
5572 Py_XDECREF(pickler);
5573 Py_XDECREF(file);
5575 return res;
5579 /* load(fileobj). */
5580 static PyObject *
5581 cpm_load(PyObject *self, PyObject *ob)
5583 Unpicklerobject *unpickler = 0;
5584 PyObject *res = NULL;
5586 if (!( unpickler = newUnpicklerobject(ob)))
5587 goto finally;
5589 res = load(unpickler);
5591 finally:
5592 Py_XDECREF(unpickler);
5594 return res;
5598 /* loads(string) */
5599 static PyObject *
5600 cpm_loads(PyObject *self, PyObject *args)
5602 PyObject *ob, *file = 0, *res = NULL;
5603 Unpicklerobject *unpickler = 0;
5605 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5606 goto finally;
5608 if (!( file = PycStringIO->NewInput(ob)))
5609 goto finally;
5611 if (!( unpickler = newUnpicklerobject(file)))
5612 goto finally;
5614 res = load(unpickler);
5616 finally:
5617 Py_XDECREF(file);
5618 Py_XDECREF(unpickler);
5620 return res;
5624 PyDoc_STRVAR(Unpicklertype__doc__,
5625 "Objects that know how to unpickle");
5627 static PyTypeObject Unpicklertype = {
5628 PyVarObject_HEAD_INIT(NULL, 0)
5629 "cPickle.Unpickler", /*tp_name*/
5630 sizeof(Unpicklerobject), /*tp_basicsize*/
5632 (destructor)Unpickler_dealloc, /* tp_dealloc */
5633 0, /* tp_print */
5634 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5635 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5636 0, /* tp_compare */
5637 0, /* tp_repr */
5638 0, /* tp_as_number */
5639 0, /* tp_as_sequence */
5640 0, /* tp_as_mapping */
5641 0, /* tp_hash */
5642 0, /* tp_call */
5643 0, /* tp_str */
5644 0, /* tp_getattro */
5645 0, /* tp_setattro */
5646 0, /* tp_as_buffer */
5647 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5648 Unpicklertype__doc__, /* tp_doc */
5649 (traverseproc)Unpickler_traverse, /* tp_traverse */
5650 (inquiry)Unpickler_clear, /* tp_clear */
5653 static struct PyMethodDef cPickle_methods[] = {
5654 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5655 PyDoc_STR("dump(obj, file, protocol=0) -- "
5656 "Write an object in pickle format to the given file.\n"
5657 "\n"
5658 "See the Pickler docstring for the meaning of optional argument proto.")
5661 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5662 PyDoc_STR("dumps(obj, protocol=0) -- "
5663 "Return a string containing an object in pickle format.\n"
5664 "\n"
5665 "See the Pickler docstring for the meaning of optional argument proto.")
5668 {"load", (PyCFunction)cpm_load, METH_O,
5669 PyDoc_STR("load(file) -- Load a pickle from the given file")},
5671 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
5672 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5674 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5675 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5676 "\n"
5677 "This takes a file-like object for writing a pickle data stream.\n"
5678 "The optional proto argument tells the pickler to use the given\n"
5679 "protocol; supported protocols are 0, 1, 2. The default\n"
5680 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5681 "only protocol that can be written to a file opened in text\n"
5682 "mode and read back successfully. When using a protocol higher\n"
5683 "than 0, make sure the file is opened in binary mode, both when\n"
5684 "pickling and unpickling.)\n"
5685 "\n"
5686 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5687 "more efficient than protocol 1.\n"
5688 "\n"
5689 "Specifying a negative protocol version selects the highest\n"
5690 "protocol version supported. The higher the protocol used, the\n"
5691 "more recent the version of Python needed to read the pickle\n"
5692 "produced.\n"
5693 "\n"
5694 "The file parameter must have a write() method that accepts a single\n"
5695 "string argument. It can thus be an open file object, a StringIO\n"
5696 "object, or any other custom object that meets this interface.\n")
5699 {"Unpickler", (PyCFunction)get_Unpickler, METH_O,
5700 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5702 { NULL, NULL }
5705 static int
5706 init_stuff(PyObject *module_dict)
5708 PyObject *copyreg, *t, *r;
5710 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
5712 if (PyType_Ready(&Unpicklertype) < 0)
5713 return -1;
5714 if (PyType_Ready(&Picklertype) < 0)
5715 return -1;
5717 INIT_STR(__class__);
5718 INIT_STR(__getinitargs__);
5719 INIT_STR(__dict__);
5720 INIT_STR(__getstate__);
5721 INIT_STR(__setstate__);
5722 INIT_STR(__name__);
5723 INIT_STR(__main__);
5724 INIT_STR(__reduce__);
5725 INIT_STR(__reduce_ex__);
5726 INIT_STR(write);
5727 INIT_STR(append);
5728 INIT_STR(read);
5729 INIT_STR(readline);
5730 INIT_STR(copyreg);
5731 INIT_STR(dispatch_table);
5733 if (!( copyreg = PyImport_ImportModule("copy_reg")))
5734 return -1;
5736 /* This is special because we want to use a different
5737 one in restricted mode. */
5738 dispatch_table = PyObject_GetAttr(copyreg, dispatch_table_str);
5739 if (!dispatch_table) return -1;
5741 extension_registry = PyObject_GetAttrString(copyreg,
5742 "_extension_registry");
5743 if (!extension_registry) return -1;
5745 inverted_registry = PyObject_GetAttrString(copyreg,
5746 "_inverted_registry");
5747 if (!inverted_registry) return -1;
5749 extension_cache = PyObject_GetAttrString(copyreg,
5750 "_extension_cache");
5751 if (!extension_cache) return -1;
5753 Py_DECREF(copyreg);
5755 if (!(empty_tuple = PyTuple_New(0)))
5756 return -1;
5758 two_tuple = PyTuple_New(2);
5759 if (two_tuple == NULL)
5760 return -1;
5761 /* We use this temp container with no regard to refcounts, or to
5762 * keeping containees alive. Exempt from GC, because we don't
5763 * want anything looking at two_tuple() by magic.
5765 PyObject_GC_UnTrack(two_tuple);
5767 /* Ugh */
5768 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5769 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5770 return -1;
5772 if (!( t=PyDict_New())) return -1;
5773 if (!( r=PyRun_String(
5774 "def __str__(self):\n"
5775 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5776 Py_file_input,
5777 module_dict, t) )) return -1;
5778 Py_DECREF(r);
5780 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5781 if (!PickleError)
5782 return -1;
5784 Py_DECREF(t);
5786 PicklingError = PyErr_NewException("cPickle.PicklingError",
5787 PickleError, NULL);
5788 if (!PicklingError)
5789 return -1;
5791 if (!( t=PyDict_New())) return -1;
5792 if (!( r=PyRun_String(
5793 "def __str__(self):\n"
5794 " a=self.args\n"
5795 " a=a and type(a[0]) or '(what)'\n"
5796 " return 'Cannot pickle %s objects' % a\n"
5797 , Py_file_input,
5798 module_dict, t) )) return -1;
5799 Py_DECREF(r);
5801 if (!( UnpickleableError = PyErr_NewException(
5802 "cPickle.UnpickleableError", PicklingError, t)))
5803 return -1;
5805 Py_DECREF(t);
5807 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5808 PickleError, NULL)))
5809 return -1;
5811 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5812 UnpicklingError, NULL)))
5813 return -1;
5815 if (PyDict_SetItemString(module_dict, "PickleError",
5816 PickleError) < 0)
5817 return -1;
5819 if (PyDict_SetItemString(module_dict, "PicklingError",
5820 PicklingError) < 0)
5821 return -1;
5823 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5824 UnpicklingError) < 0)
5825 return -1;
5827 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5828 UnpickleableError) < 0)
5829 return -1;
5831 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5832 BadPickleGet) < 0)
5833 return -1;
5835 PycString_IMPORT;
5837 return 0;
5840 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5841 #define PyMODINIT_FUNC void
5842 #endif
5843 PyMODINIT_FUNC
5844 initcPickle(void)
5846 PyObject *m, *d, *di, *v, *k;
5847 Py_ssize_t i;
5848 char *rev = "1.71"; /* XXX when does this change? */
5849 PyObject *format_version;
5850 PyObject *compatible_formats;
5852 /* XXX: Should mention that the pickle module will include the C
5853 XXX: optimized implementation automatically. */
5854 if (PyErr_WarnPy3k("the cPickle module has been removed in "
5855 "Python 3.0", 2) < 0)
5856 return;
5858 Py_TYPE(&Picklertype) = &PyType_Type;
5859 Py_TYPE(&Unpicklertype) = &PyType_Type;
5860 Py_TYPE(&PdataType) = &PyType_Type;
5862 /* Initialize some pieces. We need to do this before module creation,
5863 * so we're forced to use a temporary dictionary. :(
5865 di = PyDict_New();
5866 if (!di) return;
5867 if (init_stuff(di) < 0) return;
5869 /* Create the module and add the functions */
5870 m = Py_InitModule4("cPickle", cPickle_methods,
5871 cPickle_module_documentation,
5872 (PyObject*)NULL,PYTHON_API_VERSION);
5873 if (m == NULL)
5874 return;
5876 /* Add some symbolic constants to the module */
5877 d = PyModule_GetDict(m);
5878 v = PyString_FromString(rev);
5879 PyDict_SetItemString(d, "__version__", v);
5880 Py_XDECREF(v);
5882 /* Copy data from di. Waaa. */
5883 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5884 if (PyObject_SetItem(d, k, v) < 0) {
5885 Py_DECREF(di);
5886 return;
5889 Py_DECREF(di);
5891 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5892 if (i < 0)
5893 return;
5895 /* These are purely informational; no code uses them. */
5896 /* File format version we write. */
5897 format_version = PyString_FromString("2.0");
5898 /* Format versions we can read. */
5899 compatible_formats = Py_BuildValue("[sssss]",
5900 "1.0", /* Original protocol 0 */
5901 "1.1", /* Protocol 0 + INST */
5902 "1.2", /* Original protocol 1 */
5903 "1.3", /* Protocol 1 + BINFLOAT */
5904 "2.0"); /* Original protocol 2 */
5905 PyDict_SetItemString(d, "format_version", format_version);
5906 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5907 Py_XDECREF(format_version);
5908 Py_XDECREF(compatible_formats);