Added information on function name added to LogRecord, and the 'extra' keyword parameter.
[python.git] / Modules / cPickle.c
blobcc821fdf464a4a377cfaa271e0ba460cbf06d873
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 * Pickle opcodes. These must be kept in synch with pickle.py. Extensive
22 * docs are in pickletools.py.
24 #define MARK '('
25 #define STOP '.'
26 #define POP '0'
27 #define POP_MARK '1'
28 #define DUP '2'
29 #define FLOAT 'F'
30 #define BINFLOAT 'G'
31 #define INT 'I'
32 #define BININT 'J'
33 #define BININT1 'K'
34 #define LONG 'L'
35 #define BININT2 'M'
36 #define NONE 'N'
37 #define PERSID 'P'
38 #define BINPERSID 'Q'
39 #define REDUCE 'R'
40 #define STRING 'S'
41 #define BINSTRING 'T'
42 #define SHORT_BINSTRING 'U'
43 #define UNICODE 'V'
44 #define BINUNICODE 'X'
45 #define APPEND 'a'
46 #define BUILD 'b'
47 #define GLOBAL 'c'
48 #define DICT 'd'
49 #define EMPTY_DICT '}'
50 #define APPENDS 'e'
51 #define GET 'g'
52 #define BINGET 'h'
53 #define INST 'i'
54 #define LONG_BINGET 'j'
55 #define LIST 'l'
56 #define EMPTY_LIST ']'
57 #define OBJ 'o'
58 #define PUT 'p'
59 #define BINPUT 'q'
60 #define LONG_BINPUT 'r'
61 #define SETITEM 's'
62 #define TUPLE 't'
63 #define EMPTY_TUPLE ')'
64 #define SETITEMS 'u'
66 /* Protocol 2. */
67 #define PROTO '\x80' /* identify pickle protocol */
68 #define NEWOBJ '\x81' /* build object by applying cls.__new__ to argtuple */
69 #define EXT1 '\x82' /* push object from extension registry; 1-byte index */
70 #define EXT2 '\x83' /* ditto, but 2-byte index */
71 #define EXT4 '\x84' /* ditto, but 4-byte index */
72 #define TUPLE1 '\x85' /* build 1-tuple from stack top */
73 #define TUPLE2 '\x86' /* build 2-tuple from two topmost stack items */
74 #define TUPLE3 '\x87' /* build 3-tuple from three topmost stack items */
75 #define NEWTRUE '\x88' /* push True */
76 #define NEWFALSE '\x89' /* push False */
77 #define LONG1 '\x8a' /* push long from < 256 bytes */
78 #define LONG4 '\x8b' /* push really big long */
80 /* There aren't opcodes -- they're ways to pickle bools before protocol 2,
81 * so that unpicklers written before bools were introduced unpickle them
82 * as ints, but unpicklers after can recognize that bools were intended.
83 * Note that protocol 2 added direct ways to pickle bools.
85 #undef TRUE
86 #define TRUE "I01\n"
87 #undef FALSE
88 #define FALSE "I00\n"
90 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
91 * batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
92 * break if this gets out of synch with pickle.py, but it's unclear that
93 * would help anything either.
95 #define BATCHSIZE 1000
97 static char MARKv = MARK;
99 static PyObject *PickleError;
100 static PyObject *PicklingError;
101 static PyObject *UnpickleableError;
102 static PyObject *UnpicklingError;
103 static PyObject *BadPickleGet;
105 /* As the name says, an empty tuple. */
106 static PyObject *empty_tuple;
108 /* copy_reg.dispatch_table, {type_object: pickling_function} */
109 static PyObject *dispatch_table;
111 /* For EXT[124] opcodes. */
112 /* copy_reg._extension_registry, {(module_name, function_name): code} */
113 static PyObject *extension_registry;
114 /* copy_reg._inverted_registry, {code: (module_name, function_name)} */
115 static PyObject *inverted_registry;
116 /* copy_reg._extension_cache, {code: object} */
117 static PyObject *extension_cache;
119 /* For looking up name pairs in copy_reg._extension_registry. */
120 static PyObject *two_tuple;
122 static PyObject *__class___str, *__getinitargs___str, *__dict___str,
123 *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
124 *__reduce_ex___str,
125 *write_str, *append_str,
126 *read_str, *readline_str, *__main___str, *__basicnew___str,
127 *copy_reg_str, *dispatch_table_str;
129 /*************************************************************************
130 Internal Data type for pickle data. */
132 typedef struct {
133 PyObject_HEAD
134 int length; /* number of initial slots in data currently used */
135 int size; /* number of slots in data allocated */
136 PyObject **data;
137 } Pdata;
139 static void
140 Pdata_dealloc(Pdata *self)
142 int i;
143 PyObject **p;
145 for (i = self->length, p = self->data; --i >= 0; p++) {
146 Py_DECREF(*p);
148 if (self->data)
149 free(self->data);
150 PyObject_Del(self);
153 static PyTypeObject PdataType = {
154 PyObject_HEAD_INIT(NULL) 0, "cPickle.Pdata", sizeof(Pdata), 0,
155 (destructor)Pdata_dealloc,
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0L,0L,0L,0L, ""
159 #define Pdata_Check(O) ((O)->ob_type == &PdataType)
161 static PyObject *
162 Pdata_New(void)
164 Pdata *self;
166 if (!(self = PyObject_New(Pdata, &PdataType)))
167 return NULL;
168 self->size = 8;
169 self->length = 0;
170 self->data = malloc(self->size * sizeof(PyObject*));
171 if (self->data)
172 return (PyObject*)self;
173 Py_DECREF(self);
174 return PyErr_NoMemory();
177 static int
178 stackUnderflow(void)
180 PyErr_SetString(UnpicklingError, "unpickling stack underflow");
181 return -1;
184 /* Retain only the initial clearto items. If clearto >= the current
185 * number of items, this is a (non-erroneous) NOP.
187 static int
188 Pdata_clear(Pdata *self, int clearto)
190 int i;
191 PyObject **p;
193 if (clearto < 0) return stackUnderflow();
194 if (clearto >= self->length) return 0;
196 for (i = self->length, p = self->data + clearto;
197 --i >= clearto;
198 p++) {
199 Py_DECREF(*p);
201 self->length = clearto;
203 return 0;
206 static int
207 Pdata_grow(Pdata *self)
209 int bigger;
210 size_t nbytes;
212 bigger = self->size << 1;
213 if (bigger <= 0) /* was 0, or new value overflows */
214 goto nomemory;
215 if ((int)(size_t)bigger != bigger)
216 goto nomemory;
217 nbytes = (size_t)bigger * sizeof(PyObject *);
218 if (nbytes / sizeof(PyObject *) != (size_t)bigger)
219 goto nomemory;
220 self->data = realloc(self->data, nbytes);
221 if (self->data == NULL)
222 goto nomemory;
223 self->size = bigger;
224 return 0;
226 nomemory:
227 self->size = 0;
228 PyErr_NoMemory();
229 return -1;
232 /* D is a Pdata*. Pop the topmost element and store it into V, which
233 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
234 * is raised and V is set to NULL. D and V may be evaluated several times.
236 #define PDATA_POP(D, V) { \
237 if ((D)->length) \
238 (V) = (D)->data[--((D)->length)]; \
239 else { \
240 PyErr_SetString(UnpicklingError, "bad pickle data"); \
241 (V) = NULL; \
245 /* PDATA_PUSH and PDATA_APPEND both push rvalue PyObject* O on to Pdata*
246 * D. If the Pdata stack can't be grown to hold the new value, both
247 * raise MemoryError and execute "return ER". The difference is in ownership
248 * of O after: _PUSH transfers ownership of O from the caller to the stack
249 * (no incref of O is done, and in case of error O is decrefed), while
250 * _APPEND pushes a new reference.
253 /* Push O on stack D, giving ownership of O to the stack. */
254 #define PDATA_PUSH(D, O, ER) { \
255 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
256 Pdata_grow((Pdata*)(D)) < 0) { \
257 Py_DECREF(O); \
258 return ER; \
260 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
263 /* Push O on stack D, pushing a new reference. */
264 #define PDATA_APPEND(D, O, ER) { \
265 if (((Pdata*)(D))->length == ((Pdata*)(D))->size && \
266 Pdata_grow((Pdata*)(D)) < 0) \
267 return ER; \
268 Py_INCREF(O); \
269 ((Pdata*)(D))->data[((Pdata*)(D))->length++] = (O); \
273 static PyObject *
274 Pdata_popTuple(Pdata *self, int start)
276 PyObject *r;
277 int i, j, l;
279 l = self->length-start;
280 r = PyTuple_New(l);
281 if (r == NULL)
282 return NULL;
283 for (i = start, j = 0 ; j < l; i++, j++)
284 PyTuple_SET_ITEM(r, j, self->data[i]);
286 self->length = start;
287 return r;
290 static PyObject *
291 Pdata_popList(Pdata *self, int start)
293 PyObject *r;
294 int i, j, l;
296 l=self->length-start;
297 if (!( r=PyList_New(l))) return NULL;
298 for (i=start, j=0 ; j < l; i++, j++)
299 PyList_SET_ITEM(r, j, self->data[i]);
301 self->length=start;
302 return r;
305 /*************************************************************************/
307 #define ARG_TUP(self, o) { \
308 if (self->arg || (self->arg=PyTuple_New(1))) { \
309 Py_XDECREF(PyTuple_GET_ITEM(self->arg,0)); \
310 PyTuple_SET_ITEM(self->arg,0,o); \
312 else { \
313 Py_DECREF(o); \
317 #define FREE_ARG_TUP(self) { \
318 if (self->arg->ob_refcnt > 1) { \
319 Py_DECREF(self->arg); \
320 self->arg=NULL; \
324 typedef struct Picklerobject {
325 PyObject_HEAD
326 FILE *fp;
327 PyObject *write;
328 PyObject *file;
329 PyObject *memo;
330 PyObject *arg;
331 PyObject *pers_func;
332 PyObject *inst_pers_func;
334 /* pickle protocol number, >= 0 */
335 int proto;
337 /* bool, true if proto > 0 */
338 int bin;
340 int fast; /* Fast mode doesn't save in memo, don't use if circ ref */
341 int nesting;
342 int (*write_func)(struct Picklerobject *, const char *, int);
343 char *write_buf;
344 int buf_size;
345 PyObject *dispatch_table;
346 int fast_container; /* count nested container dumps */
347 PyObject *fast_memo;
348 } Picklerobject;
350 #ifndef PY_CPICKLE_FAST_LIMIT
351 #define PY_CPICKLE_FAST_LIMIT 50
352 #endif
354 static PyTypeObject Picklertype;
356 typedef struct Unpicklerobject {
357 PyObject_HEAD
358 FILE *fp;
359 PyObject *file;
360 PyObject *readline;
361 PyObject *read;
362 PyObject *memo;
363 PyObject *arg;
364 Pdata *stack;
365 PyObject *mark;
366 PyObject *pers_func;
367 PyObject *last_string;
368 int *marks;
369 int num_marks;
370 int marks_size;
371 int (*read_func)(struct Unpicklerobject *, char **, int);
372 int (*readline_func)(struct Unpicklerobject *, char **);
373 int buf_size;
374 char *buf;
375 PyObject *find_class;
376 } Unpicklerobject;
378 static PyTypeObject Unpicklertype;
380 /* Forward decls that need the above structs */
381 static int save(Picklerobject *, PyObject *, int);
382 static int put2(Picklerobject *, PyObject *);
384 static
385 PyObject *
386 cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...)
388 va_list va;
389 PyObject *args=0, *retval=0;
390 va_start(va, format);
392 if (format) args = Py_VaBuildValue(format, va);
393 va_end(va);
394 if (format && ! args) return NULL;
395 if (stringformat && !(retval=PyString_FromString(stringformat)))
396 return NULL;
398 if (retval) {
399 if (args) {
400 PyObject *v;
401 v=PyString_Format(retval, args);
402 Py_DECREF(retval);
403 Py_DECREF(args);
404 if (! v) return NULL;
405 retval=v;
408 else
409 if (args) retval=args;
410 else {
411 PyErr_SetObject(ErrType,Py_None);
412 return NULL;
414 PyErr_SetObject(ErrType,retval);
415 Py_DECREF(retval);
416 return NULL;
419 static int
420 write_file(Picklerobject *self, const char *s, int n)
422 size_t nbyteswritten;
424 if (s == NULL) {
425 return 0;
428 Py_BEGIN_ALLOW_THREADS
429 nbyteswritten = fwrite(s, sizeof(char), n, self->fp);
430 Py_END_ALLOW_THREADS
431 if (nbyteswritten != (size_t)n) {
432 PyErr_SetFromErrno(PyExc_IOError);
433 return -1;
436 return n;
439 static int
440 write_cStringIO(Picklerobject *self, const char *s, int n)
442 if (s == NULL) {
443 return 0;
446 if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
447 return -1;
450 return n;
453 static int
454 write_none(Picklerobject *self, const char *s, int n)
456 if (s == NULL) return 0;
457 return n;
460 static int
461 write_other(Picklerobject *self, const char *s, int n)
463 PyObject *py_str = 0, *junk = 0;
465 if (s == NULL) {
466 if (!( self->buf_size )) return 0;
467 py_str = PyString_FromStringAndSize(self->write_buf,
468 self->buf_size);
469 if (!py_str)
470 return -1;
472 else {
473 if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
474 if (write_other(self, NULL, 0) < 0)
475 return -1;
478 if (n > WRITE_BUF_SIZE) {
479 if (!( py_str =
480 PyString_FromStringAndSize(s, n)))
481 return -1;
483 else {
484 memcpy(self->write_buf + self->buf_size, s, n);
485 self->buf_size += n;
486 return n;
490 if (self->write) {
491 /* object with write method */
492 ARG_TUP(self, py_str);
493 if (self->arg) {
494 junk = PyObject_Call(self->write, self->arg, NULL);
495 FREE_ARG_TUP(self);
497 if (junk) Py_DECREF(junk);
498 else return -1;
500 else
501 PDATA_PUSH(self->file, py_str, -1);
503 self->buf_size = 0;
504 return n;
508 static int
509 read_file(Unpicklerobject *self, char **s, int n)
511 size_t nbytesread;
513 if (self->buf_size == 0) {
514 int size;
516 size = ((n < 32) ? 32 : n);
517 if (!( self->buf = (char *)malloc(size))) {
518 PyErr_NoMemory();
519 return -1;
522 self->buf_size = size;
524 else if (n > self->buf_size) {
525 self->buf = (char *)realloc(self->buf, n);
526 if (!self->buf) {
527 PyErr_NoMemory();
528 return -1;
530 self->buf_size = n;
533 Py_BEGIN_ALLOW_THREADS
534 nbytesread = fread(self->buf, sizeof(char), n, self->fp);
535 Py_END_ALLOW_THREADS
536 if (nbytesread != (size_t)n) {
537 if (feof(self->fp)) {
538 PyErr_SetNone(PyExc_EOFError);
539 return -1;
542 PyErr_SetFromErrno(PyExc_IOError);
543 return -1;
546 *s = self->buf;
548 return n;
552 static int
553 readline_file(Unpicklerobject *self, char **s)
555 int i;
557 if (self->buf_size == 0) {
558 if (!( self->buf = (char *)malloc(40))) {
559 PyErr_NoMemory();
560 return -1;
562 self->buf_size = 40;
565 i = 0;
566 while (1) {
567 int bigger;
568 for (; i < (self->buf_size - 1); i++) {
569 if (feof(self->fp) ||
570 (self->buf[i] = getc(self->fp)) == '\n') {
571 self->buf[i + 1] = '\0';
572 *s = self->buf;
573 return i + 1;
576 bigger = self->buf_size << 1;
577 if (bigger <= 0) { /* overflow */
578 PyErr_NoMemory();
579 return -1;
581 self->buf = (char *)realloc(self->buf, bigger);
582 if (!self->buf) {
583 PyErr_NoMemory();
584 return -1;
586 self->buf_size = bigger;
591 static int
592 read_cStringIO(Unpicklerobject *self, char **s, int n)
594 char *ptr;
596 if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
597 PyErr_SetNone(PyExc_EOFError);
598 return -1;
601 *s = ptr;
603 return n;
607 static int
608 readline_cStringIO(Unpicklerobject *self, char **s)
610 int n;
611 char *ptr;
613 if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
614 return -1;
617 *s = ptr;
619 return n;
623 static int
624 read_other(Unpicklerobject *self, char **s, int n)
626 PyObject *bytes, *str=0;
628 if (!( bytes = PyInt_FromLong(n))) return -1;
630 ARG_TUP(self, bytes);
631 if (self->arg) {
632 str = PyObject_Call(self->read, self->arg, NULL);
633 FREE_ARG_TUP(self);
635 if (! str) return -1;
637 Py_XDECREF(self->last_string);
638 self->last_string = str;
640 if (! (*s = PyString_AsString(str))) return -1;
641 return n;
645 static int
646 readline_other(Unpicklerobject *self, char **s)
648 PyObject *str;
649 int str_size;
651 if (!( str = PyObject_CallObject(self->readline, empty_tuple))) {
652 return -1;
655 if ((str_size = PyString_Size(str)) < 0)
656 return -1;
658 Py_XDECREF(self->last_string);
659 self->last_string = str;
661 if (! (*s = PyString_AsString(str)))
662 return -1;
664 return str_size;
667 /* Copy the first n bytes from s into newly malloc'ed memory, plus a
668 * trailing 0 byte. Return a pointer to that, or NULL if out of memory.
669 * The caller is responsible for free()'ing the return value.
671 static char *
672 pystrndup(const char *s, int n)
674 char *r = (char *)malloc(n+1);
675 if (r == NULL)
676 return (char*)PyErr_NoMemory();
677 memcpy(r, s, n);
678 r[n] = 0;
679 return r;
683 static int
684 get(Picklerobject *self, PyObject *id)
686 PyObject *value, *mv;
687 long c_value;
688 char s[30];
689 size_t len;
691 if (!( mv = PyDict_GetItem(self->memo, id))) {
692 PyErr_SetObject(PyExc_KeyError, id);
693 return -1;
696 if (!( value = PyTuple_GetItem(mv, 0)))
697 return -1;
699 if (!( PyInt_Check(value))) {
700 PyErr_SetString(PicklingError, "no int where int expected in memo");
701 return -1;
703 c_value = PyInt_AS_LONG((PyIntObject*)value);
705 if (!self->bin) {
706 s[0] = GET;
707 PyOS_snprintf(s + 1, sizeof(s) - 1, "%ld\n", c_value);
708 len = strlen(s);
710 else if (Pdata_Check(self->file)) {
711 if (write_other(self, NULL, 0) < 0) return -1;
712 PDATA_APPEND(self->file, mv, -1);
713 return 0;
715 else {
716 if (c_value < 256) {
717 s[0] = BINGET;
718 s[1] = (int)(c_value & 0xff);
719 len = 2;
721 else {
722 s[0] = LONG_BINGET;
723 s[1] = (int)(c_value & 0xff);
724 s[2] = (int)((c_value >> 8) & 0xff);
725 s[3] = (int)((c_value >> 16) & 0xff);
726 s[4] = (int)((c_value >> 24) & 0xff);
727 len = 5;
731 if (self->write_func(self, s, len) < 0)
732 return -1;
734 return 0;
738 static int
739 put(Picklerobject *self, PyObject *ob)
741 if (ob->ob_refcnt < 2 || self->fast)
742 return 0;
744 return put2(self, ob);
748 static int
749 put2(Picklerobject *self, PyObject *ob)
751 char c_str[30];
752 int p;
753 size_t len;
754 int res = -1;
755 PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
757 if (self->fast)
758 return 0;
760 if ((p = PyDict_Size(self->memo)) < 0)
761 goto finally;
763 /* Make sure memo keys are positive! */
764 /* XXX Why?
765 * XXX And does "positive" really mean non-negative?
766 * XXX pickle.py starts with PUT index 0, not 1. This makes for
767 * XXX gratuitous differences between the pickling modules.
769 p++;
771 if (!( py_ob_id = PyLong_FromVoidPtr(ob)))
772 goto finally;
774 if (!( memo_len = PyInt_FromLong(p)))
775 goto finally;
777 if (!( t = PyTuple_New(2)))
778 goto finally;
780 PyTuple_SET_ITEM(t, 0, memo_len);
781 Py_INCREF(memo_len);
782 PyTuple_SET_ITEM(t, 1, ob);
783 Py_INCREF(ob);
785 if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
786 goto finally;
788 if (!self->bin) {
789 c_str[0] = PUT;
790 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%d\n", p);
791 len = strlen(c_str);
793 else if (Pdata_Check(self->file)) {
794 if (write_other(self, NULL, 0) < 0) return -1;
795 PDATA_APPEND(self->file, memo_len, -1);
796 res=0; /* Job well done ;) */
797 goto finally;
799 else {
800 if (p >= 256) {
801 c_str[0] = LONG_BINPUT;
802 c_str[1] = (int)(p & 0xff);
803 c_str[2] = (int)((p >> 8) & 0xff);
804 c_str[3] = (int)((p >> 16) & 0xff);
805 c_str[4] = (int)((p >> 24) & 0xff);
806 len = 5;
808 else {
809 c_str[0] = BINPUT;
810 c_str[1] = p;
811 len = 2;
815 if (self->write_func(self, c_str, len) < 0)
816 goto finally;
818 res = 0;
820 finally:
821 Py_XDECREF(py_ob_id);
822 Py_XDECREF(memo_len);
823 Py_XDECREF(t);
825 return res;
828 static PyObject *
829 whichmodule(PyObject *global, PyObject *global_name)
831 int i, j;
832 PyObject *module = 0, *modules_dict = 0,
833 *global_name_attr = 0, *name = 0;
835 module = PyObject_GetAttrString(global, "__module__");
836 if (module)
837 return module;
838 if (PyErr_ExceptionMatches(PyExc_AttributeError))
839 PyErr_Clear();
840 else
841 return NULL;
843 if (!( modules_dict = PySys_GetObject("modules")))
844 return NULL;
846 i = 0;
847 while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
849 if (PyObject_Compare(name, __main___str)==0) continue;
851 global_name_attr = PyObject_GetAttr(module, global_name);
852 if (!global_name_attr) {
853 if (PyErr_ExceptionMatches(PyExc_AttributeError))
854 PyErr_Clear();
855 else
856 return NULL;
857 continue;
860 if (global_name_attr != global) {
861 Py_DECREF(global_name_attr);
862 continue;
865 Py_DECREF(global_name_attr);
867 break;
870 /* The following implements the rule in pickle.py added in 1.5
871 that used __main__ if no module is found. I don't actually
872 like this rule. jlf
874 if (!j) {
875 j=1;
876 name=__main___str;
879 Py_INCREF(name);
880 return name;
884 static int
885 fast_save_enter(Picklerobject *self, PyObject *obj)
887 /* if fast_container < 0, we're doing an error exit. */
888 if (++self->fast_container >= PY_CPICKLE_FAST_LIMIT) {
889 PyObject *key = NULL;
890 if (self->fast_memo == NULL) {
891 self->fast_memo = PyDict_New();
892 if (self->fast_memo == NULL) {
893 self->fast_container = -1;
894 return 0;
897 key = PyLong_FromVoidPtr(obj);
898 if (key == NULL)
899 return 0;
900 if (PyDict_GetItem(self->fast_memo, key)) {
901 Py_DECREF(key);
902 PyErr_Format(PyExc_ValueError,
903 "fast mode: can't pickle cyclic objects "
904 "including object type %s at %p",
905 obj->ob_type->tp_name, obj);
906 self->fast_container = -1;
907 return 0;
909 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
910 Py_DECREF(key);
911 self->fast_container = -1;
912 return 0;
914 Py_DECREF(key);
916 return 1;
920 fast_save_leave(Picklerobject *self, PyObject *obj)
922 if (self->fast_container-- >= PY_CPICKLE_FAST_LIMIT) {
923 PyObject *key = PyLong_FromVoidPtr(obj);
924 if (key == NULL)
925 return 0;
926 if (PyDict_DelItem(self->fast_memo, key) < 0) {
927 Py_DECREF(key);
928 return 0;
930 Py_DECREF(key);
932 return 1;
935 static int
936 save_none(Picklerobject *self, PyObject *args)
938 static char none = NONE;
939 if (self->write_func(self, &none, 1) < 0)
940 return -1;
942 return 0;
945 static int
946 save_bool(Picklerobject *self, PyObject *args)
948 static const char *buf[2] = {FALSE, TRUE};
949 static char len[2] = {sizeof(FALSE)-1, sizeof(TRUE)-1};
950 long l = PyInt_AS_LONG((PyIntObject *)args);
952 if (self->proto >= 2) {
953 char opcode = l ? NEWTRUE : NEWFALSE;
954 if (self->write_func(self, &opcode, 1) < 0)
955 return -1;
957 else if (self->write_func(self, buf[l], len[l]) < 0)
958 return -1;
959 return 0;
962 static int
963 save_int(Picklerobject *self, PyObject *args)
965 char c_str[32];
966 long l = PyInt_AS_LONG((PyIntObject *)args);
967 int len = 0;
969 if (!self->bin
970 #if SIZEOF_LONG > 4
971 || l > 0x7fffffffL
972 || l < -0x80000000L
973 #endif
975 /* Text-mode pickle, or long too big to fit in the 4-byte
976 * signed BININT format: store as a string.
978 c_str[0] = INT;
979 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%ld\n", l);
980 if (self->write_func(self, c_str, strlen(c_str)) < 0)
981 return -1;
983 else {
984 /* Binary pickle and l fits in a signed 4-byte int. */
985 c_str[1] = (int)( l & 0xff);
986 c_str[2] = (int)((l >> 8) & 0xff);
987 c_str[3] = (int)((l >> 16) & 0xff);
988 c_str[4] = (int)((l >> 24) & 0xff);
990 if ((c_str[4] == 0) && (c_str[3] == 0)) {
991 if (c_str[2] == 0) {
992 c_str[0] = BININT1;
993 len = 2;
995 else {
996 c_str[0] = BININT2;
997 len = 3;
1000 else {
1001 c_str[0] = BININT;
1002 len = 5;
1005 if (self->write_func(self, c_str, len) < 0)
1006 return -1;
1009 return 0;
1013 static int
1014 save_long(Picklerobject *self, PyObject *args)
1016 int size;
1017 int res = -1;
1018 PyObject *repr = NULL;
1020 static char l = LONG;
1022 if (self->proto >= 2) {
1023 /* Linear-time pickling. */
1024 size_t nbits;
1025 size_t nbytes;
1026 unsigned char *pdata;
1027 char c_str[5];
1028 int i;
1029 int sign = _PyLong_Sign(args);
1031 if (sign == 0) {
1032 /* It's 0 -- an empty bytestring. */
1033 c_str[0] = LONG1;
1034 c_str[1] = 0;
1035 i = self->write_func(self, c_str, 2);
1036 if (i < 0) goto finally;
1037 res = 0;
1038 goto finally;
1040 nbits = _PyLong_NumBits(args);
1041 if (nbits == (size_t)-1 && PyErr_Occurred())
1042 goto finally;
1043 /* How many bytes do we need? There are nbits >> 3 full
1044 * bytes of data, and nbits & 7 leftover bits. If there
1045 * are any leftover bits, then we clearly need another
1046 * byte. Wnat's not so obvious is that we *probably*
1047 * need another byte even if there aren't any leftovers:
1048 * the most-significant bit of the most-significant byte
1049 * acts like a sign bit, and it's usually got a sense
1050 * opposite of the one we need. The exception is longs
1051 * of the form -(2**(8*j-1)) for j > 0. Such a long is
1052 * its own 256's-complement, so has the right sign bit
1053 * even without the extra byte. That's a pain to check
1054 * for in advance, though, so we always grab an extra
1055 * byte at the start, and cut it back later if possible.
1057 nbytes = (nbits >> 3) + 1;
1058 if ((int)nbytes < 0 || (size_t)(int)nbytes != nbytes) {
1059 PyErr_SetString(PyExc_OverflowError, "long too large "
1060 "to pickle");
1061 goto finally;
1063 repr = PyString_FromStringAndSize(NULL, (int)nbytes);
1064 if (repr == NULL) goto finally;
1065 pdata = (unsigned char *)PyString_AS_STRING(repr);
1066 i = _PyLong_AsByteArray((PyLongObject *)args,
1067 pdata, nbytes,
1068 1 /* little endian */, 1 /* signed */);
1069 if (i < 0) goto finally;
1070 /* If the long is negative, this may be a byte more than
1071 * needed. This is so iff the MSB is all redundant sign
1072 * bits.
1074 if (sign < 0 && nbytes > 1 && pdata[nbytes - 1] == 0xff &&
1075 (pdata[nbytes - 2] & 0x80) != 0)
1076 --nbytes;
1078 if (nbytes < 256) {
1079 c_str[0] = LONG1;
1080 c_str[1] = (char)nbytes;
1081 size = 2;
1083 else {
1084 c_str[0] = LONG4;
1085 size = (int)nbytes;
1086 for (i = 1; i < 5; i++) {
1087 c_str[i] = (char)(size & 0xff);
1088 size >>= 8;
1090 size = 5;
1092 i = self->write_func(self, c_str, size);
1093 if (i < 0) goto finally;
1094 i = self->write_func(self, (char *)pdata, (int)nbytes);
1095 if (i < 0) goto finally;
1096 res = 0;
1097 goto finally;
1100 /* proto < 2: write the repr and newline. This is quadratic-time
1101 * (in the number of digits), in both directions.
1103 if (!( repr = PyObject_Repr(args)))
1104 goto finally;
1106 if ((size = PyString_Size(repr)) < 0)
1107 goto finally;
1109 if (self->write_func(self, &l, 1) < 0)
1110 goto finally;
1112 if (self->write_func(self,
1113 PyString_AS_STRING((PyStringObject *)repr),
1114 size) < 0)
1115 goto finally;
1117 if (self->write_func(self, "\n", 1) < 0)
1118 goto finally;
1120 res = 0;
1122 finally:
1123 Py_XDECREF(repr);
1124 return res;
1128 static int
1129 save_float(Picklerobject *self, PyObject *args)
1131 double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
1133 if (self->bin) {
1134 char str[9];
1135 str[0] = BINFLOAT;
1136 if (_PyFloat_Pack8(x, (unsigned char *)&str[1], 0) < 0)
1137 return -1;
1138 if (self->write_func(self, str, 9) < 0)
1139 return -1;
1141 else {
1142 char c_str[250];
1143 c_str[0] = FLOAT;
1144 PyOS_snprintf(c_str + 1, sizeof(c_str) - 1, "%.17g\n", x);
1146 if (self->write_func(self, c_str, strlen(c_str)) < 0)
1147 return -1;
1150 return 0;
1154 static int
1155 save_string(Picklerobject *self, PyObject *args, int doput)
1157 int size, len;
1158 PyObject *repr=0;
1160 if ((size = PyString_Size(args)) < 0)
1161 return -1;
1163 if (!self->bin) {
1164 char *repr_str;
1166 static char string = STRING;
1168 if (!( repr = PyObject_Repr(args)))
1169 return -1;
1171 if ((len = PyString_Size(repr)) < 0)
1172 goto err;
1173 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1175 if (self->write_func(self, &string, 1) < 0)
1176 goto err;
1178 if (self->write_func(self, repr_str, len) < 0)
1179 goto err;
1181 if (self->write_func(self, "\n", 1) < 0)
1182 goto err;
1184 Py_XDECREF(repr);
1186 else {
1187 int i;
1188 char c_str[5];
1190 if ((size = PyString_Size(args)) < 0)
1191 return -1;
1193 if (size < 256) {
1194 c_str[0] = SHORT_BINSTRING;
1195 c_str[1] = size;
1196 len = 2;
1198 else {
1199 c_str[0] = BINSTRING;
1200 for (i = 1; i < 5; i++)
1201 c_str[i] = (int)(size >> ((i - 1) * 8));
1202 len = 5;
1205 if (self->write_func(self, c_str, len) < 0)
1206 return -1;
1208 if (size > 128 && Pdata_Check(self->file)) {
1209 if (write_other(self, NULL, 0) < 0) return -1;
1210 PDATA_APPEND(self->file, args, -1);
1212 else {
1213 if (self->write_func(self,
1214 PyString_AS_STRING(
1215 (PyStringObject *)args),
1216 size) < 0)
1217 return -1;
1221 if (doput)
1222 if (put(self, args) < 0)
1223 return -1;
1225 return 0;
1227 err:
1228 Py_XDECREF(repr);
1229 return -1;
1233 #ifdef Py_USING_UNICODE
1234 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
1235 backslash and newline characters to \uXXXX escapes. */
1236 static PyObject *
1237 modified_EncodeRawUnicodeEscape(const Py_UNICODE *s, int size)
1239 PyObject *repr;
1240 char *p;
1241 char *q;
1243 static const char *hexdigit = "0123456789ABCDEF";
1245 repr = PyString_FromStringAndSize(NULL, 6 * size);
1246 if (repr == NULL)
1247 return NULL;
1248 if (size == 0)
1249 return repr;
1251 p = q = PyString_AS_STRING(repr);
1252 while (size-- > 0) {
1253 Py_UNICODE ch = *s++;
1254 /* Map 16-bit characters to '\uxxxx' */
1255 if (ch >= 256 || ch == '\\' || ch == '\n') {
1256 *p++ = '\\';
1257 *p++ = 'u';
1258 *p++ = hexdigit[(ch >> 12) & 0xf];
1259 *p++ = hexdigit[(ch >> 8) & 0xf];
1260 *p++ = hexdigit[(ch >> 4) & 0xf];
1261 *p++ = hexdigit[ch & 15];
1263 /* Copy everything else as-is */
1264 else
1265 *p++ = (char) ch;
1267 *p = '\0';
1268 _PyString_Resize(&repr, p - q);
1269 return repr;
1273 static int
1274 save_unicode(Picklerobject *self, PyObject *args, int doput)
1276 int size, len;
1277 PyObject *repr=0;
1279 if (!PyUnicode_Check(args))
1280 return -1;
1282 if (!self->bin) {
1283 char *repr_str;
1284 static char string = UNICODE;
1286 repr = modified_EncodeRawUnicodeEscape(
1287 PyUnicode_AS_UNICODE(args), PyUnicode_GET_SIZE(args));
1288 if (!repr)
1289 return -1;
1291 if ((len = PyString_Size(repr)) < 0)
1292 goto err;
1293 repr_str = PyString_AS_STRING((PyStringObject *)repr);
1295 if (self->write_func(self, &string, 1) < 0)
1296 goto err;
1298 if (self->write_func(self, repr_str, len) < 0)
1299 goto err;
1301 if (self->write_func(self, "\n", 1) < 0)
1302 goto err;
1304 Py_XDECREF(repr);
1306 else {
1307 int i;
1308 char c_str[5];
1310 if (!( repr = PyUnicode_AsUTF8String(args)))
1311 return -1;
1313 if ((size = PyString_Size(repr)) < 0)
1314 goto err;
1316 c_str[0] = BINUNICODE;
1317 for (i = 1; i < 5; i++)
1318 c_str[i] = (int)(size >> ((i - 1) * 8));
1319 len = 5;
1321 if (self->write_func(self, c_str, len) < 0)
1322 goto err;
1324 if (size > 128 && Pdata_Check(self->file)) {
1325 if (write_other(self, NULL, 0) < 0)
1326 goto err;
1327 PDATA_APPEND(self->file, repr, -1);
1329 else {
1330 if (self->write_func(self, PyString_AS_STRING(repr),
1331 size) < 0)
1332 goto err;
1335 Py_DECREF(repr);
1338 if (doput)
1339 if (put(self, args) < 0)
1340 return -1;
1342 return 0;
1344 err:
1345 Py_XDECREF(repr);
1346 return -1;
1348 #endif
1350 /* A helper for save_tuple. Push the len elements in tuple t on the stack. */
1351 static int
1352 store_tuple_elements(Picklerobject *self, PyObject *t, int len)
1354 int i;
1355 int res = -1; /* guilty until proved innocent */
1357 assert(PyTuple_Size(t) == len);
1359 for (i = 0; i < len; i++) {
1360 PyObject *element = PyTuple_GET_ITEM(t, i);
1362 if (element == NULL)
1363 goto finally;
1364 if (save(self, element, 0) < 0)
1365 goto finally;
1367 res = 0;
1369 finally:
1370 return res;
1373 /* Tuples are ubiquitous in the pickle protocols, so many techniques are
1374 * used across protocols to minimize the space needed to pickle them.
1375 * Tuples are also the only builtin immutable type that can be recursive
1376 * (a tuple can be reached from itself), and that requires some subtle
1377 * magic so that it works in all cases. IOW, this is a long routine.
1379 static int
1380 save_tuple(Picklerobject *self, PyObject *args)
1382 PyObject *py_tuple_id = NULL;
1383 int len, i;
1384 int res = -1;
1386 static char tuple = TUPLE;
1387 static char pop = POP;
1388 static char pop_mark = POP_MARK;
1389 static char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
1391 if ((len = PyTuple_Size(args)) < 0)
1392 goto finally;
1394 if (len == 0) {
1395 char c_str[2];
1397 if (self->proto) {
1398 c_str[0] = EMPTY_TUPLE;
1399 len = 1;
1401 else {
1402 c_str[0] = MARK;
1403 c_str[1] = TUPLE;
1404 len = 2;
1406 if (self->write_func(self, c_str, len) >= 0)
1407 res = 0;
1408 /* Don't memoize an empty tuple. */
1409 goto finally;
1412 /* A non-empty tuple. */
1414 /* id(tuple) isn't in the memo now. If it shows up there after
1415 * saving the tuple elements, the tuple must be recursive, in
1416 * which case we'll pop everything we put on the stack, and fetch
1417 * its value from the memo.
1419 py_tuple_id = PyLong_FromVoidPtr(args);
1420 if (py_tuple_id == NULL)
1421 goto finally;
1423 if (len <= 3 && self->proto >= 2) {
1424 /* Use TUPLE{1,2,3} opcodes. */
1425 if (store_tuple_elements(self, args, len) < 0)
1426 goto finally;
1427 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1428 /* pop the len elements */
1429 for (i = 0; i < len; ++i)
1430 if (self->write_func(self, &pop, 1) < 0)
1431 goto finally;
1432 /* fetch from memo */
1433 if (get(self, py_tuple_id) < 0)
1434 goto finally;
1435 res = 0;
1436 goto finally;
1438 /* Not recursive. */
1439 if (self->write_func(self, len2opcode + len, 1) < 0)
1440 goto finally;
1441 goto memoize;
1444 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
1445 * Generate MARK elt1 elt2 ... TUPLE
1447 if (self->write_func(self, &MARKv, 1) < 0)
1448 goto finally;
1450 if (store_tuple_elements(self, args, len) < 0)
1451 goto finally;
1453 if (PyDict_GetItem(self->memo, py_tuple_id)) {
1454 /* pop the stack stuff we pushed */
1455 if (self->bin) {
1456 if (self->write_func(self, &pop_mark, 1) < 0)
1457 goto finally;
1459 else {
1460 /* Note that we pop one more than len, to remove
1461 * the MARK too.
1463 for (i = 0; i <= len; i++)
1464 if (self->write_func(self, &pop, 1) < 0)
1465 goto finally;
1467 /* fetch from memo */
1468 if (get(self, py_tuple_id) >= 0)
1469 res = 0;
1470 goto finally;
1473 /* Not recursive. */
1474 if (self->write_func(self, &tuple, 1) < 0)
1475 goto finally;
1477 memoize:
1478 if (put(self, args) >= 0)
1479 res = 0;
1481 finally:
1482 Py_XDECREF(py_tuple_id);
1483 return res;
1486 /* iter is an iterator giving items, and we batch up chunks of
1487 * MARK item item ... item APPENDS
1488 * opcode sequences. Calling code should have arranged to first create an
1489 * empty list, or list-like object, for the APPENDS to operate on.
1490 * Returns 0 on success, <0 on error.
1492 static int
1493 batch_list(Picklerobject *self, PyObject *iter)
1495 PyObject *obj;
1496 PyObject *slice[BATCHSIZE];
1497 int i, n;
1499 static char append = APPEND;
1500 static char appends = APPENDS;
1502 assert(iter != NULL);
1504 if (self->proto == 0) {
1505 /* APPENDS isn't available; do one at a time. */
1506 for (;;) {
1507 obj = PyIter_Next(iter);
1508 if (obj == NULL) {
1509 if (PyErr_Occurred())
1510 return -1;
1511 break;
1513 i = save(self, obj, 0);
1514 Py_DECREF(obj);
1515 if (i < 0)
1516 return -1;
1517 if (self->write_func(self, &append, 1) < 0)
1518 return -1;
1520 return 0;
1523 /* proto > 0: write in batches of BATCHSIZE. */
1524 do {
1525 /* Get next group of (no more than) BATCHSIZE elements. */
1526 for (n = 0; n < BATCHSIZE; ++n) {
1527 obj = PyIter_Next(iter);
1528 if (obj == NULL) {
1529 if (PyErr_Occurred())
1530 goto BatchFailed;
1531 break;
1533 slice[n] = obj;
1536 if (n > 1) {
1537 /* Pump out MARK, slice[0:n], APPENDS. */
1538 if (self->write_func(self, &MARKv, 1) < 0)
1539 goto BatchFailed;
1540 for (i = 0; i < n; ++i) {
1541 if (save(self, slice[i], 0) < 0)
1542 goto BatchFailed;
1544 if (self->write_func(self, &appends, 1) < 0)
1545 goto BatchFailed;
1547 else if (n == 1) {
1548 if (save(self, slice[0], 0) < 0)
1549 goto BatchFailed;
1550 if (self->write_func(self, &append, 1) < 0)
1551 goto BatchFailed;
1554 for (i = 0; i < n; ++i) {
1555 Py_DECREF(slice[i]);
1557 } while (n == BATCHSIZE);
1558 return 0;
1560 BatchFailed:
1561 while (--n >= 0) {
1562 Py_DECREF(slice[n]);
1564 return -1;
1567 static int
1568 save_list(Picklerobject *self, PyObject *args)
1570 int res = -1;
1571 char s[3];
1572 int len;
1573 PyObject *iter;
1575 if (self->fast && !fast_save_enter(self, args))
1576 goto finally;
1578 /* Create an empty list. */
1579 if (self->bin) {
1580 s[0] = EMPTY_LIST;
1581 len = 1;
1583 else {
1584 s[0] = MARK;
1585 s[1] = LIST;
1586 len = 2;
1589 if (self->write_func(self, s, len) < 0)
1590 goto finally;
1592 /* Get list length, and bow out early if empty. */
1593 if ((len = PyList_Size(args)) < 0)
1594 goto finally;
1596 /* Memoize. */
1597 if (len == 0) {
1598 if (put(self, args) >= 0)
1599 res = 0;
1600 goto finally;
1602 if (put2(self, args) < 0)
1603 goto finally;
1605 /* Materialize the list elements. */
1606 iter = PyObject_GetIter(args);
1607 if (iter == NULL)
1608 goto finally;
1609 res = batch_list(self, iter);
1610 Py_DECREF(iter);
1612 finally:
1613 if (self->fast && !fast_save_leave(self, args))
1614 res = -1;
1616 return res;
1620 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
1621 * MARK key value ... key value SETITEMS
1622 * opcode sequences. Calling code should have arranged to first create an
1623 * empty dict, or dict-like object, for the SETITEMS to operate on.
1624 * Returns 0 on success, <0 on error.
1626 * This is very much like batch_list(). The difference between saving
1627 * elements directly, and picking apart two-tuples, is so long-winded at
1628 * the C level, though, that attempts to combine these routines were too
1629 * ugly to bear.
1631 static int
1632 batch_dict(Picklerobject *self, PyObject *iter)
1634 PyObject *p;
1635 PyObject *slice[BATCHSIZE];
1636 int i, n;
1638 static char setitem = SETITEM;
1639 static char setitems = SETITEMS;
1641 assert(iter != NULL);
1643 if (self->proto == 0) {
1644 /* SETITEMS isn't available; do one at a time. */
1645 for (;;) {
1646 p = PyIter_Next(iter);
1647 if (p == NULL) {
1648 if (PyErr_Occurred())
1649 return -1;
1650 break;
1652 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1653 PyErr_SetString(PyExc_TypeError, "dict items "
1654 "iterator must return 2-tuples");
1655 return -1;
1657 i = save(self, PyTuple_GET_ITEM(p, 0), 0);
1658 if (i >= 0)
1659 i = save(self, PyTuple_GET_ITEM(p, 1), 0);
1660 Py_DECREF(p);
1661 if (i < 0)
1662 return -1;
1663 if (self->write_func(self, &setitem, 1) < 0)
1664 return -1;
1666 return 0;
1669 /* proto > 0: write in batches of BATCHSIZE. */
1670 do {
1671 /* Get next group of (no more than) BATCHSIZE elements. */
1672 for (n = 0; n < BATCHSIZE; ++n) {
1673 p = PyIter_Next(iter);
1674 if (p == NULL) {
1675 if (PyErr_Occurred())
1676 goto BatchFailed;
1677 break;
1679 if (!PyTuple_Check(p) || PyTuple_Size(p) != 2) {
1680 PyErr_SetString(PyExc_TypeError, "dict items "
1681 "iterator must return 2-tuples");
1682 goto BatchFailed;
1684 slice[n] = p;
1687 if (n > 1) {
1688 /* Pump out MARK, slice[0:n], SETITEMS. */
1689 if (self->write_func(self, &MARKv, 1) < 0)
1690 goto BatchFailed;
1691 for (i = 0; i < n; ++i) {
1692 p = slice[i];
1693 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1694 goto BatchFailed;
1695 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1696 goto BatchFailed;
1698 if (self->write_func(self, &setitems, 1) < 0)
1699 goto BatchFailed;
1701 else if (n == 1) {
1702 p = slice[0];
1703 if (save(self, PyTuple_GET_ITEM(p, 0), 0) < 0)
1704 goto BatchFailed;
1705 if (save(self, PyTuple_GET_ITEM(p, 1), 0) < 0)
1706 goto BatchFailed;
1707 if (self->write_func(self, &setitem, 1) < 0)
1708 goto BatchFailed;
1711 for (i = 0; i < n; ++i) {
1712 Py_DECREF(slice[i]);
1714 } while (n == BATCHSIZE);
1715 return 0;
1717 BatchFailed:
1718 while (--n >= 0) {
1719 Py_DECREF(slice[n]);
1721 return -1;
1724 static int
1725 save_dict(Picklerobject *self, PyObject *args)
1727 int res = -1;
1728 char s[3];
1729 int len;
1730 PyObject *iter;
1732 if (self->fast && !fast_save_enter(self, args))
1733 goto finally;
1735 /* Create an empty dict. */
1736 if (self->bin) {
1737 s[0] = EMPTY_DICT;
1738 len = 1;
1740 else {
1741 s[0] = MARK;
1742 s[1] = DICT;
1743 len = 2;
1746 if (self->write_func(self, s, len) < 0)
1747 goto finally;
1749 /* Get dict size, and bow out early if empty. */
1750 if ((len = PyDict_Size(args)) < 0)
1751 goto finally;
1753 if (len == 0) {
1754 if (put(self, args) >= 0)
1755 res = 0;
1756 goto finally;
1758 if (put2(self, args) < 0)
1759 goto finally;
1761 /* Materialize the dict items. */
1762 iter = PyObject_CallMethod(args, "iteritems", "()");
1763 if (iter == NULL)
1764 goto finally;
1765 res = batch_dict(self, iter);
1766 Py_DECREF(iter);
1768 finally:
1769 if (self->fast && !fast_save_leave(self, args))
1770 res = -1;
1772 return res;
1776 static int
1777 save_inst(Picklerobject *self, PyObject *args)
1779 PyObject *class = 0, *module = 0, *name = 0, *state = 0,
1780 *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
1781 char *module_str, *name_str;
1782 int module_size, name_size, res = -1;
1784 static char inst = INST, obj = OBJ, build = BUILD;
1786 if (self->fast && !fast_save_enter(self, args))
1787 goto finally;
1789 if (self->write_func(self, &MARKv, 1) < 0)
1790 goto finally;
1792 if (!( class = PyObject_GetAttr(args, __class___str)))
1793 goto finally;
1795 if (self->bin) {
1796 if (save(self, class, 0) < 0)
1797 goto finally;
1800 if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
1801 PyObject *element = 0;
1802 int i, len;
1804 if (!( class_args =
1805 PyObject_Call(getinitargs_func, empty_tuple, NULL)))
1806 goto finally;
1808 if ((len = PyObject_Size(class_args)) < 0)
1809 goto finally;
1811 for (i = 0; i < len; i++) {
1812 if (!( element = PySequence_GetItem(class_args, i)))
1813 goto finally;
1815 if (save(self, element, 0) < 0) {
1816 Py_DECREF(element);
1817 goto finally;
1820 Py_DECREF(element);
1823 else {
1824 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1825 PyErr_Clear();
1826 else
1827 goto finally;
1830 if (!self->bin) {
1831 if (!( name = ((PyClassObject *)class)->cl_name )) {
1832 PyErr_SetString(PicklingError, "class has no name");
1833 goto finally;
1836 if (!( module = whichmodule(class, name)))
1837 goto finally;
1840 if ((module_size = PyString_Size(module)) < 0 ||
1841 (name_size = PyString_Size(name)) < 0)
1842 goto finally;
1844 module_str = PyString_AS_STRING((PyStringObject *)module);
1845 name_str = PyString_AS_STRING((PyStringObject *)name);
1847 if (self->write_func(self, &inst, 1) < 0)
1848 goto finally;
1850 if (self->write_func(self, module_str, module_size) < 0)
1851 goto finally;
1853 if (self->write_func(self, "\n", 1) < 0)
1854 goto finally;
1856 if (self->write_func(self, name_str, name_size) < 0)
1857 goto finally;
1859 if (self->write_func(self, "\n", 1) < 0)
1860 goto finally;
1862 else if (self->write_func(self, &obj, 1) < 0) {
1863 goto finally;
1866 if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
1867 state = PyObject_Call(getstate_func, empty_tuple, NULL);
1868 if (!state)
1869 goto finally;
1871 else {
1872 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1873 PyErr_Clear();
1874 else
1875 goto finally;
1877 if (!( state = PyObject_GetAttr(args, __dict___str))) {
1878 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1879 PyErr_Clear();
1880 else
1881 goto finally;
1882 res = 0;
1883 goto finally;
1887 if (!PyDict_Check(state)) {
1888 if (put2(self, args) < 0)
1889 goto finally;
1891 else {
1892 if (put(self, args) < 0)
1893 goto finally;
1896 if (save(self, state, 0) < 0)
1897 goto finally;
1899 if (self->write_func(self, &build, 1) < 0)
1900 goto finally;
1902 res = 0;
1904 finally:
1905 if (self->fast && !fast_save_leave(self, args))
1906 res = -1;
1908 Py_XDECREF(module);
1909 Py_XDECREF(class);
1910 Py_XDECREF(state);
1911 Py_XDECREF(getinitargs_func);
1912 Py_XDECREF(getstate_func);
1913 Py_XDECREF(class_args);
1915 return res;
1919 static int
1920 save_global(Picklerobject *self, PyObject *args, PyObject *name)
1922 PyObject *global_name = 0, *module = 0, *mod = 0, *klass = 0;
1923 char *name_str, *module_str;
1924 int module_size, name_size, res = -1;
1926 static char global = GLOBAL;
1928 if (name) {
1929 global_name = name;
1930 Py_INCREF(global_name);
1932 else {
1933 if (!( global_name = PyObject_GetAttr(args, __name___str)))
1934 goto finally;
1937 if (!( module = whichmodule(args, global_name)))
1938 goto finally;
1940 if ((module_size = PyString_Size(module)) < 0 ||
1941 (name_size = PyString_Size(global_name)) < 0)
1942 goto finally;
1944 module_str = PyString_AS_STRING((PyStringObject *)module);
1945 name_str = PyString_AS_STRING((PyStringObject *)global_name);
1947 /* XXX This can be doing a relative import. Clearly it shouldn't,
1948 but I don't know how to stop it. :-( */
1949 mod = PyImport_ImportModule(module_str);
1950 if (mod == NULL) {
1951 cPickle_ErrFormat(PicklingError,
1952 "Can't pickle %s: import of module %s "
1953 "failed",
1954 "OS", args, module);
1955 goto finally;
1957 klass = PyObject_GetAttrString(mod, name_str);
1958 if (klass == NULL) {
1959 cPickle_ErrFormat(PicklingError,
1960 "Can't pickle %s: attribute lookup %s.%s "
1961 "failed",
1962 "OSS", args, module, global_name);
1963 goto finally;
1965 if (klass != args) {
1966 Py_DECREF(klass);
1967 cPickle_ErrFormat(PicklingError,
1968 "Can't pickle %s: it's not the same object "
1969 "as %s.%s",
1970 "OSS", args, module, global_name);
1971 goto finally;
1973 Py_DECREF(klass);
1975 if (self->proto >= 2) {
1976 /* See whether this is in the extension registry, and if
1977 * so generate an EXT opcode.
1979 PyObject *py_code; /* extension code as Python object */
1980 long code; /* extension code as C value */
1981 char c_str[5];
1982 int n;
1984 PyTuple_SET_ITEM(two_tuple, 0, module);
1985 PyTuple_SET_ITEM(two_tuple, 1, global_name);
1986 py_code = PyDict_GetItem(extension_registry, two_tuple);
1987 if (py_code == NULL)
1988 goto gen_global; /* not registered */
1990 /* Verify py_code has the right type and value. */
1991 if (!PyInt_Check(py_code)) {
1992 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
1993 "extension code %s isn't an integer",
1994 "OO", args, py_code);
1995 goto finally;
1997 code = PyInt_AS_LONG(py_code);
1998 if (code <= 0 || code > 0x7fffffffL) {
1999 cPickle_ErrFormat(PicklingError, "Can't pickle %s: "
2000 "extension code %ld is out of range",
2001 "Ol", args, code);
2002 goto finally;
2005 /* Generate an EXT opcode. */
2006 if (code <= 0xff) {
2007 c_str[0] = EXT1;
2008 c_str[1] = (char)code;
2009 n = 2;
2011 else if (code <= 0xffff) {
2012 c_str[0] = EXT2;
2013 c_str[1] = (char)(code & 0xff);
2014 c_str[2] = (char)((code >> 8) & 0xff);
2015 n = 3;
2017 else {
2018 c_str[0] = EXT4;
2019 c_str[1] = (char)(code & 0xff);
2020 c_str[2] = (char)((code >> 8) & 0xff);
2021 c_str[3] = (char)((code >> 16) & 0xff);
2022 c_str[4] = (char)((code >> 24) & 0xff);
2023 n = 5;
2026 if (self->write_func(self, c_str, n) >= 0)
2027 res = 0;
2028 goto finally; /* and don't memoize */
2031 gen_global:
2032 if (self->write_func(self, &global, 1) < 0)
2033 goto finally;
2035 if (self->write_func(self, module_str, module_size) < 0)
2036 goto finally;
2038 if (self->write_func(self, "\n", 1) < 0)
2039 goto finally;
2041 if (self->write_func(self, name_str, name_size) < 0)
2042 goto finally;
2044 if (self->write_func(self, "\n", 1) < 0)
2045 goto finally;
2047 if (put(self, args) < 0)
2048 goto finally;
2050 res = 0;
2052 finally:
2053 Py_XDECREF(module);
2054 Py_XDECREF(global_name);
2055 Py_XDECREF(mod);
2057 return res;
2060 static int
2061 save_pers(Picklerobject *self, PyObject *args, PyObject *f)
2063 PyObject *pid = 0;
2064 int size, res = -1;
2066 static char persid = PERSID, binpersid = BINPERSID;
2068 Py_INCREF(args);
2069 ARG_TUP(self, args);
2070 if (self->arg) {
2071 pid = PyObject_Call(f, self->arg, NULL);
2072 FREE_ARG_TUP(self);
2074 if (! pid) return -1;
2076 if (pid != Py_None) {
2077 if (!self->bin) {
2078 if (!PyString_Check(pid)) {
2079 PyErr_SetString(PicklingError,
2080 "persistent id must be string");
2081 goto finally;
2084 if (self->write_func(self, &persid, 1) < 0)
2085 goto finally;
2087 if ((size = PyString_Size(pid)) < 0)
2088 goto finally;
2090 if (self->write_func(self,
2091 PyString_AS_STRING(
2092 (PyStringObject *)pid),
2093 size) < 0)
2094 goto finally;
2096 if (self->write_func(self, "\n", 1) < 0)
2097 goto finally;
2099 res = 1;
2100 goto finally;
2102 else if (save(self, pid, 1) >= 0) {
2103 if (self->write_func(self, &binpersid, 1) < 0)
2104 res = -1;
2105 else
2106 res = 1;
2109 goto finally;
2112 res = 0;
2114 finally:
2115 Py_XDECREF(pid);
2117 return res;
2120 /* We're saving ob, and args is the 2-thru-5 tuple returned by the
2121 * appropriate __reduce__ method for ob.
2123 static int
2124 save_reduce(Picklerobject *self, PyObject *args, PyObject *ob)
2126 PyObject *callable;
2127 PyObject *argtup;
2128 PyObject *state = NULL;
2129 PyObject *listitems = NULL;
2130 PyObject *dictitems = NULL;
2132 int use_newobj = self->proto >= 2;
2134 static char reduce = REDUCE;
2135 static char build = BUILD;
2136 static char newobj = NEWOBJ;
2138 if (! PyArg_UnpackTuple(args, "save_reduce", 2, 5,
2139 &callable,
2140 &argtup,
2141 &state,
2142 &listitems,
2143 &dictitems))
2144 return -1;
2146 if (!PyTuple_Check(argtup)) {
2147 PyErr_SetString(PicklingError,
2148 "args from reduce() should be a tuple");
2149 return -1;
2152 if (state == Py_None)
2153 state = NULL;
2154 if (listitems == Py_None)
2155 listitems = NULL;
2156 if (dictitems == Py_None)
2157 dictitems = NULL;
2159 /* Protocol 2 special case: if callable's name is __newobj__, use
2160 * NEWOBJ. This consumes a lot of code.
2162 if (use_newobj) {
2163 PyObject *temp = PyObject_GetAttr(callable, __name___str);
2165 if (temp == NULL) {
2166 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2167 PyErr_Clear();
2168 else
2169 return -1;
2170 use_newobj = 0;
2172 else {
2173 use_newobj = PyString_Check(temp) &&
2174 strcmp(PyString_AS_STRING(temp),
2175 "__newobj__") == 0;
2176 Py_DECREF(temp);
2179 if (use_newobj) {
2180 PyObject *cls;
2181 PyObject *newargtup;
2182 int n, i;
2184 /* Sanity checks. */
2185 n = PyTuple_Size(argtup);
2186 if (n < 1) {
2187 PyErr_SetString(PicklingError, "__newobj__ arglist "
2188 "is empty");
2189 return -1;
2192 cls = PyTuple_GET_ITEM(argtup, 0);
2193 if (! PyObject_HasAttrString(cls, "__new__")) {
2194 PyErr_SetString(PicklingError, "args[0] from "
2195 "__newobj__ args has no __new__");
2196 return -1;
2199 /* XXX How could ob be NULL? */
2200 if (ob != NULL) {
2201 PyObject *ob_dot_class;
2203 ob_dot_class = PyObject_GetAttr(ob, __class___str);
2204 if (ob_dot_class == NULL) {
2205 if (PyErr_ExceptionMatches(
2206 PyExc_AttributeError))
2207 PyErr_Clear();
2208 else
2209 return -1;
2211 i = ob_dot_class != cls; /* true iff a problem */
2212 Py_XDECREF(ob_dot_class);
2213 if (i) {
2214 PyErr_SetString(PicklingError, "args[0] from "
2215 "__newobj__ args has the wrong class");
2216 return -1;
2220 /* Save the class and its __new__ arguments. */
2221 if (save(self, cls, 0) < 0)
2222 return -1;
2224 newargtup = PyTuple_New(n-1); /* argtup[1:] */
2225 if (newargtup == NULL)
2226 return -1;
2227 for (i = 1; i < n; ++i) {
2228 PyObject *temp = PyTuple_GET_ITEM(argtup, i);
2229 Py_INCREF(temp);
2230 PyTuple_SET_ITEM(newargtup, i-1, temp);
2232 i = save(self, newargtup, 0) < 0;
2233 Py_DECREF(newargtup);
2234 if (i < 0)
2235 return -1;
2237 /* Add NEWOBJ opcode. */
2238 if (self->write_func(self, &newobj, 1) < 0)
2239 return -1;
2241 else {
2242 /* Not using NEWOBJ. */
2243 if (save(self, callable, 0) < 0 ||
2244 save(self, argtup, 0) < 0 ||
2245 self->write_func(self, &reduce, 1) < 0)
2246 return -1;
2249 /* Memoize. */
2250 /* XXX How can ob be NULL? */
2251 if (ob != NULL) {
2252 if (state && !PyDict_Check(state)) {
2253 if (put2(self, ob) < 0)
2254 return -1;
2256 else if (put(self, ob) < 0)
2257 return -1;
2261 if (listitems && batch_list(self, listitems) < 0)
2262 return -1;
2264 if (dictitems && batch_dict(self, dictitems) < 0)
2265 return -1;
2267 if (state) {
2268 if (save(self, state, 0) < 0 ||
2269 self->write_func(self, &build, 1) < 0)
2270 return -1;
2273 return 0;
2276 static int
2277 save(Picklerobject *self, PyObject *args, int pers_save)
2279 PyTypeObject *type;
2280 PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0;
2281 PyObject *arg_tup;
2282 int res = -1;
2283 int tmp, size;
2285 if (self->nesting++ > Py_GetRecursionLimit()){
2286 PyErr_SetString(PyExc_RuntimeError,
2287 "maximum recursion depth exceeded");
2288 goto finally;
2291 if (!pers_save && self->pers_func) {
2292 if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
2293 res = tmp;
2294 goto finally;
2298 if (args == Py_None) {
2299 res = save_none(self, args);
2300 goto finally;
2303 type = args->ob_type;
2305 switch (type->tp_name[0]) {
2306 case 'b':
2307 if (args == Py_False || args == Py_True) {
2308 res = save_bool(self, args);
2309 goto finally;
2311 break;
2312 case 'i':
2313 if (type == &PyInt_Type) {
2314 res = save_int(self, args);
2315 goto finally;
2317 break;
2319 case 'l':
2320 if (type == &PyLong_Type) {
2321 res = save_long(self, args);
2322 goto finally;
2324 break;
2326 case 'f':
2327 if (type == &PyFloat_Type) {
2328 res = save_float(self, args);
2329 goto finally;
2331 break;
2333 case 't':
2334 if (type == &PyTuple_Type && PyTuple_Size(args) == 0) {
2335 res = save_tuple(self, args);
2336 goto finally;
2338 break;
2340 case 's':
2341 if ((type == &PyString_Type) && (PyString_GET_SIZE(args) < 2)) {
2342 res = save_string(self, args, 0);
2343 goto finally;
2346 #ifdef Py_USING_UNICODE
2347 case 'u':
2348 if ((type == &PyUnicode_Type) && (PyString_GET_SIZE(args) < 2)) {
2349 res = save_unicode(self, args, 0);
2350 goto finally;
2352 #endif
2355 if (args->ob_refcnt > 1) {
2356 if (!( py_ob_id = PyLong_FromVoidPtr(args)))
2357 goto finally;
2359 if (PyDict_GetItem(self->memo, py_ob_id)) {
2360 if (get(self, py_ob_id) < 0)
2361 goto finally;
2363 res = 0;
2364 goto finally;
2368 switch (type->tp_name[0]) {
2369 case 's':
2370 if (type == &PyString_Type) {
2371 res = save_string(self, args, 1);
2372 goto finally;
2374 break;
2376 #ifdef Py_USING_UNICODE
2377 case 'u':
2378 if (type == &PyUnicode_Type) {
2379 res = save_unicode(self, args, 1);
2380 goto finally;
2382 break;
2383 #endif
2385 case 't':
2386 if (type == &PyTuple_Type) {
2387 res = save_tuple(self, args);
2388 goto finally;
2390 if (type == &PyType_Type) {
2391 res = save_global(self, args, NULL);
2392 goto finally;
2394 break;
2396 case 'l':
2397 if (type == &PyList_Type) {
2398 res = save_list(self, args);
2399 goto finally;
2401 break;
2403 case 'd':
2404 if (type == &PyDict_Type) {
2405 res = save_dict(self, args);
2406 goto finally;
2408 break;
2410 case 'i':
2411 if (type == &PyInstance_Type) {
2412 res = save_inst(self, args);
2413 goto finally;
2415 break;
2417 case 'c':
2418 if (type == &PyClass_Type) {
2419 res = save_global(self, args, NULL);
2420 goto finally;
2422 break;
2424 case 'f':
2425 if (type == &PyFunction_Type) {
2426 res = save_global(self, args, NULL);
2427 if (res && PyErr_ExceptionMatches(PickleError)) {
2428 /* fall back to reduce */
2429 PyErr_Clear();
2430 break;
2432 goto finally;
2434 break;
2436 case 'b':
2437 if (type == &PyCFunction_Type) {
2438 res = save_global(self, args, NULL);
2439 goto finally;
2443 if (!pers_save && self->inst_pers_func) {
2444 if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
2445 res = tmp;
2446 goto finally;
2450 if (PyType_IsSubtype(type, &PyType_Type)) {
2451 res = save_global(self, args, NULL);
2452 goto finally;
2455 /* Get a reduction callable, and call it. This may come from
2456 * copy_reg.dispatch_table, the object's __reduce_ex__ method,
2457 * or the object's __reduce__ method.
2459 __reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type);
2460 if (__reduce__ != NULL) {
2461 Py_INCREF(__reduce__);
2462 Py_INCREF(args);
2463 ARG_TUP(self, args);
2464 if (self->arg) {
2465 t = PyObject_Call(__reduce__, self->arg, NULL);
2466 FREE_ARG_TUP(self);
2469 else {
2470 /* Check for a __reduce_ex__ method. */
2471 __reduce__ = PyObject_GetAttr(args, __reduce_ex___str);
2472 if (__reduce__ != NULL) {
2473 t = PyInt_FromLong(self->proto);
2474 if (t != NULL) {
2475 ARG_TUP(self, t);
2476 t = NULL;
2477 if (self->arg) {
2478 t = PyObject_Call(__reduce__,
2479 self->arg, NULL);
2480 FREE_ARG_TUP(self);
2484 else {
2485 if (PyErr_ExceptionMatches(PyExc_AttributeError))
2486 PyErr_Clear();
2487 else
2488 goto finally;
2489 /* Check for a __reduce__ method. */
2490 __reduce__ = PyObject_GetAttr(args, __reduce___str);
2491 if (__reduce__ != NULL) {
2492 t = PyObject_Call(__reduce__,
2493 empty_tuple, NULL);
2495 else {
2496 PyErr_SetObject(UnpickleableError, args);
2497 goto finally;
2502 if (t == NULL)
2503 goto finally;
2505 if (PyString_Check(t)) {
2506 res = save_global(self, args, t);
2507 goto finally;
2510 if (! PyTuple_Check(t)) {
2511 cPickle_ErrFormat(PicklingError, "Value returned by "
2512 "%s must be string or tuple",
2513 "O", __reduce__);
2514 goto finally;
2517 size = PyTuple_Size(t);
2518 if (size < 2 || size > 5) {
2519 cPickle_ErrFormat(PicklingError, "tuple returned by "
2520 "%s must contain 2 through 5 elements",
2521 "O", __reduce__);
2522 goto finally;
2525 arg_tup = PyTuple_GET_ITEM(t, 1);
2526 if (!(PyTuple_Check(arg_tup) || arg_tup == Py_None)) {
2527 cPickle_ErrFormat(PicklingError, "Second element of "
2528 "tuple returned by %s must be a tuple",
2529 "O", __reduce__);
2530 goto finally;
2533 res = save_reduce(self, t, args);
2535 finally:
2536 self->nesting--;
2537 Py_XDECREF(py_ob_id);
2538 Py_XDECREF(__reduce__);
2539 Py_XDECREF(t);
2541 return res;
2545 static int
2546 dump(Picklerobject *self, PyObject *args)
2548 static char stop = STOP;
2550 if (self->proto >= 2) {
2551 char bytes[2];
2553 bytes[0] = PROTO;
2554 assert(self->proto >= 0 && self->proto < 256);
2555 bytes[1] = (char)self->proto;
2556 if (self->write_func(self, bytes, 2) < 0)
2557 return -1;
2560 if (save(self, args, 0) < 0)
2561 return -1;
2563 if (self->write_func(self, &stop, 1) < 0)
2564 return -1;
2566 if (self->write_func(self, NULL, 0) < 0)
2567 return -1;
2569 return 0;
2572 static PyObject *
2573 Pickle_clear_memo(Picklerobject *self, PyObject *args)
2575 if (self->memo)
2576 PyDict_Clear(self->memo);
2577 Py_INCREF(Py_None);
2578 return Py_None;
2581 static PyObject *
2582 Pickle_getvalue(Picklerobject *self, PyObject *args)
2584 int l, i, rsize, ssize, clear=1, lm;
2585 long ik;
2586 PyObject *k, *r;
2587 char *s, *p, *have_get;
2588 Pdata *data;
2590 /* Can be called by Python code or C code */
2591 if (args && !PyArg_ParseTuple(args, "|i:getvalue", &clear))
2592 return NULL;
2594 /* Check to make sure we are based on a list */
2595 if (! Pdata_Check(self->file)) {
2596 PyErr_SetString(PicklingError,
2597 "Attempt to getvalue() a non-list-based pickler");
2598 return NULL;
2601 /* flush write buffer */
2602 if (write_other(self, NULL, 0) < 0) return NULL;
2604 data=(Pdata*)self->file;
2605 l=data->length;
2607 /* set up an array to hold get/put status */
2608 lm = PyDict_Size(self->memo);
2609 if (lm < 0) return NULL;
2610 lm++;
2611 have_get = malloc(lm);
2612 if (have_get == NULL) return PyErr_NoMemory();
2613 memset(have_get, 0, lm);
2615 /* Scan for gets. */
2616 for (rsize = 0, i = l; --i >= 0; ) {
2617 k = data->data[i];
2619 if (PyString_Check(k))
2620 rsize += PyString_GET_SIZE(k);
2622 else if (PyInt_Check(k)) { /* put */
2623 ik = PyInt_AS_LONG((PyIntObject*)k);
2624 if (ik >= lm || ik == 0) {
2625 PyErr_SetString(PicklingError,
2626 "Invalid get data");
2627 return NULL;
2629 if (have_get[ik]) /* with matching get */
2630 rsize += ik < 256 ? 2 : 5;
2633 else if (! (PyTuple_Check(k) &&
2634 PyTuple_GET_SIZE(k) == 2 &&
2635 PyInt_Check((k = PyTuple_GET_ITEM(k, 0))))
2637 PyErr_SetString(PicklingError,
2638 "Unexpected data in internal list");
2639 return NULL;
2642 else { /* put */
2643 ik = PyInt_AS_LONG((PyIntObject *)k);
2644 if (ik >= lm || ik == 0) {
2645 PyErr_SetString(PicklingError,
2646 "Invalid get data");
2647 return NULL;
2649 have_get[ik] = 1;
2650 rsize += ik < 256 ? 2 : 5;
2654 /* Now generate the result */
2655 r = PyString_FromStringAndSize(NULL, rsize);
2656 if (r == NULL) goto err;
2657 s = PyString_AS_STRING((PyStringObject *)r);
2659 for (i = 0; i < l; i++) {
2660 k = data->data[i];
2662 if (PyString_Check(k)) {
2663 ssize = PyString_GET_SIZE(k);
2664 if (ssize) {
2665 p=PyString_AS_STRING((PyStringObject *)k);
2666 while (--ssize >= 0)
2667 *s++ = *p++;
2671 else if (PyTuple_Check(k)) { /* get */
2672 ik = PyInt_AS_LONG((PyIntObject *)
2673 PyTuple_GET_ITEM(k, 0));
2674 if (ik < 256) {
2675 *s++ = BINGET;
2676 *s++ = (int)(ik & 0xff);
2678 else {
2679 *s++ = LONG_BINGET;
2680 *s++ = (int)(ik & 0xff);
2681 *s++ = (int)((ik >> 8) & 0xff);
2682 *s++ = (int)((ik >> 16) & 0xff);
2683 *s++ = (int)((ik >> 24) & 0xff);
2687 else { /* put */
2688 ik = PyInt_AS_LONG((PyIntObject*)k);
2690 if (have_get[ik]) { /* with matching get */
2691 if (ik < 256) {
2692 *s++ = BINPUT;
2693 *s++ = (int)(ik & 0xff);
2695 else {
2696 *s++ = LONG_BINPUT;
2697 *s++ = (int)(ik & 0xff);
2698 *s++ = (int)((ik >> 8) & 0xff);
2699 *s++ = (int)((ik >> 16) & 0xff);
2700 *s++ = (int)((ik >> 24) & 0xff);
2706 if (clear) {
2707 PyDict_Clear(self->memo);
2708 Pdata_clear(data, 0);
2711 free(have_get);
2712 return r;
2713 err:
2714 free(have_get);
2715 return NULL;
2718 static PyObject *
2719 Pickler_dump(Picklerobject *self, PyObject *args)
2721 PyObject *ob;
2722 int get=0;
2724 if (!( PyArg_ParseTuple(args, "O|i:dump", &ob, &get)))
2725 return NULL;
2727 if (dump(self, ob) < 0)
2728 return NULL;
2730 if (get) return Pickle_getvalue(self, NULL);
2732 /* XXX Why does dump() return self? */
2733 Py_INCREF(self);
2734 return (PyObject*)self;
2738 static struct PyMethodDef Pickler_methods[] =
2740 {"dump", (PyCFunction)Pickler_dump, METH_VARARGS,
2741 PyDoc_STR("dump(object) -- "
2742 "Write an object in pickle format to the object's pickle stream")},
2743 {"clear_memo", (PyCFunction)Pickle_clear_memo, METH_NOARGS,
2744 PyDoc_STR("clear_memo() -- Clear the picklers memo")},
2745 {"getvalue", (PyCFunction)Pickle_getvalue, METH_VARARGS,
2746 PyDoc_STR("getvalue() -- Finish picking a list-based pickle")},
2747 {NULL, NULL} /* sentinel */
2751 static Picklerobject *
2752 newPicklerobject(PyObject *file, int proto)
2754 Picklerobject *self;
2756 if (proto < 0)
2757 proto = HIGHEST_PROTOCOL;
2758 if (proto > HIGHEST_PROTOCOL) {
2759 PyErr_Format(PyExc_ValueError, "pickle protocol %d asked for; "
2760 "the highest available protocol is %d",
2761 proto, HIGHEST_PROTOCOL);
2762 return NULL;
2765 self = PyObject_GC_New(Picklerobject, &Picklertype);
2766 if (self == NULL)
2767 return NULL;
2768 self->proto = proto;
2769 self->bin = proto > 0;
2770 self->fp = NULL;
2771 self->write = NULL;
2772 self->memo = NULL;
2773 self->arg = NULL;
2774 self->pers_func = NULL;
2775 self->inst_pers_func = NULL;
2776 self->write_buf = NULL;
2777 self->fast = 0;
2778 self->nesting = 0;
2779 self->fast_container = 0;
2780 self->fast_memo = NULL;
2781 self->buf_size = 0;
2782 self->dispatch_table = NULL;
2784 self->file = NULL;
2785 if (file)
2786 Py_INCREF(file);
2787 else {
2788 file = Pdata_New();
2789 if (file == NULL)
2790 goto err;
2792 self->file = file;
2794 if (!( self->memo = PyDict_New()))
2795 goto err;
2797 if (PyFile_Check(file)) {
2798 self->fp = PyFile_AsFile(file);
2799 if (self->fp == NULL) {
2800 PyErr_SetString(PyExc_ValueError,
2801 "I/O operation on closed file");
2802 goto err;
2804 self->write_func = write_file;
2806 else if (PycStringIO_OutputCheck(file)) {
2807 self->write_func = write_cStringIO;
2809 else if (file == Py_None) {
2810 self->write_func = write_none;
2812 else {
2813 self->write_func = write_other;
2815 if (! Pdata_Check(file)) {
2816 self->write = PyObject_GetAttr(file, write_str);
2817 if (!self->write) {
2818 PyErr_Clear();
2819 PyErr_SetString(PyExc_TypeError,
2820 "argument must have 'write' "
2821 "attribute");
2822 goto err;
2826 self->write_buf = (char *)PyMem_Malloc(WRITE_BUF_SIZE);
2827 if (self->write_buf == NULL) {
2828 PyErr_NoMemory();
2829 goto err;
2833 if (PyEval_GetRestricted()) {
2834 /* Restricted execution, get private tables */
2835 PyObject *m = PyImport_Import(copy_reg_str);
2837 if (m == NULL)
2838 goto err;
2839 self->dispatch_table = PyObject_GetAttr(m, dispatch_table_str);
2840 Py_DECREF(m);
2841 if (self->dispatch_table == NULL)
2842 goto err;
2844 else {
2845 self->dispatch_table = dispatch_table;
2846 Py_INCREF(dispatch_table);
2848 PyObject_GC_Track(self);
2850 return self;
2852 err:
2853 Py_DECREF(self);
2854 return NULL;
2858 static PyObject *
2859 get_Pickler(PyObject *self, PyObject *args, PyObject *kwds)
2861 static const char *kwlist[] = {"file", "protocol", NULL};
2862 PyObject *file = NULL;
2863 int proto = 0;
2865 /* XXX
2866 * The documented signature is Pickler(file, protocol=0), but this
2867 * accepts Pickler() and Pickler(integer) too. The meaning then
2868 * is clear as mud, undocumented, and not supported by pickle.py.
2869 * I'm told Zope uses this, but I haven't traced into this code
2870 * far enough to figure out what it means.
2872 if (!PyArg_ParseTuple(args, "|i:Pickler", &proto)) {
2873 PyErr_Clear();
2874 proto = 0;
2875 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:Pickler",
2876 kwlist, &file, &proto))
2877 return NULL;
2879 return (PyObject *)newPicklerobject(file, proto);
2883 static void
2884 Pickler_dealloc(Picklerobject *self)
2886 PyObject_GC_UnTrack(self);
2887 Py_XDECREF(self->write);
2888 Py_XDECREF(self->memo);
2889 Py_XDECREF(self->fast_memo);
2890 Py_XDECREF(self->arg);
2891 Py_XDECREF(self->file);
2892 Py_XDECREF(self->pers_func);
2893 Py_XDECREF(self->inst_pers_func);
2894 Py_XDECREF(self->dispatch_table);
2895 PyMem_Free(self->write_buf);
2896 self->ob_type->tp_free((PyObject *)self);
2899 static int
2900 Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
2902 int err;
2903 #define VISIT(SLOT) \
2904 if (SLOT) { \
2905 err = visit((PyObject *)(SLOT), arg); \
2906 if (err) \
2907 return err; \
2909 VISIT(self->write);
2910 VISIT(self->memo);
2911 VISIT(self->fast_memo);
2912 VISIT(self->arg);
2913 VISIT(self->file);
2914 VISIT(self->pers_func);
2915 VISIT(self->inst_pers_func);
2916 VISIT(self->dispatch_table);
2917 #undef VISIT
2918 return 0;
2921 static int
2922 Pickler_clear(Picklerobject *self)
2924 #define CLEAR(SLOT) Py_XDECREF(SLOT); SLOT = NULL;
2925 CLEAR(self->write);
2926 CLEAR(self->memo);
2927 CLEAR(self->fast_memo);
2928 CLEAR(self->arg);
2929 CLEAR(self->file);
2930 CLEAR(self->pers_func);
2931 CLEAR(self->inst_pers_func);
2932 CLEAR(self->dispatch_table);
2933 #undef CLEAR
2934 return 0;
2937 static PyObject *
2938 Pickler_get_pers_func(Picklerobject *p)
2940 if (p->pers_func == NULL)
2941 PyErr_SetString(PyExc_AttributeError, "persistent_id");
2942 else
2943 Py_INCREF(p->pers_func);
2944 return p->pers_func;
2947 static int
2948 Pickler_set_pers_func(Picklerobject *p, PyObject *v)
2950 if (v == NULL) {
2951 PyErr_SetString(PyExc_TypeError,
2952 "attribute deletion is not supported");
2953 return -1;
2955 Py_XDECREF(p->pers_func);
2956 Py_INCREF(v);
2957 p->pers_func = v;
2958 return 0;
2961 static int
2962 Pickler_set_inst_pers_func(Picklerobject *p, PyObject *v)
2964 if (v == NULL) {
2965 PyErr_SetString(PyExc_TypeError,
2966 "attribute deletion is not supported");
2967 return -1;
2969 Py_XDECREF(p->inst_pers_func);
2970 Py_INCREF(v);
2971 p->inst_pers_func = v;
2972 return 0;
2975 static PyObject *
2976 Pickler_get_memo(Picklerobject *p)
2978 if (p->memo == NULL)
2979 PyErr_SetString(PyExc_AttributeError, "memo");
2980 else
2981 Py_INCREF(p->memo);
2982 return p->memo;
2985 static int
2986 Pickler_set_memo(Picklerobject *p, PyObject *v)
2988 if (v == NULL) {
2989 PyErr_SetString(PyExc_TypeError,
2990 "attribute deletion is not supported");
2991 return -1;
2993 if (!PyDict_Check(v)) {
2994 PyErr_SetString(PyExc_TypeError, "memo must be a dictionary");
2995 return -1;
2997 Py_XDECREF(p->memo);
2998 Py_INCREF(v);
2999 p->memo = v;
3000 return 0;
3003 static PyObject *
3004 Pickler_get_error(Picklerobject *p)
3006 /* why is this an attribute on the Pickler? */
3007 Py_INCREF(PicklingError);
3008 return PicklingError;
3011 static PyMemberDef Pickler_members[] = {
3012 {"binary", T_INT, offsetof(Picklerobject, bin)},
3013 {"fast", T_INT, offsetof(Picklerobject, fast)},
3014 {NULL}
3017 static PyGetSetDef Pickler_getsets[] = {
3018 {"persistent_id", (getter)Pickler_get_pers_func,
3019 (setter)Pickler_set_pers_func},
3020 {"inst_persistent_id", NULL, (setter)Pickler_set_inst_pers_func},
3021 {"memo", (getter)Pickler_get_memo, (setter)Pickler_set_memo},
3022 {"PicklingError", (getter)Pickler_get_error, NULL},
3023 {NULL}
3026 PyDoc_STRVAR(Picklertype__doc__,
3027 "Objects that know how to pickle objects\n");
3029 static PyTypeObject Picklertype = {
3030 PyObject_HEAD_INIT(NULL)
3031 0, /*ob_size*/
3032 "cPickle.Pickler", /*tp_name*/
3033 sizeof(Picklerobject), /*tp_basicsize*/
3035 (destructor)Pickler_dealloc, /* tp_dealloc */
3036 0, /* tp_print */
3037 0, /* tp_getattr */
3038 0, /* tp_setattr */
3039 0, /* tp_compare */
3040 0, /* tp_repr */
3041 0, /* tp_as_number */
3042 0, /* tp_as_sequence */
3043 0, /* tp_as_mapping */
3044 0, /* tp_hash */
3045 0, /* tp_call */
3046 0, /* tp_str */
3047 PyObject_GenericGetAttr, /* tp_getattro */
3048 PyObject_GenericSetAttr, /* tp_setattro */
3049 0, /* tp_as_buffer */
3050 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3051 Picklertype__doc__, /* tp_doc */
3052 (traverseproc)Pickler_traverse, /* tp_traverse */
3053 (inquiry)Pickler_clear, /* tp_clear */
3054 0, /* tp_richcompare */
3055 0, /* tp_weaklistoffset */
3056 0, /* tp_iter */
3057 0, /* tp_iternext */
3058 Pickler_methods, /* tp_methods */
3059 Pickler_members, /* tp_members */
3060 Pickler_getsets, /* tp_getset */
3063 static PyObject *
3064 find_class(PyObject *py_module_name, PyObject *py_global_name, PyObject *fc)
3066 PyObject *global = 0, *module;
3068 if (fc) {
3069 if (fc==Py_None) {
3070 PyErr_SetString(UnpicklingError, "Global and instance "
3071 "pickles are not supported.");
3072 return NULL;
3074 return PyObject_CallFunction(fc, "OO", py_module_name,
3075 py_global_name);
3078 module = PySys_GetObject("modules");
3079 if (module == NULL)
3080 return NULL;
3082 module = PyDict_GetItem(module, py_module_name);
3083 if (module == NULL) {
3084 module = PyImport_Import(py_module_name);
3085 if (!module)
3086 return NULL;
3087 global = PyObject_GetAttr(module, py_global_name);
3088 Py_DECREF(module);
3090 else
3091 global = PyObject_GetAttr(module, py_global_name);
3092 return global;
3095 static int
3096 marker(Unpicklerobject *self)
3098 if (self->num_marks < 1) {
3099 PyErr_SetString(UnpicklingError, "could not find MARK");
3100 return -1;
3103 return self->marks[--self->num_marks];
3107 static int
3108 load_none(Unpicklerobject *self)
3110 PDATA_APPEND(self->stack, Py_None, -1);
3111 return 0;
3114 static int
3115 bad_readline(void)
3117 PyErr_SetString(UnpicklingError, "pickle data was truncated");
3118 return -1;
3121 static int
3122 load_int(Unpicklerobject *self)
3124 PyObject *py_int = 0;
3125 char *endptr, *s;
3126 int len, res = -1;
3127 long l;
3129 if ((len = self->readline_func(self, &s)) < 0) return -1;
3130 if (len < 2) return bad_readline();
3131 if (!( s=pystrndup(s,len))) return -1;
3133 errno = 0;
3134 l = strtol(s, &endptr, 0);
3136 if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
3137 /* Hm, maybe we've got something long. Let's try reading
3138 it as a Python long object. */
3139 errno = 0;
3140 py_int = PyLong_FromString(s, NULL, 0);
3141 if (py_int == NULL) {
3142 PyErr_SetString(PyExc_ValueError,
3143 "could not convert string to int");
3144 goto finally;
3147 else {
3148 if (len == 3 && (l == 0 || l == 1)) {
3149 if (!( py_int = PyBool_FromLong(l))) goto finally;
3151 else {
3152 if (!( py_int = PyInt_FromLong(l))) goto finally;
3156 free(s);
3157 PDATA_PUSH(self->stack, py_int, -1);
3158 return 0;
3160 finally:
3161 free(s);
3163 return res;
3166 static int
3167 load_bool(Unpicklerobject *self, PyObject *boolean)
3169 assert(boolean == Py_True || boolean == Py_False);
3170 PDATA_APPEND(self->stack, boolean, -1);
3171 return 0;
3174 /* s contains x bytes of a little-endian integer. Return its value as a
3175 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
3176 * int, but when x is 4 it's a signed one. This is an historical source
3177 * of x-platform bugs.
3179 static long
3180 calc_binint(char *s, int x)
3182 unsigned char c;
3183 int i;
3184 long l;
3186 for (i = 0, l = 0L; i < x; i++) {
3187 c = (unsigned char)s[i];
3188 l |= (long)c << (i * 8);
3190 #if SIZEOF_LONG > 4
3191 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
3192 * is signed, so on a box with longs bigger than 4 bytes we need
3193 * to extend a BININT's sign bit to the full width.
3195 if (x == 4 && l & (1L << 31))
3196 l |= (~0L) << 32;
3197 #endif
3198 return l;
3202 static int
3203 load_binintx(Unpicklerobject *self, char *s, int x)
3205 PyObject *py_int = 0;
3206 long l;
3208 l = calc_binint(s, x);
3210 if (!( py_int = PyInt_FromLong(l)))
3211 return -1;
3213 PDATA_PUSH(self->stack, py_int, -1);
3214 return 0;
3218 static int
3219 load_binint(Unpicklerobject *self)
3221 char *s;
3223 if (self->read_func(self, &s, 4) < 0)
3224 return -1;
3226 return load_binintx(self, s, 4);
3230 static int
3231 load_binint1(Unpicklerobject *self)
3233 char *s;
3235 if (self->read_func(self, &s, 1) < 0)
3236 return -1;
3238 return load_binintx(self, s, 1);
3242 static int
3243 load_binint2(Unpicklerobject *self)
3245 char *s;
3247 if (self->read_func(self, &s, 2) < 0)
3248 return -1;
3250 return load_binintx(self, s, 2);
3253 static int
3254 load_long(Unpicklerobject *self)
3256 PyObject *l = 0;
3257 char *end, *s;
3258 int len, res = -1;
3260 if ((len = self->readline_func(self, &s)) < 0) return -1;
3261 if (len < 2) return bad_readline();
3262 if (!( s=pystrndup(s,len))) return -1;
3264 if (!( l = PyLong_FromString(s, &end, 0)))
3265 goto finally;
3267 free(s);
3268 PDATA_PUSH(self->stack, l, -1);
3269 return 0;
3271 finally:
3272 free(s);
3274 return res;
3277 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
3278 * data following.
3280 static int
3281 load_counted_long(Unpicklerobject *self, int size)
3283 int i;
3284 char *nbytes;
3285 unsigned char *pdata;
3286 PyObject *along;
3288 assert(size == 1 || size == 4);
3289 i = self->read_func(self, &nbytes, size);
3290 if (i < 0) return -1;
3292 size = calc_binint(nbytes, size);
3293 if (size < 0) {
3294 /* Corrupt or hostile pickle -- we never write one like
3295 * this.
3297 PyErr_SetString(UnpicklingError, "LONG pickle has negative "
3298 "byte count");
3299 return -1;
3302 if (size == 0)
3303 along = PyLong_FromLong(0L);
3304 else {
3305 /* Read the raw little-endian bytes & convert. */
3306 i = self->read_func(self, (char **)&pdata, size);
3307 if (i < 0) return -1;
3308 along = _PyLong_FromByteArray(pdata, (size_t)size,
3309 1 /* little endian */, 1 /* signed */);
3311 if (along == NULL)
3312 return -1;
3313 PDATA_PUSH(self->stack, along, -1);
3314 return 0;
3317 static int
3318 load_float(Unpicklerobject *self)
3320 PyObject *py_float = 0;
3321 char *endptr, *s;
3322 int len, res = -1;
3323 double d;
3325 if ((len = self->readline_func(self, &s)) < 0) return -1;
3326 if (len < 2) return bad_readline();
3327 if (!( s=pystrndup(s,len))) return -1;
3329 errno = 0;
3330 d = PyOS_ascii_strtod(s, &endptr);
3332 if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
3333 PyErr_SetString(PyExc_ValueError,
3334 "could not convert string to float");
3335 goto finally;
3338 if (!( py_float = PyFloat_FromDouble(d)))
3339 goto finally;
3341 free(s);
3342 PDATA_PUSH(self->stack, py_float, -1);
3343 return 0;
3345 finally:
3346 free(s);
3348 return res;
3351 static int
3352 load_binfloat(Unpicklerobject *self)
3354 PyObject *py_float;
3355 double x;
3356 char *p;
3358 if (self->read_func(self, &p, 8) < 0)
3359 return -1;
3361 x = _PyFloat_Unpack8((unsigned char *)p, 0);
3362 if (x == -1.0 && PyErr_Occurred())
3363 return -1;
3365 py_float = PyFloat_FromDouble(x);
3366 if (py_float == NULL)
3367 return -1;
3369 PDATA_PUSH(self->stack, py_float, -1);
3370 return 0;
3373 static int
3374 load_string(Unpicklerobject *self)
3376 PyObject *str = 0;
3377 int len, res = -1;
3378 char *s, *p;
3380 if ((len = self->readline_func(self, &s)) < 0) return -1;
3381 if (len < 2) return bad_readline();
3382 if (!( s=pystrndup(s,len))) return -1;
3385 /* Strip outermost quotes */
3386 while (s[len-1] <= ' ')
3387 len--;
3388 if(s[0]=='"' && s[len-1]=='"'){
3389 s[len-1] = '\0';
3390 p = s + 1 ;
3391 len -= 2;
3392 } else if(s[0]=='\'' && s[len-1]=='\''){
3393 s[len-1] = '\0';
3394 p = s + 1 ;
3395 len -= 2;
3396 } else
3397 goto insecure;
3398 /********************************************/
3400 str = PyString_DecodeEscape(p, len, NULL, 0, NULL);
3401 if (str) {
3402 PDATA_PUSH(self->stack, str, -1);
3403 res = 0;
3405 free(s);
3406 return res;
3408 insecure:
3409 free(s);
3410 PyErr_SetString(PyExc_ValueError,"insecure string pickle");
3411 return -1;
3415 static int
3416 load_binstring(Unpicklerobject *self)
3418 PyObject *py_string = 0;
3419 long l;
3420 char *s;
3422 if (self->read_func(self, &s, 4) < 0) return -1;
3424 l = calc_binint(s, 4);
3426 if (self->read_func(self, &s, l) < 0)
3427 return -1;
3429 if (!( py_string = PyString_FromStringAndSize(s, l)))
3430 return -1;
3432 PDATA_PUSH(self->stack, py_string, -1);
3433 return 0;
3437 static int
3438 load_short_binstring(Unpicklerobject *self)
3440 PyObject *py_string = 0;
3441 unsigned char l;
3442 char *s;
3444 if (self->read_func(self, &s, 1) < 0)
3445 return -1;
3447 l = (unsigned char)s[0];
3449 if (self->read_func(self, &s, l) < 0) return -1;
3451 if (!( py_string = PyString_FromStringAndSize(s, l))) return -1;
3453 PDATA_PUSH(self->stack, py_string, -1);
3454 return 0;
3458 #ifdef Py_USING_UNICODE
3459 static int
3460 load_unicode(Unpicklerobject *self)
3462 PyObject *str = 0;
3463 int len, res = -1;
3464 char *s;
3466 if ((len = self->readline_func(self, &s)) < 0) return -1;
3467 if (len < 1) return bad_readline();
3469 if (!( str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL)))
3470 goto finally;
3472 PDATA_PUSH(self->stack, str, -1);
3473 return 0;
3475 finally:
3476 return res;
3478 #endif
3481 #ifdef Py_USING_UNICODE
3482 static int
3483 load_binunicode(Unpicklerobject *self)
3485 PyObject *unicode;
3486 long l;
3487 char *s;
3489 if (self->read_func(self, &s, 4) < 0) return -1;
3491 l = calc_binint(s, 4);
3493 if (self->read_func(self, &s, l) < 0)
3494 return -1;
3496 if (!( unicode = PyUnicode_DecodeUTF8(s, l, NULL)))
3497 return -1;
3499 PDATA_PUSH(self->stack, unicode, -1);
3500 return 0;
3502 #endif
3505 static int
3506 load_tuple(Unpicklerobject *self)
3508 PyObject *tup;
3509 int i;
3511 if ((i = marker(self)) < 0) return -1;
3512 if (!( tup=Pdata_popTuple(self->stack, i))) return -1;
3513 PDATA_PUSH(self->stack, tup, -1);
3514 return 0;
3517 static int
3518 load_counted_tuple(Unpicklerobject *self, int len)
3520 PyObject *tup = PyTuple_New(len);
3522 if (tup == NULL)
3523 return -1;
3525 while (--len >= 0) {
3526 PyObject *element;
3528 PDATA_POP(self->stack, element);
3529 if (element == NULL)
3530 return -1;
3531 PyTuple_SET_ITEM(tup, len, element);
3533 PDATA_PUSH(self->stack, tup, -1);
3534 return 0;
3537 static int
3538 load_empty_list(Unpicklerobject *self)
3540 PyObject *list;
3542 if (!( list=PyList_New(0))) return -1;
3543 PDATA_PUSH(self->stack, list, -1);
3544 return 0;
3547 static int
3548 load_empty_dict(Unpicklerobject *self)
3550 PyObject *dict;
3552 if (!( dict=PyDict_New())) return -1;
3553 PDATA_PUSH(self->stack, dict, -1);
3554 return 0;
3558 static int
3559 load_list(Unpicklerobject *self)
3561 PyObject *list = 0;
3562 int i;
3564 if ((i = marker(self)) < 0) return -1;
3565 if (!( list=Pdata_popList(self->stack, i))) return -1;
3566 PDATA_PUSH(self->stack, list, -1);
3567 return 0;
3570 static int
3571 load_dict(Unpicklerobject *self)
3573 PyObject *dict, *key, *value;
3574 int i, j, k;
3576 if ((i = marker(self)) < 0) return -1;
3577 j=self->stack->length;
3579 if (!( dict = PyDict_New())) return -1;
3581 for (k = i+1; k < j; k += 2) {
3582 key =self->stack->data[k-1];
3583 value=self->stack->data[k ];
3584 if (PyDict_SetItem(dict, key, value) < 0) {
3585 Py_DECREF(dict);
3586 return -1;
3589 Pdata_clear(self->stack, i);
3590 PDATA_PUSH(self->stack, dict, -1);
3591 return 0;
3594 static PyObject *
3595 Instance_New(PyObject *cls, PyObject *args)
3597 PyObject *r = 0;
3599 if (PyClass_Check(cls)) {
3600 int l;
3602 if ((l=PyObject_Size(args)) < 0) goto err;
3603 if (!( l )) {
3604 PyObject *__getinitargs__;
3606 __getinitargs__ = PyObject_GetAttr(cls,
3607 __getinitargs___str);
3608 if (!__getinitargs__) {
3609 /* We have a class with no __getinitargs__,
3610 so bypass usual construction */
3611 PyObject *inst;
3613 PyErr_Clear();
3614 if (!( inst=PyInstance_NewRaw(cls, NULL)))
3615 goto err;
3616 return inst;
3618 Py_DECREF(__getinitargs__);
3621 if ((r=PyInstance_New(cls, args, NULL))) return r;
3622 else goto err;
3625 if ((r=PyObject_CallObject(cls, args))) return r;
3627 err:
3629 PyObject *tp, *v, *tb;
3631 PyErr_Fetch(&tp, &v, &tb);
3632 if ((r=PyTuple_Pack(3,v,cls,args))) {
3633 Py_XDECREF(v);
3634 v=r;
3636 PyErr_Restore(tp,v,tb);
3638 return NULL;
3642 static int
3643 load_obj(Unpicklerobject *self)
3645 PyObject *class, *tup, *obj=0;
3646 int i;
3648 if ((i = marker(self)) < 0) return -1;
3649 if (!( tup=Pdata_popTuple(self->stack, i+1))) return -1;
3650 PDATA_POP(self->stack, class);
3651 if (class) {
3652 obj = Instance_New(class, tup);
3653 Py_DECREF(class);
3655 Py_DECREF(tup);
3657 if (! obj) return -1;
3658 PDATA_PUSH(self->stack, obj, -1);
3659 return 0;
3663 static int
3664 load_inst(Unpicklerobject *self)
3666 PyObject *tup, *class=0, *obj=0, *module_name, *class_name;
3667 int i, len;
3668 char *s;
3670 if ((i = marker(self)) < 0) return -1;
3672 if ((len = self->readline_func(self, &s)) < 0) return -1;
3673 if (len < 2) return bad_readline();
3674 module_name = PyString_FromStringAndSize(s, len - 1);
3675 if (!module_name) return -1;
3677 if ((len = self->readline_func(self, &s)) >= 0) {
3678 if (len < 2) return bad_readline();
3679 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3680 class = find_class(module_name, class_name,
3681 self->find_class);
3682 Py_DECREF(class_name);
3685 Py_DECREF(module_name);
3687 if (! class) return -1;
3689 if ((tup=Pdata_popTuple(self->stack, i))) {
3690 obj = Instance_New(class, tup);
3691 Py_DECREF(tup);
3693 Py_DECREF(class);
3695 if (! obj) return -1;
3697 PDATA_PUSH(self->stack, obj, -1);
3698 return 0;
3701 static int
3702 load_newobj(Unpicklerobject *self)
3704 PyObject *args = NULL;
3705 PyObject *clsraw = NULL;
3706 PyTypeObject *cls; /* clsraw cast to its true type */
3707 PyObject *obj;
3709 /* Stack is ... cls argtuple, and we want to call
3710 * cls.__new__(cls, *argtuple).
3712 PDATA_POP(self->stack, args);
3713 if (args == NULL) goto Fail;
3714 if (! PyTuple_Check(args)) {
3715 PyErr_SetString(UnpicklingError, "NEWOBJ expected an arg "
3716 "tuple.");
3717 goto Fail;
3720 PDATA_POP(self->stack, clsraw);
3721 cls = (PyTypeObject *)clsraw;
3722 if (cls == NULL) goto Fail;
3723 if (! PyType_Check(cls)) {
3724 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3725 "isn't a type object");
3726 goto Fail;
3728 if (cls->tp_new == NULL) {
3729 PyErr_SetString(UnpicklingError, "NEWOBJ class argument "
3730 "has NULL tp_new");
3731 goto Fail;
3734 /* Call __new__. */
3735 obj = cls->tp_new(cls, args, NULL);
3736 if (obj == NULL) goto Fail;
3738 Py_DECREF(args);
3739 Py_DECREF(clsraw);
3740 PDATA_PUSH(self->stack, obj, -1);
3741 return 0;
3743 Fail:
3744 Py_XDECREF(args);
3745 Py_XDECREF(clsraw);
3746 return -1;
3749 static int
3750 load_global(Unpicklerobject *self)
3752 PyObject *class = 0, *module_name = 0, *class_name = 0;
3753 int len;
3754 char *s;
3756 if ((len = self->readline_func(self, &s)) < 0) return -1;
3757 if (len < 2) return bad_readline();
3758 module_name = PyString_FromStringAndSize(s, len - 1);
3759 if (!module_name) return -1;
3761 if ((len = self->readline_func(self, &s)) >= 0) {
3762 if (len < 2) {
3763 Py_DECREF(module_name);
3764 return bad_readline();
3766 if ((class_name = PyString_FromStringAndSize(s, len - 1))) {
3767 class = find_class(module_name, class_name,
3768 self->find_class);
3769 Py_DECREF(class_name);
3772 Py_DECREF(module_name);
3774 if (! class) return -1;
3775 PDATA_PUSH(self->stack, class, -1);
3776 return 0;
3780 static int
3781 load_persid(Unpicklerobject *self)
3783 PyObject *pid = 0;
3784 int len;
3785 char *s;
3787 if (self->pers_func) {
3788 if ((len = self->readline_func(self, &s)) < 0) return -1;
3789 if (len < 2) return bad_readline();
3791 pid = PyString_FromStringAndSize(s, len - 1);
3792 if (!pid) return -1;
3794 if (PyList_Check(self->pers_func)) {
3795 if (PyList_Append(self->pers_func, pid) < 0) {
3796 Py_DECREF(pid);
3797 return -1;
3800 else {
3801 ARG_TUP(self, pid);
3802 if (self->arg) {
3803 pid = PyObject_Call(self->pers_func, self->arg,
3804 NULL);
3805 FREE_ARG_TUP(self);
3809 if (! pid) return -1;
3811 PDATA_PUSH(self->stack, pid, -1);
3812 return 0;
3814 else {
3815 PyErr_SetString(UnpicklingError,
3816 "A load persistent id instruction was encountered,\n"
3817 "but no persistent_load function was specified.");
3818 return -1;
3822 static int
3823 load_binpersid(Unpicklerobject *self)
3825 PyObject *pid = 0;
3827 if (self->pers_func) {
3828 PDATA_POP(self->stack, pid);
3829 if (! pid) return -1;
3831 if (PyList_Check(self->pers_func)) {
3832 if (PyList_Append(self->pers_func, pid) < 0) {
3833 Py_DECREF(pid);
3834 return -1;
3837 else {
3838 ARG_TUP(self, pid);
3839 if (self->arg) {
3840 pid = PyObject_Call(self->pers_func, self->arg,
3841 NULL);
3842 FREE_ARG_TUP(self);
3844 if (! pid) return -1;
3847 PDATA_PUSH(self->stack, pid, -1);
3848 return 0;
3850 else {
3851 PyErr_SetString(UnpicklingError,
3852 "A load persistent id instruction was encountered,\n"
3853 "but no persistent_load function was specified.");
3854 return -1;
3859 static int
3860 load_pop(Unpicklerobject *self)
3862 int len;
3864 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
3866 /* Note that we split the (pickle.py) stack into two stacks,
3867 an object stack and a mark stack. We have to be clever and
3868 pop the right one. We do this by looking at the top of the
3869 mark stack.
3872 if ((self->num_marks > 0) &&
3873 (self->marks[self->num_marks - 1] == len))
3874 self->num_marks--;
3875 else {
3876 len--;
3877 Py_DECREF(self->stack->data[len]);
3878 self->stack->length=len;
3881 return 0;
3885 static int
3886 load_pop_mark(Unpicklerobject *self)
3888 int i;
3890 if ((i = marker(self)) < 0)
3891 return -1;
3893 Pdata_clear(self->stack, i);
3895 return 0;
3899 static int
3900 load_dup(Unpicklerobject *self)
3902 PyObject *last;
3903 int len;
3905 if ((len = self->stack->length) <= 0) return stackUnderflow();
3906 last=self->stack->data[len-1];
3907 Py_INCREF(last);
3908 PDATA_PUSH(self->stack, last, -1);
3909 return 0;
3913 static int
3914 load_get(Unpicklerobject *self)
3916 PyObject *py_str = 0, *value = 0;
3917 int len;
3918 char *s;
3919 int rc;
3921 if ((len = self->readline_func(self, &s)) < 0) return -1;
3922 if (len < 2) return bad_readline();
3924 if (!( py_str = PyString_FromStringAndSize(s, len - 1))) return -1;
3926 value = PyDict_GetItem(self->memo, py_str);
3927 if (! value) {
3928 PyErr_SetObject(BadPickleGet, py_str);
3929 rc = -1;
3931 else {
3932 PDATA_APPEND(self->stack, value, -1);
3933 rc = 0;
3936 Py_DECREF(py_str);
3937 return rc;
3941 static int
3942 load_binget(Unpicklerobject *self)
3944 PyObject *py_key = 0, *value = 0;
3945 unsigned char key;
3946 char *s;
3947 int rc;
3949 if (self->read_func(self, &s, 1) < 0) return -1;
3951 key = (unsigned char)s[0];
3952 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3954 value = PyDict_GetItem(self->memo, py_key);
3955 if (! value) {
3956 PyErr_SetObject(BadPickleGet, py_key);
3957 rc = -1;
3959 else {
3960 PDATA_APPEND(self->stack, value, -1);
3961 rc = 0;
3964 Py_DECREF(py_key);
3965 return rc;
3969 static int
3970 load_long_binget(Unpicklerobject *self)
3972 PyObject *py_key = 0, *value = 0;
3973 unsigned char c;
3974 char *s;
3975 long key;
3976 int rc;
3978 if (self->read_func(self, &s, 4) < 0) return -1;
3980 c = (unsigned char)s[0];
3981 key = (long)c;
3982 c = (unsigned char)s[1];
3983 key |= (long)c << 8;
3984 c = (unsigned char)s[2];
3985 key |= (long)c << 16;
3986 c = (unsigned char)s[3];
3987 key |= (long)c << 24;
3989 if (!( py_key = PyInt_FromLong((long)key))) return -1;
3991 value = PyDict_GetItem(self->memo, py_key);
3992 if (! value) {
3993 PyErr_SetObject(BadPickleGet, py_key);
3994 rc = -1;
3996 else {
3997 PDATA_APPEND(self->stack, value, -1);
3998 rc = 0;
4001 Py_DECREF(py_key);
4002 return rc;
4005 /* Push an object from the extension registry (EXT[124]). nbytes is
4006 * the number of bytes following the opcode, holding the index (code) value.
4008 static int
4009 load_extension(Unpicklerobject *self, int nbytes)
4011 char *codebytes; /* the nbytes bytes after the opcode */
4012 long code; /* calc_binint returns long */
4013 PyObject *py_code; /* code as a Python int */
4014 PyObject *obj; /* the object to push */
4015 PyObject *pair; /* (module_name, class_name) */
4016 PyObject *module_name, *class_name;
4018 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4019 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4020 code = calc_binint(codebytes, nbytes);
4021 if (code <= 0) { /* note that 0 is forbidden */
4022 /* Corrupt or hostile pickle. */
4023 PyErr_SetString(UnpicklingError, "EXT specifies code <= 0");
4024 return -1;
4027 /* Look for the code in the cache. */
4028 py_code = PyInt_FromLong(code);
4029 if (py_code == NULL) return -1;
4030 obj = PyDict_GetItem(extension_cache, py_code);
4031 if (obj != NULL) {
4032 /* Bingo. */
4033 Py_DECREF(py_code);
4034 PDATA_APPEND(self->stack, obj, -1);
4035 return 0;
4038 /* Look up the (module_name, class_name) pair. */
4039 pair = PyDict_GetItem(inverted_registry, py_code);
4040 if (pair == NULL) {
4041 Py_DECREF(py_code);
4042 PyErr_Format(PyExc_ValueError, "unregistered extension "
4043 "code %ld", code);
4044 return -1;
4046 /* Since the extension registry is manipulable via Python code,
4047 * confirm that pair is really a 2-tuple of strings.
4049 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
4050 !PyString_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
4051 !PyString_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
4052 Py_DECREF(py_code);
4053 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
4054 "isn't a 2-tuple of strings", code);
4055 return -1;
4057 /* Load the object. */
4058 obj = find_class(module_name, class_name, self->find_class);
4059 if (obj == NULL) {
4060 Py_DECREF(py_code);
4061 return -1;
4063 /* Cache code -> obj. */
4064 code = PyDict_SetItem(extension_cache, py_code, obj);
4065 Py_DECREF(py_code);
4066 if (code < 0) {
4067 Py_DECREF(obj);
4068 return -1;
4070 PDATA_PUSH(self->stack, obj, -1);
4071 return 0;
4074 static int
4075 load_put(Unpicklerobject *self)
4077 PyObject *py_str = 0, *value = 0;
4078 int len, l;
4079 char *s;
4081 if ((l = self->readline_func(self, &s)) < 0) return -1;
4082 if (l < 2) return bad_readline();
4083 if (!( len=self->stack->length )) return stackUnderflow();
4084 if (!( py_str = PyString_FromStringAndSize(s, l - 1))) return -1;
4085 value=self->stack->data[len-1];
4086 l=PyDict_SetItem(self->memo, py_str, value);
4087 Py_DECREF(py_str);
4088 return l;
4092 static int
4093 load_binput(Unpicklerobject *self)
4095 PyObject *py_key = 0, *value = 0;
4096 unsigned char key;
4097 char *s;
4098 int len;
4100 if (self->read_func(self, &s, 1) < 0) return -1;
4101 if (!( (len=self->stack->length) > 0 )) return stackUnderflow();
4103 key = (unsigned char)s[0];
4105 if (!( py_key = PyInt_FromLong((long)key))) return -1;
4106 value=self->stack->data[len-1];
4107 len=PyDict_SetItem(self->memo, py_key, value);
4108 Py_DECREF(py_key);
4109 return len;
4113 static int
4114 load_long_binput(Unpicklerobject *self)
4116 PyObject *py_key = 0, *value = 0;
4117 long key;
4118 unsigned char c;
4119 char *s;
4120 int len;
4122 if (self->read_func(self, &s, 4) < 0) return -1;
4123 if (!( len=self->stack->length )) return stackUnderflow();
4125 c = (unsigned char)s[0];
4126 key = (long)c;
4127 c = (unsigned char)s[1];
4128 key |= (long)c << 8;
4129 c = (unsigned char)s[2];
4130 key |= (long)c << 16;
4131 c = (unsigned char)s[3];
4132 key |= (long)c << 24;
4134 if (!( py_key = PyInt_FromLong(key))) return -1;
4135 value=self->stack->data[len-1];
4136 len=PyDict_SetItem(self->memo, py_key, value);
4137 Py_DECREF(py_key);
4138 return len;
4142 static int
4143 do_append(Unpicklerobject *self, int x)
4145 PyObject *value = 0, *list = 0, *append_method = 0;
4146 int len, i;
4148 len=self->stack->length;
4149 if (!( len >= x && x > 0 )) return stackUnderflow();
4150 /* nothing to do */
4151 if (len==x) return 0;
4153 list=self->stack->data[x-1];
4155 if (PyList_Check(list)) {
4156 PyObject *slice;
4157 int list_len;
4159 slice=Pdata_popList(self->stack, x);
4160 list_len = PyList_GET_SIZE(list);
4161 i=PyList_SetSlice(list, list_len, list_len, slice);
4162 Py_DECREF(slice);
4163 return i;
4165 else {
4167 if (!( append_method = PyObject_GetAttr(list, append_str)))
4168 return -1;
4170 for (i = x; i < len; i++) {
4171 PyObject *junk;
4173 value=self->stack->data[i];
4174 junk=0;
4175 ARG_TUP(self, value);
4176 if (self->arg) {
4177 junk = PyObject_Call(append_method, self->arg,
4178 NULL);
4179 FREE_ARG_TUP(self);
4181 if (! junk) {
4182 Pdata_clear(self->stack, i+1);
4183 self->stack->length=x;
4184 Py_DECREF(append_method);
4185 return -1;
4187 Py_DECREF(junk);
4189 self->stack->length=x;
4190 Py_DECREF(append_method);
4193 return 0;
4197 static int
4198 load_append(Unpicklerobject *self)
4200 return do_append(self, self->stack->length - 1);
4204 static int
4205 load_appends(Unpicklerobject *self)
4207 return do_append(self, marker(self));
4211 static int
4212 do_setitems(Unpicklerobject *self, int x)
4214 PyObject *value = 0, *key = 0, *dict = 0;
4215 int len, i, r=0;
4217 if (!( (len=self->stack->length) >= x
4218 && x > 0 )) return stackUnderflow();
4220 dict=self->stack->data[x-1];
4222 for (i = x+1; i < len; i += 2) {
4223 key =self->stack->data[i-1];
4224 value=self->stack->data[i ];
4225 if (PyObject_SetItem(dict, key, value) < 0) {
4226 r=-1;
4227 break;
4231 Pdata_clear(self->stack, x);
4233 return r;
4237 static int
4238 load_setitem(Unpicklerobject *self)
4240 return do_setitems(self, self->stack->length - 2);
4243 static int
4244 load_setitems(Unpicklerobject *self)
4246 return do_setitems(self, marker(self));
4250 static int
4251 load_build(Unpicklerobject *self)
4253 PyObject *state, *inst, *slotstate;
4254 PyObject *__setstate__;
4255 PyObject *d_key, *d_value;
4256 int i;
4257 int res = -1;
4259 /* Stack is ... instance, state. We want to leave instance at
4260 * the stack top, possibly mutated via instance.__setstate__(state).
4262 if (self->stack->length < 2)
4263 return stackUnderflow();
4264 PDATA_POP(self->stack, state);
4265 if (state == NULL)
4266 return -1;
4267 inst = self->stack->data[self->stack->length - 1];
4269 __setstate__ = PyObject_GetAttr(inst, __setstate___str);
4270 if (__setstate__ != NULL) {
4271 PyObject *junk = NULL;
4273 /* The explicit __setstate__ is responsible for everything. */
4274 ARG_TUP(self, state);
4275 if (self->arg) {
4276 junk = PyObject_Call(__setstate__, self->arg, NULL);
4277 FREE_ARG_TUP(self);
4279 Py_DECREF(__setstate__);
4280 if (junk == NULL)
4281 return -1;
4282 Py_DECREF(junk);
4283 return 0;
4285 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4286 return -1;
4287 PyErr_Clear();
4289 /* A default __setstate__. First see whether state embeds a
4290 * slot state dict too (a proto 2 addition).
4292 if (PyTuple_Check(state) && PyTuple_Size(state) == 2) {
4293 PyObject *temp = state;
4294 state = PyTuple_GET_ITEM(temp, 0);
4295 slotstate = PyTuple_GET_ITEM(temp, 1);
4296 Py_INCREF(state);
4297 Py_INCREF(slotstate);
4298 Py_DECREF(temp);
4300 else
4301 slotstate = NULL;
4303 /* Set inst.__dict__ from the state dict (if any). */
4304 if (state != Py_None) {
4305 PyObject *dict;
4306 if (! PyDict_Check(state)) {
4307 PyErr_SetString(UnpicklingError, "state is not a "
4308 "dictionary");
4309 goto finally;
4311 dict = PyObject_GetAttr(inst, __dict___str);
4312 if (dict == NULL)
4313 goto finally;
4315 i = 0;
4316 while (PyDict_Next(state, &i, &d_key, &d_value)) {
4317 if (PyObject_SetItem(dict, d_key, d_value) < 0)
4318 goto finally;
4320 Py_DECREF(dict);
4323 /* Also set instance attributes from the slotstate dict (if any). */
4324 if (slotstate != NULL) {
4325 if (! PyDict_Check(slotstate)) {
4326 PyErr_SetString(UnpicklingError, "slot state is not "
4327 "a dictionary");
4328 goto finally;
4330 i = 0;
4331 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
4332 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
4333 goto finally;
4336 res = 0;
4338 finally:
4339 Py_DECREF(state);
4340 Py_XDECREF(slotstate);
4341 return res;
4345 static int
4346 load_mark(Unpicklerobject *self)
4348 int s;
4350 /* Note that we split the (pickle.py) stack into two stacks, an
4351 object stack and a mark stack. Here we push a mark onto the
4352 mark stack.
4355 if ((self->num_marks + 1) >= self->marks_size) {
4356 s=self->marks_size+20;
4357 if (s <= self->num_marks) s=self->num_marks + 1;
4358 if (self->marks == NULL)
4359 self->marks=(int *)malloc(s * sizeof(int));
4360 else
4361 self->marks=(int *)realloc(self->marks,
4362 s * sizeof(int));
4363 if (! self->marks) {
4364 PyErr_NoMemory();
4365 return -1;
4367 self->marks_size = s;
4370 self->marks[self->num_marks++] = self->stack->length;
4372 return 0;
4375 static int
4376 load_reduce(Unpicklerobject *self)
4378 PyObject *callable = 0, *arg_tup = 0, *ob = 0;
4380 PDATA_POP(self->stack, arg_tup);
4381 if (! arg_tup) return -1;
4382 PDATA_POP(self->stack, callable);
4383 if (callable) {
4384 ob = Instance_New(callable, arg_tup);
4385 Py_DECREF(callable);
4387 Py_DECREF(arg_tup);
4389 if (! ob) return -1;
4391 PDATA_PUSH(self->stack, ob, -1);
4392 return 0;
4395 /* Just raises an error if we don't know the protocol specified. PROTO
4396 * is the first opcode for protocols >= 2.
4398 static int
4399 load_proto(Unpicklerobject *self)
4401 int i;
4402 char *protobyte;
4404 i = self->read_func(self, &protobyte, 1);
4405 if (i < 0)
4406 return -1;
4408 i = calc_binint(protobyte, 1);
4409 /* No point checking for < 0, since calc_binint returns an unsigned
4410 * int when chewing on 1 byte.
4412 assert(i >= 0);
4413 if (i <= HIGHEST_PROTOCOL)
4414 return 0;
4416 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
4417 return -1;
4420 static PyObject *
4421 load(Unpicklerobject *self)
4423 PyObject *err = 0, *val = 0;
4424 char *s;
4426 self->num_marks = 0;
4427 if (self->stack->length) Pdata_clear(self->stack, 0);
4429 while (1) {
4430 if (self->read_func(self, &s, 1) < 0)
4431 break;
4433 switch (s[0]) {
4434 case NONE:
4435 if (load_none(self) < 0)
4436 break;
4437 continue;
4439 case BININT:
4440 if (load_binint(self) < 0)
4441 break;
4442 continue;
4444 case BININT1:
4445 if (load_binint1(self) < 0)
4446 break;
4447 continue;
4449 case BININT2:
4450 if (load_binint2(self) < 0)
4451 break;
4452 continue;
4454 case INT:
4455 if (load_int(self) < 0)
4456 break;
4457 continue;
4459 case LONG:
4460 if (load_long(self) < 0)
4461 break;
4462 continue;
4464 case LONG1:
4465 if (load_counted_long(self, 1) < 0)
4466 break;
4467 continue;
4469 case LONG4:
4470 if (load_counted_long(self, 4) < 0)
4471 break;
4472 continue;
4474 case FLOAT:
4475 if (load_float(self) < 0)
4476 break;
4477 continue;
4479 case BINFLOAT:
4480 if (load_binfloat(self) < 0)
4481 break;
4482 continue;
4484 case BINSTRING:
4485 if (load_binstring(self) < 0)
4486 break;
4487 continue;
4489 case SHORT_BINSTRING:
4490 if (load_short_binstring(self) < 0)
4491 break;
4492 continue;
4494 case STRING:
4495 if (load_string(self) < 0)
4496 break;
4497 continue;
4499 #ifdef Py_USING_UNICODE
4500 case UNICODE:
4501 if (load_unicode(self) < 0)
4502 break;
4503 continue;
4505 case BINUNICODE:
4506 if (load_binunicode(self) < 0)
4507 break;
4508 continue;
4509 #endif
4511 case EMPTY_TUPLE:
4512 if (load_counted_tuple(self, 0) < 0)
4513 break;
4514 continue;
4516 case TUPLE1:
4517 if (load_counted_tuple(self, 1) < 0)
4518 break;
4519 continue;
4521 case TUPLE2:
4522 if (load_counted_tuple(self, 2) < 0)
4523 break;
4524 continue;
4526 case TUPLE3:
4527 if (load_counted_tuple(self, 3) < 0)
4528 break;
4529 continue;
4531 case TUPLE:
4532 if (load_tuple(self) < 0)
4533 break;
4534 continue;
4536 case EMPTY_LIST:
4537 if (load_empty_list(self) < 0)
4538 break;
4539 continue;
4541 case LIST:
4542 if (load_list(self) < 0)
4543 break;
4544 continue;
4546 case EMPTY_DICT:
4547 if (load_empty_dict(self) < 0)
4548 break;
4549 continue;
4551 case DICT:
4552 if (load_dict(self) < 0)
4553 break;
4554 continue;
4556 case OBJ:
4557 if (load_obj(self) < 0)
4558 break;
4559 continue;
4561 case INST:
4562 if (load_inst(self) < 0)
4563 break;
4564 continue;
4566 case NEWOBJ:
4567 if (load_newobj(self) < 0)
4568 break;
4569 continue;
4571 case GLOBAL:
4572 if (load_global(self) < 0)
4573 break;
4574 continue;
4576 case APPEND:
4577 if (load_append(self) < 0)
4578 break;
4579 continue;
4581 case APPENDS:
4582 if (load_appends(self) < 0)
4583 break;
4584 continue;
4586 case BUILD:
4587 if (load_build(self) < 0)
4588 break;
4589 continue;
4591 case DUP:
4592 if (load_dup(self) < 0)
4593 break;
4594 continue;
4596 case BINGET:
4597 if (load_binget(self) < 0)
4598 break;
4599 continue;
4601 case LONG_BINGET:
4602 if (load_long_binget(self) < 0)
4603 break;
4604 continue;
4606 case GET:
4607 if (load_get(self) < 0)
4608 break;
4609 continue;
4611 case EXT1:
4612 if (load_extension(self, 1) < 0)
4613 break;
4614 continue;
4616 case EXT2:
4617 if (load_extension(self, 2) < 0)
4618 break;
4619 continue;
4621 case EXT4:
4622 if (load_extension(self, 4) < 0)
4623 break;
4624 continue;
4625 case MARK:
4626 if (load_mark(self) < 0)
4627 break;
4628 continue;
4630 case BINPUT:
4631 if (load_binput(self) < 0)
4632 break;
4633 continue;
4635 case LONG_BINPUT:
4636 if (load_long_binput(self) < 0)
4637 break;
4638 continue;
4640 case PUT:
4641 if (load_put(self) < 0)
4642 break;
4643 continue;
4645 case POP:
4646 if (load_pop(self) < 0)
4647 break;
4648 continue;
4650 case POP_MARK:
4651 if (load_pop_mark(self) < 0)
4652 break;
4653 continue;
4655 case SETITEM:
4656 if (load_setitem(self) < 0)
4657 break;
4658 continue;
4660 case SETITEMS:
4661 if (load_setitems(self) < 0)
4662 break;
4663 continue;
4665 case STOP:
4666 break;
4668 case PERSID:
4669 if (load_persid(self) < 0)
4670 break;
4671 continue;
4673 case BINPERSID:
4674 if (load_binpersid(self) < 0)
4675 break;
4676 continue;
4678 case REDUCE:
4679 if (load_reduce(self) < 0)
4680 break;
4681 continue;
4683 case PROTO:
4684 if (load_proto(self) < 0)
4685 break;
4686 continue;
4688 case NEWTRUE:
4689 if (load_bool(self, Py_True) < 0)
4690 break;
4691 continue;
4693 case NEWFALSE:
4694 if (load_bool(self, Py_False) < 0)
4695 break;
4696 continue;
4698 case '\0':
4699 /* end of file */
4700 PyErr_SetNone(PyExc_EOFError);
4701 break;
4703 default:
4704 cPickle_ErrFormat(UnpicklingError,
4705 "invalid load key, '%s'.",
4706 "c", s[0]);
4707 return NULL;
4710 break;
4713 if ((err = PyErr_Occurred())) {
4714 if (err == PyExc_EOFError) {
4715 PyErr_SetNone(PyExc_EOFError);
4717 return NULL;
4720 PDATA_POP(self->stack, val);
4721 return val;
4725 /* No-load functions to support noload, which is used to
4726 find persistent references. */
4728 static int
4729 noload_obj(Unpicklerobject *self)
4731 int i;
4733 if ((i = marker(self)) < 0) return -1;
4734 return Pdata_clear(self->stack, i+1);
4738 static int
4739 noload_inst(Unpicklerobject *self)
4741 int i;
4742 char *s;
4744 if ((i = marker(self)) < 0) return -1;
4745 Pdata_clear(self->stack, i);
4746 if (self->readline_func(self, &s) < 0) return -1;
4747 if (self->readline_func(self, &s) < 0) return -1;
4748 PDATA_APPEND(self->stack, Py_None, -1);
4749 return 0;
4752 static int
4753 noload_newobj(Unpicklerobject *self)
4755 PyObject *obj;
4757 PDATA_POP(self->stack, obj); /* pop argtuple */
4758 if (obj == NULL) return -1;
4759 Py_DECREF(obj);
4761 PDATA_POP(self->stack, obj); /* pop cls */
4762 if (obj == NULL) return -1;
4763 Py_DECREF(obj);
4765 PDATA_APPEND(self->stack, Py_None, -1);
4766 return 0;
4769 static int
4770 noload_global(Unpicklerobject *self)
4772 char *s;
4774 if (self->readline_func(self, &s) < 0) return -1;
4775 if (self->readline_func(self, &s) < 0) return -1;
4776 PDATA_APPEND(self->stack, Py_None,-1);
4777 return 0;
4780 static int
4781 noload_reduce(Unpicklerobject *self)
4784 if (self->stack->length < 2) return stackUnderflow();
4785 Pdata_clear(self->stack, self->stack->length-2);
4786 PDATA_APPEND(self->stack, Py_None,-1);
4787 return 0;
4790 static int
4791 noload_build(Unpicklerobject *self) {
4793 if (self->stack->length < 1) return stackUnderflow();
4794 Pdata_clear(self->stack, self->stack->length-1);
4795 return 0;
4798 static int
4799 noload_extension(Unpicklerobject *self, int nbytes)
4801 char *codebytes;
4803 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
4804 if (self->read_func(self, &codebytes, nbytes) < 0) return -1;
4805 PDATA_APPEND(self->stack, Py_None, -1);
4806 return 0;
4810 static PyObject *
4811 noload(Unpicklerobject *self)
4813 PyObject *err = 0, *val = 0;
4814 char *s;
4816 self->num_marks = 0;
4817 Pdata_clear(self->stack, 0);
4819 while (1) {
4820 if (self->read_func(self, &s, 1) < 0)
4821 break;
4823 switch (s[0]) {
4824 case NONE:
4825 if (load_none(self) < 0)
4826 break;
4827 continue;
4829 case BININT:
4830 if (load_binint(self) < 0)
4831 break;
4832 continue;
4834 case BININT1:
4835 if (load_binint1(self) < 0)
4836 break;
4837 continue;
4839 case BININT2:
4840 if (load_binint2(self) < 0)
4841 break;
4842 continue;
4844 case INT:
4845 if (load_int(self) < 0)
4846 break;
4847 continue;
4849 case LONG:
4850 if (load_long(self) < 0)
4851 break;
4852 continue;
4854 case LONG1:
4855 if (load_counted_long(self, 1) < 0)
4856 break;
4857 continue;
4859 case LONG4:
4860 if (load_counted_long(self, 4) < 0)
4861 break;
4862 continue;
4864 case FLOAT:
4865 if (load_float(self) < 0)
4866 break;
4867 continue;
4869 case BINFLOAT:
4870 if (load_binfloat(self) < 0)
4871 break;
4872 continue;
4874 case BINSTRING:
4875 if (load_binstring(self) < 0)
4876 break;
4877 continue;
4879 case SHORT_BINSTRING:
4880 if (load_short_binstring(self) < 0)
4881 break;
4882 continue;
4884 case STRING:
4885 if (load_string(self) < 0)
4886 break;
4887 continue;
4889 #ifdef Py_USING_UNICODE
4890 case UNICODE:
4891 if (load_unicode(self) < 0)
4892 break;
4893 continue;
4895 case BINUNICODE:
4896 if (load_binunicode(self) < 0)
4897 break;
4898 continue;
4899 #endif
4901 case EMPTY_TUPLE:
4902 if (load_counted_tuple(self, 0) < 0)
4903 break;
4904 continue;
4906 case TUPLE1:
4907 if (load_counted_tuple(self, 1) < 0)
4908 break;
4909 continue;
4911 case TUPLE2:
4912 if (load_counted_tuple(self, 2) < 0)
4913 break;
4914 continue;
4916 case TUPLE3:
4917 if (load_counted_tuple(self, 3) < 0)
4918 break;
4919 continue;
4921 case TUPLE:
4922 if (load_tuple(self) < 0)
4923 break;
4924 continue;
4926 case EMPTY_LIST:
4927 if (load_empty_list(self) < 0)
4928 break;
4929 continue;
4931 case LIST:
4932 if (load_list(self) < 0)
4933 break;
4934 continue;
4936 case EMPTY_DICT:
4937 if (load_empty_dict(self) < 0)
4938 break;
4939 continue;
4941 case DICT:
4942 if (load_dict(self) < 0)
4943 break;
4944 continue;
4946 case OBJ:
4947 if (noload_obj(self) < 0)
4948 break;
4949 continue;
4951 case INST:
4952 if (noload_inst(self) < 0)
4953 break;
4954 continue;
4956 case NEWOBJ:
4957 if (noload_newobj(self) < 0)
4958 break;
4959 continue;
4961 case GLOBAL:
4962 if (noload_global(self) < 0)
4963 break;
4964 continue;
4966 case APPEND:
4967 if (load_append(self) < 0)
4968 break;
4969 continue;
4971 case APPENDS:
4972 if (load_appends(self) < 0)
4973 break;
4974 continue;
4976 case BUILD:
4977 if (noload_build(self) < 0)
4978 break;
4979 continue;
4981 case DUP:
4982 if (load_dup(self) < 0)
4983 break;
4984 continue;
4986 case BINGET:
4987 if (load_binget(self) < 0)
4988 break;
4989 continue;
4991 case LONG_BINGET:
4992 if (load_long_binget(self) < 0)
4993 break;
4994 continue;
4996 case GET:
4997 if (load_get(self) < 0)
4998 break;
4999 continue;
5001 case EXT1:
5002 if (noload_extension(self, 1) < 0)
5003 break;
5004 continue;
5006 case EXT2:
5007 if (noload_extension(self, 2) < 0)
5008 break;
5009 continue;
5011 case EXT4:
5012 if (noload_extension(self, 4) < 0)
5013 break;
5014 continue;
5016 case MARK:
5017 if (load_mark(self) < 0)
5018 break;
5019 continue;
5021 case BINPUT:
5022 if (load_binput(self) < 0)
5023 break;
5024 continue;
5026 case LONG_BINPUT:
5027 if (load_long_binput(self) < 0)
5028 break;
5029 continue;
5031 case PUT:
5032 if (load_put(self) < 0)
5033 break;
5034 continue;
5036 case POP:
5037 if (load_pop(self) < 0)
5038 break;
5039 continue;
5041 case POP_MARK:
5042 if (load_pop_mark(self) < 0)
5043 break;
5044 continue;
5046 case SETITEM:
5047 if (load_setitem(self) < 0)
5048 break;
5049 continue;
5051 case SETITEMS:
5052 if (load_setitems(self) < 0)
5053 break;
5054 continue;
5056 case STOP:
5057 break;
5059 case PERSID:
5060 if (load_persid(self) < 0)
5061 break;
5062 continue;
5064 case BINPERSID:
5065 if (load_binpersid(self) < 0)
5066 break;
5067 continue;
5069 case REDUCE:
5070 if (noload_reduce(self) < 0)
5071 break;
5072 continue;
5074 case PROTO:
5075 if (load_proto(self) < 0)
5076 break;
5077 continue;
5079 case NEWTRUE:
5080 if (load_bool(self, Py_True) < 0)
5081 break;
5082 continue;
5084 case NEWFALSE:
5085 if (load_bool(self, Py_False) < 0)
5086 break;
5087 continue;
5088 default:
5089 cPickle_ErrFormat(UnpicklingError,
5090 "invalid load key, '%s'.",
5091 "c", s[0]);
5092 return NULL;
5095 break;
5098 if ((err = PyErr_Occurred())) {
5099 if (err == PyExc_EOFError) {
5100 PyErr_SetNone(PyExc_EOFError);
5102 return NULL;
5105 PDATA_POP(self->stack, val);
5106 return val;
5110 static PyObject *
5111 Unpickler_load(Unpicklerobject *self, PyObject *args)
5113 if (!( PyArg_ParseTuple(args, ":load")))
5114 return NULL;
5116 return load(self);
5119 static PyObject *
5120 Unpickler_noload(Unpicklerobject *self, PyObject *args)
5122 if (!( PyArg_ParseTuple(args, ":noload")))
5123 return NULL;
5125 return noload(self);
5129 static struct PyMethodDef Unpickler_methods[] = {
5130 {"load", (PyCFunction)Unpickler_load, METH_VARARGS,
5131 PyDoc_STR("load() -- Load a pickle")
5133 {"noload", (PyCFunction)Unpickler_noload, METH_VARARGS,
5134 PyDoc_STR(
5135 "noload() -- not load a pickle, but go through most of the motions\n"
5136 "\n"
5137 "This function can be used to read past a pickle without instantiating\n"
5138 "any objects or importing any modules. It can also be used to find all\n"
5139 "persistent references without instantiating any objects or importing\n"
5140 "any modules.\n")
5142 {NULL, NULL} /* sentinel */
5146 static Unpicklerobject *
5147 newUnpicklerobject(PyObject *f)
5149 Unpicklerobject *self;
5151 if (!( self = PyObject_GC_New(Unpicklerobject, &Unpicklertype)))
5152 return NULL;
5154 self->file = NULL;
5155 self->arg = NULL;
5156 self->stack = (Pdata*)Pdata_New();
5157 self->pers_func = NULL;
5158 self->last_string = NULL;
5159 self->marks = NULL;
5160 self->num_marks = 0;
5161 self->marks_size = 0;
5162 self->buf_size = 0;
5163 self->read = NULL;
5164 self->readline = NULL;
5165 self->find_class = NULL;
5167 if (!( self->memo = PyDict_New()))
5168 goto err;
5170 Py_INCREF(f);
5171 self->file = f;
5173 /* Set read, readline based on type of f */
5174 if (PyFile_Check(f)) {
5175 self->fp = PyFile_AsFile(f);
5176 if (self->fp == NULL) {
5177 PyErr_SetString(PyExc_ValueError,
5178 "I/O operation on closed file");
5179 goto err;
5181 self->read_func = read_file;
5182 self->readline_func = readline_file;
5184 else if (PycStringIO_InputCheck(f)) {
5185 self->fp = NULL;
5186 self->read_func = read_cStringIO;
5187 self->readline_func = readline_cStringIO;
5189 else {
5191 self->fp = NULL;
5192 self->read_func = read_other;
5193 self->readline_func = readline_other;
5195 if (!( (self->readline = PyObject_GetAttr(f, readline_str)) &&
5196 (self->read = PyObject_GetAttr(f, read_str)))) {
5197 PyErr_Clear();
5198 PyErr_SetString( PyExc_TypeError,
5199 "argument must have 'read' and "
5200 "'readline' attributes" );
5201 goto err;
5204 PyObject_GC_Track(self);
5206 return self;
5208 err:
5209 Py_DECREF((PyObject *)self);
5210 return NULL;
5214 static PyObject *
5215 get_Unpickler(PyObject *self, PyObject *args)
5217 PyObject *file;
5219 if (!( PyArg_ParseTuple(args, "O:Unpickler", &file)))
5220 return NULL;
5221 return (PyObject *)newUnpicklerobject(file);
5225 static void
5226 Unpickler_dealloc(Unpicklerobject *self)
5228 PyObject_GC_UnTrack((PyObject *)self);
5229 Py_XDECREF(self->readline);
5230 Py_XDECREF(self->read);
5231 Py_XDECREF(self->file);
5232 Py_XDECREF(self->memo);
5233 Py_XDECREF(self->stack);
5234 Py_XDECREF(self->pers_func);
5235 Py_XDECREF(self->arg);
5236 Py_XDECREF(self->last_string);
5237 Py_XDECREF(self->find_class);
5239 if (self->marks) {
5240 free(self->marks);
5243 if (self->buf_size) {
5244 free(self->buf);
5247 self->ob_type->tp_free((PyObject *)self);
5250 static int
5251 Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
5253 int err;
5255 #define VISIT(SLOT) \
5256 if (SLOT) { \
5257 err = visit((PyObject *)(SLOT), arg); \
5258 if (err) \
5259 return err; \
5261 VISIT(self->readline);
5262 VISIT(self->read);
5263 VISIT(self->file);
5264 VISIT(self->memo);
5265 VISIT(self->stack);
5266 VISIT(self->pers_func);
5267 VISIT(self->arg);
5268 VISIT(self->last_string);
5269 VISIT(self->find_class);
5270 #undef VISIT
5271 return 0;
5274 static int
5275 Unpickler_clear(Unpicklerobject *self)
5277 #define CLEAR(SLOT) Py_XDECREF(SLOT); SLOT = NULL
5278 CLEAR(self->readline);
5279 CLEAR(self->read);
5280 CLEAR(self->file);
5281 CLEAR(self->memo);
5282 CLEAR(self->stack);
5283 CLEAR(self->pers_func);
5284 CLEAR(self->arg);
5285 CLEAR(self->last_string);
5286 CLEAR(self->find_class);
5287 #undef CLEAR
5288 return 0;
5291 static PyObject *
5292 Unpickler_getattr(Unpicklerobject *self, char *name)
5294 if (!strcmp(name, "persistent_load")) {
5295 if (!self->pers_func) {
5296 PyErr_SetString(PyExc_AttributeError, name);
5297 return NULL;
5300 Py_INCREF(self->pers_func);
5301 return self->pers_func;
5304 if (!strcmp(name, "find_global")) {
5305 if (!self->find_class) {
5306 PyErr_SetString(PyExc_AttributeError, name);
5307 return NULL;
5310 Py_INCREF(self->find_class);
5311 return self->find_class;
5314 if (!strcmp(name, "memo")) {
5315 if (!self->memo) {
5316 PyErr_SetString(PyExc_AttributeError, name);
5317 return NULL;
5320 Py_INCREF(self->memo);
5321 return self->memo;
5324 if (!strcmp(name, "UnpicklingError")) {
5325 Py_INCREF(UnpicklingError);
5326 return UnpicklingError;
5329 return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
5333 static int
5334 Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value)
5337 if (!strcmp(name, "persistent_load")) {
5338 Py_XDECREF(self->pers_func);
5339 self->pers_func = value;
5340 Py_XINCREF(value);
5341 return 0;
5344 if (!strcmp(name, "find_global")) {
5345 Py_XDECREF(self->find_class);
5346 self->find_class = value;
5347 Py_XINCREF(value);
5348 return 0;
5351 if (! value) {
5352 PyErr_SetString(PyExc_TypeError,
5353 "attribute deletion is not supported");
5354 return -1;
5357 if (strcmp(name, "memo") == 0) {
5358 if (!PyDict_Check(value)) {
5359 PyErr_SetString(PyExc_TypeError,
5360 "memo must be a dictionary");
5361 return -1;
5363 Py_XDECREF(self->memo);
5364 self->memo = value;
5365 Py_INCREF(value);
5366 return 0;
5369 PyErr_SetString(PyExc_AttributeError, name);
5370 return -1;
5373 /* ---------------------------------------------------------------------------
5374 * Module-level functions.
5377 /* dump(obj, file, protocol=0). */
5378 static PyObject *
5379 cpm_dump(PyObject *self, PyObject *args, PyObject *kwds)
5381 static const char *kwlist[] = {"obj", "file", "protocol", NULL};
5382 PyObject *ob, *file, *res = NULL;
5383 Picklerobject *pickler = 0;
5384 int proto = 0;
5386 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "OO|i", kwlist,
5387 &ob, &file, &proto)))
5388 goto finally;
5390 if (!( pickler = newPicklerobject(file, proto)))
5391 goto finally;
5393 if (dump(pickler, ob) < 0)
5394 goto finally;
5396 Py_INCREF(Py_None);
5397 res = Py_None;
5399 finally:
5400 Py_XDECREF(pickler);
5402 return res;
5406 /* dumps(obj, protocol=0). */
5407 static PyObject *
5408 cpm_dumps(PyObject *self, PyObject *args, PyObject *kwds)
5410 static const char *kwlist[] = {"obj", "protocol", NULL};
5411 PyObject *ob, *file = 0, *res = NULL;
5412 Picklerobject *pickler = 0;
5413 int proto = 0;
5415 if (!( PyArg_ParseTupleAndKeywords(args, kwds, "O|i:dumps", kwlist,
5416 &ob, &proto)))
5417 goto finally;
5419 if (!( file = PycStringIO->NewOutput(128)))
5420 goto finally;
5422 if (!( pickler = newPicklerobject(file, proto)))
5423 goto finally;
5425 if (dump(pickler, ob) < 0)
5426 goto finally;
5428 res = PycStringIO->cgetvalue(file);
5430 finally:
5431 Py_XDECREF(pickler);
5432 Py_XDECREF(file);
5434 return res;
5438 /* load(fileobj). */
5439 static PyObject *
5440 cpm_load(PyObject *self, PyObject *args)
5442 Unpicklerobject *unpickler = 0;
5443 PyObject *ob, *res = NULL;
5445 if (!( PyArg_ParseTuple(args, "O:load", &ob)))
5446 goto finally;
5448 if (!( unpickler = newUnpicklerobject(ob)))
5449 goto finally;
5451 res = load(unpickler);
5453 finally:
5454 Py_XDECREF(unpickler);
5456 return res;
5460 /* loads(string) */
5461 static PyObject *
5462 cpm_loads(PyObject *self, PyObject *args)
5464 PyObject *ob, *file = 0, *res = NULL;
5465 Unpicklerobject *unpickler = 0;
5467 if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5468 goto finally;
5470 if (!( file = PycStringIO->NewInput(ob)))
5471 goto finally;
5473 if (!( unpickler = newUnpicklerobject(file)))
5474 goto finally;
5476 res = load(unpickler);
5478 finally:
5479 Py_XDECREF(file);
5480 Py_XDECREF(unpickler);
5482 return res;
5486 PyDoc_STRVAR(Unpicklertype__doc__,
5487 "Objects that know how to unpickle");
5489 static PyTypeObject Unpicklertype = {
5490 PyObject_HEAD_INIT(NULL)
5491 0, /*ob_size*/
5492 "cPickle.Unpickler", /*tp_name*/
5493 sizeof(Unpicklerobject), /*tp_basicsize*/
5495 (destructor)Unpickler_dealloc, /* tp_dealloc */
5496 0, /* tp_print */
5497 (getattrfunc)Unpickler_getattr, /* tp_getattr */
5498 (setattrfunc)Unpickler_setattr, /* tp_setattr */
5499 0, /* tp_compare */
5500 0, /* tp_repr */
5501 0, /* tp_as_number */
5502 0, /* tp_as_sequence */
5503 0, /* tp_as_mapping */
5504 0, /* tp_hash */
5505 0, /* tp_call */
5506 0, /* tp_str */
5507 0, /* tp_getattro */
5508 0, /* tp_setattro */
5509 0, /* tp_as_buffer */
5510 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
5511 Unpicklertype__doc__, /* tp_doc */
5512 (traverseproc)Unpickler_traverse, /* tp_traverse */
5513 (inquiry)Unpickler_clear, /* tp_clear */
5516 static struct PyMethodDef cPickle_methods[] = {
5517 {"dump", (PyCFunction)cpm_dump, METH_VARARGS | METH_KEYWORDS,
5518 PyDoc_STR("dump(obj, file, protocol=0) -- "
5519 "Write an object in pickle format to the given file.\n"
5520 "\n"
5521 "See the Pickler docstring for the meaning of optional argument proto.")
5524 {"dumps", (PyCFunction)cpm_dumps, METH_VARARGS | METH_KEYWORDS,
5525 PyDoc_STR("dumps(obj, protocol=0) -- "
5526 "Return a string containing an object in pickle format.\n"
5527 "\n"
5528 "See the Pickler docstring for the meaning of optional argument proto.")
5531 {"load", (PyCFunction)cpm_load, METH_VARARGS,
5532 PyDoc_STR("load(file) -- Load a pickle from the given file")},
5534 {"loads", (PyCFunction)cpm_loads, METH_VARARGS,
5535 PyDoc_STR("loads(string) -- Load a pickle from the given string")},
5537 {"Pickler", (PyCFunction)get_Pickler, METH_VARARGS | METH_KEYWORDS,
5538 PyDoc_STR("Pickler(file, protocol=0) -- Create a pickler.\n"
5539 "\n"
5540 "This takes a file-like object for writing a pickle data stream.\n"
5541 "The optional proto argument tells the pickler to use the given\n"
5542 "protocol; supported protocols are 0, 1, 2. The default\n"
5543 "protocol is 0, to be backwards compatible. (Protocol 0 is the\n"
5544 "only protocol that can be written to a file opened in text\n"
5545 "mode and read back successfully. When using a protocol higher\n"
5546 "than 0, make sure the file is opened in binary mode, both when\n"
5547 "pickling and unpickling.)\n"
5548 "\n"
5549 "Protocol 1 is more efficient than protocol 0; protocol 2 is\n"
5550 "more efficient than protocol 1.\n"
5551 "\n"
5552 "Specifying a negative protocol version selects the highest\n"
5553 "protocol version supported. The higher the protocol used, the\n"
5554 "more recent the version of Python needed to read the pickle\n"
5555 "produced.\n"
5556 "\n"
5557 "The file parameter must have a write() method that accepts a single\n"
5558 "string argument. It can thus be an open file object, a StringIO\n"
5559 "object, or any other custom object that meets this interface.\n")
5562 {"Unpickler", (PyCFunction)get_Unpickler, METH_VARARGS,
5563 PyDoc_STR("Unpickler(file) -- Create an unpickler.")},
5565 { NULL, NULL }
5568 static int
5569 init_stuff(PyObject *module_dict)
5571 PyObject *copy_reg, *t, *r;
5573 #define INIT_STR(S) if (!( S ## _str=PyString_InternFromString(#S))) return -1;
5575 if (PyType_Ready(&Unpicklertype) < 0)
5576 return -1;
5577 if (PyType_Ready(&Picklertype) < 0)
5578 return -1;
5580 INIT_STR(__class__);
5581 INIT_STR(__getinitargs__);
5582 INIT_STR(__dict__);
5583 INIT_STR(__getstate__);
5584 INIT_STR(__setstate__);
5585 INIT_STR(__name__);
5586 INIT_STR(__main__);
5587 INIT_STR(__reduce__);
5588 INIT_STR(__reduce_ex__);
5589 INIT_STR(write);
5590 INIT_STR(append);
5591 INIT_STR(read);
5592 INIT_STR(readline);
5593 INIT_STR(copy_reg);
5594 INIT_STR(dispatch_table);
5595 INIT_STR(__basicnew__);
5597 if (!( copy_reg = PyImport_ImportModule("copy_reg")))
5598 return -1;
5600 /* This is special because we want to use a different
5601 one in restricted mode. */
5602 dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str);
5603 if (!dispatch_table) return -1;
5605 extension_registry = PyObject_GetAttrString(copy_reg,
5606 "_extension_registry");
5607 if (!extension_registry) return -1;
5609 inverted_registry = PyObject_GetAttrString(copy_reg,
5610 "_inverted_registry");
5611 if (!inverted_registry) return -1;
5613 extension_cache = PyObject_GetAttrString(copy_reg,
5614 "_extension_cache");
5615 if (!extension_cache) return -1;
5617 Py_DECREF(copy_reg);
5619 if (!(empty_tuple = PyTuple_New(0)))
5620 return -1;
5622 two_tuple = PyTuple_New(2);
5623 if (two_tuple == NULL)
5624 return -1;
5625 /* We use this temp container with no regard to refcounts, or to
5626 * keeping containees alive. Exempt from GC, because we don't
5627 * want anything looking at two_tuple() by magic.
5629 PyObject_GC_UnTrack(two_tuple);
5631 /* Ugh */
5632 if (!( t=PyImport_ImportModule("__builtin__"))) return -1;
5633 if (PyDict_SetItemString(module_dict, "__builtins__", t) < 0)
5634 return -1;
5636 if (!( t=PyDict_New())) return -1;
5637 if (!( r=PyRun_String(
5638 "def __init__(self, *args): self.args=args\n\n"
5639 "def __str__(self):\n"
5640 " return self.args and ('%s' % self.args[0]) or '(what)'\n",
5641 Py_file_input,
5642 module_dict, t) )) return -1;
5643 Py_DECREF(r);
5645 PickleError = PyErr_NewException("cPickle.PickleError", NULL, t);
5646 if (!PickleError)
5647 return -1;
5649 Py_DECREF(t);
5651 PicklingError = PyErr_NewException("cPickle.PicklingError",
5652 PickleError, NULL);
5653 if (!PicklingError)
5654 return -1;
5656 if (!( t=PyDict_New())) return -1;
5657 if (!( r=PyRun_String(
5658 "def __init__(self, *args): self.args=args\n\n"
5659 "def __str__(self):\n"
5660 " a=self.args\n"
5661 " a=a and type(a[0]) or '(what)'\n"
5662 " return 'Cannot pickle %s objects' % a\n"
5663 , Py_file_input,
5664 module_dict, t) )) return -1;
5665 Py_DECREF(r);
5667 if (!( UnpickleableError = PyErr_NewException(
5668 "cPickle.UnpickleableError", PicklingError, t)))
5669 return -1;
5671 Py_DECREF(t);
5673 if (!( UnpicklingError = PyErr_NewException("cPickle.UnpicklingError",
5674 PickleError, NULL)))
5675 return -1;
5677 if (!( BadPickleGet = PyErr_NewException("cPickle.BadPickleGet",
5678 UnpicklingError, NULL)))
5679 return -1;
5681 if (PyDict_SetItemString(module_dict, "PickleError",
5682 PickleError) < 0)
5683 return -1;
5685 if (PyDict_SetItemString(module_dict, "PicklingError",
5686 PicklingError) < 0)
5687 return -1;
5689 if (PyDict_SetItemString(module_dict, "UnpicklingError",
5690 UnpicklingError) < 0)
5691 return -1;
5693 if (PyDict_SetItemString(module_dict, "UnpickleableError",
5694 UnpickleableError) < 0)
5695 return -1;
5697 if (PyDict_SetItemString(module_dict, "BadPickleGet",
5698 BadPickleGet) < 0)
5699 return -1;
5701 PycString_IMPORT;
5703 return 0;
5706 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
5707 #define PyMODINIT_FUNC void
5708 #endif
5709 PyMODINIT_FUNC
5710 initcPickle(void)
5712 PyObject *m, *d, *di, *v, *k;
5713 int i;
5714 char *rev = "1.71"; /* XXX when does this change? */
5715 PyObject *format_version;
5716 PyObject *compatible_formats;
5718 Picklertype.ob_type = &PyType_Type;
5719 Unpicklertype.ob_type = &PyType_Type;
5720 PdataType.ob_type = &PyType_Type;
5722 /* Initialize some pieces. We need to do this before module creation,
5723 * so we're forced to use a temporary dictionary. :(
5725 di = PyDict_New();
5726 if (!di) return;
5727 if (init_stuff(di) < 0) return;
5729 /* Create the module and add the functions */
5730 m = Py_InitModule4("cPickle", cPickle_methods,
5731 cPickle_module_documentation,
5732 (PyObject*)NULL,PYTHON_API_VERSION);
5733 if (m == NULL)
5734 return;
5736 /* Add some symbolic constants to the module */
5737 d = PyModule_GetDict(m);
5738 v = PyString_FromString(rev);
5739 PyDict_SetItemString(d, "__version__", v);
5740 Py_XDECREF(v);
5742 /* Copy data from di. Waaa. */
5743 for (i=0; PyDict_Next(di, &i, &k, &v); ) {
5744 if (PyObject_SetItem(d, k, v) < 0) {
5745 Py_DECREF(di);
5746 return;
5749 Py_DECREF(di);
5751 i = PyModule_AddIntConstant(m, "HIGHEST_PROTOCOL", HIGHEST_PROTOCOL);
5752 if (i < 0)
5753 return;
5755 /* These are purely informational; no code uses them. */
5756 /* File format version we write. */
5757 format_version = PyString_FromString("2.0");
5758 /* Format versions we can read. */
5759 compatible_formats = Py_BuildValue("[sssss]",
5760 "1.0", /* Original protocol 0 */
5761 "1.1", /* Protocol 0 + INST */
5762 "1.2", /* Original protocol 1 */
5763 "1.3", /* Protocol 1 + BINFLOAT */
5764 "2.0"); /* Original protocol 2 */
5765 PyDict_SetItemString(d, "format_version", format_version);
5766 PyDict_SetItemString(d, "compatible_formats", compatible_formats);
5767 Py_XDECREF(format_version);
5768 Py_XDECREF(compatible_formats);